.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_examples\indexes\plot_sari_b_clustermap.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end <sphx_glr_download__examples_indexes_plot_sari_b_clustermap.py>` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr__examples_indexes_plot_sari_b_clustermap.py: ``SARI`` - By specimen w/ ``sns.clustermap`` ------------------------------------------------ .. todo:: Explain... .. GENERATED FROM PYTHON SOURCE LINES 8-242 .. rst-class:: sphx-glr-horizontal * .. image-sg:: /_examples/indexes/images/sphx_glr_plot_sari_b_clustermap_001.png :alt: Antibiogram (clustered) - BLDCUL :srcset: /_examples/indexes/images/sphx_glr_plot_sari_b_clustermap_001.png :class: sphx-glr-multi-img * .. image-sg:: /_examples/indexes/images/sphx_glr_plot_sari_b_clustermap_002.png :alt: Antibiogram (clustered) - SPTCUL :srcset: /_examples/indexes/images/sphx_glr_plot_sari_b_clustermap_002.png :class: sphx-glr-multi-img * .. image-sg:: /_examples/indexes/images/sphx_glr_plot_sari_b_clustermap_003.png :alt: Antibiogram (clustered) - URICUL :srcset: /_examples/indexes/images/sphx_glr_plot_sari_b_clustermap_003.png :class: sphx-glr-multi-img * .. image-sg:: /_examples/indexes/images/sphx_glr_plot_sari_b_clustermap_004.png :alt: Antibiogram (clustered) - WOUCUL :srcset: /_examples/indexes/images/sphx_glr_plot_sari_b_clustermap_004.png :class: sphx-glr-multi-img * .. image-sg:: /_examples/indexes/images/sphx_glr_plot_sari_b_clustermap_005.png :alt: Antibiogram (clustered) - XINCUL :srcset: /_examples/indexes/images/sphx_glr_plot_sari_b_clustermap_005.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none Data: date_received date_outcome patient_id laboratory_number specimen_code specimen_name specimen_description ... microorganism_name antimicrobial_code antimicrobial_name sensitivity_method sensitivity mic reported 0 2009-01-03 NaN 20091 X428501 BLDCUL NaN blood ... klebsiella AAMI amikacin NaN sensitive NaN NaN 1 2009-01-03 NaN 20091 X428501 BLDCUL NaN blood ... klebsiella AAMO amoxycillin NaN resistant NaN NaN 2 2009-01-03 NaN 20091 X428501 BLDCUL NaN blood ... klebsiella AAUG augmentin NaN sensitive NaN NaN 3 2009-01-03 NaN 20091 X428501 BLDCUL NaN blood ... klebsiella AAZT aztreonam NaN sensitive NaN NaN 4 2009-01-03 NaN 20091 X428501 BLDCUL NaN blood ... klebsiella ACAZ ceftazidime NaN sensitive NaN NaN ... ... ... ... ... ... ... ... ... ... ... ... ... ... .. ... 319117 2009-12-31 NaN 24645 H2012337 BLDCUL NaN blood ... enterococcus AAMO amoxycillin NaN sensitive NaN NaN 319118 2009-12-31 NaN 24645 H2012337 BLDCUL NaN blood ... enterococcus ALIN linezolid NaN sensitive NaN NaN 319119 2009-12-31 NaN 24645 H2012337 BLDCUL NaN blood ... enterococcus ASYN synercid NaN resistant NaN NaN 319120 2009-12-31 NaN 24645 H2012337 BLDCUL NaN blood ... enterococcus ATEI teicoplanin NaN sensitive NaN NaN 319121 2009-12-31 NaN 24645 H2012337 BLDCUL NaN blood ... enterococcus AVAN vancomycin NaN sensitive NaN NaN [319122 rows x 15 columns] Columns: Index(['date_received', 'date_outcome', 'patient_id', 'laboratory_number', 'specimen_code', 'specimen_name', 'specimen_description', 'microorganism_code', 'microorganism_name', 'antimicrobial_code', 'antimicrobial_name', 'sensitivity_method', 'sensitivity', 'mic', 'reported'], dtype='object') SARI (overall): intermediate resistant sensitive freq sari specimen_code microorganism_name antimicrobial_name BFLCUL anaerobes metronidazole 0.0 0.0 1.0 1.0 0.0000 bacillus ciprofloxacin 0.0 0.0 1.0 1.0 0.0000 clindamycin 0.0 3.0 1.0 4.0 0.7500 erythromycin 0.0 1.0 3.0 4.0 0.2500 fusidic acid 0.0 3.0 1.0 4.0 0.7500 ... ... ... ... ... ... XINCUL streptococcus beta-haemolytic group b cephalexin 0.0 1.0 0.0 1.0 1.0000 clindamycin 0.0 1.0 8.0 9.0 0.1111 erythromycin 0.0 1.0 8.0 9.0 0.1111 penicillin 0.0 0.0 9.0 9.0 0.0000 tetracycline 0.0 8.0 1.0 9.0 0.8889 [4491 rows x 5 columns] specimen_code object microorganism_name object antimicrobial_name object intermediate float64 resistant float64 sensitive float64 freq float64 sari float64 genus object species object domain object phylum object class object order object family object acronym_x object gram_stain object exists_in_registry bool name object category object acronym_y object dtype: object Cultures: specimen_code URICUL 116627.0 WOUCUL 94918.0 XINCUL 21427.0 SPTCUL 21113.0 BLDCUL 20333.0 ENTCUL 13110.0 T&FCUL 8150.0 MRSCUL 7865.0 VAGCUL 7425.0 EYECUL 2839.0 GUMCUL 1634.0 FAECUL 1317.0 URECUL 802.0 TISCUL 474.0 BFLCUL 450.0 SEMCUL 290.0 NEOCUL 213.0 PDFCUL 68.0 CSFCUL 32.0 RGNS 20.0 FUNSTC 14.0 TBCUL 1.0 Name: freq, dtype: float64 Data (BLDCUL) antimicrobial_name Amikacin Amoxycillin Aztreonam Cefotaxime Cefoxitin ... Temocillin Tetracycline Tigecycline Tobramycin Vancomycin category Aminoglycosides Aminopenicillins Monobactams Cephalosporins Cephalosporins ... Penicillins Tetracyclines Tetracyclines Aminoglycosides Glycopeptide microorganism_name genus ... achromobacter Achromobacter 0 0 0 0 0 ... 0 0 0 0 0 achromobacter xylosoxidans Achromobacter 100 100 100 100 100 ... 0 0 0 100 0 acinetobacter Acinetobacter 10 6 87 33 33 ... 80 0 0 25 0 acinetobacter baumannii Acinetobacter 14 77 100 77 77 ... 100 0 0 0 0 aerococcus Aerococcus 0 0 0 0 0 ... 0 0 0 0 0 aeromonas Aeromonas 0 100 0 0 0 ... 0 0 0 0 0 anaerobes Anaerobes 0 0 0 0 0 ... 0 0 0 0 0 bacillus Bacillus 0 0 100 0 0 ... 0 0 0 0 0 burkholderia cepacia Burkholderia 0 100 0 0 100 ... 0 0 0 0 0 campylobacter Campylobacter 0 0 0 0 0 ... 0 100 0 0 0 chryseobacterium Chryseobacterium 0 100 100 100 100 ... 100 0 0 0 0 citrobacter Citrobacter 33 100 50 40 40 ... 0 0 0 50 0 coliform Coliform 0 100 25 25 18 ... 28 0 0 0 0 comamonas testosteroni Comamonas 0 0 0 0 0 ... 0 0 0 0 0 corynebacterium Corynebacterium 0 0 0 0 0 ... 0 12 0 0 0 enterobacter Enterobacter 0 96 15 32 90 ... 0 0 0 15 0 enterococcus Enterococcus 0 60 0 100 0 ... 0 75 0 0 35 escherichia coli Escherichia 4 76 25 23 12 ... 0 0 0 72 0 gemella morbillorum Gemella 0 0 0 0 0 ... 0 0 0 0 0 haemophilus Haemophilus 0 50 0 0 0 ... 0 0 0 0 0 haemophilus influenzae Haemophilus 0 20 0 0 0 ... 0 0 0 0 0 klebsiella Klebsiella 6 99 30 16 10 ... 3 0 0 54 0 lactobacillus Lactobacillus 0 0 0 0 0 ... 0 0 0 0 100 leuconostoc Leuconostoc 0 0 0 0 0 ... 0 0 0 0 100 listeria monocytogenes Listeria 0 0 0 0 0 ... 0 0 0 0 0 micrococcus Micrococcus 0 0 0 0 0 ... 0 0 0 0 0 moraxella catarrhalis Moraxella 0 100 0 0 0 ... 0 0 0 0 0 morganella morganii Morganella 0 100 50 33 33 ... 0 0 0 0 0 neisseria Neisseria 0 0 0 0 0 ... 0 0 0 0 0 neisseria meningitidis Neisseria 0 0 0 0 0 ... 0 0 0 0 0 proteus Proteus 5 50 0 0 0 ... 0 0 0 33 0 providencia Providencia 0 100 0 0 0 ... 0 0 0 0 0 pseudomonas Pseudomonas 20 33 66 33 33 ... 0 0 0 22 0 pseudomonas aeruginosa Pseudomonas 9 100 27 100 100 ... 0 0 0 12 0 ralstonia pickettii Ralstonia 0 100 0 100 100 ... 0 0 0 0 0 raoultella Raoultella 0 100 0 0 0 ... 0 0 0 0 0 rhizobium radiobacter Rhizobium 0 0 0 0 0 ... 0 0 0 100 0 salmonella Salmonella 0 0 0 0 0 ... 0 0 0 0 0 serratia Serratia 0 90 33 30 50 ... 0 0 0 100 0 staphylococcus aureus Staphylococcus 0 0 0 0 0 ... 0 16 0 0 0 staphylococcus coagulase negative Staphylococcus 0 0 0 0 0 ... 0 23 0 0 0 stenotrophomonas maltophilia Stenotrophomonas 0 0 0 0 0 ... 0 0 0 0 0 streptococcus alpha-haemolytic Streptococcus 0 2 0 2 0 ... 0 27 0 0 0 streptococcus anginosus Streptococcus 0 0 0 0 0 ... 0 0 0 0 0 streptococcus beta-haemolytic group a Streptococcus 0 0 0 0 0 ... 0 9 0 0 0 streptococcus beta-haemolytic group b Streptococcus 0 0 0 0 0 ... 0 100 0 0 0 streptococcus beta-haemolytic group c/g Streptococcus 0 0 0 0 0 ... 0 16 0 0 0 streptococcus milleri Streptococcus 0 0 0 0 0 ... 0 50 0 0 0 streptococcus pneumoniae Streptococcus 0 0 0 0 0 ... 0 4 0 0 0 [49 rows x 32 columns] Data (SPTCUL) antimicrobial_name Amikacin Amoxycillin Azithromycin Aztreonam Cefotaxime ... Teicoplanin Temocillin Tetracycline Tobramycin Vancomycin category Aminoglycosides Aminopenicillins Macrolides Monobactams Cephalosporins ... Glycopeptide Penicillins Tetracyclines Aminoglycosides Glycopeptide microorganism_name genus ... acinetobacter Acinetobacter 50 100 0 100 66 ... 0 100 0 100 0 acinetobacter baumannii Acinetobacter 56 100 0 100 100 ... 0 100 0 66 0 aeromonas Aeromonas 0 100 0 0 0 ... 0 0 0 0 0 anaerobes Anaerobes 0 0 0 0 0 ... 0 0 0 0 0 arcanobacterium haemolyticum Arcanobacterium 0 0 0 0 0 ... 0 0 100 0 0 burkholderia cepacia Burkholderia 50 0 0 0 0 ... 0 0 0 100 0 citrobacter Citrobacter 0 94 0 0 11 ... 0 0 0 0 0 coliform Coliform 3 83 0 11 11 ... 0 3 0 0 0 enterobacter Enterobacter 0 95 0 12 21 ... 0 16 0 9 0 enterococcus Enterococcus 0 51 0 0 0 ... 29 0 100 0 29 escherichia coli Escherichia 2 84 0 5 22 ... 0 6 0 22 0 haemophilus Haemophilus 0 30 0 0 0 ... 0 0 14 0 0 haemophilus influenzae Haemophilus 0 25 100 0 0 ... 0 0 3 0 0 hafnia alvei Hafnia 0 75 0 0 0 ... 0 0 0 0 0 klebsiella Klebsiella 3 98 0 40 19 ... 0 100 0 14 0 kluyvera Kluyvera 0 100 0 0 0 ... 0 0 0 0 0 moraxella catarrhalis Moraxella 0 88 0 0 0 ... 0 0 0 0 0 morganella morganii Morganella 0 100 0 0 0 ... 0 0 0 0 0 proteus Proteus 11 27 0 16 5 ... 0 0 0 0 0 pseudomonas Pseudomonas 8 100 0 38 66 ... 0 0 0 18 0 pseudomonas aeruginosa Pseudomonas 1 0 0 26 0 ... 0 0 0 10 0 serratia Serratia 0 100 0 0 5 ... 0 0 0 0 0 staphylococcus aureus Staphylococcus 0 0 0 0 0 ... 0 0 5 0 0 staphylococcus coagulase negative Staphylococcus 0 0 0 0 0 ... 0 0 28 0 0 stenotrophomonas maltophilia Stenotrophomonas 0 100 0 100 100 ... 0 100 0 0 0 streptococcus beta-haemolytic group a Streptococcus 0 0 0 0 0 ... 0 0 0 0 0 streptococcus beta-haemolytic group b Streptococcus 0 0 0 0 0 ... 0 0 100 0 0 streptococcus beta-haemolytic group c/g Streptococcus 0 0 0 0 0 ... 0 0 60 0 0 streptococcus pneumoniae Streptococcus 0 0 100 0 0 ... 0 0 24 0 0 [29 rows x 31 columns] Data (URICUL) antimicrobial_name Amikacin Amoxycillin Aztreonam Cefotaxime Cefoxitin ... Teicoplanin Temocillin Tetracycline Tobramycin Vancomycin category Aminoglycosides Aminopenicillins Monobactams Cephalosporins Cephalosporins ... Glycopeptide Penicillins Tetracyclines Aminoglycosides Glycopeptide microorganism_name genus ... acinetobacter Acinetobacter 0 0 100 100 100 ... 0 0 0 0 0 acinetobacter baumannii Acinetobacter 50 100 100 100 100 ... 0 100 0 0 0 anaerobes Anaerobes 0 0 0 0 0 ... 0 0 0 0 0 citrobacter Citrobacter 0 0 0 57 100 ... 0 0 0 33 0 coliform Coliform 3 70 100 40 46 ... 0 6 0 28 0 enterobacter Enterobacter 0 100 100 57 97 ... 0 0 0 18 0 enterococcus Enterococcus 0 2 0 0 0 ... 1 0 78 0 1 escherichia coli Escherichia 0 33 0 66 27 ... 0 6 0 54 0 haemophilus Haemophilus 0 0 0 0 0 ... 0 0 0 0 0 hafnia Hafnia 0 0 0 100 0 ... 0 0 0 0 0 klebsiella Klebsiella 2 100 75 83 19 ... 0 0 0 76 0 morganella morganii Morganella 0 0 0 18 31 ... 0 0 0 0 0 neisseria Neisseria 0 0 0 0 0 ... 0 0 0 0 0 neisseria gonorrhoeae Neisseria 0 0 0 0 0 ... 0 0 0 0 0 proteus Proteus 1 100 0 17 7 ... 0 0 0 13 0 pseudomonas Pseudomonas 6 0 39 100 50 ... 0 0 0 20 0 pseudomonas aeruginosa Pseudomonas 0 0 21 100 100 ... 0 0 0 8 0 serratia Serratia 0 0 0 13 80 ... 0 0 0 0 0 staphylococcus aureus Staphylococcus 0 66 0 0 0 ... 0 0 1 0 0 staphylococcus coagulase negative Staphylococcus 0 44 0 0 0 ... 0 0 25 0 0 staphylococcus saprophyticus Staphylococcus 0 8 0 0 0 ... 0 0 25 0 0 stenotrophomonas maltophilia Stenotrophomonas 0 0 0 100 100 ... 0 0 0 100 0 streptococcus alpha-haemolytic Streptococcus 0 7 0 0 0 ... 0 0 0 0 0 streptococcus beta-haemolytic group a Streptococcus 0 0 0 0 0 ... 0 0 0 0 0 streptococcus beta-haemolytic group b Streptococcus 0 0 0 0 0 ... 0 0 100 0 0 streptococcus beta-haemolytic group c/g Streptococcus 0 0 0 0 0 ... 0 0 0 0 0 [26 rows x 31 columns] Data (WOUCUL) antimicrobial_name Amikacin Amoxycillin Aztreonam Cefotaxime Cefoxitin ... Temocillin Tetracycline Tigecycline Tobramycin Vancomycin category Aminoglycosides Aminopenicillins Monobactams Cephalosporins Cephalosporins ... Penicillins Tetracyclines Tetracyclines Aminoglycosides Glycopeptide microorganism_name genus ... achromobacter Achromobacter 100 100 0 0 100 ... 0 0 0 100 0 acinetobacter Acinetobacter 8 25 100 25 34 ... 33 0 0 10 0 acinetobacter baumannii Acinetobacter 33 100 100 96 88 ... 0 0 0 17 0 aeromonas Aeromonas 0 100 0 0 100 ... 0 0 0 0 0 alcaligenes Alcaligenes 100 0 0 50 50 ... 0 0 0 100 0 anaerobes Anaerobes 0 0 0 0 0 ... 0 0 0 0 0 bacillus Bacillus 0 0 0 0 0 ... 0 0 0 0 3 citrobacter Citrobacter 0 97 0 8 48 ... 0 0 0 0 0 clostridium Clostridium 0 0 0 0 0 ... 0 0 0 0 0 coliform Coliform 2 71 23 13 19 ... 2 0 0 8 0 corynebacterium Corynebacterium 0 0 0 0 0 ... 0 42 0 0 0 enterobacter Enterobacter 0 94 9 17 93 ... 2 0 0 15 0 enterococcus Enterococcus 0 39 0 0 0 ... 0 80 0 0 32 escherichia coli Escherichia 0 84 37 41 17 ... 7 0 0 34 0 haemophilus Haemophilus 0 60 0 0 0 ... 0 42 0 0 0 hafnia alvei Hafnia 0 100 0 100 0 ... 0 0 0 0 0 klebsiella Klebsiella 12 96 53 48 15 ... 0 0 0 69 0 microbacterium Microbacterium 0 0 0 0 0 ... 0 0 0 0 100 micrococcus Micrococcus 0 0 0 0 0 ... 0 0 0 0 0 morganella morganii Morganella 0 95 0 10 15 ... 0 0 0 0 0 neisseria Neisseria 0 0 0 0 0 ... 0 0 0 0 0 neisseria gonorrhoeae Neisseria 0 0 0 0 0 ... 0 73 0 0 0 neisseria meningitidis Neisseria 0 0 0 0 0 ... 0 84 0 0 0 pantoea Pantoea 0 0 0 0 0 ... 0 0 0 0 0 pasteurella Pasteurella 0 0 0 0 0 ... 0 0 0 50 0 proteus Proteus 0 31 0 0 2 ... 0 0 0 33 0 providencia Providencia 0 66 0 0 0 ... 0 0 0 50 0 pseudomonas Pseudomonas 8 75 36 75 75 ... 100 0 0 11 0 pseudomonas aeruginosa Pseudomonas 1 0 17 0 0 ... 0 0 0 2 0 raoultella Raoultella 0 100 0 100 0 ... 0 0 0 0 0 raoultella terrigena Raoultella 0 100 66 75 100 ... 0 0 0 0 0 rhizobium radiobacter Rhizobium 0 0 0 0 0 ... 0 0 0 0 0 serratia Serratia 9 100 0 10 86 ... 0 0 0 22 0 staphylococcus aureus Staphylococcus 0 0 0 0 85 ... 0 9 0 0 0 staphylococcus coagulase negative Staphylococcus 0 0 0 0 0 ... 0 29 0 0 0 stenotrophomonas maltophilia Stenotrophomonas 0 0 0 0 0 ... 0 0 0 0 0 streptococcus alpha-haemolytic Streptococcus 0 0 0 0 0 ... 0 25 0 0 0 streptococcus anginosus Streptococcus 0 0 0 0 0 ... 0 66 0 0 0 streptococcus beta-haemolytic group a Streptococcus 0 0 0 0 0 ... 0 30 0 0 0 streptococcus beta-haemolytic group b Streptococcus 0 0 0 0 0 ... 0 87 0 0 0 streptococcus beta-haemolytic group c/g Streptococcus 0 0 0 0 0 ... 0 33 0 0 0 streptococcus beta-haemolytic group g Streptococcus 0 0 0 0 0 ... 0 0 0 0 0 streptococcus constellatus Streptococcus 0 0 0 0 0 ... 0 0 0 0 0 streptococcus milleri Streptococcus 0 0 0 0 0 ... 0 27 0 0 0 streptococcus pneumoniae Streptococcus 0 0 0 0 0 ... 0 36 0 0 0 [45 rows x 31 columns] Data (XINCUL) antimicrobial_name Amikacin Amoxycillin Aztreonam Cefotaxime Cefoxitin ... Teicoplanin Temocillin Tetracycline Tobramycin Vancomycin category Aminoglycosides Aminopenicillins Monobactams Cephalosporins Cephalosporins ... Glycopeptide Penicillins Tetracyclines Aminoglycosides Glycopeptide microorganism_name genus ... acinetobacter Acinetobacter 50 100 100 100 100 ... 0 0 0 100 0 acinetobacter baumannii Acinetobacter 90 100 100 100 100 ... 0 100 0 87 0 anaerobes Anaerobes 0 0 0 0 0 ... 0 0 0 0 0 bacillus Bacillus 0 0 0 0 0 ... 0 0 0 0 0 citrobacter Citrobacter 0 100 0 50 100 ... 0 0 0 0 0 coliform Coliform 0 82 33 17 11 ... 0 0 0 0 0 enterobacter Enterobacter 0 100 0 90 100 ... 0 0 0 20 0 enterococcus Enterococcus 0 87 0 0 0 ... 87 0 66 0 87 escherichia coli Escherichia 5 100 0 88 41 ... 0 0 0 46 0 klebsiella Klebsiella 8 100 50 92 28 ... 0 0 0 80 0 morganella morganii Morganella 0 100 0 100 0 ... 0 0 0 0 0 pseudomonas Pseudomonas 0 0 0 0 0 ... 0 0 0 0 0 pseudomonas aeruginosa Pseudomonas 0 0 0 0 0 ... 0 0 0 66 0 staphylococcus aureus Staphylococcus 0 0 0 0 0 ... 0 0 4 0 0 stenotrophomonas maltophilia Stenotrophomonas 0 0 0 0 0 ... 0 0 0 0 0 streptococcus beta-haemolytic group b Streptococcus 0 0 0 0 0 ... 0 0 88 0 0 [16 rows x 27 columns] | .. code-block:: default :lineno-start: 9 # Libraries import sys import numpy as np import pandas as pd import seaborn as sns import matplotlib as mpl import matplotlib.pyplot as plt # Import specific libraries from pyamr.core.sari import SARI from pyamr.core.freq import Frequency from pyamr.datasets.load import make_susceptibility # ------------------------- # Configuration # ------------------------- # Configure seaborn style (context=talk) sns.set(style="white") # Set matplotlib mpl.rcParams['xtick.labelsize'] = 9 mpl.rcParams['ytick.labelsize'] = 9 mpl.rcParams['axes.titlesize'] = 11 mpl.rcParams['legend.fontsize'] = 9 # Pandas configuration pd.set_option('display.max_colwidth', 40) pd.set_option('display.width', 300) pd.set_option('display.precision', 4) # Numpy configuration np.set_printoptions(precision=2) # ------------------ # Methods # ------------------ def get_category_colors(index, category, cmap='hls'): """This method creates the colors for the different elements in categorical feature vector. Parameters ---------- values : array-like The vector with the categorical values cmap: string-like The colormap to use default: string-like The color to be used for the first value. Note that this value needs to appear first on the the sorted list, as such it is recommended to set is as _default. Returns ------- """ # Get categories categories = index.get_level_values(category) # Get unique elements unique = np.unique(categories) # Create the palette palette = sns.color_palette(cmap, desat=0.5, n_colors=unique.shape[0]) # Create mappers from category to color mapper = dict(zip(map(str, unique), palette)) # Create list with colors for each category colors = pd.Series(categories, index=index).map(mapper) # Return return colors # ------------------------ # Methods # ------------------------ def create_mapper(dataframe, column_key, column_value): """This method constructs a mapper Parameters ---------- dataframe: dataframe-like The dataframe from which the columns are extracted column_key: string-like The name of the column with the values for the keys of the mapper column_value: string-like The name of the column with the values for the values of the mapper Returns ------- dictionary """ dataframe = dataframe[[column_key, column_value]] dataframe = dataframe.drop_duplicates() return dict(zip(dataframe[column_key], dataframe[column_value])) # ------------------------------------------- # Load data # ------------------------------------------- # Load data data = make_susceptibility() # Show print("\nData:") print(data) print("\nColumns:") print(data.columns) # ------------------------------------------- # Compute SARI # ------------------------------------------- # Libraries from pyamr.core.sari import SARI # Create sari instance sari = SARI(groupby=['specimen_code', 'microorganism_name', 'antimicrobial_name', 'sensitivity']) # Compute SARI overall sari_overall = sari.compute(data, return_frequencies=True) # Show print("SARI (overall):") print(sari_overall) # ------------------------------ # Include registries information # ------------------------------ # Libraries from pyamr.datasets.registries import MicroorganismRegistry from pyamr.datasets.registries import AntimicrobialRegistry # Load registry mreg = MicroorganismRegistry() areg = AntimicrobialRegistry() # Format sari dataframe dataframe = sari_overall.copy(deep=True) dataframe = dataframe.reset_index() # Create genus and species dataframe[['genus', 'species']] = \ dataframe.microorganism_name \ .str.capitalize() \ .str.split(expand=True, n=1) # Combine with registry information dataframe = mreg.combine(dataframe, on='microorganism_name') dataframe = areg.combine(dataframe, on='antimicrobial_name') print(dataframe.dtypes) # ------------------------------------------- # Plot # ------------------------------------------- # Reset sari_overall = dataframe.reset_index() # Count records per specimen specimen_count = dataframe \ .groupby('specimen_code').freq.sum() \ .sort_values(ascending=False) # Show print("Cultures:") print(specimen_count) # Filter dataframe = dataframe[dataframe \ .specimen_code.isin( \ specimen_count.index.values[:5])] # Loop for specimen, df in dataframe.groupby(by='specimen_code'): # ------------------------------- # Create matrix # ------------------------------- # Filter matrix = df.copy(deep=True) matrix = df.reset_index() # Pivot table matrix = pd.pivot_table(matrix, index=['microorganism_name', 'genus'], columns=['antimicrobial_name', 'category'], values='sari') # Convert to percent matrix = matrix * 100 # Create mask mask = pd.isnull(matrix) # Fill missing (error when computing distance) matrix = matrix.fillna(1e-10) # Show print("\n\n\nData (%s)" % specimen) print(matrix.astype(int)) # ------------------------------- # Plot # ------------------------------- # Create colormap cmap = sns.color_palette("Reds", desat=0.5, n_colors=10) # Row and col colors col_colors = get_category_colors( \ index=matrix.columns, category=matrix.columns.names[1]) row_colors = get_category_colors( \ index=matrix.index, category=matrix.index.names[1]) # .. note: It is possible to also pass kwargs that would # be used by sns.heatmap function (annot, fmt, # annot_kws, ... try: # Plot cluster map grid = sns.clustermap(data=matrix, vmin=0, vmax=100, method='centroid', metric='euclidean', cmap=cmap, linewidth=0.05, mask=mask, square=True, row_colors=row_colors, col_colors=col_colors) except Exception as e: print("Exception: %s" % e) # Configuration plt.suptitle('Antibiogram (clustered) - %s' % specimen, fontsize=12) plt.tight_layout() # Show plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 5.396 seconds) .. _sphx_glr_download__examples_indexes_plot_sari_b_clustermap.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_sari_b_clustermap.py <plot_sari_b_clustermap.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_sari_b_clustermap.ipynb <plot_sari_b_clustermap.ipynb>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_