forked from DamCB/tyssue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.py
189 lines (141 loc) · 6.13 KB
/
test_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import numpy as np
import pandas as pd
import pytest
from scipy.spatial import Voronoi
from tyssue.utils import utils
from tyssue import Sheet, SheetGeometry
from tyssue.generation import three_faces_sheet, extrude
from tyssue.generation import hexa_grid2d, from_2d_voronoi
from tyssue.generation import hexa_grid3d, from_3d_voronoi
from numpy.testing import assert_almost_equal, assert_allclose, assert_array_equal
from tyssue import Monolayer, config
from tyssue import SheetGeometry as geom
from tyssue.topology.base_topology import close_face
from tyssue.core.sheet import get_opposite
def test_get_next():
sheet = Sheet("emin", *three_faces_sheet())
next_ = utils.get_next(sheet)
expected = np.array([1, 2, 3, 4, 5, 0, 7, 8, 9, 10, 11, 6, 13, 14, 15, 16, 17, 12])
np.testing.assert_array_equal(next_, expected)
def test_to_nd():
grid = hexa_grid2d(6, 4, 3, 3)
datasets = from_2d_voronoi(Voronoi(grid))
sheet = Sheet("test", datasets)
result = utils._to_3d(sheet.face_df["x"])
assert (sheet.face_df[["x", "y", "z"]] * result).shape[1] == 3
def test_spec_updater():
specs = {
"face": {"is_active": True, "height": 4, "radial_tension": 0.1},
"edge": {"x": 2, "y": 1},
}
new_specs = {"face": {"geometry": "ellipsoidal"}}
utils.spec_updater(specs, new_specs)
print(specs)
assert specs["face"]["geometry"] == new_specs["face"]["geometry"]
new_specs = {"extract":{"z_boundary":(-10,10)}}
utils.spec_updater(specs, new_specs)
assert "extract" in specs
assert specs["extract"]['z_boundary'] == new_specs["extract"]['z_boundary']
def test_set_data_columns():
dsets, _ = three_faces_sheet()
specs = {"cell": {"nope": 0}, "vert": {"new": 100}, "edge": {"new": "r"}}
with pytest.warns(UserWarning):
utils.set_data_columns(dsets, specs, reset=False)
assert dsets["vert"]["new"].loc[0] == 100
assert dsets["edge"]["new"].loc[0] == "r"
specs = {"vert": {"new": 10}, "edge": {"new": "v"}}
utils.set_data_columns(dsets, specs, reset=False)
assert dsets["edge"]["new"].loc[0] == "r"
assert dsets["vert"]["new"].loc[0] == 100
utils.set_data_columns(dsets, specs, reset=True)
assert dsets["vert"]["new"].loc[0] == 10
assert dsets["edge"]["new"].loc[0] == "v"
def test_data_at_opposite():
sheet = Sheet("emin", *three_faces_sheet())
geom.update_all(sheet)
sheet.get_opposite()
opp = utils.data_at_opposite(sheet, sheet.edge_df["length"], free_value=None)
assert opp.shape == (sheet.Ne,)
assert opp.loc[0] == 1.0
assert ~np.isfinite(opp.loc[1])
opp = utils.data_at_opposite(sheet, sheet.edge_df["length"], free_value=-1)
assert opp.loc[1] == -1.0
def test_data_at_opposite_df():
sheet = Sheet("emin", *three_faces_sheet())
geom.update_all(sheet)
sheet.get_opposite()
opp = utils.data_at_opposite(sheet, sheet.edge_df[["dx", "dy"]], free_value=None)
assert opp.shape == (sheet.Ne, 2)
assert list(opp.columns) == ["dx", "dy"]
def test_data_at_opposite_array():
sheet = Sheet("emin", *three_faces_sheet())
geom.update_all(sheet)
sheet.get_opposite()
opp = utils.data_at_opposite(
sheet, sheet.edge_df[["dx", "dy"]].to_numpy(), free_value=None
)
assert opp.shape == (sheet.Ne, 2)
assert_array_equal(opp.index, sheet.edge_df.index)
def test_single_cell():
grid = hexa_grid3d(6, 4, 3)
datasets = from_3d_voronoi(Voronoi(grid))
sheet = Sheet("test", datasets)
eptm = utils.single_cell(sheet, 1)
assert len(eptm.edge_df) == len(sheet.edge_df[sheet.edge_df["cell"] == 1])
def test_scaled_unscaled():
sheet = Sheet("3faces_3D", *three_faces_sheet())
SheetGeometry.update_all(sheet)
def mean_area():
return sheet.face_df.area.mean()
prev_area = sheet.face_df.area.mean()
sc_area = utils.scaled_unscaled(mean_area, 2, sheet, SheetGeometry)
post_area = sheet.face_df.area.mean()
assert post_area == prev_area
assert_almost_equal(sc_area / post_area, 4.0)
def fails():
raise ValueError
with pytest.raises(ValueError):
utils.scaled_unscaled(fails, 2, sheet, SheetGeometry)
post_area = sheet.face_df.area.mean()
assert post_area == prev_area
def test_modify():
datasets, _ = three_faces_sheet()
extruded = extrude(datasets, method="translation")
mono = Monolayer("test", extruded, config.geometry.bulk_spec())
mono.update_specs(config.dynamics.quasistatic_bulk_spec(), reset=True)
modifiers = {
"apical": {"edge": {"line_tension": 1.0}, "face": {"contractility": 0.2}},
"basal": {"edge": {"line_tension": 3.0}, "face": {"contractility": 0.1}},
}
utils.modify_segments(mono, modifiers)
assert mono.edge_df.loc[mono.apical_edges, "line_tension"].unique()[0] == 1.0
assert mono.edge_df.loc[mono.basal_edges, "line_tension"].unique()[0] == 3.0
def test_ar_calculation():
sheet = Sheet("test", *three_faces_sheet())
SheetGeometry.update_all(sheet)
sheet.face_df["AR"] = utils.ar_calculation(sheet, coords=["x", "y"])
sheet.vert_df["x"] = sheet.vert_df["x"] * 2
sheet.face_df["AR2"] = utils.ar_calculation(sheet, coords=["x", "y"])
assert_allclose(sheet.face_df["AR2"], 2 * sheet.face_df["AR"])
def test_face_centered_patch():
grid = hexa_grid2d(6, 4, 3, 3)
datasets = from_2d_voronoi(Voronoi(grid))
sheet = Sheet("test", datasets)
subsheet = utils.face_centered_patch(sheet, 5, 2)
assert subsheet.Nf == 6
extruded = extrude(datasets, method="translation")
mono = Monolayer("test", extruded, config.geometry.bulk_spec())
submono = utils.face_centered_patch(mono, 15, 1)
assert submono.Nf == 19
def test_cell_centered_patch():
grid = hexa_grid2d(6, 4, 3, 3)
datasets = from_2d_voronoi(Voronoi(grid))
_ = Sheet("test", datasets)
extruded = extrude(datasets, method="translation")
mono = Monolayer("test", extruded, config.geometry.bulk_spec())
submono = utils.cell_centered_patch(mono, 5, 1)
assert submono.Nc == 4
def test_patch_raises():
sheet = Sheet("3faces_3D", *three_faces_sheet())
with pytest.raises(ValueError):
utils.elem_centered_patch(sheet, 0, 1, "not")