Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
with assert_raises(ValueError):
treecorr.util.gen_write(file_name, ['a', 'b'], [a, b], file_type='Invalid')
with assert_raises(ValueError):
treecorr.util.gen_read(file_name, file_type='Invalid')
# Now some places that do sanity checks for invalid coords or metrics which would already
# be checked in normal operation.
with assert_raises(ValueError):
treecorr.util.parse_metric('Euclidean', 'invalid')
with assert_raises(ValueError):
treecorr.util.parse_metric('Invalid', 'flat')
with assert_raises(ValueError):
treecorr.util.coord_enum('invalid')
with assert_raises(ValueError):
treecorr.util.metric_enum('Invalid')
def test_util():
# Test some error handling in utility functions that shouldn't be possible to get to
# in normal running, so we need to call things explicitly to get the coverage.
# First some I/O sanity checks
a = np.array([1,2,3])
b = np.array([4,5,6])
file_name = 'junk.out'
with assert_raises(ValueError):
treecorr.util.gen_write(file_name, ['a', 'b'], [a])
with assert_raises(ValueError):
treecorr.util.gen_write(file_name, ['a', 'b'], [a, b, a])
with assert_raises(ValueError):
treecorr.util.gen_write(file_name, ['a'], [a, b])
with assert_raises(ValueError):
treecorr.util.gen_write(file_name, [], [])
with assert_raises(ValueError):
treecorr.util.gen_write(file_name, ['a', 'b'], [a, b[:1]])
with assert_raises(ValueError):
treecorr.util.gen_write(file_name, ['a', 'b'], [a, b], file_type='Invalid')
with assert_raises(ValueError):
treecorr.util.gen_read(file_name, file_type='Invalid')
# Now some places that do sanity checks for invalid coords or metrics which would already
# be checked in normal operation.
col_names = ['RA', 'DEC']
cols = [info_data[n] for n in col_names] # These come from wlinfo
col_names += ['E_1', 'E_2', 'W']
cols += [ngmix_data[n] for n in col_names[2:]] # These are in ngmix
# combine the two sensitivity estimates
col_names += ['SENS']
cols += [(ngmix_data['SENS_1'] + ngmix_data['SENS_2'])/2.]
# Save time by cutting to only flag != 0 objects here.
use = info_data['NGMIX_FLAG'] == 0
print('total number of galaxies = ',len(use))
print('number to use = ',np.sum(use))
cols = [col[use] for col in cols]
print('writing merged file: ',source_file)
treecorr.util.gen_write(source_file, col_names, cols, file_type='FITS')
return source_file, lens_file
col_names = [ 'r_nom', 'u_nom', 'v_nom',
'meand1', 'meanlogd1', 'meand2', 'meanlogd2',
'meand3', 'meanlogd3', 'meanu', 'meanv',
'zeta', 'sigma_zeta', 'weight', 'ntri' ]
columns = [ self.rnom, self.u, self.v,
self.meand1, self.meanlogd1, self.meand2, self.meanlogd2,
self.meand3, self.meanlogd3, self.meanu, self.meanv,
self.zeta, np.sqrt(self.varzeta), self.weight, self.ntri ]
params = { 'coords' : self.coords, 'metric' : self.metric,
'sep_units' : self.sep_units, 'bin_type' : self.bin_type }
if precision is None:
precision = self.config.get('precision', 4)
treecorr.util.gen_write(
file_name, col_names, columns,
params=params, precision=precision, file_type=file_type, logger=self.logger)
if coords != self.coords:
self.logger.warning("Detected a change in catalog coordinate systems.\n"+
"This probably doesn't make sense!")
if metric != self.metric:
self.logger.warning("Detected a change in metric.\n"+
"This probably doesn't make sense!")
if metric == 'Periodic':
if self.xperiod == 0 or self.yperiod == 0 or (coords=='3d' and self.zperiod == 0):
raise ValueError("Periodic metric requires setting the period to use.")
else:
if self.xperiod != 0 or self.yperiod != 0 or self.zperiod != 0:
raise ValueError("period options are not valid for %s metric."%metric)
self.coords = coords # These are the regular string values
self.metric = metric
self._coords = treecorr.util.coord_enum(coords) # These are the C++-layer enums
self._metric = treecorr.util.metric_enum(metric)
if drr is not None:
col_names += ['DRR','RDR','RRD','DDR','DRD','RDD']
columns += [ drr.weight * (self.tot/drr.tot), rdr.weight * (self.tot/rdr.tot),
rrd.weight * (self.tot/rrd.tot), ddr.weight * (self.tot/ddr.tot),
drd.weight * (self.tot/drd.tot), rdd.weight * (self.tot/rdd.tot) ]
col_names += [ 'ntri' ]
columns += [ self.ntri ]
if precision is None:
precision = self.config.get('precision', 4)
params = { 'tot' : self.tot, 'coords' : self.coords, 'metric' : self.metric,
'sep_units' : self.sep_units, 'bin_type' : self.bin_type }
treecorr.util.gen_write(
file_name, col_names, columns,
params=params, precision=precision, file_type=file_type, logger=self.logger)
is no loss of information.
.. warning::
The `NKCorrelation` object should be constructed with the same configuration
parameters as the one being read. e.g. the same min_sep, max_sep, etc. This is not
checked by the read function.
Parameters:
file_name (str): The name of the file to read in.
file_type (str): The type of file ('ASCII' or 'FITS'). (default: determine the type
automatically from the extension of file_name.)
"""
self.logger.info('Reading NK correlations from %s',file_name)
data, params = treecorr.util.gen_read(file_name, file_type=file_type, logger=self.logger)
if 'R_nom' in data.dtype.names: # pragma: no cover
self.rnom = data['R_nom']
self.meanr = data['meanR']
self.meanlogr = data['meanlogR']
else:
self.rnom = data['r_nom']
self.meanr = data['meanr']
self.meanlogr = data['meanlogr']
self.logr = np.log(self.rnom)
self.xi = data['kappa']
self.varxi = data['sigma']**2
self.weight = data['weight']
self.npairs = data['npairs']
self.coords = params['coords'].strip()
self.metric = params['metric'].strip()
self.sep_units = params['sep_units'].strip()
def _set_metric(self, metric, coords1, coords2=None, coords3=None):
if metric is None:
metric = treecorr.config.get(self.config,'metric',str,'Euclidean')
coords, metric = treecorr.util.parse_metric(metric, coords1, coords2, coords3)
if self.coords != None or self.metric != None:
if coords != self.coords:
self.logger.warning("Detected a change in catalog coordinate systems. "+
"This probably doesn't make sense!")
if metric != self.metric:
self.logger.warning("Detected a change in metric. "+
"This probably doesn't make sense!")
if metric == 'Periodic':
if self.xperiod == 0 or self.yperiod == 0 or (coords=='3d' and self.zperiod == 0):
raise ValueError("Periodic metric requires setting the period to use.")
else:
if self.xperiod != 0 or self.yperiod != 0 or self.zperiod != 0:
raise ValueError("period options are not valid for %s metric."%metric)
self.coords = coords
self.metric = metric
self._coords = treecorr.util.coord_enum(coords)
is no loss of information.
.. warning::
The `NNCorrelation` object should be constructed with the same configuration
parameters as the one being read. e.g. the same min_sep, max_sep, etc. This is not
checked by the read function.
Parameters:
file_name (str): The name of the file to read in.
file_type (str): The type of file ('ASCII' or 'FITS'). (default: determine the type
automatically from the extension of file_name.)
"""
self.logger.info('Reading NN correlations from %s',file_name)
data, params = treecorr.util.gen_read(file_name, file_type=file_type, logger=self.logger)
if 'R_nom' in data.dtype.names: # pragma: no cover
self.rnom = data['R_nom']
self.meanr = data['meanR']
self.meanlogr = data['meanlogR']
else:
self.rnom = data['r_nom']
self.meanr = data['meanr']
self.meanlogr = data['meanlogr']
self.logr = np.log(self.rnom)
self.weight = data['DD']
self.npairs = data['npairs']
self.tot = params['tot']
self.coords = params['coords'].strip()
self.metric = params['metric'].strip()
self.sep_units = params['sep_units'].strip()
self.bin_type = params['bin_type'].strip()
if self.coords != None or self.metric != None:
if coords != self.coords:
self.logger.warning("Detected a change in catalog coordinate systems.\n"+
"This probably doesn't make sense!")
if metric != self.metric:
self.logger.warning("Detected a change in metric.\n"+
"This probably doesn't make sense!")
if metric == 'Periodic':
if self.xperiod == 0 or self.yperiod == 0 or (coords=='3d' and self.zperiod == 0):
raise ValueError("Periodic metric requires setting the period to use.")
else:
if self.xperiod != 0 or self.yperiod != 0 or self.zperiod != 0:
raise ValueError("period options are not valid for %s metric."%metric)
self.coords = coords # These are the regular string values
self.metric = metric
self._coords = treecorr.util.coord_enum(coords) # These are the C++-layer enums
self._metric = treecorr.util.metric_enum(metric)