def get_spectrum(self, nr_id=None, abmn=None, plot_filename=None): """Return a spectrum and its reciprocal counter part, if present in the dataset. Optimally, refer to the spectrum by its normal-reciprocal id. Returns ------- spectrum_nor : :py:class:`reda.eis.plots.sip_response` Normal spectrum. None if no normal spectrum is available spectrum_rec : :py:class:`reda.eis.plots.sip_response` or None Reciprocal spectrum. None if no reciprocal spectrum is available fig : :py:class:`matplotlib.Figure.Figure` , optional Figure object (only if plot_filename is set) """ assert nr_id is None or abmn is None # determine nr_id for given abmn tuple if abmn is not None: subdata = self.data.query( 'a == {} and b == {} and m == {} and n == {}'.format( *abmn)).sort_values('frequency') if subdata.shape[0] == 0: return None, None # determine the norrec-id of this spectrum nr_id = subdata['id'].iloc[0] # get spectra subdata_nor = self.data.query('id == {} and norrec=="nor"'.format( nr_id)).sort_values('frequency') subdata_rec = self.data.query('id == {} and norrec=="rec"'.format( nr_id)).sort_values('frequency') # create spectrum objects spectrum_nor = None spectrum_rec = None if subdata_nor.shape[0] > 0: spectrum_nor = eis_plot.sip_response( frequencies=subdata_nor['frequency'].values, rmag=subdata_nor['r'], rpha=subdata_nor['rpha'], ) if subdata_rec.shape[0] > 0: spectrum_rec = eis_plot.sip_response( frequencies=subdata_rec['frequency'].values, rmag=subdata_rec['r'], rpha=subdata_rec['rpha'], ) if plot_filename is not None: if spectrum_nor is not None: fig = spectrum_nor.plot( plot_filename, reciprocal=spectrum_rec, return_fig=True, title='a: {} b: {} m: {}: n: {}'.format( *subdata_nor[['a', 'b', 'm', 'n']].values[0, :])) return spectrum_nor, spectrum_rec, fig return spectrum_nor, spectrum_rec
def plot_result_spectrum(self, filename, nr): """Plot a given data and inversion response spectrum to file. WARNING: At this point does not take into account missing configurations for certain frequencies... Parameters ---------- filename : str Output filename nr : int Index of spectrum to plot. Starts at 0 """ rpha = np.vstack([ td.configs.measurements[td.a['inversion']['fwd_response_rpha'][-1]] for f, td in sorted(self.tds.items()) ]).T rmag = np.vstack([ td.configs.measurements[td.a['inversion']['fwd_response_rmag'][-1]] for f, td in sorted(self.tds.items()) ]).T spec1 = sip_response(self.frequencies, rmag=rmag[nr, :], rpha=rpha[nr, :]) rmag_true = np.vstack([ td.configs.measurements[td.a['measurements'][0]] for f, td in sorted(self.tds.items()) ]).T rpha_true = np.vstack([ td.configs.measurements[td.a['measurements'][1]] for f, td in sorted(self.tds.items()) ]).T spec_true = sip_response(self.frequencies, rmag=rmag_true[nr, :], rpha=rpha_true[nr, :]) spec_true.plot( filename, reciprocal=spec1, title='abmn: {}-{}-{}-{}'.format( *self.tds[self.frequencies[0]].configs.configs[nr]), label_nor='data', label_rec='inversion response', )
def get_measurement_responses(self): """Return a dictionary of sip_responses for the modeled SIP spectra Note that this function does NOT check that each frequency contains the same configurations! Returns ------- responses : dict Dictionary with configurations as keys """ # take configurations from first tomodir configs = self.tds[sorted(self.tds.keys())[0]].configs.configs measurements = self.measurements() responses = {} for config, sip_measurement in zip(configs, np.rollaxis(measurements, 1)): sip = sip_response(frequencies=self.frequencies, rmag=sip_measurement[:, 0], rpha=sip_measurement[:, 1]) responses[tuple(config)] = sip return responses
def get_spectrum(self, nr_id=None, abmn=None, withK=False, plot_filename=None): """ Return a spectrum and its reciprocal counter part, if present in the dataset. Optimally, refer to the spectrum by its normal-reciprocal id. If the timestep column is present, then return dictionaries for normal and reciprocal data, with one sip_response object associated with each timestep. If the parameter plot_filename is specified, then plots will be created using the SIP objects. If multiple timesteps are present, then the parameter plot_filename will be used as a template, and the timesteps will be appended for each plot. Parameters ---------- withK : bool If True, and the column "k" exists, then return an apparent spectrum with geometric factors included Returns ------- spectrum_nor : :py:class:`reda.eis.plots.sip_response` or dict or None Normal spectrum. None if no normal spectrum is available spectrum_rec : :py:class:`reda.eis.plots.sip_response` or dict or None Reciprocal spectrum. None if no reciprocal spectrum is available fig : :py:class:`matplotlib.Figure.Figure`, optional Figure object (only if plot_filename is set) """ assert nr_id is None or isinstance(nr_id, int) assert nr_id is None or abmn is None assert not withK or (withK and 'k' in self.data.columns) # Here are some problems with |dict|{} at end of 369 and 371 # determine nr_id for given abmn tuple if abmn is not None: subdata = self.data.query( 'a == {} and b == {} and m == {} and n == {}'.format( *abmn)).sort_values('frequency') if subdata.shape[0] == 0: return None, None # determine the norrec-id of this spectrum nr_id = subdata['id'].iloc[0] # get spectra subdata_nor = self.data.query('id == {} and norrec=="nor"'.format( nr_id)).sort_values('frequency') subdata_rec = self.data.query('id == {} and norrec=="rec"'.format( nr_id)).sort_values('frequency') # create spectrum objects spectrum_nor = {} spectrum_rec = {} if subdata_nor.shape[0] > 0: # create a spectrum for each timestep if 'timestep' in subdata_nor.columns: g_nor_ts = subdata_nor.groupby('timestep') with_timesteps = True else: # create a dummy group g_nor_ts = subdata_nor.groupby('id') with_timesteps = False spectrum_nor = {} for timestep, item in g_nor_ts: if withK: k = item['k'] else: k = 1 spectrum_nor[timestep] = eis_plot.sip_response( frequencies=item['frequency'].values, rmag=item['r'] * k, rpha=item['rpha'], ) if subdata_rec.shape[0] > 0: if 'timestep' in subdata_rec.columns: g_rec_ts = subdata_rec.groupby('timestep') with_timesteps = True else: g_rec_ts = subdata_rec.groupby('id') with_timesteps = False spectrum_rec = {} for timestep, item in g_rec_ts: if withK: k = item['k'] else: k = 1 spectrum_rec[timestep] = eis_plot.sip_response( frequencies=item['frequency'].values, rmag=item['r'] * k, rpha=item['rpha'], ) def _reduce_dicts(dictA, dictB): if len(dictA) <= 1 and len(dictB) <= 1: # reduce if len(dictA) > 0: dictA_reduced = [*dictA.values()][0] else: dictA_reduced = (None, ) if len(dictB) > 0: dictB_reduced = [*dictB.values()][0] else: dictB_reduced = (None, ) return dictA_reduced, dictB_reduced else: # do nothing return dictA, dictB if plot_filename is not None: ending = plot_filename[-4:] all_timesteps = { k for d in (spectrum_nor, spectrum_rec) for k in d.keys() } pairs = { k: [d.get(k, None) for d in (spectrum_nor, spectrum_rec)] for k in all_timesteps } for timestep, pair in pairs.items(): if with_timesteps: ts_suffix = '_ts_{}'.format(timestep) else: ts_suffix = '' filename = plot_filename[:-4] + ts_suffix + ending if pair[0] is None and pair[1] is not None: obj = pair[1] title = 'a: {} b: {} m: {}: n: {}'.format( *subdata_rec[['a', 'b', 'm', 'n']].values[0, :]) else: obj = pair[0] title = 'a: {} b: {} m: {}: n: {}'.format( *subdata_nor[['a', 'b', 'm', 'n']].values[0, :]) fig = obj.plot( filename, reciprocal=pair[1], return_fig=True, title=title, ) return [*_reduce_dicts(spectrum_nor, spectrum_rec), fig] return _reduce_dicts(spectrum_nor, spectrum_rec)
############################################################################# # Import the SIP data sip.import_sip04('sip_data.mat') ############################################################################# # show the data print(type(sip.data)) print(sip.data[['a', 'b', 'm', 'n', 'frequency', 'r', 'rpha']]) ############################################################################# # plot the spectrum from reda.eis.plots import sip_response spectrum = sip_response( frequencies=sip.data['frequency'].values, rcomplex=sip.data['zt'].values, ) # note the dtype indicates that no geometric factor was applied to the data fig = spectrum.plot(filename='spectrum.png', dtype='r', return_fig=True) ############################################################################# # save data to ascii file sip.export_specs_to_ascii('frequencies.dat', 'data.dat') # optionally: # install ccd_tools: pip install ccd_tools # then in the command line, run: # ccd_single --plot --norm 10
ax.set_title('f: {} Hz'.format(key)) fig.subplots_adjust( hspace=1, wspace=0.5, right=0.9, top=0.95, ) fig.savefig('pseudosections_radic.pdf') ############################################################################### # plotting of SIP/EIS spectra is still somewhat cumbersome, but will be # improved in the future import reda.eis.plots as eis_plot import numpy as np subdata = seit.data.query( 'a == 1 and b == 2 and m == 5 and n == 4').sort_values('frequency') # determine the norrec-id of this spectrum nr_id = subdata['id'].iloc[0] subdata_rec = seit.data.query( 'id == {} and norrec=="rec"'.format(nr_id)).sort_values('frequency') spectrum_nor = eis_plot.sip_response(frequencies=subdata['frequency'].values, rcomplex=subdata['r'] * np.exp(1j * subdata['rpha'] / 1000)) spectrum_rec = eis_plot.sip_response( frequencies=subdata_rec['frequency'].values, rcomplex=subdata_rec['r'] * np.exp(1j * subdata_rec['rpha'] / 1000)) with reda.CreateEnterDirectory('output_radic'): spectrum_nor.plot('spectrum.pdf', reciprocal=spectrum_rec, return_fig=True)