Skip to content

Commit c52ad0e

Browse files
authored
Improve plot_force and fix spec_updater (DamCB#227)
* Fix `spec_updater`according to its docstring * Update `plot_force` function to allows crop tyssue * Add test * Simplify loop & add test * Fix typo error * There is more than one force... * add test
1 parent f2f0cc1 commit c52ad0e

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

tests/draw/test_plt.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from tyssue import Sheet, config
1616
from tyssue.draw.plt_draw import quick_edge_draw, sheet_view
1717
from tyssue.draw.plt_draw import _face_color_from_sequence
18-
from tyssue.draw.plt_draw import create_gif
18+
from tyssue.draw.plt_draw import create_gif, plot_forces
1919

2020

2121
class TestsPlt:
@@ -51,7 +51,7 @@ def test_sheet_view(self):
5151
0.0, 1.0, num=self.sheet.edge_df.shape[0]
5252
)[::-1]
5353

54-
self.draw_specs["edge"]["visible"] = True
54+
self.draw_specs["edge"]["visible"] = True
5555
self.draw_specs["edge"]["color"] = self.sheet.edge_df["rand"] # [0, 0, 0, 1]
5656
self.draw_specs["edge"]["alpha"] = 1.0
5757
self.draw_specs["edge"]["color_range"] = 0, 3
@@ -155,3 +155,19 @@ def test_create_gif():
155155

156156
os.remove("frames.gif")
157157
os.remove("interval.gif")
158+
159+
160+
def test_plot_forces():
161+
geom = SheetGeometry
162+
model = PlanarModel
163+
sheet = Sheet("3", *three_faces_sheet())
164+
sheet.update_specs(model.specs)
165+
geom.update_all(sheet)
166+
fig, ax = plot_forces(sheet,
167+
geom,
168+
model,
169+
list('xy'),
170+
0.05,
171+
**{'extract': {'x_boundary': (-10, 10)}})
172+
173+
assert ax.lines[0].get_xydata().shape == (54, 2)

tests/utils/test_utils.py

+5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ def test_spec_updater():
3939
print(specs)
4040
assert specs["face"]["geometry"] == new_specs["face"]["geometry"]
4141

42+
new_specs = {"extract":{"z_boundary":(-10,10)}}
43+
utils.spec_updater(specs, new_specs)
44+
assert "extract" in specs
45+
assert specs["extract"]['z_boundary'] == new_specs["extract"]['z_boundary']
46+
4247

4348
def test_set_data_columns():
4449
dsets, _ = three_faces_sheet()

tyssue/draw/plt_draw.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -402,15 +402,17 @@ def plot_forces(
402402
else:
403403
grad_i = model.compute_gradient(sheet, components=False) * scaling
404404
grad_i = grad_i.loc[sheet.vert_df["is_active"].astype(bool)]
405-
arrows = pd.DataFrame(columns=coords + gcoords, index=sheet.vert_df.index)
406-
arrows[coords] = sheet.vert_df[coords]
407-
arrows[gcoords] = -grad_i[gcoords] # F = -grad E
405+
sheet.vert_df[gcoords]=-grad_i[gcoords] # F = -grad E
406+
407+
if 'extract' in draw_specs:
408+
sheet = sheet.extract_bounding_box(**draw_specs['extract'])
408409

409410
if ax is None:
410411
fig, ax = quick_edge_draw(sheet, coords)
411412
else:
412413
fig = ax.get_figure()
413414

415+
arrows = sheet.vert_df[coords+gcoords]
414416
for _, arrow in arrows.iterrows():
415417
ax.arrow(*arrow, **draw_specs["grad"])
416418
return fig, ax

tyssue/utils/utils.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@ def spec_updater(specs, new):
5858
specs: specification that will be modified
5959
new: dictionary of new specification
6060
"""
61-
for key, spec in specs.items():
62-
if new.get(key) is not None:
63-
spec.update(new[key])
61+
for key in new.keys():
62+
if specs.get(key) is not None:
63+
specs[key].update(new[key])
64+
else:
65+
specs[key] = new[key]
66+
6467

6568

6669
def set_data_columns(datasets, specs, reset=False):

0 commit comments

Comments
 (0)