Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importer corrections #553

Merged
merged 10 commits into from
Apr 29, 2021
Merged
3 changes: 3 additions & 0 deletions src/MoBi.Assets/AppConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,7 @@ public static class MenuNames
public static readonly string ModelParts = "Model Parts";
public static readonly string ImportSBML = "Open SBML Model...";
public static readonly string SaveAsPKML = "Save As PKML...";
public static readonly string ReloadAll = "Reload all under same settings...";

public static string AddNew(string objectTypeName) => $"Create {objectTypeName}...";

Expand Down Expand Up @@ -1900,6 +1901,8 @@ public static string NameIsAlreadyUsedInThisContainer(string containerPath, stri
public static readonly string BrowseForFile = "Select File";
public static readonly string Undefined = "Undefined";
public static readonly string PleaseSelectCurveInChartEditor = "Please select a curve from the chart editor to be displayed in the chart";
public static readonly IReadOnlyList<string> DefaultObservedDataCategories = new[] { Constants.ObservedData.MOLECULE, Constants.ObservedData.COMPARTMENT, Constants.ObservedData.ORGAN};
msevestre marked this conversation as resolved.
Show resolved Hide resolved


public static string PathType(string pathTypeAsString)
{
Expand Down
3 changes: 3 additions & 0 deletions src/MoBi.Assets/MoBi.Assets.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OSPSuite.Assets" Version="10.0.159-unroltob" />
georgeDaskalakis marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="OSPSuite.Assets.Images" Version="10.0.159-unroltob" />
<PackageReference Include="OSPSuite.Core" Version="10.0.159-unroltob" />
georgeDaskalakis marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="OSPSuite.Assets" Version="10.0.122" />
<PackageReference Include="OSPSuite.Assets.Images" Version="10.0.122" />
<PackageReference Include="OSPSuite.Core" Version="10.0.122" />
Expand Down
1 change: 1 addition & 0 deletions src/MoBi.BatchTool/MoBi.BatchTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OSPSuite.Core" Version="10.0.159-unroltob" />
<PackageReference Include="OSPSuite.Core" Version="10.0.122" />
<PackageReference Include="OSPSuite.DevExpress" Version="20.1.6" />
<PackageReference Include="OSPSuite.FuncParser" Version="4.0.0.50" GeneratePathProperty="true" />
Expand Down
7 changes: 7 additions & 0 deletions src/MoBi.Core/MoBi.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
<PackageReference Include="FluentNHibernate" Version="2.1.2" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="OSPSuite.Utility" Version="4.0.0.4" />
<PackageReference Include="OSPSuite.Core" Version="10.0.159-unroltob" />
<PackageReference Include="OSPSuite.Assets" Version="10.0.159-unroltob" />
<PackageReference Include="OSPSuite.Assets.Images" Version="10.0.159-unroltob" />
georgeDaskalakis marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="OSPSuite.Infrastructure.Export" Version="10.0.159-unroltob" />
<PackageReference Include="OSPSuite.Infrastructure.Reporting" Version="10.0.159-unroltob" />
<PackageReference Include="OSPSuite.Infrastructure.Serialization" Version="10.0.159-unroltob" />
<PackageReference Include="OSPSuite.Infrastructure.Import" Version="10.0.159-unroltob" />
<PackageReference Include="OSPSuite.Core" Version="10.0.122" />
<PackageReference Include="OSPSuite.Assets" Version="10.0.122" />
<PackageReference Include="OSPSuite.Assets.Images" Version="10.0.122" />
Expand Down
10 changes: 1 addition & 9 deletions src/MoBi.Core/Services/ModelPartsToExcelExporterTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,7 @@ public void ExportModelPartsToExcelFile(string excelFileName, IMoBiSimulation si
var moleculeParameterDataTable = _moleculeStartValuesBuildingBlockToParameterDataTableMapper.MapFrom(simulation.MoBiBuildConfiguration.MoleculeStartValues.Where(msv => msv.IsPresent), simulation.MoBiBuildConfiguration.Molecules);

var dataTables = new List<DataTable> {reactionDataTable, simulationParameterDataTable, moleculeParameterDataTable};
//TODO: have to fix the export here
/*
ExportToExcelTask.ExportDataTablesToExcel(dataTables, excelFileName, openExcel: openExcel, workbookConfiguration: (wb, dt) =>
{
wb.setSelection(0, 0, 0, dt.Columns.Count);
var rangeStyle = wb.getRangeStyle();
rangeStyle.FontBold = true;
wb.setRangeStyle(rangeStyle);
});*/
ExportToExcelTask.ExportDataTablesToExcel(dataTables, excelFileName, openExcel: openExcel);
}
}
}
2 changes: 2 additions & 0 deletions src/MoBi.Engine/MoBi.Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OSPSuite.Core" Version="10.0.159-unroltob" />
<PackageReference Include="OSPSuite.Assets" Version="10.0.159-unroltob" />
<PackageReference Include="OSPSuite.Core" Version="10.0.122" />
<PackageReference Include="OSPSuite.Assets" Version="10.0.122" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public IContextMenu InitializeWith(DataRepository dataRepository)
editMenuItemFor(dataRepository),
renameMenuItemFor(dataRepository),
createSaveItemFor(dataRepository),
createReloadItemFor(dataRepository),
deleteMenuItemFor(dataRepository),
exportToExcel(dataRepository),
reportMenuItemFor(dataRepository),
Expand Down Expand Up @@ -95,6 +96,14 @@ private static IMenuBarButton deleteMenuItemFor(DataRepository dataRepository)
.WithIcon(ApplicationIcons.Delete);
}

