ararpy 0.0.1a1__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.
ararpy/smp/initial.py ADDED
@@ -0,0 +1,232 @@
1
+ #!/usr/bin/env python
2
+ # -*- coding: UTF-8 -*-
3
+ """
4
+ # ==========================================
5
+ # Copyright 2023 Yang
6
+ # ararpy - smp - initial
7
+ # ==========================================
8
+ #
9
+ #
10
+ #
11
+ """
12
+
13
+ import uuid
14
+ import pandas as pd
15
+ import numpy as np
16
+ import copy
17
+
18
+ from . import (sample as samples, basic)
19
+
20
+ Sample = samples.Sample
21
+ Info = samples.Info
22
+ Table = samples.Table
23
+ Plot = samples.Plot
24
+
25
+ plateau_res_keys = [
26
+ 'F', 'sF', 'Num', 'MSWD', 'Chisq', 'Pvalue', 'age', 's1', 's2', 's3', 'Ar39',
27
+ 'rs', # 'rs' means relative error of the total sum
28
+ ]
29
+ PLATEAU_RES = dict(zip(plateau_res_keys, [np.nan] * len(plateau_res_keys)))
30
+ iso_res_keys = [
31
+ 'k', 'sk', 'm1', 'sm1',
32
+ 'MSWD', 'abs_conv', 'iter', 'mag', 'R2', 'Chisq', 'Pvalue',
33
+ 'rs', # 'rs' means relative error of the total sum
34
+ 'age', 's1', 's2', 's3',
35
+ 'conv', 'initial', 'sinitial', 'F', 'sF',
36
+ ]
37
+ ISO_RES = dict(zip(
38
+ iso_res_keys, [np.nan] * len(iso_res_keys))
39
+ )
40
+
41
+
42
+ # create sample instance
43
+ def create_sample_from_df(content: pd.DataFrame, smp_info: dict):
44
+ """
45
+
46
+ Parameters
47
+ ----------
48
+ content : [
49
+ sample_values, blank_values, corrected_values, degas_values, publish_values,
50
+ apparent_age_values, isochron_values, total_param, sample_info, isochron_mark,
51
+ sequence_name, sequence_value
52
+ ]
53
+ smp_info : dict
54
+
55
+ Returns
56
+ -------
57
+ Sample instance
58
+ """
59
+ content_dict = content.to_dict('list')
60
+ res = dict(zip([key[0] for key in content_dict.keys()], [[]] * len(content_dict)))
61
+ for key, val in content_dict.items():
62
+ res[key[0]] = res[key[0]] + [val]
63
+
64
+ return create_sample_from_dict(content=res, smp_info=smp_info)
65
+
66
+
67
+ def create_sample_from_dict(content: dict, smp_info: dict):
68
+ """
69
+ content:
70
+ {
71
+ 'smp': [], 'blk': [], 'cor': [], 'deg': [], 'pub': [],
72
+ 'age': [], 'iso': [], 'pam': [], 'mak': [], 'seq': [],
73
+ 'seq': []
74
+ }
75
+ return sample instance
76
+ """
77
+ # Create sample file
78
+ smp = Sample()
79
+ # Initializing
80
+ initial(smp)
81
+ smp.SampleIntercept = content['smp']
82
+ smp.BlankIntercept = content['blk']
83
+ smp.CorrectedValues = content['cor']
84
+ smp.DegasValues = content['deg']
85
+ smp.PublishValues = content['pub']
86
+ smp.ApparentAgeValues = content['age']
87
+ smp.IsochronValues = content['iso']
88
+ smp.TotalParam = content['pam']
89
+ smp.IsochronMark = content['mak'][0]
90
+ smp.SequenceName = content['seq'][0]
91
+ smp.SequenceValue = content['seq'][1]
92
+
93
+ basic.update_plot_from_dict(smp.Info, smp_info)
94
+
95
+ smp.SelectedSequence1 = [index for index, item in enumerate(smp.IsochronMark) if item == 1]
96
+ smp.SelectedSequence2 = [index for index, item in enumerate(smp.IsochronMark) if item == 2]
97
+ smp.UnselectedSequence = [index for index, item in enumerate(smp.IsochronMark) if item not in [1, 2]]
98
+
99
+ return smp
100
+
101
+
102
+ def initial(smp: Sample):
103
+ # 已更新 2023/7/4
104
+ smp.TotalParam = [[]] * 123
105
+ smp.BlankIntercept = [[]] * 10
106
+ smp.SampleIntercept = [[]] * 10
107
+ smp.PublishValues = [[]] * 11
108
+ smp.DecayCorrected = [[]] * 10
109
+ smp.CorrectedValues = [[]] * 10
110
+ smp.DegasValues = [[]] * 32
111
+ smp.ApparentAgeValues = [[]] * 8
112
+ smp.IsochronValues = [[]] * 47
113
+
114
+ # Doi
115
+ if not hasattr(smp, 'Doi') or getattr(smp, 'Doi') in (None, ""):
116
+ setattr(smp, 'Doi', str(uuid.uuid4().hex))
117
+
118
+ # Info
119
+ setattr(smp, 'Info', Info(
120
+ id='0', name='info', attr_name='Info',
121
+ sample=Info(
122
+ name='SAMPLE NAME', material='MATERIAL', location='LOCATION'
123
+ ),
124
+ researcher=Info(
125
+ name='RESEARCHER', addr='ADDRESS', email='EMAIL'
126
+ ),
127
+ laboratory=Info(
128
+ name='LABORATORY', addr='ADDRESS', email='EMAIL', info='INFORMATION', analyst='ANALYST'
129
+ ),
130
+ results=Info(
131
+ name='RESULTS', plateau_F=[], plateau_age=[], total_F=[], total_age=[],
132
+ isochron_F=[], isochron_age=[], J=[],
133
+ # set1=result_set_1, set2=result_set_2,
134
+ isochron={
135
+ 'figure_2': {
136
+ 0: copy.deepcopy(ISO_RES), 1: copy.deepcopy(ISO_RES), 2: copy.deepcopy(ISO_RES)},
137
+ 'figure_3': {
138
+ 0: copy.deepcopy(ISO_RES), 1: copy.deepcopy(ISO_RES), 2: copy.deepcopy(ISO_RES)},
139
+ 'figure_4': {
140
+ 0: copy.deepcopy(ISO_RES), 1: copy.deepcopy(ISO_RES), 2: copy.deepcopy(ISO_RES)},
141
+ 'figure_5': {
142
+ 0: copy.deepcopy(ISO_RES), 1: copy.deepcopy(ISO_RES), 2: copy.deepcopy(ISO_RES)},
143
+ 'figure_6': {
144
+ 0: copy.deepcopy(ISO_RES), 1: copy.deepcopy(ISO_RES), 2: copy.deepcopy(ISO_RES)},
145
+ 'figure_7': {
146
+ 0: copy.deepcopy(ISO_RES), 1: copy.deepcopy(ISO_RES), 2: copy.deepcopy(ISO_RES)},
147
+ },
148
+ age_plateau={
149
+ 0: copy.deepcopy(PLATEAU_RES), 1: copy.deepcopy(PLATEAU_RES), 2: copy.deepcopy(PLATEAU_RES)},
150
+ age_spectra={},
151
+ ),
152
+ reference=Info(
153
+ name='REFERENCE', journal='JOURNAL', doi='DOI'
154
+ ),
155
+ ))
156
+
157
+ # Plots and Tables
158
+ setattr(smp, 'UnknownTable', Table(
159
+ id='1', name='Unknown', header=samples.SAMPLE_INTERCEPT_HEADERS,
160
+ textindexs=[0], numericindexs=list(range(1, 20))
161
+ ))
162
+ setattr(smp, 'BlankTable', Table(
163
+ id='2', name='Blank', header=samples.BLANK_INTERCEPT_HEADERS,
164
+ textindexs=[0], numericindexs=list(range(1, 20))
165
+ ))
166
+ setattr(smp, 'CorrectedTable', Table(
167
+ id='3', name='Corrected', header=samples.CORRECTED_HEADERS,
168
+ textindexs=[0], numericindexs=list(range(1, 35))
169
+ ))
170
+ setattr(smp, 'DegasPatternTable', Table(
171
+ id='4', name='Degas Pattern', header=samples.DEGAS_HEADERS,
172
+ textindexs=[0], numericindexs=list(range(1, 35))
173
+ ))
174
+ setattr(smp, 'PublishTable', Table(
175
+ id='5', name='Publish', header=samples.PUBLISH_TABLE_HEADERS,
176
+ textindexs=[0], numericindexs=list(range(1, 20))
177
+ ))
178
+ setattr(smp, 'AgeSpectraTable', Table(
179
+ id='6', name='Age Spectra', header=samples.SPECTRUM_TABLE_HEADERS,
180
+ textindexs=[0], numericindexs=list(range(1, 26))
181
+ ))
182
+ setattr(smp, 'IsochronsTable', Table(
183
+ id='7', name='Isochrons', header=samples.ISOCHRON_TABLE_HEADERS,
184
+ textindexs=[0], numericindexs=list(range(1, 42))
185
+ ))
186
+ setattr(smp, 'TotalParamsTable', Table(
187
+ id='8', name='Total Params', header=samples.TOTAL_PARAMS_HEADERS,
188
+ textindexs=[0, 29, 30, 32, 33, 60, 99, 102, *list(range(103, 115))],
189
+ # numericindexs=list(range(1, 120)),
190
+ numericindexs=[1],
191
+ ))
192
+
193
+ initial_plot_styles(smp)
194
+
195
+ return smp
196
+
197
+
198
+ def initial_plot_styles(sample: Sample, except_attrs=None):
199
+ """
200
+ Initialize plot components styles based on Default Styles. Except attrs is a list containing attrs
201
+ that are not expected to be initialized.
202
+ Judgment order:
203
+ 1. The attr name is in except attrs and the sample has this attr: skip
204
+ 2. The value is not a dict instance: setattr()
205
+ 3. The sample has attr and it is a Set/Label/Text/Axis instance: iteration
206
+ """
207
+
208
+ if except_attrs is None:
209
+ except_attrs = []
210
+
211
+ def set_attr(obj, name, value):
212
+ if name in except_attrs and hasattr(obj, name):
213
+ pass
214
+ elif not isinstance(value, dict):
215
+ setattr(obj, name, value)
216
+ else:
217
+ if not (hasattr(obj, name) and isinstance(getattr(obj, name), Plot.BasicAttr)):
218
+ setattr(obj, name, getattr(Plot, value['type'].capitalize())())
219
+ for k, v in value.items():
220
+ set_attr(getattr(obj, name), k, v)
221
+
222
+ default_styles = copy.deepcopy(samples.DEFAULT_PLOT_STYLES)
223
+ for figure_index, figure_attr in default_styles.items():
224
+ plot = getattr(sample, figure_attr['attr_name'], Plot())
225
+ for key, attr in figure_attr.items():
226
+ set_attr(plot, key, attr)
227
+
228
+
229
+ def re_set_smp(sample: Sample):
230
+ std = initial(Sample())
231
+ basic.get_merged_smp(sample, std)
232
+ return sample