diff --git a/RockPy/core/file_io.py b/RockPy/core/file_io.py index 9e49bc5..278d7d1 100644 --- a/RockPy/core/file_io.py +++ b/RockPy/core/file_io.py @@ -22,7 +22,10 @@ def read_abbreviations(): # create the mtype:abbreviation dict get_abbreviations = [tuple(i.rstrip().split(":")) for i in get_abbreviations if i.rstrip() if not i.startswith("#")] - get_abbreviations = dict((i[0], [j.lstrip() for j in i[1].split(",")]) for i in get_abbreviations) + get_abbreviations = { + i[0]: [j.lstrip() for j in i[1].split(",")] for i in get_abbreviations + } + # create inverse abbrev:mtype/ftype get_mtype_ftype = {i: k for k in get_abbreviations for i in get_abbreviations[k]} @@ -141,7 +144,7 @@ def extract_add_dialect_block(cls, block): * *list of additionals* * *str, dialect* """ - if not 'dialect' in block: + if 'dialect' not in block: return block, '' parts = block.split(",") dialect = [p for p in parts if "dialect" in p][0].replace("dialect=", "") @@ -161,8 +164,8 @@ def from_folder(cls, folder, filter=None): RockPy.minfo: """ - if filter == None: - filter = dict() + if filter is None: + filter = {} dfiles = [os.path.join(folder, i) for i in os.listdir(folder) if not i.startswith("#")] @@ -175,12 +178,7 @@ def from_folder(cls, folder, filter=None): cls.log().debug("cant read file: %s" % os.path.basename(f)) continue - if minfo is None: - minfo = finfo - else: - # append subsequent minfos - minfo = minfo + finfo - + minfo = finfo if minfo is None else minfo + finfo return minfo @classmethod @@ -204,7 +202,7 @@ def from_file(cls, fpath): splits = filename.split("#") # check if RockPy compatible e.g. first part must be len(4) - if not len(splits[0].split("_")) == 4: + if len(splits[0].split("_")) != 4: cls.log().debug('filename << %s >> does not conform to the RockPy file naming scheme. At least 3 elements (sname, mtype, ftype) ' 'have to be given, separated by \'_\' '%filename) return @@ -234,17 +232,25 @@ def from_file(cls, fpath): additional, dialect = (None, None) # comment - if len(splits) > 4: - comment = splits[4] - else: - comment = None - - return cls(snames=snames, mtypes=mtypes, ftype=ftype, fpath=fpath, sgroups=sgroups, - dialect=dialect, - mass=mass, massunit=massunit if massunit else "kg", - height=height, heightunit=heightunit if heightunit else "m", - diameter=diameter, diameterunit=diameterunit if diameterunit else "m", - series=series, comment=comment, additional=additional, suffix=suffix) + comment = splits[4] if len(splits) > 4 else None + return cls( + snames=snames, + mtypes=mtypes, + ftype=ftype, + fpath=fpath, + sgroups=sgroups, + dialect=dialect, + mass=mass, + massunit=massunit or "kg", + height=height, + heightunit=heightunit or "m", + diameter=diameter, + diameterunit=diameterunit or "m", + series=series, + comment=comment, + additional=additional, + suffix=suffix, + ) @classmethod def from_dict(cls, **kwargs): @@ -341,10 +347,8 @@ def __init__( else: self.lengthunit = RockPy.core.utils.to_list(lengthunit) - if series is not None: - # if only one series and not three - if len(series) == 3 and not len(series[0]) == 3: - series = tuple2list_of_tuples(to_tuple(series)) + if series is not None and len(series) == 3 and len(series[0]) != 3: + series = tuple2list_of_tuples(to_tuple(series)) self.series = [series] @@ -372,7 +376,7 @@ def get_measurement_block(cls, sgroups, snames, mtypes, ftype): """ mtypes = (RockPy.classname_to_abbrev[i][0] for i in mtypes) block = [sgroups, snames, mtypes, ftype] - if not all(i for i in block): + if not all(block): raise ImportError("sname, mtype, ftype needed for minfo to be generated") return "_".join((RockPy.core.utils.tuple2str(b) for b in block)) @@ -419,11 +423,8 @@ def get_sample_block(cls, for i in range(n): b = block[i] - if not all(j for j in b): - if i == 0: - aux = "XXmg" - else: - aux = "XXmm" + if not all(b): + aux = "XXmg" if i == 0 else "XXmm" else: out.append("".join(map(str, b))) return ",".join(out) @@ -475,7 +476,7 @@ def new_filenames(self): blocks = [measurement_block, sample_block, series_block, add_block] # work through blocks backwards throw out blocks that are empty, # if ones is not empty, stop. - for i, block in enumerate(blocks[::-1]): + for block in blocks[::-1]: if not block: blocks.pop() else: @@ -509,9 +510,9 @@ def getImportHelper(self, snames=None, mtypes=None): mtypes = RockPy.to_tuple(mtypes) for ih in self._gen_dicts: - if all(i for i in snames) and ih["snames"] not in snames: + if all(snames) and ih["snames"] not in snames: continue - if all(i for i in mtypes) and ih["mtypes"] not in mtypes: + if all(mtypes) and ih["mtypes"] not in mtypes: continue a = self.__class__.from_dict(**ih) yield a @@ -583,9 +584,8 @@ def return_file_infos(self): Returns: dict: """ - out = {} + out = {'snames': list_or_item(self.snames[0])} - out['snames'] = list_or_item(self.snames[0]) out['sgroups'] = list_or_item(self.sgroups[0]) out['mtypes'] = list_or_item(self.mtypes[0]) out['ftype'] = self.ftype[0] diff --git a/RockPy/core/ftype.py b/RockPy/core/ftype.py index 092817f..cd7e7d7 100644 --- a/RockPy/core/ftype.py +++ b/RockPy/core/ftype.py @@ -166,13 +166,12 @@ def _has_specimen(self, specimen): bool: `True` if file contains `specimen` otherwise `False` """ - if specimen not in self.data['specimen'].values: - Ftype.log().error('CANNOT IMPORT -- sobj_name not in ftype_data specimen list.') - Ftype.log().error('wrong sample name?') - Ftype.log().error('These samples exist: %s' % set(self.data['specimens'])) - return False - else: + if specimen in self.data['specimen'].values: return True + Ftype.log().error('CANNOT IMPORT -- sobj_name not in ftype_data specimen list.') + Ftype.log().error('wrong sample name?') + Ftype.log().error('These samples exist: %s' % set(self.data['specimens'])) + return False def to_si_units(self): """ converts each numeric column in self.data to SI internal_units TODO: write to out_units @@ -180,7 +179,7 @@ def to_si_units(self): Q_ = ureg.Quantity for col in self.data.columns: if col in self.in_units: - if not col in self.units: + if col not in self.units: self.log().warning( 'Unit of data column << {} >> has no internal unit equivalent. The input unit is << {:P} >>.'.format( col, self.in_units[col])) @@ -296,7 +295,6 @@ def is_implemented(ftype): """ if ftype in RockPy.implemented_ftypes: return True - else: - RockPy.log.error( - f'Ftype << {ftype} >> is not implemented. Check RockPy.implemented_ftypes for which ftypes are.') - return False + RockPy.log.error( + f'Ftype << {ftype} >> is not implemented. Check RockPy.implemented_ftypes for which ftypes are.') + return False diff --git a/RockPy/core/measurement.py b/RockPy/core/measurement.py index e6268cc..13be4dc 100644 --- a/RockPy/core/measurement.py +++ b/RockPy/core/measurement.py @@ -338,7 +338,7 @@ def __init__(self, sobj=None, for s in series: self.add_series(*s) - self.idx = idx if idx else self.__idx # external index e.g. 3rd hys measurement of sample 1 + self.idx = idx or self.__idx # add the data to the clsdata self.append_to_clsdata(mdata) @@ -424,7 +424,7 @@ def get_recipes(self, res): :return: """ result = self._results[res] - recipes = [r for r in result._recipes()] + recipes = list(result._recipes()) return set(recipes) def has_result(self, result): @@ -440,7 +440,7 @@ def has_result(self, result): bool """ - return True if result in self._results.keys() else False + return result in self._results.keys() def __lt__(self, other): """ @@ -460,10 +460,7 @@ def __lt__(self, other): pass def __repr__(self): - if self.is_mean: - add = 'mean_' - else: - add = '' + add = 'mean_' if self.is_mean else '' return '<>'.format(self.sobj.name, add, self.mtype, '[' + ';'.join(['{},{}({})'.format(i[0], i[1], i[2]) for i in self.get_series()]) + ']' if self.has_series() else '', @@ -534,13 +531,13 @@ def __sub__(self, other): first.data[dtype] = first.data[dtype].sort() return self.sobj.add_measurement(mtype=first.mtype, mdata=first.data) - def reset_data(self): # todo rewrite new data pandas + def reset_data(self): # todo rewrite new data pandas """ Resets all data back to the original state. Deepcopies _raw_data back to _data and resets correction """ midx = self.__class__._mids.index(self.mid) - self.log().debug(f'Resetting the data') + self.log().debug('Resetting the data') self.clsdata[midx] = self._clsdata[midx] # create _correction if not exists @@ -635,7 +632,7 @@ def has_initial_state(self): """ checks if there is an initial state """ - return True if self.initial_state else False + return bool(self.initial_state) @property def stypes(self): @@ -748,9 +745,8 @@ def series(self): """ if self._series: return self._series - else: - series = (None, np.nan, None) # no series - return [series] + series = (None, np.nan, None) # no series + return [series] def has_sval(self, sval=None, method='all'): """ @@ -775,16 +771,15 @@ def has_sval(self, sval=None, method='all'): if not self._series: return False - if sval is not None: - sval = to_tuple(sval) - if method == 'all': - return True if all(i in self.svals for i in sval) else False - if method == 'any': - return True if any(i in self.svals for i in sval) else False - if method == 'none': - return True if not any(i in self.svals for i in sval) else False - else: - return True if not self.svals else False + if sval is None: + return not self.svals + sval = to_tuple(sval) + if method == 'all': + return all(i in self.svals for i in sval) + if method == 'any': + return any(i in self.svals for i in sval) + if method == 'none': + return all(i not in self.svals for i in sval) def has_stype(self, stype=None, method='all'): """ @@ -809,16 +804,15 @@ def has_stype(self, stype=None, method='all'): if not self._series: return False - if stype is not None: - stype = to_tuple(stype) - if method == 'all': - return True if all(i in self.stypes for i in stype) else False - if method == 'any': - return True if any(i in self.stypes for i in stype) else False - if method == 'none': - return True if not any(i in self.stypes for i in stype) else False - else: - return True if not self.stypes else False + if stype is None: + return not self.stypes + stype = to_tuple(stype) + if method == 'all': + return all(i in self.stypes for i in stype) + if method == 'any': + return any(i in self.stypes for i in stype) + if method == 'none': + return all(i not in self.stypes for i in stype) def has_series(self, series=None, method='all'): ''' @@ -840,16 +834,15 @@ def has_series(self, series=None, method='all'): returns true if Nothing is passes ''' - if series is not None: - series = tuple2list_of_tuples(series) - if method == 'all': - return True if all(i in self.series for i in series) else False - if method == 'any': - return True if any(i in self.series for i in series) else False - if method == 'none': - return True if not any(i in self.svals for i in series) else False - else: - return True if self._series else False + if series is None: + return bool(self._series) + series = tuple2list_of_tuples(series) + if method == 'all': + return all(i in self.series for i in series) + if method == 'any': + return any(i in self.series for i in series) + if method == 'none': + return all(i not in self.svals for i in series) def add_series(self, stype, sval, sunit=None): # todo add (stype,sval,sunit) type calling # todo change to set_series with stype, sval, suit, series @@ -959,13 +952,9 @@ def equal_series(self, other, ignore_stypes=()): ignore_stypes = to_tuple(ignore_stypes) ignore_stypes = [st.lower() for st in ignore_stypes if type(st) == str] selfseries = (s for s in self.series if not s[0] in ignore_stypes) - otherseries = (s for s in other.series if not s[0] in ignore_stypes) - - if all(i in otherseries for i in selfseries): - return True + otherseries = (s for s in other.series if s[0] not in ignore_stypes) - else: - return False + return all(i in otherseries for i in selfseries) # todo test normalize functions #################################################################################################################### @@ -975,7 +964,7 @@ def normalize(self, reference='data', ref_dtype='mag', norm_dtypes='all', vval=None, norm_method='max', norm_factor=None, result=None, normalize_variable=False, dont_normalize=('temperature', 'field'), - norm_initial_state=True, **options): # todo check if works + norm_initial_state=True, **options): # todo check if works """ normalizes all available data to reference value, using norm_method @@ -1033,17 +1022,17 @@ def normalize(self, for dtype, dtype_data in self.data.items(): # cycling through all dtypes in data if dtype_data: if 'all' in norm_dtypes: # if all, all non stype data will be normalized - norm_dtypes = [i for i in dtype_data.column_names if not 'stype' in i] + norm_dtypes = [i for i in dtype_data.column_names if 'stype' not in i] ### DO not normalize: # variable if not normalize_variable: variable = dtype_data.column_names[dtype_data.column_dict['variable'][0]] - norm_dtypes = [i for i in norm_dtypes if not i == variable] + norm_dtypes = [i for i in norm_dtypes if i != variable] if dont_normalize: dont_normalize = RockPy.core.utils.to_tuple(dont_normalize) - norm_dtypes = [i for i in norm_dtypes if not i in dont_normalize] + norm_dtypes = [i for i in norm_dtypes if i not in dont_normalize] for ntype in norm_dtypes: # else use norm_dtypes specified try: @@ -1116,7 +1105,7 @@ def _get_norm_factor(self, reference, rtype, vval, norm_method, result, **calcul return 1 return m.data['data']['mass'].v[0] - if isinstance(reference, float) or isinstance(reference, int): + if isinstance(reference, (float, int)): norm_factor = float(reference) elif result: @@ -1126,21 +1115,19 @@ def _get_norm_factor(self, reference, rtype, vval, norm_method, result, **calcul return norm_factor def _norm_method(self, norm_method, vval, rtype, data): - methods = {'max': max, - 'min': min, - } - if not vval: - if not norm_method in methods: + methods = {'max': max, + 'min': min, + } + + if norm_method not in methods: raise NotImplemented('NORMALIZATION METHOD << %s >>' % norm_method) - return else: return methods[norm_method](data[rtype].v) if vval: idx = np.argmin(abs(data['variable'].v - vval)) - out = data.filter_idx([idx])[rtype].v[0] - return out + return data.filter_idx([idx])[rtype].v[0] def get_mtype_prior_to(self, mtype): """ diff --git a/RockPy/core/result.py b/RockPy/core/result.py index 9d0016c..031c9a7 100644 --- a/RockPy/core/result.py +++ b/RockPy/core/result.py @@ -72,7 +72,7 @@ def get_stack(self, stack=None): for dep_res in self._dependencies: stack = dep_res.get_stack(stack) - if not self in stack: + if self not in stack: stack.append(self) else: return stack @@ -141,13 +141,12 @@ def __call__(self, recipe=None, **parameters): signature = inspect.signature(result._recipes()[recipe]).parameters for p in signature: - if p == 'check': - continue - elif p == 'self': - continue - elif p in parameters: - continue - elif p == 'unused_params': + if ( + p == 'check' + or p == 'self' + or p in parameters + or p == 'unused_params' + ): continue else: parameters[p] = signature[p].default @@ -198,21 +197,20 @@ def _needs_to_be_calculated(result, recipe, **parameters): return True if result._parameters_changed(**parameters): return True - if result._recipe_changed(recipe): - return True - else: - return False + return bool(result._recipe_changed(recipe)) @property def _is_calculated(self): """Checks if the result has been calculated e.g. checks in Measurement.results """ - if self.mobj.results is not None: - if self.name in self.mobj.results: - if not np.isnan(self.get_result()): - self.log().debug('%s IS calculated' % self.name) - return True + if ( + self.mobj.results is not None + and self.name in self.mobj.results + and not np.isnan(self.get_result()) + ): + self.log().debug('%s IS calculated' % self.name) + return True self.log().debug('%s NOT calculated' % self.name) return False @@ -234,7 +232,7 @@ def _parameters_changed(self, **params): else: self.log().debug('YES parameters changed') for p in params: - if p in self.params and not params[p] == self.params[p]: + if p in self.params and params[p] != self.params[p]: try: self.log().debug('%s %f --> %f' % (p, self.params[p], params[p])) except TypeError: @@ -284,7 +282,7 @@ def __init__(self, mobj, **kwargs): def set_default_recipe(self): """Sets the default_recipe recipe if only one recipe exists.""" if self.default_recipe is None: - if not len(self._recipes()) == 1: + if len(self._recipes()) != 1: self.log().error('Result << %s >> has more than one recipe, but no default_recipe recipe ' % (self.name)) raise KeyError self.default_recipe = list(self._recipes().keys())[0] diff --git a/RockPy/core/sample.py b/RockPy/core/sample.py index 356ad9a..03f70c6 100644 --- a/RockPy/core/sample.py +++ b/RockPy/core/sample.py @@ -46,8 +46,7 @@ def __iter__(self) -> object: ------- RockPy.measurement """ - for m in self.measurements: - yield m + yield from self.measurements def __getitem__(self, item): @@ -131,11 +130,7 @@ def __init__(self, self._results = _results.set_index('mID', drop=True) # assign name to sample if no name is specified - if not name: - name = 'S%02i' % Sample.snum - else: - name = name # unique name, only one per study - + name = 'S%02i' % Sample.snum if not name else name # set name self.name = name @@ -143,11 +138,8 @@ def __init__(self, if not study: study = RockPy.Study() - else: - if not isinstance(study, RockPy.Study): - self.log().error('STUDY not a valid RockPy3.core.Study object. Using RockPy MasterStudy') - # study = RockPy.MasterStudy - + elif not isinstance(study, RockPy.Study): + self.log().error('STUDY not a valid RockPy3.core.Study object. Using RockPy MasterStudy') self.study = study # add sample to study @@ -183,15 +175,11 @@ def info(self): info = pd.DataFrame(columns=['mass [kg]', 'sample groups', 'mtypes', 'stypes', 'svals']) info.loc[self.name, 'mass [kg]'] = self.mass - info.loc[self.name, 'sample groups'] = self._samplegroups if self._samplegroups else 'None' + info.loc[self.name, 'sample groups'] = self._samplegroups or 'None' mtypes = [(mt, len(self.get_measurement(mtype=mt))) for mt in self.mtypes] - if mtypes: - info.loc[self.name, 'mtypes'] = ', '.join(self.mtypes) # if len(mtypes) > 1 else mtypes[0] - else: - info.loc[self.name, 'mtypes'] = None - + info.loc[self.name, 'mtypes'] = ', '.join(self.mtypes) if mtypes else None ''' STYPES ''' if len(self.stypes) == 0: stypes = 'None' @@ -358,17 +346,16 @@ def add_measurement( idx = len(self.measurements) # todo change so it counts the number of subclasses created ''' MINFO object generation ''' - if self.samplegroups: - sgroups = self.samplegroups - else: - sgroups = None - + sgroups = self.samplegroups or None """ DATA import from mass, height, diameter, len ... """ - # check for parameters in kwargs (i.e. mass = '12mg' - parameters = ['mass', 'diameter', 'height', 'x_len', 'y_len', 'z_len'] if create_parameters: - if any([(k in parameters) and (kwargs[k] is not None) for k in kwargs.keys()]): + # check for parameters in kwargs (i.e. mass = '12mg' + parameters = ['mass', 'diameter', 'height', 'x_len', 'y_len', 'z_len'] + if any( + (k in parameters) and (kwargs[k] is not None) + for k in kwargs.keys() + ): mobj = self._add_measurement_from_str(fpath, kwargs, mobj, mtype, series) return mobj @@ -504,7 +491,7 @@ def series(self): ------- set: set of all series in the sample """ - return set(series for m in self.measurements for series in m.series) + return {series for m in self.measurements for series in m.series} @property def samplegroups(self): @@ -521,7 +508,7 @@ def stypes(self): ------- set: stypes """ - return set(stype for m in self.measurements for stype in m.stypes if stype) + return {stype for m in self.measurements for stype in m.stypes if stype} @property def svals(self): @@ -531,9 +518,9 @@ def svals(self): ------- """ - return set(series[1] for series in self.series - if isinstance(series[1], (int, float)) - if not np.isnan(series[1])) + return {series[1] for series in self.series + if isinstance(series[1], (int, float)) + if not np.isnan(series[1])} @property def sunits(self): @@ -543,7 +530,7 @@ def sunits(self): ------- set: series units """ - return set(series[2] for series in self.series) + return {series[2] for series in self.series} @property def mtypes(self): @@ -554,7 +541,7 @@ def mtypes(self): ------- set: mtype """ - return set(m.mtype for m in self.measurements) + return {m.mtype for m in self.measurements} @property def mids(self): @@ -717,7 +704,7 @@ def _convert_sval_range(self, sval_range, mean): out = [] if mean: - svals = set(sval for m in self.mean_measurements for sval in m.svals) + svals = {sval for m in self.mean_measurements for sval in m.svals} else: svals = self.svals diff --git a/RockPy/core/study.py b/RockPy/core/study.py index 841eace..2df15f2 100644 --- a/RockPy/core/study.py +++ b/RockPy/core/study.py @@ -40,13 +40,9 @@ def __init__(self, name=None, folder=None): self.studID = id(self) # use time if no name is specified for the study - if not name: - self.name = time.strftime("%Y%m%d:%H%M") - else: - self.name = name - + self.name = time.strftime("%Y%m%d:%H%M") if not name else name # create empty dictionary for storing samples - self._samples = dict() # {'sname':'sobj'} + self._samples = {} # create variable for all imported files to be stored. If file has been imported the fpath is stored here. self.imported_files = [] @@ -61,8 +57,7 @@ def __repr__(self): return '<< RockPy.Study.{} -- {} >>'.format(self.name, self.studID) def __iter__(self): - for s in sorted(self._samples.values()): - yield s + yield from sorted(self._samples.values()) def __getitem__(self, item): if isinstance(item, int): @@ -96,8 +91,7 @@ def samples(self): RockPy.sample """ - for s in sorted(self._samples.values()): - yield s + yield from sorted(self._samples.values()) @property def samplenames(self): @@ -109,8 +103,7 @@ def samplenames(self): str: RockPy.Sample.name """ - for sname in sorted(self._samples.keys()): - yield sname + yield from sorted(self._samples.keys()) @property def sample_list(self): @@ -137,8 +130,7 @@ def measurements(self): ''' for s in self.samples: - for m in s.measurements: - yield m + yield from s.measurements @property def measurement_list(self): @@ -156,7 +148,7 @@ def mtypes(self): ''' returns a sorted list of unique mtypes ''' - return sorted(set(m.mtype for m in self.measurements)) + return sorted({m.mtype for m in self.measurements}) ''' SAMPLE GROUPS ''' @@ -166,7 +158,7 @@ def n_groups(self): @property def groupnames(self): - return sorted(set(i for j in self.samples for i in j._samplegroups)) + return sorted({i for j in self.samples for i in j._samplegroups}) @property def samplegroups(self): @@ -358,7 +350,9 @@ def get_sample(self, slist = list(self.samples) - if not any(i for i in [gname, sname, mtype, series, stype, sval, sval_range, mean, invert]): + if not any( + [gname, sname, mtype, series, stype, sval, sval_range, mean, invert] + ): return slist # samplegroup filtering @@ -371,7 +365,7 @@ def get_sample(self, sname = to_tuple(sname) slist = [s for s in slist if s.name in sname] - if any(i for i in [mtype, series, stype, sval, sval_range, mean, invert]): + if any([mtype, series, stype, sval, sval_range, mean, invert]): slist = [s for s in slist if s.get_measurement(mtype=mtype, stype=stype, sval=sval, sval_range=sval_range, series=series, @@ -393,14 +387,13 @@ def get_measurement(self, if mid: return [m for s in self.samples for m in s.get_measurement(mid=mid, invert=invert)] - else: - samples = self.get_sample(gname=gname, sname=sname, mtype=mtype, series=series, - stype=stype, sval=sval, sval_range=sval_range, invert=invert, - sid=sid) + samples = self.get_sample(gname=gname, sname=sname, mtype=mtype, series=series, + stype=stype, sval=sval, sval_range=sval_range, invert=invert, + sid=sid) - mlist = (m for s in samples for m in s.get_measurement(mtype=mtype, series=series, - stype=stype, sval=sval, sval_range=sval_range, - invert=invert)) + mlist = (m for s in samples for m in s.get_measurement(mtype=mtype, series=series, + stype=stype, sval=sval, sval_range=sval_range, + invert=invert)) return list(mlist) ''' IMPORT functions ''' @@ -440,7 +433,7 @@ def import_folder(self, slist.append(s) # create all measurements - for i, measurement_dict in enumerate(iHelper.gen_measurement_dict): + for measurement_dict in iHelper.gen_measurement_dict: if s.name != measurement_dict['sname']: continue m = s.add_measurement(create_parameters=False, **measurement_dict) diff --git a/RockPy/core/utils.py b/RockPy/core/utils.py index 064e8ef..009d6af 100644 --- a/RockPy/core/utils.py +++ b/RockPy/core/utils.py @@ -119,7 +119,7 @@ def mtype_implemented(mtype): Returns: bool """ - return True if mtype in RockPy.implemented_measurements else False + return mtype in RockPy.implemented_measurements ''' ARRAY related ''' @@ -158,7 +158,7 @@ def tuple2list_of_tuples(item) -> list: # check if item is a list -> each item in item has to be converted to a tuple if isinstance(item, list): for i, elem in enumerate(item): - if not type(elem) == tuple: + if type(elem) != tuple: item[i] = (elem,) if not isinstance(item, (list, tuple)): @@ -261,7 +261,7 @@ def split_num_alph(item): else: idx = i - if not idx == len(item) - 1: + if idx != len(item) - 1: return float(item[:idx + 1]), item[idx + 1:].strip() else: return float(item), None @@ -419,11 +419,7 @@ def conversion(*args, **kwargs): defaults.update(kwargs) kwargs = defaults - if 'xyz' in kwargs: - xyz = kwargs.pop('xyz') - else: - xyz = args[0] - + xyz = kwargs.pop('xyz') if 'xyz' in kwargs else args[0] ## maintain vector shape part s = np.array(xyz).shape @@ -448,21 +444,25 @@ def conversion(*args, **kwargs): # calculate function xyz = func(xyz, *args[1:], **kwargs) - if transform_output: # return the same data type and shape as input # for internal dtype == dim, the data up to here is dim. Needs to be converted, if input was xyz. - if internal_dtype == 'dim': - # if input data dtype == 'xyz' (i.e. input = 'xyz') - if 'intype' in kwargs and kwargs['intype'] == 'xyz': - from RockPy.tools.compute import convert_to_xyz - xyz = convert_to_xyz(xyz) - - # if the internal dtype is xyz, input data in the format of 'dim' needs to be converted - elif internal_dtype == 'xyz': - # if input data dtype == 'xyz' (i.e. input = 'xyz') - if 'intype' in kwargs and kwargs['intype'] == 'dim': - from RockPy.tools.compute import convert_to_dim - xyz = convert_to_dim(xyz) + if internal_dtype == 'dim': + if ( + transform_output + and 'intype' in kwargs + and kwargs['intype'] == 'xyz' + ): + from RockPy.tools.compute import convert_to_xyz + xyz = convert_to_xyz(xyz) + + elif internal_dtype == 'xyz': + if ( + transform_output + and 'intype' in kwargs + and kwargs['intype'] == 'dim' + ): + from RockPy.tools.compute import convert_to_dim + xyz = convert_to_dim(xyz) return xyz @@ -492,15 +492,14 @@ def maintain_n3_shape(xyz): ## maintain vector shape part s = np.array(xyz).shape - if not any(i == 3 for i in s): + if all(i != 3 for i in s): raise ValueError('At least one dimension needs to be length 3') # for [x,y,z] or [d,i,m] if s == (3,): - if len(set(np.shape(elem) for elem in xyz)) != 1: + if len({np.shape(elem) for elem in xyz}) != 1: raise ValueError('Number of elements ix xyz is inconsistent') xyz = np.array(xyz).reshape((1, 3)) - # for array like [[x1,x2,... ],[y1,y2,...],[z1,z2,...]], elif s[0] == 3 and s[1] != 3: xyz = np.array(xyz).T elif s[1] == 3 and s[0] != 3: diff --git a/RockPy/ftypes/cif.py b/RockPy/ftypes/cif.py index dd3d9ae..38b8287 100644 --- a/RockPy/ftypes/cif.py +++ b/RockPy/ftypes/cif.py @@ -157,11 +157,7 @@ def _separate_row(raw_data): num_index += 1 mtype = row[:num_index].rstrip() - if not row[num_index:6]: - level = 0 - else: - level = int(row[num_index:6]) - + level = 0 if not row[num_index:6] else int(row[num_index:6]) # other columns are separate by whitespace -> split(' ') values = [i for i in row[6:].split(' ') if i] @@ -236,8 +232,7 @@ def _correct_holder(sample_means, holder_means): Returns: :obj:`pandas.Series`: Holder corrected 'x', 'y' and 'z' values """ - corrected = sample_means - holder_means - return corrected + return sample_means - holder_means def _write_cif_line(self, series): """ Writes one cit formatted line. @@ -260,7 +255,7 @@ def _write_cif_line(self, series): level = int(series['level'] * 10000) for l in series.index: - if not l in self.out_units: + if l not in self.out_units: continue try: series[l] *= (1 * self.units[l]).to(self.out_units[l]).magnitude @@ -284,7 +279,7 @@ def _write_cif_line(self, series): 'std_x': '{:>.6f}', 'std_y': '{:>.6f}', 'std_z': '{:>.6f}', 'user': '{:>7}', 'date': '{:>4}', 'time': '{:>4}'} - if mtype == 'NRM' or mtype == 'ARM': + if mtype in ['NRM', 'ARM']: formats['mtype'] = "{:<3}" formats['level'] = '{:>3}' level = '' @@ -333,10 +328,7 @@ def _read_header(self, header_rows): for label in lw_dict: v = header_rows[1][lw_dict[label][0]:lw_dict[label][1]].strip(' ') - if v: - header.loc[sample_id, label] = float(v) - else: - header.loc[sample_id, label] = None + header.loc[sample_id, label] = float(v) if v else None header.index.name = 'sample_id' return header @@ -396,7 +388,7 @@ def _read_UP_file(cls, dfile, sample_id, reload=False): out = cls.imported_files[dfile].copy() # todo does this have to be a copy? # check if the sample is in the data - if not sample_id in set(out['Sample']): + if sample_id not in set(out['Sample']): RockPy.log.warning('Could not find sample_id << {} >> in file << {} >.! ' 'Please check correct spelling'.format(sample_id, os.path.basename(dfile))) return @@ -558,22 +550,19 @@ def from_rapid(cls, files_or_folder, # read all the files , create list of Dataframes raw_df = [] - for i, dfile in enumerate(files): + for dfile in files: # print('reading file << {:>20} >> {:>4} of {:>4}'.format(os.path.basename(dfile), i, len(files)), end='\r') readdf = cls._read_UP_file(dfile, sample_id, reload=reload) if readdf is not None: raw_df.append(readdf) - average_df = [] - for i, df in enumerate(raw_df): - # print('averaging file {:>4} of {:>4}'.format(i, len(raw_df)), end='\r') - average_df.append(cls._return_mean_from_UP_file(df, subtract_holder=subtract_holder)) - if len(average_df) > 1: - data = pd.concat(average_df) - else: - data = average_df[0] + average_df = [ + cls._return_mean_from_UP_file(df, subtract_holder=subtract_holder) + for df in raw_df + ] + data = pd.concat(average_df) if len(average_df) > 1 else average_df[0] data = xyz2dim(data, colX='x', colY='y', colZ='z', colI='plate_inc', colD='plate_dec', colM='intensity') data = data.sort_index() @@ -767,11 +756,17 @@ def _write_header(cls, core_strike, core_dip, comment += f' >> RockPy exported {datetime.now().strftime("%Y-%m-%d %H:%M")}' - out = ['{:<4}{:<9}{}\n'.format(locality_id, sample_id, comment[:255]), - ' {:>6} {:>5} {:>5} {:>5} {:>5} {:>5}\n'.format(stratigraphic_level, core_strike, core_dip, - bedding_strike, bedding_dip, core_volume_or_mass), - ] - return out + return [ + '{:<4}{:<9}{}\n'.format(locality_id, sample_id, comment[:255]), + ' {:>6} {:>5} {:>5} {:>5} {:>5} {:>5}\n'.format( + stratigraphic_level, + core_strike, + core_dip, + bedding_strike, + bedding_dip, + core_volume_or_mass, + ), + ] def read_file(self): """ diff --git a/RockPy/ftypes/cryomag.py b/RockPy/ftypes/cryomag.py index 66f864f..8656400 100644 --- a/RockPy/ftypes/cryomag.py +++ b/RockPy/ftypes/cryomag.py @@ -29,8 +29,7 @@ def __init__(self, dfile, snames=None, dialect='tdt', reload=False): @property def _raw_data(self): - out = CryoMag.imported_files[self.dfile] - return out + return CryoMag.imported_files[self.dfile] def read_file(self): diff --git a/RockPy/ftypes/jr6.py b/RockPy/ftypes/jr6.py index 1b41caf..6c798aa 100644 --- a/RockPy/ftypes/jr6.py +++ b/RockPy/ftypes/jr6.py @@ -85,11 +85,7 @@ def lookup_lab_treatment_code(self, item): out = None if self.dialect == 'tdt': - if item.lower() == 'nrm': - split = [0, item.upper()] - else: - split = item.split('.') - + split = [0, item.upper()] if item.lower() == 'nrm' else item.split('.') idx = Jr6.table[self.dialect].index(split[1]) out = Jr6.pint_treatment_codes[idx] diff --git a/RockPy/ftypes/mpms.py b/RockPy/ftypes/mpms.py index 99b7056..e938645 100644 --- a/RockPy/ftypes/mpms.py +++ b/RockPy/ftypes/mpms.py @@ -50,7 +50,7 @@ def group_by(self, what): iterator - pandas.DataFrame: """ - if not what in self.data.columns: + if what not in self.data.columns: raise KeyError('Value << %s >> not in data.columns. Chose from: %s'%(what, ', '.join(self.data.columns))) for v in sorted(set(self.data[what])): diff --git a/RockPy/ftypes/tools.py b/RockPy/ftypes/tools.py index 0c2320a..925acf2 100644 --- a/RockPy/ftypes/tools.py +++ b/RockPy/ftypes/tools.py @@ -2,7 +2,7 @@ from RockPy.core.ftype import Ftype -def __implemented__(cls): # todo move into RockPy core has nothing to do with measurement +def __implemented__(cls): # todo move into RockPy core has nothing to do with measurement """Dictionary of all implemented filetypes. Looks for all subclasses of RockPy3.core.ftype.ftypes generating a @@ -15,8 +15,7 @@ def __implemented__(cls): # todo move into RockPy core has nothing to do with m Returns: classname:: **dict** """ - implemented = {cl.__name__.lower(): cl for cl in extract_inheritors_from_cls(cls)} - return implemented + return {cl.__name__.lower(): cl for cl in extract_inheritors_from_cls(cls)} if __name__ == '__main__': diff --git a/RockPy/ftypes/variforc.py b/RockPy/ftypes/variforc.py index d13d2bd..416a43f 100644 --- a/RockPy/ftypes/variforc.py +++ b/RockPy/ftypes/variforc.py @@ -33,9 +33,8 @@ def read_header(cls, dfile): with open(dfile) as f: raw_header = f.readlines() - header = {} + header = {'mtype': raw_header[0].split(' ')[1].rstrip()} - header['mtype'] = raw_header[0].split(' ')[1].rstrip() header['VariForc_version'] = raw_header[0].split(' ')[0].rstrip() header['data_start_idx'] = [i + 1 for i, v in enumerate(raw_header) if cls.header_ends[header['mtype']] in v][0] @@ -49,7 +48,10 @@ def read_header(cls, dfile): for i, x in enumerate(v): if x.startswith(' '): x = x[1:] - if not any(letter in x.lower() for letter in 'abcdfghijklmnopqrstuvwxyz'): + if all( + letter not in x.lower() + for letter in 'abcdfghijklmnopqrstuvwxyz' + ): v[i] = float(x) elif x.lower() == 'false': v[i] = False diff --git a/RockPy/ftypes/vsm.py b/RockPy/ftypes/vsm.py index 7a7df67..53633fe 100644 --- a/RockPy/ftypes/vsm.py +++ b/RockPy/ftypes/vsm.py @@ -46,13 +46,16 @@ def __init__(self, dfile, snames=None, dialect=None, reload=False): self.calibration_factor = float(self.header.loc['Calibration factor']) self.correct_exp = None - if not np.isnan(self.calibration_factor): - if np.floor(np.log10(self.calibration_factor)) != self.standard_calibration_exponent: - self.correct_exp = np.power(10, np.floor(np.log10(self.calibration_factor))) - RockPy.log.warning( - 'CALIBRATION FACTOR (cf) seems to be wrong. CF should be {} here: {}. Data was corrected'.format( - self.standard_calibration_exponent, - int(np.floor(np.log10(self.calibration_factor))))) + if ( + not np.isnan(self.calibration_factor) + and np.floor(np.log10(self.calibration_factor)) + != self.standard_calibration_exponent + ): + self.correct_exp = np.power(10, np.floor(np.log10(self.calibration_factor))) + RockPy.log.warning( + 'CALIBRATION FACTOR (cf) seems to be wrong. CF should be {} here: {}. Data was corrected'.format( + self.standard_calibration_exponent, + int(np.floor(np.log10(self.calibration_factor))))) if self.correct_exp: for c in self.data: @@ -97,7 +100,13 @@ def read_header(self, dfile, header_end): skiprows=2, skip_blank_lines=True, widths=(31, 13), index_col=0, names=[0]) # remove empty line and section headers - idx = [i for i,v in enumerate(header.index) if not str(v).upper() == v if str(v) != 'nan'] + idx = [ + i + for i, v in enumerate(header.index) + if str(v).upper() != v + if str(v) != 'nan' + ] + header = header.iloc[idx] header = header.replace('No', False) @@ -112,7 +121,7 @@ def read_header(self, dfile, header_end): # add file location to header header.loc['fpath'] = dfile - if not 'Calibration factor' in header.index: + if 'Calibration factor' not in header.index: header.loc['Calibration factor'] = None return header @@ -135,7 +144,7 @@ def read_segement_infos(self, dfile, mtype, Returns -------s """ - if not 'First-order reversal curves' in mtype: + if 'First-order reversal curves' not in mtype: # reading segments_tab data head = self.raw_data[header_end+1:segment_start] head = pd.read_fwf(io.StringIO(''.join(head)), widths=segment_widths) @@ -176,10 +185,7 @@ def _construct_segment_infos_from_data(self): for i, idx in enumerate(nanidx): # get start index of segment - if i == 0: - sidx = 0 - else: - sidx = nanidx[i - 1] + 1 + sidx = 0 if i == 0 else nanidx[i - 1] + 1 # end index eidx = idx - 1 @@ -210,11 +216,10 @@ def read_file(self): pd.read_fwf(self.dfile, skiprows=self.data_start - 4, nrows=3, widths=self.data_widths).values.T] - data = pd.read_csv(io.StringIO(''.join(self.raw_data[self.data_start:])), + return pd.read_csv(io.StringIO(''.join(self.raw_data[self.data_start:])), nrows=int(self.file_length - self.data_start) - 2, names=data_header, skip_blank_lines=False, squeeze=True, ) - return data @property def iter_segments(self): diff --git a/RockPy/packages/generic/parameter.py b/RockPy/packages/generic/parameter.py index 8d38b9d..e5d5672 100644 --- a/RockPy/packages/generic/parameter.py +++ b/RockPy/packages/generic/parameter.py @@ -54,10 +54,7 @@ def _format_generic(self): pass def __repr__(self): - if self.is_mean: - add = 'mean_' - else: - add = '' + add = 'mean_' if self.is_mean else '' return '<>'.format(self.sobj.name, add, self.mtype, '[' + ';'.join(['{},{}({})'.format(i[0], i[1], i[2]) for i in self.get_series()]) + ']' if self.has_series() else '', diff --git a/RockPy/packages/magnetism/measurements.py b/RockPy/packages/magnetism/measurements.py index 1a42fc5..3ed255f 100644 --- a/RockPy/packages/magnetism/measurements.py +++ b/RockPy/packages/magnetism/measurements.py @@ -48,7 +48,7 @@ def _format_vsm(ftype_data, sobj_name=None): # expected column names for typical VSM hysteresis experiments expected_columns = ['Field (T)', 'Moment (Am2)'] - if not all(i in expected_columns for i in ftype_data.data.columns): + if any(i not in expected_columns for i in ftype_data.data.columns): Hysteresis.log().debug('ftype_data has more than the expected columns: %s' % list(ftype_data.data.columns)) data = ftype_data.data.rename(columns={"Field (T)": "B", "Moment (Am2)": "M"}) @@ -131,10 +131,7 @@ def has_virgin(self): bool: """ # check if the first point is close to the maximum/minimum field - if np.abs(self.data.index[0]) > 0.9 * self.data.index.max(): - return False - else: - return True + return np.abs(self.data.index[0]) <= 0.9 * self.data.index.max() def get_polarity_switch(self, window=5): """ @@ -168,10 +165,7 @@ def get_polarity_switch(self, window=5): diffs = diffs.fillna(method='bfill') # filling missing values at beginning diffs = diffs.fillna(method='ffill') # filling missing values at end - # reduce to sign of the differences - asign = diffs.apply(np.sign) - - return asign + return diffs.apply(np.sign) def get_polarity_switch_index(self, window=1): """Method calls hysteresis.get_polarity_switch with window and then @@ -204,7 +198,7 @@ def downfield(self): if len(idx) > 1: return self.data.iloc[int(idx[0]):int(idx[1])].dropna(axis=1).dropna() else: - return self.data.iloc[0:int(idx[1])].dropna(axis=1).dropna() + return self.data.iloc[:int(idx[1])].dropna(axis=1).dropna() @property def upfield(self): @@ -374,7 +368,7 @@ def recipe_default(self, npoints=4, check=False, **unused_params): down_f = m.downfield[m.downfield['M'].abs() <= df_moment] up_f = m.upfield[m.upfield['M'].abs() <= uf_moment] - for i, dir in enumerate([down_f, up_f]): + for dir in [down_f, up_f]: slope, intercept, r_value, p_value, std_err = stats.linregress(dir.index, dir['M']) result.append(intercept) @@ -693,7 +687,7 @@ def data_gridding(self, order=2, grid_points=20, tuning=1, ommit_n_points=0, che **parameter: Keyword arguments passed through """ - if any([len(i.index) <= 50 for i in [self.downfield, self.upfield]]): + if any(len(i.index) <= 50 for i in [self.downfield, self.upfield]): self.log.warning('Hysteresis branches have less than 50 (%i) points, gridding not possible' % ( len(self.data['down_field']['field'].v))) return @@ -714,7 +708,7 @@ def data_gridding(self, order=2, grid_points=20, tuning=1, ommit_n_points=0, che # initialize DataFrame for gridded data interp_data = pd.DataFrame(columns=self.data.columns) - for n, dtype in enumerate(['downfield', 'upfield', 'virgin']): + for dtype in ['downfield', 'upfield', 'virgin']: aux = pd.DataFrame(columns=self.data.columns) # catch missing branches @@ -768,13 +762,10 @@ def data_gridding(self, order=2, grid_points=20, tuning=1, ommit_n_points=0, che aux = aux.reset_index(drop=True) interp_data = pd.concat([interp_data, aux], sort=True) - # interp_data.index = interp_data.index.astype(np.float) - self.replace_data(interp_data) if check: - ax = self.check_plot(self.data, uncorrected_data) - return ax + return self.check_plot(self.data, uncorrected_data) # def correct_symmetry(self, check=False): # @@ -994,8 +985,7 @@ def zf_steps(self): @property def nrm(self): - d = self.data[self.data['LT_code'] == 'LT-NO'].set_index('ti') - return d + return self.data[self.data['LT_code'] == 'LT-NO'].set_index('ti') @property def if_steps(self): @@ -1036,8 +1026,7 @@ def ac(self): pandas.DataFrame: """ - d = self.data[self.data['LT_code'] == 'LT-PTRM-Z'].set_index('ti') - return d + return self.data[self.data['LT_code'] == 'LT-PTRM-Z'].set_index('ti') @property def tr(self): @@ -1047,8 +1036,7 @@ def tr(self): pandas.DataFrame: """ - d = self.data[self.data['LT_code'] == 'LT-PTRM-MD'].set_index('ti') - return d + return self.data[self.data['LT_code'] == 'LT-PTRM-MD'].set_index('ti') @property def ifzf_diff(self): @@ -1201,8 +1189,7 @@ def delta_x_dash(self, vmin, vmax, component, **unused_params: """ x_dash = self.x_dash(vmin=vmin, vmax=vmax, component=component, **unused_params) - out = abs(np.max(x_dash) - np.min(x_dash)) - return out + return abs(np.max(x_dash) - np.min(x_dash)) def delta_y_dash(self, vmin, vmax, component, **unused_params): @@ -1217,8 +1204,7 @@ def delta_y_dash(self, vmin, vmax, component, **unused_params: """ y_dash = self.y_dash(vmin=vmin, vmax=vmax, component=component, **unused_params) - out = abs(np.max(y_dash) - np.min(y_dash)) - return out + return abs(np.max(y_dash) - np.min(y_dash)) def best_fit_line_length(self, vmin=20, vmax=700, component='m'): """ @@ -1227,9 +1213,10 @@ def best_fit_line_length(self, vmin=20, vmax=700, component='m'): vmax: component: """ - L = np.sqrt((self.delta_x_dash(vmin=vmin, vmax=vmax, component=component)) ** 2 + - (self.delta_y_dash(vmin=vmin, vmax=vmax, component=component)) ** 2) - return L + return np.sqrt( + (self.delta_x_dash(vmin=vmin, vmax=vmax, component=component)) ** 2 + + (self.delta_y_dash(vmin=vmin, vmax=vmax, component=component)) ** 2 + ) def recipe_default(self, vmin=20, vmax=700, component='m', **unused_params): """calculates the least squares slope for the specified temperature @@ -1613,18 +1600,17 @@ def _format_vsm(ftype_data, **kwargs): # expected column names for typical VSM hysteresis experiments expected_columns = ['Field (T)', 'Remanence (Am2)'] - if not all(i in expected_columns for i in ftype_data.data.columns): + if any(i not in expected_columns for i in ftype_data.data.columns): Dcd.log().error('ftype_data has more than the expected columns: %s' % list(ftype_data.data.columns)) segment_index = ftype_data.mtype.index('dcd') - data = ftype_data.get_segment_data(segment_index).rename(columns={"Field (T)": "B", "Remanence (Am2)": "M"}) - - return data + return ftype_data.get_segment_data(segment_index).rename( + columns={"Field (T)": "B", "Remanence (Am2)": "M"} + ) @staticmethod def _format_agm(ftype_data, **kwargs): - data = Dcd._format_vsm(ftype_data, **kwargs) - return data + return Dcd._format_vsm(ftype_data, **kwargs) @staticmethod def _format_vftb(ftype_data, sobj_name=None): # todo implement VFTB @@ -1782,14 +1768,14 @@ def _format_vsm(ftype_data, **kwargs): # expected column names for typical VSM hysteresis experiments expected_columns = ['Field (T)', 'Remanence (Am2)'] - if not all(i in expected_columns for i in ftype_data.data.columns): + if any(i not in expected_columns for i in ftype_data.data.columns): IrmAcquisition.log().error( 'ftype_data has more than the expected columns: %s' % list(ftype_data.data.columns)) segment_index = ftype_data.mtype.index('irm') - data = ftype_data.get_segment_data(segment_index).rename(columns={"Field (T)": "B", "Remanence (Am2)": "M"}) - - return data + return ftype_data.get_segment_data(segment_index).rename( + columns={"Field (T)": "B", "Remanence (Am2)": "M"} + ) if __name__ == '__main__': diff --git a/RockPy/packages/magnetism/simulations.py b/RockPy/packages/magnetism/simulations.py index 5364343..3f86291 100644 --- a/RockPy/packages/magnetism/simulations.py +++ b/RockPy/packages/magnetism/simulations.py @@ -40,9 +40,13 @@ def get_steps(steps, tmax=680., ck_every=2, tr_every=2, ac_every=2): calls RockPy.packages.Magnetismnetism.Measurement.Simulation.utils.ThellierStepMaker """ - out = SimUtils.ThellierStepMaker(steps=steps, tmax=tmax, ck_every=ck_every, tr_every=tr_every, - ac_every=ac_every) - return out + return SimUtils.ThellierStepMaker( + steps=steps, + tmax=tmax, + ck_every=ck_every, + tr_every=tr_every, + ac_every=ac_every, + ) def __init__(self, preset=None, a11=None, a12=None, a13=None, a1t=None, @@ -120,7 +124,7 @@ def __init__(self, preset=None, if preset is not None and preset not in self.presets: print('preset << %s >> not implemented, chose from:' % preset) - print(', '.join([i for i in self.presets])) + print(', '.join(list(self.presets))) print('using: Fabian Fig. 4a') preset = 'Fabian4a' @@ -297,11 +301,7 @@ def FieldMatrix(self, tau_i, hlab, pressure_demag=False): data = np.ones((self.tau_ub.size, self.tau_b.size)) * 10 # the index is where ti == tau_b and tau_ub - if tau_i == 0: - idx = 0 - else: - idx = np.argmin(np.abs(self.tau_b - tau_i)) + 1 - + idx = 0 if tau_i == 0 else np.argmin(np.abs(self.tau_b - tau_i)) + 1 # self.log().debug('Tau_i = %.2f, idx = %i'%(tau_i, idx)) data[:idx, :idx] = hlab # self.log().debug('hlab rectangle shape: (%s, %s)'%data[:idx, :idx].shape) @@ -443,20 +443,11 @@ def get_data(self, steps=None, pressure_demag=False, norm=False): tau = self.tau(t) # self.log().debug('Calculating temperature %i (tau_i = %.2f)'%(t, tau)) - if typ == 'LT-NO': # todo moment in x,y,z - m = self.moment(tau_i=tau, applied_field=0, pressure_demag=pressure_demag) - elif typ == 'LT-T-Z': + if typ in ['LT-NO', 'LT-T-Z']: # todo moment in x,y,z m = self.moment(tau_i=tau, applied_field=0, pressure_demag=pressure_demag) elif typ == 'LT-T-I': m = self.moment(tau_i=tau, applied_field=self.simparams['hlab'], pressure_demag=pressure_demag) - # elif typ == 'LT-PTRM-I': #todo add AC, TR, CK steps - # NRM_Tj = self.get_moment(tau_i=self.tau(prev), hlab=0, pressure_demag=pressure_demag) - # pTRM_Ti = self.get_moment(tau_i=tau, hlab=hlab, pressure_demag=pressure_demag) - # NRM_Ti = self.get_moment(tau_i=tau, hlab=0, pressure_demag=pressure_demag) - # m = pTRM_Ti - NRM_Ti + NRM_Tj - # # print(row, column, typ, t, tau, prev, m) - else: continue @@ -621,11 +612,7 @@ def plot_roquet(self, steps=None, hlab=1, pressure_demag=False, norm=False, ax=N ls = kwargs.pop('ls', '-') marker = kwargs.pop('marker', '.') - if norm: - norm = th['m'].max() - else: - norm = 1 - + norm = th['m'].max() if norm else 1 color = kwargs.pop('color', None) # pTRM plot ax.plot(th.index, (pt['m'] - th['m'])/norm, diff --git a/RockPy/packages/magnetism/tools.py b/RockPy/packages/magnetism/tools.py index 0eb4ea5..4d7c34d 100644 --- a/RockPy/packages/magnetism/tools.py +++ b/RockPy/packages/magnetism/tools.py @@ -61,7 +61,7 @@ def ThellierStepMaker(steps, tmax=680., ck_every=2, tr_every=2, ac_every=2): elif i <= len(steps): out.loc[i, 'LT-PTRM-Z'] = ac_step - if tr_every != 0 and not i % tr_every and not i == 0: + if tr_every != 0 and not i % tr_every and i != 0: out.loc[i, 'LT-PTRM-MD'] = t return out \ No newline at end of file diff --git a/RockPy/packages/xrd/tools.py b/RockPy/packages/xrd/tools.py index 074ca5f..540978b 100644 --- a/RockPy/packages/xrd/tools.py +++ b/RockPy/packages/xrd/tools.py @@ -26,8 +26,7 @@ def theta_to_q(theta, lamb): if isinstance(lamb, str): lamb = wavelength(lamb) - q = (4 * np.pi * np.sin(np.deg2rad(theta))) / lamb - return q + return (4 * np.pi * np.sin(np.deg2rad(theta))) / lamb def q_to_theta(q, lamb): @@ -59,11 +58,7 @@ def pdd_transpose_wavelength(pdd, lambda1, lambda2, column='index'): pdd = pdd.copy() - if column == 'index': - theta = pdd.index - else: - theta = pdd[column] - + theta = pdd.index if column == 'index' else pdd[column] q = theta_to_q(theta=theta/2, lamb=lambda1) theta_new = q_to_theta(q, lamb=lambda2) *2 diff --git a/RockPy/tests/test_study.py b/RockPy/tests/test_study.py index ad7dfd3..8bd4b66 100644 --- a/RockPy/tests/test_study.py +++ b/RockPy/tests/test_study.py @@ -7,9 +7,7 @@ def setUp(self): def create_samples(self): samples_to_add = ['S%i'%i for i in range(10)] - slist = [] - for sname in samples_to_add: - slist.append(self.S.add_sample(sname)) + slist = [self.S.add_sample(sname) for sname in samples_to_add] return samples_to_add, slist # def test_n_samples(self): diff --git a/RockPy/tools/compute.py b/RockPy/tools/compute.py index 1ae3468..5a0836e 100644 --- a/RockPy/tools/compute.py +++ b/RockPy/tools/compute.py @@ -16,10 +16,9 @@ def rx(angle): Returns: ndarray: Rotationmatrix """ - RX = [[1, 0, 0], + return [[1, 0, 0], [0, np.cos(angle), -np.sin(angle)], [0, np.sin(angle), np.cos(angle)]] - return RX def ry(angle): @@ -31,10 +30,9 @@ def ry(angle): Returns: ndarray: Rotationmatrix """ - RY = [[np.cos(angle), 0, np.sin(angle)], + return [[np.cos(angle), 0, np.sin(angle)], [0, 1, 0], [-np.sin(angle), 0, np.cos(angle)]] - return RY def rz(angle): @@ -46,10 +44,9 @@ def rz(angle): Returns: ndarray: Rotationmatrix """ - RZ = [[np.cos(angle), -np.sin(angle), 0], + return [[np.cos(angle), -np.sin(angle), 0], [np.sin(angle), np.cos(angle), 0], [0, 0, 1]] - return RZ def rotmat(dec, inc): @@ -106,9 +103,7 @@ def rotate_around_axis(xyz, *, axis_unit_vector, theta, axis_di=False, intype='x [uy * ux * (1 - cost) + uz * sint, cost + uy ** 2 * (1 - cost), uy * uz * (1 - cost) - ux * sint], [uz * ux * (1 - cost) - uy * sint, uz * uy * (1 - cost) + ux * sint, cost + uz ** 2 * (1 - cost)]]) - out = np.dot(R, xyz.T).T - - return out + return np.dot(R, xyz.T).T @handle_shape_dtype @@ -181,10 +176,11 @@ def rotate_360_deg(xyz, theta, intype='xyz'): intype: """ - circle = [] - # rotate around z axis - for deg in np.arange(0, 360, 2): - circle.append(rotate([0, 90 - theta, 1], axis='z', intype='dim', theta=deg)[0]) + circle = [ + rotate([0, 90 - theta, 1], axis='z', intype='dim', theta=deg)[0] + for deg in np.arange(0, 360, 2) + ] + circle = np.array(circle) # rotate that by 90-inc around 'y' axis (note: rotations are anticlockwise) @@ -225,18 +221,13 @@ def convert_to_xyz(dim, *, M=True): D = dim[:, 0] I = dim[:, 1] - if M: - M = dim[:, 2] - else: - M = np.ones(len(D)) - + M = dim[:, 2] if M else np.ones(len(D)) M = 1 if M is None else M x = np.cos(np.radians(I)) * np.cos(np.radians(D)) * M y = np.cos(np.radians(I)) * np.sin(np.radians(D)) * M z = np.cos(np.radians(I)) * np.tan(np.radians(I)) * M - out = np.array([x, y, z]).T - return out + return np.array([x, y, z]).T @handle_shape_dtype(transform_output=False) @@ -301,9 +292,7 @@ def convert_to_stereographic(xyz, intype='dim'): r = 1 - (1 - np.tan((np.pi / 4) - (abs(i) / 2))) - out = np.array([d, r, neg]).T - - return out + return np.array([d, r, neg]).T @handle_shape_dtype(internal_dtype='xyz', transform_output=False) @@ -350,9 +339,7 @@ def convert_to_equal_area(xyz, intype='xyz'): i = np.radians(np.abs(i)) r = 1 - np.sqrt(1 - np.sin(i)) - # r = np.abs(xyz[:, 2]) # Tauxe - out = np.array([d, r0 - r, neg]).T - return out + return np.array([d, r0 - r, neg]).T @handle_shape_dtype(internal_dtype='dim', transform_output=False) @@ -491,9 +478,8 @@ def detect_outlier(x, y, order, threshold): p, residual, rank, singular_values, rcond = np.polyfit(x, y, order, full=True) rmse = np.sqrt(sum(residual) / len(x)) # root mean squared error p = np.poly1d(p) # polynomial p(x) - outliers = [i for i, v in enumerate(y) if v < p(x[i]) - threshold * rmse] + \ + return [i for i, v in enumerate(y) if v < p(x[i]) - threshold * rmse] + \ [i for i, v in enumerate(y) if v > p(x[i]) + threshold * rmse] - return outliers def crossing_1d(x1, y1, x2, y2, lim=None, check=False, **kwargs): diff --git a/RockPy/tools/plotting.py b/RockPy/tools/plotting.py index d28ac59..ff2d57d 100644 --- a/RockPy/tools/plotting.py +++ b/RockPy/tools/plotting.py @@ -228,11 +228,7 @@ def plot_stems(hkl, ymin=0, ymax=None, minI=0.5, ax=None, color=None): for r in hkl.index: if hkl.loc[r]['Iobs'] >= minI: - if ymax is None: - y = hkl.loc[r]['Iobs'] - else: - y = ymax - + y = hkl.loc[r]['Iobs'] if ymax is None else ymax ax.axvline(r, ymin=ymin / ymx, ymax=y / ymx, color=color, lw=0.7) @@ -316,7 +312,7 @@ def combined_label_legend(ax=None, pad=0.25, bbox_to_anchor=[1, 1], h += add_handles l += add_labels - mxlen = max([len(i) for i in handles]) + mxlen = max(len(i) for i in handles) ax.legend(handles, labels, bbox_to_anchor=bbox_to_anchor, handler_map={tuple: HandlerTuple(ndivide=None, pad=-1)}, handletextpad=mxlen * pad, @@ -341,7 +337,7 @@ def log10_isolines(ax=None, angle=45): # np.power(10, (np.array([-20., 20.]) * s0 - int0)), '-r')#, scaley=False, scalex=False) # plot iso lines - for i, s in enumerate(np.arange(-20, 20, 1)): + for s in np.arange(-20, 20, 1): # for each power xnew = np.power(10, (np.array([-20., 20.]) - s / 2)) ynew = np.power(10, (np.array([-20., 20.]) + s / 2)) @@ -393,10 +389,10 @@ def line_through_points(p1, p2, x_extent=None, ax=None, **kwargs): slope = (p2[1] - p1[1]) / (p2[0] - p1[0]) intercept = p1[1] - slope * p1[0] - if not ('linestyle' in kwargs or 'ls' in kwargs): + if 'linestyle' not in kwargs and 'ls' not in kwargs: kwargs = kwargs.update({'linestyle': '--'}) - if not ('color' in kwargs or 'c' in kwargs): + if 'color' not in kwargs and 'c' not in kwargs: kwargs.update({'color': '0.5'}) if kwargs and ('ls' in kwargs): @@ -668,7 +664,7 @@ def plot_metamorphic_facies(ax=None, facies_list=None, text=None, **kwargs): kwargscopy = deepcopy(kwargs) fontdictcopy = deepcopy(kwargs) - if not fascies in xls.sheet_names: + if fascies not in xls.sheet_names: continue # todo add warning data = pd.read_excel(xls, fascies).rolling(2).mean() @@ -795,8 +791,7 @@ def transform(self, abc): x = 1 / 2 * ((2 * b + c) / np.sum(abc, axis=1)) y = np.sqrt(3) / 2 * (c / np.sum(abc, axis=1)) - xy = np.array(list(zip(x, y))) - return xy + return np.array(list(zip(x, y))) def transform_2d(ab, ): ''' @@ -808,8 +803,7 @@ def transform_2d(ab, ): ''' ab = np.array(ab) c = 1 - np.linalg.norm(ab, axis=1) - abc = np.array([ab[:, 0], ab[:, 1], c]).T - return abc + return np.array([ab[:, 0], ab[:, 1], c]).T def label_corner(self, corner, name, formula=None, **kwargs): """ @@ -831,14 +825,8 @@ def label_corner(self, corner, name, formula=None, **kwargs): h_align = ['center', 'center', 'center'] v_align = ['top', 'top', 'bottom'] - s = None - if formula: - s = formula - if s: - s = '\n'.join([s, name]) - else: - s = name - + s = formula or None + s = '\n'.join([s, name]) if s else name self.ax.text(*coordinates[corner], s=s, ha=h_align[corner], va=v_align[corner], **kwargs) def plot_abc(self, abc, **kwargs): diff --git a/RockPy/tools/pressure.py b/RockPy/tools/pressure.py index 7955979..eeb1e24 100644 --- a/RockPy/tools/pressure.py +++ b/RockPy/tools/pressure.py @@ -17,9 +17,7 @@ def pressure(force, diameter): area = ((diameter / 2) ** 2) * np.pi - pressure = force / area # N/m2 / Pa - - return pressure + return force / area def overburden_pressure(thickness, density=2600): """Calculates the approximate pressure of a layer (thickness) of rock with