-
Notifications
You must be signed in to change notification settings - Fork 2
/
destest.py
1381 lines (1101 loc) · 49.4 KB
/
destest.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
from __future__ import division
from __future__ import print_function
from future import standard_library
standard_library.install_aliases()
from builtins import str
from builtins import range
from past.builtins import basestring
from builtins import object
from past.utils import old_div
import numpy as np
import fitsio as fio
import h5py
import pickle as pickle
import yaml
import os
import sys
import time
import cProfile, pstats
# and maybe a bit optimistic...
from multiprocessing import Pool
# from mpi4py import MPI
try:
from sharedNumpyMemManager import SharedNumpyMemManager as snmm
use_snmm = True
except:
use_snmm = False
import matplotlib
matplotlib.use ('agg')
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import LogNorm
import matplotlib.gridspec as gridspec
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
import pylab
if sys.version_info[0] == 3:
string_types = str,
else:
string_types = basestring,
def get_array( array ):
if use_snmm:
return snmm.getArray( array )
else:
return array
def save_obj( obj, name ):
with open(name, 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def load_obj( name ):
with open(name, 'rb') as f:
return pickle.load(f)
def file_path( params, subdir, name, var=None, var2=None, var3=None, ftype='txt' ):
"""
Set up a file path, and create the path if it doesn't exist.
"""
if var is not None:
name += '_' + var
if var2 is not None:
name += '_' + var2
if var3 is not None:
name += '_' + var3
name += '.' + ftype
fpath = os.path.join(params['output'],params['param_file'][:params['param_file'].index('.')],subdir)
if os.path.exists(fpath):
if not params['output_exists']:
raise IOError('Output directory already exists. Set output_exists to True to use existing output directory at your own peril.')
else:
if not os.path.exists(os.path.join(params['output'],params['param_file'][:params['param_file'].index('.')])):
os.mkdir(os.path.join(params['output'],params['param_file'][:params['param_file'].index('.')]))
try:
os.mkdir(fpath)
except:
pass
params['output_exists'] = True
return os.path.join(fpath,name)
def write_table( params, table, subdir, name, var=None, var2=None, var3=None ):
"""
Save a text table to file. Table must be a numpy-compatible array.
"""
fpath = file_path(params,subdir,name,var=var,var2=var2,var3=var3)
np.savetxt(fpath,table)
def child_testsuite( calc ):
params, selector, calibrator = calc
Testsuite( params, selector=selector, calibrator=calibrator, child=True )
def scalar_sum(x,length):
# catches scalar weights, responses and multiplies by the vector length for the mean
if np.isscalar(x):
return x*length
return np.sum(x)
class Testsuite(object):
"""
Testsuite manager class. Initiated with a yaml file path or dictionary that contains necessary settings and instructions.
"""
def __init__( self, param_file, selector = None, calibrator = None, child = False, **kwargs ):
# Read in yaml information
if isinstance(param_file, string_types):
self.params = yaml.load(open(param_file))
self.params['param_file'] = param_file
# Archive yaml file used for results
self.save_input_yaml()
else:
self.params = param_file
for x in self.params:
if self.params[x] == 'None':
self.params[x] = None
if self.params[x] == 'False':
self.params[x] = False
if self.params[x] == 'True':
self.params[x] = True
# Set up classes that manage data
# Source is an HDF5 file.
if self.params['source'] == 'hdf5':
self.source = H5Source(self.params)
# Source is a FITS file.
if self.params['source'] == 'fits':
self.source = FITSSource(self.params)
# Source is a desdm table
elif self.params['source'] == 'desdm':
self.source = DESDMSource(self.params)
# Source is an LSST thing
elif self.params['source'] == 'lsstdm':
self.source = LSSTDMSource(self.params)
else:
raise NameError('Something went wrong with parsing your source.')
if selector is None:
self.selector = Selector(self.params,self.source)
else:
self.selector = selector
self.selector.build_source(self.source)
if calibrator is None:
if self.params['cal_type'] == None:
self.calibrator = NoCalib(self.params,self.selector)
elif self.params['cal_type'] == 'mcal':
self.calibrator = MetaCalib(self.params,self.selector)
elif self.params['cal_type'] == 'classic':
self.calibrator = ClassCalib(self.params,self.selector)
else:
raise NameError('Something went wrong with parsing your calibration type.')
else:
self.calibrator = calibrator
# Run tests
if 'general_stats' in self.params:
GeneralStats(self.params,self.selector,self.calibrator,self.source,self.params['general_stats'])
if 'hist_1d' in self.params:
test = Hist1D(self.params,self.selector,self.calibrator,self.source,self.params['hist_1d'])
test.plot()
if 'hist_2d' in self.params:
test = Hist2D(self.params,self.selector,self.calibrator,self.source,self.params['hist_2d'])
test.plot()
if 'split_mean' in self.params:
if self.params['use_mpi'] and (not child):
procs = comm.Get_size()
iter_list = [self.params['split_x'][i::procs] for i in xrange(procs)]
calcs = []
for proc in range(procs):
if iter_list[proc] == []:
continue
params = self.params.copy()
params['split_x'] = iter_list[proc]
self.selector.kill_source()
calcs.append((params,self.selector,self.calibrator))
pool.map(child_testsuite, calcs)
else:
test = LinearSplit(self.params,self.selector,self.calibrator,self.source,self.params['split_x'],self.params['split_mean'])
test.plot()
def save_input_yaml( self ):
"""
Arxiv input yaml settings.
"""
fpath = file_path(self.params,'',self.params['param_file'][:self.params['param_file'].index('.')],ftype='yaml')
print('saving input yaml to: '+fpath)
with open(fpath, 'w') as outfile:
yaml.dump(self.params, outfile, default_flow_style=False)
class SourceParser(object):
"""
A class to manage the actual reading or downloading of data from external sources.
Initiate with a testsuite param dictionary.
To use later: source_parser.read(...). All the messy details are hidden.
"""
def __init__( self, params ):
self.params = params
self.open()
def open( self ):
raise NotImplementedError('Subclass '+self.__class__.__name__+' should have method open().')
def read( self ):
raise NotImplementedError('Subclass '+self.__class__.__name__+' should have method read().')
def close( self ):
raise NotImplementedError('Subclass '+self.__class__.__name__+' should have method close().')
class H5Source(SourceParser):
"""
A class to manage the actual reading or downloading of data from HDF5 sources.
"""
def __init__( self, params ):
super(H5Source,self).__init__(params)
if 'filename' not in self.params.keys():
raise NameError('Must provide a filename for hdf5 source.')
if 'table' not in self.params.keys():
raise NameError('Must specify table name for hdf5 file.')
if type(self.params['table']) is not list:
raise TypeError('Table must be provided as a list of names (even a list of one).')
if 'group' in self.params.keys():
self.hdf = h5py.File(self.params['filename'], mode = 'r')
# save all column names
self.cols = list(self.hdf[self.params['group']][self.params['table'][0]].keys())
# save length of tables
self.size = self.hdf[self.params['group']][self.params['table'][0]][self.cols[0]].shape[0]
# Loop over tables and save convenience information
for t in self.params['table']:
keys = list(self.hdf[self.params['group']][t].keys())
if self.hdf[self.params['group']][t][keys[0]].shape[0] != self.size:
raise TypeError('Length of sheared tables in hdf5 file must match length of unsheared table.')
if len(self.params['table'])>1:
# save sheared column names
self.sheared_cols = list(self.hdf[self.params['group']][self.params['table'][1]].keys())
print(self.sheared_cols)
else:
raise NameError('Need group name for hdf5 file.')
self.close()
def open( self ):
self.hdf = h5py.File(self.params['filename'], mode = 'r')
def read_direct( self, group, table, col):
# print('READING FROM HDF5 FILE: group = ',group,' table = ',table,' col = ',col)
self.open() #attempting this
return self.hdf[group][table][col][:]
self.close()
def read( self, col=None, rows=None, nosheared=False, full_path = None ):
self.open()
def add_out( table, rows, col ):
"""
Extract a portion of a column from the file.
"""
if rows is not None:
if hasattr(rows,'__len__'):
if len(rows==2):
out = self.hdf[self.params['group']][table][col][rows[0]:rows[1]]
else:
out = self.hdf[self.params['group']][table][col][rows]
else:
out = self.hdf[self.params['group']][table][col][:]
return out
if full_path is not None:
return self.hdf[full_path][:]
if col is None:
raise NameError('Must specify column.')
out = []
# For metacal file, loop over tables and return list of 5 unsheared+sheared values for column (or just unsheraed if 'nosheared' is true or there doesn't exist sheared values for this column)
# For classic file, get single column.
# For both metacal and classic files, output is a list of columns (possible of length 1)
for i,t in enumerate(self.params['table']):
if i==0:
if col not in list(self.hdf[self.params['group']][t].keys()):
print(self.params['group'],t,col,list(self.hdf[self.params['group']][t].keys()))
raise NameError('Col '+col+' not found in hdf5 file.')
else:
if nosheared:
print('skipping sheared columns for',col)
continue
if col not in self.sheared_cols:
print(col,'not in sheared cols')
continue
if col not in list(self.hdf[self.params['group']][t].keys()):
print(col,'not in table keys')
raise NameError('Col '+col+' not found in sheared table '+t+' of hdf5 file.')
if rows is not None:
out.append( add_out(t,rows,col) )
else:
out.append( add_out(t,None,col) )
self.close()
return out
def close( self ):
if hasattr(self,'hdf'):
self.hdf.close()
class FITSSource(SourceParser):
"""
A class to manage the actual reading or downloading of data from HDF5 sources.
"""
def __init__( self, params ):
super(FITSSource,self).__init__(params)
if 'filename' not in self.params.keys():
raise NameError('Must provide a filename for fits source.')
if 'table' not in self.params.keys():
raise NameError('Must specify table name for fits file.')
if type(self.params['table']) is not list:
raise TypeError('Table must be provided as a list of names (even a list of one).')
self.fits = fio.FITS(self.params['filename'])
# save all column names
self.cols = self.fits[self.params['table'][0]][0].dtype.names
# save length of tables
self.size = self.fits[self.params['table'][0]].read_header()['NAXIS2']
# No metacal sheared capability currently
self.close()
def open( self ):
self.fits = fio.FITS(self.params['filename'])
def read_direct( self, group, table, col):
# print('READING FROM HDF5 FILE: group = ',group,' table = ',table,' col = ',col)
self.open() #attempting this
return self.fits[table][col][:]
self.close()
def read( self, col=None, rows=None, nosheared=False ):
self.open()
def add_out( table, rows, col ):
"""
Extract a portion of a column from the file.
"""
if rows is not None:
if hasattr(rows,'__len__'):
if len(rows==2):
out = self.fits[table][col][rows[0]:rows[1]]
else:
out = self.fits[table][col][rows]
else:
out = self.fits[table][col][:]
return out
if col is None:
raise NameError('Must specify column.')
out = []
# For metacal file, loop over tables and return list of 5 unsheared+sheared values for column (or just unsheraed if 'nosheared' is true or there doesn't exist sheared values for this column)
# For classic file, get single column.
# For both metacal and classic files, output is a list of columns (possible of length 1)
for i,t in enumerate(self.params['table']):
if i==0:
if col not in self.cols:
raise NameError('Col '+col+' not found in fits file.')
else:
if nosheared:
print('skipping sheared columns for',col)
continue
if col not in self.sheared_cols:
print(col,'not in sheared cols')
continue
if col not in self.fits[t][0].dtype.names:
print(col,'not in table keys')
raise NameError('Col '+col+' not found in sheared table '+t+' of fits file.')
if rows is not None:
out.append( add_out(t,rows,col) )
else:
out.append( add_out(t,None,col) )
self.close()
return out
def close( self ):
if hasattr(self,'fits'):
self.fits.close()
class DESDMSource(SourceParser):
"""
A class to manage the actual reading or downloading of data from DESDM sources.
"""
def __init__( self ):
raise NotImplementedError('You should write this.')
class LSSTDMSource(SourceParser):
"""
A class to manage the actual reading or downloading of data from LSSTDM sources.
"""
def __init__( self ):
raise NotImplementedError('You should write this.')
class Selector(object):
"""
A class to manage masking and selections of the data.
Initiate with a testsuite object.
Initiation will parse the 'select_cols' conditions in the yaml file and create a limiting mask 'mask_', ie, an 'or' of the individual unsheared and sheared metacal masks. The individual masks (list of 1 or 5 masks) are 'mask'.
"""
def __init__( self, params, source, inherit = None ):
self.params = params
self.source = source
if inherit is None:
self.build_limiting_mask()
else:
self.mask = inherit.mask
self.mask_ = inherit.mask_
def kill_source( self ):
self.source = None
def build_source ( self, source ):
self.source = source
def build_limiting_mask( self ):
"""
Build the limiting mask for use in discarding any data that will never be used.
"""
mask = None
# Setup mask file cache path.
mask_file = file_path(self.params,'cache',self.params['name']+'mask',ftype='pickle')
if self.params['load_cache']:
# if mask cache exists, read mask from pickle and skip parsing yaml selection conditions.
if os.path.exists(mask_file):
mask, mask_ = load_obj(mask_file)
print('loaded mask cache')
if mask is None:
if 'select_path' in self.params:
print('using select_path for mask')
if (self.params['select_path'] is None)|(self.params['select_path'].lower() == 'none'):
print('None select path - ignoring selection')
mask = [np.ones(self.source.size,dtype=bool)]
mask_ = np.where(mask[0])[0]
else:
mask = []
tmp = np.zeros(self.source.size,dtype=bool)
select = self.source.read(full_path=self.params['select_path'])
print('destest',self.params['filename'],self.params['select_path'],len(tmp),len(select))
tmp[select]=True
mask.append( tmp )
try:
tmp = np.zeros(self.source.size,dtype=bool)
select = self.source.read(full_path=self.params['select_path']+'_1p')
tmp[select]=True
mask.append( tmp )
tmp = np.zeros(self.source.size,dtype=bool)
select = self.source.read(full_path=self.params['select_path']+'_1m')
tmp[select]=True
mask.append( tmp )
tmp = np.zeros(self.source.size,dtype=bool)
select = self.source.read(full_path=self.params['select_path']+'_2p')
tmp[select]=True
mask.append( tmp )
tmp = np.zeros(self.source.size,dtype=bool)
select = self.source.read(full_path=self.params['select_path']+'_2m')
tmp[select]=True
mask.append( tmp )
except:
print('No sheared select_path ',self.params['select_path'])
mask_ = np.zeros(self.source.size, dtype=bool)
for imask in mask:
mask_ = mask_ | imask
mask_ = np.where(mask_)[0]
# Cut down masks to the limiting mask
# Its important to note that all operations will assume that data has been trimmed to satisfy selector.mask_ from now on
for i in range(len(mask)):
mask[i] = mask[i][mask_]
print('end mask',mask_,mask[0])
else:
# mask cache doesn't exist, or you chose to ignore it, so masks are built from yaml selection conditions
# set up 'empty' mask
mask = [np.ones(self.source.size, dtype=bool)]
if self.params['cal_type']=='mcal':
mask = mask * 5
# For each of 'select_cols' in yaml file, read in the data and iteratively apply the appropriate mask
for i,select_col in enumerate(self.params['select_cols']):
cols = self.source.read(col=select_col)
for j,col in enumerate(cols):
mask[j] = mask[j] & eval(self.params['select_exp'][i])
# Loop over unsheared and sheared mask arrays and build limiting mask
mask_ = np.zeros(self.source.size, dtype=bool)
for imask in mask:
mask_ = mask_ | imask
# Cut down masks to the limiting mask
# Its important to note that all operations will assume that data has been trimmed to satisfy selector.mask_ from now on
for i in range(len(mask)):
mask[i] = mask[i][mask_]
mask_ = np.where(mask_)[0]
# save cache of masks to speed up reruns
save_obj( [mask, mask_], mask_file )
if use_snmm:
# print('using snmm')
self.mask_ = snmm.createArray((len(mask_),), dtype=np.int64)
snmm.getArray(self.mask_)[:] = mask_[:]
mask_ = None
else:
self.mask_ = mask_
self.mask = []
for i in range(len(mask)):
if use_snmm:
self.mask.append( snmm.createArray((len(mask[i]),), dtype=np.bool) )
snmm.getArray(self.mask[i])[:] = mask[i][:]
mask[i] = None
else:
self.mask.append( mask[i] )
def get_col( self, col, nosheared=False, uncut=False ):
"""
Wrapper to retrieve a column of data from the source and trim to the limiting mask (mask_)
"""
# x at this point is the full column
x = self.source.read(col=col, nosheared=nosheared)
# print('get_col length',len(x))
# if col=='zmean_sof':
# print('inside get_col')
# print(x[0])
# trim and return
for i in range(len(x)):
x[i] = x[i][get_array(self.mask_)]
# print('get_col length i',len(x[i]))
if col=='zmean_sof':
print(x[0])
if uncut:
return x
for i in range(len(x)):
x[i] = x[i][get_array(self.mask[i])]
# print('get_col length2 i',len(x[i])
if col=='zmean_sof':
print(x[0])
return x
def get_masked( self, x, mask ):
"""
Accept a mask and column(s), apply the mask jointly with selector.mask (mask from yaml selection) and return masked array.
"""
if mask is None:
mask = [np.s_[:]]*5
if type(mask) is not list:
mask = [ mask ]
if type(x) is not list:
if np.isscalar(x):
return x
else:
return x[get_array(self.mask[0])][mask[0]]
if np.isscalar(x[0]):
return x
return [ x_[get_array(self.mask[i])][mask[i]] for i,x_ in enumerate(x) ]
def get_mask( self, mask=None ):
"""
Same as get_masked, but only return the mask.
"""
if mask is None:
return [ np.where(get_array(self.mask[i]))[0] for i in range(len(self.mask)) ]
return [ np.where(get_array(self.mask[i]))[0][mask_] for i,mask_ in enumerate(mask) ]
def get_match( self ):
"""
Get matching to parent catalog.
"""
return self.source.read_direct( self.params['group'].replace('catalog','index'), self.params['table'][0], 'match_gold')
def get_tuple_col( self, col ):
"""
Force a tuple return of sheared selections of an unsheared quantity (like coadd_object_id).
"""
# x at this point is the full column
x = self.source.read(col=col, nosheared=True)
x = x*5
# trim and return
for i in range(len(x)):
x[i] = x[i][get_array(self.mask_)]
for i in range(len(x)):
x[i] = x[i][get_array(self.mask[i])]
return x
class Calibrator(object):
"""
A class to manage calculating and returning calibration factors and weights for the catalog.
Initiate with a testsuite params object.
When initiated, will read in the shear response (or m), additive corrections (or c), and weights as requested in the yaml file. These are a necessary overhead that will be stored in memory, but truncated to the limiting mask (selector.mask_), so not that bad.
"""
def __init__( self, params, selector ):
self.params = params
self.selector = selector
def calibrate(self,col,mask=None,return_full_w=False,weight_only=False,return_wRg=False,return_wRgS=False,return_full=False):
"""
Return the calibration factor and weights, given potentially an ellipticity and selection.
"""
# Get the weights
w = self.selector.get_masked(self.w,mask)
if return_full_w:
w_ = w
else:
w_ = w[0]
if weight_only:
return w_
if col == self.params['e'][0]:
Rg = self.selector.get_masked(get_array(self.Rg1),mask)
c = self.selector.get_masked(self.c1,mask)
if col == self.params['e'][1]:
Rg = self.selector.get_masked(get_array(self.Rg2),mask)
c = self.selector.get_masked(self.c2,mask)
print('-----',col, self.params['e'])
if col in self.params['e']:
ws = [ scalar_sum(wx_,len(Rg)) for i,wx_ in enumerate(w)]
# Get a selection response
Rs = self.select_resp(col,mask,w,ws)
print('Rs',col,np.mean(Rg),Rs)
R = Rg + Rs
if return_wRg:
Rg1 = self.selector.get_masked(get_array(self.Rg1),mask)
Rg2 = self.selector.get_masked(get_array(self.Rg2),mask)
return ((Rg1+Rg2)/2.)*w[0]
if return_wRgS:
Rg1 = self.selector.get_masked(get_array(self.Rg1),mask)
Rg2 = self.selector.get_masked(get_array(self.Rg2),mask)
if col == self.params['e'][0]:
Rs2 = self.select_resp(self.params['e'][1],mask,w,ws)
else:
Rs2 = self.select_resp(self.params['e'][0],mask,w,ws)
return ((Rg1+Rg2)/2.+(Rs+Rs2)/2.)*w[0]
elif return_full:
return R,c,w_
else:
R = np.sum((Rg+Rs)*w[0],)/ws[0]
return R,c,w_
else:
return None,None,w_
def select_resp(self,col,mask,w,ws):
"""
Return a zero selection response (default).
"""
return 0.
class NoCalib(Calibrator):
"""
A class to manage calculating and returning calibration factors and weights for a general catalog without shear corrections.
"""
def __init__( self, params, selector ):
super(NoCalib,self).__init__(params,selector)
self.Rg1 = self.Rg2 = None
self.c1 = self.c2 = None
self.w = [1]
if 'w' in self.params:
self.w = self.selector.get_col(self.params['w'])
class MetaCalib(Calibrator):
"""
A class to manage calculating and returning calibration factors and weights for a metacal catalog.
"""
def __init__( self, params, selector ):
super(MetaCalib,self).__init__(params,selector)
self.Rg1 = self.Rg2 = 1.
if 'Rg' in self.params:
Rg1 = self.selector.get_col(self.params['Rg'][0],uncut=True)[0]
Rg2 = self.selector.get_col(self.params['Rg'][1],uncut=True)[0]
e1 = self.selector.get_col(self.params['e'][0],nosheared=True,uncut=True)[0]
e2 = self.selector.get_col(self.params['e'][1],nosheared=True,uncut=True)[0]
if use_snmm:
self.Rg1 = snmm.createArray((len(Rg1),), dtype=np.float64)
snmm.getArray(self.Rg1)[:] = Rg1[:]
Rg1 = None
self.Rg2 = snmm.createArray((len(Rg2),), dtype=np.float64)
snmm.getArray(self.Rg2)[:] = Rg2[:]
Rg2 = None
self.e1 = snmm.createArray((len(e1),), dtype=np.float64)
snmm.getArray(self.e1)[:] = e1[:]
e1 = None
self.e2 = snmm.createArray((len(e2),), dtype=np.float64)
snmm.getArray(self.e2)[:] = e2[:]
e2 = None
else:
self.Rg1 = Rg1
self.Rg2 = Rg2
self.e1 = e1
self.e2 = e2
self.c1 = self.c2 = 0.
if 'c' in self.params:
self.c1 = self.selector.get_col(self.params['c'][0],uncut=True)
self.c2 = self.selector.get_col(self.params['c'][1],uncut=True)
self.w = [1] * 5
if 'w' in self.params:
self.w = self.selector.get_col(self.params['w'],uncut=True)
def select_resp(self,col,mask,w,ws):
"""
Get the selection response.
"""
# if an ellipticity column, calculate and return the selection response and weight
if col in self.params['e']:
if mask is not None:
if len(mask)==1: # exit for non-sheared column selections
return 0.
mask_ = [ get_array(imask) for imask in self.selector.mask ]
if col == self.params['e'][0]:
if mask is not None:
eSp = np.sum((get_array(self.e1)[mask_[1]])[mask[1]]*w[1])
eSm = np.sum(get_array(self.e1)[mask_[2]][mask[2]]*w[2])
else:
eSp = np.sum(get_array(self.e1)[mask_[1]]*w[1])
eSm = np.sum(get_array(self.e1)[mask_[2]]*w[2])
Rs = eSp/ws[1] - eSm/ws[2]
elif col == self.params['e'][1]:
if mask is not None:
eSp = np.sum(get_array(self.e2)[mask_[3]][mask[3]]*w[3])
eSm = np.sum(get_array(self.e2)[mask_[4]][mask[4]]*w[4])
else:
eSp = np.sum(get_array(self.e2)[mask_[3]]*w[3])
eSm = np.sum(get_array(self.e2)[mask_[4]]*w[4])
Rs = eSp/ws[3] - eSm/ws[4]
else:
return 0.
Rs /= 2.*self.params['dg']
# print('Rs',Rs,Rs*2.*self.params['dg']
# print('check what dg is used....'
return Rs
class ClassicCalib(Calibrator):
"""
A class to manage calculating and returning calibration factors and weights for a metacal catalog.
"""
def __init__( self, params, selector ):
super(ClassCalib,self).__init__(params,selector)
self.Rg1 = self.Rg2 = 1.
if 'Rg' in self.params:
self.Rg1 = self.selector.get_col(self.params['Rg'][0])
self.Rg2 = self.selector.get_col(self.params['Rg'][1])
self.c1 = self.c2 = 0.
if 'c' in self.params:
self.c1 = self.selector.get_col(self.params['c'][0])
self.c2 = self.selector.get_col(self.params['c'][1])
self.w = [1]
if 'w' in self.params:
self.w = self.selector.get_col(self.params['w'])
class Splitter(object):
"""
A class for managing splitting the data set into bins and accessing the binned data.
Initiate with a testsuite object.
"""
def __init__( self, params, selector, calibrator, source, nbins = None ):
self.params = params
self.selector = selector
self.calibrator = calibrator
self.source = source
self.bins = self.params['linear_bins']
self.x = None
self.y = None
self.xcol = None
self.ycol = None
self.order = None
if 'split_x' in self.params:
for col in self.params['split_x']:
if col not in self.source.cols:
raise NameError(col + ' not in source.')
else:
self.params['split_x'] = self.source.cols
if nbins is not None:
self.bins = nbins
return
def get_x( self, col, xbin=None, return_mask=False ):
"""
Get the 'x' column - the column you're binning the data by.
If you haven't already called splitter with this x col, the data will be read from the source and the binning edges will be set up.
Optionally give a bin number, it will return the portion of the x array that falls in that bin. Can also optionally return the mask for that bin.
"""
# If column doesn't already exist in splitter, read the data and define bins in self.split().
if col != self.xcol:
self.xcol = col
self.order = None
self.split(col)
# If not asking for a bin selection, return
if xbin is None:
return
# If asking for a bin selection, find the appropriate mask and return that part of the x array.
start,end = self.get_bin_edges(xbin)
# print('returning x bin',start,end
mask = [np.s_[start_:end_] for start_,end_ in tuple(zip(start,end))] # np.s_ creates an array slice 'object' that can be passed to functions
mask = [ order_[mask_] for order_,mask_ in tuple(zip(self.order,mask)) ]
if return_mask:
return self.x[start[0]:end[0]],mask
return self.x[start[0]:end[0]]
def get_y( self, col, xbin=None, return_mask=False ):
"""
Get the 'y' column - the column you're doing stuff with in bins of the x col. If you haven't called splitter.get_x(), an error will be raised, since you haven't defined what you're binning against.
If you haven't already called splitter with this y col, the data will be read from the source.
Optionally give a bin number, it will return the portion of the y array that falls in that bin. Can also optionally return the mask for that bin.
"""
if self.xcol is None:
raise NameError('There is no x column associated with this splitter.')
# If column doesn't already exist in splitter, read the data and order it to match x ordering for efficient splitting.
if col != self.ycol:
self.ycol = col
self.y = self.selector.get_col(col,nosheared=True)
for i,y_ in enumerate(self.y):
self.y[i] = y_[self.order[i]]
self.y = self.y[0]
print('ysize',len(self.y),self.y.nbytes)
# If not asking for a bin selection, return
if xbin is None:
return
# If asking for a bin selection, find the appropriate mask and return that part of the y array.
start,end = self.get_bin_edges(xbin)
# print('returning y bin',start,end
mask = [np.s_[start_:end_] for start_,end_ in tuple(zip(start,end))]
mask = [ order_[mask_] for order_,mask_ in tuple(zip(self.order,mask)) ]
if return_mask:
return self.y[start[0]:end[0]],mask
return self.y[start[0]:end[0]]
def split( self, col ):
"""
Reads in a column (x) and sorts it. If you allowed cache reading, it will check if you've already done this and just read it in from the pickle cach. Then finds the edges of the bins you've requested.
"""
# Check if cache file exists and use it if you've requested that.
sort_file = file_path(self.params,'cache',self.params['name']+'sort',var=col,ftype='pickle')
if self.params['load_cache']:
print('loading split sort cache',sort_file)
if os.path.exists(sort_file):
self.order,self.x = load_obj(sort_file)
# Cache file doesn't exist or you're remaking it
if self.order is None:
print('split sort cache not found')
# Read x
self.x = self.selector.get_col(col)
# save the index order to sort the x array for more efficient binning
self.order = []
for i,x_ in enumerate(self.x):
self.order.append( np.argsort(x_) )
self.x[i] = x_[self.order[i]]
# save cache of sorted x and its order relative to the source
save_obj( [self.order,self.x], sort_file )
# get bin edges
self.get_edge_idx()
self.x = self.x[0]
return
def get_edge_idx( self ):
"""
Find the bin edges that split the data into the ranges you set in the yaml or into a number of equal-weighted bins.
"""