Skip to content

Commit

Permalink
obtain initial concentrations from truncated normal dist. that doesn'…
Browse files Browse the repository at this point in the history
…t oversample 0
  • Loading branch information
artgoldberg committed Dec 21, 2020
1 parent 4f33a39 commit 93a1d3e
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion tests/test_dynamic_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ def test_initialize_mass_and_density(self):
self.assertIn("initial accounted ratio (", str(w[-1].message))
self.assertIn(") greater than 1.0", str(w[-1].message))

dynamic_compartment = self.specify_init_accounted_fraction(2)
dynamic_compartment = self.specify_init_accounted_fraction(4)
with self.assertRaises(MultialgorithmError):
dynamic_compartment.initialize_mass_and_density(self.local_species_pop)

Expand Down
4 changes: 2 additions & 2 deletions wc_sim/dynamic_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from obj_tables.math.expression import Expression, ObjTablesTokenCodes
from wc_lang import Species, Compartment
from wc_onto import onto
from wc_sim.model_utilities import ModelUtilities
from wc_sim.multialgorithm_errors import MultialgorithmError, MultialgorithmWarning
from wc_sim.species_populations import LocalSpeciesPopulation
from wc_utils.util.enumerate import CaseInsensitiveEnum
Expand All @@ -29,7 +30,6 @@
import wc_sim.config
import wc_sim.submodels


# mapping from wc_lang Models to DynamicComponents
WC_LANG_MODEL_TO_DYNAMIC_MODEL = {}

Expand Down Expand Up @@ -464,7 +464,7 @@ def __init__(self, dynamic_model, random_state, wc_lang_compartment, species_ids
config_multialgorithm = wc_sim.config.core.get_config()['wc_sim']['multialgorithm']
MEAN_TO_STD_DEV_RATIO = config_multialgorithm['mean_to_std_dev_ratio']
std = mean / MEAN_TO_STD_DEV_RATIO
self.init_volume = max(0., random_state.normal(mean, std))
self.init_volume = ModelUtilities.non_neg_normal_sample(random_state, mean, std)
else:
raise MultialgorithmError('Initial volume must be normally distributed')

Expand Down
4 changes: 2 additions & 2 deletions wc_sim/model_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def sample_copy_num_from_concentration(species, volume, random_state):
std = dist_conc.std
if numpy.isnan(std):
std = mean / MEAN_TO_STD_DEV_RATIO
conc = max(0., random_state.normal(mean, std))
conc = ModelUtilities.non_neg_normal_sample(random_state, mean, std)

if not isinstance(dist_conc.units, unit_registry.Unit):
raise ValueError('Unsupported unit type "{}"'.format(type(dist_conc.units)))
Expand Down Expand Up @@ -183,7 +183,7 @@ def parse_species_id(species_id):
return species_id[0:comp_start], species_id[comp_start+1:-1]

@staticmethod
def non_neg_normal_sample(random_state, mean, std, max_iters=1000):
def non_neg_normal_sample(random_state, mean, std, max_iters=100):
""" Obtain a non-negative sample from a normal distribution
The distribution returned is 0 for x < 0 and normal for 0 <= x
Expand Down

0 comments on commit 93a1d3e

Please sign in to comment.