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

To export non-log10 data when plot (X/Y) Axis Spacing is set to Log10… #44

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
126 changes: 125 additions & 1 deletion Vts.Gui.Wpf.Test/ViewModel/Panels/PlotViewModelTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
using NUnit.Framework;
using NSubstitute;
using NUnit.Framework;
using OxyPlot.Legends;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading;
using System.Windows;
using Vts.Common;
using Vts.Gui.Wpf.FileSystem;
using Vts.Gui.Wpf.Model;
using Vts.Gui.Wpf.ViewModel;
using Vts.IO;

namespace Vts.Gui.Wpf.Test.ViewModel.Panels
{
Expand Down Expand Up @@ -575,5 +583,121 @@ public void Verify_curve_normalization_complex()
/// DuplicateWindowCommand - not sure if can test
/// </summary>

/// <summary>
/// Test to verify only linear values written if user specifies
/// x-axis log scale and/or y-axis log scale to be plotted
/// </summary>
[Test]
public void Verify_ExportDataToText_correct_when_X_and_Y_scaling_set_to_log()
{
// clear any prior test results
const string exportFilename = "testexportdata.txt";
FileIO.FileDelete(exportFilename);

var points = new[]
{
new Point(1, 1),
new Point(2, 2),
new Point(3, 3),
new Point(4, 4),
new Point(5, 5),
new Point(6, 6),
};
// plot the data to be saved
var plotData = new[] { new PlotData(points, "Diagonal Line") };
// can't call WindowViewModel because not fixed yet
//var windowViewModel = new WindowViewModel();
//var plotViewModel = windowViewModel.PlotVM;
//plotViewModel.PlotValues.Execute(_plotData);

// mock the IOpenFileDialog
var openFileDialogMock = Substitute.For<ISaveFileDialog>();
openFileDialogMock.FileName.Returns(exportFilename);
openFileDialogMock.ShowDialog().Returns(true);

_plotViewModel = new PlotViewModel(0, openFileDialogMock);
_plotViewModel.PlotValues.Execute(plotData);
_plotViewModel.XAxisSpacingOptionVm.SelectedValue = ScalingType.Log;
_plotViewModel.YAxisSpacingOptionVm.SelectedValue = ScalingType.Log;
_plotViewModel.ExportDataToTextCommand.Execute(plotData);

// verify results in file
var stream = new FileStream(exportFilename, FileMode.Open);
using var sw = new StreamReader(stream);
// skip header
for (var i = 0; i < 4; i++)
{
sw.ReadLine();
}
// read and verify data
foreach (var t in points)
{
var line = sw.ReadLine();
if (line == null) continue;
var data = line.Split();
// check x value
Assert.AreEqual(t.X.ToString(CultureInfo.InvariantCulture), data[0]);
// check y value
Assert.AreEqual(t.Y.ToString(CultureInfo.InvariantCulture), data[1]);
}
}

/// <summary>
/// Test to verify only real and imaginary values written if user specifies
/// phase or amplitude to be plotted
/// </summary>
[Test]
public void Verify_ExportDataToText_correct_when_complex_data_plotted()
{
// clear any prior test results
const string exportFilename = "testexportdata.txt";
FileIO.FileDelete(exportFilename);

IDataPoint[] points =
{
new ComplexDataPoint(1, new Complex(1, 1)),
new ComplexDataPoint(2, new Complex(2, 2)),
new ComplexDataPoint(3, new Complex(3, 3)),
new ComplexDataPoint(4, new Complex(4, 4)),
new ComplexDataPoint(5, new Complex(5, 5)),
new ComplexDataPoint(6, new Complex(6, 6)),
};
// plot the data to be saved
var plotData = new[] { new PlotData(points, "Real and Imaginary Lines") };

// mock the IOpenFileDialog
var openFileDialogMock = Substitute.For<ISaveFileDialog>();
openFileDialogMock.FileName.Returns(exportFilename);
openFileDialogMock.ShowDialog().Returns(true);

_plotViewModel = new PlotViewModel(0, openFileDialogMock);
_plotViewModel.PlotValues.Execute(plotData);
_plotViewModel.PlotToggleTypeOptionVm.SelectedValue = PlotToggleType.Amp;
_plotViewModel.ExportDataToTextCommand.Execute(plotData);

// verify results in file
var stream = new FileStream(exportFilename, FileMode.Open);
using var sw = new StreamReader(stream);
// skip header
for (var i = 0; i < 4; i++)
{
sw.ReadLine();
}
// read and verify data
foreach (var t in points.Cast<ComplexDataPoint>().ToArray())
{
var line = sw.ReadLine();
if (line == null) continue;
var data = line.Split();
// check x value
Assert.AreEqual(t.X.ToString(CultureInfo.InvariantCulture), data[0]);
// check real value
Assert.AreEqual(t.Y.Real.ToString(CultureInfo.InvariantCulture), data[1]);
// check imaginary value
Assert.AreEqual(t.Y.Imaginary.ToString(CultureInfo.InvariantCulture), data[1]);

}
}

}
}
1 change: 1 addition & 0 deletions Vts.Gui.Wpf.Test/Vts.Gui.Wpf.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Microsoft.Windows.Compatibility" Version="7.0.5" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions Vts.Gui.Wpf/FileSystem/ISaveFileDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Vts.Gui.Wpf.FileSystem
{
public interface ISaveFileDialog
{
string Filter { get; set; }
bool? ShowDialog();
string FileName { get; set; }
string DefaultExtension { get; set; }
}
}
10 changes: 10 additions & 0 deletions Vts.Gui.Wpf/FileSystem/ITextFileService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.IO;

