Skip to content

Commit

Permalink
Add `simulation.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jafetcc02 committed Apr 3, 2024
1 parent e3012c3 commit 2709e98
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 68 deletions.
11 changes: 9 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ dmypy.json
.pyre/

# Ignore test_<files>.py for testing
/test_tools.py
*test*.py
*.sql
*.html

# Ignore <files>.txt for testing
/examples/*.txt
Expand All @@ -134,4 +136,9 @@ dmypy.json
/examples/*.png

# Ignore <files>.pkl for testing
/examples/*.pkl
/examples/*.pkl

*.csv
*.xlsx

labelencoder_dictionary.pkl
2 changes: 1 addition & 1 deletion likelihood/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.6
1.2.8
1 change: 1 addition & 0 deletions likelihood/graph/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .graph import *
50 changes: 50 additions & 0 deletions likelihood/graph/graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from IPython.display import HTML, display
from pandas.core.frame import DataFrame
from pyvis.network import Network

from likelihood.tools import FeatureSelection


class DynamicGraph(FeatureSelection):
"""A class to represent a dynamic graph"""

def __init__(self, df: DataFrame, n_importances: int):
self.G = Network(
notebook=True, cdn_resources="remote", directed=True
) # enable interactive visualization in Jupyter Notebooks
self.df = df
self.n_importances = n_importances
super().__init__()

def fit(self, **kwargs) -> None:
"""Fit the model according to the given data and parameters."""
self.get_digraph(self.df, self.n_importances)
# create a dictionary with the indexes and names of the dataframe
self.get_index = dict(zip(self.df.columns, range(len(self.df.columns))))
self._make_network()

def _make_network(self) -> None:
"""Create nodes and edges of the network based on feature importance scores"""
self._add_nodes()
for i in range(len(self.all_features_imp_graph)):
node = self.all_features_imp_graph[i][0]
edges = self.all_features_imp_graph[i][1]

for label, weight in edges:
self.G.add_edge(i, self.get_index[label], weight=weight)

def _add_nodes(self) -> None:
for i in range(len(self.all_features_imp_graph)):
node = self.all_features_imp_graph[i][0]
self.G.add_node(n_id=i, label=node)

def draw(self, name="graph.html", **kwargs) -> None:
"""Display the network using HTML format"""
spring_length = kwargs["spring_length"] if "spring_length" in kwargs else 500
node_distance = kwargs["node_distance"] if "node_distance" in kwargs else 100
self.G.repulsion(node_distance=node_distance, spring_length=spring_length)
self.G.show_buttons(filter_=["physics"])
self.G.show(name)

html_file_content = open(name, "r").read()
display(HTML(html_file_content))
1 change: 1 addition & 0 deletions likelihood/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .regression import *
from .simulation import *
44 changes: 44 additions & 0 deletions likelihood/models/simulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import matplotlib.pyplot as plt
import numpy as np
from numpy import ndarray
from pandas.core.frame import DataFrame

from likelihood.tools import FeatureSelection, OneHotEncoder

# --------------------------------------------------------------------------------------------------------------------------------------


class SimulationEngine(FeatureSelection):

def __init__(self, df: DataFrame, n_importances: int, **kwargs):

self.df = df
self.n_importances = n_importances

super().__init__(**kwargs)

def predict(self, column: str, n: int = None) -> ndarray | list:

# We assing the entries of the dictionary corresponding to the column
w, quick_encoder, names_cols, dfe = self.w_dict[column]

df_aux = dfe._df

if n != None:
df_aux = df_aux.iloc[:n, :]

y = df_aux.to_numpy() @ w

if quick_encoder != None:

one_hot = OneHotEncoder()
y = one_hot.decode(y)
encoding_dic = quick_encoder.decoding_list[0]
y = [encoding_dic[item] for item in y]

return y

def fit(self, **kwargs) -> None:

# We run the feature selection algorithm
self.get_digraph(self.df, self.n_importances)
Loading

0 comments on commit 2709e98

Please sign in to comment.