pymechlab 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pymechlab/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from .classes.batchresult import batch_from_json as batch_from_json
File without changes
@@ -0,0 +1,259 @@
1
+ from json import load
2
+
3
+ from py2md.classes import MDHeading, MDReport, MDTable
4
+
5
+ from .specimenresult import SpecimenResult
6
+ from .statistics import Statistics
7
+
8
+
9
+ class BatchResult():
10
+ name: str = None
11
+ nomthick: float = None
12
+ emodlabel: str = None
13
+ emodunit: str = None
14
+ lunit: str = None
15
+ sunit: str = None
16
+ dunit: str = None
17
+ eunit: str = None
18
+ specresults: dict[str, SpecimenResult] = None
19
+ _emodstats: Statistics = None
20
+ _stressstats: Statistics = None
21
+ _normemodstats: Statistics = None
22
+ _normstressstats: Statistics = None
23
+ _strainstats: Statistics = None
24
+
25
+ def __init__(self, name: str, nomthick: float) -> None:
26
+ self.name = name
27
+ self.nomthick = nomthick
28
+ self.specresults = {}
29
+
30
+ def add_specimen_result(self, specresult: SpecimenResult) -> None:
31
+ self.specresults[specresult.label] = specresult
32
+
33
+ def normalise_stress(self, nomthick: float) -> None:
34
+ for specresult in self.specresults.values():
35
+ specresult.normalise_stress(nomthick)
36
+
37
+ def trim_max_load(self) -> None:
38
+ for specresult in self.specresults.values():
39
+ specresult.trim_max_load()
40
+
41
+ def trim_max_stress(self) -> None:
42
+ for specresult in self.specresults.values():
43
+ specresult.trim_max_stress()
44
+
45
+ def calc_emod_load(self, lf1: float, lf2: float) -> None:
46
+ for specresult in self.specresults.values():
47
+ specresult.calc_emod_load(lf1, lf2)
48
+
49
+ def calc_emod_strain(self, strn1: float, strn2: float) -> None:
50
+ for specresult in self.specresults.values():
51
+ specresult.calc_emod_strain(strn1, strn2)
52
+
53
+ def plot_load_deflection(self, ax=None, figsize=(10, 6)):
54
+ for specresult in self.specresults.values():
55
+ ax = specresult.plot_load_deflection(ax=ax, figsize=figsize)
56
+ ax.set_xlabel(f'Deflection [{self.dunit:s}]')
57
+ ax.set_ylabel(f'Load [{self.lunit:s}]')
58
+ ax.legend()
59
+ return ax
60
+
61
+ def plot_stress_strain(self, ax=None, figsize=(10, 6)):
62
+ for specresult in self.specresults.values():
63
+ ax = specresult.plot_stress_strain(ax=ax, figsize=figsize)
64
+ ax.set_xlabel(f'Strain [{self.eunit:s}]')
65
+ ax.set_ylabel(f'Stress [{self.sunit:s}]')
66
+ ax.legend()
67
+ return ax
68
+
69
+ def plot_normalised_stress_strain(self, ax=None, figsize=(10, 6)):
70
+ for specresult in self.specresults.values():
71
+ ax = specresult.plot_normalised_stress_strain(ax=ax, figsize=figsize)
72
+ ax.set_xlabel(f'Strain [{self.eunit:s}]')
73
+ ax.set_ylabel(f'Normalised Stress [{self.sunit:s}]')
74
+ ax.legend()
75
+ return ax
76
+
77
+ def plot_stress_deflection(self, ax=None, figsize=(10, 6)):
78
+ for specresult in self.specresults.values():
79
+ ax = specresult.plot_stress_deflection(ax=ax, figsize=figsize)
80
+ ax.set_xlabel(f'Deflection [{self.dunit:s}]')
81
+ ax.set_ylabel(f'Stress [{self.sunit:s}]')
82
+ ax.legend()
83
+ return ax
84
+
85
+ def plot_normalised_stress_deflection(self, ax=None, figsize=(10, 6)):
86
+ for specresult in self.specresults.values():
87
+ ax = specresult.plot_normalised_stress_deflection(ax=ax, figsize=figsize)
88
+ ax.set_xlabel(f'Deflection [{self.dunit:s}]')
89
+ ax.set_ylabel(f'Normalised Stress [{self.sunit:s}]')
90
+ ax.legend()
91
+ return ax
92
+
93
+ @property
94
+ def emodstats(self) -> Statistics:
95
+ if self._emodstats is None:
96
+ emod = [sr.emod for sr in self.specresults.values()]
97
+ self._emodstats = Statistics(emod)
98
+ return self._emodstats
99
+
100
+ @property
101
+ def stressstats(self) -> Statistics:
102
+ if self._stressstats is None:
103
+ stress = [sr.maxstress for sr in self.specresults.values()]
104
+ self._stressstats = Statistics(stress)
105
+ return self._stressstats
106
+
107
+ @property
108
+ def normemodstats(self) -> Statistics:
109
+ if self._normemodstats is None:
110
+ normemod = [sr.normemod for sr in self.specresults.values()]
111
+ self._normemodstats = Statistics(normemod)
112
+ return self._normemodstats
113
+
114
+ @property
115
+ def normstressstats(self) -> Statistics:
116
+ if self._normstressstats is None:
117
+ normstress = [sr.maxnormstress for sr in self.specresults.values()]
118
+ self._normstressstats = Statistics(normstress)
119
+ return self._normstressstats
120
+
121
+ @property
122
+ def strainstats(self) -> Statistics:
123
+ if self._strainstats is None:
124
+ strain = [sr.maxstress/sr.emod for sr in self.specresults.values()]
125
+ self._strainstats = Statistics(strain)
126
+ return self._strainstats
127
+
128
+ def to_mdobj(self) -> MDReport:
129
+ report = MDReport()
130
+ heading = MDHeading(f'{self.name:s} Results', 1)
131
+ report.add_object(heading)
132
+ table = MDTable()
133
+ table.add_column('Label', 's', '<')
134
+ table.add_column('Width', '.3f', '')
135
+ table.add_column('Thickness', '.3f', '')
136
+ table.add_column('Max Load', '.0f', '')
137
+ table.add_column('Max Stress', '.1f', '')
138
+ table.add_column('Max Norm Stress', '.1f', '')
139
+ if self.emodlabel is not None:
140
+ table.add_column(self.emodlabel, '.0f', '')
141
+ table.add_column('Elastic Modulus', '.0f', '')
142
+ table.add_column('Norm Elastic Modulus', '.0f', '')
143
+ for sr in self.specresults.values():
144
+ row = [sr.label, sr.width, sr.thick, sr.maxload,
145
+ sr.maxstress, sr.maxnormstress]
146
+ if self.emodlabel is not None:
147
+ row = row + [sr.emodgiven, sr.emod, sr.normemod]
148
+ table.add_row(row)
149
+ report.add_object(table)
150
+ table = MDTable()
151
+ table.add_column('Parameter', 's', '<')
152
+ table.add_column('Mean Value', '.1f')
153
+ table.add_column('Standard Deviation', '.2f')
154
+ table.add_column('B Value', '.1f')
155
+ table.add_column('A Value', '.1f')
156
+ if self.emodlabel is not None:
157
+ row = [
158
+ f'Elastic Modulus [{self.emodunit:s}]',
159
+ self.emodstats.mean,
160
+ self.emodstats.std,
161
+ self.emodstats.B,
162
+ self.emodstats.A,
163
+ ]
164
+ table.add_row(row)
165
+ row = [
166
+ f'Strength [{self.sunit:s}]',
167
+ self.stressstats.mean,
168
+ self.stressstats.std,
169
+ self.stressstats.B,
170
+ self.stressstats.A,
171
+ ]
172
+ table.add_row(row)
173
+ if self.emodlabel is not None:
174
+ row = [
175
+ f'Normalised Elastic Modulus [{self.emodunit:s}]',
176
+ self.normemodstats.mean,
177
+ self.normemodstats.std,
178
+ self.normemodstats.B,
179
+ self.normemodstats.A,
180
+ ]
181
+ table.add_row(row)
182
+ row = [
183
+ f'Normalised Strength [{self.sunit:s}]',
184
+ self.normstressstats.mean,
185
+ self.normstressstats.std,
186
+ self.normstressstats.B,
187
+ self.normstressstats.A,
188
+ ]
189
+ table.add_row(row)
190
+ if self.emodlabel is not None:
191
+ row = [
192
+ 'Failure Strain [μS]',
193
+ self.strainstats.mean*1e6,
194
+ self.strainstats.std*1e6,
195
+ self.strainstats.B*1e6,
196
+ self.strainstats.A*1e6,
197
+ ]
198
+ table.add_row(row)
199
+ report.add_object(table)
200
+ return report
201
+
202
+ def __str__(self) -> str:
203
+ return self.to_mdobj().__str__()
204
+
205
+ def _repr_markdown_(self):
206
+ return self.to_mdobj()._repr_markdown_()
207
+
208
+
209
+ def batch_from_json(jsonfilepath: str):
210
+ with open(jsonfilepath, 'rt') as jsonfile:
211
+ batchdict = load(jsonfile)
212
+ name = batchdict['name']
213
+ nomthick = batchdict['nomthick']
214
+ batch = BatchResult(name, nomthick)
215
+ if 'metainfo' in batchdict:
216
+ if 'emod' in batchdict['metainfo']:
217
+ emodmeta = batchdict['metainfo']['emod']
218
+ batch.emodlabel = emodmeta['value']
219
+ batch.emodunit = emodmeta['unit']
220
+ if 'testinfo' in batchdict:
221
+ if 'load' in batchdict['testinfo']:
222
+ loadmeta = batchdict['testinfo']['load']
223
+ batch.lunit = loadmeta['unit']
224
+ if 'deflection' in batchdict['testinfo']:
225
+ deflmeta = batchdict['testinfo']['deflection']
226
+ batch.dunit = deflmeta['unit']
227
+ if 'stress' in batchdict['testinfo']:
228
+ stressmeta = batchdict['testinfo']['stress']
229
+ batch.sunit = stressmeta['unit']
230
+ if 'strain' in batchdict['testinfo']:
231
+ strainmeta = batchdict['testinfo']['strain']
232
+ batch.eunit = strainmeta['unit']
233
+ for speclabel in batchdict['specimens']:
234
+ specdict = batchdict['specimens'][speclabel]
235
+ active = True
236
+ if 'active' in specdict:
237
+ active = specdict['active']
238
+ if active:
239
+ width = specdict['width']
240
+ thick = specdict['thick']
241
+ specres = SpecimenResult(speclabel, width, thick)
242
+ batch.add_specimen_result(specres)
243
+ if batch.emodlabel is not None:
244
+ emod = specdict['meta'][batch.emodlabel]['value']
245
+ specres.set_emod_given(emod)
246
+ if batch.lunit is not None:
247
+ loaddata = specdict['test'][loadmeta['value']]['data']
248
+ specres.set_load(loaddata)
249
+ if batch.dunit is not None:
250
+ defldata = specdict['test'][deflmeta['value']]['data']
251
+ specres.set_deflection(defldata)
252
+ if batch.sunit is not None:
253
+ stressdata = specdict['test'][stressmeta['value']]['data']
254
+ specres.set_stress(stressdata)
255
+ if batch.eunit is not None:
256
+ straindata = specdict['test'][strainmeta['value']]['data']
257
+ specres.set_strain(straindata)
258
+ batch.normalise_stress(batch.nomthick)
259
+ return batch