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

adds reshaping functions to Images and Series #343

Merged
merged 2 commits into from
Aug 2, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
adds reshaping function to Images and Series
  • Loading branch information
jwittenbach committed Jun 27, 2016
commit f62185c3e71b4d1d495b71a0cee18a292a5b6834
13 changes: 13 additions & 0 deletions test/test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,16 @@ def f(x):

assert allclose(data.map_as_series(f, chunk_size=size).toarray(), result)
assert allclose(data.map_as_series(f, chunk_size=size, value_size=4).toarray(), result)

def test_reshape_values(eng):
original = fromarray(arange(72).reshape(2, 6, 6), engine=eng)
arr = original.toarray()

reshaped = original.reshape_values(12, 3)
assert allclose(arr.reshape(2, 12, 3), reshaped.toarray())

reshaped = original.reshape_values(36)
assert allclose(arr.reshape(2, 36), reshaped.toarray())

reshaped = original.reshape_values(4, 3, 3)
assert allclose(arr.reshape(2, 4, 3, 3), reshaped.toarray())
13 changes: 13 additions & 0 deletions test/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,16 @@ def test_mean_by_window(eng):
assert allclose(test3, [2, 3, 4, 5])
test4 = data.mean_by_window(indices=[3], window=4).toarray()
assert allclose(test4, [1, 2, 3, 4])

def test_reshape_keys(eng):
original = fromarray(arange(72).reshape(6, 6, 2), engine=eng)
arr = original.toarray()

reshaped = original.reshape_keys(12, 3)
assert allclose(arr.reshape(12, 3, 2), reshaped.toarray())

reshaped = original.reshape_keys(36)
assert allclose(arr.reshape(36, 2), reshaped.toarray())

reshaped = original.reshape_keys(4, 3, 3)
assert allclose(arr.reshape(4, 3, 3, 2), reshaped.toarray())
15 changes: 15 additions & 0 deletions thunder/images/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,21 @@ def squeeze(self):
axis = tuple(range(1, len(self.shape) - 1)) if prod(self.shape[1:]) == 1 else None
return self.map(lambda x: x.squeeze(axis=axis))

def reshape_values(self, *shape):
"""
Reshape images

Parameters
----------
shape: one or more ints
New shape of images
"""
if prod(self.value_shape) != prod(shape):
raise ValueError("Total number of elements in each image must be unchanged")

newshape = (self.shape[0], ) + shape
return self._constructor(self.values.reshape(newshape)).__finalize__(self)

def max_projection(self, axis=2):
"""
Compute maximum projections of images along a dimension.
Expand Down
15 changes: 15 additions & 0 deletions thunder/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,21 @@ def min(self):
"""
return self._constructor(self.values.min(axis=self.baseaxes, keepdims=True))

def reshape_keys(self, *shape):
"""
Reshape the keys of the time series
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't explicitly mention time


Parameters
----------
shape: one or more ints
Shape of the new keys
"""
if prod(self.baseshape) != prod(shape):
raise ValueError("Total number of series elements must remain unchanged")

newshape = shape + (self.shape[-1], )
return self._constructor(self.values.reshape(newshape)).__finalize__(self)
Copy link
Member

@freeman-lab freeman-lab Aug 1, 2016

Choose a reason for hiding this comment

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

should be self.keys.reshape right?

Copy link
Member

Choose a reason for hiding this comment

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

oh sorry, nevermind, this is correct, i was confused


def between(self, left, right):
"""
Select subset of values within the given index range.
Expand Down