This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Add combinations #1
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9876fd6
Add combinations
Jayd-1234 abe229e
Update jagged.py
Jayd-1234 c769571
Change import
Jayd-1234 9f199a6
Changes done
Jayd-1234 ace50bf
Merge branch 'master' of https://github.com/Jayd-1234/awkward-array
Jayd-1234 9de54a3
Update jagged.py
Jayd-1234 4707c2c
More refined.. Use product with argproduct
Jayd-1234 d846a56
Merge branch 'master' of https://github.com/Jayd-1234/awkward-array
Jayd-1234 8787376
Fixed a bug in product. Added product to ByteJaggedArray
Jayd-1234 c7987f6
add one test for product
Jayd-1234 8aa61a6
Implemented David's suggestions
Jayd-1234 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"python.unitTest.unittestArgs": [ | ||
"-v", | ||
"-s", | ||
"./tests", | ||
"-p", | ||
"test_*.py" | ||
], | ||
"python.unitTest.pyTestEnabled": false, | ||
"python.unitTest.nosetestsEnabled": false, | ||
"python.unitTest.unittestEnabled": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,6 +208,7 @@ def parents(self): | |
out[starts[i]:stops[i]] = i | ||
i += 1 | ||
return out | ||
|
||
|
||
def __len__(self): # length is determined by starts | ||
return len(self._starts) # data can grow by appending contents and stops before starts | ||
|
@@ -406,6 +407,78 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): | |
return None | ||
else: | ||
return JaggedArray(starts, stops, result) | ||
|
||
|
||
def argproduct(self, other): | ||
''' | ||
Performs product (combinations) of current JaggedArray with JaggedArray `other`. Return the indices of the product. | ||
|
||
Inputs: other; a JaggedArray instance. | ||
Output: JaggedArray containing thw two indices as an awkward.array.table.Table() | ||
|
||
Example usage: | ||
>>> arr1 = JaggedArray([0,1,4,4],[1,4,4,8],content=[0,1,2,3,4,5,6,7]) | ||
>>> arr2 = JaggedArray([0,1,1,4],[1,1,4,5],content=['z', 'a','b','c','d']) | ||
>>> result = arr1.argproduct(arr2) | ||
''' | ||
import awkward.array.table | ||
if not isinstance(other, JaggedArray): | ||
raise ValueError("array given isn't instance of JaggedArray; need JaggedArrays to proceed") | ||
|
||
if (len(self._starts) != len(other)): | ||
raise ValueError("Number of events in each array must be equal") | ||
|
||
starts1 = self._starts | ||
stops1 = self._stops | ||
counts1 = stops1 - starts1 | ||
|
||
starts2 = other.starts | ||
stops2 = other.stops | ||
counts2 = stops2 - starts2 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we check that counts1.shape == counts2.shape? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That won't be necessary, as we are doing shape checking for starts array. The shape of counts and starts are same. |
||
|
||
pairs_counts = numpy.zeros(len(starts1)+1, dtype=self.INDEXTYPE) | ||
pairs_counts[1:] = numpy.cumsum(counts1*counts2, dtype=self.INDEXTYPE) | ||
|
||
def parents_from_offsets(offsets): | ||
out = numpy.full(offsets[-1], -1, dtype=self.INDEXTYPE) | ||
lenstarts = len(offsets)-1 | ||
i = 0 | ||
while i < lenstarts: | ||
out[offsets[i]:offsets[i+1]] = i | ||
i += 1 | ||
return out | ||
|
||
pairs_indices = numpy.arange(pairs_counts[-1], dtype=self.INDEXTYPE) | ||
pairs_parents = parents_from_offsets(pairs_counts) | ||
pairs_parents = pairs_parents.astype(self.INDEXTYPE) | ||
|
||
left = numpy.empty_like(pairs_indices) | ||
right = numpy.empty_like(pairs_indices) | ||
|
||
left[pairs_indices] = starts1[pairs_parents[pairs_indices]] + numpy.floor((pairs_indices - pairs_counts[pairs_parents[pairs_indices]])/counts2[pairs_parents[pairs_indices]]).astype(self.INDEXTYPE) | ||
right[pairs_indices] = starts2[pairs_parents[pairs_indices]] + (pairs_indices - pairs_counts[pairs_parents[pairs_indices]]) - counts2[pairs_parents[pairs_indices]] * numpy.floor((pairs_indices - pairs_counts[pairs_parents[pairs_indices]])/counts2[pairs_parents[pairs_indices]]) | ||
|
||
return JaggedArray(pairs_counts[:-1], pairs_counts[1:], awkward.array.table.Table(pairs_indices[-1], left, right), writeable=self._writeable) | ||
|
||
def product(self, other): | ||
''' | ||
Performs product ( combinations) between two JaggedArrays and returns the resulting combined content as a JaggedArray() | ||
|
||
Inputs: other; a JaggedArray instance | ||
Output: JaggedArray containing thw two indices as an awkward.array.table.Table() | ||
|
||
Example usage: | ||
>>> arr1 = JaggedArray([0,1,4,4],[1,4,4,8],content=[0,1,2,3,4,5,6,7]) | ||
>>> arr2 = JaggedArray([0,1,1,4],[1,1,4,5],content=['z', 'a','b','c','d']) | ||
>>> result = arr1.product(arr2) | ||
''' | ||
import awkward.array.table | ||
product_indexes = self.argproduct(other) | ||
arr_list = list(product_indexes._content._content.values()) | ||
return JaggedArray(product_indexes.starts, product_indexes.stops, awkward.array.table.Table(len(arr_list[0]), self._content[arr_list[0]], other.content[arr_list[1]])) | ||
|
||
|
||
|
||
class ByteJaggedArray(JaggedArray): | ||
@classmethod | ||
|
@@ -567,3 +640,73 @@ def tojagged(self, starts=None, stops=None, copy=True, writeable=True): | |
i += 1 | ||
|
||
return JaggedArray(starts, stops, content, writeable=writeable) | ||
|
||
def argproduct(self, other): | ||
''' | ||
Performs product (combinations) of current JaggedArray with JaggedArray `other`. Return the indices of the product. | ||
|
||
Inputs: other; a JaggedArray instance. | ||
Output: JaggedArray containing thw two indices as an awkward.array.table.Table() | ||
|
||
Example usage: | ||
>>> arr1 = JaggedArray([0,1,4,4],[1,4,4,8],content=[0,1,2,3,4,5,6,7]) | ||
>>> arr2 = JaggedArray([0,1,1,4],[1,1,4,5],content=['z', 'a','b','c','d']) | ||
>>> result = arr1.argproduct(arr2) | ||
''' | ||
import awkward.array.table | ||
if not isinstance(other, JaggedArray): | ||
raise ValueError("array given isn't instance of JaggedArray; need JaggedArrays to proceed") | ||
|
||
if (len(self._starts) != len(other)): | ||
raise ValueError("Number of events in each array must be equal") | ||
|
||
starts1 = self._starts | ||
stops1 = self._stops | ||
counts1 = stops1 - starts1 | ||
|
||
starts2 = other.starts | ||
stops2 = other.stops | ||
counts2 = stops2 - starts2 | ||
|
||
|
||
pairs_counts = numpy.zeros(len(starts1)+1, dtype=self.INDEXTYPE) | ||
pairs_counts[1:] = numpy.cumsum(counts1*counts2, dtype=self.INDEXTYPE) | ||
|
||
def parents_from_offsets(offsets): | ||
out = numpy.full(offsets[-1], -1, dtype=self.INDEXTYPE) | ||
lenstarts = len(offsets)-1 | ||
i = 0 | ||
while i < lenstarts: | ||
out[offsets[i]:offsets[i+1]] = i | ||
i += 1 | ||
return out | ||
|
||
pairs_indices = numpy.arange(pairs_counts[-1], dtype=self.INDEXTYPE) | ||
pairs_parents = parents_from_offsets(pairs_counts) | ||
pairs_parents = pairs_parents.astype(self.INDEXTYPE) | ||
|
||
left = numpy.empty_like(pairs_indices) | ||
right = numpy.empty_like(pairs_indices) | ||
|
||
left[pairs_indices] = starts1[pairs_parents[pairs_indices]] + numpy.floor((pairs_indices - pairs_counts[pairs_parents[pairs_indices]])/counts2[pairs_parents[pairs_indices]]).astype(self.INDEXTYPE) | ||
right[pairs_indices] = starts2[pairs_parents[pairs_indices]] + (pairs_indices - pairs_counts[pairs_parents[pairs_indices]]) - counts2[pairs_parents[pairs_indices]] * numpy.floor((pairs_indices - pairs_counts[pairs_parents[pairs_indices]])/counts2[pairs_parents[pairs_indices]]) | ||
|
||
return JaggedArray(pairs_counts[:-1], pairs_counts[1:], awkward.array.table.Table(pairs_indices[-1], left, right), writeable=self._writeable) | ||
|
||
def product(self, other): | ||
''' | ||
Performs product ( combinations) between two JaggedArrays and returns the resulting combined content as a JaggedArray() | ||
|
||
Inputs: other; a JaggedArray instance | ||
Output: JaggedArray containing thw two indices as an awkward.array.table.Table() | ||
|
||
Example usage: | ||
>>> arr1 = JaggedArray([0,1,4,4],[1,4,4,8],content=[0,1,2,3,4,5,6,7]) | ||
>>> arr2 = JaggedArray([0,1,1,4],[1,1,4,5],content=['z', 'a','b','c','d']) | ||
>>> result = arr1.product(arr2) | ||
''' | ||
import awkward.array.table | ||
product_indexes = self.argproduct(other) | ||
arr_list = list(product_indexes._content._content.values()) | ||
|
||
return JaggedArray(product_indexes.starts, product_indexes.stops, awkward.array.table.Table(len(arr_list[0]), self._content[arr_list[0]], other.content[arr_list[1]])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a small bit of documentation about what this function does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check it out. I have added simple docstrings describing the usage