Skip to content

Commit

Permalink
Merge 80c2b15 into 54a94a8
Browse files Browse the repository at this point in the history
  • Loading branch information
iprafols committed Apr 28, 2022
2 parents 54a94a8 + 80c2b15 commit c084e6f
Show file tree
Hide file tree
Showing 142 changed files with 457,489 additions and 689 deletions.
4 changes: 2 additions & 2 deletions py/picca/delta_extraction/astronomical_objects/desi_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class DesiForest(Forest):
Class Attributes
----------------
(see Forest in py/picca/delta_extraction/astronomical_objects/forest.py)
Attributes
----------
(see Forest in py/picca/delta_extraction/astronomical_objects/forest.py)
Expand Down Expand Up @@ -92,7 +92,7 @@ def coadd(self, other):
if not isinstance(other, DesiForest):
raise AstronomicalObjectError("Error coadding DesiForest. Expected "
"DesiForest instance in other. Found: "
f"{type(other)}")
f"{type(other).__name__}")
self.night += other.night
self.petal += other.petal
self.tile += other.tile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DesiPk1dForest(DesiForest, Pk1dForest):
----------
(see DesiForest in py/picca/delta_extraction/astronomical_objects/desi_forest.py)
(see Pk1dForest in py/picca/delta_extraction/astronomical_objects/pk1d_forest.py)
resolution_matrix: 2d-array of floats or None
Resolution matrix of the forests
"""
Expand All @@ -57,7 +57,7 @@ def __init__(self, **kwargs):
raise AstronomicalObjectError(
"Error constructing DesiPk1dForest. "
"Missing variable 'resolution_matrix'")
if "resolution_matrix" in kwargs:
else:
del kwargs["resolution_matrix"]

# call parent constructors
Expand All @@ -70,7 +70,7 @@ def consistency_check(self):
super().consistency_check()
if self.resolution_matrix.shape[1] != self.flux.shape[0]:
raise AstronomicalObjectError(
"Error constructing DesiPk1dForest. 'resolution_matrix', "
"Error constructing DesiPk1dForest. 'resolution_matrix' "
"and 'flux' don't have the "
"same size")
if "resolution_matrix" not in Forest.mask_fields:
Expand All @@ -95,7 +95,7 @@ def coadd(self, other):
raise AstronomicalObjectError(
"Error coadding DesiPk1dForest. Expected "
"DesiPk1dForest instance in other. Found: "
f"{type(other)}")
f"{type(other).__name__}")

if other.resolution_matrix.size > 0 and self.resolution_matrix.size > 0:
self.resolution_matrix = np.append(self.resolution_matrix,
Expand Down
80 changes: 43 additions & 37 deletions py/picca/delta_extraction/astronomical_objects/forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Forest(AstronomicalObject):
blinding = "none"
log_lambda_grid = None
log_lambda_rest_frame_grid = None
mask_fields = []
mask_fields = None
wave_solution = None

def __init__(self, **kwargs):
Expand Down Expand Up @@ -136,49 +136,60 @@ def __init__(self, **kwargs):
if kwargs.get("weights") is not None:
del kwargs["weights"]

# compute mean quality variables
snr = self.flux * np.sqrt(self.ivar)
self.mean_snr = sum(snr) / float(len(snr))

# call parent constructor
super().__init__(**kwargs)

self.consistency_check()

# compute mean quality variables
snr = self.flux * np.sqrt(self.ivar)
self.mean_snr = sum(snr) / float(len(snr))

@classmethod
def class_variable_check(cls):
"""Check that class variables have been correctly initialized"""
if cls.log_lambda_grid is None:
raise AstronomicalObjectError("Error constructing Forest. "
"Class variable 'log_lambda_grid' "
"must be set prior to initialize "
"instances of this type")
raise AstronomicalObjectError(
"Error constructing Forest. Class variable 'log_lambda_grid' "
"must be set prior to initialize instances of this type. This "
"probably means you did not run Forest.set_class_variables"
)
if cls.log_lambda_rest_frame_grid is None:
raise AstronomicalObjectError("Error constructing Forest. "
"Class variable 'log_lambda_rest_frame_grid' "
"must be set prior to initialize "
"instances of this type")
raise AstronomicalObjectError(
"Error constructing Forest. Class variable "
"'log_lambda_rest_frame_grid' must be set prior to initialize "
"instances of this type. This probably means you did not run "
"Forest.set_class_variables"
)
if cls.mask_fields is None:
raise AstronomicalObjectError(
"Error constructing Forest. Class variable "
"'mask_fields' must be set prior to initialize "
"instances of this type. This probably means you did not run "
"Forest.set_class_variables"
)
if not isinstance(cls.mask_fields, list):
raise AstronomicalObjectError(
"Error constructing Forest. "
"Expected list in class variable 'mask fields'. "
f"Found {cls.mask_fields}.")

f"Found '{cls.mask_fields}'."
)
if cls.wave_solution is None:
raise AstronomicalObjectError("Error constructing Forest. "
"Class variable 'wave_solution' "
"must be set prior to initialize "
"instances of this type")
raise AstronomicalObjectError(
"Error constructing Forest. Class variable 'wave_solution' "
"must be set prior to initialize instances of this type. This "
"probably means you did not run Forest.set_class_variables"
)

def consistency_check(self):
"""Consistency checks after __init__"""
if self.flux.size != self.ivar.size:
raise AstronomicalObjectError("Error constructing Forest. 'flux', "
raise AstronomicalObjectError("Error constructing Forest. 'flux' "
"and 'ivar' don't have the same size")
if self.log_lambda.size != self.flux.size:
raise AstronomicalObjectError("Error constructing Forest. "
"'flux' and 'log_lambda' don't "
"have the same size")
"'flux' and 'log_lambda' don't "
"have the same size")

def coadd(self, other):
"""Coadd the information of another forest.
Expand All @@ -199,7 +210,7 @@ def coadd(self, other):
if not isinstance(other, Forest):
raise AstronomicalObjectError("Error coadding Forest. Expected "
"Forest instance in other. Found: "
f"{type(other)}")
f"{type(other).__name__}")

