in functions/source/preprocess/preprocess.py [0:0]
def generate_iwp_plot(dataframe, temperature, plot_index, patient):
"""
Generate individual IWP plot for each positive blood collection.
"""
dataframe = dataframe.copy()
# Convert all datetime values to datetime
datetime_column_names = [
'beg_effective_dt_tm',
'end_effective_dt_tm',
'collection_dt_tm',
'admit_dt_tm',
'disch_dt_tm',
'first_activity_start_dt_tm',
'last_activity_end_dt_tm',
]
# Convert all date to to datetime format, the input data is mm-dd-yyyy
for column_name in datetime_column_names:
dataframe[column_name] = pd.to_datetime(
dataframe[column_name], errors='coerce',
# format='%m/%d/%Y',
)
collection_date = dataframe.loc[plot_index, 'collection_dt_tm']
day3 = pd.Timedelta(days=3)
fig, axis = plt.subplots(
2, 1, True, False,
figsize=(7, 7), dpi=150,
gridspec_kw={'height_ratios': [1, 2.5]},
)
# Generate the temperature plot - top portion
# Fever limit, above limit the
temperature_limit = 38.0
# Mark the temperature limit (38 C) with a solid line
axis[0].plot_date(
[collection_date - day3, collection_date + day3],
[temperature_limit, temperature_limit],
'k-',
color='0.4',
)
# Plot all temperature information
for temperature_index in temperature.index:
temp_date = temperature.loc[temperature_index, 'event_end_dt_tm']
value = temperature.loc[temperature_index, 'result_val']
try:
# Above limit the marker is red
if value < temperature_limit:
markercolor = '0.4'
else:
markercolor = [0.8, 0.2, 0.2]
# Plot the dates - temperature information
axis[0].plot_date(
temp_date, value, 'wo',
markeredgecolor=markercolor,
markersize=6,
markeredgewidth=4,
)
except ValueError:
print('failure in plotting temperature')
# Plot catheter start and end
if not (pd.isnull(dataframe.loc[plot_index, 'first_activity_start_dt_tm'])
or pd.isnull(dataframe.loc[
plot_index, 'last_activity_end_dt_tm'])):
axis[1].plot_date(
[dataframe.loc[plot_index, 'first_activity_start_dt_tm'],
dataframe.loc[plot_index, 'last_activity_end_dt_tm']],
[0.1, 0.1],
'k-',
color='0.8',
linewidth=60,
)
catheter_information = ' - '.join((
str(dataframe.loc[plot_index, 'first_site_result']),
str(dataframe.loc[plot_index, 'first_catheter_type_result']),
))
axis[1].text(
collection_date - pd.Timedelta(days=2),
0.09,
catheter_information,
size=estimate_text_size(catheter_information),
)
# Plot nursing unit start and end
if not (pd.isnull(dataframe.loc[plot_index, 'beg_effective_dt_tm'])
or pd.isnull(dataframe.loc[plot_index, 'end_effective_dt_tm'])):
axis[1].plot_date(
[dataframe.loc[plot_index, 'beg_effective_dt_tm'],
dataframe.loc[plot_index, 'end_effective_dt_tm']],
[0.3, 0.3],
'k-',
color='0.8',
linewidth=60,
)
nursing_inforamtion = ' - '.join((
str(dataframe.loc[plot_index,
'nursing_unit_short_desc_at_collection']),
str(dataframe.loc[plot_index,
'med_service_desc_src_at_collection']),
))
axis[1].text(
collection_date - pd.Timedelta(days=2),
0.29,
nursing_inforamtion,
size=estimate_text_size(nursing_inforamtion),
)
# Helper line for organism and collection dates
axis[1].plot_date(
[dataframe.loc[plot_index, 'collection_dt_tm'] for _ in range(2)],
[0.5, 0.63],
'k-',
color='0.8',
)
# Plot all collection dates
for index in dataframe.index:
axis[1].plot_date(
[dataframe.loc[index, 'collection_dt_tm']],
[0.5],
'ko',
color='0.4',
markersize=16
)
# Corresponding organism
organism_information = dataframe.loc[plot_index, 'organism']
axis[1].text(
collection_date - datetime.timedelta(days=1.2),
0.65,
organism_information,
size=12,
)
# Axis settings
axis[0].set_ylabel('Temperature /C')
axis[0].set_ylim(35, 41)
axis[0].set_yticks(range(35, 42))
axis[0].set_yticklabels(['{}.0'.format(value) for value in range(35, 42)])
axis[0].grid(axis='y', linestyle='-')
collection_date = pd.to_datetime(collection_date)
# print((collection_date - day3).date(),(collection_date + day3).date())
axis[0].set_xlim(
pd.to_datetime(collection_date - day3).date(),
pd.to_datetime(collection_date + day3).date())
axis[1].set_ylim(0, 0.8)
axis[1].set_yticks([0.1, 0.3, 0.5, 0.7])
axis[1].set_yticklabels([
'Central line',
'Nursing unit',
'Blood sample',
'Organism',
])
for label in axis[1].get_xticklabels():
label.set_ha('center')
label.set_rotation(90)
plt.tight_layout()
buf = io.BytesIO()
fig.savefig(buf, format="png")
buf.seek(0)
image = buf.read()
s3_resource = boto3.resource("s3")
# saving the infection window time plots
# to processed/images/patient/IWP/plots_{plot-number}.png
# filename = f'images/{patient}/IWP/plots_{plot_index}.png'
# Modify filename because ordering plot_index results:
# 1, 10, 11, 12, 2, 3 instead of 1, 2, 3, 10, 11, 12
filename = 'images/{patient}/IWP/plots_{index}.png'.format(**{
'patient': patient,
'index': str(plot_index).rjust(2, '0'),
})
bucket = os.environ['patient_bucket']
s3_resource.Object(bucket, filename).put(
Body=image, ServerSideEncryption="aws:kms")
return True