forked from all-umass/superman-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_datasets.py
418 lines (379 loc) · 18.5 KB
/
custom_datasets.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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# -*- coding: utf-8 -*-
import h5py
import logging
import numpy as np
import os.path
import pandas as pd
import sys
from numbers import Number
from six.moves import xrange
from superman.dana import dana_class_names
from superman.dataset import (
NumericMetadata, BooleanMetadata, PrimaryKeyMetadata, LookupMetadata,
CompositionMetadata, TagMetadata, DateMetadata)
# Helper function for loading HDF5 or NPZ files.
from backend.dataset_loaders import try_load
def load_mhc_multipower(ds, filepath, with_blr=False):
data = try_load(filepath, str(ds))
if data is None:
return False
kind = 'preprocessed' if with_blr else 'formatted'
s = data['/spectra/' + kind]
spectra = np.vstack((s['low_power'], s['med_power'], s['high_power']))
comps = data['/composition']
meta = data['/meta']
names = np.asarray(meta['names'])
idx = np.argsort(names)
powers = ('3.2', '5', '7')
pkey = ['%s @ %s%%' % (n, p) for p in powers for n in names]
comp_meta = {k: NumericMetadata(comps[k], display_name=k, repeats=3)
for k in comps.keys()}
ds.set_data(meta['waves'], spectra,
pkey=PrimaryKeyMetadata(pkey),
in_lanl=BooleanMetadata(meta['in_lanl'], 'In LANL', repeats=3),
name=LookupMetadata(names[idx], 'Name', labels=np.tile(idx, 3)),
power=LookupMetadata(powers, 'Laser Power',
labels=np.repeat([0, 1, 2], len(names))),
comp=CompositionMetadata(comp_meta, 'Composition'))
return True
def load_mars_big(ds, msl_ccs_dir):
# , pred_file, mixed_pred_file, moc_pred_file, dust_pred_file):
logging.info('Loading Mars (big) LIBS data...')
file_pattern = os.path.join(msl_ccs_dir, 'ccs.%03d.hdf5')
meta_file = os.path.join(msl_ccs_dir, 'ccs_meta.npz')
chan_file = os.path.join(msl_ccs_dir, 'ccs_channels.npy')
try:
import dask.array as da
except ImportError as e:
logging.warning('Failed to load Mars (big) LIBS library')
logging.warning(str(e))
return False
try:
f = h5py.File(file_pattern, mode='r', driver='family')
spectra = da.from_array(f['/spectra'], chunks=10000, name='mars_big')
bands = np.load(chan_file, allow_pickle=True)
meta = np.load(meta_file, allow_pickle=True)
# comps = np.load(pred_file, allow_pickle=True)
# mixed_comps = np.load(mixed_pred_file, allow_pickle=True)
# moc_comps = np.load(moc_pred_file, allow_pickle=True)
# TODO: store this in a better way
# is_dust = np.load(dust_pred_file, allow_pickle=True)['Is dust?'].astype(bool)
except IOError as e:
logging.warning('Failed to load Mars (big) LIBS data!')
logging.warning(str(e))
return False
logging.info('Making Mars (big) metadata...')
# Predicted compositions
# pred_comps = {elem: NumericMetadata(comps[elem], display_name=elem)
# for elem in comps.files}
# mixed_pred_comps = {elem: NumericMetadata(mixed_comps[elem],display_name=elem)
# for elem in mixed_comps.files}
# moc_pred_comps = {elem: NumericMetadata(moc_comps[elem], display_name=elem)
# for elem in moc_comps.files}
# Cal target uct_mask
uniq_targets, target_labels = np.unique(meta['names'], return_inverse=True)
uct_mask = np.array([n.startswith(b'Cal Target') or n.startswith(b'Cal_Target')
for n in uniq_targets], dtype=bool)
ct_mask = np.in1d(target_labels, uct_mask.nonzero()[0])
# Location numbers (per target)
uniq_ids, id_labels = np.unique(meta['ids'], return_inverse=True)
locations = np.zeros_like(target_labels)
for tl in xrange(len(uniq_targets)):
mask = target_labels == tl
locations[mask] = 1 + np.unique(id_labels[mask], return_inverse=True)[1]
ds.set_data(
bands, spectra,
target=LookupMetadata(uniq_targets, 'Target Name', labels=target_labels),
dist=NumericMetadata(meta['distances'], 0.1, 'Standoff Distance'),
sol=NumericMetadata(meta['sols'], 1, 'Sol #'),
shot=NumericMetadata(meta['numbers'], 1, 'Shot #'),
Autofocus=BooleanMetadata(meta['foci']),
id=LookupMetadata(uniq_ids, 'Instrument ID', labels=id_labels),
# umass_pred=CompositionMetadata(pred_comps, 'LANL PLS'),
# mixed_pred=CompositionMetadata(mixed_pred_comps, 'MHC/LANL/Mars PLS'),
# moc_pred=CompositionMetadata(moc_pred_comps, 'MOC (ChemCam Team)'),
# adust=BooleanMetadata(is_dust, 'Dust? (predicted)'),
caltarget=BooleanMetadata(ct_mask, 'Cal Target?'),
loc=NumericMetadata(locations, 1, 'Location #'))
logging.info('Finished Mars (big) setup.')
return True
def load_usda(ds, filepath):
usda = try_load(filepath, str(ds))
if usda is None:
return False
filenames, keys = usda['key'].T
bands = usda['data_names']
# XXX: There are 183 different compositions here, but I'm just looking at
# the first three. If we want to add more, do so here.
comp_names = [n.split(',', 1)[0] for n in usda['target_names'][:3]]
comp_vals = usda['target'][:, :3].T
comp_meta = {name: NumericMetadata(arr, display_name=name) for name, arr
in zip(comp_names, comp_vals)}
ds.set_data(bands, usda['data'], pkey=PrimaryKeyMetadata(filenames),
key=LookupMetadata(keys, 'Natural Key'),
Composition=CompositionMetadata(comp_meta))
return True
def load_corn(ds, filepath):
data = try_load(filepath, str(ds))
if data is None:
return False
# band info given by: http://www.eigenvector.com/data/Corn/index.html
bands = np.arange(1100, 2500, 2).astype(float)
instrument_names = ['m5', 'mp5', 'mp6']
spectra = np.vstack([data['/spectra/' + n] for n in instrument_names])
metadata = {key: NumericMetadata(val, repeats=3)
for key, val in data['/meta'].items()}
metadata['inst'] = LookupMetadata(instrument_names,
display_name='Spectrometer',
labels=np.repeat(np.arange(3), 80))
ds.set_data(bands, spectra, **metadata)
return True
def load_mhc_hydrogen(ds, filepath):
hdf5 = try_load(filepath, str(ds))
if hdf5 is None:
return False
powers = hdf5['/meta/powers']
names = hdf5['/meta/names']
pkey = ['%s - %s%%' % (name, power) for name, power in zip(names, powers)]
bands = hdf5['/meta/waves']
comp = {'H2O': NumericMetadata(hdf5['/composition/H2O'], display_name='H2O')}
ds.set_data(bands, hdf5['/spectra'],
pkey=PrimaryKeyMetadata(pkey),
Composition=CompositionMetadata(comp),
names=LookupMetadata(names, 'Sample Name'),
powers=LookupMetadata(powers, 'Laser Power'))
return True
def load_mhc_libs(ds, data_dir, master_file):
logging.info('Loading LIBS data for {}...'.format(ds.name))
data_file = os.path.join(data_dir, 'prepro_no_blr.%03d.hdf5')
chan_file = os.path.join(data_dir, 'prepro_channels.npy')
try:
hdf5 = h5py.File(data_file, driver='family', mode='r')
meta = np.load(master_file, allow_pickle=True)
bands = np.load(chan_file, allow_pickle=True)
except IOError as e:
logging.warning('Failed to load data in %s!' % data_dir)
logging.warning(str(e))
return None
logging.info('Making {} metadata...'.format(ds.name))
projects = [set(filter(None, p.split(','))) for p in meta['Projects']]
if ds.name.startswith('SuperLIBS'):
dates = [str(d) for d in meta['Date']]
else:
dates = [d.decode() for d in meta['Date']]
matrices = [str(m) for m in meta['Matrix']]
compositions = {}
for key in meta.files:
if key.startswith('e_'):
vals = np.array(meta[key], dtype=float, copy=False)
min_val = np.nanmin(vals)
max_val = np.nanmax(vals)
if min_val < max_val:
elem = key.lstrip('e_').replace('*', '')
compositions[elem] = NumericMetadata(vals, display_name=elem)
else:
logging.warning('Invalid values for %s composition %s: %s >= %s over %s', ds.name, key, min_val, max_val, meta[key])
logging.info('Got {} compositions; calling ds.set_data()'.format(ds.name))
try:
ds.set_data(bands, hdf5['/spectra'],
Composition=CompositionMetadata(compositions),
samples=LookupMetadata(meta['Sample'], 'Sample Name'),
carousels=LookupMetadata(meta['Carousel'], 'Carousel #'),
locations=LookupMetadata(meta['Location'], 'Location #'),
shots=NumericMetadata(meta['Number'], 1, 'Shot #'),
targets=LookupMetadata(meta['Target'], 'Target Name'),
powers=LookupMetadata(meta['LaserAttenuation'], 'Laser Power'),
projects=TagMetadata(projects, 'Project'),
date=DateMetadata(pd.to_datetime(dates),
display_name='Acquisition Time'),
atmospheres=LookupMetadata(meta['Atmosphere'], 'Atmosphere'),
dists=LookupMetadata(meta['DistToTarget'],
'Distance to Target'),
rock_types=LookupMetadata(meta['TASRockType'],
'TAS Rock Type'),
randoms=NumericMetadata(meta['RandomNumber'],
display_name='Random Number'),
matrices=LookupMetadata(matrices, 'Matrix'),
dopant_concs=NumericMetadata(meta['ApproxDopantConc'],
display_name='Approx. Dopant Conc.')
)
except Exception as e:
logging.error('{0} loader exception: {1}: {2}'.format(ds.name, type(e).__name__, str(e)))
raise
logging.info('Finished {} setup.'.format(ds.name))
return True
def load_mhc_superlibs(ds, data_dir, master_file):
logging.info('Loading SuperLIBS data for {}...'.format(ds.name))
data_file = os.path.join(data_dir, 'prepro_no_blr.%03d.hdf5')
try:
hdf5 = h5py.File(data_file, driver='family', mode='r')
meta = np.load(master_file, allow_pickle=True)
except IOError as e:
logging.warning('Failed to load data in %s!' % data_dir)
logging.warning(str(e))
return None
logging.info('Making SuperLIBS metadata for {}...'.format(ds.name))
pkey = np.array(meta['pkey'], dtype=bytes)
projects = [set(filter(None, p.split(','))) for p in meta['Projects']]
dates = [d.decode() for d in meta['Date']]
matrices = [str(m) for m in meta['Matrix']]
metakwargs = {
'si': NumericMetadata(meta['Si Ratio'], display_name='Si Ratio'),
'samples': LookupMetadata(meta['Sample'], 'Sample Name'),
'carousels': LookupMetadata(meta['Carousel'], 'Carousel #'),
'locations': LookupMetadata(meta['Location'], 'Location #'),
'shots': NumericMetadata(meta['Number'], 1, 'Shot #'),
'targets': LookupMetadata(meta['Target'], 'Target Name'),
'powers': LookupMetadata(meta['LaserAttenuation'], 'Laser Power'),
'projects': TagMetadata(projects, 'Project'),
'date': DateMetadata(pd.to_datetime(dates),
display_name='Acquisition Time'),
'atmospheres': LookupMetadata(meta['Atmosphere'], 'Atmosphere'),
'dists': LookupMetadata(meta['DistToTarget'], 'Distance to Target'),
'rock_types': LookupMetadata(meta['TASRockType'], 'TAS Rock Type'),
'randoms': NumericMetadata(meta['RandomNumber'],
display_name='Random Number'),
'matrices': LookupMetadata(matrices, 'Matrix'),
}
# Only include dopant concentrations if we have non-NaN values.
dopants = np.array(meta['ApproxDopantConc'], dtype=float, copy=False)
if np.isnan(dopants).all():
logging.warning('No non-NaN values for {} Approx. Dopant Conc.'.format(ds.name))
else:
adc = NumericMetadata(meta['ApproxDopantConc'],
display_name='Approx. Dopant Conc.')
metakwargs['dopant_concs'] = adc
compositions = {}
for key in meta.files:
if not key.startswith('e_'):
continue
vals = np.array(meta[key], dtype=float, copy=False)
if np.isnan(vals).all():
logging.warning('No non-NaN values for {0} composition {1}'.format(ds.name, key))
continue
min_val = np.nanmin(vals)
max_val = np.nanmax(vals)
if min_val < max_val:
elem = key.lstrip('e_').replace('*', '')
compositions[elem] = NumericMetadata(vals, display_name=elem)
else:
logging.warning('Invalid values for {0} composition {1}: {2} >= {3}'.format(ds.name, key, min_val, max_val))
metakwargs['Composition'] = CompositionMetadata(compositions)
logging.info('Got compositions for {}; calling ds.set_data()'.format(ds.name))
try:
ds.set_data(pkey, hdf5['/spectra'], **metakwargs)
except Exception as e:
logging.error('SuperLIBS loader exception for {0}: {1}: {2}'.format(ds.name, type(e).__name__, str(e)))
raise
logging.info('Finished SuperLIBS setup for {}.'.format(ds.name))
return True
def load_mhc_raman(ds, data_dir, meta_file):
data_file = os.path.join(data_dir, 'raman.hdf5')
logging.info('Loading MHC Raman data...')
try:
hdf5 = h5py.File(data_file, mode='r')
meta = np.load(meta_file, allow_pickle=True)
except IOError as e:
logging.warning('Failed to load data in %s!' % data_dir)
logging.warning(str(e))
return None
pkey = np.array(meta['spectrum_number'], dtype=bytes)
def _utolower(array):
# return [spec.lower() if spec is not None else None for spec in array]
return [spec.lower() if spec is not None else '' for spec in array]
def str_to_none(field):
return [mix if isinstance(mix, Number) else None for mix in field]
try:
ds.set_data(pkey, hdf5['/spectra'],
vial=LookupMetadata(meta['vial_name'], 'Vial Name'),
Instrument=LookupMetadata(meta['instrument']),
Project=LookupMetadata([str(p) for p in meta['project']]),
SpeciesA=LookupMetadata(_utolower(meta['conf_species_A']),
display_name='Species A'),
SpeciesB=LookupMetadata(_utolower(meta['conf_species_B']),
display_name='Species B'),
SpeciesC=LookupMetadata(_utolower(meta['conf_species_C']),
display_name='Species C'),
AmountA=NumericMetadata(str_to_none(meta['#_in_mix_A']),
display_name='%A'),
AmountB=NumericMetadata(str_to_none(meta['#_in_mix_B']),
display_name='%B'),
AmountC=NumericMetadata(str_to_none(meta['#_in_mix_C']),
display_name='%C')
)
except Exception as e:
logging.error('MHC Raman loader exception: {0}: {1}'.format(type(e).__name__, str(e)))
raise
logging.info('Finished MHC Raman setup.')
return True
def load_mhc_mossbauer(ds, data_dir, meta_file):
logging.info('Loading MHC Mossbauer data...')
data_file = os.path.join(data_dir, 'mossbauer.hdf5')
# jproctor 2020-05-11 switch to traj
# chan_file = os.path.join(data_dir, 'channels.npy')
try:
hdf5 = h5py.File(data_file, mode='r')
meta = np.load(meta_file, allow_pickle=True)
# jproctor 2020-05-11 switch to traj
# bands = np.load(chan_file, allow_pickle=True)
except IOError as e:
logging.warning('Failed to load data in %s!' % data_dir)
logging.warning(str(e))
return None
# convert Dana class names
# projects = [set(filter(None, p.split(','))) for p in meta['Projects']]
dana_nums, dana_labels = np.unique(meta['Dana Group'], return_inverse=True)
dana_classes = []
for d in dana_nums:
try:
d = int(d)
except (ValueError, TypeError):
dana_classes.append('N/A')
else:
dana_classes.append(dana_class_names.get(d, 'Unknown'))
dana = LookupMetadata(dana_classes, labels=dana_labels,
display_name='Dana Class')
# TODO: Convert temps to numeric form.
# Currently it's mostly numeric, with some free-form garbage values.
temp = LookupMetadata(meta['T(K)'], display_name='Temperature (K)')
pkey = meta['Sample #']
sources = [str(s) for s in meta['Owner/Source']]
ds.set_data(pkey, hdf5['/spectra'], temp=temp, dana=dana,
# jproctor 2020-05-11 switch to traj:
# pkey=PrimaryKeyMetadata(meta['Sample #']),
name=LookupMetadata(meta['Sample Name'],
display_name='Sample Name'),
folder=LookupMetadata(meta['Group Folder']),
source=LookupMetadata(sources, display_name='Owner/Source'),
)
return True
def load_mhc_xrf(ds, data_file, meta_file):
logging.info('Loading MHC XRF data...')
try:
hdf5 = h5py.File(data_file, mode='r')
meta = np.load(meta_file, allow_pickle=True)
except IOError as e:
logging.warning('Failed to load data file %s!' % data_file)
logging.warning(str(e))
return None
pkey = np.array(meta['spectrum_number'], dtype=bytes)
compositions = {}
for key in sorted(meta.files):
# TODO figure out why e_Hg doesn't work
if key.startswith('e_') and key != 'e_Hg':
vals = np.array(meta[key], dtype=float, copy=False)
if not np.isnan(vals).all() and np.nanmin(vals) < np.nanmax(vals):
elem = key.lstrip('e_').replace('*', '')
compositions[elem] = NumericMetadata(vals, display_name=elem)
ds.set_data(
pkey, hdf5['/spectra'],
Composition=CompositionMetadata(compositions),
Instrument=TagMetadata([tags.split() for tags in meta['Project']]),
Pellet=LookupMetadata(meta['pellet_name']),
Filter=LookupMetadata(meta['Filter']),
Dopant=LookupMetadata(meta['instrument']),
Matrix=LookupMetadata(meta['Vacuum']),
Duration=NumericMetadata(meta['Duration Time'],
display_name='Duration Time'),
)
return True