Skip to content

Commit 8740035

Browse files
committed
plotting and saving
1 parent eeb9288 commit 8740035

File tree

1 file changed

+49
-25
lines changed

1 file changed

+49
-25
lines changed

pycode/memilio-epidata/memilio/epidata/getNPIData.py

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -892,8 +892,8 @@ def get_npi_data(fine_resolution=2,
892892
df_count_joint_codes[maincode][1] *= 0
893893
df_counted_joint_codes = count_code_multiplicities_init(df_npis_old, df_count_joint_codes,
894894
counties_considered=counties_considered)
895-
save_counter(df_counted_joint_codes, 'joint_codes', directory)
896-
plot_counter('joint_codes', directory)
895+
save_interaction_matrix(df_counted_joint_codes, 'joint_codes', directory)
896+
plot_interaction_matrix('joint_codes', directory)
897897
plot_multiple_prescriptions('joint_codes', directory)
898898

899899
# create dataframe to count multiple codes after incidence dependent (de-)activation
@@ -1165,8 +1165,8 @@ def get_npi_data(fine_resolution=2,
11651165
'. Estimated time remaining: ' +
11661166
str(int(time_remain / 60)) + ' min.')
11671167

1168-
save_counter(df_count_deactivation, 'count_deactivation', directory)
1169-
plot_counter('count_deactivation', directory)
1168+
save_interaction_matrix(df_count_deactivation, 'count_deactivation', directory)
1169+
plot_interaction_matrix('count_deactivation', directory)
11701170

11711171
if counter_cases_start >= len(counties_considered)*0.05:
11721172
print('WARNING: DataFrame starts with reported cases > 0 '
@@ -1176,11 +1176,11 @@ def get_npi_data(fine_resolution=2,
11761176
'Please consider a start date of some weeks ahead of the '
11771177
'time window to be analyzed for NPI\'s effects.')
11781178

1179-
save_counter(df_count_incid_depend, 'joint_codes_incid_depend', directory)
1180-
plot_counter('joint_codes_incid_depend', directory)
1179+
save_interaction_matrix(df_count_incid_depend, 'joint_codes_incid_depend', directory)
1180+
plot_interaction_matrix('joint_codes_incid_depend', directory)
11811181

1182-
save_counter(df_count_active, 'joint_codes_active', directory)
1183-
plot_counter('joint_codes_active', directory)
1182+
save_interaction_matrix(df_count_active, 'joint_codes_active', directory)
1183+
plot_interaction_matrix('joint_codes_active', directory)
11841184

11851185
# print sub counters
11861186
print('Sub task counters are: ')
@@ -1234,7 +1234,7 @@ def count_code_multiplicities_init(df_npis_old, df_count, counties_considered):
12341234
12351235
@param[in] df_npis_old Initial data frame read from Corona Datenplattform.
12361236
@param[in,out] df_count Dictionnary of main NPI codes with empty interaction
1237-
matrix (to be filled) for all codes under main code in df_count[maincode][1]
1237+
matrix (to be filled) for all codes under main code in df_count[maincode][1].
12381238
@param[in] counties_considered County IDs for which initial data frame is
12391239
considered.
12401240
"""
@@ -1299,40 +1299,62 @@ def count_codes(df_old, df_count, county):
12991299
return df_count
13001300

13011301

1302-
def save_counter(df_count, filename, directory):
1303-
# save results
1302+
def save_interaction_matrix(df_interactions, filename, directory):
1303+
"""! Saves interaction matrices for all subcodes in provided main codes.
1304+
1305+
@param[in] df_interactions Dictionnary of main NPI codes with interaction
1306+
matrix for all subcodes under main code in df_interactions[maincode][1].
1307+
@param[in] filename Filename to store result.
1308+
@param[in] directory Directory where to save data.
1309+
"""
13041310

13051311
writer = pd.ExcelWriter(
13061312
os.path.join(directory, filename + '.xlsx'),
13071313
engine='xlsxwriter')
1308-
for code in df_count.keys():
1309-
df_count[code][1].to_excel(writer, sheet_name=code)
1314+
for code in df_interactions.keys():
1315+
df_interactions[code][1].to_excel(writer, sheet_name=code)
13101316
writer.close()
13111317

13121318
# saves plot in folder directory/heatmaps_filename
13131319

13141320

1315-
def plot_counter(filename, directory):
1321+
def plot_interaction_matrix(filename, directory):
1322+
"""! Reads interaction matrices from hard drive and writes heatmap plots
1323+
to hard drive.
1324+
1325+
@param[in] filename Filename to read results from.
1326+
@param[in] directory Directory where to read and save data.
1327+
"""
13161328
target_directory = os.path.join(directory, 'heatmaps_' + filename)
13171329
if not os.path.exists(target_directory):
13181330
os.makedirs(target_directory)
13191331

1320-
codelist = pd.ExcelFile(os.path.join(
1321-
directory, filename + '.xlsx'), engine='openpyxl').sheet_names
1332+
try:
1333+
codelist = pd.ExcelFile(os.path.join(
1334+
directory, filename + '.xlsx'), engine='openpyxl').sheet_names
1335+
except:
1336+
raise FileNotFoundError('File ' + filename + ' not found.')
13221337

1323-
cmap = copy.copy(mpl.cm.get_cmap('OrRd'))
1338+
# invert color map elements for tab20c such that subcolors are shown
1339+
# from light to dark
1340+
cmap = copy.copy(mpl.cm.get_cmap('tab20b'))
1341+
colors = [cmap(i) for i in np.array([list(range(4*(i+1)-1,4*i-1,-1)) for i in range(5)]).flatten()]
1342+
colors = colors + [(0.6, 0.6, 0.6), (0.4, 0.4, 0.4),
1343+
(0.2, 0.2, 0.2), (0, 0, 0)]
1344+
cmap = mpl.colors.ListedColormap(colors)
13241345

13251346
for code in codelist:
13261347
df = pd.read_excel(
13271348
os.path.join(directory, filename + '.xlsx'),
13281349
sheet_name=code, engine='openpyxl')
1329-
# set diag = 0
1350+
# set diag = 0, access (i,i+1) as first column contains index
13301351
for i in range(df.shape[0]):
13311352
df.iloc[i, i+1] = 0
1353+
# remove first column and convert to numpy array
13321354
array_exclusion = df.iloc[:, 1:].to_numpy()
13331355
if filename != 'count_deactivation':
13341356
# for count deactivation xlabel != ylabel
1335-
# else matrix is of squared form
1357+
# else matrix is of squared form and symmetric
13361358
array_exclusion += np.transpose(array_exclusion)
13371359
positions = [i for i in range(len(df.columns)-1)]
13381360
plt.xticks(positions, df.columns.to_list()[1:], rotation='vertical')
@@ -1356,15 +1378,17 @@ def plot_counter(filename, directory):
13561378
plt.ylabel('NPI')
13571379
plt.title('Joint NPI prescriptions')
13581380
else:
1359-
raise gd.DataError('unknown filename: '+filename)
1381+
raise gd.DataError('Unknown filename: ' + filename)
13601382

1361-
# set vmin = 1 so that only combinations that are simultaneously active at least on one day are in colour,
1362-
# else white
1363-
# set vmax = 300000, this should be larger than maxima in all dataframes,
1364-
# this way colours of heatmaps are comparable (e.g. between codes or between joint_codes and exclusions)
1383+
# Set vmin = 1 so that only combinations that are simultaneously active
1384+
# at least on one day are in color, else use white.
1385+
# Set vmax = 1e6 to be adjusted with colormap, this value is larger
1386+
# than the maximum in all dataframes, this way colors of heatmaps are
1387+
# comparable across different visualizations
1388+
# (e.g. between codes or between joint_codes and exclusions)
13651389

13661390
plt.imshow(array_exclusion, cmap=cmap,
1367-
norm=mpl.colors.LogNorm(vmin=1, vmax=300000))
1391+
norm=mpl.colors.LogNorm(vmin=1, vmax=1e6))
13681392
plt.colorbar()
13691393
plt.tight_layout()
13701394
plt.savefig(

0 commit comments

Comments
 (0)