diff --git a/__pycache__/heatmap_visualisation.cpython-39.pyc b/__pycache__/heatmap_visualisation.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..8161ab61c997fbecdf290aa568589925a223abfd Binary files /dev/null and b/__pycache__/heatmap_visualisation.cpython-39.pyc differ diff --git a/exploration.py b/exploration.py index 82d8c5b188525896a0f315070d4ad9bb41998b60..d7f5e529ecd64e327bbe4cd8ce235496d4cefb0b 100644 --- a/exploration.py +++ b/exploration.py @@ -11,11 +11,13 @@ os.chdir(r"C:\Users\adria036\OneDrive - Wageningen University & Research\iAdriae #%% import modules -from datetime import date, timedelta,datetime +from datetime import date, datetime import pandas as pd import numpy as np import matplotlib.pyplot as plt +import matplotlib import seaborn as sns +from heatmap_visualisation import heatmap_barn #%matplotlib qt #%% set paths and constants and load data @@ -165,12 +167,12 @@ for cow in cows: plt.close() -# summarize variables -data[["xnew","X","ynew","y"]].describe() -data[""] -test = data.loc[(data["ynew"] < -17) & (data["ynew"] > -19) & (data["cowid"] = cow),:] -sns.scatterplot(data=test, x = test.index.values, y = "ynew") -sns.scatterplot(data=test, x = test.index.values, y = "y") +# # summarize variables +# data[["xnew","X","ynew","y"]].describe() +# data[""] +# test = data.loc[(data["ynew"] < -17) & (data["ynew"] > -19) & (data["cowid"] = cow),:] +# sns.scatterplot(data=test, x = test.index.values, y = "ynew") +# sns.scatterplot(data=test, x = test.index.values, y = "y") @@ -183,13 +185,11 @@ sns.scatterplot(data=test, x = test.index.values, y = "y") - explore gaps and gapsize """ -#gapsize - +#-------------------------------gapsize---------------------------------------- subset = data.loc[data["X"].isna(),["cowid","barn","date","t"]] sumdata = subset.groupby(by = ["cowid","barn","date"]).count() subset = data.loc[data["xnew"].isna(),["cowid","barn","date","t"]] test = subset.groupby(by = ["cowid","barn","date"]).count() - sumdata["xnew"] = test["t"] sumdata["gapperc"] = sumdata["t"]/864 @@ -209,10 +209,173 @@ ax = sns.scatterplot(data=sumdata,x = "xlabel", y = "gap180s", palette = "tab10", hue = "cowid", style = "date", legend = False) plt.savefig(svpath + "\\gap_imputed.png") -# average gap + + +#------------------------------behaviour over 24h------------------------------ +#sns.set_style("darkgrid") # whitegrid, dark, white, ticks + +cows = data["cowid"].drop_duplicates().reset_index(drop=1) +for cow in cows: + print(cow) + days = data.loc[(data["cowid"]==cow),["date"]].drop_duplicates() + days = days.sort_values(by = ["date"]).reset_index(drop=1) + + for dd in days["date"]: + print(dd) + subset = data.loc[(data["cowid"]==cow) & \ + (data["date"]==dd),:].copy() + palette = {"walking" : "darkorange", + "concentrate " : "tan", + "cubicle_A" : "darkorchid", + "cubicle_B" : "violet", + "cubicle_C" : "palevioletred", + "feed_rack" : "crimson", + "drink_trough" : "gold", + "slatted" : "limegreen", + "resting" : "darkorchid", + "waiting_area" : "mediumseagreen", + "unknown" : "royalblue"} + fig,ax = plt.subplots(1,1,figsize = (18,7)) + sns.scatterplot(data=subset,x="t",y="zone", + hue="area", + marker = "s", linewidth = 0, + palette = palette, + legend = False) + sns.lineplot(data=subset, x='t',y = 'zone', + linewidth = 0.2, color = 'k') + ax.set_yticks(range(9)) + ax.set_yticklabels(["walking","cubicles","feed rack","drink trough", + "concentrate feeder","slatted (b62)","resting (b62)", + "waiting area","unknown"]) + sns.set_style("ticks") + ax.set_xticks([0,14400,28800,43200,57600,72000,86400]) + ax.set_xticklabels(['00:00','04:00','08:00','12:00','16:00','20:00','24:00']) + ax.set_xlabel("time [h]") + ax.set_title("cow = " + str(cow) + ', date = ' + str(dd)) + ax.set_xlim([-1000,87400]) + plt.savefig(svpath+"\\timebudget_cow_" + str(cow) + "_" + str(dd).replace("-","") + ".png") + plt.close() + + +# ------------------------- individual cow heatmaps --------------------------- +cows = data["cowid"].drop_duplicates().reset_index(drop=1) +#barn limits +barn = {"60":[64.8,75.6], + "61":[54,64.8], + "62":[32.4,54], + "70":[21.6,32.4], + "71":[10.8,21.6], + "72":[0,10.8], + "73":[-10.8,0], + } +# plots per cow-day +for cow in cows["cowid"]: + # unique days + days = data.loc[(data["cowid"]==cow),["date"]].drop_duplicates() + days = days.sort_values(by = ["date"]).reset_index(drop=1) + + # select data and create heatmaps + for dd in days["date"]: + print(cow, dd) + # select data + data_x = data.loc[(data["cowid"] == cow) & + (data["date"] == dd),"X"] + data_y = data.loc[(data["cowid"] == cow) & + (data["date"] == dd),"y"] + + # set limits (based on information barn) + barnnr = int(data.loc[(data["cowid"] == cow) & + (data["date"] == dd),"barn"].mean()) + x_lim = barn[str(barnnr)] + y_lim = [-18,0] + + # plot heatmap + # matplotlib.rcdefaults() + fig, ax = plt.subplots() + ax = heatmap_barn(data_x, data_y, 0.1, 0.1, x_lim, y_lim) + ax.set_xlabel('y coordinate') + ax.set_ylabel('x coordinate') + ax.set_title("barn = " +str(barnnr) + ', cow = ' + str(cow) + \ + ", date = " + datetime.strftime(dd,"%Y-%m-%d").replace("-","")) + ax.xaxis.tick_top() + plt.savefig(svpath + "//heatmap_" + str(cow) + "_" + str(dd).replace("-","")) + plt.close() + + +#------------------combine heatmap with budget-area over time------------------ +#barn limits +barn = {"60":[64.8,75.6], + "61":[54,64.8], + "62":[32.4,54], + "70":[21.6,32.4], + "71":[10.8,21.6], + "72":[0,10.8], + "73":[-10.8,0], + } +palette = {"walking" : "darkorange", + "concentrate " : "tan", + "cubicle_A" : "darkorchid", + "cubicle_B" : "violet", + "cubicle_C" : "palevioletred", + "feed_rack" : "crimson", + "drink_trough" : "gold", + "slatted" : "limegreen", + "resting" : "darkorchid", + "waiting_area" : "mediumseagreen", + "unknown" : "royalblue"} +cows = data["cowid"].drop_duplicates().reset_index(drop=1) +for cow in cows: + print(cow) + days = data.loc[(data["cowid"]==cow),["date"]].drop_duplicates() + days = days.sort_values(by = ["date"]).reset_index(drop=1) + + for dd in days["date"]: + print(dd) + subset = data.loc[(data["cowid"]==cow) & \ + (data["date"]==dd),:].copy() + + fig, ax = plt.subplots(1, 2, gridspec_kw={'width_ratios': [3, 1.5]}, figsize = (22,7)) + # plot scatter + line + sns.scatterplot(data=subset,x="t",y="zone", + hue="area", + marker = "s", linewidth = 0, + palette = palette, + legend = False, + ax = ax[0]) + sns.lineplot(data=subset, x='t',y = 'zone', + linewidth = 0.2, color = 'k', ax = ax[0]) + ax[0].set_yticks(range(9)) + ax[0].set_yticklabels(["walking","cubicles","feed rack","drink trough", + "concentrate feeder","slatted (b62)","resting (b62)", + "waiting area","unknown"]) + sns.set_style("ticks") + ax[0].set_xticks([0,14400,28800,43200,57600,72000,86400]) + ax[0].set_xticklabels(['00:00','04:00','08:00','12:00','16:00','20:00','24:00']) + ax[0].set_xlabel("time [h]") + ax[0].set_title("cow = " + str(cow) + ', date = ' + str(dd)) + ax[0].set_xlim([-1000,87400]) + + # set limits (based on information barn) + barnnr = int(data.loc[(data["cowid"] == cow) & + (data["date"] == dd),"barn"].mean()) + x_lim = barn[str(barnnr)] + y_lim = [-18,0] + + # plot heatmap + ax[1] = heatmap_barn(subset["xnew"], subset["ynew"], 0.1, 0.1, x_lim, y_lim) + ax[1].set_xlabel('y coordinate') + ax[1].set_ylabel('x coordinate') + ax[1].set_title("barn = " +str(barnnr) + ', cow = ' + str(cow) + \ + ", date = " + str(dd)) + ax[1].xaxis.tick_top() + ax[1].yaxis.tick_right() + ax[1].yaxis.set_label_position("right") + + plt.savefig(svpath+"\\spatial_cow_" + str(cow) + "_" + str(dd).replace("-","") + ".png") + plt.close() \ No newline at end of file diff --git a/heatmap_visualisation.py b/heatmap_visualisation.py index 0cf37a0bdcf91e08d133354f0d7778521463a1d6..d49d6d232408acbd8f814523b027bcaef25b0292 100644 --- a/heatmap_visualisation.py +++ b/heatmap_visualisation.py @@ -12,7 +12,8 @@ import numpy as np # from read_data_sewio import start_end_dates, read_all_data, read_ids, combine_data_id import os import seaborn as sns -import matplotlib as plt +import matplotlib +import matplotlib.pyplot as plt # path_out @@ -113,11 +114,15 @@ def heatmap_barn(data_x,data_y,xstep,ystep,x_lim,y_lim): ax = sns.heatmap(df, yticklabels = round(1/xstep), xticklabels = round(1/ystep*2), - robust = True + robust = True, + cbar = False ) return ax + + +""" #%% usage # ------------------------- individual cow heatmaps --------------------------- # unique cows @@ -163,14 +168,14 @@ for cow in cows["cowid"]: y_lim = [-18,0] #[data_y.min(),data_y.max()] # plot heatmap - plt.rcdefaults() - fig, ax = plt.pyplot.subplots() + matplotlib.rcdefaults() + fig, ax = plt.subplots() ax = heatmap_barn(data_x, data_y, xstep, ystep, x_lim, y_lim) ax.set_xlabel('y coordinate') ax.set_ylabel('x coordinate') ax.set_title("barn = " +str(barnnr) + ', cow = ' + str(cow) + ", date = " + datetime.strftime(dd,"%Y-%m-%d").replace("-","")) ax.xaxis.tick_top() - plt.pyplot.savefig(path_out + "//heatmap_" + str(cow) + "_" + datetime.strftime(dd,"%Y-%m-%d").replace("-","")) - plt.pyplot.close() - + plt.savefig(path_out + "//heatmap_" + str(cow) + "_" + datetime.strftime(dd,"%Y-%m-%d").replace("-","")) + plt.close() +"""