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

1748 output mapping wrong namespace #1756

Merged
merged 2 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
using OSPSuite.Core.Domain.Data;
using OSPSuite.Core.Domain.ParameterIdentifications;
using OSPSuite.Core.Domain.UnitSystem;
using OSPSuite.Core.Extensions;

namespace OSPSuite.Core.Domain.ParameterIdentifications
namespace OSPSuite.Core.Domain
{
public class OutputMapping
{
public virtual WeightedObservedData WeightedObservedData { get; set; }

public virtual SimulationQuantitySelection OutputSelection { get; set; }

public virtual Scalings Scaling { get; set; }
public float Weight { get; set; }

public float Weight { get; set; } = Constants.DEFAULT_WEIGHT;

/// <summary>
/// Returns the consolidated path of the mapped output (e.g without the name of the simulation)
Expand All @@ -21,34 +24,21 @@ public class OutputMapping
/// </summary>
public virtual string FullOutputPath => OutputSelection?.FullQuantityPath ?? string.Empty;


/// <summary>
/// Returns the underlying mapped output (Molecule Amount or Observer)
/// </summary>
public virtual IQuantity Output => OutputSelection?.Quantity;

public OutputMapping()
{
Weight = Constants.DEFAULT_WEIGHT;
}

public virtual ISimulation Simulation => OutputSelection?.Simulation;

public IDimension Dimension => OutputSelection?.Quantity?.Dimension;

public virtual bool UsesObservedData(DataRepository observerData)
{
return Equals(WeightedObservedData.ObservedData, observerData);
}
public virtual bool UsesObservedData(DataRepository observerData) => Equals(WeightedObservedData.ObservedData, observerData);

public virtual bool UsesSimulation(ISimulation simulation)
{
return Equals(OutputSelection.Simulation, simulation);
}
public virtual bool UsesSimulation(ISimulation simulation) => Equals(OutputSelection.Simulation, simulation);

public virtual bool IsValid => Output != null && WeightedObservedData != null;



public virtual OutputMapping Clone()
{
return new OutputMapping
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OSPSuite.Serializer;
using OSPSuite.Core.Domain;
using OSPSuite.Serializer;
using OSPSuite.Core.Domain.ParameterIdentifications;

namespace OSPSuite.Core.Serialization.ParameterIdentificationExport.Serializer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OSPSuite.Core.Domain.ParameterIdentifications;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.ParameterIdentifications;

namespace OSPSuite.Core.Serialization.Xml
{
Expand Down
52 changes: 42 additions & 10 deletions src/OSPSuite.Core/Services/ResidualsVsTimeChartService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OSPSuite.Core.Chart;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.Data;
using OSPSuite.Core.Domain.ParameterIdentifications;
using OSPSuite.Core.Domain.UnitSystem;
using OSPSuite.Core.Extensions;
using OSPSuite.Utility.Extensions;
using static OSPSuite.Assets.Captions.ParameterIdentification;

namespace OSPSuite.Core.Services
{
Expand All @@ -27,12 +30,19 @@ public interface IResidualsVsTimeChartService
/// <paramref name="id" /> and <paramref name="repositoryName" />
/// </summary>
DataRepository CreateScatterDataRepository(string id, string repositoryName, OutputResiduals outputResidual);

void ConfigureChartAxis(AnalysisChartWithLocalRepositories chart);

/// <summary>
/// Get or creates the DataRepository for the given residuals <paramref name="outputResidual" /> for the given chart.
/// If it already exists, update the values with the output residuals values (update of chart)
/// </summary>
DataRepository GetOrCreateScatterDataRepositoryInChart(AnalysisChartWithLocalRepositories chart, OutputResiduals outputResidual, int? runIndex = null);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is so much code duplication between the two presenters. I don't quite understand why since we have a service exactly for this purpose. I started to move some so that I could fix the issue only in one place. But at the end of the day, the unit fo residual is too m uch work. I still suggest to keep my chanages

}

public class ResidualsVsTimeChartService : IResidualsVsTimeChartService
{
private const string ZERO = "Zero";
private string _markerCurveId = string.Empty;
private readonly IDimensionFactory _dimensionFactory;

public ResidualsVsTimeChartService(IDimensionFactory dimensionFactory)
Expand All @@ -45,11 +55,7 @@ public ResidualsVsTimeChartService(IDimensionFactory dimensionFactory)
public DataRepository AddZeroMarkerCurveToChart(AnalysisChartWithLocalRepositories chart, float minObservedDataTime, float maxObservedDataTime)
{
var markerRepository = createMarkerRepository(minObservedDataTime, maxObservedDataTime, chart);
AddCurvesFor(markerRepository, (column, curve) =>
{
curve.UpdateMarkerCurve(ZERO);
_markerCurveId = curve.Id;
}, chart);
AddCurvesFor(markerRepository, (column, curve) => { curve.UpdateMarkerCurve(ZERO); }, chart);

return markerRepository;
}
Expand All @@ -58,17 +64,17 @@ private DataRepository createMarkerRepository(float minObservedDataTime, float m
{
var id = $"{chart.Id}-{ZERO}";
var dataRepository = createEmptyRepository(id, ZERO, ZERO);
dataRepository.BaseGrid.Values = new[] { minObservedDataTime, maxObservedDataTime };
dataRepository.FirstDataColumn().Values = new[] { 0f, 0f };
dataRepository.BaseGrid.Values = new[] {minObservedDataTime, maxObservedDataTime};
dataRepository.FirstDataColumn().Values = new[] {0f, 0f};
return dataRepository;
}

private DataRepository createEmptyRepository(string id, string name, string valueName)
{
var dataRepository = new DataRepository(id) { Name = name };
var dataRepository = new DataRepository(id) {Name = name};
var baseGrid = new BaseGrid($"{id}-Time", "Time", _dimensionFactory.Dimension(Constants.Dimension.TIME));
var values = new DataColumn($"{id}-{valueName}", valueName, _dimensionFactory.NoDimension, baseGrid)
{ DataInfo = { Origin = ColumnOrigins.CalculationAuxiliary } };
{DataInfo = {Origin = ColumnOrigins.CalculationAuxiliary}};
dataRepository.Add(values);
return dataRepository;
}
Expand All @@ -90,5 +96,31 @@ public DataRepository CreateScatterDataRepository(string id, string repositoryNa
scatterColumn.QuantityInfo.Path = outputPath;
return dataRepository;
}

public void ConfigureChartAxis(AnalysisChartWithLocalRepositories chart)
{
chart.AxisBy(AxisTypes.Y).Caption = Residuals;
chart.AxisBy(AxisTypes.Y).Scaling = Scalings.Linear;
}

public DataRepository GetOrCreateScatterDataRepositoryInChart(AnalysisChartWithLocalRepositories chart, OutputResiduals outputResidual, int? runIndex = null)
{
var repositoryName = runIndex.HasValue ? SimulationResultsForRun(runIndex.Value) : "Simulation Results";
var idSuffix = runIndex.HasValue ? $"-{runIndex}" : "";
var residuals = outputResidual.Residuals;
var id = $"{chart.Id}-{outputResidual.FullOutputPath}-{outputResidual.ObservedData.Id}{idSuffix}";

var dataRepository = chart.DataRepositories.FindById(id);
if (dataRepository == null)
{
dataRepository = CreateScatterDataRepository(id, repositoryName, outputResidual);
chart.AddRepository(dataRepository);
}

dataRepository.BaseGrid.Values = residuals.Select(x => x.Time).ToFloatArray();
dataRepository.FirstDataColumn().Values = residuals.Select(x => x.Value).ToFloatArray();

return dataRepository;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.ParameterIdentifications;
using OSPSuite.Presentation.DTO;
using OSPSuite.Presentation.DTO.ParameterIdentifications;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.ParameterIdentifications;
using OSPSuite.Presentation.DTO;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using OSPSuite.Core.Chart;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.Data;
using OSPSuite.Core.Domain.ParameterIdentifications;
using OSPSuite.Core.Extensions;
using OSPSuite.Presentation.Extensions;
using OSPSuite.Presentation.Services.Charts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
using OSPSuite.Assets;
using OSPSuite.Core.Chart;
using OSPSuite.Core.Chart.ParameterIdentifications;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.Data;
using OSPSuite.Core.Domain.ParameterIdentifications;
using OSPSuite.Core.Extensions;
using OSPSuite.Core.Services;
using OSPSuite.Presentation.Services.Charts;
using OSPSuite.Presentation.Views.ParameterIdentifications;
Expand All @@ -23,7 +21,10 @@ public class ParameterIdentificationResidualVsTimeChartPresenter : ParameterIden
private readonly IResidualsVsTimeChartService _residualsVsTimeChartService;
private DataRepository _zeroRepository;

public ParameterIdentificationResidualVsTimeChartPresenter(IParameterIdentificationSingleRunAnalysisView view, ChartPresenterContext chartPresenterContext, IResidualsVsTimeChartService residualsVsTimeChartService) :
public ParameterIdentificationResidualVsTimeChartPresenter(
IParameterIdentificationSingleRunAnalysisView view,
ChartPresenterContext chartPresenterContext,
IResidualsVsTimeChartService residualsVsTimeChartService) :
base(view, chartPresenterContext, ApplicationIcons.ResidualVsTimeAnalysis, PresenterConstants.PresenterKeys.ParameterIdentificationResidualVsTimeChartPresenter)
{
_residualsVsTimeChartService = residualsVsTimeChartService;
Expand All @@ -36,13 +37,10 @@ protected override void UpdateAnalysisBasedOn(IReadOnlyList<ParameterIdentificat
return;

_zeroRepository = _residualsVsTimeChartService.AddZeroMarkerCurveToChart(Chart, _parameterIdentification.MinObservedDataTime, _parameterIdentification.MaxObservedDataTime);
AddDataRepositoriesToEditor(new[] { _zeroRepository });
AddDataRepositoriesToEditor(new[] {_zeroRepository});

if (ChartIsBeingCreated)
{
Chart.AxisBy(AxisTypes.Y).Caption = Captions.ParameterIdentification.Residuals;
Chart.AxisBy(AxisTypes.Y).Scaling = Scalings.Linear;
}
_residualsVsTimeChartService.ConfigureChartAxis(Chart);

UpdateChartFromTemplate();
}
Expand All @@ -55,7 +53,7 @@ protected override void AddRunResultToChart(ParameterIdentificationRunResult run
private void addOutputToScatter(IGrouping<string, OutputResiduals> outputMappingsByOutput, ParameterIdentificationRunResult runResult)
{
var fullOutputPath = outputMappingsByOutput.Key;
bool shouldShowInLegend = true;
var shouldShowInLegend = true;
foreach (var outputMapping in outputMappingsByOutput)
{
var dataRepository = getOrCreateScatterDataRepositoryFor(runResult.Index, outputMapping);
Expand All @@ -77,27 +75,8 @@ private void addOutputToScatter(IGrouping<string, OutputResiduals> outputMapping
}
}


private DataRepository getOrCreateScatterDataRepositoryFor(int runIndex, OutputResiduals outputResidual)
{
var repositoryName = Captions.ParameterIdentification.SimulationResultsForRun(runIndex);
var id = $"{Chart.Id}-{outputResidual.FullOutputPath}-{outputResidual.ObservedData.Id}-{runIndex}";

var timeValues = outputResidual.Residuals.Select(x => x.Time).ToList();
var outputValues = outputResidual.Residuals.Select(x => x.Value).ToList();

var dataRepository = Chart.DataRepositories.FindById(id);
if (dataRepository == null)
{
dataRepository = _residualsVsTimeChartService.CreateScatterDataRepository(id, repositoryName, outputResidual);
Chart.AddRepository(dataRepository);
}

dataRepository.BaseGrid.Values = timeValues.ToFloatArray();
dataRepository.FirstDataColumn().Values = outputValues.ToFloatArray();

return dataRepository;
}
private DataRepository getOrCreateScatterDataRepositoryFor(int runIndex, OutputResiduals outputResidual) =>
_residualsVsTimeChartService.GetOrCreateScatterDataRepositoryInChart(Chart, outputResidual, runIndex);

public override void Clear()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@ protected override void UpdateAnalysis()
_zeroRepository = _residualsVsTimeChartService.AddZeroMarkerCurveToChart(Chart, minObservedDataTime(), maxObservedDataTime());
AddDataRepositoriesToEditor(new[] { _zeroRepository });

if (ChartIsBeingCreated)
{
Chart.AxisBy(AxisTypes.Y).Caption = Captions.ParameterIdentification.Residuals;
Chart.AxisBy(AxisTypes.Y).Scaling = Scalings.Linear;
}
if (ChartIsBeingCreated)
_residualsVsTimeChartService.ConfigureChartAxis(Chart);

UpdateChartFromTemplate();
View.SetTotalError(simulationResidual.TotalError);
Expand Down Expand Up @@ -102,26 +99,8 @@ private float maxObservedDataTime()
return getAllAvailableObservedData().Select(x => x.BaseGrid.Values.Last()).Max();
}

private DataRepository getOrCreateScatterDataRepositoryFor(OutputResiduals outputResidual)
{
var repositoryName = "Simulation Results";
var id = $"{Chart.Id}-{outputResidual.FullOutputPath}-{outputResidual.ObservedData.Id}";

var timeValues = outputResidual.Residuals.Select(x => x.Time).ToList();
var outputValues = outputResidual.Residuals.Select(x => x.Value).ToList();

var dataRepository = Chart.DataRepositories.FindById(id);
if (dataRepository == null)
{
dataRepository = _residualsVsTimeChartService.CreateScatterDataRepository(id, repositoryName, outputResidual);
Chart.AddRepository(dataRepository);
}

dataRepository.BaseGrid.Values = timeValues.ToFloatArray();
dataRepository.FirstDataColumn().Values = outputValues.ToFloatArray();

return dataRepository;
}
private DataRepository getOrCreateScatterDataRepositoryFor(OutputResiduals outputResidual) =>
_residualsVsTimeChartService.GetOrCreateScatterDataRepositoryInChart(Chart, outputResidual);

private IEnumerable<DataRepository> getAllAvailableObservedData()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using OSPSuite.BDDHelper.Extensions;
using OSPSuite.Core.Chart;
using OSPSuite.Core.Chart.ParameterIdentifications;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.Data;
using OSPSuite.Core.Domain.Mappers;
using OSPSuite.Core.Domain.ParameterIdentifications;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using OSPSuite.BDDHelper;
using OSPSuite.BDDHelper.Extensions;
using OSPSuite.Core.Chart.ParameterIdentifications;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.Data;
using OSPSuite.Core.Domain.Mappers;
using OSPSuite.Core.Domain.ParameterIdentifications;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FakeItEasy;
using OSPSuite.BDDHelper;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.ParameterIdentifications;
using OSPSuite.Helpers;
using OSPSuite.Presentation.Core;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using OSPSuite.BDDHelper;
using OSPSuite.BDDHelper.Extensions;
using OSPSuite.Core.Domain;
using OSPSuite.Core.Domain.Data;
using OSPSuite.Core.Domain.ParameterIdentifications;
using OSPSuite.Helpers;
Expand Down