if self.los_id != other.los_id:
raise AstronomicalObjectError("Attempting to coadd two Forests "
Expand Down Expand Up @@ -259,10 +270,10 @@ def get_data(self):
units += ["Angstrom"]
array_size = self.log_lambda.size
else:
raise AstronomicalObjectError("Error in getting data from Forest. "
raise AstronomicalObjectError("Error in Forest.get_data(). "
"Class variable 'wave_solution' "
"must be either 'lin' or 'log'. "
f"Found: {Forest.wave_solution}")
f"Found: '{Forest.wave_solution}'")

if self.deltas is None:
cols += [np.zeros(array_size, dtype=float)]
Expand Down Expand Up @@ -344,7 +355,7 @@ def get_header(self):
raise AstronomicalObjectError("Error in Forest.get_header(). "
"Class variable 'wave_solution' "
"must be either 'lin' or 'log'. "
f"Found: {Forest.wave_solution}")
f"Found: '{Forest.wave_solution}'")

return header

Expand Down Expand Up @@ -408,10 +419,10 @@ def rebin(self):
w1 &= (lambda_ / (1. + self.z) < 10**Forest.log_lambda_rest_frame_grid[-1] + half_pixel_step_rest_frame)
w1 &= (self.ivar > 0.)
else:
raise AstronomicalObjectError("Error in rebinning Forest. "
raise AstronomicalObjectError("Error in Forest.rebin(). "
"Class variable 'wave_solution' "
"must be either 'lin' or 'log'. "
f"Found: {Forest.wave_solution}")
f"Found: '{Forest.wave_solution}'")

if w1.sum() == 0:
self.log_lambda = np.array([])
Expand Down Expand Up @@ -442,12 +453,10 @@ def rebin(self):
)] += rebin_transmission_correction_aux
rebin_ivar[:len(rebin_ivar_aux)] += rebin_ivar_aux

# this condition should always be non-zero for at least one pixel
# this does not mean that all rebin_ivar pixels will be non-zero,
# as we could have a masked region of the spectra
w2 = (rebin_ivar > 0.)
if w2.sum() == 0:
raise AstronomicalObjectError(
"Attempting to rebin arrays flux and "
"ivar in class Forest, but ivar seems "
"to contain only zeros")
self.flux = rebin_flux[w2] / rebin_ivar[w2]
self.transmission_correction = rebin_transmission_correction[
w2] / rebin_ivar[w2]
Expand All @@ -458,13 +467,10 @@ def rebin(self):
rebin_log_lambda = (Forest.log_lambda_grid[0] +
np.arange(bins.max() + 1) * pixel_step)
self.log_lambda = rebin_log_lambda[w2]
elif self.wave_solution == "lin":
else: # we have already checked that it will always be "lin" at this point
rebin_lambda = (10**Forest.log_lambda_grid[0] +
np.arange(bins.max() + 1) * pixel_step)
self.log_lambda = np.log10(rebin_lambda[w2])
else:
raise AstronomicalObjectError("wavelength solution must be either "
"'log' or 'linear'")

# finally update control variables
snr = self.flux * np.sqrt(self.ivar)
Expand Down
13 changes: 6 additions & 7 deletions py/picca/delta_extraction/astronomical_objects/pk1d_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,16 @@ def __init__(self, **kwargs):
def class_variable_check(cls):
"""Check that class variables have been correctly initialized"""
if cls.lambda_abs_igm is None:
raise AstronomicalObjectError("Error constructing Pk1DForest. "
"Class variable 'lambda_abs_igm' "
"must be set prior to initialize "
"instances of this type")
raise AstronomicalObjectError(
"Error constructing Pk1dForest. Class variable 'lambda_abs_igm' "
"must be set prior to initialize instances of this type")

def consistency_check(self):
"""Consistency checks after __init__"""
super().consistency_check()
if self.flux.size != self.exposures_diff.size:
raise AstronomicalObjectError(
"Error constructing Pk1dForest. 'flux', "
"Error constructing Pk1dForest. 'flux' "
"and 'exposures_diff' don't have the "
"same size")
if "exposures_diff" not in Forest.mask_fields:
Expand All @@ -110,7 +109,7 @@ def consistency_check(self):
Forest.mask_fields += ["reso"]
if "reso_pix" not in Forest.mask_fields:
Forest.mask_fields += ["reso_pix"]


def coadd(self, other):
"""Coadd the information of another forest.
Expand All @@ -131,7 +130,7 @@ def coadd(self, other):
raise AstronomicalObjectError(
"Error coadding Pk1dForest. Expected "
"Pk1dForest instance in other. Found: "
f"{type(other)}")
f"{type(other).__name__}")
self.exposures_diff = np.append(self.exposures_diff,
other.exposures_diff)
self.reso = np.append(self.reso, other.reso)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def coadd(self, other):
if not isinstance(other, SdssForest):
raise AstronomicalObjectError("Error coadding SdssForest. Expected "
"SdssForest instance in other. Found: "
f"{type(other)}")
f"{type(other).__name__}")

self.fiberid += other.fiberid
self.mjd += other.mjd
Expand Down

0 comments on commit c084e6f

Please sign in to comment.