Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 37 additions & 37 deletions RockPy/core/file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Comment on lines -25 to +28

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function read_abbreviations refactored with the following changes:


# create inverse abbrev:mtype/ftype
get_mtype_ftype = {i: k for k in get_abbreviations for i in get_abbreviations[k]}
Expand Down Expand Up @@ -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:
Comment on lines -144 to +147

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ImportHelper.extract_add_dialect_block refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

return block, ''
parts = block.split(",")
dialect = [p for p in parts if "dialect" in p][0].replace("dialect=", "")
Expand All @@ -161,8 +164,8 @@ def from_folder(cls, folder, filter=None):
RockPy.minfo:
"""

if filter == None:
filter = dict()
if filter is None:
filter = {}
Comment on lines -164 to +168

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ImportHelper.from_folder refactored with the following changes:

This removes the following comments ( why? ):

# append subsequent minfos


dfiles = [os.path.join(folder, i) for i in os.listdir(folder) if not i.startswith("#")]

Expand All @@ -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
Expand All @@ -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:
Comment on lines -207 to +205

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ImportHelper.from_file refactored with the following changes:

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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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))
Comment on lines -344 to +351

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ImportHelper.__init__ refactored with the following changes:

This removes the following comments ( why? ):

# if only one series and not three


self.series = [series]

Expand Down Expand Up @@ -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):
Comment on lines -375 to +379

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ImportHelper.get_measurement_block refactored with the following changes:

raise ImportError("sname, mtype, ftype needed for minfo to be generated")
return "_".join((RockPy.core.utils.tuple2str(b) for b in block))

Expand Down Expand Up @@ -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"
Comment on lines -422 to +427

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ImportHelper.get_sample_block refactored with the following changes:

else:
out.append("".join(map(str, b)))
return ",".join(out)
Expand Down Expand Up @@ -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]:
Comment on lines -478 to +479

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ImportHelper.new_filenames refactored with the following changes:

if not block:
blocks.pop()
else:
Expand Down Expand Up @@ -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:
Comment on lines -512 to +515

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ImportHelper.getImportHelper refactored with the following changes:

continue
a = self.__class__.from_dict(**ih)
yield a
Expand Down Expand Up @@ -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])
Comment on lines -586 to -588

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ImportHelper.return_file_infos refactored with the following changes:

out['sgroups'] = list_or_item(self.sgroups[0])
out['mtypes'] = list_or_item(self.mtypes[0])
out['ftype'] = self.ftype[0]
Expand Down
20 changes: 9 additions & 11 deletions RockPy/core/ftype.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,20 @@ 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
Comment on lines -169 to +174

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Ftype._has_specimen refactored with the following changes:


def to_si_units(self):
""" converts each numeric column in self.data to SI internal_units TODO: write to out_units
"""
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:
Comment on lines -183 to +182

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Ftype.to_si_units refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)

self.log().warning(
'Unit of data column << {} >> has no internal unit equivalent. The input unit is << {:P} >>.'.format(
col, self.in_units[col]))
Expand Down Expand Up @@ -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
Comment on lines -299 to +300

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function is_implemented refactored with the following changes:

Loading