Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jakirkham committed Feb 17, 2017
1 parent 36fa291 commit 60f8cd9
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 86 deletions.
66 changes: 66 additions & 0 deletions kenjutsu/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


import collections
import itertools
import numbers


Expand Down Expand Up @@ -280,3 +281,68 @@ def reformat_slices(slices, lengths=None):
new_slices = tuple(new_slices)

return(new_slices)




def split_indices(slices):
"""
Splits slices with multiple indices into multiple splices.
Support of slices with a sequence of indices is varied. Some
libraries like NumPy support them without issues. Other libraries
like h5py support them as long as they are in sequential order.
In still other libraries support is non-existent. However, in
all those cases normally a single index is permissible. This
converts slices with multiple indices into a list of slices with
a single index each. While this still leaves it up to the user
to iterate over these and combine the results in some sensible
way, it is better than just getting a failure and should extend
well to a variety of cases.
Args:
slices(tuple(slice)): a tuple of slices to split
keepdims(bool): whether to keep dimensions with
multiple indices or not.
Returns:
(list(tuple(slice))): a list of a tuple of slices
Examples:
>>> split_indices(
... (
... 3,
... Ellipsis,
... [0, 1, 2],
... slice(2, 5),
... slice(4, 6, 2)
... )
... ) # doctest: +NORMALIZE_WHITESPACE
[(3, Ellipsis, slice(0, 1, 1), slice(2, 5, 1), slice(4, 6, 2)),
(3, Ellipsis, slice(1, 2, 1), slice(2, 5, 1), slice(4, 6, 2)),
(3, Ellipsis, slice(2, 3, 1), slice(2, 5, 1), slice(4, 6, 2))]
"""

ref_slices = reformat_slices(slices)

mtx_slices = []
for each_dim_slice in ref_slices:
if each_dim_slice is Ellipsis:
mtx_slices.append([each_dim_slice])
elif isinstance(each_dim_slice, numbers.Integral):
mtx_slices.append([each_dim_slice])
elif isinstance(each_dim_slice, slice):
mtx_slices.append([each_dim_slice])
elif isinstance(each_dim_slice, collections.Sequence):
new_slice = []
for i in each_dim_slice:
new_slice.append(index_to_slice(i))
mtx_slices.append(new_slice)

result_slices = []
for each_dim_slice in itertools.product(*mtx_slices):
result_slices.append(tuple(each_dim_slice))

return result_slices

72 changes: 0 additions & 72 deletions kenjutsu/operators.py
Original file line number Diff line number Diff line change
@@ -1,76 +1,4 @@
from __future__ import absolute_import

__author__ = "John Kirkham <kirkhamj@janelia.hhmi.org>"
__date__ = "$Dec 08, 2016 16:20:14 GMT-0500$"

import collections
import itertools
import numbers

try:
from future_builtins import map
except ImportError:
pass

import kenjutsu.format


def split_indices(slices):
"""
Splits slices with multiple indices into multiple splices.
Support of slices with a sequence of indices is varied. Some
libraries like NumPy support them without issues. Other libraries
like h5py support them as long as they are in sequential order.
In still other libraries support is non-existent. However, in
all those cases normally a single index is permissible. This
converts slices with multiple indices into a list of slices with
a single index each. While this still leaves it up to the user
to iterate over these and combine the results in some sensible
way, it is better than just getting a failure and should extend
well to a variety of cases.
Args:
slices(tuple(slice)): a tuple of slices to split
keepdims(bool): whether to keep dimensions with
multiple indices or not.
Returns:
(list(tuple(slice))): a list of a tuple of slices
Examples:
>>> split_indices(
... (
... 3,
... Ellipsis,
... [0, 1, 2],
... slice(2, 5),
... slice(4, 6, 2)
... )
... ) # doctest: +NORMALIZE_WHITESPACE
[(3, Ellipsis, slice(0, 1, 1), slice(2, 5, 1), slice(4, 6, 2)),
(3, Ellipsis, slice(1, 2, 1), slice(2, 5, 1), slice(4, 6, 2)),
(3, Ellipsis, slice(2, 3, 1), slice(2, 5, 1), slice(4, 6, 2))]
"""

ref_slices = kenjutsu.format.reformat_slices(slices)

mtx_slices = []
for each_dim_slice in ref_slices:
if each_dim_slice is Ellipsis:
mtx_slices.append([each_dim_slice])
elif isinstance(each_dim_slice, numbers.Integral):
mtx_slices.append([each_dim_slice])
elif isinstance(each_dim_slice, slice):
mtx_slices.append([each_dim_slice])
elif isinstance(each_dim_slice, collections.Sequence):
mtx_slices.append(
list(map(kenjutsu.format.index_to_slice, each_dim_slice))
)

result_slices = []
for each_dim_slice in itertools.product(*mtx_slices):
result_slices.append(tuple(each_dim_slice))

return result_slices
3 changes: 3 additions & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,6 @@ def test_reformat_slices(self):
slice(0, 1, 1)
)
)

def test_split_indices(self):
pass
14 changes: 0 additions & 14 deletions tests/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,4 @@
__date__ = "$Dec 08, 2016 16:20:14 GMT-0500$"


import doctest
import unittest

from kenjutsu import operators


# Load doctests from `operators`.
def load_tests(loader, tests, ignore):
tests.addTests(doctest.DocTestSuite(operators))
return tests


class TestOperators(unittest.TestCase):
def test_split_indices(self):
pass

0 comments on commit 60f8cd9

Please sign in to comment.