Skip to content

Commit e22453c

Browse files
committed
add new function to plot fractal dimension over time
1 parent 3e0a6d3 commit e22453c

File tree

1 file changed

+64
-6
lines changed

1 file changed

+64
-6
lines changed

cr_bayesian_optim/fractal_dim.py

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,71 @@ def produce_options():
120120
return options
121121

122122

123-
def calculate_fractal_dim_over_time(cells: crb.CellOutput, options: crb.Options):
124-
iterations = sorted(cells.keys())
125-
dims = []
126-
for i in iterations:
123+
def fractal_dim_over_time():
124+
options = produce_options()
125+
126+
cells, _ = load_or_compute_full(options)
127+
128+
iterations = sorted(cells.keys())[::4]
129+
colony_diam = []
130+
dims_mean = []
131+
dims_std = []
132+
for i in tqdm(iterations, desc="Calculating dim(t)"):
127133
pos = np.array([c[0].mechanics.pos for c in cells[i].values()])
128134

129-
x, popt, pcov, count_boxes = calculate_fractal_dim_for_pos(pos, options, None)
130-
dims.append(popt[0])
135+
# Calculate Diameter of colony with convex hull
136+
hull = sp.spatial.ConvexHull(pos)
137+
hull_points = pos[hull.vertices]
138+
139+
diam = 0
140+
n_points = int(np.ceil(len(hull_points) / 2))
141+
for p in hull_points[:n_points]:
142+
d = np.linalg.norm(hull_points - p, axis=1)
143+
diam = max(diam, np.max(d))
144+
colony_diam.append(diam)
145+
146+
_, _, popt, pcov = calculate_fractal_dim_for_pos(pos, options, None)
147+
dims_mean.append(-popt[0])
148+
dims_std.append(pcov[0, 0] ** 0.5)
149+
150+
t = np.array(iterations) * options.time.dt / 60
151+
y1 = np.array(dims_mean)
152+
y1_err = np.array(dims_std)
153+
y2 = np.array(colony_diam) / 1000
154+
155+
fig, ax = plt.subplots(figsize=(8, 8))
156+
157+
# Introduce new y-axis for colony size
158+
ax2 = ax.twinx()
159+
ax2.set_ylabel("Diameter [mm]")
160+
ax2.plot(t, y2, label="Colony Size", linestyle="--", color=COLOR5)
161+
162+
# Plot Fractal Dimension
163+
ax.plot(t, y1, label="dim", color=COLOR1)
164+
ax.fill_between(t, y1 - y1_err, y1 + y1_err, color=COLOR3, alpha=0.3)
165+
ax.set_ylabel("Fractal Dimension")
166+
ax.set_xlabel("Time [min]")
167+
168+
handles1, labels1 = ax.get_legend_handles_labels()
169+
handles2, labels2 = ax2.get_legend_handles_labels()
170+
handles = [handles1[0], handles2[0]]
171+
labels = [labels1[0], labels2[0]]
172+
ax.legend(
173+
handles,
174+
labels,
175+
loc="upper center",
176+
bbox_to_anchor=(0.5, 1.10),
177+
ncol=4,
178+
frameon=False,
179+
)
180+
181+
ax.grid(True, which="major", linestyle="-", linewidth=0.75, alpha=0.25)
182+
ax.minorticks_on()
183+
ax.grid(True, which="minor", linestyle="-", linewidth=0.25, alpha=0.15)
184+
ax.set_axisbelow(True)
185+
fig.tight_layout()
186+
fig.savefig(options.storage_location / "fractal-dim-over-time.pdf")
187+
plt.close(fig)
131188

132189

133190
def calculate_fractal_dim_for_pos(
@@ -246,3 +303,4 @@ def fractal_dim_comparison():
246303

247304
def fractal_dim_main():
248305
fractal_dim_comparison()
306+
fractal_dim_over_time()

0 commit comments

Comments
 (0)