How to use the libpysal.weights function in libpysal

To help you get started, we’ve selected a few libpysal examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github martinfleis / momepy / tests / test_weights.py View on Github external
def test_sw_high(self):
        first_order = libpysal.weights.Queen.from_dataframe(self.df_tessellation)
        from_sw = mm.sw_high(2, gdf=None, weights=first_order)
        from_df = mm.sw_high(2, gdf=self.df_tessellation)
        rook = mm.sw_high(2, gdf=self.df_tessellation, contiguity="rook")
        check = [133, 134, 111, 112, 113, 114, 115, 121, 125]
        assert from_sw.neighbors[0] == check
        assert from_df.neighbors[0] == check
        assert rook.neighbors[0] == check

        with pytest.raises(AttributeError):
            mm.sw_high(2, gdf=None, weights=None)

        with pytest.raises(ValueError):
            mm.sw_high(2, gdf=self.df_tessellation, contiguity="nonexistent")
github martinfleis / momepy / momepy / weights.py View on Github external
Examples
    --------
    >>> first_order = libpysal.weights.Queen.from_dataframe(geodataframe)
    >>> first_order.mean_neighbors
    5.848032564450475
    >>> fourth_order = sw_high(k=4, gdf=geodataframe)
    >>> fourth.mean_neighbors
    85.73188602442333

    """
    if weights is not None:
        first_order = weights
    elif gdf is not None:
        if contiguity == "queen":
            first_order = libpysal.weights.Queen.from_dataframe(
                gdf, ids=ids, silence_warnings=silent
            )
        elif contiguity == "rook":
            first_order = libpysal.weights.Rook.from_dataframe(
                gdf, ids=ids, silence_warnings=silent
            )
        else:
            raise ValueError(f"{contiguity} is not supported. Use 'queen' or 'rook'.")
    else:
        raise AttributeError("GeoDataFrame or spatial weights must be given.")

    if k > 1:
        id_order = first_order.id_order
        w = first_order.sparse
        wk = sum(map(lambda x: w ** x, range(2, k + 1)))
        rk, ck = wk.nonzero()
github martinfleis / momepy / momepy / sandbox / contiguity.py View on Github external
import libpysal
import geopandas as gpd

tessellation = gpd.read_file("/Users/martin/Dropbox/StrathUni/PhD/Sample Data/Prague/Tests/181122/tess_oldtown_nid.shp")

weights_queen = libpysal.weights.Queen.from_dataframe(tessellation)  # classic queen of 1st order
second_order = libpysal.weights.higher_order(weights_queen, k=2)  # second order neighbours (only)
joined_weights = libpysal.weights.w_union(weights_queen, second_order)  # joined together

weights_queen.neighbors[10]
second_order.neighbors[10]
joined_weights.neighbors[10]


def Queen_higher(dataframe, k):
    first_order = libpysal.weights.Queen.from_dataframe(dataframe)
    joined = first_order
    for i in list(range(2, k + 1)):
        i_order = libpysal.weights.higher_order(first_order, k=i)
        joined = libpysal.weights.w_union(joined, i_order)
    return joined