Skip to content

Commit

Permalink
WIP mean
Browse files Browse the repository at this point in the history
  • Loading branch information
PProfizi committed Mar 13, 2023
1 parent e9af80f commit 5bc0d0c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
16 changes: 16 additions & 0 deletions examples/01-Detailed-Examples/06-compute-statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,19 @@

minimum = displacement.min(axis="set_ids")
print(minimum)

###############################################################################
# Compute the average displacement value for each component at each time-step
# ---------------------------------------------------------------------------

mean = displacement.mean()
print("Mean over mesh")
print(mean)

###############################################################################
# Compute the average displacement for each node and component across time
# ------------------------------------------------------------------------

mean = displacement.mean(axis=1)
print("Mean over time")
print(mean)
80 changes: 77 additions & 3 deletions src/ansys/dpf/post/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def __init__(
self._last_display_max_colwidth = display_max_colwidth

self._last_minmax: dict = {"axis": None, "min": None, "max": None}
self._last_mean: dict = {"axis": None, "mean": None}

@property
def columns(self) -> MultiIndex:
Expand Down Expand Up @@ -836,7 +837,7 @@ def animate(
save_as=save_as, deform_by=deform_by, scale_factor=scale_factor, **kwargs
)

def min(self, axis: Union[int, str, None] = 0) -> Union[DataFrame, float]:
def min(self, axis: Union[int, str, None] = 0) -> DataFrame:
"""Return the minimum value over the requested axis.
Parameters
Expand All @@ -850,7 +851,7 @@ def min(self, axis: Union[int, str, None] = 0) -> Union[DataFrame, float]:
self._query_min_max(axis)
return self._last_minmax["min"]

def max(self, axis: Union[int, str, None] = 0) -> Union[DataFrame, float]:
def max(self, axis: Union[int, str, None] = 0) -> DataFrame:
"""Return the maximum value over the requested axis.
Parameters
Expand All @@ -864,6 +865,20 @@ def max(self, axis: Union[int, str, None] = 0) -> Union[DataFrame, float]:
self._query_min_max(axis)
return self._last_minmax["max"]

def mean(self, axis: Union[int, str, None] = 0) -> DataFrame:
"""Return the mean value over the requested axis.
Parameters
----------
axis:
Axis to perform average across.
Defaults to the MeshIndex (0).
Can also be the SetIndex (1).
"""
self._query_mean(axis)
return self._last_mean["mean"]

def _query_min_max(self, axis: Union[int, str, None]) -> None:
"""Create a DPF workflow based on the query arguments for min/max."""
# Translate None query to empty dict
Expand Down Expand Up @@ -932,7 +947,11 @@ def _query_min_max(self, axis: Union[int, str, None]) -> None:

index = self.index
columns = MultiIndex(
indexes=[c for c in self.columns if c != self.columns.set_ids]
indexes=[
c
for c in self.columns
if c != getattr(self.columns, ref_labels.set_ids)
]
)

min_fc = wf.get_output("min", dpf.types.fields_container)
Expand All @@ -949,3 +968,58 @@ def _query_min_max(self, axis: Union[int, str, None]) -> None:
columns=columns,
)
self._last_minmax["axis"] = axis

def _query_mean(self, axis: Union[int, str, None]) -> None:
"""Create a DPF workflow based on the query arguments for mean."""
# Translate None query to empty dict
if axis in [None, 0, self.index.mesh_index.name]:
axis = 0
elif axis in [1, ref_labels.set_ids]:
axis = 1
else:
raise ValueError(f"'{axis}' is not an available axis value.")
# If same query as last and last is not None, do not change
if self._last_mean["axis"] == axis and not self._last_mean["axis"] is None:
return
# If in need of an update, create the appropriate workflow
wf = dpf.Workflow(server=self._fc._server)
wf.progress_bar = False

# If over mesh
if axis == 0:
sum_op = dpf.operators.math.accumulate_fc(
fields_container=self._fc,
server=self._fc._server,
)

mean_fc = sum_op.outputs.fields_container()

index = MultiIndex(
indexes=[i for i in self.index if i != self.index.mesh_index]
)
columns = self.columns

# If over time
else:
average_over_label_op = dpf.operators.math.average_over_label_fc(
fields_container=self._fc,
server=self._fc._server,
)

mean_fc = average_over_label_op.outputs.field()

index = self.index
columns = MultiIndex(
indexes=[
c
for c in self.columns
if c != getattr(self.columns, ref_labels.set_ids)
]
)

self._last_mean["mean"] = DataFrame(
data=mean_fc,
index=index,
columns=columns,
)
self._last_mean["axis"] = axis

0 comments on commit 5bc0d0c

Please sign in to comment.