def normalize(self): '''Normalizes timecourse.''' factor, unit = self.remove_substance_dimension() if unit: if ureg(unit) != ureg(self.unit): for key, value in self.norm_fields.items(): if value is not None: list_norm_values = list(factor * np.array(value)) setattr(self, key, list_norm_values) self.unit = unit else: self.unit = str(ureg(self.unit).u) if not self.is_norm: for key, value in self.norm_fields.items(): if value is not None: list_norm_values = list( self.measurement_type.normalize(value, self.unit).magnitude) setattr(self, key, list_norm_values) self.unit = self.measurement_type.norm_unit(self.unit).__str__() # for time_unit if not self.time_unit == TIME_NORM_UNIT: p_time_unit = self.measurement_type.p_unit(self.time_unit) times = p_time_unit * self.time norm_times = times.to(TIME_NORM_UNIT) self.time = list(norm_times.m) self.time_unit = TIME_NORM_UNIT
def p_unit(unit): try: return ureg(unit) except (UndefinedUnitError, AttributeError): if unit == "%": raise ValueError(f"unit: [{unit}] has to written as 'percent'") raise ValueError( f"unit [{unit}] is not defined in unit registry or not allowed." )
def p_unit(self): return ureg(self.name).u
def is_norm_unit(self, unit): return ureg(unit) in self.n_p_units
def get_pharmacokinetic_variables(self): """Get data for pharmacokinetics calculation :return: dict of data for calculation of pharmacokinetics """ # TODO: This should be refactored in the pharmacokinetics module pk_dict = {} # substance pk_dict["compound"] = self.substance.name # bodyweight bodyweight = self.get_bodyweight().first() # time pk_dict["t"] = pd.Series(self.time) pk_dict["t_unit"] = self.time_unit # concentration # FIXME: the timecourse data must be filtered based on the dosing times # (or alternatively this should be handled in the pk calculation) pk_dict["c_unit"] = self.unit if self.mean: pk_dict["c"] = pd.Series(self.mean) pk_dict["c_type"] = "mean" elif self.median: pk_dict["c"] = pd.Series(self.median) pk_dict["c_type"] = "median" elif self.value: pk_dict["c"] = pd.Series(self.value) pk_dict["c_type"] = "value" # dosing dosing = self.get_dosing() restricted_dosing_units = [ 'g', 'g/kg', 'mol', 'mol/kg', ] MeasurementType() if dosing: if MeasurementType.objects.get( name="restricted dosing").is_valid_unit(dosing.unit): p_unit_dosing = self.measurement_type.p_unit(dosing.unit) p_unit_concentration = self.measurement_type.p_unit( pk_dict["c_unit"]) vd_unit = p_unit_dosing / p_unit_concentration pk_dict["vd_unit"] = str(vd_unit) pk_dict["dose"] = dosing.value if dosing.time: pk_dict["intervention_time"] = ( ureg(dosing.time_unit) * dosing.time).to( self.time_unit).magnitude pk_dict["dose_unit"] = dosing.unit # bodyweight dependent values if bodyweight: pk_dict["bodyweight_unit"] = bodyweight.unit if bodyweight.value: pk_dict["bodyweight"] = bodyweight.value pk_dict["bodyweight_type"] = "value" elif bodyweight.mean: pk_dict["bodyweight"] = bodyweight.mean pk_dict["bodyweight_type"] = "mean" elif bodyweight.median: pk_dict["bodyweight"] = bodyweight.median pk_dict["bodyweight_type"] = "median" return pk_dict