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

A few improvements to slicing of a catalog. #543

Merged
merged 4 commits into from Dec 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions nbodykit/base/catalog.py
Expand Up @@ -281,8 +281,9 @@ def _get_slice(self, index):
if index is Ellipsis:
return self
elif isinstance(index, slice):
start, stop, stride = index.indices(self.size)
size = (stop - start) // stride
start, stop, step = index.indices(self.size)
# from https://stackoverflow.com/a/36188683
size = max(0, (stop - start + (step - (1 if step > 0 else -1))) // step)
else:
# compute the index slice if needed and get the size
index = CatalogSourceBase.make_column(index)
Expand All @@ -302,7 +303,12 @@ def _get_slice(self, index):
size = len(index)

# initialize subset Source of right size
subset_data = {col:self[col][index] for col in self}
subset_data = {col:self[col][index] for col in self if not self[col].is_default}
if size <= 0.51 * self.size:
# if the subsample ratio is substential, then always make
# a copy to decouple from the original data
subset_data = {col:subset_data[col].map_blocks(numpy.copy) for col in subset_data}

cls = self.__class__ if self.base is None else self.base.__class__
toret = cls._from_columns(size, self.comm, **subset_data)

Expand Down
2 changes: 1 addition & 1 deletion nbodykit/transform.py
Expand Up @@ -107,7 +107,7 @@ def ConstantArray(value, size, chunks=100000):
"""
ele = numpy.array(value)
toret = numpy.lib.stride_tricks.as_strided(ele, [size] + list(ele.shape), [0] + list(ele.strides))
return da.from_array(toret, chunks=chunks)
return da.from_array(toret, chunks=chunks, name=False)


def CartesianToEquatorial(pos, observer=[0,0,0]):
Expand Down