-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55df0c7
commit ac77bc5
Showing
7 changed files
with
841 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import matplotlib as mpl | ||
|
||
mpl.rcParams["figure.dpi"] = 150 | ||
|
||
from .generate_inputs import * | ||
from .plot_luminosity import * | ||
from .plot_population import * | ||
from .plot_temperature import * | ||
from .animate import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import warnings | ||
|
||
from matplotlib import animation | ||
from matplotlib import colors as cls | ||
from matplotlib import pyplot as plt | ||
from matplotlib import colorbar as cb | ||
from matplotlib import cm as cm | ||
import matplotlib | ||
import numpy as np | ||
|
||
from IPython.display import HTML | ||
|
||
import lue.data_model as ldm | ||
import campo | ||
|
||
|
||
warnings.filterwarnings("ignore", category=matplotlib.MatplotlibDeprecationWarning) | ||
|
||
|
||
def animate(show_notebook=True): | ||
# import data | ||
dataset = ldm.open_dataset("daisy_world.lue") | ||
field_dataframe = campo.dataframe.select( | ||
dataset.climate, property_names=["temperature"] | ||
) | ||
daisy_dataframe = campo.dataframe.select( | ||
dataset.daisies, property_names=["age", "mask", "breed"] | ||
) | ||
x_coords = daisy_dataframe["daisies"]["site"]["breed"]["coordinates"][:, 0].data | ||
y_coords = daisy_dataframe["daisies"]["site"]["breed"]["coordinates"][:, 1].data | ||
|
||
nr_timesteps = field_dataframe["climate"]["surface"]["temperature"][0].shape[0] | ||
|
||
field_property = field_dataframe["climate"]["surface"]["temperature"] | ||
|
||
# get metadata | ||
cellsize = 10 | ||
minx = field_property[0].xcoord[0].data | ||
miny = field_property[0].ycoord[0].data | ||
maxx = field_property[0].xcoord[-1].data + cellsize | ||
maxy = field_property[0].ycoord[-1].data + cellsize | ||
extent = (minx, maxx, miny, maxy) | ||
|
||
######### | ||
|
||
# create the figure | ||
f = plt.figure() | ||
# [left, bottom, width, height] | ||
ax1 = f.add_axes([0.8, 0.05, 0.1, 0.9]) | ||
ax2 = f.add_axes([0.05, 0.05, 0.7, 0.9]) | ||
plt.axis("off") | ||
|
||
# making normalization scheme between -30 and 80 degrees Celcius | ||
norm = cls.Normalize(vmin=-30, vmax=80) | ||
cmap = cm.coolwarm | ||
# and corresponding legend | ||
leg = cb.ColorbarBase(ax1, cmap=cmap, norm=norm, orientation="vertical") | ||
leg.set_label("Temperature (degrees Celcius)") | ||
|
||
# no agent colour receives full transparency | ||
# acmap = cm.get_cmap("binary").copy() # DID NOT WORK FOR ME (MY MPL VERSION?) | ||
acmap = cm.binary | ||
acmap.set_under("green", alpha=0.0) | ||
|
||
# use imshow to plot the field over time | ||
# and scatter to plot the agents | ||
def init(): | ||
# temperature field | ||
field_values = field_dataframe["climate"]["surface"]["temperature"][0][0].data | ||
im = ax2.imshow( | ||
field_values, norm=norm, extent=extent, cmap=cmap, animated=True | ||
) | ||
|
||
# daisies, mask the alives | ||
agent_values = daisy_dataframe["daisies"]["site"]["breed"]["values"][:, 0] | ||
agent_active = daisy_dataframe["daisies"]["site"]["mask"]["values"][:, 0] | ||
agent_values = np.where(agent_active == 1, agent_values, -99) | ||
agents = ax2.scatter( | ||
x_coords, | ||
y_coords, | ||
marker="o", | ||
c=agent_values, | ||
s=50, | ||
cmap=acmap, | ||
vmin=0, | ||
vmax=1, | ||
animated=True, | ||
) | ||
|
||
# time label | ||
month = ax2.text(0.02, 0.975, "step = 0", transform=ax2.transAxes, fontsize=10) | ||
return im, agents, month | ||
|
||
def animate_ts(i): | ||
ax2.clear() | ||
ax2.axis("off") | ||
|
||
# temperature field | ||
field_values = field_dataframe["climate"]["surface"]["temperature"][0][i].data | ||
im = ax2.imshow( | ||
field_values, norm=norm, extent=extent, cmap=cmap, animated=True | ||
) | ||
|
||
# daisies, mask the alives | ||
agent_values = daisy_dataframe["daisies"]["site"]["breed"]["values"][:, i] | ||
agent_active = daisy_dataframe["daisies"]["site"]["mask"]["values"][:, i] | ||
agent_values = np.where(agent_active == 1, agent_values, -99) | ||
|
||
agents = ax2.scatter( | ||
x_coords, | ||
y_coords, | ||
marker="o", | ||
c=agent_values, | ||
s=50, | ||
cmap=acmap, | ||
vmin=0, | ||
vmax=1, | ||
animated=True, | ||
) | ||
|
||
# time label | ||
month = ax2.text( | ||
0.02, 0.975, f"step = {i+1}", transform=ax2.transAxes, fontsize=10 | ||
) | ||
|
||
return im, agents, month | ||
|
||
im_ani = animation.FuncAnimation( | ||
f, animate_ts, interval=75, blit=True, frames=nr_timesteps, init_func=init | ||
) | ||
# im_ani.save("im.mp4", dpi=100, metadata={"artist":"Judith Verstegen"}) | ||
|
||
if show_notebook: | ||
plt.close(im_ani._fig) | ||
return HTML(im_ani.to_html5_video()) | ||
else: | ||
plt.show() | ||
|
||
|
||
if __name__ == "__main__": | ||
animate(False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
def generate_input_files(rows=30, cols=30): | ||
cellsize = 10 | ||
x0 = 0 | ||
y0 = 0 | ||
|
||
x1 = x0 + cols * cellsize | ||
y1 = y0 + rows * cellsize | ||
|
||
with open("area_extent.csv", "w") as content: | ||
content.write(f"{x0},{y0},{x1},{y1},{cols},{rows}") | ||
|
||
agent_id = 0 | ||
|
||
with open("location_daisies.csv", "w") as content: | ||
for r in range(rows, 0, -1): | ||
r -= 1 | ||
for c in range(cols): | ||
x = x0 + c * cellsize + cellsize / 2 | ||
y = y0 + r * cellsize + cellsize / 2 | ||
content.write(f"{x},{y}\n") | ||
agent_id += 1 | ||
|
||
|
||
if __name__ == "__main__": | ||
generate_input_files(19, 19) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import matplotlib.pyplot as plt | ||
|
||
import lue.data_model as ldm | ||
import campo | ||
|
||
|
||
def plot_luminosity(): | ||
dataset = ldm.open_dataset("daisy_world.lue") | ||
prop = campo.dataframe.select(dataset.climate, property_names=["solar_luminosity"]) | ||
|
||
values = prop["climate"]["surface"]["solar_luminosity"][0].data[:, 0, 0] | ||
|
||
plt.plot(values) | ||
plt.ylabel("Solar luminosity (-)") | ||
plt.xlabel("Time step") | ||
plt.show() | ||
|
||
|
||
if __name__ == "__main__": | ||
plot_luminosity() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import numpy | ||
|
||
import matplotlib.pyplot as plt | ||
|
||
import lue.data_model as ldm | ||
import campo | ||
|
||
|
||
def plot_population(): | ||
dataset = ldm.open_dataset("daisy_world.lue") | ||
|
||
daisy_dataframe = campo.dataframe.select( | ||
dataset.daisies, property_names=["mask", "breed"] | ||
) | ||
|
||
breed = daisy_dataframe["daisies"]["site"]["breed"]["values"] | ||
mask = daisy_dataframe["daisies"]["site"]["mask"]["values"] | ||
|
||
nr_timesteps = breed.shape[1] | ||
nr_agents = breed.shape[0] | ||
|
||
daisy_b = numpy.zeros(nr_timesteps) | ||
daisy_w = numpy.zeros(nr_timesteps) | ||
|
||
for ts in range(0, nr_timesteps): | ||
breed_ts = breed[:, ts] | ||
mask_ts = mask[:, ts] | ||
breeds_alive = numpy.ma.masked_where(mask_ts != 1, breed_ts) | ||
w = numpy.ma.masked_where(breeds_alive == 1, numpy.ones(breed.shape[0])) | ||
b = numpy.ma.masked_where(breeds_alive == 0, numpy.ones(breed.shape[0])) | ||
daisy_b[ts] = len(numpy.ma.compressed(b)) | ||
daisy_w[ts] = len(numpy.ma.compressed(w)) | ||
|
||
plt.plot(daisy_b, label="black daisies", color="k") | ||
plt.plot(daisy_w, label="white daisies", color="lightgrey") | ||
plt.ylim([0, nr_agents]) | ||
plt.ylabel("Population size (#)") | ||
plt.xlabel("Time step") | ||
plt.legend() | ||
plt.show() | ||
|
||
|
||
if __name__ == "__main__": | ||
plot_population() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import numpy | ||
import matplotlib.pyplot as plt | ||
|
||
import lue.data_model as ldm | ||
import campo | ||
|
||
|
||
def plot_temperature(): | ||
dataset = ldm.open_dataset("daisy_world.lue") | ||
init_properties = campo.dataframe.select( | ||
dataset.climate, property_names=["init_temp"] | ||
) | ||
properties = campo.dataframe.select(dataset.climate, property_names=["temperature"]) | ||
|
||
t_init = init_properties["climate"]["surface"]["init_temp"][0].data | ||
t_init_mean = numpy.mean(t_init) | ||
|
||
values = properties["climate"]["surface"]["temperature"][0].data | ||
|
||
nr_timesteps = values.shape[0] | ||
|
||
avg_temp = numpy.zeros(nr_timesteps + 1) | ||
|
||
avg_temp[0] = t_init_mean | ||
|
||
for ts in range(0, nr_timesteps): | ||
avg_temp[ts + 1] = numpy.mean(values[ts]) | ||
|
||
plt.plot(avg_temp) | ||
plt.ylabel("Temperature (degrees Celcius)") | ||
plt.xlabel("Time step") | ||
plt.show() | ||
|
||
|
||
if __name__ == "__main__": | ||
plot_temperature() |