Skip to content

Commit ea6d85d

Browse files
committed
Minor changes and refactoring
1 parent b92b399 commit ea6d85d

File tree

4 files changed

+43
-107
lines changed

4 files changed

+43
-107
lines changed

NeuralNetwork.NET/Extensions/MiscExtensions.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Runtime.CompilerServices;
5+
using System.Text;
56
using System.Threading.Tasks;
67
using JetBrains.Annotations;
78

@@ -20,8 +21,10 @@ public static class MiscExtensions
2021
/// <param name="item">The item to cast</param>
2122
[Pure, NotNull]
2223
[MethodImpl(MethodImplOptions.AggressiveInlining)]
23-
public static TOut To<TIn, TOut>([NotNull] this TIn item) where TOut : class, TIn => item as TOut
24-
?? throw new InvalidOperationException($"The item of type {typeof(TIn)} is a {item.GetType()} instance and can't be cast to {typeof(TOut)}");
24+
public static TOut To<TIn, TOut>([NotNull] this TIn item)
25+
where TIn : class
26+
where TOut : TIn
27+
=> (TOut)item;
2528

2629
/// <summary>
2730
/// Returns the maximum value between two numbers
@@ -138,5 +141,20 @@ public static void AssertCompleted(in this ParallelLoopResult result)
138141
{
139142
if (!result.IsCompleted) throw new InvalidOperationException("Error while performing the parallel loop");
140143
}
144+
145+
/// <summary>
146+
/// Removes the left spaces from the input verbatim string
147+
/// </summary>
148+
/// <param name="text">The string to trim</param>
149+
[Pure, NotNull]
150+
public static String TrimVerbatim([NotNull] this String text)
151+
{
152+
String[] lines = text.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
153+
return lines.Aggregate(new StringBuilder(), (b, s) =>
154+
{
155+
b.AppendLine(s.Trim());
156+
return b;
157+
}).ToString();
158+
}
141159
}
142160
}

NeuralNetwork.NET/Helpers/TensorMap.cs

Lines changed: 0 additions & 104 deletions
This file was deleted.

NeuralNetwork.NET/Helpers/TrainingProgressExportHelpers.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using JetBrains.Annotations;
66
using NeuralNetworkNET.APIs.Enums;
77
using NeuralNetworkNET.APIs.Results;
8+
using NeuralNetworkNET.Extensions;
89

910
namespace NeuralNetworkNET.Helpers
1011
{
@@ -20,7 +21,7 @@ public static class TrainingProgressExportHelpers
2021
plt.ylabel(""$YLABEL$"")
2122
plt.xlabel(""Epoch"")
2223
plt.plot(x)
23-
plt.show()".Split(new[] { Environment.NewLine }, StringSplitOptions.None).Aggregate(String.Empty, (s, l) => $"{s}{l.Trim()}{Environment.NewLine}");
24+
plt.show()".TrimVerbatim();
2425

2526
// The custom 4-spaces indentation for the data points (the \t character is not consistent across different editors)
2627
private const String Tab = " ";

Unit/NeuralNetwork.NET.Unit/MiscTest.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,26 @@ public void ThresholdAccuracyTest()
5252
Assert.IsFalse(tester(yHat, y1));
5353
Assert.IsTrue(tester(yHat, y2));
5454
}
55+
56+
[TestMethod]
57+
public void TrimVerbatim()
58+
{
59+
const String text = @"import matplotlib.pyplot as plt
60+
x = [$VALUES$]
61+
plt.grid(linestyle=""dashed"")
62+
plt.ylabel(""$YLABEL$"")
63+
plt.xlabel(""Epoch"")
64+
plt.plot(x)
65+
plt.show()";
66+
const String expected = @"import matplotlib.pyplot as plt
67+
x = [$VALUES$]
68+
plt.grid(linestyle=""dashed"")
69+
plt.ylabel(""$YLABEL$"")
70+
plt.xlabel(""Epoch"")
71+
plt.plot(x)
72+
plt.show()
73+
";
74+
Assert.IsTrue(text.TrimVerbatim().Equals(expected));
75+
}
5576
}
5677
}

0 commit comments

Comments
 (0)