-
Notifications
You must be signed in to change notification settings - Fork 13
/
desi.py
1156 lines (925 loc) · 46.4 KB
/
desi.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
redrock.external.desi
=====================
redrock wrapper tools for DESI
"""
import os
import sys
import re
import warnings
import traceback
import itertools
import argparse
import numpy as np
from astropy.io import fits
from astropy.table import Table, vstack
from desiutil.io import encode_table
from desiutil.depend import add_dependencies, setdep
from desispec.resolution import Resolution
from desispec.coaddition import coadd_fibermap
from desispec.specscore import compute_coadd_tsnr_scores
from desispec.maskbits import fibermask
from desispec.io.util import get_tempfilename
from desispec.io.fibermap import annotate_fibermap
from ..utils import elapsed, get_mp, distribute_work, getGPUCountMPI
from ..targets import (Spectrum, Target, DistTargets)
from ..templates import load_dist_templates
from ..results import write_zscan
from ..zfind import zfind
from ..zwarning import ZWarningMask
from .._version import __version__
from ..archetypes import All_archetypes, Archetype
def _get_header(templates, archetypes=None, spec_header=None):
"""Get standardized header with template and archetype versions
Args:
templates : list or dict of template objects
Options:
archetypes : list or dict of Archetype objects
spec_header (dict-like): header of HDU 0 of input spectra/coadd
"""
if isinstance(templates, dict):
templates = templates.values()
if isinstance(archetypes, dict):
archetypes = archetypes.values()
header = fits.Header()
header['LONGSTRN'] = 'OGIP 1.0'
header['RRVER'] = (__version__, 'Redrock version')
for i, t in enumerate(templates):
header[f'TEMNAM{i:02d}'] = t.full_type
header[f'TEMVER{i:02d}'] = t.version
header[f'TEMFIL{i:02d}'] = os.path.basename(t.filename)
if archetypes is not None:
for i, atyp in enumerate(archetypes):
header[f'ARCNAM{i:02d}'] = atyp.template_type
header[f'ARCVER{i:02d}'] = atyp.version
header[f'ARCFIL{i:02d}'] = os.path.basename(atyp.filename)
# record code versions and key environment variables
add_dependencies(header)
for key in ['RR_TEMPLATE_DIR', 'RR_ARCHETYPE_DIR']:
if key in os.environ:
setdep(header, key, os.environ[key])
if spec_header is not None:
for key in ('SPGRP', 'SPGRPVAL', 'TILEID', 'SPECTRO', 'PETAL',
'NIGHT', 'EXPID', 'HPXPIXEL', 'HPXNSIDE', 'HPXNEST',
'SURVEY', 'PROGRAM', 'FAPRGRM'):
if key in spec_header:
header[key] = spec_header[key]
return header
def write_zbest(outfile, zbest, fibermap, exp_fibermap, tsnr2,
templates, archetypes=None,
spec_header=None):
"""Write zbest and fibermap Tables to outfile
Args:
outfile (str): output path.
zbest (Table): best fit table.
fibermap (Table): the coadded fibermap from the original inputs.
tsnr2 (Table): table of input coadded TSNR2 values
exp_fibermap (Table): the per-exposure fibermap from the orig inputs.
templates : list or dict of template objects
Options:
archetypes : list or dict of Archetype objects
spec_header (dict-like): header of HDU 0 of input spectra
Modifies input tables.meta['EXTNAME']
"""
header = _get_header(templates, archetypes, spec_header)
zbest.meta['EXTNAME'] = 'REDSHIFTS'
fibermap.meta['EXTNAME'] = 'FIBERMAP'
exp_fibermap.meta['EXTNAME'] = 'EXP_FIBERMAP'
tsnr2.meta['EXTNAME'] = 'TSNR2'
hx = fits.HDUList()
hx.append(fits.PrimaryHDU(header=header))
hx.append(fits.convenience.table_to_hdu(zbest))
# ADM annotate_fibermap adds units to fibermap HDUs.
hx.append(annotate_fibermap(fits.convenience.table_to_hdu(fibermap)))
hx.append(fits.convenience.table_to_hdu(exp_fibermap))
hx.append(fits.convenience.table_to_hdu(tsnr2))
outfile = os.path.expandvars(outfile)
outdir = os.path.dirname(os.path.abspath(outfile))
if not os.path.exists(outdir):
os.makedirs(outdir)
tempfile = outfile + '.tmp'
hx.writeto(tempfile, overwrite=True)
os.rename(tempfile, outfile)
return
def write_bestmodel(outfile, zbest, modeldict, wavedict, templates,
archetypes=None, spec_header=None):
"""
Writes best fit redrock model in the outfile
Args:
outfile (str): output file name (fits)
zbest (Table): best fit redshift table
modeldict (dict): models[ntargets, nwave] keyed by camera band
wavedict (dict): wavelength dictionary keyed by camera band
templates : list or dict of template objects
Options:
archetypes : list or dict of Archetype objects
spec_header (dict-like): header of HDU 0 of input spectra
The output file format mirrors the structure of an input coadd, with B/R/Z_MODEL
HDUs instead of B/R/Z_FLUX HDUs.
"""
header = _get_header(templates, archetypes, spec_header)
zbest.meta['EXTNAME'] = 'REDSHIFTS'
hx = fits.HDUList()
hx.append(fits.PrimaryHDU(header=header))
hx.append(fits.convenience.table_to_hdu(zbest))
for band in wavedict.keys():
BAND = band.upper()
hdu = fits.ImageHDU(name=f'{BAND}_WAVELENGTH')
hdu.data = wavedict[band]
hdu.header["BUNIT"] = "Angstrom"
hx.append(hdu)
hdu = fits.ImageHDU(name=f'{BAND}_MODEL')
hdu.data = modeldict[band].astype('float32')
hx.append(hdu)
outfile = os.path.expandvars(outfile)
outdir = os.path.dirname(os.path.abspath(outfile))
if not os.path.exists(outdir):
os.makedirs(outdir)
tempfile = get_tempfilename(outfile)
hx.writeto(tempfile, overwrite=True)
os.rename(tempfile, outfile)
return
class DistTargetsDESI(DistTargets):
"""Distributed targets for DESI.
DESI spectral data is grouped by sky location, but is just a random
collection of spectra for all targets. Read this into memory while
grouping by target ID, preserving order in which each target first appears.
We pass through the spectra files once to compute all the book-keeping
associated with regrouping the spectra by target. Then we pass through
again and actually read and distribute the data.
Args:
spectrafiles (str or list): a list of input files or pattern match
of files.
coadd (bool): if False, do not compute the coadds.
targetids (list): (optional) restrict the global set of target IDs
to this list.
first_target (int): (optional) integer offset of the first target to
consider in each file. Useful for debugging / testing.
n_target (int): (optional) number of targets to consider in each file.
Useful for debugging / testing.
comm (mpi4py.MPI.Comm): (optional) the MPI communicator.
cache_Rcsr: pre-calculate and cache sparse CSR format of resolution
matrix R
cosmics_nsig (float): cosmic rejection threshold used in coaddition
negflux_nsig (float): mask negative flux significance threshold
capacities (list): (optional) list of process capacities. If None,
use equal capacity per process. A process with higher capacity
can handle more work.
"""
### @profile
def __init__(self, spectrafiles, coadd=True, targetids=None,
first_target=None, n_target=None, comm=None, cache_Rcsr=False,
cosmics_nsig=0, negflux_nsig=5, capacities=None):
comm_size = 1
comm_rank = 0
if comm is not None:
comm_size = comm.size
comm_rank = comm.rank
# check the file list
if isinstance(spectrafiles, str):
import glob
spectrafiles = glob.glob(spectrafiles)
assert len(spectrafiles) > 0
self._spectrafiles = spectrafiles
self.cosmics_nsig = cosmics_nsig
# This is the mapping between specs to targets for each file
self._spec_to_target = {}
self._target_specs = {}
self._spec_keep = {}
self._spec_sliced = {}
# The bands for each file
self._bands = {}
self._wave = {}
# The full list of targets from all files
self._alltargetids = list()
# The fibermaps from all files
self._coadd_fmaps = {}
self._exp_fmaps = {}
self._tsnr2 = {} #- template signal-to-noise from SCORES
self.header0 = None #- header 0 of the first spectrafile
for sfile in spectrafiles:
hdus = None
nhdu = None
input_coadded = 'unknown'
coadd_fmap = None
exp_fmap = None
tsnr2 = None
if comm_rank == 0:
hdus = fits.open(sfile, memmap=False)
nhdu = len(hdus)
if self.header0 is None:
self.header0 = hdus[0].header.copy()
if 'EXP_FIBERMAP' in hdus:
input_coadded = True
coadd_fmap = encode_table(Table(hdus["FIBERMAP"].data,
copy=True).as_array())
exp_fmap = encode_table(Table(hdus["EXP_FIBERMAP"].data,
copy=True).as_array())
tsnr2 = encode_table(Table(hdus["SCORES"].data,
copy=True).as_array())
for col in tsnr2.colnames.copy():
if col == 'TARGETID' or col.startswith('TSNR2_'):
continue
else:
tsnr2.remove_column(col)
else:
input_coadded = False
tmpfmap = encode_table(Table(hdus["FIBERMAP"].data,
copy=True).as_array())
assert 'COADD_NUMEXP' not in tmpfmap.dtype.names
if np.all(tmpfmap['TILEID'] == tmpfmap['TILEID'][0]):
onetile = True
else:
onetile = False
coadd_fmap, exp_fmap = coadd_fibermap(tmpfmap, onetile=onetile)
scores = encode_table(Table(hdus["SCORES"].data,
copy=True).as_array())
tsnr2 = Table(compute_coadd_tsnr_scores(scores)[0])
#- we later rely upon exp_fmap having same order as the
#- uncoadded input fmap, so check that now
assert np.all(exp_fmap['TARGETID'] == tmpfmap['TARGETID'])
if comm is not None:
nhdu = comm.bcast(nhdu, root=0)
input_coadded = comm.bcast(input_coadded, root=0)
coadd_fmap = comm.bcast(coadd_fmap, root=0)
exp_fmap = comm.bcast(exp_fmap, root=0)
tsnr2 = comm.bcast(tsnr2, root=0)
self.header0 = comm.bcast(self.header0, root=0)
# Now every process has the fibermap and number of HDUs. Build the
# mapping between spectral rows and target IDs.
if targetids is None:
keep_targetids = coadd_fmap["TARGETID"]
else:
keep_targetids = targetids
# Select a subset of the target range from each file if desired.
if first_target is None:
first_target = 0
if first_target > len(keep_targetids):
raise RuntimeError("first_target value \"{}\" is beyond the "
"number of selected targets in the file".\
format(first_target))
if n_target is None:
nkeep = len(keep_targetids)
else:
nkeep = n_target
if first_target + nkeep > len(keep_targetids):
msg = "Requested first_target ({}) + nkeep ({})".format(
first_target, nkeep)
msg += " is larger than number of selected targets ({})".format(
len(keep_targetids))
raise RuntimeError(msg)
keep_targetids = keep_targetids[first_target:first_target+nkeep]
self._alltargetids.extend(keep_targetids)
# This is the spectral row to target mapping using the original
# global indices (before slicing).
if input_coadded:
input_targetids = coadd_fmap['TARGETID']
else:
input_targetids = exp_fmap['TARGETID']
self._spec_to_target[sfile] = [ x if y in keep_targetids else -1 \
for x, y in enumerate(input_targetids) ]
# The reduced set of spectral rows.
self._spec_keep[sfile] = [ x for x in self._spec_to_target[sfile] \
if x >= 0 ]
# The mapping between original spectral indices and the sliced ones
self._spec_sliced[sfile] = { x : y for y, x in \
enumerate(self._spec_keep[sfile]) }
# Slice the fibermap to keep just the requested targets
keep_coadd = np.isin(coadd_fmap['TARGETID'], keep_targetids)
self._coadd_fmaps[sfile] = coadd_fmap[keep_coadd]
self._tsnr2[sfile] = tsnr2[keep_coadd]
keep_exp = np.isin(exp_fmap['TARGETID'], keep_targetids)
self._exp_fmaps[sfile] = exp_fmap[keep_exp]
if input_coadded:
input_targetids = input_targetids[keep_coadd]
else:
input_targetids = input_targetids[keep_exp]
# For each target, store the sliced row index of all spectra,
# so that we can do a fast lookup later.
self._target_specs[sfile] = {}
for id in keep_targetids:
self._target_specs[sfile][id] = [ x for x, y in \
enumerate(input_targetids) if y == id ]
# We need some more metadata information for each file-
# specifically, the bands that are used and their wavelength grids.
# That information will allow us to pre-allocate our local target
# list and then fill that with one pass through all HDUs in the
# files.
self._bands[sfile] = []
self._wave[sfile] = dict()
if comm_rank == 0:
for h in range(nhdu):
name = None
if "EXTNAME" not in hdus[h].header:
continue
name = hdus[h].header["EXTNAME"]
mat = re.match(r"(.*)_(.*)", name)
if mat is None:
continue
band = mat.group(1).lower()
htype = mat.group(2)
if htype == "WAVELENGTH":
if band not in self._bands[sfile]:
self._bands[sfile].append(band)
self._wave[sfile][band] = \
hdus[h].data.astype(np.float64).copy()
if comm is not None:
self._bands[sfile] = comm.bcast(self._bands[sfile], root=0)
self._wave[sfile] = comm.bcast(self._wave[sfile], root=0)
if comm_rank == 0:
hdus.close()
# _alltargetids can have repeats from multiple files. Trim to
# unique set while retaining order in which they appeared
sortedidx = np.unique(self._alltargetids, return_index=True)[1]
ii = np.argsort(sortedidx)
unique_targetids = np.asarray(self._alltargetids)[sortedidx[ii]]
self._alltargetids = unique_targetids
self._keep_targets = unique_targetids.copy()
# Now we have the metadata for all targets in all files. Distribute
# the targets among process weighted by the amount of work to do for
# each target. This weight is either "1" if we are going to use coadds
# or the number of spectra if we are using all the data.
tweights = None
if not coadd:
tweights = dict()
for t in self._keep_targets:
tweights[t] = 0
for sfile in spectrafiles:
if t in self._target_specs[sfile]:
tweights[t] += len(self._target_specs[sfile][t])
self.capacities = capacities
if self.capacities is None:
self.is_lopsided = False
self._proc_targets = distribute_work(comm_size,
self._keep_targets, weights=tweights)
else:
self.is_lopsided = True
self._proc_targets = distribute_work(comm_size,
self._keep_targets, weights=tweights, capacities=self.capacities)
self._my_targets = self._proc_targets[comm_rank]
# Reverse mapping- target ID to index in our list
self._my_target_indx = {y : x for x, y in enumerate(self._my_targets)}
# Now every process has its local target IDs assigned. Pre-create our
# local target list with empty spectral data (except for wavelengths)
self._my_data = list()
for t in self._my_targets:
speclist = list()
for sfile in spectrafiles:
for b in self._bands[sfile]:
if t in self._target_specs[sfile]:
nspec = len(self._target_specs[sfile][t])
for s in range(nspec):
sindx = self._target_specs[sfile][t][s]
speclist.append(Spectrum(wave=self._wave[sfile][b],
flux=None, ivar=None, R=None, band=b))
self._my_data.append(Target(t, speclist, coadd=False))
# Iterate over the data and broadcast. Every process selects the rows
# of each table that contain pieces of local target data and copies it
# into place.
# these are for tracking offsets within the spectra for each target.
tspec_flux = { x : 0 for x in self._my_targets }
tspec_ivar = tspec_flux.copy()
tspec_mask = tspec_flux.copy()
tspec_res = tspec_flux.copy()
for sfile in spectrafiles:
rows = self._spec_keep[sfile]
if len(rows) == 0:
continue
hdus = None
if comm_rank == 0:
hdus = fits.open(sfile, memmap=False)
for b in self._bands[sfile]:
extname = "{}_{}".format(b.upper(), "FLUX")
hdata = None
badflux = None
if comm_rank == 0:
hdata = hdus[extname].data[rows]
# check for NaN and Inf here (should never happen of course)
badflux = np.isnan(hdata) | np.isinf(hdata) | np.isneginf(hdata)
hdata[badflux] = 0.0
if comm is not None:
hdata = comm.bcast(hdata, root=0)
badflux = comm.bcast(badflux, root=0)
toff = 0
for t in self._my_targets:
if t in self._target_specs[sfile]:
for trow in self._target_specs[sfile][t]:
self._my_data[toff].spectra[tspec_flux[t]].flux = \
hdata[trow].astype(np.float64).copy()
tspec_flux[t] += 1
toff += 1
extname = "{}_{}".format(b.upper(), "IVAR")
hdata = None
if comm_rank == 0:
hdata = hdus[extname].data[rows]
# check for NaN and Inf here (should never happen of course)
bad = np.isnan(hdata) | np.isinf(hdata) | np.isneginf(hdata)
hdata[bad] = 0.0
hdata[badflux] = 0.0 # also set ivar=0 to bad flux
if comm is not None:
hdata = comm.bcast(hdata, root=0)
toff = 0
for t in self._my_targets:
if t in self._target_specs[sfile]:
for trow in self._target_specs[sfile][t]:
self._my_data[toff].spectra[tspec_ivar[t]].ivar = \
hdata[trow].astype(np.float64).copy()
# mask significantly negative data (set ivar to 0)
sigma = self._my_data[toff].spectra[tspec_ivar[t]].flux * \
np.sqrt(self._my_data[toff].spectra[tspec_ivar[t]].ivar)
bignegflux = (sigma<-abs(negflux_nsig))
self._my_data[toff].spectra[tspec_ivar[t]].ivar[bignegflux] = 0.0
tspec_ivar[t] += 1
toff += 1
extname = "{}_{}".format(b.upper(), "MASK")
hdata = None
if comm_rank == 0:
if extname in hdus:
hdata = hdus[extname].data[rows]
if comm is not None:
hdata = comm.bcast(hdata, root=0)
if hdata is not None:
toff = 0
for t in self._my_targets:
if t in self._target_specs[sfile]:
for trow in self._target_specs[sfile][t]:
self._my_data[toff].spectra[tspec_mask[t]]\
.ivar *= (hdata[trow] == 0)
tspec_mask[t] += 1
toff += 1
extname = "{}_{}".format(b.upper(), "RESOLUTION")
hdata = None
if comm_rank == 0:
hdata = hdus[extname].data[rows]
if comm is not None:
hdata = comm.bcast(hdata, root=0)
toff = 0
for t in self._my_targets:
if t in self._target_specs[sfile]:
for trow in self._target_specs[sfile][t]:
dia = Resolution(hdata[trow].astype(np.float64))
self._my_data[toff].spectra[tspec_res[t]].R = dia
#- Coadds replace Rcsr so only compute if not coadding
if not coadd and cache_Rcsr:
self._my_data[toff].spectra[tspec_res[t]].Rcsr = dia.tocsr()
tspec_res[t] += 1
toff += 1
del hdata
if comm_rank == 0:
hdus.close()
# Compute the coadds now if we are going to use those
if coadd:
for t in self._my_data:
t.compute_coadd(cache_Rcsr,cosmics_nsig=self.cosmics_nsig)
self.fibermap = Table(np.hstack([ self._coadd_fmaps[x] \
for x in self._spectrafiles ]))
self.exp_fibermap = Table(np.hstack([ self._exp_fmaps[x] \
for x in self._spectrafiles ]))
self.tsnr2 = Table(np.hstack([ self._tsnr2[x] \
for x in self._spectrafiles ]))
super(DistTargetsDESI, self).__init__(self._keep_targets, comm=comm)
def _local_target_ids(self):
return self._my_targets
def _local_data(self):
return self._my_data
def rrdesi(options=None, comm=None):
"""Estimate redshifts for DESI targets.
This loads distributed DESI targets from one or more spectra grouping
files and computes the redshifts. The outputs are written to a redrock
scan file and a DESI redshift catalog.
Args:
options (list): optional list of commandline options to parse.
comm (mpi4py.Comm): MPI communicator to use.
"""
global_start = elapsed(None, "", comm=comm)
parser = argparse.ArgumentParser(description="Estimate redshifts from"
" DESI target spectra.")
parser.add_argument("-t", "--templates", type=str, default=None,
required=False, help="template file or directory")
parser.add_argument("--archetypes", type=str, default=None,
required=False,
help="archetype file or directory for final redshift comparison")
parser.add_argument("--archetype-legendre-degree", type=int, default=2,
required=False, help="if archetypes are provided legendre polynomials upto deg_legendre-1 will be used (default is 2)")
parser.add_argument("--archetype-nnearest", type=int, default=None,
required=False, help="number of nearest archetypes (in chi2 space) to be used in archetype modeling, must be greater than 1 (default is None)")
parser.add_argument("--archetype-legendre-percamera", default=True, action="store_true",
required=False, help="If True, in archetype mode the fitting will be done for each camera/band")
parser.add_argument("--archetype-legendre-prior", type=float, default=0.1,
required=False, help="sigma to add as prior in solving linear equation, 1/sig**2 will be added, default is 0.1, Note: provide anything <=0 if you do not want to add any prior")
parser.add_argument("--archetypes-no-legendre", default=False, action="store_true",
required=False, help="Use this flag with archetypes if want to TURN OFF all archetype related default values")
parser.add_argument("--zminfit-npoints", type=int, default=15,
required=False, help="number of finer redshift to be used around best fit redshifts (default is 15)")
parser.add_argument("-d", "--details", type=str, default=None,
required=False, help="output file for full redrock fit details")
parser.add_argument("-o", "--outfile", type=str, default=None,
required=False, help="output FITS file with best redshift per target")
parser.add_argument("--model", type=str, default=None,
required=False, help="output FITS file containing spectral model")
parser.add_argument("--targetids", type=str, default=None,
required=False, help="comma-separated list of target IDs")
parser.add_argument("--mintarget", type=int, default=None,
required=False, help="first target to process in each file")
parser.add_argument("--priors", type=str, default=None,
required=False, help="optional redshift prior file")
parser.add_argument("--chi2-scan", type=str, default=None,
required=False, help="Load the chi2-scan from the input file")
parser.add_argument("--zscan-galaxy", type=str, default='-0.005,1.7,3e-4',
required=False, help="Redshift scan parameters for galaxies: zmin,zmax,dz")
parser.add_argument("--zscan-qso", type=str, default='0.05,6.0,5e-4',
required=False, help="Redshift scan parameters for QSO: zmin,zmax,dz")
parser.add_argument("--zscan-star", type=str, default='-0.002,0.00201,4e-5',
required=False, help="Redshift scan parameters for stars: zmin,zmax,dz")
parser.add_argument("-n", "--ntargets", type=int,
required=False, help="the number of targets to process in each file")
parser.add_argument("--nminima", type=int, default=None,
required=False, help="the number of redshift minima to search")
parser.add_argument("--allspec", default=False, action="store_true",
required=False, help="use individual spectra instead of coadd")
parser.add_argument("--ncpu", type=int, default=None,
required=False, help="DEPRECATED: the number of multiprocessing"
" processes; use --mp instead")
parser.add_argument("--mp", type=int, default=0,
required=False, help="if not using MPI, the number of multiprocessing"
" processes to use (defaults to half of the hardware threads)")
parser.add_argument("--no-skymask", default=False, action="store_true",
required=False, help="Do not do extra masking of sky lines")
parser.add_argument("--no-mpi-abort", default=False, action="store_true",
required=False, help="Do not call MPI Abort upon failure of a single rank")
parser.add_argument("--debug", default=False, action="store_true",
required=False, help="debug with ipython (only if communicator has a "
"single process)")
parser.add_argument("--cosmics-nsig", type=float, default=0,
required=False, help="n sigma cosmic ray threshold in coaddition")
parser.add_argument("--negflux-nsig", type=float, default=5,
required=False, help="n sigma negative flux mask threshold")
parser.add_argument("-i", "--infiles", nargs='+', required=True,
help="Input spectra, coadd, or cframe files")
parser.add_argument("--gpu", action="store_true",
required=False, help="use GPUs")
parser.add_argument("--max-gpuprocs", type=int, default=None,
required=False, help="limit number of MPI processes using GPUs")
args = None
if options is None:
args = parser.parse_args()
else:
args = parser.parse_args(options)
if args.ncpu is not None:
print('WARNING: --ncpu is deprecated; use --mp instead')
args.mp = args.ncpu
comm_size = 1
comm_rank = 0
if comm is not None:
comm_size = comm.size
comm_rank = comm.rank
# Check arguments- all processes have this, so just check on the first
# process
# if user sets --nminima, use that
if args.nminima is not None:
nminima = args.nminima
# otherwise use different defaults for archetypes vs. non-archetypes
elif args.archetypes is None:
nminima = 3 # default for non-archetypes
else:
nminima = 9 # default for archetypes
if comm_rank == 0:
if args.debug and comm_size != 1:
print("--debug can only be used if the communicator has one "
" process")
sys.stdout.flush()
if comm is not None:
comm.Abort()
if (args.details is None) and (args.outfile is None):
parser.print_help()
print("ERROR: --details or --outfile required")
sys.stdout.flush()
if comm is not None:
comm.Abort()
else:
sys.exit(1)
if len(args.infiles) == 0:
print("ERROR: must provide input files")
sys.stdout.flush()
if comm is not None:
comm.Abort()
else:
sys.exit(1)
if (args.targetids is not None) and ((args.mintarget is not None) \
or (args.ntargets is not None)):
print("ERROR: cannot select targets by both ID and range")
sys.stdout.flush()
if comm is not None:
comm.Abort()
else:
sys.exit(1)
if args.gpu:
try:
import cupy
gpu_ok = cupy.is_available()
except ImportError:
gpu_ok = False
if not gpu_ok:
print("ERROR: cupy or GPU not available")
sys.stdout.flush()
if comm is not None:
comm.Abort()
else:
sys.exit(1)
if args.archetypes is not None:
print('\n===== Archetype argument is provided, doing necessary checks=======\n')
if os.path.exists(args.archetypes) and os.access(args.archetypes, os.R_OK):
if os.path.isdir(args.archetypes):
if os.listdir(args.archetypes):
print('Archetype directory exists and readable and it is not empty..')
print('Archetype will be applied to all spectype')
else:
print('ERROR: Archetype directory is empty')
sys.stdout.flush()
if comm is not None:
comm.Abort()
else:
sys.exit(1)
if os.path.isfile(args.archetypes):
print('Archetype is a file and it exists and readable')
print('Archetype will only be applied to SPECTYPE %s'%(os.path.basename(args.archetypes).split('-')[1].split('.')[0].upper()))
else:
print("ERROR: can't find archetypes_dir or it is unreadable")
sys.stdout.flush()
if comm is not None:
comm.Abort()
else:
sys.exit(1)
#- All ranks read the archetypes file(s)
archetypes = None
if args.archetypes is not None:
archetypes = All_archetypes(archetypes_dir=args.archetypes, verbose=(comm_rank==0)).archetypes
targetids = None
if args.targetids is not None:
targetids = [ int(x) for x in args.targetids.split(",") ]
n_target = None
if args.ntargets is not None:
n_target = args.ntargets
first_target = None
if args.mintarget is not None:
first_target = args.mintarget
elif n_target is not None:
first_target = 0
# Multiprocessing processes to use if MPI is disabled.
mpprocs = 0
if comm is None:
mpprocs = get_mp(args.mp)
print("Running with {} processes".format(mpprocs))
if "OMP_NUM_THREADS" in os.environ:
nthread = int(os.environ["OMP_NUM_THREADS"])
if nthread != 1:
print("WARNING: {} multiprocesses running, each with "
"{} threads ({} total)".format(mpprocs, nthread,
mpprocs*nthread))
print("WARNING: Please ensure this is <= the number of "
"physical cores on the system")
else:
print("WARNING: using multiprocessing, but the OMP_NUM_THREADS")
print("WARNING: environment variable is not set- your system may")
print("WARNING: be oversubscribed.")
sys.stdout.flush()
elif comm_rank == 0:
print("Running with {} processes".format(comm_size))
sys.stdout.flush()
# GPU configuration
if args.gpu:
# Determine which processes will use a GPU
if args.max_gpuprocs is not None:
max_gpuprocs = args.max_gpuprocs
else:
#Check actual number of GPUs available
if (comm is not None):
#Use custom method that checks PCI ids for MPI
max_gpuprocs = getGPUCountMPI(comm)
else:
#cupy getDeviceCount works for non MPI
import cupy
max_gpuprocs = cupy.cuda.runtime.getDeviceCount()
use_gpu = comm_rank < max_gpuprocs
# Determine cpu/gpu process capacities for target distribution
if comm is not None:
gpu_proc_flags = comm.allgather(use_gpu)
else:
gpu_proc_flags = [use_gpu, ]
if (mpprocs > 1):
#Force mpprocs == 1 for multiprocessing mode with GPU
print("WARNING: using GPU mode without MPI requires --mp 1")
print("WARNING: Overriding {} multiprocesses to force this.".format(mpprocs))
print("WARNING: Running with 1 process.")
mpprocs = 1
ngpu_procs = sum(gpu_proc_flags)
ncpu_procs = comm_size - ngpu_procs
if ngpu_procs > 0 and ncpu_procs > 0:
# On Perlmutter, 1:15 seems like a good ratio
#capacities = [1 if is_gpu_proc else 1.0/15 for is_gpu_proc in gpu_proc_flags]
#With new GPU implementation of zscan, use 1:10000 so that only GPU-enabled
#procs get allocated targets
capacities = [1 if is_gpu_proc else 1.0/10000 for is_gpu_proc in gpu_proc_flags]
else:
capacities = None
# Redistribute templates after rebinning when using GPUs
redistribute_templates = True
else:
use_gpu = False
capacities = None
redistribute_templates = False
try:
# Load and distribute the targets
if comm_rank == 0:
print("Loading targets...")
sys.stdout.flush()
start = elapsed(None, "", comm=comm)
# Load the targets. If comm is None, then the target data will be
# stored in shared memory.
targets = DistTargetsDESI(args.infiles, coadd=(not args.allspec),
targetids=targetids, first_target=first_target, n_target=n_target,
comm=comm, cache_Rcsr=True, cosmics_nsig=args.cosmics_nsig,
negflux_nsig=abs(args.negflux_nsig),
capacities=capacities)
#- Mask some problematic sky lines
if not args.no_skymask:
for t in targets.local():
for s in t.spectra:
ii = (5572. <= s.wave) & (s.wave <= 5582.)
ii |= (9792. <= s.wave) & (s.wave <= 9795.)
s.ivar[ii] = 0.0
# Get the dictionary of wavelength grids (with keys as wavehashes)
dwave = targets.wavegrids()
ncamera = len(list(dwave.keys())) # number of cameras for given instrument
if args.archetypes_no_legendre or args.archetypes is None:
if comm_rank == 0:
print('no archetypes or --archetypes-no-legendre; will turn off all the Legendre related arguments')
archetype_legendre_prior = None
archetype_legendre_degree =0
archetype_legendre_percamera = False
else:
if comm_rank == 0 and args.archetypes is not None:
print('Will be using default archetype values.')
print('Number of minimum redshift for which archetype redshift fitting will be done = %d'%(nminima))
if ncamera>=1:
archetype_legendre_percamera = True
if comm_rank == 0 and args.archetypes is not None:
print('Number of cameras = %d, percamera fitting will be done'%(ncamera))
else:
archetype_legendre_percamera = False
if comm_rank == 0 and args.archetypes is not None:
print('No per camera fitting will be done.')
if args.archetype_legendre_degree>0:
if comm_rank == 0 and args.archetypes is not None:
print('legendre polynomials of degrees %s will be added to Archetypes'%([i for i in range(args.archetype_legendre_degree)]))
else:
if comm_rank == 0 and args.archetypes is not None:
print('No Legendre polynomial will be added to archetypes.')
if args.archetype_legendre_prior<=0:
archetype_legendre_prior = None
printstr = 'no'
else:
archetype_legendre_prior = args.archetype_legendre_prior
printstr = 'a'
if comm_rank == 0 and args.archetypes is not None:
if archetype_legendre_prior is None:
print('A zero or negative prior is provided so setting archetype_legendre_prior = None')
print('archetype_legendre_prior = %s has been provided, so %s prior will be added while solving for the coefficients'%(archetype_legendre_prior, printstr))
if args.archetype_nnearest is not None:
if comm_rank == 0 and args.archetypes is not None:
print('nearest neighbour = %d is provided, will do the final fitting for N-best nearest archetypes in chi2 space..'%(args.archetype_nnearest))
stop = elapsed(start, "Read and distribution of {} targets"\
.format(len(targets.all_target_ids)), comm=comm)
# Read the template data
# Pass both use_gpu (this proc) and args.gpu (if any proc is using GPU)
dtemplates = load_dist_templates(dwave, templates=args.templates,
zscan_galaxy=args.zscan_galaxy,
zscan_qso=args.zscan_qso,
zscan_star=args.zscan_star,
comm=comm, mp_procs=mpprocs,
redistribute=redistribute_templates,
use_gpu=use_gpu, gpu_mode=args.gpu)
# Compute the redshifts, including both the coarse scan and the
# refinement. This function only returns data on the rank 0 process.