-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
include src/rsdiv/embedding/*.pkl | ||
include src/rsdiv/encoding/*.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from .dataset import * | ||
from .embedding import * | ||
from .encoding import * | ||
from .evaluation import * | ||
from .recommenders import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
from abc import ABCMeta | ||
from pathlib import Path | ||
from typing import Dict | ||
|
||
import numpy as np | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import imp | ||
|
||
from .base import BaseEncoder | ||
from .geo_encoder import GeoEncoder | ||
|
||
__all__ = [ | ||
"BaseEncoder", | ||
"GeoEncoder", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from abc import ABCMeta, abstractclassmethod, abstractmethod | ||
from typing import Any, Dict, List, Union | ||
|
||
|
||
class BaseEncoder(metaclass=ABCMeta): | ||
encode_source: Dict[str, Any] | ||
|
||
@abstractmethod | ||
def encoding_single(cls, org: Union[List, str]) -> Union[int, str]: | ||
raise NotImplementedError("embedding_single must be implemented.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import json | ||
import pkgutil | ||
from typing import Any, Dict, List, Optional, Union | ||
|
||
import numpy as np | ||
import pandas as pd | ||
import plotly.express as px | ||
from scipy import spatial | ||
|
||
from .base import BaseEncoder | ||
|
||
|
||
class GeoEncoder(BaseEncoder): | ||
r"""Plotly Sample Datasets.""" | ||
ECD_PATH: Optional[bytes] = pkgutil.get_data( | ||
"rsdiv.encoding", "geojson-counties-fips.json" | ||
) | ||
if ECD_PATH: | ||
encode_source: Dict[str, Any] = json.loads(ECD_PATH) | ||
|
||
def __init__(self) -> None: | ||
super().__init__() | ||
self.encoder: pd.DataFrame = self.read_source() | ||
self.coord: List[np.ndarray] = self.encoder.coord.to_list() | ||
self.index: pd.Index = pd.Index(self.encoder["index"]) | ||
|
||
def read_source(self) -> pd.DataFrame: | ||
geo_county_dict: Dict[str, List] = {} | ||
for item in self.encode_source["features"]: | ||
coordinates = item["geometry"]["coordinates"] | ||
parts = [] | ||
for part in coordinates: | ||
parts.append(np.asarray(part).squeeze().mean(axis=0).squeeze()) | ||
coord = np.asarray(parts).mean(axis=0)[::-1] # reverse lat/lng | ||
name = item["properties"]["NAME"] | ||
lsad = item["properties"]["LSAD"] | ||
id = item["id"] | ||
geo_county_dict[id] = [coord, name, lsad] | ||
dataframe = pd.DataFrame.from_dict( | ||
geo_county_dict, orient="index", columns=["coord", "name", "lstd"] | ||
).reset_index() | ||
return dataframe | ||
|
||
def encoding_single(self, org: Union[List, str]) -> Union[int, str]: | ||
tree = spatial.KDTree(self.coord) | ||
return str(self.index[int(tree.query(org)[1])]) | ||
|
||
def encoding_series(self, series: pd.Series) -> pd.Series: | ||
encodings = pd.Series(series.apply(lambda x: self.encoding_single(x))) | ||
return encodings | ||
|
||
def graw_geo_graph(self, dataframe: pd.DataFrame, source_name: str) -> None: | ||
max_value: float = np.ceil(dataframe[source_name].max()) | ||
min_value: float = np.floor(dataframe[source_name].min()) | ||
fig = px.choropleth( | ||
dataframe, | ||
geojson=self.encode_source, | ||
locations="index", | ||
color=source_name, | ||
color_continuous_scale="OrRd", | ||
range_color=(min_value, max_value), | ||
scope="usa", | ||
) | ||
fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0}) | ||
fig.show() |
Large diffs are not rendered by default.
Oops, something went wrong.