private IMenuBarItem createReloadItemFor(DataRepository dataRepository)
{
return CreateMenuButton.WithCaption(AppConstants.MenuNames.ReloadAll) //ToDo: move to Core, also from PK-Sim
.WithCommandFor<ReloadAllObservedDataCommand, DataRepository>(dataRepository)
.AsDisabledIf(string.IsNullOrEmpty(dataRepository.ConfigurationId))
.WithIcon(ApplicationIcons.Excel);
}

private IMenuBarItem createSaveItemFor(DataRepository dataRepository)
{
return CreateMenuButton.WithCaption(AppConstants.MenuNames.SaveAsPKML)
Expand Down
6 changes: 6 additions & 0 deletions src/MoBi.Presentation/MoBi.Presentation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@

<ItemGroup>
<PackageReference Include="Northwoods.GoWin" Version="5.2.0" />
<PackageReference Include="OSPSuite.Presentation" Version="10.0.159-unroltob" />
<PackageReference Include="OSPSuite.Presentation.Importer" Version="10.0.92" />
<PackageReference Include="OSPSuite.Presentation.Serialization" Version="10.0.159-unroltob" />
<PackageReference Include="OSPSuite.TeXReporting" Version="3.0.0.4" />
<PackageReference Include="OSPSuite.Utility" Version="4.0.0.4" />
<PackageReference Include="OSPSuite.Core" Version="10.0.159-unroltob" />
<PackageReference Include="OSPSuite.Presentation" Version="10.0.122" />
<PackageReference Include="OSPSuite.Presentation.Importer" Version="10.0.92" />
<PackageReference Include="OSPSuite.Presentation.Serialization" Version="10.0.122" />
Expand Down
157 changes: 129 additions & 28 deletions src/MoBi.Presentation/Tasks/ObservedDataTask.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MoBi.Assets;
using MoBi.Core.Commands;
using MoBi.Core.Domain.Model;
Expand All @@ -13,9 +10,14 @@
using OSPSuite.Core.Domain.Data;
using OSPSuite.Core.Domain.Services;
using OSPSuite.Core.Domain.UnitSystem;
using OSPSuite.Core.Import;
using OSPSuite.Core.Services;
using OSPSuite.Infrastructure.Import.Services;
using OSPSuite.Utility.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using ColumnInfo = OSPSuite.Infrastructure.Import.Core.ColumnInfo;
using OSPSuite.Infrastructure.Import.Services;
using Command = OSPSuite.Assets.Command;
using CoreConstants = OSPSuite.Core.Domain.Constants;
using DimensionInfo = OSPSuite.Infrastructure.Import.Core;
Expand All @@ -33,6 +35,8 @@ public interface IObservedDataTask : OSPSuite.Core.Domain.Services.IObservedData
/// Removes selected <paramref name="resultsToRemove" /> from their respective simulations
/// </summary>
void RemoveResultsFromSimulations(IReadOnlyList<DataRepository> resultsToRemove);

void AddAndReplaceObservedDataFromConfigurationToProject(ImporterConfiguration configuration, IEnumerable<DataRepository> observedDataFromSameFile);
georgeDaskalakis marked this conversation as resolved.
Show resolved Hide resolved
}

