Skip to content

Commit

Permalink
life is ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
ljwolf committed Dec 14, 2023
1 parent de59347 commit 2c04acf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
14 changes: 9 additions & 5 deletions libpysal/graph/_matching.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import warnings

import numpy
from ._utils import _validate_geometry_input
from scipy import spatial

from ._utils import _validate_geometry_input

_VALID_GEOMETRY_TYPES = ["Point"]


Expand All @@ -15,6 +16,7 @@ def _spatial_matching(
solver=None,
return_mip=False,
allow_partial_match=False,
**metric_kwargs,
):
"""
Match locations in one dataset to at least `n_matches`
Expand All @@ -23,10 +25,10 @@ def _spatial_matching(
Letting d_{ij} be
minimize \sum_i^n \sum_j^n d_{ij}m_{ij}
minimize \\sum_i^n \\sum_j^n d_{ij}m_{ij}
subject to
\sum_j^n m_{ij} >= k \forall i
m_{ij} \in {0,1} forall ij
\\sum_j^n m_{ij} >= k \forall i
m_{ij} \\in {0,1} forall ij
Paramters
Expand Down Expand Up @@ -83,7 +85,9 @@ def _spatial_matching(
x, ids=None, valid_geometry_types=_VALID_GEOMETRY_TYPES
)
y_ids = x_ids
D = spatial.distance.squareform(spatial.distance.pdist(x, metric=metric))
D = spatial.distance.squareform(
spatial.distance.pdist(x, metric=metric, **metric_kwargs)
)
match_between = False

n_targets, n_sources = D.shape
Expand Down
11 changes: 9 additions & 2 deletions libpysal/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,14 +718,21 @@ def build_knn(cls, data, k, metric="euclidean", p=2, coincident="raise"):

@classmethod
def build_spatial_matches(
cls, data, k, metric="euclidean", p=2, solver=None, allow_partial_match=False
cls,
data,
k,
metric="euclidean",
solver=None,
allow_partial_match=False,
**metric_kwargs,
):
head, tail, weight = _spatial_matching(
x=data,
metric=metric,
n_matches=k,
solver=None,
solver=solver,
allow_partial_match=allow_partial_match,
**metric_kwargs
)
# ids need to be addressed here, rather than in the matching
# because x and y can have different id sets. It's only
Expand Down
4 changes: 1 addition & 3 deletions libpysal/graph/tests/test_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
import geodatasets
import geopandas
import numpy as np
import pandas as pd
from scipy import sparse
import pytest
import shapely

from libpysal.graph._matching import _spatial_matching
from libpysal.graph.base import Graph

Expand Down

0 comments on commit 2c04acf

Please sign in to comment.