Skip to content

Commit

Permalink
neighborhood distributions, debugging/cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
wpbonelli committed Mar 7, 2022
1 parent c0e1051 commit 1a65447
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 441 deletions.
27 changes: 27 additions & 0 deletions cactice/grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,33 @@ def get_neighborhoods(
for j in range(0, grid.shape[1])}


def neighborhood_distribution(
grid: np.ndarray,
neighbors: Neighbors = Neighbors.CARDINAL,
exclude_zero: bool = False) -> Dict[int, Dict[int, float]]:
"""
:param grid:
:param neighbors:
:param exclude_zero:
:return:
"""

neighborhoods = get_neighborhoods(grid=grid, neighbors=neighbors, exclude_zero=exclude_zero, absolute_coords=True)
unique = list(set(np.unique(np.ravel(grid))))
if exclude_zero: unique = [c for c in unique if c != 0]
freq = {k: {kk: 0 for kk in unique} for k in unique}
for loc, neighborhood in neighborhoods.items():
k = grid[loc[0], loc[1]]
if exclude_zero and k == 0: continue
for nloc, neighbor in neighborhood.items():
nk = grid[nloc[0], nloc[1]]
if exclude_zero and nk == 0: continue
freq[k][nk] += 1

return {k: {kk: (v[kk] / sum(freq[k].values())) for kk in unique} for (k, v) in freq.items()}


def get_band(
grid: np.ndarray,
i: int,
Expand Down
1 change: 0 additions & 1 deletion cactice/knn.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import cactice.grids
from cactice.grids import get_neighborhood, get_neighborhoods, Neighbors
from cactice.distance import hamming_distance
import cactice.stats as stats


class KNN:
Expand Down
1 change: 0 additions & 1 deletion cactice/mrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from numpy.random import choice, RandomState

import cactice.grids
import cactice.stats as stats
from cactice.grids import Neighbors, get_neighborhood, get_neighborhoods

# bond interaction signature
Expand Down
4 changes: 1 addition & 3 deletions cactice/rns.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import logging
import random
from typing import List, Dict
from pprint import pprint
import logging

import numpy as np

import cactice.grids
import cactice.stats as stats
from cactice.grids import Neighbors, get_neighborhood


Expand Down
521 changes: 85 additions & 436 deletions notebooks/explore.ipynb

Large diffs are not rendered by default.

0 comments on commit 1a65447

Please sign in to comment.