def format_sushibar(ftype_data, sobj_name=None): if not sobj_name in ftype_data.raw_data: return data = ftype_data.raw_data[sobj_name] # data = np.nan_to_num(ftype_data.raw_data[sobj_name]) header = ftype_data.header af3 = np.array([i for i in data if np.isnan(i[header.index('par2')])]) data = np.array([i for i in data if not np.isnan(i[header.index('par2')])]) # GENERATE RockPy Data object for data data = RockPy3.Data(data=data, column_names=list(map(str.lower, header))) data.rename_column('m', 'mag') data.rename_column('meas. time', 'time') data.define_alias('m', ('x', 'y', 'z')) if af3.any(): af3 = RockPy3.Data(data=af3, column_names=list(map(str.lower, header))) af3.rename_column('m', 'mag') af3.rename_column('meas. time', 'time') af3.define_alias('m', ('x', 'y', 'z')) else: af3 = None out = {'data': data, 'af3': af3} return out
def import_folder(folder, name='study', study=None): if not study: study = RockPy3.Study(name=name) files = [ i for i in os.listdir(folder) if not i == '.DS_Store' if not i.startswith('#') ] samples = defaultdict(list) for i in files: d = RockPy3.get_info_from_fname(join(folder, i)) samples[d['name']].append(d) for s in samples: sgroup_name = samples[s][0]['samplegroup'] if not sgroup_name in study.samplegroup_names: sg = RockPy3.SampleGroup(name=samples[s][0]['samplegroup']) study.add_samplegroup(sg) sg = study[sgroup_name] if not s in study.sdict: smpl = RockPy3.Sample(**samples[s][0]) sg.add_samples(smpl) for m in samples[s]: measurement = smpl.add_measurement(**m) if 'ISindex' in m: initial = get_IS(m, samples[s]) measurement.set_initial_state(**initial) samples[s].remove(initial) if 'IS' in m and m['IS'] == True: continue return study
def generate_import_info(self, mtype=None, fpath=None, ftype=None, idx=None, series=None): """ First generate the file info. It is read from the fname, if possible. After that the mtype, ftype, fpath and idx will be overwritten, assuming that you gave a proper filname. """ out = {'mtype': mtype, 'fpath': fpath, 'ftype': ftype, 'idx': idx, 'series': series} file_info = dict() with RockPy3.ignored(ValueError): file_info = RockPy3.core.file_operations.get_info_from_fname(path=fpath) if not file_info: self.log.warning( 'CANNOT readin fpath automatically.', extra='See RockPy naming conventions for proper naming scheme.') fname = RockPy3.get_fname_from_info(samplegroup='SG', sample_name=self.name, mtype=mtype, ftype=ftype, series=series) self.log.info('FILE NAME proposition:') self.log.info('-'.join('' for i in range(50))) self.log.info('%s' % fname) self.log.info('-'.join('' for i in range(50))) self.log.info('Please check for consistency and add samplegroup and mass, height, diameter') else: for check in ['mtype', 'ftype', 'series']: if check in file_info and locals()[check]: if not out[check] == file_info[check]: self.log.warning( '!!! INPUT != file_name: info does not match. Please check input, assuming filename correct') self.log.warning('!!! {} != {}'.format(locals()[check], file_info[check])) out.update(file_info) out.pop('name', None) return out
def measurement_infos(self): idict = {'fpath': self.fpath, 'ftype': self.ftype, 'idx': self.suffix, 'series': self.series} samples = RockPy3._to_tuple(self.samples) for i in samples: for j in self.mtypes: mtype = RockPy3.abbrev_to_classname(j) idict.update({'mtype': mtype, 'sample': i}) yield idict
def save(self, file_name=None, folder=None): if not file_name: import time file_name = '_'.join([time.strftime("%Y%m%d"), 'RockPy', self.name, '[{}]SG_[{}]S'.format(len(self._samplegroups), len(self.samples)), '.rpy']) if not folder: folder = RockPy3.core.file_operations.default_folder RockPy3.logger.info('SAVING RockPy data to {}'.format(os.path.join(folder, file_name))) RockPy3.save(self, folder=folder, file_name=file_name)
def plot(self, gname=None, sname=None, mtype=None, series=None, stype=None, sval=None, sval_range=None, mean=False, groupmean=False, invert=False, id=None, **plt_props): samples = self.get_sample( sname=sname, mtype=mtype, series=series, stype=stype, sval=sval, sval_range=sval_range, mean=mean, invert=invert, ) mlists = [[ m for m in s.get_measurement(mtype=mtype, series=series, stype=stype, sval=sval, sval_range=sval_range, mean=mean, invert=invert, id=id) if m._visuals ] for s in samples] columns = max(len(ml) for ml in mlists) if any(m for m in mlists): fig = RockPy3.Figure(title=self.name, columns=columns) for ml in mlists: for m in ml: vidx = deepcopy(fig._n_visuals) m.add_visuals(fig, **plt_props) v = fig.visuals[vidx][2] v.title = ' '.join((m.sobj.name, v.title)) if m.has_series(): stuples = '\n'.join('{}'.format(s) for s in m.series) with RockPy3.ignored(IndexError): fig.visuals[vidx][2].add_feature('generic_text', transform='ax', s=stuples, x=0.05, y=0.9) fig.show()
def measurement_block(self, block): sgroups, samples, mtypes, ftype = block.split('_') # names with , need to be replaced if not '(' in samples and ',' in samples: samples = samples.replace(',', '.') RockPy3.logger.warning('sample name %s contains \',\' will be replaced with \'.\'' % samples) self.sgroups, self.samples, self.mtypes, self.ftype = self.extract_tuple(sgroups), self.extract_tuple( samples), self.extract_tuple(mtypes), ftype self.mtypes = tuple(RockPy3.abbrev_to_classname(mtype) for mtype in RockPy3._to_tuple(self.mtypes)) self.ftype = RockPy3.abbrev_to_classname(ftype)
def generate_import_info(self, mtype=None, fpath=None, ftype=None, idx=None, series=None): """ First generate the file info. It is read from the fname, if possible. After that the mtype, ftype, fpath and idx will be overwritten, assuming that you gave a proper filname. """ out = { 'mtype': mtype, 'fpath': fpath, 'ftype': ftype, 'idx': idx, 'series': series } file_info = dict() with RockPy3.ignored(ValueError): file_info = RockPy3.core.file_operations.get_info_from_fname( path=fpath) if not file_info: self.log.warning( 'CANNOT readin fpath automatically.', extra='See RockPy naming conventions for proper naming scheme.' ) fname = RockPy3.get_fname_from_info(samplegroup='SG', sample_name=self.name, mtype=mtype, ftype=ftype, series=series) self.log.info('FILE NAME proposition:') self.log.info('-'.join('' for i in range(50))) self.log.info('%s' % fname) self.log.info('-'.join('' for i in range(50))) self.log.info( 'Please check for consistency and add samplegroup and mass, height, diameter' ) else: for check in ['mtype', 'ftype', 'series']: if check in file_info and locals()[check]: if not out[check] == file_info[check]: self.log.warning( '!!! INPUT != file_name: info does not match. Please check input, assuming filename correct' ) self.log.warning('!!! {} != {}'.format( locals()[check], file_info[check])) out.update(file_info) out.pop('name', None) return out
def save(self, file_name=None, folder=None): if not file_name: import time file_name = '_'.join([ time.strftime("%Y%m%d"), 'RockPy', self.name, '[{}]SG_[{}]S'.format(len(self._samplegroups), len(self.samples)), '.rpy' ]) if not folder: folder = RockPy3.core.file_operations.default_folder RockPy3.logger.info('SAVING RockPy data to {}'.format( os.path.join(folder, file_name))) RockPy3.save(self, folder=folder, file_name=file_name)
def measurement_infos(self): idict = { 'fpath': self.fpath, 'ftype': self.ftype, 'idx': self.suffix, 'series': self.series } samples = RockPy3._to_tuple(self.samples) for i in samples: for j in self.mtypes: mtype = RockPy3.abbrev_to_classname(j) idict.update({'mtype': mtype, 'sample': i}) yield idict
def test(): Sample = RockPy3.Sample(name='parameter_test', mass=10., mass_unit='kg', height=4.5, diameter=6., length_unit='mm') Sample = RockPy3.Sample(name='parameter_test', mass=10., mass_unit='kg', sample_shape='sphere', x_len=4.5, y_len=6., z_len=6., length_unit='mm')
def get_measurement_block(self): block = deepcopy(self.storage[0]) block[2] = [abbreviate_name(mtype).upper() for mtype in RockPy3._to_tuple(block[2]) if mtype] block[3] = abbreviate_name(block[3]).upper() if not all(block[1:]): raise ImportError('sname, mtype, ftype needed for minfo to be generated') return '_'.join((self.tuple2str(b) for b in block))
def measurement_block(self, block): sgroups, samples, mtypes, ftype = block.split('_') # names with , need to be replaced if not '(' in samples and ',' in samples: samples = samples.replace(',', '.') RockPy3.logger.warning( 'sample name %s contains \',\' will be replaced with \'.\'' % samples) self.sgroups, self.samples, self.mtypes, self.ftype = self.extract_tuple( sgroups), self.extract_tuple(samples), self.extract_tuple( mtypes), ftype self.mtypes = tuple( RockPy3.abbrev_to_classname(mtype) for mtype in RockPy3._to_tuple(self.mtypes)) self.ftype = RockPy3.abbrev_to_classname(ftype)
def add_mean_measurements(self, interpolate=True, substfunc='mean', mean_of_mean=False, reference=None, ref_dtype='mag', norm_dtypes='all', vval=None, norm_method='max', normalize_variable=False, dont_normalize=None, ignore_stypes=False): """ Creates mean measurements for all measurements and measurement types :param interpolate: :param substfunc: :param mean_of_mean: :param reference: :param ref_dtype: :param norm_dtypes: :param vval: :param norm_method: :param normalize_variable: :param dont_normalize: :param ignore_stypes: :return: """ ignore_stypes = RockPy3._to_tuple(ignore_stypes) mean_measurements = [] # separate the different mtypes for mtype in self.mtypes: # all measurements with that mtype measurements = self.get_measurement(mtype=mtype, mean=False) # use first measurement as template to check for series while measurements: m = measurements[0] if isinstance(m, RockPy3.Packages.Generic.Measurements.parameters.Parameter): break if ignore_stypes == 'all': mlist = measurements else: # get measurements with the same series mlist = [measurement for measurement in measurements if m.equal_series(measurement, ignore_stypes=ignore_stypes)] # remove the measurements from the measurements list so they don't get averaged twice measurements = [m for m in measurements if m not in mlist] if not mlist: self.log.debug('NO MORE measurements in mlist') break if self.mean_measurement_exists(mlist): self.log.warning('MEAN measurement already exists for these measurements:\n\t\t{}'.format(mlist)) mean_measurements.extend(self.mean_measurement_exists(mlist)) continue mean_measurements.append(self.create_mean_measurement(mlist=mlist, ignore_stypes=ignore_stypes, interpolate=interpolate, substfunc=substfunc, reference=reference, ref_dtype=ref_dtype, norm_dtypes=norm_dtypes, vval=vval, norm_method=norm_method, normalize_variable=normalize_variable, dont_normalize=dont_normalize)) return mean_measurements
def test(): file = '/Users/mike/Dropbox/experimental_data/COE/FeNiX/FeNiX_FeNi20-G-a-001-M02_COE_VSM#15[mg]_[]_[]#milling time_1_hrs;Ni_20_perc#STD003.001' s = RockPy3.Sample(name='test_sample') coe = s.add_simulation(mtype='backfield', bmax=1, noise=1) # print(coe.data['data']) plt.plot(coe.data['data']['field'].v, coe.data['data']['mag'].v) plt.show()
def plot(self, gname=None, sname=None, mtype=None, series=None, stype=None, sval=None, sval_range=None, mean=False, groupmean=False, invert=False, id=None, **plt_props): samples = self.get_sample(sname=sname, mtype=mtype, series=series, stype=stype, sval=sval, sval_range=sval_range, mean=mean, invert=invert, ) mlists = [[m for m in s.get_measurement(mtype=mtype, series=series, stype=stype, sval=sval, sval_range=sval_range, mean=mean, invert=invert, id=id) if m._visuals] for s in samples] columns = max(len(ml) for ml in mlists) if any(m for m in mlists): fig = RockPy3.Figure(title=self.name, columns=columns) for ml in mlists: for m in ml: vidx = deepcopy(fig._n_visuals) m.add_visuals(fig, **plt_props) v = fig.visuals[vidx][2] v.title = ' '.join((m.sobj.name, v.title)) if m.has_series(): stuples = '\n'.join('{}'.format(s) for s in m.series) with RockPy3.ignored(IndexError): fig.visuals[vidx][2].add_feature('generic_text', transform='ax', s=stuples, x=0.05, y=0.9) fig.show()
def plot(self, mtype=None, series=None, stype=None, sval=None, sval_range=None, mean=False, invert=False, id=None, result=None, **plt_props): mlist = self.get_measurement(mtype=mtype, series=series, stype=stype, sval=sval, sval_range=sval_range, mean=mean, invert=invert, id=id, result=result) # max_columns = max(len(m._visuals) for m in mlist) max_columns = sum(len(m._visuals) for m in mlist) if len(mlist) < 4 else max(len(m._visuals) for m in mlist) fig = RockPy3.Figure(title=self.name, columns=max_columns) for m in mlist: # get index of the first visual of this measurement vidx = deepcopy(fig._n_visuals) # only add the measurement if it has _visuals defined (e.g. no mass measurements) if m._visuals: m.add_visuals(fig, **plt_props) if m.has_series(): stuples = '\n'.join('{}'.format(s) for s in m.series) with RockPy3.ignored(IndexError): fig.visuals[vidx][2].add_feature('generic_text', transform='ax', s=stuples, x=0.05, y=0.9) fig.show()
def __init__(self, sobj, mtype='location', fpath=None, ftype='generic', lat=None, lon=None, alt=None, coordinate_system='wgs84', time=None, series=None, **options): super(LocationGeo, self).__init__(sobj=sobj, mtype=mtype, fpath=fpath, ftype=ftype, series=series, **options) if coordinate_system != 'wgs84': self.log.warning( 'support for geographic coordinate systems is not implemented yet.' ) self._data = { 'data': RockPy3.RockPyData( column_names=['variable', 'lat', 'lon', 'alt', 'time']) } self._data['data']['lat'] = lat self._data['data']['lon'] = lon self._data['data']['alt'] = alt self._data['data']['time'] = time
def format_jr6(ftype_data, sobj_name=None): data = RockPy3.Data(data=ftype_data.data, column_names=ftype_data.header) data.define_alias('m', ('x', 'y', 'z')) data = data.append_columns(column_names='mag', data=data.magnitude(key='m')) data = data.sort(key='variable') out = {'data': data} return out
def __init__(self, sobj, mtype='mass', fpath=None, ftype='generic', mass=1.0, mass_unit='kg', std=None, time=None, series=None, **options): super(Mass, self).__init__(sobj=sobj, fpath=fpath, ftype=ftype, series=series, **options) mass_conversion = convert2(mass_unit, 'kg', 'mass') if not mass_conversion: self.log.warning( 'mass unit << %s >> most likely not mass-compatible' % unit) self.log.error('CAN NOT create Measurement') self.has_data = False return self._data = { 'data': RockPy3.Data(column_names=['variable', 'mass', 'time', 'std_dev']) } self._data['data'][mtype] = mass * mass_conversion self._data['data']['time'] = time self._data['data']['std_dev'] = std
def import_folder(folder, name='study', study=None): if not study: study = RockPy3.Study(name=name) files = [i for i in os.listdir(folder) if not i == '.DS_Store' if not i.startswith('#')] samples = defaultdict(list) for i in files: d = RockPy3.get_info_from_fname(join(folder, i)) samples[d['name']].append(d) for s in samples: sgroup_name = samples[s][0]['samplegroup'] if not sgroup_name in study.samplegroup_names: sg = RockPy3.SampleGroup(name=samples[s][0]['samplegroup']) study.add_samplegroup(sg) sg = study[sgroup_name] if not s in study.sdict: smpl = RockPy3.Sample(**samples[s][0]) sg.add_samples(smpl) for m in samples[s]: measurement = smpl.add_measurement(**m) if 'ISindex' in m: initial = get_IS(m, samples[s]) measurement.set_initial_state(**initial) samples[s].remove(initial) if 'IS' in m and m['IS'] == True: continue return study
def __init__(self, sobj, mtype='bedding', fpath=None, ftype='generic', dip_dir=None, dip=None, time=None, series=None, **options): super(Bedding, self).__init__(sobj=sobj, mtype=mtype, fpath=fpath, ftype=ftype, series=series, **options) self._data = { 'data': RockPy3.RockPyData( column_names=['variable', 'dip_dir', 'dip', 'time']) } self._data['data']['dip_dir'] = dip_dir self._data['data']['dip'] = dip self._data['data']['time'] = time
def __init__(self, sobj, mtype='orientationgeo', fpath=None, ftype='generic', dec=None, inc=None, sun=None, time=None, series=None, **options): super(OrientationGeo, self).__init__(sobj=sobj, mtype=mtype, fpath=fpath, ftype=ftype, series=series, **options) self._data = { 'data': RockPy3.RockPyData( column_names=['variable', 'dec', 'inc', 'sun', 'time']) } self._data['data']['dec'] = dec self._data['data']['inc'] = inc self._data['data']['sun'] = sun self._data['data']['time'] = time
def plot(self, mtype=None, series=None, stype=None, sval=None, sval_range=None, mean=False, invert=False, id=None, result=None, **plt_props): mlist = self.get_measurement(mtype=mtype, series=series, stype=stype, sval=sval, sval_range=sval_range, mean=mean, invert=invert, id=id, result=result) # max_columns = max(len(m._visuals) for m in mlist) max_columns = sum(len(m._visuals) for m in mlist) if len(mlist) < 4 else max( len(m._visuals) for m in mlist) fig = RockPy3.Figure(title=self.name, columns=max_columns) for m in mlist: # get index of the first visual of this measurement vidx = deepcopy(fig._n_visuals) # only add the measurement if it has _visuals defined (e.g. no mass measurements) if m._visuals: m.add_visuals(fig, **plt_props) if m.has_series(): stuples = '\n'.join('{}'.format(s) for s in m.series) with RockPy3.ignored(IndexError): fig.visuals[vidx][2].add_feature('generic_text', transform='ax', s=stuples, x=0.05, y=0.9) fig.show()
def test_init_works(self): s = RockPy3.Sample(name='test_sample', comment='no comment') self.assertTrue(s.name == 'test_sample') self.assertTrue(s.comment == 'no comment') # check if a study was created self.assertTrue(s in s._study.samples.keys()) self.assertTrue(s in s._study.samplelist) self.assertTrue(s in s._study.samplenames)
def combine_measurements(self, others, remove_others=False, normalize_to_last=False): """ it combines several measurements into the specified one. e.g. a second cooling run at the end of the measurement Parameters ---------- others: list the measurements to appended to the self.data dictionary. The names are chosen according to the total number of heating and cooling runs remove_others: bool default: False if True the measurements are removed from the sample after it is combined with the measurement if False the measurements only combined normalize_to_last: default: False nomalizes the data to be combined to the last point of the last segment. So that different calibrations do not affect the measurement Returns ------- """ others = RockPy3._to_tuple(others) self.log.info('COMBINING << {} >> with {}'.format(self, others)) cool_idx = sum(1 for i in self.data if 'cool' in i) warm_idx = sum(1 for i in self.data if 'warm' in i) c, w = cool_idx - 1, warm_idx - 1 # old maximum indices nfactor = None last_segment = list(self.data.keys())[-1] if normalize_to_last: nfactor = self.data[last_segment]['mag'].v[-1] for m in others: m = deepcopy(m) if normalize_to_last: m.normalize('cooling00') m.normalize(norm_factor=1 / nfactor) for dtype in m.data: if 'cool' in dtype: self.data.update({'cooling%02i' % cool_idx: m.data[dtype]}) cool_idx += 1 if 'warming' in dtype: self.data.update({'warming%02i' % warm_idx: m.data[dtype]}) warm_idx += 1 if remove_others: self.log.info('REMOVING << {} >> from sample << {} >>'.format( self, self.sobj)) self.sobj.remove_measurement(mobj=m) return self
def sample_infos(self): sdict = dict(mass=self.mass, diameter=self.diameter, height=self.height, mass_unit=self.massunit, height_unit=self.heightunit, diameter_unit=self.diameterunit, samplegroup=self.sgroups) samples = RockPy3._to_tuple(self.samples) for i in samples: sdict.update({'name': i}) yield sdict
def setLatex(latex=True, font='Computer Modern Roman'): if latex == True: serif = ['Times', 'Palatino', 'New Century Schoolbook', 'Bookman', 'Computer Modern Roman'] sans_serif = ['Helvetica', 'Avant Garde', 'Computer Modern Sans serif'] if font in sans_serif: rc('font', **{'family': 'sans-serif', 'sans-serif': [font]}) if font in serif: rc('font', **{'family': 'serif', 'serif': [font]}) else: RockPy3.logger.warning('Font not recognized: use one of these:') RockPy3.logger.warning('\t{:2}\t{}'.format('', serif + sans_serif)) RockPy3.logger.info('USING LATEX %s: %s' % (latex, font)) rc('text', usetex=latex) RockPy3.set_fontsize(RockPy3.fontsize)
def results(self): results = RockPy3.Data(column_names='mID') for i, mid in enumerate(self._raw_results): if not 'mID' in self._raw_results[mid].column_names: aux = self._raw_results[mid].append_columns(column_names='mID', data=mid) else: aux = self._raw_results[mid] results = results.append_rows(aux) return results
def results(self): results = RockPy3.Data(column_names='mID') for s in sorted(self._raw_results): for mid in self._raw_results[s]: if not 'mID' in results.column_names: aux = self._raw_results[s][mid].append_columns( column_names='mID', data=mid) else: aux = self._raw_results[s][mid] results = results.append_rows(aux) return results
def get_measurement_block(self): block = deepcopy(self.storage[0]) block[2] = [ abbreviate_name(mtype).upper() for mtype in RockPy3._to_tuple(block[2]) if mtype ] block[3] = abbreviate_name(block[3]).upper() if not all(block[1:]): raise ImportError( 'sname, mtype, ftype needed for minfo to be generated') return '_'.join((self.tuple2str(b) for b in block))
def combine_measurements(self, others, remove_others = False, normalize_to_last=False): """ it combines several measurements into the specified one. e.g. a second cooling run at the end of the measurement Parameters ---------- others: list the measurements to appended to the self.data dictionary. The names are chosen according to the total number of heating and cooling runs remove_others: bool default: False if True the measurements are removed from the sample after it is combined with the measurement if False the measurements only combined normalize_to_last: default: False nomalizes the data to be combined to the last point of the last segment. So that different calibrations do not affect the measurement Returns ------- """ others = RockPy3._to_tuple(others) self.log.info('COMBINING << {} >> with {}'.format(self, others)) cool_idx = sum(1 for i in self.data if 'cool' in i) warm_idx = sum(1 for i in self.data if 'warm' in i) c,w = cool_idx-1, warm_idx-1 # old maximum indices nfactor = None last_segment = list(self.data.keys())[-1] if normalize_to_last: nfactor = self.data[last_segment]['mag'].v[-1] for m in others: m = deepcopy(m) if normalize_to_last: m.normalize('cooling00') m.normalize(norm_factor=1/nfactor) for dtype in m.data: if 'cool' in dtype: self.data.update({'cooling%02i'%cool_idx: m.data[dtype]}) cool_idx += 1 if 'warming' in dtype: self.data.update({'warming%02i'%warm_idx: m.data[dtype]}) warm_idx += 1 if remove_others: self.log.info('REMOVING << {} >> from sample << {} >>'.format(self, self.sobj)) self.sobj.remove_measurement(mobj=m) return self
def __init__(self, sobj, mtype, fpath=None, ftype='combined', height=None, diameter=None, sample_shape='cylinder', x_len=None, y_len=None, z_len=None, std=None, time=None, series=None, **options): super(Volume, self).__init__(sobj=sobj, fpath=fpath, ftype='combined', **options) self.sample_shape = sample_shape volume = np.nan if sample_shape == 'cylinder' and height and diameter: height_data = height.data['data']['height'].v[0] diameter_data = diameter.data['data']['diameter'].v[0] volume = self.cylinder(height_data, diameter_data) if x_len and y_len and z_len: if sample_shape != 'cube': # check if all three dimensions but wrong/unset sample_shape self.log.warning( 'sample_shape != cube \t but x_len, y_len, z_len provided -> assuming cube' ) sample_shape = 'cube' if sample_shape == 'cube': x = x_len.data['data']['length'].v[0] y = y_len.data['data']['length'].v[0] z = z_len.data['data']['length'].v[0] volume = self.cube(x, y, z) if diameter and not height: if sample_shape == 'sphere': diameter_data = diameter.data['data']['diameter'].v[0] volume = self.sphere(diameter_data) # store in RockPy Data object self._data = { 'data': RockPy3.Data(column_names=['volume', 'time', 'std_dev']) } self._data['data'][mtype] = volume self._data['data']['time'] = time self._data['data']['std_dev'] = std
def convert2float_or_str(item): """ Converts an item to a float or if not possible returns the str Parameters ---------- item: str Returns ------- str, float """ with RockPy3.ignored(ValueError): item = float(item) return item
def get_result(self, result, mtype=None, mean=False, base=False, return_mean=True, **calculation_parameter): """ A function that returns a list of all the result calculated from all measurements, that actually have the result Parameters ---------- result: str: the result to be calculated mtype: str or list of str if provided, only results from that mtype are calculated calculation_parameter: dict the calculation parameters to be used mean: bool if true the results are calculated for the mean measuremnets base: bool if true the results are calculated for the base measurements (needs to be a mean measurement) return_mean: bool if true a numpy mean is calculated and returned Returns ------- list of results Note ---- each of the results is a tuple of the actual value and the error if calculated """ # get all measurements that have the result # if mean use only mean measurements if mean: mlist = filter(lambda x: x.has_result(result=result), self.mean_measurements) if base: mlist = [m for mean in mlist for m in mean.base_measurements if m.has_result(result=result)] # if not use all non mean measurements else: mlist = filter(lambda x: x.has_result(result=result), self.measurements) if mtype: mtypes = RockPy3.to_list(mtype) mlist = filter(lambda x: x.mtype in mtypes, mlist) res = [getattr(m, 'result_' + result)(**calculation_parameter) for m in mlist] self.log.debug('Calculating result << {} >> for {} measurements'.format(result, len(res))) if return_mean: res = [(np.mean(res, axis=0)[0], np.std(res, axis=0)[0])] return res
def create_dummy_measurement(mtype, fpath=None, ftype=None, idx=0, mdata=None, sample=None): s = RockPy3.Sample(name='dummy_sample') m = s.add_measurement( mtype=mtype, fpath=fpath, ftype=ftype, # general idx=idx, mdata=mdata, ) if sample: m.sobj = sample return m
def mean_results(self): """ Gives a RockPyData object with all results from mean measurements Note: does not average the results Returns ------- RockPyData """ results = RockPy3.Data(column_names='mID') for i, mid in enumerate(self._raw_results): aux = self._raw_mean_results[mid].append_columns( column_names='mID', data=mid) results = results.append_rows(aux) return results
def remove_measurement(self, mtype=None, series=None, stype=None, sval=None, sval_range=None, mean=False, invert=False, id=None, mobj=None): """ Removes measurements from the sample Parameters ---------- mtype series stype sval sval_range mean invert id mobj Returns ------- """ if mobj: mlist = RockPy3._to_tuple(mobj) else: mlist = self.get_measurement(mtype=mtype, series=series, stype=stype, sval=sval, sval_range=sval_range, mean=mean, invert=invert, id=id) for m in mlist: if not mean: self.measurements = np.delete(self.measurements, m._idx) else: self.mean_measurements = np.delete(self.mean_measurements, m._idx)
def tuple2str(tup): """ takes a tuple and converts it to text, if more than one element, brackets are put around it """ if tup is None: return '' tup = RockPy3._to_tuple(tup) # if type(tup) == list: # if len(tup) == 1: # tup = tup[0] # else: # tup = tuple(tup) if len(tup) == 1: return str(tup[0]) else: return str(tup).replace('\'', ' ').replace(' ', '')
def add_visual(self, visual, name=None, data=None, plot_groupmean=None, plot_samplemean=None, plot_samplebase=None, plot_groupbase=None, plot_other=None, base_alpha=None, result_from_means=None, xlabel=None, ylabel=None, **visual_opt): """ adds a visual to the plot. This creates a new subplot. Parameters ---------- visual: list, str name of visual to add. """ # calculation_parameters, kwargs = RockPy3.core.utils.separate_calculation_parameter_from_kwargs(rpobj=self, **visual_opt) # convert visual to list visuals = RockPy3._to_tuple(visual) # for easy checking convert the names to lower case visuals = map(str.lower, visuals) for visual in visuals: # check if visual exists otherwise don't create it if visual in RockPy3.implemented_visuals: self.log.debug('VISUAL << %s > found' % visual) self.__log_implemented() if not name: name = visual n = self._n_visuals # create instance of visual by dynamically calling from implemented_visuals dictionary visual_obj = RockPy3.implemented_visuals[visual]( data=data, plt_index=n, fig=self, name=name, plot_groupmean=plot_groupmean, plot_groupbase=plot_groupbase, plot_samplemean=plot_samplemean, plot_samplebase=plot_samplebase, plot_other=plot_other, base_alpha=base_alpha, result_from_means=result_from_means, xlabel=xlabel, ylabel=ylabel, **visual_opt) self._visuals.append([name, visual, visual_obj]) self._n_visuals += 1 else: self.log.warning('VISUAL << %s >> not implemented yet' % visual) self.__log_implemented(ignore_once=True) return return visual_obj
def format_sushibar(ftype_data, sobj_name=None): if not sobj_name in ftype_data.raw_data: return data = np.nan_to_num(ftype_data.raw_data[sobj_name]) header = ftype_data.header # data = np.array([i for i in data if not np.isnan(i[header.index('par2')])]) # print(data) # GENERATE RockPy Data object for data data = RockPy3.Data(data=data, column_names=list(map(str.lower, header))) data.rename_column('m', 'mag') data.rename_column('meas. time', 'time') data.define_alias('m', ('x', 'y', 'z')) data.define_alias('variable', 'par1') out = { 'data': data, } return out
def import_file(self, fpath): """ Import function for a single file. :param fpath: :return: if file not readable it returns None """ try: info = RockPy3.get_info_from_fname(fpath) if not info['mtype'] in RockPy3.implemented_measurements: return if not info['sample_name'] in self._samples: s = self.add_sample(name=info['sample_name'], mass=info['mass'], mass_unit=info['mass_unit']) else: s = self._samples[info['sample_name']] m = s.add_measurement(fpath=info['fpath'], series=info['series']) return m except ValueError: return
def __add__(self, other): """ Adds two studies and returns a new study with all samples and measurements from both studies. The old studies will get a new samplegroup from the name of the study Parameters ---------- other: second samplegroup to be added Returns ------- RockPyStudy """ study = RockPy3.RockPyStudy() for s in self.samplelist: study.add_sample(sobj=s, samplegroup=self.name) for s in other.samplelist: study.add_sample(sobj=s, samplegroup=other.name) return study
def get_significant_digits(values): values = RockPy3._to_tuple(values) values = map(str, values) digits = [len(s.split(".")[-1]) for s in values] return digits if len(digits) > 1 else digits[0]
def __init__(self, fpath, sgroups=None, samples=None, mtypes=None, ftype=None, mass=None, height=None, diameter=None, massunit=None, lengthunit=None, heightunit=None, diameterunit=None, series=None, comment=None, folder=None, suffix=None, read_fpath=True, **kwargs): """ Parameters ---------- fpath sgroups samples mtypes ftype mass height diameter massunit lengthunit heightunit diameterunit series comment folder suffix read_fpath: bool if true the path will be read for info kwargs """ if 'mtype' in kwargs and not mtypes: mtypes = kwargs.pop('mtype') if 'sgroup' in kwargs and not sgroups: mtypes = kwargs.pop('sgroup') if 'sample' in kwargs and not samples: mtypes = kwargs.pop('sample') blocks = (self.measurement_block, self.sample_block, self.series_block, self.add_block, self.comment_block) additional = tuple() sgroups = RockPy3._to_tuple(sgroups) sgroups = tuple([sg if sg != 'None' else None for sg in sgroups]) if mtypes: mtypes = tuple(RockPy3.abbrev_to_classname(mtype) for mtype in RockPy3._to_tuple(mtypes)) if ftype: ftype = RockPy3.abbrev_to_classname(ftype) self.__dict__.update({i: None for i in ('sgroups', 'samples', 'mtypes', 'ftype', 'mass', 'height', 'diameter', 'massunit', 'lengthunit', 'heightunit', 'diameterunit', 'series', 'additional', 'comment', 'folder', 'suffix') }) self.fpath = fpath if read_fpath and fpath: # todo add check for if path is readable self.folder = os.path.dirname(fpath) f, self.suffix = os.path.splitext(os.path.basename(fpath)) self.suffix = self.suffix.strip('.') splits = f.split('#') # check if RockPy compatible e.g. first part must be len(4) if not len(splits[0]) == 4: pass for i, block in enumerate(blocks[:len(splits)]): if splits[i]: try: block(splits[i]) except (ValueError,): pass for i in ('sgroups', 'samples', 'mtypes', 'ftype', 'mass', 'height', 'diameter', 'massunit', 'lengthunit', 'heightunit', 'diameterunit', 'series', 'additional', 'comment', 'folder'): if locals()[i]: if isinstance(locals()[i], (tuple, list, set)): if not all(locals()[i]): continue setattr(self, i, locals()[i]) if self.additional is None: self.additional = '' if kwargs: for k, v in kwargs.items(): if v: print(k, v, self.additional) self.additional += '{}:{}'.format(k, v) if suffix: self.suffix = suffix if type(self.suffix) == int: self.suffix = '%03i' % self.suffix if not self.suffix: self.suffix = '000' if not self.sgroups: self.sgroups = None self.storage = [[self.sgroups, self.samples, self.mtypes, self.ftype], [[self.mass, self.massunit], [self.height, self.heightunit], [self.diameter, self.diameterunit], ], self.series, (self.additional,), self.comment]
def load(self, file_name=None, folder=None): return RockPy3.load(folder=folder, file_name=file_name)
def get_measurement(self, mtype=None, series=None, stype=None, sval=None, sval_range=None, mean=False, invert=False, id=None, result=None ): """ Returns a list of measurements of type = mtypes Parameters ---------- mtypes: list, str mtypes to be returned series: list(tuple) list of tuples, to search for several sepcific series. e.g. [('mtime',4),('gc',2)] will only return mesurements that fulfill both criteria. stypes: list, str series type sval_range: list, str series range e.g. sval_range = [0,2] will give all from 0 to 2 including 0,2 also '<2', '<=2', '>2', and '>=2' are allowed. svals: float series value to be searched for. caution: will be overwritten when sval_range is given invert: if invert true it returns only measurements that do not meet criteria sval_range: can be used to look up measurements within a certain range. if only one value is given, it is assumed to be an upper limit and the range is set to [0, sval_range] mean: bool id: list(int) search for given measurement id Returns ------- if no arguments are passed all sample.measurements list of RockPy.Measurements that meet search criteria or if invert is True, do not meet criteria. [] if none are found Note ---- there is no connection between stype and sval. This may cause problems. I you have measurements with M1: [pressure, 0.0, GPa], [temperature, 100.0, C] M2: [pressure, 1.0, GPa], [temperature, 100.0, C] and you search for stypes=['pressure','temperature'], svals=[0,100]. It will return both M1 and M2 because both M1 and M2 have [temperature, 100.0, C]. """ if mean: mlist = self.mean_measurements else: mlist = self.measurements if id is not None: id = RockPy3._to_tuple(id) mlist = filter(lambda x: x.id in id, mlist) return list(mlist) if mtype: mtype = RockPy3._to_tuple(mtype) mtype = tuple(RockPy3.abbrev_to_classname(mt) for mt in mtype) mlist = filter(lambda x: x.mtype in mtype, mlist) if stype: mlist = filter(lambda x: x.has_stype(stype=stype, method='any'), mlist) if sval_range is not None: sval_range = self._convert_sval_range(sval_range=sval_range, mean=mean) if not sval: sval = sval_range else: sval = RockPy3._to_tuple() sval += RockPy3._to_tuple(sval_range) if sval is not None: mlist = filter(lambda x: x.has_sval(sval=sval, method='any'), mlist) if series: series = RockPy3.core.utils.tuple2list_of_tuples(series) mlist = (x for x in mlist if x.has_series(series=series, method='all')) if result: mlist = filter(lambda x: x.has_result(result=result), mlist) if invert: if mean: mlist = filter(lambda x: x not in mlist, self.mean_measurements) else: mlist = filter(lambda x: x not in mlist, self.measurements) return list(mlist)
def import_folder(self, folder, sname=None, sgroup=None, mtype=None, automatic_results = True, ): # todo specify samples, mtypes and series for selective import of folder """ imports all files in the specified folder Parameters ---------- folder gname Returns ------- """ files = [os.path.join(folder, i) for i in os.listdir(folder) if not i.startswith('#') if not i.startswith(".") if not i.endswith("pynb") if not i.endswith("log") if not os.path.isdir(os.path.join(folder, i)) ] measurements = [] start = time.clock() minfos = (RockPy3.core.file_operations.minfo(f) for f in files) minfos = (minfo for minfo in minfos if minfo.is_readable()) if mtype: mtype = RockPy3._to_tuple(mtype) mtype = [RockPy3.abbrev_to_classname(mt) for mt in mtype] for mi in minfos: RockPy3.logger.info('importing {}'.format(mi.fpath)) # dont import samples that are not in the measurement info if sname: sname = RockPy3._to_tuple(sname) if not any(s in mi.samples for s in sname): continue # dont import samples that are not included in a certain samplegroup in the measurement info if sgroup: sgroup = RockPy3._to_tuple(sgroup) if not any(sg in mi.sgroups for sg in sgroup): continue # check if the filename has not been imported, yet if mi.fpath in self.imported_files: continue else: self.imported_files.append(mi.fpath) for sinfo in mi.sample_infos: s = self.add_sample(warnings=False, **sinfo) for minfo in mi.measurement_infos: if mtype and minfo['mtype'].lower() not in mtype: continue if minfo['sample'] == sinfo['name']: measurements.append(s.add_measurement(**minfo)) end = time.clock() print('IMPORT generated {} measurements: finished in {:.02e}s'.format(len(measurements), end - start)) return measurements
def show(self, xlim=None, ylim=None, equal_lims=False, center_lims=False, save_path=None, pad=0.4, w_pad=0.5, h_pad=1.0, file_name=None, format='pdf', legend=True, sort_labels = True, return_figure=False, append_to_filename = '', ): """ calls all visuals Raises ------ TypeError if no visuals have been added """ self._fig, self.axes = self._create_fig(xsize=self.xsize, ysize=self.ysize) if not self.visuals: self.log.error('NO VISUALS ADDED! Please add any of the followig visuals:') for visual in sorted(Visual.implemented_visuals()): self.log.info('\t %s' % visual) raise TypeError('add a visual') # actual plotting of the visuals self.plt_all() for name, type, visual in self._visuals: if visual.xlim: visual.ax.set_xlim(visual.xlim) if visual.ylim: visual.ax.set_ylim(visual.ylim) if visual.xscale: visual.ax.set_xscale(visual.xscale) if visual.yscale: visual.ax.set_yscale(visual.yscale) # prevent scientific notation for x axis # if type in ('thermocurve', ): visual.ax.ticklabel_format(style='plain', axis='x') # else: # xlim = visual.ax.get_xlim() # ylim = visual.ax.get_ylim() if xlim == 'equal' or ylim == 'equal' or equal_lims: if equal_lims: xlim, ylim = self.get_xylims() if center_lims: xl = max(np.abs(xlim)) yl = max(np.abs(ylim)) xlim = [-xl, xl] ylim = [-yl, yl] # cycle through visuals to set for name, type, visual in self._visuals: if xlim == 'equal' or equal_lims: visual.ax.set_xlim(xlim) if ylim == 'equal' or equal_lims: visual.ax.set_ylim(ylim) # prepare legends for individual visuals for name, type, visual in self._visuals: # check if the legend should be drawn accoring to the visual.legend dictionary if not visual.show_legend(): continue if not legend: break handles, labels = visual.ax.get_legend_handles_labels() if not all(i for i in (handles, labels)): continue # sorting of labels if sort_labels: labels, handles = zip(*sorted(zip(labels, handles), key=lambda t: t[0])) visual.ax.legend(handles, labels, **visual.legend_options) # check if two entries and each is float or int if xlim: if len(xlim) == 2 and any(isinstance(i, (float, int)) for i in xlim): for name, type, visual in self._visuals: visual.ax.set_xlim(xlim) # check if two entries and each is float or int if ylim: if len(ylim) == 2 and any(isinstance(i, (float, int)) for i in ylim): for name, type, visual in self._visuals: visual.ax.set_ylim(ylim) self._fig.set_tight_layout(tight={'pad': pad, 'w_pad': w_pad, 'h_pad': h_pad}) if self.title: self._fig.suptitle(self.title, fontsize=20) # (left, bottom, right, top) in the normalized figure coordinate that the whole subplots area # (including labels) will fit into self._fig.set_tight_layout(tight={'rect': (0, 0, 1, 0.95)}) if return_figure: return self._fig if save_path: if save_path.lower() == 'desktop': if not file_name: file_name = os.path.basename(inspect.stack()[-1][1]) file_name += append_to_filename save_path = os.path.join(os.path.expanduser('~'), 'Desktop', file_name) else: save_path = os.path.join(save_path, file_name) if not format in file_name: save_path +='.' save_path += format plt.savefig(save_path) else: with RockPy3.ignored(AttributeError): self._fig.canvas.manager.window.raise_() plt.show() plt.close('all')
def create_mean_measurement(self, mtype=None, stype=None, sval=None, sval_range=None, series=None, invert=False, mlist=None, interpolate=False, substfunc='mean', ignore_stypes=False, recalc_mag=False, reference=None, ref_dtype='mag', norm_dtypes='all', vval=None, norm_method='max', normalize_variable=False, dont_normalize=None, create_only=False, color=None, marker=None, linestyle=None): """ takes a list of measurements and creates a mean measurement out of all measurements data Parameters ---------- mlist: list list of measurements to be combined into a new measurement ignore_stypes: list stypes to be ignored. mtype: str mtype to be returned series: list(tuple) list of tuples, to search for several specific series. e.g. [('mtime',4),('gc',2)] will only return measurements that fulfill both criteria. Supersedes stype, sval and sval_range. Returns only measurements that meet series exactly! stype: str series type sval_range: list, str series range e.g. sval_range = [0,2] will give all from 0 to 2 including 0,2 also '<2', '<=2', '>2', and '>=2' are allowed. sval: float series value to be searched for. caution: will be overwritten when sval_range is given invert: if invert true it returns only measurements that do not meet above criteria sval_range: can be used to look up measurements within a certain range. if only one value is given, it is assumed to be an upper limit and the range is set to [0, sval_range] interpolate: bool substfunc: str recalc_mag: bool reference: str ref_dtype: str norm_dtypes: list vval: float norm_method: str normalize_variable: bool dont_normalize: list create_only: bool will not add measurement to the mean_measurements list or mean_mdict color: str color for new mean measurement marker: str marker for new mean measurement linestyle: str linestyle for new mean measurement Returns ------- RockPy.Measurement The mean measurement that fits to the specified lookup """ # check for mtype if no mtype specified if mlist and not mtype: mtype = list(set(m.mtype for m in mlist)) if len(mtype) != 1: raise TypeError('NO mtype specified. List of measurements may only contain one mtype') else: mtype = mtype[0] if not mtype and not mlist: raise TypeError('NO mtype specified. Please specify mtype') if not mlist: mlist = self.get_measurement(mtype=mtype, series=series, stype=stype, sval=sval, sval_range=sval_range, mean=False, invert=invert) # normalze all measurements if reference: mlist = [m.normalize(reference=reference, ref_dtype=ref_dtype, norm_dtypes=norm_dtypes, vval=vval, norm_method=norm_method, normalize_variable=normalize_variable, dont_normalize=dont_normalize) for m in mlist] if not mlist: self.log.warning('NO measurement found >> %s, %s, %f >>' % (mtype, stype, sval)) return None mtype = RockPy3.abbrev_to_classname(mtype) if len(mlist) == 1: self.log.warning('Only one measurement found returning measurement') # create mean measurement from a list of measurements mean = RockPy3.implemented_measurements[mtype].from_measurements_create_mean( sobj=self, mlist=mlist, interpolate=interpolate, recalc_mag=recalc_mag, substfunc=substfunc, ignore_stypes=ignore_stypes, color=color, marker=marker, linestyle=linestyle) mean.calc_all(force_recalc=True) # add to self.mean_measurements if specified if not create_only: self.mean_measurements.append(mean) return mean
# self.ylabel = '' def set_colorscheme(scheme): RockPy3.colorscheme = RockPy3.core.utils.colorscheme(scheme) return RockPy3.core.utils.colorscheme(scheme) if __name__ == '__main__': # pass # S = RockPy3.Study # s = S.add_sample(name='S1') # s.add_simulation(mtype='hysteresis') # # fig = RockPy3.Figure() # v = fig.add_visual(visual='hysteresis', data=S) # print(v.data) # for f in v.features: # print(v.features[f]['data']) # # fig.show() # print(RockPy3.implemented_measurements.keys()) # print(RockPy3.implemented_visuals.keys()) RockPy3.set_fontsize(fontsize=14) RockPy3.core.utils.setLatex(True) fig = RockPy3.Figure() v = fig.add_visual('hysteresis') v.enumerate() fig.show()
def get_info_from_fname(path=None): # todo redundant """ extracts the file information out of the filename Parameters ---------- path: complete path, with folder/fname. Will be split into the two Raises ------ KeyError if ftype or mtype not in RockPy3.mtype_ftype_abbreviations_inversed """ # change add_measurement accordingly folder = os.path.split(path)[0] fname = os.path.split(path)[1] fpath = fname index = fname.split('.')[-1] fname = fname.split('.')[:-1][0] rest = fname.split('#') samplegroup, sample_name, mtype, ftype = rest[0].split('_') sample_info = [i.strip(']').split('[') for i in rest[1].split('_')] mass, diameter, height = sample_info if rest[2]: series = rest[2] # separate the series (combined with ;) series = series.split(';') # change , -> . series = [i.replace(',', '.').split('_') for i in series] series = [(i[0], float(i[1]), i[2]) for i in series] else: series = None try: STD = [int(i.lower().strip('std')) for i in rest if 'std' in i.lower()][0] except IndexError: STD = None try: options = [i.split('_') for i in rest[4].split('.')[0].split(';')] options = {i[0]: i[1] for i in options} except IndexError: options = None # convert mass to float with RockPy3.ignored(ValueError): mass[0] = mass[0].replace(',', '.') mass[0] = float(mass[0]) # convert height to float with RockPy3.ignored(ValueError): diameter[0] = diameter[0].replace(',', '.') diameter[0] = float(diameter[0]) # convert diameter to float with RockPy3.ignored(ValueError): height[0] = height[0].replace(',', '.') height[0] = float(height[0]) if diameter[1] and height[1]: if diameter[1] != height[1]: diameter[0] = diameter[0] * getattr(ureg, height[1]).to( getattr(ureg, diameter[1])).magnitude mtype = mtype.lower() # convert to upper for ease of checking ftype = ftype.lower() # convert to upper for ease of checking try: mtype = RockPy3.mtype_ftype_abbreviations_inversed[mtype] except KeyError: raise KeyError('%s not implemented yet' % mtype) return try: ftype = RockPy3.mtype_ftype_abbreviations_inversed[ftype] except KeyError: raise KeyError('%s not implemented yet' % mtype) return # replace 'xml' ending with idx: if index == 'xml': index = 1 out = { 'samplegroup': samplegroup, 'sample_name': sample_name, # not needed since 3.5 rewrite 'mtype': mtype, 'ftype': ftype, 'fpath': join(folder, fpath), 'series': series, # 'std': STD, 'idx': int(index), 'mass': None, 'mass_unit': None, 'diameter': None, 'height': None, 'length_unit': None, } # if mtype == 'mass': if mass[0]: out.update({'mass': mass[0], 'mass_unit': mass[1]}) # if mtype == 'diameter': if diameter[0]: out.update({'diameter': diameter[0], 'length_unit': diameter[1]}) # if mtype == 'height': if height[0]: out.update({'height': height[0], 'length_unit': diameter[1]}) if options: out.update(options) return out
def QuickFig(data, visuals, **kwargs): f = RockPy3.Figure(data=data) visuals = RockPy3._to_tuple(visuals) for v in visuals: f.add_visual(visual=v) f.show(**kwargs)