Files
paper_2025/python/misc_functions.py
j-hartling 298969a067 Cross-checked and polished remainders of fig_invariance_thresh_lp_species.pdf.
Added misc_functions.py for anything not plot-related.
2026-03-31 09:36:55 +02:00

32 lines
1.1 KiB
Python

import numpy as np
def shorten_species(name):
genus, species = name.split('_')
return genus[0] + '. ' + species
def get_saturation(sigmoid, low=0.05, high=0.95, first=True, last=True,
condense=None):
if condense == 'norm' and sigmoid.ndim == 2:
sigmoid = np.linalg.norm(sigmoid, axis=1)
min_value = sigmoid[0] if first else sigmoid.min(axis=0)
max_value = sigmoid[-1] if last else sigmoid.max(axis=0)
span = max_value - min_value
low_value = min_value + low * span
high_value = min_value + high * span
low_mask = sigmoid >= low_value
high_mask = sigmoid >= high_value
if sigmoid.ndim == 1:
low_ind = np.nonzero(low_mask)[0][0]
high_ind = np.nonzero(high_mask)[0][0]
elif condense == 'all':
low_ind = np.nonzero(low_mask.all(axis=1))[0][0]
high_ind = np.nonzero(high_mask.all(axis=1))[0][0]
else:
low_ind, high_ind = [], []
for i in range(sigmoid.shape[1]):
low_ind.append(np.nonzero(low_mask[:, i])[0][0])
high_ind.append(np.nonzero(high_mask[:, i])[0][0])
return low_ind, high_ind