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
ENH: alignaxis function added #44
Changes from 5 commits
88edb37
79e474a
bc45c31
660f024
4ec94b0
26eefdd
5f7027a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,9 @@ | |
from la.farray import covMissing | ||
from la.missing import missing_marker, ismissing | ||
|
||
__all__ = ['align', 'align_raw', 'isaligned', 'union', 'intersection', | ||
'binaryop', 'add', 'sortby', 'subtract', 'multiply', 'divide', | ||
'unique', 'stack', 'panel', 'cov', 'rand', 'randn'] | ||
__all__ = ['align', 'align_axis', 'align_raw', 'isaligned', 'union', | ||
'intersection', 'binaryop', 'add', 'sortby', 'subtract', 'multiply', | ||
'divide', 'unique', 'stack', 'panel', 'cov', 'rand', 'randn'] | ||
|
||
|
||
# Alignment ----------------------------------------------------------------- | ||
|
@@ -375,6 +375,69 @@ def align_raw(lar1, lar2, join='inner', cast=True): | |
|
||
return x1, x2, label, x1isview, x2isview | ||
|
||
def align_axis(lars, axis=0, join='inner', flag=False): | ||
""" | ||
Align many larrys along potentially different axes. | ||
|
||
Parameters | ||
---------- | ||
lars : array_like | ||
A collection (list, tuple, set, array, etc.) of larrys to align. | ||
axis : {int, array_like}, optional | ||
An integer indicating which axis along which to align the larrys in | ||
`lars`, or a sequence of integers of the same length as `lars` | ||
indicating which axis to use for each entry in `lars`. | ||
join : {'inner', 'outer', 'left', 'right'} | ||
If inner, then labels present in every larry will be kept. If 'outer', | ||
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. inner --> 'inner' |
||
all labels appearing in any array are kept, and additional entries are | ||
added to larrys containing fewer labels. See la.morph() for rules on | ||
how this is done. If 'right' or 'left' then the labels of the output | ||
will match those of either the first or last entry of `lars`, | ||
respectively | ||
|
||
Returns | ||
------- | ||
(lar1, lar2, ...) : tuple | ||
Tuple of larrys, one corresponding to each entry of lars. None of the | ||
output refer to input, and the labels of the output do not refer to one | ||
another. | ||
|
||
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. This is a nice function. A couple of examples would be useful. |
||
""" | ||
# input checks and preprocessing | ||
nlar = len(lars) | ||
if isinstance(axis, int): | ||
axis = np.repeat(axis, nlar) | ||
if isinstance(axis, list) or isinstance(axis, tuple): | ||
axis = np.array(axis) | ||
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. Why make axis into an array? |
||
for j, lar in enumerate(lars): | ||
if not isinstance(lar, larry): | ||
raise ValueError("Inputs must be larry.") | ||
if axis[j] > len(lar.shape): | ||
raise ValueError("Axis out of range for input larry " + j) | ||
if join not in ['inner', 'outer', 'left', 'right']: | ||
raise ValueError("Value of `join` not recognized.") | ||
# alignment | ||
if join == 'left': | ||
label = lars[0].label[axis[0]] | ||
elif join == 'right': | ||
label = lars[-1].label[axis[-1]] | ||
else: | ||
labels = [set(lar.label[axis[j]]) for j, lar in enumerate(lars)] | ||
label = labels[0] | ||
if join == 'inner': | ||
for new_label in labels[1:]: | ||
label &= new_label | ||
elif join == 'outer': | ||
for new_label in labels[1:]: | ||
label |= new_label | ||
if join in ('inner', 'outer'): | ||
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. Is this 'if statement' needed? Can
be put in the else above? Also can you put the two statements on two different lines instead of using ';'? |
||
label = list(label); label.sort() | ||
lars_out = [] | ||
for j, lar in enumerate(lars): | ||
lab = list(label) | ||
lars_out.append(lar.morph(lab, axis[j])) | ||
return tuple(lars_out) | ||
|
||
def isaligned(lar1, lar2, axis=None): | ||
""" | ||
Return True if labels of two given larrys are aligned along specified axis. | ||
|
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.
", optional"