public class ObservedDataTask : OSPSuite.Core.Domain.Services.ObservedDataTask, IObservedDataTask
Expand All @@ -44,6 +48,7 @@ public class ObservedDataTask : OSPSuite.Core.Domain.Services.ObservedDataTask,
private readonly IDimension _molWeightDimension;
private readonly IDialogCreator _mobiDialogCreator;


public ObservedDataTask(
IDataImporter dataImporter,
IDimensionFactory dimensionFactory,
Expand All @@ -64,26 +69,28 @@ public ObservedDataTask(

public void AddObservedDataToProject()
{
//maybe the formats are not being registered
var data = _dataImporter.ImportDataSets(createMetaData().ToList(), createColumnInfos().ToList(), createDataImportSettings());

if (data.DataRepositories == null || data.Configuration == null) return;

foreach (var repository in data.DataRepositories)
{
adjustMolWeight(repository);
georgeDaskalakis marked this conversation as resolved.
Show resolved Hide resolved
AddObservedDataToProject(repository);
adjustRepositoryPaths(repository);
}
AddImporterConfigurationToProject(data.Configuration);
}

private void adjustRepositoryPaths(DataRepository repository)
{
var baseGrid = repository.BaseGrid;
var baseGridName = baseGrid.Name.Replace(ObjectPath.PATH_DELIMITER, "\\");
baseGrid.QuantityInfo = new QuantityInfo(baseGrid.Name, new[] {repository.Name, baseGridName}, QuantityType.Time);
baseGrid.QuantityInfo = new QuantityInfo(baseGrid.Name, new[] { repository.Name, baseGridName }, QuantityType.Time);

foreach (var col in repository.AllButBaseGrid())
{
var colName = col.Name.Replace(ObjectPath.PATH_DELIMITER, "\\");
var quantityInfo = new QuantityInfo(col.Name, new[] {repository.Name, colName}, QuantityType.Undefined);
var quantityInfo = new QuantityInfo(col.Name, new[] { repository.Name, colName }, QuantityType.Undefined);
col.QuantityInfo = quantityInfo;
}
}
Expand Down Expand Up @@ -151,24 +158,11 @@ private DimensionInfo.DataImporterSettings createDataImportSettings()
IconName = ApplicationIcons.MoBi.IconName,
Caption = $"{AppConstants.PRODUCT_NAME} - {AppConstants.Captions.ImportObservedData}"
};
settings.AddNamingPatternMetaData(Constants.FILE);
addNamingPatterns(settings);
settings.NameOfMetaDataHoldingMolecularWeightInformation = AppConstants.Parameters.MOLECULAR_WEIGHT;
return settings;
}

private void adjustMolWeight(DataRepository observedData)
{
if (!observedData.ExtendedProperties.Contains(AppConstants.Parameters.MOLECULAR_WEIGHT))
return;

// molweight is provided in default unit should be saved in core unit
var molWeightExtendedProperty = observedData.ExtendedProperties[AppConstants.Parameters.MOLECULAR_WEIGHT].DowncastTo<IExtendedProperty<double>>();
var molWeight = _molWeightDimension.UnitValueToBaseUnitValue(_molWeightDimension.DefaultUnit, molWeightExtendedProperty.Value);
observedData.AllButBaseGrid().Each(x => x.DataInfo.MolWeight = molWeight);

//Remove Molweight extended properties
observedData.ExtendedProperties.Remove(AppConstants.Parameters.MOLECULAR_WEIGHT);
}

