Skip to content
Merged
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
21 changes: 18 additions & 3 deletions sasdata/trend.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def get_metadatum_from_path(data: SasData, metadata_path: list[str]):
current_node = current_item
raise ValueError("End of path without finding a dataset.")


@dataclass
class Trend:
data: list[SasData]
Expand Down Expand Up @@ -148,6 +147,12 @@ def get_trend_values(self, axis_name: str) -> list:
# Manual values - return as-is
return axis_config.copy() # Return copy to prevent modification

def get_trend_data_value(self, data: SasData, axis_name: str):
"""Get the value of `axis_name` for `data`. `data` is assumed to be part
of the trend, although it doesn't necessarily have to. However, it will
fail if the metadata doesn't exist on `data`."""
return get_metadatum_from_path(data, self.trend_axes[axis_name])

def add_manual_axis(self, axis_name: str, values: list):
"""Add a new manual trend axis"""
if len(values) != len(self.data):
Expand All @@ -168,6 +173,7 @@ def add_metadata_axis(self, axis_name: str, path: list[str]):

@property
def axis_names(self) -> list[str]:
"""Return all of the trend's axis names."""
return list(self.trend_axes.keys())

def is_manual_axis(self, axis_name: str) -> bool:
Expand All @@ -178,9 +184,9 @@ def is_manual_axis(self, axis_name: str) -> bool:
axis_config = self.trend_axes[axis_name]
return not (isinstance(axis_config, list) and len(axis_config) > 0 and isinstance(axis_config[0], str))

# TODO: Assumes there are at least 2 items in data. Is this reasonable to assume? Should there be error handling for
# situations where this may not be the case?
def all_axis_match(self, axis: str) -> bool:
if len(self.data) < 2:
return True
Comment on lines +188 to +189

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should this be true when self.data is empty?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Well in one sense, it is vacuously true since all the axes match if there is no data to check against.

It looks like Miguel already had a check to see if there are less than two value data remaining, but his only prints out a warning. We could change that into an error, but I'm inclined just to leave it for now.

reference_data = self.data[0]
data_axis = reference_data[axis]
for datum in self.data[1::]:
Expand Down Expand Up @@ -220,3 +226,12 @@ def interpolate(self, axis: str) -> "Trend":
new_data.append(new_datum)
new_trend = Trend(new_data, self.trend_axes)
return new_trend

class NamedTrend(Trend):
name: str

def __init__(self, data: list[SasData], trend_axes: dict[str, list[str] | list], name: str):
if name.strip() == "":
raise ValueError("A named trend cannot have an empty name.")
super().__init__(data=data, trend_axes=trend_axes)
self.name = name
Loading