-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.py
175 lines (126 loc) · 4.98 KB
/
Cell.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
import images
from pathlib import Path
from PIL.Image import fromarray
from oval import RegressOval
from numpy import pi, linspace, array
from universal import copyfile, cart2polar
from residual_figure import Figure as RFigure
from error_figure import Figure as EFigure
from polar_points_figure import Figure as PPFigure
from cartesian_points_figure import Figure as CPFigure
from cartesian_oval_figure import Figure as COFigure
import csv
class Cell:
def __init__(self, file: Path):
self.name = file.stem
self.path = self._path()
self.image = images.open_file(file)
self.polar_image = images.polar(self.image)
self.edges = None
self.oval = None
self.items = {'Title': self.name}
self._save_figs()
self._fit()
def _path(self):
html_lib = Path.cwd() / 'html_lib'
folder = Path(f'cell_lib\\{self.name}\\')
folder.mkdir(parents=True, exist_ok=True)
copyfile(html_lib / 'report_template' / 'report.html', folder)
static = html_lib / 'static'
f = folder / 'static'
f.mkdir(parents=True, exist_ok=True)
for file in static.iterdir():
copyfile(file, f)
f = folder / 'images'
f.mkdir(parents=True, exist_ok=True)
return folder
def _save_figs(self):
ims = {'normal.png': fromarray(self.image),
'masked.png': fromarray(images.mask(self.image)),
'polar.png': fromarray(self.polar_image)}
for file, im in ims.items():
im.save(self.path / 'images' / file)
def _fit(self):
im = images.get_smooth(self.polar_image)
x, y = images.edge_detection(im)
oval = RegressOval(x, y)
for i in range(15):
oval.regress()
self.edges = zip(x, y)
self.oval = oval.copy()
self._measurements()
im = fromarray(images.oval_mask(self.image, self.oval))
im.save(self.path.parent / f'oval_masks\\{self.name}.png')
def _measurements(self):
area = self.oval.area[0]
if self.oval.short <= self.oval.long:
shrt = self.oval.short[0] * 2
long = self.oval.long[0] * 2
else:
shrt = self.oval.long[0] * 2
long = self.oval.short[0] * 2
self.items['Short'] = f'{shrt:.2f}'
self.items['Long'] = f'{long:.2f}'
self.items['Area'] = f'{area:.2f}'
self.items['Short-Fraction'] = f'{shrt / 5:.2f}%'
self.items['Long-Fraction'] = f'{long / 5:.2f}%'
self.items['Area-Fraction'] = f'{100 * area / 500 ** 2:.2f}%'
ratio = 2048 / 900
self.items['Short-Real'] = f'{ratio * shrt / 500:.2f}'
self.items['Long-Real'] = f'{ratio * long / 500:.2f}'
self.items['Area-Real'] = f'{(ratio / 500) ** 2 * area:.2f}'
measurements.append([ratio * shrt / 500, ratio * long / 500, (ratio / 500) ** 2 * area])
def _residual_figure(self):
x, y = zip(*self.edges)
self.edges = zip(x, y)
r, theta = cart2polar(x - self.oval.x0, y - self.oval.y0)
f = RFigure(self.oval, r, theta)
self.items['Residual Figure'] = f.construct()
def _error_figure(self):
x, y = zip(*self.edges)
self.edges = zip(x, y)
r, theta = cart2polar(x - self.oval.x0, y - self.oval.y0)
f = EFigure(self.oval, r, theta)
self.items['Error Figure'] = f.construct()
def _polar_points_figure(self):
x, y = zip(*self.edges)
self.edges = zip(x, y)
r, theta = cart2polar(array(x) - 250, array(y) - 250)
f = PPFigure(r, theta)
self.items['Polar Points Figure'] = f.construct()
f = CPFigure(array(x), array(y))
self.items['Cartesian Points Figure'] = f.construct()
x, y = self.oval.xy(linspace(-pi, pi, 200))
f = COFigure(x, y)
self.items['Cartesian Oval Figure'] = f.construct()
def report(self):
with open(self.path / 'report.html', 'r') as file:
report = file.read()
self._residual_figure()
self._error_figure()
self._polar_points_figure()
for key, value in self.items.items():
report = report.replace('{{' + key + '}}', value)
with open(self.path / 'report.html', 'w') as file:
file.write(report)
def make_index():
path = Path('cell_lib')
with open('html_lib\\report_template\\index.html', 'r') as file:
index = file.read()
row = '<a class="row" href="%s/report.html">%s</a>'
cells = [row % (p.stem, p.stem) for p in path.iterdir() if p.stem != 'index']
cells = '\n'.join(cells)
index = index.replace('{{List}}', cells)
with open(path / 'index.html', 'w') as file:
file.write(index)
measurements = []
def main():
path = Path('im')
for p in path.iterdir():
cell = Cell(p)
cell.report()
with open(path.parent / 'measurements.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(measurements)
make_index()
main()