def process_vis(self, vis_range=(390, 720), min_scan=None, max_scan=None, sigma=2.3, para_angle=45): data_file = self.file wls = self.vis_wls() t = data_file['t'] d = -data_file['data_Stresing CCD'][min_scan:max_scan, ...] if 'rot' in data_file: rot = data_file['rot'][min_scan:max_scan] para_idx = (abs(np.round(rot) - para_angle) < 3) else: n_ir_cwl = data_file['wl_Remote IR 32x2'].shape[0] para_idx = np.repeat(np.array([False, True], dtype='bool'), n_ir_cwl) dpm = sigma_clip(d[para_idx, ...], axis=0, sigma=sigma, max_iter=10) dsm = sigma_clip(d[~para_idx, ...], axis=0, sigma=sigma, max_iter=10) dp = dpm.mean(0) dps = dpm.std(0) ds = dsm.mean(0) dss = dsm.std(0) para = TimeResSpec(wls, t, dp[0, :, 0, ...], freq_unit='nm', disp_freq_unit='nm') perp = TimeResSpec(wls, t, ds[0, :, 0, ...], freq_unit='nm', disp_freq_unit='nm') pol = PolTRSpec(para, perp) pol = pol.cut_freq(*vis_range, invert_sel=True) return pol.para, pol.perp, pol
def test_pol_plot(): ds = TimeResSpec(wl, t, data) ds = ds.bin_freqs(50) ds = PolTRSpec(para=ds, perp=ds) ds = ds.bin_freqs(50) ds.plot.trans([550]) ds.plot.spec([2, 10]) ds.plot.trans([550], norm=1) ds.plot.trans([550], norm=1, marker='o')
def test_plot(): ds = TimeResSpec(wl, t, data) ds = ds.bin_freqs(50) ds.plot.trans([550]) ds.plot.spec([2, 10]) ds.plot.trans([550], norm=1) ds.plot.trans([550], norm=1, marker='o') ds.plot.map(plot_con=0) ds.plot.svd()
def test_plot(): ds = TimeResSpec(wl, t, data) ds = ds.bin_freqs(50) ds.plot.trans([550]) ds.plot.spec([2, 10]) ds.plot.trans_integrals((1e7 / 550, 1e7 / 600)) ds.plot.trans_integrals((1e7 / 600, 1e7 / 500)) ds.plot.trans([550], norm=1) ds.plot.trans([550], norm=1, marker='o') ds.plot.map(plot_con=0) ds.plot.svd()
def make_pol_ds(self, sigma=None) -> PolTRSpec: para = np.nanmean(self.par_data, axis=0) ds_para = TimeResSpec(self.wavelength, self.t, 1000 * para, disp_freq_unit='cm') perp = np.nanmean(self.per_data, axis=0) ds_perp = TimeResSpec(self.wavelength, self.t, 1000 * perp, disp_freq_unit='cm') return PolTRSpec(ds_para, ds_perp)
def test_sas(): from skultrafast.kinetic_model import Model ds = TimeResSpec(wl, t, data) x0 = [0.1, 0.1, 1, 1000] out = ds.fit_exp(x0) m = Model() m.add_transition('S1', 'S1*', 'k1') m.add_transition('S1', 'zero', 'k2') out.make_sas(m, {}) m2 = Model() m2.add_transition('S1', 'S1*', 'k1', 'qy1') m2.add_transition('S1', 'zero', 'k2') out.make_sas(m2, {'qy1': 0.5})
def test_concat(): ds = TimeResSpec(wl, t, data) ds = ds.bin_freqs(50) n = ds.wavelengths.size // 2 ds1 = ds.cut_freq(ds.wavelengths[n], np.inf) ds2 = ds.cut_freq(-np.inf, ds.wavelengths[n]) dsc = ds1.concat_datasets(ds2) assert (np.allclose(dsc.data, ds.data)) pol_ds = PolTRSpec(ds, ds) a = PolTRSpec(ds1, ds1) b = PolTRSpec(ds2, ds2) pol_dsc = a.concat_datasets(b) for p in 'para', 'perp', 'iso': assert (np.allclose(getattr(pol_dsc, p).data, getattr(pol_ds, p).data))
def test_methods(): ds = TimeResSpec(wl, t, data) bds = ds.bin_freqs(300) assert (len(bds.wavelengths) == 300) nds = ds.cut_freqs([(400, 600)]) assert (np.all(nds.wavelengths > 600)) nds = ds.cut_times([(-100, 1)]) assert (np.all(nds.t > .99)) nds = ds.bin_times(5) assert (nds.t.size == np.ceil(ds.t.size / 5)) ds.mask_freqs([(400, 600)]) assert (np.all(ds.data.mask[:, ds.wl_idx(550)]))
def process_ir(self, t0=0, min_scans=0, max_scans=None, subtract_background=True, center_ch=16, disp=14, sigma=3) -> PolTRSpec: data_file = self.file t = data_file['t'] - t0 wli = data_file['wl_Remote IR 32x2'] print(wli[:, 16, None]) wli = -(disp * (np.arange(32) - center_ch)) + wli[:, 16, None] wli = 1e7 / wli d = data_file['data_Remote IR 32x2'][min_scans:max_scans] print(d.shape) dp = sigma_clip(d[1::2, ...], axis=0, sigma=sigma) dpm = dp.mean(0) ds = sigma_clip(d[0::2, ...], axis=0, sigma=sigma) dsm = ds.mean(0) if subtract_background: dsm -= dsm[:, :10, ...].mean(1, keepdims=True) dpm -= dpm[:, :10, ...].mean(1, keepdims=True) para = TimeResSpec(wli[0], t, dpm[0, :, 0, ...], freq_unit='cm', disp_freq_unit='cm') perp = TimeResSpec(wli[0], t, dsm[0, :, 0, ...], freq_unit='cm', disp_freq_unit='cm') para.plot.spec(1, n_average=20) for i in range(1, wli.shape[0]): para_t = TimeResSpec(wli[i], t, dpm[i, :, 0, ...], freq_unit='cm', disp_freq_unit='cm') para_t.plot.spec(1, n_average=20) para = para.concat_datasets(para_t) perp = perp.concat_datasets( TimeResSpec(wli[i], t, dsm[i, :, 0, ...], freq_unit='cm', disp_freq_unit='cm')) both = PolTRSpec(para, perp) return both
def test_das_pol_plots(): ds = TimeResSpec(wl, t, data) pds = PolTRSpec(ds, ds) # fake pol x0 = [0.1, 0.1, 1, 1000] out = pds.fit_exp(x0) pds.plot.das() pds.plot.edas()
def test_pol_tr(): ds = TimeResSpec(wl, t, data) ds2 = TimeResSpec(wl, t, data) ps = PolTRSpec(para=ds, perp=ds2) out = ps.bin_freqs(10) assert (out.para.wavenumbers.size == 10) assert (out.perp.wavenumbers.size == 10) assert_almost_equal(out.perp.data, out.para.data) ps.subtract_background() ps.mask_freqs([(400, 550)]) print(ps.para.data.mask, ps.para.data.mask[1, ps.para.wl_idx(520)]) assert (ps.para.data.mask[1, ps.para.wl_idx(520)]) out = ps.cut_freqs([(400, 550)]) assert (np.all(out.para.wavelengths >= 550)) assert (np.all(out.perp.wavelengths >= 550)) ps.bin_times(6)
def test_methods(): ds = TimeResSpec(wl, t, data) bds = ds.bin_freqs(300) assert(len(bds.wavelengths) == 300) nds = ds.cut_freqs([(400, 600)]) assert(np.all(nds.wavelengths > 600)) nds = ds.cut_times([(-100, 1)]) assert(np.all(nds.t > .99)) nds = ds.bin_times(5) assert(nds.t.size == np.ceil(ds.t.size/5)) ds.mask_freqs([(400, 600)]) assert(np.all(ds.data.mask[:, ds.wl_idx(550)]))
def test_sas_pol_plots(): from skultrafast.kinetic_model import Model ds = TimeResSpec(wl, t, data) pds = PolTRSpec(ds, ds) # fake pol x0 = [0.1, 0.1, 1, 1000] out = pds.fit_exp(x0) m = Model() m.add_transition('S1', 'S1*', 'k1') m.add_transition('S1', 'zero', 'k2') pds.plot.sas(m)
def integrate_pump(self, lower: float = -np.inf, upper: float = np.inf) -> TimeResSpec: """ Calculate and return 1D Time-resolved spectra for given range. Parameters ---------- lower : float Lower pump wl upper : float upper pump wl Returns ------- TimeResSpec The corresponding 1D Dataset """ pu_idx = inbetween(self.pump_wn, lower, upper) data = np.trapz(self.spec2d[:, :, pu_idx], self.pump_wn[pu_idx], axis=-1) return TimeResSpec(self.probe_wn, self.t, data, freq_unit='cm')
def test_integrate(): ds = TimeResSpec(wl, t, data) ds.wn_i(15000, 20000)
def test_das_plots(): ds = TimeResSpec(wl, t, data) x0 = [0.1, 0.1, 1, 1000] out = ds.fit_exp(x0) ds.plot.das() ds.plot.edas()
def test_error_calc(): ds = TimeResSpec(wl, t, data) x0 = [0.1, 0.1, 1, 1000] out = ds.fit_exp(x0) out.calculate_stats()
def test_fitter(): ds = TimeResSpec(wl, t, data) x0 = [0.1, 0.1, 1, 1000] out = ds.fit_exp(x0)
def test_est_disp(): ds = TimeResSpec(wl, t, data) ds.auto_plot = False for s in ['abs', 'diff', 'gauss_diff', 'max']: ds.estimate_dispersion(heuristic=s)
def average_scans(self, sigma=3, max_iter=3, min_scan=0, max_scan=None, disp_freq_unit=None): """ Calculate the average of the scans. Uses sigma clipping, which also filters nans. For polarization resolved measurements, the function assumes that the polarisation switches every scan. Parameters ---------- sigma : float sigma used for sigma clipping. max_iter: int Maximum iterations in sigma clipping. min_scan : int or None All scans before min_scan are ignored. max_scan : int or None If `None`, use all scan, else just use the scans up to max_scan. disp_freq_unit : 'nm', 'cm' or None Sets `disp_freq_unit` of the created datasets. Returns ------- dict or TimeResSpec TimeResSpec or Dict of DataSets containing the averaged datasets. If the first delay-time are identical, they are interpreted as background and their mean is subtracted. """ if max_scan is None: sub_data = self.data else: sub_data = self.data[..., min_scan:max_scan] num_wls = self.data.shape[0] t = self.t if disp_freq_unit is None: disp_freq_unit = "nm" if self.wl.shape[0] > 32 else "cm" kwargs = dict(disp_freq_unit=disp_freq_unit) if not self.is_pol_resolved: data = sigma_clip(sub_data, sigma=sigma, max_iter=max_iter, axis=-1) mean = data.mean(-1) std = data.std(-1) err = std / np.sqrt((~data.mask).sum(-1)) if self.valid_channel in [0, 1]: mean = mean[..., self.valid_channel] std = std[..., self.valid_channel] err = err[..., self.valid_channel] out = {} if num_wls > 1: for i in range(num_wls): ds = TimeResSpec(self.wl[:, i], t, mean[i, ..., :], err[i, ...], **kwargs) out[self.pol_first_scan + str(i)] = ds else: out = TimeResSpec(self.wl[:, 0], t, mean[0, ...], err[0, ...], **kwargs) return out else: raise NotImplementedError("TODO") elif self.is_pol_resolved and self.valid_channel in [0, 1]: assert self.pol_first_scan in ["para", "perp"] data1 = sigma_clip(sub_data[..., self.valid_channel, ::2], sigma=sigma, max_iter=max_iter, axis=-1) mean1 = data1.mean(-1) std1 = data1.std(-1, ddof=1) err1 = std1 / np.sqrt(np.ma.count(data1, -1)) data2 = sigma_clip(sub_data[..., self.valid_channel, 1::2], sigma=sigma, max_iter=max_iter, axis=-1) mean2 = data2.mean(-1) std2 = data2.std(-1, ddof=1) err2 = std2 / np.sqrt(np.ma.count(data2, -1)) out = {} for i in range(num_wls): wl, t = self.wl[:, i], self.t if self.pol_first_scan == "para": para = mean1[i, ...] para_err = err1[i, ...] perp = mean2[i, ...] perp_err = err2[i, ...] elif self.pol_first_scan == "perp": para = mean2[i, ...] para_err = err2[i, ...] perp = mean1[i, ...] perp_err = err1[i, ...] para_ds = TimeResSpec(wl, t, para, para_err, **kwargs) perp_ds = TimeResSpec(wl, t, perp, perp_err, **kwargs) out["para" + str(i)] = para_ds out["perp" + str(i)] = perp_ds iso = 1/3*para + 2/3*perp out["iso" + str(i)] = TimeResSpec(wl, t, iso, **kwargs) self.av_scans_ = out return out else: raise NotImplementedError("Iso correction not supported yet.")
def test_methods(): ds = TimeResSpec(wl, t, data) bds = ds.bin_freqs(300) ds2 = TimeResSpec(1e7 / wl, t, data, freq_unit='cm', disp_freq_unit='cm') bds2 = ds2.bin_freqs(50) assert (np.all(np.isfinite(bds2.data))) assert (len(bds.wavelengths) == 300) nds = ds.cut_freq(400, 600) assert (np.all(nds.wavelengths > 600)) nds = ds.cut_time(-100, 1) assert (np.all(nds.t > .99)) nds = ds.bin_times(5) assert (nds.t.size == np.ceil(ds.t.size / 5)) ds.mask_freqs([(400, 600)]) assert (np.all(ds.data.mask[:, ds.wl_idx(550)])) ds2 = ds.scale_and_shift(2, t_shift=1, wl_shift=10) assert_almost_equal(2 * ds.data, ds2.data) assert_almost_equal(ds.t + 1, ds2.t) assert_almost_equal(ds.wavelengths + 10, ds2.wavelengths) assert_almost_equal(1e7 / ds2.wavelengths, ds2.wavenumbers)
# another one containing the delay times and one two dimensional array containing # the data in mOD. wavelengths, t_ps, data_mOD = data_io.load_example() # %% # Lets look at the constructor of the `TimeResSpec` (Time resolved Spectra) class, # which is the main object when working with single data: print(TimeResSpec.__init__.__doc__) # %% # As we see, we can supply all the required parameters. # Since the `freq_unit` defaults to 'nm' we don't need to supply this argument. ds = TimeResSpec(wavelengths, t_ps, data_mOD) # %% # The TimeResSpec object simply consists of data itself and methods using that # data. The attributes containing the data can be accessed under `ds.data`, # `ds.wavenumbers`, `ds.wavelengths` and `ds.t`. print(ds.data.shape, ds.t.shape, ds.wavelengths.shape) # %% # The TimeResSpec object also has some helper methods to work with the data. # These functions find the index of the nearest value for a given number, e.g. to find # the position in the time array where the time is zero we can call the `t_idx` # method
def test_merge(): ds = TimeResSpec(wl, t, data) nds = ds.merge_nearby_channels(10) assert (nds.wavelengths.size < ds.wavelengths.size)
def test_plot(): ds = TimeResSpec(wl, t, data) ds = ds.bin_freqs(50) ds.trans([550]) ds.spec([2, 10]) ds.trans([550], norm=1) ds.trans([550], norm=1, marker='o') ds.plot.map(plot_con=0) ds.plot.svd()