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

add from_adjacency and handle sorting #555

Merged
merged 8 commits into from
Sep 6, 2023

Conversation

knaaptime
Copy link
Member

for discussion

@knaaptime
Copy link
Member Author

knaaptime commented Sep 1, 2023

failures are expected because the 'known order' is different. So the failures should be specific to the placement of neighbor values, but i left the tests unchanged for demonstration

I'm not sure why 3.8 minimal is failing sparse tests

@knaaptime knaaptime marked this pull request as ready for review September 2, 2023 16:43
libpysal/graph/base.py Outdated Show resolved Hide resolved
Comment on lines +805 to +813
adj = (
self.adjacency.reset_index()
.set_index(["focal", "neighbor"])["weight"]
.astype("Sparse[float]")
.reindex(ids, level=0)
.reindex(ids, level=1)
)
# pivot to COO sparse matrix and cast to array
return sparse.coo_array(adj.sparse.to_coo(sort_labels=True)[0])
Copy link
Member

Choose a reason for hiding this comment

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

If we use the _id2i here anyway, what is the benefit of reindexing, casting to pandas sparse, to scipy sparse matrix and then to a sparse array over the direct constructor we had here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this is faster

Copy link
Member Author

Choose a reason for hiding this comment

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

Also I’m not sure we’re technically need id2i anymore because we could do self.adjacency.focal.unique() to get the ids which should always be in the correct order, but I think it’s still useful to keep the order stashed as a private attribute

Copy link
Member Author

@knaaptime knaaptime Sep 4, 2023

Choose a reason for hiding this comment

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

i also think that whenever possible we should rely on indexing rather than mapping by hand, as indices are really the benefit that the adjlist-backed implementation provides. Frankly, the multiindexed series is what we should really use under the hood at all times. That's what i sketched in the original implementation and im not sure why we've diverged. If we had the adjacency stored as multiindex sparse on init, all we'd need to do here is .to_coo

Copy link
Member

Choose a reason for hiding this comment

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

We originally started with a Series with a MultiIndex but there was something funny about sorting messing up sparse in some cases. But we may have prematurely dropped the idea and resorted to DataFrame. I also think that a Series would be optimal data structure. Also from performance perspective.

Given how much the code has changed since then and that we do this sorting now, we may want to revise that and go back to Series. It should not be a complex change. We need to adapt from_arrays and init and maybe throw in a couple of reset_index here and there.

Copy link
Member

Choose a reason for hiding this comment

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

All for revisiting that. We jettisoned it because of sorting issues, but it's the correct data structure and we started out intending to keep @knaaptime's core approach

Copy link
Member

Choose a reason for hiding this comment

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

I think that the requirement to keep a MultiIndex lexicographically sorted to do anything performant was the main issue. pandas does not like indices that are not sorted (see this section in their docs). In some cases, they even raise for unsorted indices, though that may not be our case.

While we do have indices sorted, they are sorted according to the order of appearance in the original dataframe, which is not sorted in pandas terms. So while I would like to store adjacency as a mutli-indexed series, I am not sure it is possible and if it is possible, if it is actually a good idea.

Copy link
Member

Choose a reason for hiding this comment

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

That said, I am also happy to give it a try so we're not just guessing here.

Comment on lines -1099 to +1152
ids = numpy.unique(numpy.hstack((heads, tails)))
ids = np.unique(np.hstack((heads, tails)))
Copy link
Member

Choose a reason for hiding this comment

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

Given the requirement for the presence of every observation in focal, at least as a self-loop, the hstack is not needed anymore.

@martinfleis martinfleis merged commit a5d7be0 into pysal:geographs Sep 6, 2023
0 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants