Skip to content

Commit

Permalink
Merge pull request #189 from dianna-ai/Experiments
Browse files Browse the repository at this point in the history
Added script to run Experiments
  • Loading branch information
WillemSpek authored Jun 29, 2023
2 parents 16f4ab4 + b4d23e9 commit 73c0274
Show file tree
Hide file tree
Showing 8 changed files with 342 additions and 151 deletions.
26 changes: 26 additions & 0 deletions relevance_maps_properties/experiments/config_wrappers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from typing import Any
import numpy as np

from scipy.stats import wasserstein_distance
from numpy.typing import NDArray
from skimage.segmentation import slic
from typing import Optional


class Slic_Wrapper():
def __init__(self,
n_segments: int = 10,
compactness: float = 10.,
sigma: float = 0.):
self.n_segments= n_segments
self.compactness = compactness
self.sigma = sigma

def __call__(self, image):
return slic(image,
n_segments=self.n_segments,
compactness=self.compactness,
sigma = self.sigma)

def __repr__(self):
return f'slic(n_segments={self.n_segments}, compactness={self.compactness}, sigma={self.sigma})'
5 changes: 0 additions & 5 deletions relevance_maps_properties/experiments/distance_metrics.py

This file was deleted.

93 changes: 67 additions & 26 deletions relevance_maps_properties/experiments/hyperparameter_configs.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,60 @@
from collections.abc import Mapping, Sequence
import numpy as np

from typing import Optional, Iterable
from skimage.segmentation import slic
from typing import Mapping, Optional, Iterable, Sequence, Union
from sklearn.model_selection import ParameterGrid


def create_grid(parameters: object) -> list:
''' Convert parameter objects to a grid containing all possible parameter
combinations.
Args:
parameters: Parameters to use in the grid
Returns: All possible parameter combinations
'''
return list(ParameterGrid(parameters.__dict__))


from sklearn.linear_model import Ridge

from .config_wrappers import Slic_Wrapper


class ParamGrid(ParameterGrid):
'''Wrapper for ParameterGrid from sklearn.model_selection'''
def __init__(self, param_grid: Union[Sequence, Mapping]) -> None:
cleaned_grid = {}
for key in param_grid:
if param_grid[key] is None:
continue
elif isinstance(param_grid[key], (np.ndarray, np.generic)):
cleaned_grid[key] = param_grid[key].tolist()
else:
cleaned_grid[key] = param_grid[key]
super().__init__(cleaned_grid)

def __getitem__(self, ind: int) -> dict[str, list[str, int, float]]:
'''Slight modifitcation of the sklearn.model_selection.ParameterGrid implementation
Tries to get the representation of non strings, floats and ints in order
to make this data serializable.'''
for sub_grid in self.param_grid:
if not sub_grid:
if ind == 0:
return {}
else:
ind -= 1
continue

# Reverse so most frequent cycling parameter comes first
keys, values_lists = zip(*sorted(sub_grid.items())[::-1])
sizes = [len(v_list) for v_list in values_lists]
total = np.product(sizes)

if ind >= total:
# Try the next grid
ind -= total
else:
out = {}
for key, v_list, n in zip(keys, values_lists, sizes):
ind, offset = divmod(ind, n)
val = v_list[offset]
if not isinstance(val, (str, float, int)):
val = str(val)
out[key] = val
return out

raise IndexError("ParameterGrid index out of range")


class RISE_parameters(object):
'''Set up hyperparameters for RISE.
'''
Expand Down Expand Up @@ -95,22 +133,25 @@ def __init__(self,


RISE_config = RISE_parameters(
n_masks=np.arange(200, 2000, 400),
p_keep = np.arange(.1, 1, .1),
feature_res=np.arange(1, 10, 2),
n_masks=np.arange(1000, 4000, 500)
feature_res=np.arange(3, 16, 3),
random_state=[42]
)


LIME_config = LIME_parameters(
num_samples=np.arange(1000, 4000, 500),
kernel_width=np.geomspace(0.01, 3, num=5),
distance_metric=[None], # will extend later
segmentation_fn=slic,
random_state = [42]
num_samples=np.arange(20, 200, 40),
kernel_width=np.geomspace(0.1, 3, num=5),
distance_metric=None, # will extend later
segmentation_fn=[Slic_Wrapper(n_segments=n) for n in range(10, 60,10)],
model_regressor=[Ridge(alpha=a) for a in [0, *np.geomspace(0.05, 3, num=4)]]
# random_state = [42]
)


SHAP_config = SHAP_parameters(
nsamples=np.arange(1000, 4000, 500),
l1_reg=np.geomspace(.001, 1, num=5)
)
nsamples=np.arange(20, 200, 40),
l1_reg=[0, *np.geomspace(0.05, 3, num=4)],
random_state=[42]
)
Loading

0 comments on commit 73c0274

Please sign in to comment.