Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inherit_attrs #1188

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
17 changes: 14 additions & 3 deletions WrightTools/data/_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1891,7 +1891,14 @@ def smooth(self, factors, channel=None, verbose=True) -> "Data":
print("smoothed data")

def split(
self, expression, positions, *, units=None, parent=None, verbose=True
self,
expression,
positions,
*,
units=None,
parent=None,
inherit_attrs=False,
verbose=True,
) -> wt_collection.Collection:
"""
Split the data object along a given expression, in units.
Expand Down Expand Up @@ -1928,7 +1935,7 @@ def split(
# axis ------------------------------------------------------------------------------------
old_expr = self.axis_expressions
old_units = self.units
out = wt_collection.Collection(name="split", parent=parent)
out = wt_collection.Collection(name=f"{self.natural_name}_split", parent=parent)
if isinstance(expression, int):
if units is None:
units = self._axes[expression].units
Expand Down Expand Up @@ -1962,7 +1969,7 @@ def split(
omasks.append(None)
cuts.append(None)
for i in range(len(positions) - 1):
out.create_data("split%03i" % i)
out.create_data(f"{self.natural_name}_{i:0>3}")

for var in self.variables:
for i, (imask, omask, cut) in enumerate(zip(masks, omasks, cuts)):
Expand Down Expand Up @@ -2037,6 +2044,10 @@ def split(
for ax, u in zip(self.axes, old_units):
ax.convert(u)

if inherit_attrs:
for d in out.values():
{d.attrs[k]: self.attrs[k] for k in self.attrs.keys() if k not in d.attrs.keys()}

return out

def transform(self, *axes, verbose=True):
Expand Down
2 changes: 1 addition & 1 deletion tests/data/split.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_split_parent():
a = wt.data.from_PyCMDS(p)
parent = wt.Collection()
split = a.split(1, [1500], parent=parent)
assert "split" in parent
assert f"{a.natural_name}_split" in parent
assert split.filepath == parent.filepath
assert len(split) == 2
a.close()
Expand Down
Loading