mirror of
https://github.com/Richard-Sti/csiborgtools_public.git
synced 2025-05-14 06:31:11 +00:00
New plots (#85)
* Update verbosity messages * Update verbosity messags * Update more verbosity flags * Update the iterator settings * Add basic plots * Update verbosity flags * Update arg parsre * Update plots * Remove some older code * Fix some definitions * Update plots * Update plotting * Update plots * Add support functions * Update nb * Improve plots, move back to scripts * Update plots * pep8 * Add max overlap plot * Add blank line * Upload changes * Update changes * Add weighted stats * Remove * Add import * Add Max's matching * Edit submission * Add paths to Max's matching * Fix matching * Edit submission * Edit plot * Add max overlap separation plot * Add periodic distance * Update overlap summaries * Add nsim0 for Max matvhing * Add Max's agreement plot * Add Quijote for Max method * Update ploitting * Update name
This commit is contained in:
parent
ca3772ac6f
commit
8e3127f4d9
10 changed files with 1343 additions and 2100 deletions
|
@ -15,6 +15,7 @@
|
|||
|
||||
import numpy
|
||||
from scipy.stats import binned_statistic
|
||||
from scipy.special import erf
|
||||
|
||||
dpi = 600
|
||||
fout = "../plots/"
|
||||
|
@ -56,38 +57,74 @@ def latex_float(*floats, n=2):
|
|||
return latex_floats
|
||||
|
||||
|
||||
def binned_trend(x, y, weights, bins):
|
||||
"""
|
||||
Calculate the weighted mean and standard deviation of `y` in bins of `x`.
|
||||
def nan_weighted_average(arr, weights=None, axis=None):
|
||||
if weights is None:
|
||||
weights = numpy.ones_like(arr)
|
||||
|
||||
Parameters
|
||||
----------
|
||||
x : 1-dimensional array
|
||||
The x-coordinates of the data points.
|
||||
y : 1-dimensional array
|
||||
The y-coordinates of the data points.
|
||||
weights : 1-dimensional array
|
||||
The weights of the data points.
|
||||
bins : 1-dimensional array
|
||||
The bin edges.
|
||||
valid_entries = ~numpy.isnan(arr)
|
||||
|
||||
Returns
|
||||
-------
|
||||
stat_x : 1-dimensional array
|
||||
The x-coordinates of the binned data points.
|
||||
stat_mu : 1-dimensional array
|
||||
The weighted mean of `y` in bins of `x`.
|
||||
stat_std : 1-dimensional array
|
||||
The weighted standard deviation of `y` in bins of `x`.
|
||||
"""
|
||||
stat_mu, __, __ = binned_statistic(x, y * weights, bins=bins,
|
||||
statistic="sum")
|
||||
stat_std, __, __ = binned_statistic(x, y * weights, bins=bins,
|
||||
statistic=numpy.var)
|
||||
stat_w, __, __ = binned_statistic(x, weights, bins=bins, statistic="sum")
|
||||
# Set NaN entries in arr to 0 for computation
|
||||
arr = numpy.where(valid_entries, arr, 0)
|
||||
|
||||
stat_x = (bins[1:] + bins[:-1]) / 2
|
||||
stat_mu /= stat_w
|
||||
stat_std /= stat_w
|
||||
stat_std = numpy.sqrt(stat_std)
|
||||
return stat_x, stat_mu, stat_std
|
||||
# Set weights of NaN entries to 0
|
||||
weights = numpy.where(valid_entries, weights, 0)
|
||||
|
||||
# Compute the weighted sum and the sum of weights along the axis
|
||||
weighted_sum = numpy.sum(arr * weights, axis=axis)
|
||||
sum_weights = numpy.sum(weights, axis=axis)
|
||||
|
||||
return weighted_sum / sum_weights
|
||||
|
||||
|
||||
def nan_weighted_std(arr, weights=None, axis=None, ddof=0):
|
||||
if weights is None:
|
||||
weights = numpy.ones_like(arr)
|
||||
|
||||
valid_entries = ~numpy.isnan(arr)
|
||||
|
||||
# Set NaN entries in arr to 0 for computation
|
||||
arr = numpy.where(valid_entries, arr, 0)
|
||||
|
||||
# Set weights of NaN entries to 0
|
||||
weights = numpy.where(valid_entries, weights, 0)
|
||||
|
||||
# Calculate weighted mean
|
||||
weighted_mean = numpy.sum(
|
||||
arr * weights, axis=axis) / numpy.sum(weights, axis=axis)
|
||||
|
||||
# Calculate the weighted variance
|
||||
variance = numpy.sum(
|
||||
weights * (arr - numpy.expand_dims(weighted_mean, axis))**2, axis=axis)
|
||||
variance /= numpy.sum(weights, axis=axis) - ddof
|
||||
|
||||
return numpy.sqrt(variance)
|
||||
|
||||
|
||||
def compute_error_bars(x, y, xbins, sigma):
|
||||
bin_indices = numpy.digitize(x, xbins)
|
||||
y_medians = numpy.array([numpy.median(y[bin_indices == i])
|
||||
for i in range(1, len(xbins))])
|
||||
|
||||
lower_pct = 100 * 0.5 * (1 - erf(sigma / numpy.sqrt(2)))
|
||||
upper_pct = 100 - lower_pct
|
||||
|
||||
y_lower = numpy.full(len(y_medians), numpy.nan)
|
||||
y_upper = numpy.full(len(y_medians), numpy.nan)
|
||||
|
||||
for i in range(len(y_medians)):
|
||||
if numpy.sum(bin_indices == i + 1) == 0:
|
||||
continue
|
||||
|
||||
y_lower[i] = numpy.percentile(y[bin_indices == i + 1], lower_pct)
|
||||
y_upper[i] = numpy.percentile(y[bin_indices == i + 1], upper_pct)
|
||||
|
||||
yerr = (y_medians - numpy.array(y_lower), numpy.array(y_upper) - y_medians)
|
||||
|
||||
return y_medians, yerr
|
||||
|
||||
|
||||
def normalize_hexbin(hb):
|
||||
hexagon_counts = hb.get_array()
|
||||
normalized_counts = hexagon_counts / hexagon_counts.sum()
|
||||
hb.set_array(normalized_counts)
|
||||
hb.set_clim(normalized_counts.min(), normalized_counts.max())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue