-
Notifications
You must be signed in to change notification settings - Fork 1
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
Allow compatible mapped values in __setitem__ #7
Changes from all commits
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 |
---|---|---|
|
@@ -735,15 +735,72 @@ def test_setitem_with_mapped_operands_raises_on_conflict() -> None: | |
|
||
def test_setitem_currently_does_not_allow_compatible_indices() -> None: | ||
g = nx.DiGraph() | ||
g.add_edge('a', 'c') | ||
g.add_edge('b', 'c') | ||
g.add_edge('a', 'b') | ||
g.add_edge('c', 'd') | ||
|
||
graph = cb.Graph(g) | ||
mapped = graph.map({'a': [1, 2, 3], 'b': [11, 12, 13]}).reduce('c', name='d') | ||
mapped1 = graph.map({'a': [1, 2, 3]}) | ||
mapped2 = graph['d'].map({'c': [11, 12, 13]}).reduce('d', name='e') | ||
# Note: This is a limitation of the current implementation. We could check if the | ||
# indices are identical and allow this. For simplicity we currently do not. | ||
with pytest.raises(ValueError, match="Conflicting new index names"): | ||
mapped['x'] = mapped['d'] | ||
mapped1['x'] = mapped2 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'node_values', | ||
[ | ||
{'a': [1, 2, 3]}, | ||
{'a': [1, 2, 3], 'b': [11, 12, 13]}, | ||
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. It seems that 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. Done! |
||
{'a': np.array([1, 2, 3])}, | ||
{'a': np.array([1, 2, 3]), 'b': np.array([11, 12, 13])}, | ||
pd.DataFrame({'a': [1, 2, 3]}), | ||
pd.DataFrame({'a': [1, 2, 3], 'b': [11, 12, 13]}), | ||
{'a': sc.array(dims=['x'], values=[1, 2, 3])}, | ||
{ | ||
'a': sc.array(dims=['x'], values=[1, 2, 3]), | ||
'b': sc.array(dims=['x'], values=[11, 12, 13]), | ||
}, | ||
Comment on lines
+760
to
+763
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. I think I'm a little confused because the name above is a I realize this is besides the point of the current PR... 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.
Using a 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. Ah ok, I see now |
||
{'a': xr.DataArray(dims=('x',), data=[1, 2, 3])}, | ||
{ | ||
'a': xr.DataArray(dims=('x',), data=[1, 2, 3]), | ||
'b': xr.DataArray(dims=('x',), data=[11, 12, 13]), | ||
}, | ||
], | ||
) | ||
def test_setitem_allows_compatible_node_values(node_values) -> None: | ||
g = nx.DiGraph() | ||
g.add_edge('a', 'c') | ||
g.add_edge('b', 'c') | ||
|
||
graph = cb.Graph(g) | ||
mapped = graph.map(node_values).reduce('c', name='d') | ||
mapped['x'] = mapped['d'] | ||
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. If I understand correctly, the {
'a': sc.array(dims=['x'], values=[1, 2, 3]),
'b': sc.array(dims=['x'], values=[11, 12, 13]),
} above. If so, can we use a different name here? 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. I was asking because at first it confused me, as I thought they were related. |
||
assert len(mapped.index_names) == 1 | ||
|
||
|
||
def test_setitem_raises_if_node_values_equivalent_but_of_different_type() -> None: | ||
g = nx.DiGraph() | ||
g.add_edge('a', 'b') | ||
graph = cb.Graph(g) | ||
mapped1 = graph.map({'a': [1, 2]}).reduce('b', name='d') | ||
mapped2 = graph.map({'a': np.array([1, 2])}).reduce('b', name='d') | ||
# One could imagine treating this as equivalent, but we are strict in the | ||
# comparison. | ||
with pytest.raises(ValueError, match="Conflicting new index names"): | ||
mapped1['x'] = mapped2['d'] | ||
|
||
|
||
def test_setitem_raises_if_node_values_incompatible() -> None: | ||
g = nx.DiGraph() | ||
g.add_edge('a', 'b') | ||
graph = cb.Graph(g) | ||
mapped1 = graph.map({'a': [1, 2]}).reduce('b', name='d') | ||
mapped2 = graph.map({'a': sc.array(dims=('x',), values=[1, 2])}).reduce( | ||
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. Not sure I understand why this is different from the above? In 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. Yes, but it actually raises for different reasons, this test is to ensure that both code paths work. 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. I guess my question was why is it raising for a different reason, I would have expected to raise with the same reason. 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. It is an artifact of the usual problem of having two checks in a particular order, so depending on the exact properties of the input you get one exception or the other. |
||
'b', name='d' | ||
) | ||
with pytest.raises(ValueError, match="has already been mapped"): | ||
mapped1['x'] = mapped2['d'] | ||
|
||
|
||
def test_setitem_does_currently_not_support_slice_assignment() -> None: | ||
|
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.
A bit awkward that we have to pass the module as an arg everywhere...
Was it to avoid importing
scipp
in multiple places?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.
Yes, making the code cleaner.