add csv import
This commit is contained in:
parent
f7e2b59454
commit
209c3e8545
18
KMeansBase/Csv.cs
Normal file
18
KMeansBase/Csv.cs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
using System.Globalization;
|
||||||
|
|
||||||
|
namespace KMeansBase;
|
||||||
|
|
||||||
|
public class Csv
|
||||||
|
{
|
||||||
|
public static IEnumerable<Point> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -74,7 +74,6 @@ public static class KMeans
|
|||||||
var x = (minX + maxX) * rnd.NextDouble();
|
var x = (minX + maxX) * rnd.NextDouble();
|
||||||
var y = (minY + maxY) * rnd.NextDouble();
|
var y = (minY + maxY) * rnd.NextDouble();
|
||||||
var point = new Point(x, y, i);
|
var point = new Point(x, y, i);
|
||||||
Console.WriteLine(point);
|
|
||||||
yield return point;
|
yield return point;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,18 +12,18 @@
|
|||||||
<kMeansGui:ShellViewModel />
|
<kMeansGui:ShellViewModel />
|
||||||
</Window.DataContext>
|
</Window.DataContext>
|
||||||
<Grid ColumnDefinitions="*,*">
|
<Grid ColumnDefinitions="*,*">
|
||||||
<StackPanel Grid.Row="0" Grid.Column="0" Spacing="10">
|
<StackPanel Grid.Row="0" Grid.Column="0" Spacing="10" Margin="10">
|
||||||
<Button HorizontalAlignment="Stretch" Command="{Binding OpenCsvFileAsync}"
|
<Button HorizontalAlignment="Stretch" Command="{Binding OpenCsvFileAsync}"
|
||||||
CommandParameter="{Binding ElementName=LayoutRoot}">
|
CommandParameter="{Binding ElementName=LayoutRoot}" HorizontalContentAlignment="Center">
|
||||||
Load CSV file
|
Load CSV file
|
||||||
</Button>
|
</Button>
|
||||||
<TextBox UseFloatingWatermark="True" Watermark="Hello" Text="{Binding CountOfCentroids}"></TextBox>
|
<TextBox UseFloatingWatermark="True" Watermark="Count of centroids" Text="{Binding CountOfCentroids}" />
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
<avalonia:Plot Height="150" Grid.Column="1"
|
<avalonia:Plot Grid.Column="1"
|
||||||
PlotMargins="50 0 0 0"
|
PlotMargins="50 0 0 0"
|
||||||
PlotAreaBorderColor="#999999">
|
PlotAreaBorderColor="#999999">
|
||||||
<avalonia:Plot.Series>
|
<avalonia:Plot.Series>
|
||||||
<avalonia:ScatterSeries Items="{Binding Points}" />
|
<avalonia:ScatterSeries Items="{Binding Points}" DataFieldX="X" DataFieldY="Y" />
|
||||||
</avalonia:Plot.Series>
|
</avalonia:Plot.Series>
|
||||||
</avalonia:Plot>
|
</avalonia:Plot>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
|
using KMeansBase;
|
||||||
using MvvmGen;
|
using MvvmGen;
|
||||||
|
|
||||||
namespace KMeansGui
|
namespace KMeansGui
|
||||||
@ -9,7 +11,7 @@ namespace KMeansGui
|
|||||||
public partial class ShellViewModel
|
public partial class ShellViewModel
|
||||||
{
|
{
|
||||||
[Property] private int _countOfCentroids;
|
[Property] private int _countOfCentroids;
|
||||||
[Property] private List<KMeansBase.Point> _points;
|
[Property] private List<Point> _points;
|
||||||
|
|
||||||
public async Task OpenCsvFileAsync(object parent)
|
public async Task OpenCsvFileAsync(object parent)
|
||||||
{
|
{
|
||||||
@ -24,6 +26,11 @@ namespace KMeansGui
|
|||||||
fileDialog.Filters.Add(fileDialogFilter);
|
fileDialog.Filters.Add(fileDialogFilter);
|
||||||
fileDialog.Title = "Select a csv file.";
|
fileDialog.Title = "Select a csv file.";
|
||||||
var result = await fileDialog.ShowAsync(window);
|
var result = await fileDialog.ShowAsync(window);
|
||||||
|
if (result?.Length == 1)
|
||||||
|
{
|
||||||
|
var path = result.First();
|
||||||
|
Points = Csv.Parse(path).ToList();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
using ScottPlot;
|
using ScottPlot;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Globalization;
|
|
||||||
using Point = KMeansBase.Point;
|
using Point = KMeansBase.Point;
|
||||||
|
|
||||||
namespace kMeans;
|
namespace kMeans;
|
||||||
@ -39,18 +38,6 @@ public static class Helper
|
|||||||
plot.SaveFig(path);
|
plot.SaveFig(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IEnumerable<Point> 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)
|
public static void PreviewPlot(string path)
|
||||||
{
|
{
|
||||||
using var p = Process.Start(new ProcessStartInfo(path) { UseShellExecute = true });
|
using var p = Process.Start(new ProcessStartInfo(path) { UseShellExecute = true });
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using static kMeans.Helper;
|
using KMeansBase;
|
||||||
|
using static kMeans.Helper;
|
||||||
|
|
||||||
namespace kMeans;
|
namespace kMeans;
|
||||||
|
|
||||||
@ -11,7 +12,7 @@ public static class Program
|
|||||||
public static void Main()
|
public static void Main()
|
||||||
{
|
{
|
||||||
Console.WriteLine("k-Means-Algorithm");
|
Console.WriteLine("k-Means-Algorithm");
|
||||||
var points = ParseCsv(Coordinates).ToList();
|
var points = Csv.Parse(Coordinates).ToList();
|
||||||
|
|
||||||
KMeansBase.KMeans.KMeansCalculation(points, K, out var centroids);
|
KMeansBase.KMeans.KMeansCalculation(points, K, out var centroids);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user