Note
Click here to download the full example code
06. Plot Treemap with NHS
This example displays a Treemap using a portion of the NHS dataset. This
example needs pyAMR
to load the corresponding data.
Warning
It is not completed!
Warning
It might take some time to load.
Note
It uses plotly.express
rather than go.Treemap
.
Out:
'\n# Set custom data (not needed and inconsistent)\n#fig.data[0].customdata = dataframe.to_numpy()\n#fig.data[0].hovertemplate = htmp\n\n# Uncomment to check the customdata[i] information\n#fig.data[0].hovertemplate = # guide_template(dataframe.columns.tolist())\n\n# Update: I\n#fig.update_traces(hovertemplate=\'labels=%{label}\')\n#fig.update_traces(texttemplate=\'Freq=%{value:.2f}<br>\')\nfig.update_traces(hovertemplate=htmp_guide)\n#fig.update_traces(hovertemplate=htmp)\n\n# Update: II\n# But it seems to me you want something like\n#fig.data[0].hovertemplate = \'%{label}<br>%{value}\'\n#fig.data[0].hovertemplate = \'%{Freq}<br>%{Antimicrobial Name}\'\n\nfig.update_layout(\n margin={\n \'l\': 0,\n \'r\': 0,\n \'b\': 0,\n \'t\': 0,\n \'pad\': 4\n })\n# ----------------------------\n# Save\n# ----------------------------\n# Libraries\nimport time\nfrom pathlib import Path\n\n# Define pipeline path\npath = Path(\'./objects\') / \'plot_main08_treemap\'\nfilename = \'%s.html\' % time.strftime("%Y%m%d-%H%M%S")\n\n# Create folder (if it does not exist)\npath.mkdir(parents=True, exist_ok=True)\n\n# Save\nfig.write_html("%s/%s" % (path, filename))\n\n# Show\nshow(fig)\n'
14 # Plotly
15
16 import numpy as np
17 import pandas as pd
18 import plotly.express as px
19
20 from plotly.io import show
21
22 # Import own libraries
23 from pyamr.core.sari import sari
24 from pyamr.datasets.load import load_data_nhs
25
26 try:
27 __file__
28 TERMINAL = True
29 except:
30 TERMINAL = False
31
32
33 # Methods
34 def build_hierarchical_dataframe(df, levels, value_column, color_columns=None):
35 """
36 Build a hierarchy of levels for Sunburst or Treemap charts.
37
38 Levels are given starting from the bottom to the top of the hierarchy,
39 ie the last level corresponds to the root.
40 """
41 df_all_trees = pd.DataFrame(columns=['id', 'parent', 'value', 'color'])
42 for i, level in enumerate(levels):
43 df_tree = pd.DataFrame(columns=['id', 'parent', 'value', 'color'])
44 dfg = df.groupby(levels[i:]).sum()
45 dfg = dfg.reset_index()
46 df_tree['id'] = dfg[level].copy()
47 if i < len(levels) - 1:
48 df_tree['parent'] = dfg[levels[i+1]].copy()
49 else:
50 df_tree['parent'] = 'total'
51 df_tree['value'] = dfg[value_column]
52 df_tree['color'] = dfg[color_columns[0]]
53 df_all_trees = df_all_trees.append(df_tree, ignore_index=True)
54 total = pd.Series(dict(id='total', parent='',
55 value=df[value_column].sum(),
56 color=df[color_columns[0]].sum()))
57 df_all_trees = df_all_trees.append(total, ignore_index=True)
58 return df_all_trees
59
60
61 TERMINAL = True
62
63 """
64 # --------------------------------------------------------------------
65 # Main
66 # --------------------------------------------------------------------
67 # Load data
68 data, antimicrobials, microorganisms = load_data_nhs(nrows=10000)
69
70 # Create DataFrame
71 dataframe = data.groupby(['specimen_code',
72 'microorganism_code',
73 'antimicrobial_code',
74 'sensitivity']) \
75 .size().unstack().fillna(0)
76
77 # Compute frequency
78 dataframe['freq'] = dataframe.sum(axis=1)
79
80 # Compute sari
81 dataframe['sari'] = sari(dataframe, strategy='hard')
82 dataframe['sari_medium'] = sari(dataframe, strategy='medium')
83 dataframe['sari_soft'] = sari(dataframe, strategy='soft')
84
85 # Reset index
86 dataframe = dataframe.reset_index()
87
88 # --------------------------------------------
89 # Add info for popup
90 # --------------------------------------------
91 dataframe = dataframe.merge(antimicrobials,
92 how='left', left_on='antimicrobial_code',
93 right_on='antimicrobial_code')
94
95 # Add antimicrobials information
96 dataframe = dataframe.merge(microorganisms,
97 how='left', left_on='microorganism_code',
98 right_on='microorganism_code')
99
100 # Format dataframe
101 dataframe = dataframe.round(decimals=3)
102
103 # Replace
104 dataframe.microorganism_name = \
105 dataframe.microorganism_name.str.title()
106 dataframe.columns = \
107 dataframe.columns.str.title().str.replace('_', ' ')
108
109
110 # Show
111 if TERMINAL:
112 print("\nColumns:")
113 print(dataframe.dtypes)
114 print("\nDF:")
115 print(dataframe)
116
117
118 # -------------------------------------------
119 # Plot
120 # -------------------------------------------
121 def guide_template(names):
122 return '<br>'.join([
123 "%2d <b>%-45s</b> %%{customdata[%s]}" % (i, n, i )
124 for i, n in enumerate(names)])
125
126 # Guide template
127 htmp_guide = guide_template(dataframe.columns.tolist())
128
129 # Define own template
130 htmp = "Specimen: (%{customdata[0]})<br>"
131 htmp+= "Microorganism: %{customdata[26]} (%{customdata[1]})<br>"
132 htmp+= "Antimicrobial: %{customdata[12]} (%{customdata[2]})<br>"
133 htmp+= "Freq: %{customdata[8]}<br>"
134 htmp+= "SARI: %{customdata[9]}<br>"
135
136 htmp = "(%{customdata[0]})<br>"
137 htmp+= "%{customdata[26]} (%{customdata[1]})<br>"
138 htmp+= "%{customdata[12]} (%{customdata[2]})<br>"
139 htmp+= "Freq: %{customdata[8]}<br>"
140 htmp+= "SARI: %{customdata[9]}<br>"
141
142 # Display
143 fig = px.treemap(dataframe,
144 path=['Specimen Code',
145 'Microorganism Code',
146 'Antimicrobial Code'],
147 #hover_name=,
148 hover_data=dataframe.columns.tolist(),
149 values='Freq',
150 color='Sari',
151 color_continuous_scale='Reds',
152 title='Treemap of <Microorganisms, Antimicrobials> pairs')
153
154
155 # Show current template
156 print(fig.data[0].hovertemplate)
157 """
158
159 """
160 The default hover template looks as follows:
161
162 labels=%{label}<br>
163 Freq=%{value}<br>
164 parent=%{parent}<br>
165 id=%{id}<br>
166 Microorganism Name=%{customdata[0]}<br>
167 Name=%{customdata[1]}<br>
168 Sari Medium=%{customdata[2]}<br>
169 Sari Soft=%{customdata[3]}<br>
170 Sari=%{color}
171 <extra></extra>
172 """
173
174 """
175 # Set custom data (not needed and inconsistent)
176 #fig.data[0].customdata = dataframe.to_numpy()
177 #fig.data[0].hovertemplate = htmp
178
179 # Uncomment to check the customdata[i] information
180 #fig.data[0].hovertemplate = \
181 # guide_template(dataframe.columns.tolist())
182
183 # Update: I
184 #fig.update_traces(hovertemplate='labels=%{label}')
185 #fig.update_traces(texttemplate='Freq=%{value:.2f}<br>')
186 fig.update_traces(hovertemplate=htmp_guide)
187 #fig.update_traces(hovertemplate=htmp)
188
189 # Update: II
190 # But it seems to me you want something like
191 #fig.data[0].hovertemplate = '%{label}<br>%{value}'
192 #fig.data[0].hovertemplate = '%{Freq}<br>%{Antimicrobial Name}'
193
194 fig.update_layout(
195 margin={
196 'l': 0,
197 'r': 0,
198 'b': 0,
199 't': 0,
200 'pad': 4
201 })
202 # ----------------------------
203 # Save
204 # ----------------------------
205 # Libraries
206 import time
207 from pathlib import Path
208
209 # Define pipeline path
210 path = Path('./objects') / 'plot_main08_treemap'
211 filename = '%s.html' % time.strftime("%Y%m%d-%H%M%S")
212
213 # Create folder (if it does not exist)
214 path.mkdir(parents=True, exist_ok=True)
215
216 # Save
217 fig.write_html("%s/%s" % (path, filename))
218
219 # Show
220 show(fig)
221 """
Total running time of the script: ( 0 minutes 0.099 seconds)