Skip to content

Commit

Permalink
consistent use of n instead of num
Browse files Browse the repository at this point in the history
  • Loading branch information
hageldave committed Sep 13, 2024
1 parent 792ecd5 commit 0bc2ac0
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions uadapy/plotting/plots1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ def calculate_freedman_diaconis_bins(data):
q25, q75 = np.percentile(data, [25, 75])
iqr = q75 - q25
bin_width = 2 * iqr / np.cbrt(len(data))
num_bins = int((np.max(data) - np.min(data)) / bin_width)
return num_bins
n_bins = int((np.max(data) - np.min(data)) / bin_width)
return n_bins

def calculate_offsets(count, max_count):
occupancy = (count/max_count)
return np.linspace(-0.45 * occupancy, 0.45 * occupancy, count)

def calculate_dot_size(num_samples, scale_factor):
if num_samples < 100:
def calculate_dot_size(n_samples, scale_factor):
if n_samples < 100:
dot_size = scale_factor * 3.125
else:
dot_size = scale_factor * (50 /(4 ** np.log10(num_samples)))
dot_size = scale_factor * (50 / (4 ** np.log10(n_samples)))
return dot_size

def setup_plot(distributions, num_samples, seed, fig=None, axs=None, colors=None, **kwargs):
def setup_plot(distributions, n_samples, seed, fig=None, axs=None, colors=None, **kwargs):
"""
Set up the plot for samples drawn from given distributions.
Parameters
----------
distributions : list
List of distributions to plot. If a single distribution is passed, it will be converted into a list.
num_samples : int
n_samples : int
Number of samples per distribution.
seed : int
Seed for the random number generator for reproducibility.
Expand All @@ -59,9 +59,9 @@ def setup_plot(distributions, num_samples, seed, fig=None, axs=None, colors=None
List of samples drawn from the distributions.
palette : list
List of colors to use for each distribution.
num_plots : int
n_plots : int
Number of subplots.
num_cols : int
n_cols : int
Number of columns in the subplot layout.
"""

Expand All @@ -72,31 +72,31 @@ 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].n_dims
num_rows = ceil(sqrt(num_plots))
num_cols = ceil(num_plots / num_rows)
fig, axs = plt.subplots(num_rows, num_cols)
n_plots = distributions[0].n_dims
n_rows = ceil(sqrt(n_plots))
n_cols = ceil(n_plots / n_rows)
fig, axs = plt.subplots(n_rows, n_cols)
else:
# Case 1: axs is a 2D array (multiple rows and columns)
if isinstance(axs, np.ndarray):
dim = axs.shape
if (len(axs.shape) == 1):
num_rows, num_cols = 1, dim[0]
n_rows, n_cols = 1, dim[0]
else:
num_rows, num_cols = dim
n_rows, n_cols = dim
# Case 2: axs is not an array (single subplot)
else:
num_rows, num_cols = 1, 1
num_plots = num_rows + num_cols
n_rows, n_cols = 1, 1
n_plots = n_rows + n_cols

# Ensure axs is a 2D array even if there's only one row or column
if num_rows == 1:
if n_rows == 1:
axs = [axs]
if num_cols == 1:
if n_cols == 1:
axs = [[ax] for ax in axs]

for d in distributions:
samples.append(d.sample(num_samples, seed))
samples.append(d.sample(n_samples, seed))

# Generate Glasbey colors
if colors is None:
Expand All @@ -108,17 +108,17 @@ def setup_plot(distributions, num_samples, seed, fig=None, axs=None, colors=None
colors.extend(additional_colors)
palette = colors

return fig, axs, samples, palette, num_plots, num_cols
return fig, axs, samples, palette, n_plots, n_cols

def plot_1d_distribution(distributions, num_samples, plot_types:list, seed=55, fig=None, axs=None, labels=None, titles=None, colors=None, **kwargs):
def plot_1d_distribution(distributions, n_samples, plot_types:list, seed=55, fig=None, axs=None, labels=None, titles=None, colors=None, **kwargs):
"""
Plot box plots, violin plots and dot plots for samples drawn from given distributions.
Parameters
----------
distributions : list
List of distributions to plot.
num_samples : int
n_samples : int
Number of samples per distribution.
plot_types : list
List of plot types to plot. Valid values are 'boxplot','violinplot', 'stripplot', 'swarmplot' and 'dotplot'.
Expand Down Expand Up @@ -166,12 +166,12 @@ def plot_1d_distribution(distributions, num_samples, plot_types:list, seed=55, f
List of Axes objects used for plotting.
"""

fig, axs, samples, palette, num_plots, num_cols = setup_plot(distributions, num_samples, seed, fig, axs, colors, **kwargs)
fig, axs, samples, palette, n_plots, n_cols = setup_plot(distributions, n_samples, seed, fig, axs, colors, **kwargs)

# Check number of attributes
num_attributes = 1
n_attributes = 1
if np.ndim(samples) > 2:
num_attributes = np.shape(samples)[2]
n_attributes = np.shape(samples)[2]

if labels:
ticks = range(len(labels))
Expand All @@ -180,8 +180,8 @@ def plot_1d_distribution(distributions, num_samples, plot_types:list, seed=55, f

for i, ax_row in enumerate(axs):
for j, ax in enumerate(ax_row):
index = i * num_cols + j
if index < num_plots and index < num_attributes:
index = i * n_cols + j
if index < n_plots and index < n_attributes:
y_min = 9999
y_max = -9999
for k, sample in enumerate(samples):
Expand Down Expand Up @@ -219,10 +219,10 @@ def plot_1d_distribution(distributions, num_samples, plot_types:list, seed=55, f
dot_size = kwargs['dot_size']
if 'stripplot' in plot_types:
if 'dot_size' not in kwargs:
if num_samples < 100:
if n_samples < 100:
scale_factor = 1
else:
scale_factor = 1 + np.log10(num_samples/100)
scale_factor = 1 + np.log10(n_samples / 100)
dot_size = calculate_dot_size(len(sample[:,index]), scale_factor)
if kwargs.get('vert',True):
sns.stripplot(x=[k]*len(sample[:,index]), y=sample[:,index], color=palette[k % len(palette)], size=dot_size, jitter=0.25, ax=ax)
Expand All @@ -244,8 +244,8 @@ def plot_1d_distribution(distributions, num_samples, plot_types:list, seed=55, f
y_min = np.min(flat_sample)
if y_max < np.max(flat_sample):
y_max = np.max(flat_sample)
num_bins = calculate_freedman_diaconis_bins(flat_sample)
bin_width = kwargs.get('bin_width', (np.max(flat_sample) - np.min(flat_sample)) / num_bins)
n_bins = calculate_freedman_diaconis_bins(flat_sample)
bin_width = kwargs.get('bin_width', (np.max(flat_sample) - np.min(flat_sample)) / n_bins)
bins = np.arange(np.min(flat_sample), np.max(flat_sample) + bin_width, bin_width)
binned_data, bin_edges = np.histogram(flat_sample, bins=bins)
max_count = np.max(binned_data)
Expand Down

0 comments on commit 0bc2ac0

Please sign in to comment.