Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
kianorr committed Nov 11, 2023
1 parent 2589f8e commit c2372ea
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
43 changes: 41 additions & 2 deletions desc/compute/data_index.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
"""data_index contains all the quantities calculated by the compute functions."""
from desc.utils import find_permutations
import functools
import itertools

import numpy as np


def find_permutations(name, separator="_"):
"""Finds permutations of quantity names for aliases."""
split_name = name.split(separator)
original_permutation = split_name[-1]

new_permutations = list(itertools.permutations(original_permutation))
aliases = [
"".join(split_name[:-1]) + separator + "".join(perm)
for perm in new_permutations
]
aliases = np.unique(aliases)
aliases = np.delete(aliases, np.where(aliases == name))

return aliases


def assign_alias_data(
alias, primary, base_class, data_index, params, profiles, transforms, data, **kwargs
):
"""Assigns primary data to alias."""
data = data_index[base_class][primary]["fun"](

Check warning on line 28 in desc/compute/data_index.py

View check run for this annotation

Codecov / codecov/patch

desc/compute/data_index.py#L28

Added line #L28 was not covered by tests
params, transforms, profiles, data, **kwargs
)
data[alias] = data[primary].copy()
return data

Check warning on line 32 in desc/compute/data_index.py

View check run for this annotation

Codecov / codecov/patch

desc/compute/data_index.py#L31-L32

Added lines #L31 - L32 were not covered by tests


def register_compute_fun(
Expand Down Expand Up @@ -96,7 +126,16 @@ def _decorator(func):
)
data_index[base_class][name] = d.copy()
for alias in aliases:
data_index[base_class][alias] = data_index[base_class][name]

data_index[base_class][alias] = d.copy()
# assigns alias compute func to generator to be used later
data_index[base_class][alias]["fun"] = functools.partial(
assign_alias_data,
alias=alias,
primary=name,
base_class=base_class,
data_index=data_index,
)

flag = True
if not flag:
Expand Down
3 changes: 2 additions & 1 deletion desc/compute/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ def _compute(
)
# now compute the quantity
data = data_index[parameterization][name]["fun"](
params, transforms, profiles, data, **kwargs
params=params, transforms=transforms, profiles=profiles, data=data, **kwargs
)

return data


Expand Down
16 changes: 0 additions & 16 deletions desc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,22 +196,6 @@ def __getitem__(self, index):
Index = _Indexable()


def find_permutations(name, separator="_"):
"""Finds permutations of quantity names for aliases."""
split_name = name.split(separator)
original_permutation = split_name[-1]

new_permutations = list(permutations(original_permutation))
aliases = [
"".join(split_name[:-1]) + separator + "".join(perm)
for perm in new_permutations
]
aliases = np.unique(aliases)
aliases = np.delete(aliases, np.where(aliases == name))

return aliases


def equals(a, b):
"""Compare (possibly nested) objects, such as dicts and lists.
Expand Down

0 comments on commit c2372ea

Please sign in to comment.