Skip to content

Commit

Permalink
refactor dim, dims -> n_dims
Browse files Browse the repository at this point in the history
  • Loading branch information
hageldave committed Sep 13, 2024
1 parent 905b70e commit bec5e20
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 60 deletions.
4 changes: 1 addition & 3 deletions examples/uamds.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@
]
}
],
"source": [
"distribs_lo = uamds(distribs_hi, dims=2)"
]
"source": "distribs_lo = uamds(distribs_hi, n_dims=2)"
},
{
"cell_type": "markdown",
Expand Down
74 changes: 42 additions & 32 deletions examples/uapca.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tests/api_consistency_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def test_dr_module():
import numpy as np
# list of distributions (normal distributions estimated from random data
distribs = [uadapy.distribution(np.random.rand(10, 3), name='Normal') for _ in range(4)]
uadapy.dr.uapca(distributions=distribs, dims=2)
uadapy.dr.uamds(distributions=distribs, dims=2)
uadapy.dr.uapca(distributions=distribs, n_dims=2)
uadapy.dr.uamds(distributions=distribs, n_dims=2)


def test_plotting_module():
Expand Down
8 changes: 4 additions & 4 deletions uadapy/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

class distribution:

def __init__(self, model, name="", dim = 1):
def __init__(self, model, name="", n_dims=1):
"""
Creates a distribution, if samples are passed as the first parameter,
no assumptions about the distribution are made. For the pdf and the sampling,
a KDE is used. If the name is "Normal", the samples
are treated as samples of a normal distribution.
:param model: A scipy.stats distribution or samples
:param name: The name of the distribution
:param dim: The dimensionality of the distribution
:param n_dims: The dimensionality of the distribution
"""
if name:
self.name = name
Expand All @@ -28,9 +28,9 @@ def __init__(self, model, name="", dim = 1):
self.model = model
mean = self.mean()
if isinstance(mean, np.ndarray):
self.dim = len(self.mean())
self.n_dims = len(self.mean())
else:
self.dim = 1
self.n_dims = 1
self.kde = None
if isinstance(self.model, np.ndarray):
self.kde = stats.gaussian_kde(self.model.T)
Expand Down
6 changes: 3 additions & 3 deletions uadapy/dr/uamds.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ def apply_uamds(means: list[np.ndarray], covs: list[np.ndarray], target_dim=2) -
}


