diff --git a/pesummary/gw/file/calibration.py b/pesummary/gw/file/calibration.py index 84f8f5876f674037584055be27b136d3b3a4f726..f0281704c7879131d7938011a317a09a23b7bc89 100644 --- a/pesummary/gw/file/calibration.py +++ b/pesummary/gw/file/calibration.py @@ -56,6 +56,25 @@ class Calibration(np.ndarray): ) return obj + @classmethod + def read(cls, path_to_file, IFO=None, **kwargs): + """Read in a file and initialize the Calibration class + + Parameters + ---------- + path_to_file: str + the path to the file you wish to load + IFO: str, optional + name of the IFO which relates to the input file + **kwargs: dict + all kwargs are passed to the np.genfromtxt method + """ + try: + f = np.genfromtxt(path_to_file, **kwargs) + return cls(f) + except Exception: + raise + def save_to_file(self, file_name, comments="#", delimiter=conf.delimiter): """Save the calibration data to file diff --git a/pesummary/gw/inputs.py b/pesummary/gw/inputs.py index 8ef012f769dcd8f432a215d0ed0d814de9a53c7a..f1b8af429c3d836490c7876e6e9d8dc0c09c37be 100644 --- a/pesummary/gw/inputs.py +++ b/pesummary/gw/inputs.py @@ -654,31 +654,48 @@ class _GWInput(_Input): logger.warn(msg) @staticmethod - def extract_psd_data_from_file(file, IFO=None): - """Return the data stored in a psd file + def _extract_IFO_data_from_file(file, cls, desc, IFO=None): + """Return IFO data stored in a file Parameters ---------- file: path - path to a file containing the psd data + path to a file containing the IFO data + cls: obj + class you wish to use when loading the file. This class must have + a '.read' method + desc: str + description of the IFO data stored in the file + IFO: str, optional + the IFO which the data belongs to """ - from pesummary.gw.file.psd import PSD - general = ( - "Failed to read in PSD data because {}. The PSD plot will be " - "generated and the PSD data will not be added to the metafile." - ) + "Failed to read in %s data because {}. The %s plot will not be " + "generated and the %s data will not be added to the metafile." + ) % (desc, desc, desc) try: - return PSD.read(file, IFO=IFO) + return cls.read(file, IFO=IFO) except FileNotFoundError: - logger.info( + logger.warn( general.format("the file {} does not exist".format(file)) ) return {} except ValueError as e: - logger.info(general.format(e)) + logger.warn(general.format(e)) return {} + @staticmethod + def extract_psd_data_from_file(file, IFO=None): + """Return the data stored in a psd file + + Parameters + ---------- + file: path + path to a file containing the psd data + """ + from pesummary.gw.file.psd import PSD + return _GWInput._extract_IFO_data_from_file(file, PSD, "PSD", IFO=IFO) + @staticmethod def extract_calibration_data_from_file(file, **kwargs): """Return the data stored in a calibration file @@ -688,24 +705,10 @@ class _GWInput(_Input): file: path path to a file containing the calibration data """ - general = ( - "Failed to read in calibration data because {}. The calibration " - "plot will not be generated and the calibration data will not be " - "added to the metafile" - ) from pesummary.gw.file.calibration import Calibration - - try: - f = np.genfromtxt(file) - return Calibration(f) - except FileNotFoundError: - logger.info( - general.format("the file {} does not exist".format(file)) - ) - return {} - except ValueError as e: - logger.info(general.format(e)) - return {} + return _GWInput._extract_IFO_data_from_file( + file, Calibration, "calibration", **kwargs + ) @staticmethod def get_ifo_from_file_name(file): @@ -940,9 +943,13 @@ class GWInput(_GWInput, Input): def copy_files(self): """Copy the relevant file to the web directory """ + _error = "Failed to save the {} to file" for label in self.labels: if self.psd[label] != {}: for ifo in self.psd[label].keys(): + if not isinstance(self.psd[label][ifo], PSD): + logger.warn(_error.format("{} PSD".format(ifo))) + continue self.psd[label][ifo].save_to_file( os.path.join(self.webdir, "psds", "{}_{}_psd.dat".format( label, ifo @@ -951,6 +958,18 @@ class GWInput(_GWInput, Input): if label in self.priors["calibration"].keys(): if self.priors["calibration"][label] != {}: for ifo in self.priors["calibration"][label].keys(): + _instance = isinstance( + self.priors["calibration"][label][ifo], Calibration + ) + if not _instance: + logger.warn( + _error.format( + "{} calibration envelope".format( + ifo + ) + ) + ) + continue self.priors["calibration"][label][ifo].save_to_file( os.path.join(self.webdir, "calibration", "{}_{}_cal.txt".format( label, ifo diff --git a/pesummary/gw/webpage/main.py b/pesummary/gw/webpage/main.py index 33bd0909d5d6c60efd3e63eeca74bc9ac0d8de78..6121e66b5a31bd47b5c71367bfa023d0172fb8a9 100644 --- a/pesummary/gw/webpage/main.py +++ b/pesummary/gw/webpage/main.py @@ -665,17 +665,18 @@ class _WebpageGeneration(_CoreWebpageGeneration): if self.priors is not None and "calibration" in self.priors.keys(): if label in self.priors["calibration"].keys(): for ifo in self.priors["calibration"][label].keys(): - table.append( - [ - base_string.format( - "%s calibration envelope file used for this " - "analysis" % (ifo), os.path.join( - self.calibration_path["other"], - "%s_%s_cal.txt" % (label, ifo) + if len(self.priors["calibration"][label][ifo]): + table.append( + [ + base_string.format( + "%s calibration envelope file used for " + "this analysis" % (ifo), os.path.join( + self.calibration_path["other"], + "%s_%s_cal.txt" % (label, ifo) + ) ) - ) - ] - ) + ] + ) return table def default_images_for_result_page(self, label):