def test_correct_rms_values_vs_cross(self, norm): """The rms calculated with independent event lists (from the cospectrum) is equivalent to the one calculated with one event list (from the PDS)""" rmsspec_cross = RmsEnergySpectrum( self.test_ev1, freq_interval=[0.00001, 0.1], energy_spec=(0.3, 12, 2, "lin"), bin_time=self.bin_time / 2, segment_size=9999, events2=self.test_ev2, norm=norm, ) rmsspec_pds = RmsEnergySpectrum( self.test_ev1, freq_interval=[0.00001, 0.1], energy_spec=(0.3, 12, 2, "lin"), bin_time=self.bin_time / 2, segment_size=9999, norm=norm, ) pds = rmsspec_pds.spectrum cross = rmsspec_cross.spectrum err = rmsspec_pds.spectrum_error cerr = rmsspec_cross.spectrum_error assert np.allclose(err, cerr, rtol=0.2) assert np.allclose(pds, cross, atol=3 * err) if norm == "frac": assert np.allclose(pds, 0.20, atol=3 * err)
def test_rms_invalid_evlist_warns(self): ev = EventList(time=[], energy=[], gti=self.rms.events1.gti) with pytest.warns(UserWarning) as record: rms = RmsEnergySpectrum(ev, [0., 100], (0.3, 12, 5, "lin"), bin_time=0.01, segment_size=100, events2=self.rms.events2) assert np.allclose(rms.spectrum, 0) assert np.allclose(rms.spectrum_error, 0)
def setup_class(cls): from ..simulator.simulator import Simulator simulator = Simulator(0.1, 1000, rms=0.4, mean=200) test_lc = simulator.simulate(1) test_ev1, test_ev2 = EventList(), EventList() test_ev1.simulate_times(test_lc) test_ev2.simulate_times(test_lc) test_ev1.energy = np.random.uniform(0.3, 12, len(test_ev1.time)) test_ev2.energy = np.random.uniform(0.3, 12, len(test_ev2.time)) cls.rms = RmsEnergySpectrum(test_ev1, [0., 100], (0.3, 12, 5, "lin"), bin_time=0.01, segment_size=100, events2=test_ev2)
def main(args=None): import argparse description = ('Calculates variability-energy spectra') parser = argparse.ArgumentParser(description=description) parser.add_argument("files", help="List of files", nargs='+') parser.add_argument('-f', "--freq-interval", nargs=2, type=float, default=[0., 100], help="Frequence interval") parser.add_argument("--energy-values", nargs=4, type=str, default="0.3 12 5 lin".split(" "), help="Choose Emin, Emax, number of intervals," "interval spacing, lin or log") parser.add_argument("--segment-size", type=float, default=512, help="Length of the light curve intervals to be " "averaged") parser.add_argument("-b", "--bin-time", type=float, default=1, help="Bin time for the light curve") parser.add_argument("--ref-band", nargs=2, type=float, default=None, help="Reference band when relevant") parser.add_argument("--rms", default=False, action='store_true', help="Calculate rms") parser.add_argument("--covariance", default=False, action='store_true', help="Calculate covariance spectrum") parser.add_argument("--use-pi", default=False, action='store_true', help="Energy intervals are specified as PI channels") parser.add_argument("--cross-instr", default=False, action='store_true', help="Use data files in pairs, for example with the" "reference band from one and the subbands from the" " other (useful in NuSTAR and multiple-detector " "missions)") parser.add_argument("--lag", default=False, action='store_true', help="Calculate lag-energy") parser.add_argument("--loglevel", help=("use given logging level (one between INFO, " "WARNING, ERROR, CRITICAL, DEBUG; " "default:WARNING)"), default='WARNING', type=str) parser.add_argument("--debug", help="use DEBUG logging level", default=False, action='store_true') args = parser.parse_args(args) if args.debug: args.loglevel = 'DEBUG' numeric_level = getattr(logging, args.loglevel.upper(), None) logging.basicConfig(filename='HENvarenergy.log', level=numeric_level, filemode='w') filelist = [] energy_spec = (float(args.energy_values[0]), float(args.energy_values[1]), int(args.energy_values[2]), args.energy_values[3]) from .io import sort_files if args.cross_instr: logging.info('Sorting file list') sorted_files = sort_files(args.files) logging.warning('Beware! For cpds and derivatives, I assume that the ' 'files are from only two instruments and in pairs ' '(even in random order)') instrs = list(sorted_files.keys()) files1 = sorted_files[instrs[0]] files2 = sorted_files[instrs[1]] else: files1 = args.files files2 = args.files for fnames in zip(files1, files2): fname = fnames[0] fname2 = fnames[1] events = load_events(fname) events2 = load_events(fname2) if not args.use_pi and \ (events.energy is None or events2.energy is None): raise ValueError( "If --use-pi is not specified, event lists must be" " calibrated! Please use HENcalibrate.") if args.rms: rms = RmsEnergySpectrum(events, args.freq_interval, energy_spec, segment_size=args.segment_size, bin_time=args.bin_time, events2=events2, use_pi=args.use_pi) out1 = hen_root(fname) + "_rms" + '.qdp' start_energy = np.asarray(rms.energy_intervals)[:, 0] stop_energy = np.asarray(rms.energy_intervals)[:, 1] save_as_qdp([start_energy, stop_energy, rms.spectrum], [None, None, rms.spectrum_error], filename=out1) filelist.append(out1) if args.lag: lag = LagEnergySpectrum(events, args.freq_interval, energy_spec, args.ref_band, segment_size=args.segment_size, bin_time=args.bin_time, events2=events2, use_pi=args.use_pi) start_energy = np.asarray(lag.energy_intervals)[:, 0] stop_energy = np.asarray(lag.energy_intervals)[:, 1] out2 = hen_root(fname) + "_lag" + '.qdp' save_as_qdp([start_energy, stop_energy, lag.spectrum], [None, None, lag.spectrum_error], filename=out2) filelist.append(out2) if args.covariance: try: from stingray.varenergyspectrum import CovarianceEnergySpectrum except: logging.warning('This version of Stingray does not implement ' 'the correct version of Covariance Spectrum.') continue cov = CovarianceEnergySpectrum(events, args.freq_interval, energy_spec, args.ref_band, segment_size=args.segment_size, bin_time=args.bin_time, events2=events2, use_pi=args.use_pi) start_energy = np.asarray(cov.energy_intervals)[:, 0] stop_energy = np.asarray(cov.energy_intervals)[:, 1] out2 = hen_root(fname) + "_cov" + '.qdp' save_as_qdp([start_energy, stop_energy, cov.spectrum], [None, None, cov.spectrum_error], filename=out2) filelist.append(out2) return filelist