public override void Rename(DataRepository dataRepository)
{
var newName = _mobiDialogCreator.AskForInput(AppConstants.Dialog.AskForNewName(dataRepository.Name),
Expand Down Expand Up @@ -210,6 +204,77 @@ public void RemoveResultsFromSimulations(IReadOnlyList<DataRepository> resultsTo
_context.AddToHistory(macroCommand.Run(_context));
}

public void AddAndReplaceObservedDataFromConfigurationToProject(ImporterConfiguration configuration,
IEnumerable<DataRepository> observedDataFromSameFile)
{
var importedObservedData = getObservedDataFromImporter(configuration);
var reloadDataSets =
_dataImporter.CalculateReloadDataSetsFromConfiguration(importedObservedData.ToList(), observedDataFromSameFile.ToList());

foreach (var dataSet in reloadDataSets.NewDataSets)
{
AddObservedDataToProject(dataSet);
adjustRepositoryPaths(dataSet);
}

foreach (var dataSet in reloadDataSets.DataSetsToBeDeleted.ToArray()) //toDo it should be checked if to array solves the deleting problem
{
Delete(dataSet);
}

foreach (var dataSet in reloadDataSets.OverwrittenDataSets)
{
//TODO this here should be tested
var existingDataSet = findDataRepositoryInList(observedDataFromSameFile, dataSet);

foreach (var column in dataSet.Columns)
{
var datacolumn = new DataColumn(column.Id, column.Name, column.Dimension, column.BaseGrid)
{
QuantityInfo = column.QuantityInfo,
DataInfo = column.DataInfo,
IsInternal = column.IsInternal,
Values = column.Values
};

if (column.IsBaseGrid())
{
existingDataSet.BaseGrid.Values = datacolumn.Values;
}
else
{
var existingColumn = existingDataSet.FirstOrDefault(x => x.Name == column.Name);
if (existingColumn == null)
existingDataSet.Add(column);
else
existingColumn.Values = column.Values;
}
}
}
}

private DataRepository findDataRepositoryInList(IEnumerable<DataRepository> dataRepositoryList, DataRepository targetDataRepository)
{
return (from dataRepo in dataRepositoryList
let result = targetDataRepository.ExtendedProperties.KeyValues.All(keyValuePair =>
dataRepo.ExtendedProperties[keyValuePair.Key].ValueAsObject.ToString() == keyValuePair.Value.ValueAsObject.ToString())
where result
select dataRepo).FirstOrDefault();
}

private IEnumerable<DataRepository> getObservedDataFromImporter(ImporterConfiguration configuration)
{
var dataImporterSettings = createDataImportSettings();

//do we really need this in MoBi????
dataImporterSettings.NameOfMetaDataHoldingMoleculeInformation = Constants.ObservedData.MOLECULE;
var colInfos = createColumnInfos().ToList();

var importedObservedData = _dataImporter.ImportFromConfiguration(configuration, createMetaData().ToList(),
colInfos, dataImporterSettings);
return importedObservedData;
}

private ICommand removeResultFromSimulationCommand(DataRepository dataRepository)
{
var parentSimulation = getSimulationWithHistoricResult(dataRepository);
Expand All @@ -232,7 +297,8 @@ private static ClearResultsCommand clearResultsCommand(IMoBiSimulation parentSim
return new ClearResultsCommand(parentSimulation);
}

private static RemoveHistoricResultFromSimulationCommand removeHistoricResultFromSimulationCommand(DataRepository repository, IMoBiSimulation parentSimulation)
private static RemoveHistoricResultFromSimulationCommand removeHistoricResultFromSimulationCommand(DataRepository repository,
IMoBiSimulation parentSimulation)
{
return new RemoveHistoricResultFromSimulationCommand(parentSimulation, repository);
}
Expand All @@ -250,7 +316,7 @@ private static RemoveHistoricResultFromSimulationCommand removeHistoricResultFro
NullValuesHandling = DimensionInfo.NullValuesHandlingType.DeleteRow,
};

timeColumn.DimensionInfos.Add(new DimensionInfo.DimensionInfo {Dimension = timeDimension, IsMainDimension = true});
timeColumn.DimensionInfos.Add(new DimensionInfo.DimensionInfo { Dimension = timeDimension, IsMainDimension = true });
yield return timeColumn;

var mainDimension = _dimensionFactory.Dimension(Constants.Dimension.MOLAR_CONCENTRATION);
Expand Down Expand Up @@ -331,7 +397,7 @@ private void addDimensionsTo(DimensionInfo.ColumnInfo columnInfo, IDimension mai

private void addPredefinedMoleculeNames(DimensionInfo.MetaDataCategory metaDataCategory)
{
addUndefinedValueTo(metaDataCategory);
metaDataCategory.ShouldListOfValuesBeIncluded = true;
allMolecules().OrderBy(molecule => molecule.Name).Each(molecule => addInfoToCategory(metaDataCategory, molecule));
}

Expand All @@ -348,23 +414,58 @@ private static void addUndefinedValueTo(DimensionInfo.MetaDataCategory metaDataC
private void addPredefinedOrganValues(DimensionInfo.MetaDataCategory metaDataCategory)
{
addUndefinedValueTo(metaDataCategory);
metaDataCategory.ShouldListOfValuesBeIncluded = true;
allOrgans().OrderBy(org => org.Name).Each(organ => addInfoToCategory(metaDataCategory, organ));
}

private void addNamingPatterns(DimensionInfo.DataImporterSettings dataImporterSettings)
{
dataImporterSettings.AddNamingPatternMetaData(
Constants.FILE
);

dataImporterSettings.AddNamingPatternMetaData(
Constants.FILE,
Constants.SHEET
);

dataImporterSettings.AddNamingPatternMetaData(
Constants.ObservedData.MOLECULE,
Constants.ObservedData.SPECIES,
Constants.ObservedData.ORGAN,
Constants.ObservedData.COMPARTMENT
);

dataImporterSettings.AddNamingPatternMetaData(
Constants.ObservedData.MOLECULE,
Constants.ObservedData.SPECIES,
Constants.ObservedData.ORGAN,
Constants.ObservedData.COMPARTMENT,
Constants.ObservedData.STUDY_ID,
Constants.ObservedData.GENDER,
Constants.ObservedData.DOSE,
Constants.ObservedData.ROUTE,
Constants.ObservedData.PATIENT_ID
);
}

private void addPredefinedCompartmentValues(DimensionInfo.MetaDataCategory metaDataCategory)
{
addUndefinedValueTo(metaDataCategory);
metaDataCategory.ShouldListOfValuesBeIncluded = true;
allCompartments().OrderBy(comp => comp.Name).Each(compartment => addInfoToCategory(metaDataCategory, compartment));
}

private IEnumerable<IContainer> allOrgans()
{
return allTopContainers().SelectMany(allSubContainers).Where(container => container.ContainerType == ContainerType.Organ).DistinctBy(x => x.Name);
return allTopContainers().SelectMany(allSubContainers).Where(container => container.ContainerType == ContainerType.Organ)
.DistinctBy(x => x.Name);
}

private IEnumerable<IContainer> allCompartments()
{
return allTopContainers().SelectMany(allSubContainers).Where(container => container.ContainerType == ContainerType.Compartment).DistinctBy(x => x.Name);
return allTopContainers().SelectMany(allSubContainers).Where(container => container.ContainerType == ContainerType.Compartment)
.DistinctBy(x => x.Name);
}

private IEnumerable<IContainer> allTopContainers()
Expand Down
Loading