Skip to content

Commit 5d00464

Browse files
committed
combine functions
1 parent 8740035 commit 5d00464

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

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

+18-27
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,6 @@ def get_npi_data(fine_resolution=2,
894894
counties_considered=counties_considered)
895895
save_interaction_matrix(df_counted_joint_codes, 'joint_codes', directory)
896896
plot_interaction_matrix('joint_codes', directory)
897-
plot_multiple_prescriptions('joint_codes', directory)
898897

899898
# create dataframe to count multiple codes after incidence dependent (de-)activation
900899
df_incid_depend = pd.DataFrame()
@@ -1320,7 +1319,10 @@ def save_interaction_matrix(df_interactions, filename, directory):
13201319

13211320
def plot_interaction_matrix(filename, directory):
13221321
"""! Reads interaction matrices from hard drive and writes heatmap plots
1323-
to hard drive.
1322+
to hard drive. Separates diagonal and offdiagonal entries as
1323+
interactions inside one NPI are counted for all incidence dependent
1324+
sublevels while between NPIs only one interaction is counted if more
1325+
than one sublevel is mentioned on each of the sides.
13241326
13251327
@param[in] filename Filename to read results from.
13261328
@param[in] directory Directory where to read and save data.
@@ -1347,11 +1349,16 @@ def plot_interaction_matrix(filename, directory):
13471349
df = pd.read_excel(
13481350
os.path.join(directory, filename + '.xlsx'),
13491351
sheet_name=code, engine='openpyxl')
1350-
# set diag = 0, access (i,i+1) as first column contains index
1351-
for i in range(df.shape[0]):
1352-
df.iloc[i, i+1] = 0
1352+
13531353
# remove first column and convert to numpy array
13541354
array_exclusion = df.iloc[:, 1:].to_numpy()
1355+
1356+
# separate diag and offdiag
1357+
array_exclusion_diag = copy.deepcopy(array_exclusion.diagonal())
1358+
# set diag = 0
1359+
for i in range(array_exclusion.shape[0]):
1360+
array_exclusion[i, i] = 0
1361+
13551362
if filename != 'count_deactivation':
13561363
# for count deactivation xlabel != ylabel
13571364
# else matrix is of squared form and symmetric
@@ -1380,50 +1387,34 @@ def plot_interaction_matrix(filename, directory):
13801387
else:
13811388
raise gd.DataError('Unknown filename: ' + filename)
13821389

1390+
## plot offdiagonal (interactions between NPIs)
13831391
# Set vmin = 1 so that only combinations that are simultaneously active
13841392
# at least on one day are in color, else use white.
13851393
# Set vmax = 1e6 to be adjusted with colormap, this value is larger
13861394
# than the maximum in all dataframes, this way colors of heatmaps are
13871395
# comparable across different visualizations
13881396
# (e.g. between codes or between joint_codes and exclusions)
1389-
13901397
plt.imshow(array_exclusion, cmap=cmap,
13911398
norm=mpl.colors.LogNorm(vmin=1, vmax=1e6))
13921399
plt.colorbar()
13931400
plt.tight_layout()
13941401
plt.savefig(
1395-
os.path.join(target_directory, filename + '_{}'.format(
1402+
os.path.join(target_directory, 'InterNPIs_' + filename + '_{}'.format(
13961403
code)), dpi=300)
13971404
plt.close()
13981405

1399-
1400-
def plot_multiple_prescriptions(filename, directory):
1401-
target_directory = os.path.join(
1402-
directory, 'heatmaps_mult_presc_' + filename)
1403-
if not os.path.exists(target_directory):
1404-
os.makedirs(target_directory)
1405-
1406-
codelist = pd.ExcelFile(os.path.join(
1407-
directory, filename + '.xlsx'), engine='openpyxl').sheet_names
1408-
1409-
cmap = copy.copy(mpl.cm.get_cmap('OrRd'))
1410-
1411-
for code in codelist:
1412-
df = pd.read_excel(
1413-
os.path.join(directory, filename + '.xlsx'),
1414-
sheet_name=code, engine='openpyxl')
1415-
array_exclusion = df.iloc[:, 1:].to_numpy()
1416-
fig = plt.figure()
1406+
## plot diagonal (interactions between incidence levels of one NPIs)
1407+
plt.figure()
14171408
positions = [i for i in range(len(df.columns)-1)]
14181409
plt.yticks(positions, df.columns.to_list()[1:])
14191410
plt.xticks([])
1420-
plt.imshow(np.array([array_exclusion.diagonal()]).T,
1411+
plt.imshow(np.array([array_exclusion_diag]).T,
14211412
cmap=cmap, norm=mpl.colors.LogNorm(vmin=1, vmax=50000))
14221413
plt.colorbar()
14231414
plt.title(code)
14241415
plt.tight_layout()
14251416
plt.savefig(
1426-
os.path.join(target_directory, filename + '_{}'.format(
1417+
os.path.join(target_directory, 'IntraNPIs_' + filename + '_{}'.format(
14271418
code)), dpi=300)
14281419
plt.close()
14291420

0 commit comments

Comments
 (0)