Skip to content

Commit

Permalink
Break out all measurement functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jakirkham committed Dec 8, 2016
1 parent 1c40c9b commit c05a7ee
Show file tree
Hide file tree
Showing 3 changed files with 314 additions and 103 deletions.
107 changes: 4 additions & 103 deletions kenjutsu/core.py
Expand Up @@ -26,118 +26,19 @@


import itertools
import numbers
import operator
import math
import warnings

import kenjutsu.format
import kenjutsu.measure


reformat_slice = kenjutsu.format.reformat_slice
reformat_slices = kenjutsu.format.reformat_slices


class UnknownSliceLengthException(Exception):
"""
Raised if a slice does not have a known length.
"""

pass


def len_slice(a_slice, a_length=None):
"""
Determines how many elements a slice will contain.
Raises:
UnknownSliceLengthException: Will raise an exception if
a_slice.stop and a_length is None.
Args:
a_slice(slice): a slice to reformat.
a_length(int): a length to fill for stopping if not
provided.
Returns:
(slice): a new slice with as many values filled in as
possible.
Examples:
>>> len_slice(slice(2, None), 10)
8
>>> len_slice(slice(2, 6))
4
"""

if isinstance(a_slice, numbers.Integral):
raise TypeError(
"An integral index does not provide an object with a length."
)

new_slice = reformat_slice(a_slice, a_length)

new_slice_size = 0
if isinstance(new_slice, slice):
if new_slice.stop is None:
if new_slice.step > 0:
raise UnknownSliceLengthException(
"Cannot determine slice length without a defined end"
" point. The reformatted slice was %s." % repr(new_slice)
)
else:
new_slice = slice(new_slice.start, -1, new_slice.step)

new_slice_diff = float(new_slice.stop - new_slice.start)

new_slice_size = int(math.ceil(new_slice_diff / new_slice.step))
else:
new_slice_size = len(new_slice)

return(new_slice_size)


def len_slices(slices, lengths=None):
"""
Takes a tuple of slices and reformats them to fill in as many undefined
values as possible.
Args:
slices(tuple(slice)): a tuple of slices to reformat.
lengths(tuple(int)): a tuple of lengths to fill.
Returns:
(slice): a tuple of slices with all default
values filled if possible.
Examples:
>>> len_slices(
... (
... slice(None),
... slice(3, None),
... slice(None, 5),
... slice(None, None, 2)
... ),
... (10, 13, 15, 20)
... )
(10, 10, 5, 10)
"""

new_slices = reformat_slices(slices, lengths)

lens = []

for each_slice in new_slices:
if not isinstance(each_slice, numbers.Integral):
lens.append(len_slice(each_slice))

lens = tuple(lens)

return(lens)
UnknownSliceLengthException = kenjutsu.measure.UnknownSliceLengthException
len_slice = kenjutsu.measure.len_slice
len_slices = kenjutsu.measure.len_slices


def split_blocks(space_shape, block_shape, block_halo=None):
Expand Down
108 changes: 108 additions & 0 deletions kenjutsu/measure.py
@@ -1,4 +1,112 @@
from __future__ import absolute_import

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


import numbers
import math

import kenjutsu.format


class UnknownSliceLengthException(Exception):
"""
Raised if a slice does not have a known length.
"""

pass


def len_slice(a_slice, a_length=None):
"""
Determines how many elements a slice will contain.
Raises:
UnknownSliceLengthException: Will raise an exception if
a_slice.stop and a_length is None.
Args:
a_slice(slice): a slice to reformat.
a_length(int): a length to fill for stopping if not
provided.
Returns:
(slice): a new slice with as many values filled in as
possible.
Examples:
>>> len_slice(slice(2, None), 10)
8
>>> len_slice(slice(2, 6))
4
"""

if isinstance(a_slice, numbers.Integral):
raise TypeError(
"An integral index does not provide an object with a length."
)

new_slice = kenjutsu.format.reformat_slice(a_slice, a_length)

new_slice_size = 0
if isinstance(new_slice, slice):
if new_slice.stop is None:
if new_slice.step > 0:
raise UnknownSliceLengthException(
"Cannot determine slice length without a defined end"
" point. The reformatted slice was %s." % repr(new_slice)
)
else:
new_slice = slice(new_slice.start, -1, new_slice.step)

new_slice_diff = float(new_slice.stop - new_slice.start)

new_slice_size = int(math.ceil(new_slice_diff / new_slice.step))
else:
new_slice_size = len(new_slice)

return(new_slice_size)


def len_slices(slices, lengths=None):
"""
Takes a tuple of slices and reformats them to fill in as many undefined
values as possible.
Args:
slices(tuple(slice)): a tuple of slices to reformat.
lengths(tuple(int)): a tuple of lengths to fill.
Returns:
(slice): a tuple of slices with all default
values filled if possible.
Examples:
>>> len_slices(
... (
... slice(None),
... slice(3, None),
... slice(None, 5),
... slice(None, None, 2)
... ),
... (10, 13, 15, 20)
... )
(10, 10, 5, 10)
"""

new_slices = kenjutsu.format.reformat_slices(slices, lengths)

lens = []

for each_slice in new_slices:
if not isinstance(each_slice, numbers.Integral):
lens.append(len_slice(each_slice))

lens = tuple(lens)

return(lens)

0 comments on commit c05a7ee

Please sign in to comment.