Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
kyosek committed Jul 25, 2023
1 parent 7438f84 commit 939a302
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 32 deletions.
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[flake8]
max-line-length = 127
per-file-ignores = setup.py:F821
2 changes: 1 addition & 1 deletion focus/__init__.py → cfepy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .core import Focus
from cfepy.focus.core import Focus

# Let users know if they're missing any of our hard dependencies
_hard_dependencies = ("tensorflow", "numpy")
Expand Down
21 changes: 10 additions & 11 deletions focus/core.py → cfepy/focus/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


class Focus:
r"""
"""
FOCUS Lucic, et al. 2022 computes
Counterfactual Explanations (CFE) using the gradient descent
method for predictions of the tree-based models.
Expand Down Expand Up @@ -70,9 +70,9 @@ class Focus:
- Initialize FOCUS with default parameters
- Generate counterfactual explanations
focus-cfe = Focus()
cfepy = Focus()
cfe_features = focus-cfe.generate(model, X)
cfe_features = cfepy.generate(model, X)
"""

def __init__(
Expand Down Expand Up @@ -100,7 +100,7 @@ def __init__(
self.verbose = verbose

def generate(self, model, X, x_train=None):
r"""
"""
Generate counterfactual explanations for the
predictions from a tree-based model.
Expand Down Expand Up @@ -214,7 +214,7 @@ def generate(self, model, X, x_train=None):

@staticmethod
def prepare_features_by_perturb_direction(model, X: np.ndarray, direction: str):
r"""
"""
Prepares the input data `X` based on the perturbation direction.
Args:
Expand Down Expand Up @@ -267,7 +267,7 @@ def compute_gradient(
temperature,
optimizer,
):
r"""
"""
Computes the gradient of the loss function with respect to the variables to optimize.
Returns:
Expand Down Expand Up @@ -302,7 +302,7 @@ def compute_gradient(

@staticmethod
def parse_class_tree(tree, X, sigma: float) -> list:
r"""
"""
Compute impurity of each leaf node in a decision tree and approximate it using sigmoid function.
Args:
Expand Down Expand Up @@ -341,7 +341,6 @@ def parse_class_tree(tree, X, sigma: float) -> list:
for i in range(n_nodes):
cur_node = nodes[i]
if children_left[i] != children_right[i]:

if cur_node is None:
cur_node = 1.0

Expand All @@ -362,7 +361,7 @@ def parse_class_tree(tree, X, sigma: float) -> list:

@staticmethod
def get_prob_classification_tree(tree, X, sigma: float) -> tf.Tensor:
r"""
"""
get_prob_classification_tree - computes the probability of each sample's classification in a decision tree
Args:
Expand Down Expand Up @@ -420,7 +419,7 @@ def get_prob_classification_tree(tree, X, sigma: float) -> tf.Tensor:
def get_prob_classification_forest(
model, X: tf.Tensor, sigma: float, temperature: float
) -> tf.Tensor:
r"""
"""
Calculate the softmax probabilities for classification for a random forest or AdaBoost model.
Args:
Expand Down Expand Up @@ -472,7 +471,7 @@ def filter_hinge_loss(
temperature,
model,
) -> tf.Tensor:
r"""
"""
Calculates the filtered probabilities of each data point for the given model.
Args:
Expand Down
10 changes: 5 additions & 5 deletions focus/utils.py → cfepy/focus/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import tensorflow as tf


def safe_euclidean(matrix_diff, epsilon=10.0 ** -10) -> tf.Tensor:
def safe_euclidean(matrix_diff, epsilon=10.0**-10) -> tf.Tensor:
"""
Calculates the Euclidean distance between two matrices with a small epsilon added to prevent singularities.
Expand All @@ -13,10 +13,10 @@ def safe_euclidean(matrix_diff, epsilon=10.0 ** -10) -> tf.Tensor:
Returns:
tf.Tensor: A tensor representing the Euclidean distance between the two matrices
"""
return (tf.reduce_sum(matrix_diff ** 2, axis=-1) + epsilon) ** 0.5
return (tf.reduce_sum(matrix_diff**2, axis=-1) + epsilon) ** 0.5


def safe_cosine(feat_input, perturbed, epsilon=10.0 ** -10) -> tf.Tensor:
def safe_cosine(feat_input, perturbed, epsilon=10.0**-10) -> tf.Tensor:
"""
Calculates cosine distance between two input arrays `feat_input` and `perturbed`
while ensuring numerical stability with `epsilon`.
Expand All @@ -42,7 +42,7 @@ def safe_cosine(feat_input, perturbed, epsilon=10.0 ** -10) -> tf.Tensor:
return dist


