def spec_data_rebin(self): di, ti = self.centre tside = int(250 * 1.) dside = 300 delta_t = self.data.delta_t delta_dm = self.data.delta_dm ntime = self.data.spec_data.shape[1] nfreq = self.data.nfreq freq = self.data.freq max_freq = np.max(freq) the_dm = self.data.dm[di] disp_ind = self.data.disp_ind spec_data_delay = np.zeros((nfreq, tside), dtype=float) for ii in range(nfreq): delay_ind = int(round((disp_delay(freq[ii], the_dm, disp_ind) - disp_delay(max_freq, the_dm, disp_ind)) / delta_t)) start_i = ti - tside // 2 + delay_ind stop_i = start_i + tside start_o = 0 stop_o = tside if start_i < 0: start_o = -start_i start_i = 0 if stop_i > ntime: stop_o += (ntime - stop_i) stop_i = ntime if (stop_i > start_i) and (stop_o > start_o): spec_data_delay[ii,start_o:stop_o] = self.data.spec_data[ii, start_i:stop_i] rebin_factor_freq = 64 rebin_factor_time = 1 spec_data_delay.shape = ( nfreq // rebin_factor_freq, rebin_factor_freq, tside // rebin_factor_time, rebin_factor_time, ) spec_data_rebin = np.mean(np.mean(spec_data_delay, 3), 1) return spec_data_rebin
def plot_spec(self): di, ti = self.centre delta_t = self.data.delta_t delta_dm = self.data.delta_dm ntime = self.data.spec_data.shape[1] nfreq = self.data.nfreq freq = self.data.freq max_freq = np.max(freq) the_dm = self.data.dm[di] duration = self._duration spectrum = np.zeros(nfreq, dtype=float) disp_ind = self.data.disp_ind for ii in range(nfreq): f = freq[ii] delay_ind = int(round((disp_delay(freq[ii], the_dm, disp_ind) - disp_delay(max_freq, the_dm, disp_ind)) / delta_t)) # Jon's centre index seems to be the last bin in the window. start = ti + delay_ind - duration + 1 stop = start + duration spectrum[ii] = np.mean(self.data.spec_data[ii,start:stop]) self.fluence = sum(spectrum) colors = ['blue','red','darkgreen','orange'] rebin_factors = [ 4**ii for ii in range(4) ] for ii, rfact in enumerate(rebin_factors): spectrum_r = np.reshape(spectrum, (nfreq // rfact, rfact)) plt.plot( freq[rfact//2::rfact], np.mean(spectrum_r, 1), colors[ii % len(colors)], ) plt.ylabel("Fluence") plt.xlabel("Frequency (MHz)")
def inject_events(self, t0, data): """Assumes that subsequent calls always happen with later t0, although blocks may overlap.""" ntime = data.shape[1] time = np.arange(ntime) * self._delta_t + t0 f_max = max(self._freq) f_min = min(self._freq) f_mean = np.mean(self._freq) overlap_events = [ e for e in self._simulated_events if e.arrival_time(f_min) > t0 ] new_events = [] mu = self._rate * self._delta_t events_per_bin = nprand.poisson(mu, ntime) events_per_bin[time <= self._last_time_processed] = 0 event_inds, = np.where(events_per_bin) for ind in event_inds: for ii in range(events_per_bin[ind]): dm, fluence, width, spec_ind, disp_ind = \ self.draw_event_parameters() msg = ("Injecting simulated event at time = %5.2f, DM = %6.1f," " fluence = %f, width = %f, spec_ind = %3.1f, disp_ind" " = %3.1f.") logger.info( msg % (time[ind], dm, fluence, width, spec_ind, disp_ind)) t = disp_delay(f_min, dm, disp_ind) t = t - disp_delay(f_mean, dm, disp_ind) t = t + time[ind] e = Event(t, f_mean, dm, fluence, width, spec_ind, disp_ind) new_events.append(e) for e in overlap_events + new_events: e.add_to_data(t0, self._delta_t, self._freq, data) self._simulated_events = self._simulated_events + new_events self._last_time_processed = time[-1]
def inject_events(self, t0, data): """Assumes that subsequent calls always happen with later t0, although blocks may overlap.""" ntime = data.shape[1] time = np.arange(ntime) * self._delta_t + t0 f_max = max(self._freq) f_min = min(self._freq) f_mean = np.mean(self._freq) overlap_events = [e for e in self._simulated_events if e.arrival_time(f_min) > t0] new_events = [] mu = self._rate * self._delta_t events_per_bin = nprand.poisson(mu, ntime) events_per_bin[time <= self._last_time_processed] = 0 event_inds, = np.where(events_per_bin) for ind in event_inds: for ii in range(events_per_bin[ind]): dm, fluence, width, spec_ind, disp_ind = self.draw_event_parameters() msg = ( "Injecting simulated event at time = %5.2f, DM = %6.1f," " fluence = %f, width = %f, spec_ind = %3.1f, disp_ind" " = %3.1f." ) logger.info(msg % (time[ind], dm, fluence, width, spec_ind, disp_ind)) t = disp_delay(f_min, dm, disp_ind) t = t - disp_delay(f_mean, dm, disp_ind) t = t + time[ind] e = Event(t, f_mean, dm, fluence, width, spec_ind, disp_ind) new_events.append(e) for e in overlap_events + new_events: e.add_to_data(t0, self._delta_t, self._freq, data) self._simulated_events = self._simulated_events + new_events self._last_time_processed = time[-1]
def arrival_time(self, f): t = disp_delay(f, self._dm, self._disp_ind) t = t - disp_delay(self._f_ref, self._dm, self._disp_ind) return self._t_ref + t
def plot_freq(self): di, ti = self.centre tside = int(250 * 1.) dside = 300 delta_t = self.data.delta_t delta_dm = self.data.delta_dm ntime = self.data.spec_data.shape[1] nfreq = self.data.nfreq freq = self.data.freq max_freq = np.max(freq) the_dm = self.data.dm[di] disp_ind = self.data.disp_ind spec_data_delay = np.zeros((nfreq, tside), dtype=float) for ii in range(nfreq): delay_ind = int(round((disp_delay(freq[ii], the_dm, disp_ind) - disp_delay(max_freq, the_dm, disp_ind)) / delta_t)) start_i = ti - tside // 2 + delay_ind stop_i = start_i + tside start_o = 0 stop_o = tside if start_i < 0: start_o = -start_i start_i = 0 if stop_i > ntime: stop_o -= (ntime - stop_i) stop_i = ntime spec_data_delay[ii,start_o:stop_o] = self.data.spec_data[ii, start_i:stop_i] rebin_factor_freq = 64 rebin_factor_time = 1 spec_data_delay.shape = ( nfreq // rebin_factor_freq, rebin_factor_freq, tside // rebin_factor_time, rebin_factor_time, ) spec_data_rebin = np.mean(np.mean(spec_data_delay, 3), 1) image_mean = np.mean(spec_data_rebin) image_std = np.std(spec_data_rebin) vmin = image_mean - 1 * image_std vmax = image_mean + 5 * image_std tstart = (ti - tside // 2) * delta_t tstop = tstart + tside * delta_t plt.imshow( spec_data_rebin, extent=[tstart, tstop, freq[-1], freq[0]], vmin=vmin, vmax=vmax, aspect='auto', cmap = cm.Blues ) plt.xlabel("time (s)") plt.ylabel("Frequency (MHz)") plt.colorbar()
def plot_freq(self): di, ti = self.centre tside = int(250 * 1.) dside = 300 delta_t = self.data.delta_t delta_dm = self.data.delta_dm ntime = self.data.spec_data.shape[1] nfreq = self.data.nfreq freq = self.data.freq max_freq = np.max(freq) the_dm = self.data.dm[di] disp_ind = self.data.disp_ind spec_data_delay = np.zeros((nfreq, tside), dtype=float) for ii in range(nfreq): delay_ind = int(round((disp_delay(freq[ii], the_dm, disp_ind) - disp_delay(max_freq, the_dm, disp_ind)) / delta_t)) start_i = ti - tside // 2 + delay_ind stop_i = start_i + tside start_o = 0 stop_o = tside if start_i < 0: start_o = -start_i start_i = 0 if stop_i > ntime: stop_o += (ntime - stop_i) stop_i = ntime if (stop_i > start_i) and (stop_o > start_o): spec_data_delay[ii,start_o:stop_o] = self.data.spec_data[ii, start_i:stop_i] rebin_factor_freq = 64 rebin_factor_time = 1 spec_data_delay.shape = ( nfreq // rebin_factor_freq, rebin_factor_freq, tside // rebin_factor_time, rebin_factor_time, ) spec_data_rebin = np.mean(np.mean(spec_data_delay, 3), 1) image_mean = np.mean(spec_data_rebin) image_std = np.std(spec_data_rebin) vmin = image_mean - 1 * image_std vmax = image_mean + 5 * image_std tstart = (ti - tside // 2) * delta_t tstop = tstart + tside * delta_t plt.imshow( spec_data_rebin, extent=[tstart, tstop, freq[-1], freq[0]], vmin=vmin, vmax=vmax, aspect='auto', cmap = cm.Blues ) plt.xlabel("time (s)") plt.ylabel("Frequency (MHz)") plt.colorbar()