24 #include "isoSpec++.h"
27 #define ISOSPEC_INIT_TABLE_SIZE 16
29 #define ISOSPEC_INIT_TABLE_SIZE 1024
37 double AbyssalWassersteinDistanceGrad(FixedEnvelope*
const* envelopes,
const double* scales,
double* ret_gradient,
size_t N,
double abyss_depth_exp,
double abyss_depth_the);
62 sorted_by_mass(
false),
63 sorted_by_prob(
false),
73 FixedEnvelope(
double* masses,
double* probs,
size_t confs_no,
bool masses_sorted =
false,
bool probs_sorted =
false,
double _total_prob = NAN);
85 inline size_t confs_no()
const {
return _confs_no; }
86 inline int getAllDim()
const {
return allDim; }
88 inline const double* masses()
const {
return _masses; }
89 inline const double* probs()
const {
return _probs; }
90 inline const int* confs()
const {
return _confs; }
92 inline double* release_masses() {
double* ret = _masses; _masses =
nullptr;
return ret; }
93 inline double* release_probs() {
double* ret = _probs; _probs =
nullptr;
return ret; }
94 inline int* release_confs() {
int* ret = _confs; _confs =
nullptr;
return ret; }
95 inline void release_everything() { _confs =
nullptr; _probs = _masses =
nullptr; }
98 inline double mass(
size_t i)
const {
return _masses[i]; }
99 inline double prob(
size_t i)
const {
return _probs[i]; }
100 inline const int* conf(
size_t i)
const {
return _confs + i*allDim; }
105 double get_total_prob();
106 void scale(
double factor);
108 void shift_mass(
double shift);
109 void resample(
size_t ionic_current,
double beta_bias = 1.0);
111 double empiric_average_mass();
112 double empiric_variance();
113 double empiric_stddev() {
return sqrt(empiric_variance()); }
117 double AbyssalWassersteinDistance(
FixedEnvelope& other,
double abyss_depth,
double other_scale = 1.0);
118 std::tuple<double, double, double> WassersteinMatch(
FixedEnvelope& other,
double flow_distance,
double other_scale = 1.0);
121 static FixedEnvelope LinearCombination(
const std::vector<const FixedEnvelope*>& spectra,
const std::vector<double>& intensities);
125 FixedEnvelope bin(
double bin_width = 1.0,
double middle = 0.0);
128 void sort_by(
double* order);
132 template<
typename T,
bool tgetConfs> ISOSPEC_FORCE_INLINE
void store_conf(
const T& generator)
134 *tmasses = generator.mass(); tmasses++;
135 *tprobs = generator.prob(); tprobs++;
136 constexpr_if(tgetConfs) { generator.get_conf_signature(tconfs); tconfs += allDim; }
139 ISOSPEC_FORCE_INLINE
void store_conf(
double _mass,
double _prob)
141 if(_confs_no == current_size)
142 reallocate_memory<false>(current_size*2);
152 template<
bool tgetConfs> ISOSPEC_FORCE_INLINE
void swap(
size_t idx1,
size_t idx2, ISOSPEC_MAYBE_UNUSED
int* conf_swapspace)
154 std::swap<double>(this->_probs[idx1], this->_probs[idx2]);
155 std::swap<double>(this->_masses[idx1], this->_masses[idx2]);
156 constexpr_if(tgetConfs)
158 int* c1 = this->_confs + (idx1*this->allDim);
159 int* c2 = this->_confs + (idx2*this->allDim);
160 memcpy(conf_swapspace, c1, this->allDimSizeofInt);
161 memcpy(c1, c2, this->allDimSizeofInt);
162 memcpy(c2, conf_swapspace, this->allDimSizeofInt);
166 template<
bool tgetConfs>
void reallocate_memory(
size_t new_size);
167 void slow_reallocate_memory(
size_t new_size);
170 template<
bool tgetConfs>
void threshold_init(
Iso&& iso,
double threshold,
bool absolute);
172 template<
bool tgetConfs,
typename GenType = IsoLayeredGenerator>
void addConfILG(
const GenType& generator)
174 if(this->_confs_no == this->current_size)
175 this->
template reallocate_memory<tgetConfs>(this->current_size*2);
177 this->
template store_conf<GenType, tgetConfs>(generator);
181 template<
bool tgetConfs>
void total_prob_init(
Iso&& iso,
double target_prob,
bool trim);
183 static FixedEnvelope FromThreshold(
Iso&& iso,
double threshold,
bool absolute,
bool tgetConfs =
false)
188 ret.threshold_init<
true>(std::move(iso), threshold, absolute);
190 ret.threshold_init<
false>(std::move(iso), threshold, absolute);
194 inline static FixedEnvelope FromThreshold(
const Iso& iso,
double _threshold,
bool _absolute,
bool tgetConfs =
false)
196 return FromThreshold(
Iso(iso,
false), _threshold, _absolute, tgetConfs);
199 static FixedEnvelope FromTotalProb(
Iso&& iso,
double target_total_prob,
bool optimize,
bool tgetConfs =
false)
204 ret.total_prob_init<
true>(std::move(iso), target_total_prob, optimize);
206 ret.total_prob_init<
false>(std::move(iso), target_total_prob, optimize);
211 inline static FixedEnvelope FromTotalProb(
const Iso& iso,
double _target_total_prob,
bool _optimize,
bool tgetConfs =
false)
213 return FromTotalProb(
Iso(iso,
false), _target_total_prob, _optimize, tgetConfs);
216 template<
bool tgetConfs>
void stochastic_init(
Iso&& iso,
size_t _no_molecules,
double _precision,
double _beta_bias);
218 inline static FixedEnvelope FromStochastic(
Iso&& iso,
size_t _no_molecules,
double _precision = 0.9999,
double _beta_bias = 5.0,
bool tgetConfs =
false)
223 ret.stochastic_init<
true>(std::move(iso), _no_molecules, _precision, _beta_bias);
225 ret.stochastic_init<
false>(std::move(iso), _no_molecules, _precision, _beta_bias);
230 static FixedEnvelope FromStochastic(
const Iso& iso,
size_t _no_molecules,
double _precision = 0.9999,
double _beta_bias = 5.0,
bool tgetConfs =
false)
232 return FromStochastic(
Iso(iso,
false), _no_molecules, _precision, _beta_bias, tgetConfs);
235 static FixedEnvelope Binned(
Iso&& iso,
double target_total_prob,
double bin_width,
double bin_middle = 0.0);
236 static FixedEnvelope Binned(
const Iso& iso,
double target_total_prob,
double bin_width,
double bin_middle = 0.0)
238 return Binned(
Iso(iso,
false), target_total_prob, bin_width, bin_middle);
241 friend double AbyssalWassersteinDistanceGrad(
FixedEnvelope*
const* envelopes,
const double* scales,
double* ret_gradient,
size_t N,
double abyss_depth_exp,
double abyss_depth_the);
The Iso class for the calculation of the isotopic distribution.