def safe_l1(matrix_diff, epsilon=10.0 ** -10) -> tf.Tensor:
def safe_l1(matrix_diff, epsilon=10.0**-10) -> tf.Tensor:
"""
Calculates the L1 (Manhattan) distance between two tensors with a small epsilon value
added to prevent division by zero.
Expand Down Expand Up @@ -76,7 +76,7 @@ def tf_cov(x_train) -> tf.Tensor:
return cov_xx


def safe_mahal(matrix_diff, x_train, epsilon=10.0 ** -10) -> tf.Tensor:
def safe_mahal(matrix_diff, x_train, epsilon=10.0**-10) -> tf.Tensor:
"""
Calculates Mahalanobis distance using TensorFlow
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion focus/tests/test_focus.py → cfepy/tests/test_focus.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sklearn.ensemble import AdaBoostClassifier, RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier

from focus import Focus
from cfepy import Focus

x_train, y_train = make_classification(
n_samples=200, n_features=10, n_classes=2, random_state=42
Expand Down
6 changes: 3 additions & 3 deletions focus/tests/test_utils.py → cfepy/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
import tensorflow as tf

from focus.utils import (
from cfepy import (
calculate_distance,
safe_cosine,
safe_euclidean,
Expand All @@ -13,7 +13,7 @@
tf_cov,
)

epsilon = 10.0 ** -10
epsilon = 10.0**-10
random.seed(42)

covariance_test_data = [
Expand Down Expand Up @@ -88,7 +88,7 @@ def test_tf_cov(feat_input_cov, expected_output_cov):

@pytest.mark.parametrize("feat_input, feat_input2", distance_test_data)
def test_safe_euclidean(feat_input, feat_input2):
expected = (np.sum(feat_input2 ** 2, axis=-1) + epsilon) ** 0.5
expected = (np.sum(feat_input2**2, axis=-1) + epsilon) ** 0.5
assert safe_euclidean(feat_input).numpy() == pytest.approx(expected)


Expand Down
4 changes: 2 additions & 2 deletions focus/version.py → cfepy/version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
``focus-cfe`` is a python package for
``cfepy`` is a python package for
generating counterfactual explanations for a tree-based model
"""
# PEP0440 compatible formatted version, see:
Expand All @@ -18,4 +18,4 @@
# Dev branch marker is: 'X.Y.dev' or 'X.Y.devN' where N is an integer.
# 'X.Y.dev0' is the canonical version of 'X.Y.dev'
#
__version__ = "0.0.dev3" # pragma: no cover
__version__ = "0.0.dev4" # pragma: no cover
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

# -- Project information -----------------------------------------------------

project = "focus-cfe"
project = "cfepy"
copyright = "2023, Kyosuke Morita"
author = "Kyosuke Morita"

Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: focus-cfe
name: cfepy
dependencies:
- tensorflow
- numpy
Expand Down
2 changes: 1 addition & 1 deletion examples/focus_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
train_decision_tree_model,
)

from focus import Focus
from cfepy import Focus


def run_example():
Expand Down
3 changes: 1 addition & 2 deletions examples/hyperparameter_tuning_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
train_decision_tree_model,
)

from focus import Focus
from cfepy import Focus


def objective(trial):
Expand Down Expand Up @@ -60,7 +60,6 @@ def objective(trial):


if __name__ == "__main__":

study = optuna.create_study(direction="minimize")
study.optimize(objective, n_trials=100)

Expand Down
4 changes: 2 additions & 2 deletions examples/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ def prepare_plot_df(model, X, X_focus):
Args:
model (object): A trained machine learning model capable of making predictions.
X (array-like): The input data for which predictions are made.
X_focus (array-like): Additional input data used for focus-cfe predictions.
X_focus (array-like): Additional input data used for cfepy predictions.
Returns:
tuple: A tuple containing two pandas DataFrames.
- The first DataFrame contains the PCA-transformed features of `X`
and the corresponding predictions.
- The second DataFrame contains the PCA-transformed features of `X_focus`
and the corresponding focus-cfe predictions.
and the corresponding cfepy predictions.
"""
pca = PCA(n_components=2)

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


# get __version__ from _version.py
ver_file = path.join("focus", "version.py")
ver_file = path.join("cfepy", "version.py")
with open(ver_file) as f:
exec(f.read())

Expand All @@ -28,7 +28,7 @@ def readme():
requirements = f.read().splitlines()

setup(
name="focus-cfe",
name="cfepy",
version=__version__,
author="Kyosuke Morita",
author_email="[email protected]",
Expand Down

0 comments on commit 939a302

Please sign in to comment.