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 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 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 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 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 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 get_measurement_infos(self, raw_info_header): """ takes the raw data and creates a dictionary with the measurement infos """ def separate(line): if line: if any(j in line for j in ('+', '-', '\"')): splitter = 30 else: splitter = 31 if line[splitter + 1:].rstrip(): out = (line[:splitter].rstrip().lower(), self.convert2float_or_str(line[splitter + 1:].strip())) if out[1] == 'Yes': return (line[:splitter].rstrip().lower(), True) if out[1] == 'No': return (line[:splitter].rstrip().lower(), False) else: return out t = [i.split(' ') for i in raw_info_header] t = [[j for j in i if j] for i in t] t = [tuple(i) for i in t if len(i) > 1] data = {i[0].lower(): i[1] for i in t} for k, v in data.items(): with RockPy3.ignored(ValueError): data[k] = float(v) if v == 'Yes': data[k] = True if v == 'No': data[k] = False return dict(data)
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 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 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 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')