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

Refactor dimension #112

Merged
merged 8 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[tool:pytest]
python_classes = Test Describe
python_files = test_*.py
python_functions = test_ it_ they_ but_ and_it_
testpaths =
tests

Expand Down
47 changes: 22 additions & 25 deletions src/cr/cube/crunch_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ def univariate_ca_main_axis(self):
def valid_indices_with_selections(self, include_missing=False):
"""Get all valid indices (including MR selections)."""
return [
dim.valid_indices(include_missing)
dim.element_indices(include_missing)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I renamed Dimension.valid_indices() to .element_indices() since it can be used to get both all and only valid indices. This eliminates a jarring contradiction in the naming.

for dim in self.all_dimensions
]

Expand Down Expand Up @@ -1033,7 +1033,7 @@ def _fix_shape(self, array, fix_valids=False):
0 if dim.is_mr_selections(self.all_dimensions) else slice(None)
for dim, n in zip(self.all_dimensions, array.shape)
) if not fix_valids else np.ix_(*[
dim.valid_indices(False) if n > 1 else [0]
dim.element_indices(include_missing=False) if n > 1 else [0]
for dim, n in zip(self.all_dimensions, array.shape)
])
array = array[display_ind]
Expand Down Expand Up @@ -1065,32 +1065,29 @@ def _inserted_dim_inds(self, transform_dims, axis):
return np.array(inserted_inds[dim_ind] if len(inserted_inds) else [])

def _insertions(self, result, dimension, dimension_index):
insertions = []
"""Return list of (idx, sum) pairs representing subtotals.

for indices in dimension.hs_indices:
ind_subtotal_elements = np.array(indices['inds'])

if indices['anchor_ind'] == 'top':
ind_insertion = -1
elif indices['anchor_ind'] == 'bottom':
ind_insertion = result.shape[dimension_index] - 1
else:
ind_insertion = indices['anchor_ind']

ind = tuple(
[slice(None) for _ in range(dimension_index)] +
[ind_subtotal_elements]
)
axis = dimension_index
*idx* is the int offset at which to insert the ndarray subtotal
in *sum*.
"""

# no indices are provided (should never get here)
if len(indices['inds']) == 0:
value = 0
else:
value = np.sum(result[ind], axis=axis)
insertions.append((ind_insertion, value))
def iter_insertions():
for anchor_idx, addend_idxs in dimension.hs_indices:
insertion_idx = (
-1 if anchor_idx == 'top' else
result.shape[dimension_index] - 1 if anchor_idx == 'bottom'
else anchor_idx
)
addend_fancy_idx = tuple(
[slice(None) for _ in range(dimension_index)] +
[np.array(addend_idxs)]
)
yield (
insertion_idx,
np.sum(result[addend_fancy_idx], axis=dimension_index)
)

return insertions
return [insertion for insertion in iter_insertions()]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I extracted the body of the code into the local method iter_insertions() to make this simpler to understand.


def _intersperse_hs_in_std_res(self, hs_dims, res):
for dim, inds in enumerate(self.inserted_hs_indices()):
Expand Down
Loading