Repository navigation
Expand file tree
/
Copy pathspectral_types.py
More file actions
335 lines (293 loc) · 10 KB
/
Copy pathspectral_types.py
File metadata and controls
335 lines (293 loc) · 10 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import numpy as np
import re
import logging
from sqlalchemy import and_
import sqlalchemy.exc
from simple.schema import SpectralTypes
from astrodb_utils import (
AstroDBError,
find_source_in_db,
)
__all__ = [
"ingest_spectral_type",
"convert_spt_string_to_code",
"convert_spt_code_to_string",
]
logger = logging.getLogger("AstroDB")
def ingest_spectral_type(
db,
source: str = None,
*,
spectral_type_string: str = None,
spectral_type_code: float = None,
spectral_type_error: float = None,
regime: str = None,
photometric: bool = False,
comments: str = None,
reference: str = None,
raise_error: bool = True,
):
"""
Script to ingest spectral types
Parameters
----------
db: astrodbkit2.astrodb.Database
Database object created by astrodbkit2
source: str
Name of source. Constrained by the Sources table
spectral_type_string: str
Spectral Type of source
spectral_type_error: str, optional
Spectral Type Error of source
regime: str
String. Constrained by Regimes table
comment: str, optional
Comments
reference: str
Reference of the Spectral Type
raise_error: bool, optional
Returns
-------
None
"""
db_name = find_source_in_db(db, source)
if len(db_name) != 1:
msg = f"No unique source match for {source} in the database "
raise AstroDBError(msg)
else:
db_name = db_name[0]
# Check for duplicates
duplicate_check = (
db.query(db.SpectralTypes.c.source)
.filter(
and_(
db.SpectralTypes.c.source == db_name,
db.SpectralTypes.c.regime == regime,
db.SpectralTypes.c.reference == reference,
)
)
.count()
)
logger.debug(f"Duplicate check: {duplicate_check}")
if duplicate_check > 0:
msg = f"Spectral type already in the database: {db_name}, {regime}, {reference}"
if raise_error:
logger.error(msg)
raise AstroDBError(msg)
else:
logger.warning(msg)
else:
logger.debug(f"No duplicates found for : {db_name}, {regime}, {reference}")
adopted = adopt_spectral_type(db, db_name, spectral_type_error)
if adopted:
unset_previously_adopted(db, db_name)
if spectral_type_code is None:
spectral_type_code = convert_spt_string_to_code(spectral_type_string)
msg = f"Converted {spectral_type_string} to {spectral_type_code}"
logger.debug(msg)
# Construct the data to be added
spt_data = {
"source": db_name,
"spectral_type_string": spectral_type_string,
"spectral_type_code": spectral_type_code,
"spectral_type_error": spectral_type_error,
"regime": regime,
"adopted": adopted,
"photometric": photometric,
"comments": comments,
"reference": reference,
}
logger.debug(f"Trying to insert {spt_data} into Spectral Types table ")
try:
spt_obj = SpectralTypes(**spt_data)
with db.session as session:
session.add(spt_obj)
session.commit()
logger.info(f"Spectral type added to database: {spt_data}\n")
except sqlalchemy.exc.IntegrityError as e:
if (
db.query(db.Publications)
.filter(db.Publications.c.reference == reference)
.count()
== 0
):
msg = f"The publication does not exist in the database: {reference}"
msg1 = "Add it with astrodb_utils.ingest_publication function."
logger.debug(msg + msg1)
if raise_error:
logger.error(msg)
raise AstroDBError(msg + msg1)
else:
logger.warning(msg + msg1)
else:
msg = f"Spectral type ingest failed with error {e}\n"
if raise_error:
logger.error(msg)
raise AstroDBError(msg)
else:
logger.warning(msg)
# check that there is only one adopted measurement
check_one_adopted_sptype(db, db_name, raise_error=raise_error)
def convert_spt_string_to_code(spectral_type_string):
"""
normal tests: M0, M5.5, L0, L3.5, T0, T3, T4.5, Y0, Y5, Y9.
weird TESTS: sdM4, ≥Y4, T5pec, L2:, L0blue, Lpec, >L9, >M10, >L, T, Y
digits are needed in current implementation.
:param spectral_types:
:return:
"""
logger.debug(f"Trying to convert: `{spectral_type_string}`")
spt_code = np.nan
if spectral_type_string == "":
logger.debug("Empty spectral_type_string")
return None
if spectral_type_string == "null":
return None
# identify main spectral class, loop over any prefix text to identify MLTY
for i, item in enumerate(spectral_type_string):
if item == "M":
spt_code = 60
break
elif item == "L":
spt_code = 70
break
elif item == "T":
spt_code = 80
break
elif item == "Y":
spt_code = 90
break
else: # only trigger if not MLTY
i = 0
# find integer or decimal subclass and add to spt_code
if re.search(r"\d*\.?\d+", spectral_type_string[i + 1 :]) is None:
spt_code = spt_code
else:
spt_code += float(re.findall(r"\d*\.?\d+", spectral_type_string[i + 1 :])[0])
return spt_code
def convert_spt_code_to_string(spectral_code, decimals=1):
"""
Convert spectral type codes to string values
Parameters
----------
spectral_code : float
A spectral type code
decimals : int
Number of decimal places to include in the spectral type string
Returns
-------
spectral_type_string : str
spectral type string
"""
spt_type = ""
# Identify major type
if 60 <= spectral_code < 70:
spt_type = "M"
elif 70 <= spectral_code < 80:
spt_type = "L"
elif 80 <= spectral_code < 90:
spt_type = "T"
elif 90 <= spectral_code < 100:
spt_type = "Y"
# Numeric part of type
format = f".{decimals}f"
spt_type = f"{spt_type}{spectral_code% 10:{format}}"
logger.debug(f"Converting: {spectral_code} -> {spt_type}")
return spt_type
def adopt_spectral_type(db, source, spectral_type_error):
source_spt_data = (
db.query(db.SpectralTypes).filter(db.SpectralTypes.c.source == source).table()
)
# set adopted flag
if len(source_spt_data) == 0:
logger.debug(
"No Spectral Type data for this source in the database, setting adopted flag to True"
)
return True
elif len(source_spt_data) > 0:
# Spectral Type Data already exists
logger.debug("Pre-existing Spectral Type data:")
#if logger.level <= 10:
# source_spt_data.pprint_all()
adopted_ind = source_spt_data["adopted"] == 1
if sum(adopted_ind):
old_adopted = source_spt_data[adopted_ind]
logger.debug(f"Old adopted data: {old_adopted}")
if (
old_adopted["spectral_type_error"] is not None
and spectral_type_error is not None
):
if spectral_type_error < min(old_adopted["spectral_type_error"]):
adopted = True
logger.debug(f"The new spectral type's adopted flag is:, {adopted}")
else:
adopted = False
logger.debug(f"The new spectral type's adopted flag is: {adopted}")
return adopted
else:
return True
logger.debug(
"No spectral type error found, setting adopted flag to True"
)
else:
return True
logger.debug("No adopted data found, setting adopted flag to True")
def unset_previously_adopted(db, source):
source_spt_data = (
db.query(db.SpectralTypes).filter(db.SpectralTypes.c.source == source).table()
)
logger.debug(f"Pre-existing Spectral Type data: \n {source_spt_data}")
if len(source_spt_data) == 0:
logger.debug("No previous data for this source in the database, doing nothing")
return
elif len(source_spt_data) > 0:
# Spectral Type Data already exists
adopted_ind = source_spt_data["adopted"] == 1
if sum(adopted_ind):
old_adopted = source_spt_data[adopted_ind]
with db.engine.connect() as conn:
conn.execute(
db.SpectralTypes.update()
.where(
and_(
db.SpectralTypes.c.source == old_adopted["source"][0],
db.SpectralTypes.c.regime == old_adopted["regime"][0],
db.SpectralTypes.c.reference == old_adopted["reference"][0],
)
)
.values(adopted=False)
)
conn.commit()
else:
logger.debug("No previously adopted data found, doing nothing")
def check_one_adopted_sptype(db, source, raise_error=True):
results = (
db.query(db.SpectralTypes)
.filter(
and_(
db.SpectralTypes.c.source == source,
db.SpectralTypes.c.adopted == True, # noqa: E712
)
)
.table()
)
logger.debug(f"Adopted measurements for {source}:{results}")
#if logger.level <= 10:
# results.pprint_all()
logger.debug(f"adopted column: {results['adopted']}")
if len(results) == 1:
logger.debug(f"One adopted measurement for {source}")
elif len(results) > 2:
msg = f"Multiple adopted measurements for {source}"
if raise_error:
logger.error(msg)
raise AstroDBError(msg)
else:
logger.warning(msg)
elif len(results) == 0:
msg = f"No adopted measurements for {source}"
if raise_error:
logger.error(msg)
raise AstroDBError(msg)
else:
logger.warning(msg)