-
Notifications
You must be signed in to change notification settings - Fork 12
/
input.py
1126 lines (986 loc) · 54 KB
/
input.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
# Copyright (c) 2016 by Mike Jarvis and the other collaborators on GitHub at
# https://github.com/rmjarvis/Piff All rights reserved.
#
# Piff is free software: Redistribution and use in source and binary forms
# with or without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the disclaimer given in the accompanying LICENSE
# file.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the disclaimer given in the documentation
# and/or other materials provided with the distribution.
"""
.. module:: input
"""
from __future__ import print_function
from past.builtins import basestring
import numpy as np
import scipy
import glob
import os
import galsim
from .util import run_multi, calculateSNR
from .star import Star, StarData
class Input(object):
"""The base class for handling inputs for building a Piff model.
This is essentially an abstract base class intended to define the methods that should be
implemented by any derived class.
"""
nproc = 1 # Sub-classes can overwrite this as an instance attribute.
@classmethod
def process(cls, config_input, logger=None):
"""Parse the input field of the config dict.
:param config_input: The configuration dict.
:param logger: A logger object for logging debug info. [default: None]
:returns: stars, wcs, pointing
stars is a list of Star instances with the initial data.
wcs is a dict of WCS solutions indexed by chipnum.
pointing is either a galsim.CelestialCoord or None.
"""
import piff
# Get the class to use for handling the input data
# Default type is 'Files'
input_handler_class = getattr(piff, 'Input' + config_input.get('type','Files'))
# Build handler object
input_handler = input_handler_class(config_input, logger)
# Creat a lit of StarData objects
stars = input_handler.makeStars(logger)
if len(stars) == 0:
raise RuntimeError("No stars read in from input catalog(s).")
# Get the wcs for all the input chips
wcs = input_handler.getWCS(logger)
# Get the pointing (the coordinate center of the field of view)
pointing = input_handler.getPointing(logger)
return stars, wcs, pointing
def makeStars(self, logger=None):
"""Process the input images and star data, cutting out stamps for each star along with
other relevant information.
The base class implementation expects the derived class to have appropriately set the
following attributes:
:stamp_size: The size of the postage stamp to use for the cutouts
:x_col: The name of the column in the catalogs to use for the x position.
:y_col: The name of the column in the catalogs to use for the y position.
:param logger: A logger object for logging debug info. [default: None]
:returns: a list of Star instances
"""
logger = galsim.config.LoggerWrapper(logger)
if self.nimages == 1:
logger.debug("Making star list")
else:
logger.debug("Making star list from %d catalogs", self.nimages)
args = [(self.__class__,
self.image_kwargs[k], self.cat_kwargs[k], self.wcs_list[k], self.chipnums[k])
for k in range(self.nimages)]
kwargs = dict(stamp_size=self.stamp_size, min_snr=self.min_snr, max_snr=self.max_snr,
pointing=self.pointing, use_partial=self.use_partial,
invert_weight=self.invert_weight,
remove_signal_from_weight=self.remove_signal_from_weight,
hsm_size_reject=self.hsm_size_reject,
max_mask_pixels=self.max_mask_pixels,
max_edge_frac=self.max_edge_frac,
stamp_center_size=self.stamp_center_size)
all_stars = run_multi(call_makeStarsFromImage, self.nproc, raise_except=True,
args=args, logger=logger, kwargs=kwargs)
# Apply the reserve separately on each ccd, so they each reserve 20% of their stars
# (or whatever fraction). We wouldn't want to accidentally reserve all the stars on
# one of the ccds by accident, for instance.
if self.reserve_frac != 0:
for stars in all_stars:
if stars is None or len(stars) == 0:
continue
# Mark a fraction of the stars as reserve stars
nreserve = int(self.reserve_frac * len(stars)) # round down
logger.info("Reserve %s of %s (reserve_frac=%s) input stars",
nreserve, len(stars), self.reserve_frac)
reserve_list = self.rng.choice(len(stars), nreserve, replace=False)
for i, star in enumerate(stars):
star.data.properties['is_reserve'] = i in reserve_list
# Concatenate the star lists into a single list
stars = [s for slist in all_stars if slist is not None for s in slist if slist]
logger.warning("Read a total of %d stars from %d image%s",len(stars),self.nimages,
"s" if self.nimages > 1 else "")
return stars
def getWCS(self, logger=None):
"""Get the WCS solutions for all the chips in the field of view.
:param logger: A logger object for logging debug info. [default: None]
:returns: a dict of WCS solutions (galsim.BaseWCS instances) indexed by chipnum
"""
return { chipnum : w for w, chipnum in zip(self.wcs_list, self.chipnums) }
def getPointing(self, logger=None):
"""Get the pointing coordinate of the (noinal) center of the field of view.
:param logger: A logger object for logging debug info. [default: None]
:returns: a galsim.CelestialCoord of the pointing direction.
"""
return self.pointing
class InputFiles(Input):
"""An Input handler than just takes a list of image files and catalog files.
"""
def __init__(self, config, logger=None):
"""
Parse the input config dict (Normally the 'input' field in the overall configuration dict).
The two required fields in the input dict are:
:image_file_name: The file name(s) of the input image(s).
:cat_file_name: The file name(s) of the input catalog(s).
There are a number of ways to specify these file names.
1. A string giving a single file name. e.g.::
image_file_name: image.fits
cat_file_name: input_cat.fits
2. A list of several file names. e.g.::
image_file_name: [image_00.fits, image_01.fits, image_02.fits]
cat_file_name: [input_cat_00.fits, input_cat_01.fits, input_cat_02.fits]
3. A string that glob can recognize to list several file names. e.g.::
image_file_name: image_*.fits
cat_file_name: input_cat_*.fits
4. A dict parseable as a string value according to the GalSim configuration parsing types.
In this case, you also must specify nimages to say how many file names to generate
in this way. e.g.::
nimages: 20
image_file_name:
type: FormattedStr
format: image_%03d_%02d.fits.fz
items:
- { type : Sequence, first: 0, repeat: 4 } # Exposure number
- { type : Sequence, first: 1, last: 4 } # Chip number
cat_file_name:
type: Eval
str: "image_file_name.replace('image','input_cat')"
simage_file_name: '@input.image_file_name'
See the description of the GalSim config parser for more details about the various
types that are valid here.
`https://github.com/GalSim-developers/GalSim/wiki/Config-Values`_
There are many other optional parameters, which help govern how the input files are
read or interporeted:
:chipnum: The id number of this chip used to reference this image [default:
image_num]
:image_hdu: The hdu to use in the image files. [default: None, which means use
either 0 or 1 as typical given the compression sceme of the file]
:weight_hdu: The hdu to use for weight images. [default: None, which means a weight
image with all 1's will be automatically created]
:badpix_hdu: The hdu to use for badpix images. Pixels with badpix != 0 will be given
weight == 0. [default: None]
:noise: Rather than a weight image, provide the noise variance in the image.
(Useful for simulations where this is a known value.) [default: None]
:cat_hdu: The hdu to use in the catalog files. [default: 1]
:x_col: The name of the X column in the input catalogs. [default: 'x']
:y_col: The name of the Y column in the input catalogs. [default: 'y']
:ra_col: (Alternative to x_col, y_col) The name of a right ascension column in
the input catalogs. Will use the WCS to find (x,y) [default: None]
:dec_col: (Alternative to x_col, y_col) The name of a declination column in
the input catalogs. Will use the WCS to find (x,y) [default: None]
:flag_col: The name of a flag column in the input catalogs. [default: None]
By default, this will skip any objects with flag != 0, but see
skip_flag and use_flag for other possible meanings for how the
flag column can be used to select stars.
:skip_flag: The flag indicating which items to not use. [default: -1]
Items with flag & skip_flag != 0 will be skipped.
:use_flag: The flag indicating which items to use. [default: None]
Items with flag & use_flag == 0 will be skipped.
:sky_col: The name of a column with sky values. [default: None]
:gain_col: The name of a column with gain values. [default: None]
:sky: The sky level to subtract from the image values. [default: None]
Note: It is an error to specify both sky and sky_col. If both are None,
no sky level will be subtracted off.
:gain: The gain to use for adding Poisson noise to the weight map. [default:
None] It is an error for both gain and gain_col to be specified.
If both are None, then no additional noise will be added to account
for the Poisson noise from the galaxy flux.
:satur: The staturation level. If any pixels for a star exceed this, then
the star is skipped. [default: None]
:min_snr: The minimum S/N ratio to use. If an input star is too faint, it is
removed from the input list of PSF stars.
:max_snr: The maximum S/N ratio to allow for any given star. If an input star
is too bright, it can have too large an influence on the interpolation,
so this parameter limits the effective S/N of any single star.
Basically, it adds noise to bright stars to lower their S/N down to
this value. [default: 100]
:max_edge_frac: Cutoff on the fraction of the flux comming from pixels on the edges of
the postage stamp. [default: None]
:stamp_center_size: Distance from center of postage stamp (in pixels) to consider as
defining the edge of the stamp for the purpose of the max_edge_fact cut.
The default value of 13 is most of the radius of a 32x32 stamp size.
If you change stamp_size, you should consider what makes sense here.
[default 13].
:max_mask_pixels: If given, reject stars with more than this many masked pixels
(i.e. those with w=0). [default: None]
:use_partial: Whether to use stars whose postage stamps are only partially on the
full image. [default: False]
:hsm_size_reject: Whether to reject stars with a very different hsm-measured size than
the other stars in the input catalog. (Used to reject objects with
neighbors or other junk in the postage stamp.) [default: False]
If this is a float value, it gives the number of inter-quartile-ranges
to use for rejection relative to the median. hsm_size_reject=True
is equivalent to hsm_size_reject=10.
:nstars: Stop reading the input file at this many stars. (This is applied
separately to each input catalog.) [default: None]
:nproc: How many multiprocessing processes to use for reading in data from
multiple files at once. [default: 1]
:reserve_frac: Reserve a fraction of the stars from the PSF calculations, so they
can serve as fair points for diagnostic testing. These stars will
not be used to constrain the PSF model, but the output files will
contain the reserve stars, flagged as such. Generally 0.2 is a
good choice if you are going to use this. [default: 0.]
:seed: A seed to use for numpy.random.default_rng, if desired. [default: None]
:wcs: Normally, the wcs is automatically read in when reading the image.
However, this parameter allows you to optionally provide a different
WCS. It should be defined using the same style as a wcs object
in GalSim config files. [defulat: None]
The above values are parsed separately for each input image/catalog. In addition, there
are a couple other parameters that are just parsed once:
:stamp_size: The size of the postage stamps to use for the cutouts. Note: some
stamps may be smaller than this if the star is near a chip boundary.
[default: 32]
:ra, dec: The RA, Dec of the telescope pointing. [default: None; See
:setPointing: for details about how this can be specified]
:param config: The configuration dict used to define the above parameters.
:param logger: A logger object for logging debug info. [default: None]
"""
import copy
logger = galsim.config.LoggerWrapper(logger)
req = { 'image_file_name': str,
'cat_file_name': str,
}
opt = {
'dir' : str,
'chipnum' : int,
'x_col' : str,
'y_col' : str,
'ra_col' : str,
'dec_col' : str,
'ra_units' : str,
'dec_units' : str,
'sky_col' : str,
'gain_col' : str,
'flag_col' : str,
'skip_flag' : int,
'use_flag' : int,
'image_hdu' : int,
'weight_hdu' : int,
'badpix_hdu' : int,
'cat_hdu' : int,
'invert_weight' : bool,
'remove_signal_from_weight' : bool,
'stamp_size' : int,
'gain' : str,
'satur' : str,
'min_snr' : float,
'max_snr' : float,
'use_partial' : bool,
'hsm_size_reject' : float,
'max_edge_frac': float,
'stamp_center_size': float,
'max_mask_pixels' : int,
'sky' : str,
'noise' : float,
'nstars' : int,
'reserve_frac' : float,
'seed' : int,
}
ignore = [ 'nproc', 'nimages', 'ra', 'dec', 'wcs' ] # These are parsed separately
# We're going to change the config dict a bit. Make a copy so we don't mess up the
# user's original dict (in case they care).
config = copy.deepcopy(config)
# In GalSim, the base dict holds additional parameters that may be of use.
# Here, we just make a dict with a few values that could be relevant.
base = { 'input' : config,
'index_key' : 'image_num',
}
# Convert options 2 and 3 above into option 4. (1 is also parseable by GalSim's config.)
nimages = None
image_list = None
cat_list = None
dir = None
if 'nproc' in config:
self.nproc = galsim.config.ParseValue(config, 'nproc', base, int)[0]
if 'nimages' in config:
nimages = galsim.config.ParseValue(config, 'nimages', base, int)[0]
if nimages < 1:
raise ValueError('input.nimages must be >= 1')
# Deal with dir here, since sometimes we need to have it already atteched for glob
# to work.
if 'dir' in config:
dir = galsim.config.ParseValue(config, 'dir', base, str)[0]
del config['dir']
if 'image_file_name' not in config:
raise TypeError('Parameter image_file_name is required')
elif isinstance(config['image_file_name'], list):
image_list = config['image_file_name']
if len(image_list) == 0:
raise ValueError("image_file_name may not be an empty list")
if dir is not None:
image_list = [os.path.join(dir, n) for n in image_list]
elif isinstance(config['image_file_name'], basestring):
image_file_name = config['image_file_name']
if dir is not None:
image_file_name = os.path.join(dir, image_file_name)
image_list = sorted(glob.glob(image_file_name))
if len(image_list) == 0:
raise ValueError("No files found corresponding to "+config['image_file_name'])
elif isinstance(config['image_file_name'], dict):
if nimages is None:
raise TypeError(
'input.nimages is required if not using a list or simple string for ' +
'file names')
else:
raise ValueError("image_file_name should be either a dict or a string")
if image_list is not None:
logger.debug('image_list = %s',image_list)
if nimages is not None and nimages != len(image_list):
raise ValueError("nimages = %s doesn't match length of image_file_name list (%d)"%(
config['nimages'], len(image_list)))
nimages = len(image_list)
logger.debug('nimages = %d',nimages)
config['image_file_name'] = {
'type' : 'List',
'items' : image_list
}
logger.debug('nimages = %d',nimages)
assert nimages is not None
if 'cat_file_name' not in config:
raise TypeError('Parameter cat_file_name is required')
elif isinstance(config['cat_file_name'], list):
cat_list = config['cat_file_name']
if len(cat_list) == 0:
raise ValueError("cat_file_name may not be an empty list")
if dir is not None:
cat_list = [os.path.join(dir, n) for n in cat_list]
elif isinstance(config['cat_file_name'], basestring):
cat_file_name = config['cat_file_name']
if dir is not None:
cat_file_name = os.path.join(dir, cat_file_name)
cat_list = sorted(glob.glob(cat_file_name))
if len(cat_list) == 0:
raise ValueError("No files found corresponding to "+config['cat_file_name'])
elif not isinstance(config['cat_file_name'], dict):
raise ValueError("cat_file_name should be either a dict or a string")
if cat_list is not None:
logger.debug('cat_list = %s',cat_list)
if len(cat_list) == 1 and nimages > 1:
logger.info("Using the same catlist for all image")
cat_list = cat_list * nimages
elif nimages != len(cat_list):
raise ValueError("nimages = %s doesn't match length of cat_file_name list (%d)"%(
nimages, len(cat_list)))
config['cat_file_name'] = {
'type' : 'List',
'items' : cat_list
}
self.nimages = nimages
self.chipnums = list(range(nimages))
self.stamp_size = int(config.get('stamp_size', 32))
self.image_file_name = []
self.cat_file_name = []
self.image_kwargs = []
self.cat_kwargs = []
self.remove_signal_from_weight = config.get('remove_signal_from_weight', False)
self.invert_weight = config.get('invert_weight', False)
self.reserve_frac = config.get('reserve_frac', 0.)
try:
self.rng = np.random.default_rng(config.get('seed', None))
except AttributeError: # pragma: no cover
# numpy <= 1.16 doesn't have this yet. But RandomState is fine.
self.rng = np.random.RandomState(config.get('seed', None))
logger.info("Reading in %d images",nimages)
for image_num in range(nimages):
# This changes for each input image.
base['image_num'] = image_num
logger.debug("config = %s", config)
params = galsim.config.GetAllParams(config, base, req=req, opt=opt, ignore=ignore)[0]
logger.debug("image_num = %d: params = %s", image_num, params)
# Update the chipnum if not just using image_num
if 'chipnum' in params:
self.chipnums[image_num] = params['chipnum']
# Read the image
image_file_name = params['image_file_name']
image_hdu = params.get('image_hdu', None)
weight_hdu = params.get('weight_hdu', None)
badpix_hdu = params.get('badpix_hdu', None)
noise = params.get('noise', None)
self.image_file_name.append(image_file_name)
self.image_kwargs.append({
'image_file_name' : image_file_name,
'image_hdu' : image_hdu,
'weight_hdu' : weight_hdu,
'badpix_hdu' : badpix_hdu,
'noise' : noise})
# Read the catalog
cat_file_name = params['cat_file_name']
cat_hdu = params.get('cat_hdu', None)
x_col = params.get('x_col', 'x')
y_col = params.get('y_col', 'y')
ra_col = params.get('ra_col', None)
dec_col = params.get('dec_col', None)
ra_units = params.get('ra_units', 'deg')
dec_units = params.get('dec_units', 'deg')
flag_col = params.get('flag_col', None)
skip_flag = params.get('skip_flag', -1)
use_flag = params.get('use_flag', None)
sky_col = params.get('sky_col', None)
gain_col = params.get('gain_col', None)
sky = params.get('sky', None)
gain = params.get('gain', None)
satur = params.get('satur', None)
nstars = params.get('nstars', None)
if sky_col is not None and sky is not None:
raise ValueError("Cannot provide both sky_col and sky.")
if gain_col is not None and gain is not None:
raise ValueError("Cannot provide both gain_col and gain.")
self.cat_file_name.append(cat_file_name)
self.cat_kwargs.append({
'cat_file_name' : cat_file_name,
'cat_hdu' : cat_hdu,
'x_col' : x_col,
'y_col' : y_col,
'ra_col' : ra_col,
'dec_col' : dec_col,
'ra_units' : ra_units,
'dec_units' : dec_units,
'flag_col' : flag_col,
'skip_flag' : skip_flag,
'use_flag' : use_flag,
'sky_col' : sky_col,
'gain_col' : gain_col,
'sky' : sky,
'gain' : gain,
'satur' : satur,
'nstars' : nstars,
'image_file_name' : image_file_name,
'stamp_size' : self.stamp_size})
self.min_snr = config.get('min_snr', None)
self.max_snr = config.get('max_snr', 100)
self.max_edge_frac = config.get('max_edge_frac', None)
self.max_mask_pixels = config.get('max_mask_pixels', None)
self.stamp_center_size = config.get('stamp_center_size', 13)
self.use_partial = config.get('use_partial', False)
self.hsm_size_reject = config.get('hsm_size_reject', 0.)
if self.hsm_size_reject == 1:
# Enable True to be equivalent to 10. True comes in as 1.0, which would be a
# silly value to use, so it shouldn't be a problem to turn 1.0 -> 10.0.
self.hsm_size_reject = 10.
# Read all the wcs's, since we'll need this for the pointing, which in turn we'll
# need for when we make the stars.
self.setWCS(config, logger)
# Finally, set the pointing coordinate.
ra = config.get('ra',None)
dec = config.get('dec',None)
self.setPointing(ra, dec, logger)
def getRawImageData(self, image_num, logger=None):
return self._getRawImageData(self.image_kwargs[image_num], self.cat_kwargs[image_num],
self.wcs_list[image_num], self.invert_weight,
self.remove_signal_from_weight, logger=logger)
@staticmethod
def _getRawImageData(image_kwargs, cat_kwargs, wcs,
invert_weight, remove_signal_from_weight,
logger=None):
logger = galsim.config.LoggerWrapper(logger)
image, weight = InputFiles.readImage(logger=logger, **image_kwargs)
if invert_weight:
weight.invertSelf()
# Update the wcs
image.wcs = wcs
image_pos, sky, gain, satur = InputFiles.readStarCatalog(
logger=logger, image=image, **cat_kwargs)
if remove_signal_from_weight:
# Subtract off the mean sky, since this isn't part of the "signal" we want to
# remove from the weights.
if sky is None:
signal = image
else:
signal = image - np.mean(sky)
# For the gain, either all are None or all are values.
if gain[0] is None:
# If None, then we want to estimate the gain from the weight image.
weight, g = InputFiles._removeSignalFromWeight(signal, weight)
gain = [g for _ in gain]
logger.warning("Empirically determined gain = %f",g)
else:
# If given, use the mean gain when removing the signal.
# This isn't quite right, but hopefully the gain won't vary too much for
# different objects, so it should be close.
weight, _ = InputFiles._removeSignalFromWeight(signal, weight, gain=np.mean(gain))
logger.info("Removed signal from weight image.")
return image, weight, image_pos, sky, gain, satur
@staticmethod
def _makeStarsFromImage(image_kwargs, cat_kwargs, wcs, chipnum,
stamp_size, min_snr, max_snr, pointing, use_partial,
invert_weight, remove_signal_from_weight, hsm_size_reject,
max_mask_pixels, max_edge_frac, stamp_center_size,
logger):
"""Make stars from a single input image
"""
image, wt, image_pos, sky, gain, satur = InputFiles._getRawImageData(
image_kwargs, cat_kwargs, wcs, invert_weight, remove_signal_from_weight, logger)
logger.info("Processing catalog %s with %d stars",chipnum,len(image_pos))
nstars_in_image = 0
stars = []
if max_edge_frac is not None:
cen = (stamp_size-1.)/2. # index at center of array. May be half-integral.
i,j = np.ogrid[0:stamp_size,0:stamp_size]
edge_mask = (i-cen)**2 + (j-cen)**2 > stamp_center_size**2
else:
edge_mask = None
for k in range(len(image_pos)):
x = image_pos[k].x
y = image_pos[k].y
icen = int(x+0.5)
jcen = int(y+0.5)
half_size = stamp_size // 2
bounds = galsim.BoundsI(icen+half_size-stamp_size+1, icen+half_size,
jcen+half_size-stamp_size+1, jcen+half_size)
if not image.bounds.includes(bounds):
bounds = bounds & image.bounds
if not bounds.isDefined():
logger.warning("Star at position %f,%f is off the edge of the image.", x, y)
logger.warning("Skipping this star.")
continue
if use_partial:
logger.info("Star at position %f,%f overlaps the edge of the image. "
"Using smaller than the full stamp size: %s", x, y, bounds)
else:
logger.warning("Star at position %f,%f overlaps the edge of the image.", x, y)
logger.warning("Skipping this star.")
continue
stamp = image[bounds].copy()
wt_stamp = wt[bounds].copy()
props = { 'chipnum' : chipnum,
'gain' : gain[k],
}
# if a star is totally masked, then don't add it!
if np.all(wt_stamp.array == 0):
logger.warning("Star at position %f,%f is completely masked.", x, y)
logger.warning("Skipping this star.")
continue
# If any pixels are saturated, skip it.
max_val = np.max(stamp.array)
if satur is not None and max_val > satur:
logger.warning("Star at position %f,%f has saturated pixels.", x, y)
logger.warning("Maximum value is %f.", max_val)
logger.warning("Skipping this star.")
continue
# here we remove stars that have been at least partially covered by a mask
# and thus have weight exactly 0 in at least a certain number of pixels of their
# postage stamp
if max_mask_pixels is not None:
n_masked = np.prod(wt_stamp.array.shape) - np.count_nonzero(wt_stamp.array)
if n_masked >= max_mask_pixels:
logger.warning("Star at position %f,%f has %i masked pixels, ", x, y, n_masked)
logger.warning("Skipping this star.")
continue
# Subtract the sky
if sky is not None:
logger.debug("Subtracting off sky = %f", sky[k])
logger.debug("Median pixel value = %f", np.median(stamp.array))
stamp -= sky[k]
props['sky'] = sky[k]
# Check the snr and limit it if appropriate
snr = calculateSNR(stamp, wt_stamp)
logger.debug("SNR = %f",snr)
if min_snr is not None and snr < min_snr:
logger.info("Skipping star at position %f,%f with snr=%f."%(x,y,snr))
continue
if max_snr > 0 and snr > max_snr:
factor = (max_snr / snr)**2
logger.debug("Scaling noise by factor of %f to achieve snr=%f", factor, max_snr)
wt_stamp *= factor
snr = max_snr
props['snr'] = snr
pos = galsim.PositionD(x,y)
data = StarData(stamp, pos, weight=wt_stamp, pointing=pointing,
properties=props)
star = Star(data, None)
g = gain[k]
if g is not None:
logger.debug("Adding Poisson noise to weight map according to gain=%f",g)
star = star.addPoisson(gain=g)
if max_edge_frac is not None and max_edge_frac < 1:
flux = np.sum(star.image.array)
try:
flux_extra = np.sum(star.image.array[edge_mask])
flux_frac = flux_extra / flux
except IndexError:
logger.warning("Star at position %f,%f overlaps the edge of the image and "+
"max_edge_frac cut is set.", x, y)
logger.warning("Skipping this star.")
continue
if flux_frac > max_edge_frac:
logger.warning("Star at position %f,%f fraction of flux near edge of stamp "+
"exceeds cut: %f > %f", x, y, flux_frac, max_edge_frac)
logger.warning("Skipping this star.")
continue
stars.append(star)
nstars_in_image += 1
if hsm_size_reject != 0:
# Calculate the hsm size for each star and throw out extreme outliers.
sigma = [star.hsm[3] for star in stars]
med_sigma = np.median(sigma)
iqr_sigma = scipy.stats.iqr(sigma)
logger.debug("Doing hsm sigma rejection.")
while np.max(np.abs(sigma - med_sigma)) > hsm_size_reject * iqr_sigma:
logger.debug("median = %s, iqr = %s, max_diff = %s",
med_sigma, iqr_sigma, np.max(np.abs(sigma-med_sigma)))
k = np.argmax(np.abs(sigma-med_sigma))
logger.debug("remove k=%d: sigma = %s, pos = %s",k,sigma[k],stars[k].image_pos)
del sigma[k]
del stars[k]
med_sigma = np.median(sigma)
iqr_sigma = scipy.stats.iqr(sigma)
return stars
def setWCS(self, config, logger):
self.wcs_list = []
self.center_list = []
for image_num, kwargs in enumerate(self.image_kwargs):
image_file_name = kwargs['image_file_name']
image_hdu = kwargs['image_hdu']
image = galsim.fits.read(image_file_name, hdu=image_hdu)
if 'wcs' in config:
logger.warning("Using custom wcs from config for %s",image_file_name)
base = { 'input' : config, 'index_key' : 'image_num', 'image_num' : image_num }
wcs = galsim.config.BuildWCS(config, 'wcs', base, logger)
else:
logger.warning("Getting wcs from image file %s",image_file_name)
wcs = image.wcs
self.wcs_list.append(wcs)
self.center_list.append(image.true_center)
@staticmethod
def _removeSignalFromWeight(image, weight, gain=None):
"""Remove the image signal from the weight map.
:param image: The image to use as the signal
:param weight: The weight image.
:param gain: Optionally, the gain to use as the proportionality relation.
If gain is None, then it will be estimated automatically and returned.
[default: None]
:returns: newweight, gain
"""
signal = image.array
variance = 1./weight.array
use = (weight.array != 0.) & np.isfinite(signal)
if gain is None:
fit = np.polyfit(signal[use].flatten(), variance[use].flatten(), deg=1)
gain = 1./fit[0] # fit is [ 1/gain, sky_var ]
variance[use] -= signal[use] / gain
newweight = weight.copy()
newweight.array[use] = 1. / variance[use]
return newweight, gain
@staticmethod
def readImage(image_file_name, image_hdu, weight_hdu, badpix_hdu, noise, logger):
"""Read in the image and weight map (or make one if no weight information is given
:param image_file_name: The name of the file to read.
:param image_hdu: The hdu of the main image.
:param weight_hdu: The hdu of the weight image (if any).
:param badpix_hdu: The hdu of the bad pixel mask (if any).
:param noise: A constant noise value to use in lieu of a weight map.
:param logger: A logger object for logging debug info.
:returns: image, weight
"""
# Read in the image
logger.warning("Reading image file %s",image_file_name)
image = galsim.fits.read(image_file_name, hdu=image_hdu)
# Either read in the weight image, or build a dummy one
if weight_hdu is not None:
logger.info("Reading weight image from hdu %d.", weight_hdu)
weight = galsim.fits.read(image_file_name, hdu=weight_hdu)
if np.all(weight.array == 0):
logger.error("According to the weight mask in %s, all pixels have zero weight!",
image_file_name)
if np.any(weight.array < 0):
logger.error("Warning: weight map has invalid negative-valued pixels. "+
"Taking them to be 0.0")
weight.array[weight.array < 0] = 0.
elif noise is not None:
logger.debug("Making uniform weight image based on noise variance = %f", noise)
weight = galsim.ImageF(image.bounds, init_value=1./noise)
else:
logger.debug("Making trivial (wt==1) weight image")
weight = galsim.ImageF(image.bounds, init_value=1)
# If requested, set wt=0 for any bad pixels
if badpix_hdu is not None:
logger.info("Reading badpix image from hdu %d.", badpix_hdu)
badpix = galsim.fits.read(image_file_name, hdu=badpix_hdu)
# The badpix image may be offset by 32768 from the true value.
# If so, subtract it off.
if np.any(badpix.array > 32767): # pragma: no cover
logger.debug('min(badpix) = %s',np.min(badpix.array))
logger.debug('max(badpix) = %s',np.max(badpix.array))
logger.debug("subtracting 32768 from all values in badpix image")
badpix -= 32768
if np.any(badpix.array < -32767): # pragma: no cover
logger.debug('min(badpix) = %s',np.min(badpix.array))
logger.debug('max(badpix) = %s',np.max(badpix.array))
logger.debug("adding 32768 to all values in badpix image")
badpix += 32768
if np.all(badpix.array != 0): # pragma: no cover
logger.error("According to the bad pixel array in %s, all pixels are masked!",
image_file_name)
weight.array[badpix.array != 0] = 0
return image, weight
@staticmethod
def _flag_select(col, flag):
if len(col.shape) == 1:
# Then just treat this as a straightforward bitmask.
return col & flag
else:
# Then treat this as an array of bools rather than a bitmask
mask = np.zeros(col.shape[0], dtype=bool)
for bit in range(col.shape[1]): # pragma: no branch
if flag % 2 == 1:
mask |= col[:,bit]
flag = flag // 2
if flag == 0: break
return mask
@staticmethod
def readStarCatalog(cat_file_name, cat_hdu, x_col, y_col,
ra_col, dec_col, ra_units, dec_units, image,
flag_col, skip_flag, use_flag, sky_col, gain_col,
sky, gain, satur, nstars, image_file_name, stamp_size, logger):
"""Read in the star catalogs and return lists of positions for each star in each image.
:param cat_file_name: The name of the catalog file to read in.
:param cat_hdu: The hdu to use.
:param x_col: The name of the column with x values.
:param y_col: The name of the column with y values.
:param ra_col: The name of a column with RA values.
:param dec_col: The name of a column with Dec values.
:param ra_units: The units of the ra column.
:param dec_units: The units of the dec column.
:param image: The image that was already read in (mostly for the wcs).
:param flag_col: The name of a column with flag values.
:param skip_flag: The flag indicating which items to not use. [default: -1]
Items with flag & skip_flag != 0 will be skipped.
:param use_flag: The flag indicating which items to use. [default: None]
Items with flag & use_flag == 0 will be skipped.
:param sky_col: A column with sky (background) levels.
:param gain_col: A column with gain values.
:param sky: Either a float value for the sky to use for all objects or a str
keyword to read a value from the FITS header.
:param gain: Either a float value for the gain to use for all objects or a str
keyword to read a value from the FITS header.
:param satur: Either a float value for the saturation level to use or a str
keyword to read a value from the FITS header.
:param nstars: Optionally a maximum number of stars to use.
:param image_file_name: The image file name in case needed for header values.
:param stamp_size: The stamp size being used for the star stamps.
:param logger: A logger object for logging debug info. [default: None]
:returns: lists image_pos, sky, gain, satur
"""
import fitsio
# Read in the star catalog
logger.warning("Reading star catalog %s.",cat_file_name)
cat = fitsio.read(cat_file_name, cat_hdu)
if flag_col is not None:
if flag_col not in cat.dtype.names:
raise ValueError("flag_col = %s is not a column in %s"%(flag_col,cat_file_name))
col = cat[flag_col]
if len(col.shape) == 2:
logger.warning("Flag col (%s) is multidimensional. Treating as an array of bool",
flag_col)
if use_flag is not None:
# Remove any objects with flag & use_flag == 0
mask = InputFiles._flag_select(col, use_flag) == 0
logger.info("Removing objects with flag (col %s) & %d == 0",flag_col,use_flag)
if skip_flag != -1:
mask |= InputFiles._flag_select(col, skip_flag) != 0
logger.info("Removing objects with flag (col %s) & %d != 0",flag_col,skip_flag)
else:
# Remove any objects with flag & skip_flag != 0
mask = InputFiles._flag_select(col, skip_flag) != 0
if skip_flag == -1:
logger.info("Removing objects with flag (col %s) != 0",flag_col)
else:
logger.info("Removing objects with flag (col %s) & %d != 0",flag_col,skip_flag)
cat = cat[mask == 0]
# Limit to nstars objects
if nstars is not None and nstars < len(cat):
logger.info("Limiting to %d stars for %s",nstars,cat_file_name)
cat = cat[:nstars]
# Make the list of positions:
if ra_col is not None or dec_col is not None:
if ra_col is None or dec_col is None:
raise ValueError("ra_col and dec_col are both required if one is provided.")
if ra_col not in cat.dtype.names:
raise ValueError("ra_col = %s is not a column in %s"%(ra_col,cat_file_name))
if dec_col not in cat.dtype.names:
raise ValueError("dec_col = %s is not a column in %s"%(dec_col,cat_file_name))
logger.debug("Starting to make a list of positions from ra, dec")
ra_values = cat[ra_col]
dec_values = cat[dec_col]
ra_units = galsim.AngleUnit.from_name(ra_units)
dec_units = galsim.AngleUnit.from_name(dec_units)
ra = ra_values * ra_units
dec = dec_values * dec_units
logger.debug("Initially %d positions",len(ra))
# First limit to only those that could possibly be on the image by checking the
# min/max ra and dec from the image corners.
cen = image.wcs.toWorld(image.center)
logger.debug("Center at %s",cen)
x_corners = [image.xmin, image.xmin, image.xmax, image.xmax]
y_corners = [image.ymin, image.ymax, image.ymax, image.ymin]
corners = [image.wcs.toWorld(galsim.PositionD(x,y))
for (x,y) in zip(x_corners, y_corners)]
logger.debug("Corners at %s",corners)
min_ra = np.min([c.ra.wrap(cen.ra) for c in corners])
max_ra = np.max([c.ra.wrap(cen.ra) for c in corners])
min_dec = np.min([c.dec.wrap(cen.dec) for c in corners])
max_dec = np.max([c.dec.wrap(cen.dec) for c in corners])
logger.debug("RA range = %s .. %s",min_ra,max_ra)
logger.debug("Dec range = %s .. %s",min_dec,max_dec)
use = [(r.wrap(cen.ra) > min_ra) & (r.wrap(cen.ra) < max_ra) &
(d.wrap(cen.dec) > min_dec) & (d.wrap(cen.dec) < max_dec)
for r,d in zip(ra,dec)]
ra = ra[use]
dec = dec[use]
logger.debug("After limiting to image ra,dec range, len = %s",len(ra))
# Now convert to x,y
def safe_to_image(wcs, ra, dec):
try:
return wcs.toImage(galsim.CelestialCoord(ra, dec))
except galsim.GalSimError: # pragma: no cover
# If the ra,dec is way off the image, this might fail to converge.
# In this case return None, which we can get rid of simply.
return None
image_pos = [ safe_to_image(image.wcs,r,d) for r,d in zip(ra, dec) ]
image_pos = [ pos for pos in image_pos if pos is not None ]
logger.debug("Resulting image_pos list has %s positions",len(image_pos))
else:
if x_col not in cat.dtype.names:
raise ValueError("x_col = %s is not a column in %s"%(x_col,cat_file_name))
if y_col not in cat.dtype.names:
raise ValueError("y_col = %s is not a column in %s"%(y_col,cat_file_name))
x_values = cat[x_col]
y_values = cat[y_col]
logger.debug("Initially %d positions",len(x_values))
image_pos = [ galsim.PositionD(x,y) for x,y in zip(x_values, y_values) ]
# Check for objects well off the edge. We won't use them.
big_bounds = image.bounds.expand(stamp_size)
image_pos = [ pos for pos in image_pos if big_bounds.includes(pos) ]
logger.debug("After remove those that are off the image, len = %s",len(image_pos))
# Make the list of sky values:
if sky_col is not None:
if sky_col not in cat.dtype.names:
raise ValueError("sky_col = %s is not a column in %s"%(sky_col,cat_file_name))
sky = cat[sky_col]
elif sky is not None:
try:
sky = float(sky)
except ValueError:
fits = fitsio.FITS(image_file_name)
hdu = 1 if image_file_name.endswith('.fz') else 0
header = fits[hdu].read_header()
if sky not in header:
raise KeyError("Key %s not found in FITS header"%sky)