diff --git a/KMeansBase/Csv.cs b/KMeansBase/Csv.cs new file mode 100644 index 0000000..4263282 --- /dev/null +++ b/KMeansBase/Csv.cs @@ -0,0 +1,18 @@ +using System.Globalization; + +namespace KMeansBase; + +public class Csv +{ + public static IEnumerable Parse(string path) + { + var lines = File.ReadLines(path); + foreach (var line in lines) + { + var current = line.Split(','); + if (!double.TryParse(current[0], NumberStyles.Any, CultureInfo.InvariantCulture, out var x)) continue; + if (!double.TryParse(current[1], NumberStyles.Any, CultureInfo.InvariantCulture, out var y)) continue; + yield return new Point(x, y, -1); + } + } +} \ No newline at end of file diff --git a/KMeansBase/KMeans.cs b/KMeansBase/KMeans.cs index de6c65d..71cfc22 100644 --- a/KMeansBase/KMeans.cs +++ b/KMeansBase/KMeans.cs @@ -74,7 +74,6 @@ public static class KMeans var x = (minX + maxX) * rnd.NextDouble(); var y = (minY + maxY) * rnd.NextDouble(); var point = new Point(x, y, i); - Console.WriteLine(point); yield return point; } } diff --git a/KMeansGui/Shell/ShellView.axaml b/KMeansGui/Shell/ShellView.axaml index 9851906..1323f08 100644 --- a/KMeansGui/Shell/ShellView.axaml +++ b/KMeansGui/Shell/ShellView.axaml @@ -12,18 +12,18 @@ - + - + - - + diff --git a/KMeansGui/Shell/ShellViewModel.cs b/KMeansGui/Shell/ShellViewModel.cs index 7472d7c..1cf9263 100644 --- a/KMeansGui/Shell/ShellViewModel.cs +++ b/KMeansGui/Shell/ShellViewModel.cs @@ -1,6 +1,8 @@ using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Avalonia.Controls; +using KMeansBase; using MvvmGen; namespace KMeansGui @@ -9,7 +11,7 @@ namespace KMeansGui public partial class ShellViewModel { [Property] private int _countOfCentroids; - [Property] private List _points; + [Property] private List _points; public async Task OpenCsvFileAsync(object parent) { @@ -24,6 +26,11 @@ namespace KMeansGui fileDialog.Filters.Add(fileDialogFilter); fileDialog.Title = "Select a csv file."; var result = await fileDialog.ShowAsync(window); + if (result?.Length == 1) + { + var path = result.First(); + Points = Csv.Parse(path).ToList(); + } } } } diff --git a/src/Helper.cs b/src/Helper.cs index cd628eb..aa6a3ae 100644 --- a/src/Helper.cs +++ b/src/Helper.cs @@ -1,7 +1,6 @@ using ScottPlot; using System.Diagnostics; using System.Drawing; -using System.Globalization; using Point = KMeansBase.Point; namespace kMeans; @@ -39,18 +38,6 @@ public static class Helper plot.SaveFig(path); } - public static IEnumerable ParseCsv(string path) - { - var lines = File.ReadLines(path); - foreach (var line in lines) - { - var current = line.Split(','); - if (!double.TryParse(current[0], NumberStyles.Any, CultureInfo.InvariantCulture, out var x)) continue; - if (!double.TryParse(current[1], NumberStyles.Any, CultureInfo.InvariantCulture, out var y)) continue; - yield return new Point(x, y, -1); - } - } - public static void PreviewPlot(string path) { using var p = Process.Start(new ProcessStartInfo(path) { UseShellExecute = true }); diff --git a/src/Program.cs b/src/Program.cs index 54cbc11..39882f7 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -1,4 +1,5 @@ -using static kMeans.Helper; +using KMeansBase; +using static kMeans.Helper; namespace kMeans; @@ -11,7 +12,7 @@ public static class Program public static void Main() { Console.WriteLine("k-Means-Algorithm"); - var points = ParseCsv(Coordinates).ToList(); + var points = Csv.Parse(Coordinates).ToList(); KMeansBase.KMeans.KMeansCalculation(points, K, out var centroids);