def uamds(distributions: list, dims: int=2, seed: int=0):
def uamds(distributions: list, n_dims: int = 2, seed: int = 0):
"""
Applies the UAMDS algorithm to the provided distributions and returns the projected distributions
in lower-dimensional space. It assumes multivariate normal distributions.
Expand All @@ -532,7 +532,7 @@ def uamds(distributions: list, dims: int=2, seed: int=0):
----------
distributions : list
list of input distributions (distribution objects offering mean() and cov() methods)
dims : int
n_dims : int
target dimensionality, 2 by default.
seed : int
Set the random seed for the initialization, 0 by default
Expand All @@ -546,7 +546,7 @@ def uamds(distributions: list, dims: int=2, seed: int=0):
np.random.seed(seed)
means = [d.mean() for d in distributions]
covs = [d.cov() for d in distributions]
result = apply_uamds(means, covs, dims)
result = apply_uamds(means, covs, n_dims)
distribs_lo = []
for (m, c) in zip(result['means'], result['covs']):
distribs_lo.append(distribution(multivariate_normal(m, c)))
Expand Down
6 changes: 3 additions & 3 deletions uadapy/dr/uapca.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
from uadapy import distribution
from scipy.stats import multivariate_normal

def uapca(distributions, dims: int):
def uapca(distributions, n_dims: int = 2):
"""
Applies UAPCA algorithm to the distribution and returns the distribution
in lower-dimensional space. It assumes a normal distributions. If you apply
other distributions that provide mean and covariance, these values would be used
to approximate a normal distribution
:param distributions: List of input distributions
:param dims: Target dimension
:param n_dims: Target dimension
:return: List of distributions in low-dimensional space
"""
try:
means = np.array([d.mean() for d in distributions])
covs = np.array([d.cov() for d in distributions])
means_pca, covs_pca = transform_uapca(means, covs, dims)
means_pca, covs_pca = transform_uapca(means, covs, n_dims)
dist_pca = []
for (m, c) in zip(means_pca, covs_pca):
dist_pca.append(distribution(multivariate_normal(m, c)))
Expand Down
2 changes: 1 addition & 1 deletion uadapy/plotting/plots1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def setup_plot(distributions, num_samples, seed, fig=None, axs=None, colors=None

# Calculate the layout of subplots
if axs is None:
num_plots = distributions[0].dim
num_plots = distributions[0].n_dims
num_rows = ceil(sqrt(num_plots))
num_cols = ceil(num_plots / num_rows)
fig, axs = plt.subplots(num_rows, num_cols)
Expand Down
24 changes: 12 additions & 12 deletions uadapy/plotting/plotsND.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def plot_samples(distributions, num_samples, seed=55, **kwargs):
if isinstance(distributions, distribution):
distributions = [distributions]
# Create matrix
numvars = distributions[0].dim
numvars = distributions[0].n_dims
fig, axes = plt.subplots(nrows=numvars, ncols=numvars)
contour_colors = utils.generate_spectrum_colors(len(distributions))
for ax in axes.flat:
Expand All @@ -42,7 +42,7 @@ def plot_samples(distributions, num_samples, seed=55, **kwargs):

# Fill matrix with data
for k, d in enumerate(distributions):
if d.dim < 2:
if d.n_dims < 2:
raise Exception('Wrong dimension of distribution')
samples = d.sample(num_samples, seed)
for i, j in zip(*np.triu_indices_from(axes, k=1)):
Expand Down Expand Up @@ -114,7 +114,7 @@ def plot_contour(distributions, num_samples, resolution=128, ranges=None, quanti
distributions = [distributions]
contour_colors = utils.generate_spectrum_colors(len(distributions))
# Create matrix
numvars = distributions[0].dim
numvars = distributions[0].n_dims
if ranges is None:
min_val = np.zeros(distributions[0].mean().shape)+1000
max_val = np.zeros(distributions[0].mean().shape)-1000
Expand All @@ -133,11 +133,11 @@ def plot_contour(distributions, num_samples, resolution=128, ranges=None, quanti

# Fill matrix with data
for k, d in enumerate(distributions):
if d.dim < 2:
if d.n_dims < 2:
raise Exception('Wrong dimension of distribution')
dims = ()
test = ()
for i in range(d.dim):
for i in range(d.n_dims):
test = (*test, i)
x = np.linspace(ranges[i][0], ranges[i][1], resolution)
dims = (*dims, x)
Expand Down Expand Up @@ -167,7 +167,7 @@ def plot_contour(distributions, num_samples, resolution=128, ranges=None, quanti
for i, j in zip(*np.triu_indices_from(axes, k=1)):
for x, y in [(i, j), (j, i)]:
color = contour_colors[k]
indices = list(np.arange(d.dim))
indices = list(np.arange(d.n_dims))
indices.remove(x)
indices.remove(y)
pdf_agg = np.sum(pdf, axis=tuple(indices))
Expand All @@ -177,7 +177,7 @@ def plot_contour(distributions, num_samples, resolution=128, ranges=None, quanti

# Fill diagonal
for i in range(numvars):
indices = list(np.arange(d.dim))
indices = list(np.arange(d.n_dims))
indices.remove(i)
axes[i,i].plot(dims[i], np.sum(pdf, axis=tuple(indices)), color=color)
axes[i,i].xaxis.set_visible(True)
Expand Down Expand Up @@ -243,7 +243,7 @@ def plot_contour_samples(distributions, num_samples, resolution=128, ranges=None
distributions = [distributions]
contour_colors = utils.generate_spectrum_colors(len(distributions))
# Create matrix
numvars = distributions[0].dim
numvars = distributions[0].n_dims
if ranges is None:
min_val = np.zeros(distributions[0].mean().shape)+1000
max_val = np.zeros(distributions[0].mean().shape)-1000
Expand All @@ -263,10 +263,10 @@ def plot_contour_samples(distributions, num_samples, resolution=128, ranges=None
# Fill matrix with data
for k, d in enumerate(distributions):
samples = d.sample(num_samples, seed)
if d.dim < 2:
if d.n_dims < 2:
raise Exception('Wrong dimension of distribution')
dims = ()
for i in range(d.dim):
for i in range(d.n_dims):
x = np.linspace(ranges[i][0], ranges[i][1], resolution)
dims = (*dims, x)
coordinates = np.array(np.meshgrid(*dims)).transpose(tuple(range(1, numvars+1)) + (0,))
Expand Down Expand Up @@ -295,7 +295,7 @@ def plot_contour_samples(distributions, num_samples, resolution=128, ranges=None
for i, j in zip(*np.triu_indices_from(axes, k=1)):
for x, y in [(i, j), (j, i)]:
color = contour_colors[k]
indices = list(np.arange(d.dim))
indices = list(np.arange(d.n_dims))
indices.remove(x)
indices.remove(y)
pdf_agg = np.sum(pdf, axis=tuple(indices))
Expand All @@ -308,7 +308,7 @@ def plot_contour_samples(distributions, num_samples, resolution=128, ranges=None

# Fill diagonal
for i in range(numvars):
indices = list(np.arange(d.dim))
indices = list(np.arange(d.n_dims))
indices.remove(i)
axes[i,i].plot(dims[i], np.sum(pdf, axis=tuple(indices)), color=color)
axes[i,i].xaxis.set_visible(True)
Expand Down

0 comments on commit bec5e20

Please sign in to comment.