namespace Vts.Gui.Wpf.FileSystem
{
public interface ITextFileService
{
public Tuple<FileStream, string> OpenTextFile();
}
}
21 changes: 21 additions & 0 deletions Vts.Gui.Wpf/FileSystem/SaveFileDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Vts.Gui.Wpf.FileSystem
{
public class SaveFileDialog : ISaveFileDialog
{
public string Filter { get; set; }
public bool? ShowDialog()
{
var dialog = new Microsoft.Win32.SaveFileDialog
{
DefaultExt = DefaultExtension ?? ".txt",
Filter = Filter
};
var result = dialog.ShowDialog();
FileName = dialog.FileName;
return result;
}

public string FileName { get; set; }
public string DefaultExtension { get; set; }
}
}
2 changes: 1 addition & 1 deletion Vts.Gui.Wpf/Model/IDataPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public interface IDataPoint
{
double X { get; set; }
double X { get; set; } // only X here to work for both Double and Complex points
}
}
4 changes: 2 additions & 2 deletions Vts.Gui.Wpf/Resources/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Vts.Gui.Wpf/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
<value>Clear Newest</value>
</data>
<data name="Button_ExportData" xml:space="preserve">
<value>Export Data</value>
<value>Export Raw Data</value>
</data>
<data name="Button_ExportImage" xml:space="preserve">
<value>Export As Image</value>
Expand Down
2 changes: 1 addition & 1 deletion Vts.Gui.Wpf/ViewModel/Panels/FluenceSolverViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ private async void ExecuteFluenceSolver_Executed(object sender, ExecutedRoutedEv
{
await GetMapData();
}
catch (System.ArgumentException ex)
catch (ArgumentException ex)
{
WindowViewModel.Current.TextOutputVM.TextOutput_PostMessage.Execute(
StringLookup.GetLocalizedString("Label_FluenceSolver\r"));
Expand Down
2 changes: 1 addition & 1 deletion Vts.Gui.Wpf/ViewModel/Panels/ForwardSolverViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ private void ExecuteForwardSolver_Executed(object sender, ExecutedRoutedEventArg
WindowViewModel.Current.TextOutputVM.TextOutput_PostMessage.Execute(
StringLookup.GetLocalizedString("Label_ForwardSolver") + TissueInputVM + "\r");
}
catch (System.ArgumentException ex)
catch (ArgumentException ex)
{
WindowViewModel.Current.TextOutputVM.TextOutput_PostMessage.Execute(
StringLookup.GetLocalizedString("Label_ForwardSolver\r"));
Expand Down
Loading
Loading