public Model.OriginData MapFrom(BatchOriginData batchOriginData) { var originData = new Model.OriginData(); var species = _speciesRepository.FindByName(batchOriginData.Species); if (species == null) { throw new PKSimException(PKSimConstants.Error.CouldNotFindSpecies(batchOriginData.Species, _speciesRepository.AllNames())); } originData.Species = species; var population = species.PopulationByName(batchOriginData.Population); if (population == null) { if (string.IsNullOrEmpty(batchOriginData.Population) && species.Populations.Count() == 1) { population = species.Populations.ElementAt(0); } else { throw new PKSimException(PKSimConstants.Error.CouldNotFindPopulationForSpecies(batchOriginData.Population, batchOriginData.Species, species.Populations.AllNames())); } } originData.SpeciesPopulation = population; var gender = population.GenderByName(batchOriginData.Gender); if (gender == null) { if (string.IsNullOrEmpty(batchOriginData.Gender)) { gender = population.Genders.ElementAt(0); } else { throw new PKSimException(PKSimConstants.Error.CouldNotFindGenderForPopulation(batchOriginData.Gender, batchOriginData.Population, population.Genders.AllNames())); } } originData.Gender = gender; //this is not defined in matlab yet originData.SubPopulation = _originDataTask.DefaultSubPopulationFor(species); if (originData.SpeciesPopulation.IsAgeDependent) { originData.Age = batchOriginData.Age; var meanAgeParameter = _individualModelTask.MeanAgeFor(originData); originData.Age = valueFrom(batchOriginData.Age, meanAgeParameter.Value); originData.AgeUnit = meanAgeParameter.Dimension.BaseUnit.Name; originData.GestationalAge = valueFrom(batchOriginData.GestationalAge, CoreConstants.NOT_PRETERM_GESTATIONAL_AGE_IN_WEEKS); originData.GestationalAgeUnit = CoreConstants.Units.Weeks; } var calculationMethodCategoryForSpecies = _originDataTask.AllCalculationMethodCategoryFor(species); foreach (var category in calculationMethodCategoryForSpecies) { string selectedCalculationMethod = batchOriginData.CalculationMethodFor(category.Name); if (string.IsNullOrEmpty(selectedCalculationMethod)) { originData.AddCalculationMethod(category.DefaultItemForSpecies(species)); } else { var calculationMethod = category.AllItems().FindByName(selectedCalculationMethod); if (calculationMethod == null) { throw new PKSimException(PKSimConstants.Error.CouldNotFindCalculationMethodInCategory(selectedCalculationMethod, category.Name, category.AllItems().AllNames())); } if (calculationMethod.AllSpecies.Contains(species.Name)) { originData.AddCalculationMethod(calculationMethod); } else { throw new PKSimException(PKSimConstants.Error.CalculationMethodIsNotDefinedForSpecies(selectedCalculationMethod, category.Name, species.Name)); } } } var meanWeightParameter = _individualModelTask.MeanWeightFor(originData); originData.Weight = valueFrom(batchOriginData.Weight, meanWeightParameter.Value).Value; originData.WeightUnit = meanWeightParameter.Dimension.BaseUnit.Name; if (originData.SpeciesPopulation.IsHeightDependent) { var meanHeightParameter = _individualModelTask.MeanHeightFor(originData); originData.Height = valueFrom(batchOriginData.Height, meanHeightParameter.Value); originData.HeightUnit = meanHeightParameter.Dimension.BaseUnit.Name; } return(originData); }
public override async Task <ParameterAlternative> MapToModel(Alternative snapshot, ParameterAlternativeGroup parameterAlternativeGroup) { var alternative = _parameterAlternativeFactory.CreateAlternativeFor(parameterAlternativeGroup); alternative.IsDefault = ModelValueFor(snapshot.IsDefault, DEFAULT_IS_DEFAULT); MapSnapshotPropertiesToModel(snapshot, alternative); await UpdateParametersFromSnapshot(snapshot, alternative, parameterAlternativeGroup.Name); if (parameterAlternativeGroup.IsNamed(CoreConstants.Groups.COMPOUND_SOLUBILITY)) { updateSolubilityAlternative(alternative); } var alternativeWithSpecies = alternative as ParameterAlternativeWithSpecies; if (alternativeWithSpecies == null) { return(alternative); } alternativeWithSpecies.Species = _speciesRepository.FindByName(snapshot.Species); if (alternativeWithSpecies.Species == null) { throw new SnapshotOutdatedException(PKSimConstants.Error.CouldNotFindSpecies(snapshot.Species, _speciesRepository.AllNames())); } return(alternativeWithSpecies); }
private Species speciesFrom(SnapshotOriginData snapshot) { var species = _speciesRepository.FindByName(snapshot.Species); return(species ?? throw new PKSimException(PKSimConstants.Error.CouldNotFindSpecies(snapshot.Species, _speciesRepository.AllNames()))); }