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

Refactor visualizers package to improve separation between package specific visualizers and reusable visualizers #23

Merged
merged 8 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
12 changes: 6 additions & 6 deletions Bonsai.ML.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
Expand Down Expand Up @@ -26,6 +26,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bonsai.ML.Python", "src\Bon
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bonsai.ML.Data", "src\Bonsai.ML.Data\Bonsai.ML.Data.csproj", "{A135C7DB-EA50-4FC6-A6CB-6A5A5CC5FA13}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bonsai.ML.Visualizers.LinearDynamicalSystems", "src\Bonsai.ML.Visualizers.LinearDynamicalSystems\Bonsai.ML.Visualizers.LinearDynamicalSystems.csproj", "{17DF50BE-F481-4904-A4C8-5DF9725B2CA1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bonsai.ML.Visualizers.HiddenMarkovModels", "src\Bonsai.ML.Visualizers.HiddenMarkovModels\Bonsai.ML.Visualizers.HiddenMarkovModels.csproj", "{FC395DDC-62A4-4E14-A198-272AB05B33C7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -56,22 +60,18 @@ Global
{39A4414F-52B1-42D7-82FA-E65DAD885264}.Debug|Any CPU.Build.0 = Debug|Any CPU
{39A4414F-52B1-42D7-82FA-E65DAD885264}.Release|Any CPU.ActiveCfg = Release|Any CPU
{39A4414F-52B1-42D7-82FA-E65DAD885264}.Release|Any CPU.Build.0 = Release|Any CPU
{A135C7DB-EA50-4FC6-A6CB-6A5A5CC5FA13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A135C7DB-EA50-4FC6-A6CB-6A5A5CC5FA13}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A135C7DB-EA50-4FC6-A6CB-6A5A5CC5FA13}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A135C7DB-EA50-4FC6-A6CB-6A5A5CC5FA13}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{FC395DDC-62A4-4E14-A198-272AB05B33C7} = {12312384-8828-4786-AE19-EFCEDF968290}
{AA6BE73F-1E15-49A5-AC3C-CD069035C940} = {12312384-8828-4786-AE19-EFCEDF968290}
{17AABD18-E275-4409-9E33-3D755B809FF6} = {12312384-8828-4786-AE19-EFCEDF968290}
{196AA5C7-AE8A-477B-B01A-B94676EC60EE} = {12312384-8828-4786-AE19-EFCEDF968290}
{81DB65B3-EA65-4947-8CF1-0E777324C082} = {461FE3E2-21C4-47F9-8405-DF72326AAB2B}
{BAD0A733-8EFB-4EAF-9648-9851656AF7FF} = {12312384-8828-4786-AE19-EFCEDF968290}
{39A4414F-52B1-42D7-82FA-E65DAD885264} = {12312384-8828-4786-AE19-EFCEDF968290}
{A135C7DB-EA50-4FC6-A6CB-6A5A5CC5FA13} = {12312384-8828-4786-AE19-EFCEDF968290}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B6468F13-97CD-45E0-9E1E-C122D7F1E09F}
Expand Down
6 changes: 6 additions & 0 deletions README.md
glopesdev marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ Facilitates inference using Hidden Markov Models (HMMs). It interfaces with the
### Bonsai.ML.Visualizers
Graphing and plotting library for visualizing data.

- **Bonsai.ML.Visualizers.LinearDynamicalSystems**
Visualizers specific to the LinearDynamicalSystems package.

- **Bonsai.ML.Visualizers.HiddenMarkovModels**
Visualizers specific to the HiddenMarkovModels package.

> [!NOTE]
> Bonsai.ML packages can be installed through Bonsai's integrated package manager and are generally ready for immediate use. However, some packages may require additional installation steps. Refer to the specific package section for detailed installation guides and documentation.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Title>Bonsai.ML.Visualizers.HiddenMarkovModels</Title>
<Description>A package for visualizing data from the Bonsai.ML.HiddenMarkovModels package.</Description>
<PackageTags>Bonsai Rx ML Machine Learning Visualizers Hidden Markov Models</PackageTags>
<TargetFramework>net472</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Bonsai.Core" Version="2.8.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Bonsai.ML.Visualizers\Bonsai.ML.Visualizers.csproj" />
<ProjectReference Include="..\Bonsai.ML.HiddenMarkovModels\Bonsai.ML.HiddenMarkovModels.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
using System.Collections.Generic;
using Bonsai;
using Bonsai.Design;
using Bonsai.ML.Visualizers;
using Bonsai.ML.Visualizers.HiddenMarkovModels;
glopesdev marked this conversation as resolved.
Show resolved Hide resolved
using Bonsai.ML.HiddenMarkovModels.Observations;
using OxyPlot;
using OxyPlot.Series;
using OxyPlot.Axes;
using OxyPlot.WindowsForms;
using MathNet.Numerics.LinearAlgebra;

[assembly: TypeVisualizer(typeof(GaussianObservationsStatisticsClustersVisualizer), Target = typeof(GaussianObservationsStatistics))]

namespace Bonsai.ML.Visualizers
namespace Bonsai.ML.Visualizers.HiddenMarkovModels
{
/// <summary>
/// Provides a type visualizer of <see cref="GaussianObservationsStatistics"/> to display how the observations
Expand Down Expand Up @@ -245,40 +244,20 @@ public override void Show(object value)
var yVar = gaussianObservationsStatistics.CovarianceMatrices[i, dimension2SelectedIndex, dimension2SelectedIndex];
var xyCov = gaussianObservationsStatistics.CovarianceMatrices[i, dimension2SelectedIndex, dimension1SelectedIndex];

var covariance = Matrix<double>.Build.DenseOfArray(new double[,] {
{
xVar,
xyCov
},
{
xyCov,
yVar
},
});

var evd = covariance.Evd();
var evals = evd.EigenValues.Real();
evals = evals.PointwiseAbsoluteMaximum(0);
var evecs = evd.EigenVectors;

double angle = Math.Atan2(evecs[1, 0], evecs[0, 0]);
var ellipseParameters = EllipseHelper.GetEllipseParameters(xVar, yVar, xyCov);

for (int j = 1; j < 4; j++)
{

var majorAxis = j * Math.Sqrt(evals[0]);
var minorAxis = j * Math.Sqrt(evals[1]);

var points = new List<DataPoint>();
int numPoints = 100;
for (int k = 0; k < numPoints + 1; k++)
{
double theta = 2 * Math.PI * k / numPoints;
double x = majorAxis * Math.Cos(theta);
double y = minorAxis * Math.Sin(theta);
double x = j * ellipseParameters.MajorAxis * Math.Cos(theta);
double y = j * ellipseParameters.MinorAxis * Math.Sin(theta);

double xRot = x * Math.Cos(angle) - y * Math.Sin(angle);
double yRot = x * Math.Sin(angle) + y * Math.Cos(angle);
double xRot = x * Math.Cos(ellipseParameters.Angle) - y * Math.Sin(ellipseParameters.Angle);
double yRot = x * Math.Sin(ellipseParameters.Angle) + y * Math.Cos(ellipseParameters.Angle);

points.Add(new DataPoint(xMean + xRot, yMean + yRot));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
using System.Collections.Generic;
using Bonsai;
using Bonsai.Design;
using Bonsai.ML.Visualizers;
using Bonsai.ML.Visualizers.HiddenMarkovModels;
using Bonsai.ML.HiddenMarkovModels.Observations;
using OxyPlot;
using OxyPlot.Series;

[assembly: TypeVisualizer(typeof(GaussianObservationStatisticsVisualizer), Target = typeof(GaussianObservationsStatistics))]

namespace Bonsai.ML.Visualizers
namespace Bonsai.ML.Visualizers.HiddenMarkovModels
{
/// <summary>
/// Provides a type visualizer of <see cref="GaussianObservationsStatistics"/> to display the means and standard
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"profiles": {
"Bonsai": {
"commandName": "Executable",
"executablePath": "$(registry:HKEY_CURRENT_USER\\Software\\Bonsai Foundation\\Bonsai@InstallDir)Bonsai.exe",
"commandLineArgs": "--lib:\"$(TargetDir).\"",
"nativeDebugging": true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
using System.Linq;
using Bonsai;
using Bonsai.Design;
using Bonsai.ML.Visualizers;
using Bonsai.ML.Visualizers.HiddenMarkovModels;
using Bonsai.ML.HiddenMarkovModels;
using OxyPlot;
using OxyPlot.Series;
using OxyPlot.Axes;

[assembly: TypeVisualizer(typeof(StateProbabilityVisualizer), Target = typeof(StateProbability))]

namespace Bonsai.ML.Visualizers
namespace Bonsai.ML.Visualizers.HiddenMarkovModels
{
/// <summary>
/// Provides a type visualizer of <see cref="StateProbability"/> to display the probabilities
Expand Down Expand Up @@ -67,7 +67,7 @@ public override void Show(object value)
var paddingPercentage = 0.05;

var nStates = stateProbability.Probabilities.Length;
CategoryAxis categoryAxis = (CategoryAxis)Plot.xAxis;
CategoryAxis categoryAxis = (CategoryAxis)Plot.XAxis;
categoryAxis.ItemsSource = Enumerable.Range(0, nStates);

for (int i = 0; i < nStates; i++)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Title>Bonsai.ML.Visualizers.LinearDynamicalSystems</Title>
<Description>A package for the Bonsai visual programming language.</Description>
<PackageTags>Bonsai Rx ML Machine Learning Visualizers LinearDynamicalSystems</PackageTags>
<TargetFramework>net472</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Bonsai.ML.Visualizers\Bonsai.ML.Visualizers.csproj" />
<ProjectReference Include="..\Bonsai.ML.LinearDynamicalSystems\Bonsai.ML.LinearDynamicalSystems.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Bonsai.Vision.Design" Version="2.8.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Bonsai.Design;
using Bonsai.Vision.Design;
using Bonsai;
using Bonsai.ML.Visualizers;
using Bonsai.ML.Visualizers.LinearDynamicalSystems;
using Bonsai.ML.LinearDynamicalSystems.Kinematics;
using System;
using System.Collections.Generic;
Expand All @@ -11,7 +11,7 @@

[assembly: TypeVisualizer(typeof(ForecastImageOverlay), Target = typeof(MashupSource<ImageMashupVisualizer, ForecastVisualizer>))]

namespace Bonsai.ML.Visualizers
namespace Bonsai.ML.Visualizers.LinearDynamicalSystems
{
/// <summary>
/// Provides a mashup visualizer to display the forecast of a Kalman Filter kinematics model overtime of an ImageMashupVisualizer.
Expand Down Expand Up @@ -50,26 +50,17 @@ public override void Show(object value)
double yVar = kinematicState.Position.Y.Variance;
double xyCov = kinematicState.Position.Covariance;

var covariance = Matrix<double>.Build.DenseOfArray(new double[,] {
{ xVar, xyCov },
{ xyCov, yVar }
});

var evd = covariance.Evd();
var evals = evd.EigenValues.Real();
var evecs = evd.EigenVectors;

double angle = Math.Atan2(evecs[1, 0], evecs[0, 0]) * 180 / Math.PI;
EllipseParameters ellipseParameters = EllipseHelper.GetEllipseParameters(xVar, yVar, xyCov);

Size axes = new Size
{
Width = (int)(2 * Math.Sqrt(evals[0])),
Height = (int)(2 * Math.Sqrt(evals[1]))
Width = (int)(2 * ellipseParameters.MajorAxis),
Height = (int)(2 * ellipseParameters.MinorAxis)
};

OxyColor color = OxyColors.Yellow;

CV.Ellipse(overlay, center, axes, angle, 0, 360, new Scalar(color.B, color.G, color.R, color.A), -1);
CV.Ellipse(overlay, center, axes, ellipseParameters.Angle, 0, 360, new Scalar(color.B, color.G, color.R, color.A), -1);
}

CV.AddWeighted(image, 1 - alpha, overlay, alpha, 1, image);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Bonsai.Design;
using Bonsai;
using Bonsai.ML.Visualizers;
using Bonsai.ML.Visualizers.LinearDynamicalSystems;
using Bonsai.ML.LinearDynamicalSystems;
using Bonsai.ML.LinearDynamicalSystems.Kinematics;
using System;
Expand All @@ -10,10 +10,10 @@

[assembly: TypeVisualizer(typeof(ForecastPlotOverlay), Target = typeof(MashupSource<KinematicStateVisualizer, ForecastVisualizer>))]

namespace Bonsai.ML.Visualizers
namespace Bonsai.ML.Visualizers.LinearDynamicalSystems
{
/// <summary>
/// Provides a mashup visualizer to display the forecast of a Kalman Filter kinematics model overtime of a KinematicStateVisualizer.
/// Provides a mashup visualizer to display the forecast of a Kalman Filter kinematics model alongside the kinematic state.
/// </summary>
public class ForecastPlotOverlay : DialogTypeVisualizer
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Collections.Generic;
using Bonsai;
using Bonsai.Design;
using Bonsai.ML.Visualizers;
using Bonsai.ML.Visualizers.LinearDynamicalSystems;
using Bonsai.ML.LinearDynamicalSystems.Kinematics;
using OxyPlot;
using System.Reactive;
Expand All @@ -12,7 +12,7 @@

[assembly: TypeVisualizer(typeof(ForecastVisualizer), Target = typeof(Forecast))]

namespace Bonsai.ML.Visualizers
namespace Bonsai.ML.Visualizers.LinearDynamicalSystems
{
/// <summary>
/// Provides a type visualizer to display the forecast of a Kalman Filter kinematics model.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Bonsai.Design;
using Bonsai;
using Bonsai.ML.LinearDynamicalSystems.Kinematics;
using Bonsai.ML.Visualizers;
using Bonsai.ML.Visualizers.LinearDynamicalSystems;
using System.Collections.Generic;
using System;
using System.Windows.Forms;
Expand All @@ -11,7 +11,7 @@

[assembly: TypeVisualizer(typeof(KinematicStateVisualizer), Target = typeof(KinematicState))]

namespace Bonsai.ML.Visualizers
namespace Bonsai.ML.Visualizers.LinearDynamicalSystems
{

/// <summary>
Expand All @@ -20,7 +20,7 @@ namespace Bonsai.ML.Visualizers
public class KinematicStateVisualizer : MashupVisualizer
{
private TableLayoutPanel container;
private int updateFrequency = 1000 / 50;
private int updateFrequency = 20;
private bool resetAxes = true;
private int rowCount = 3;
private int columnCount = 2;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Reflection;
using Bonsai;
using Bonsai.Design;
using Bonsai.ML.Visualizers;
using Bonsai.ML.LinearDynamicalSystems;
using Bonsai.ML.Visualizers.LinearDynamicalSystems;
using Bonsai.ML.LinearDynamicalSystems.LinearRegression;
using System.Drawing;
using System.Reactive;
using Bonsai.Reactive;

[assembly: TypeVisualizer(typeof(MultivariatePDFVisualizer), Target = typeof(MultivariatePDF))]

namespace Bonsai.ML.Visualizers
namespace Bonsai.ML.Visualizers.LinearDynamicalSystems
{

/// <summary>
/// Provides a type visualizer to display the state components of a Kalman Filter kinematics model.
/// Provides a type visualizer to display a multivariate probability distribution as a heatmap.
/// </summary>
public class MultivariatePDFVisualizer : DialogTypeVisualizer
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"profiles": {
"Bonsai": {
"commandName": "Executable",
"executablePath": "$(registry:HKEY_CURRENT_USER\\Software\\Bonsai Foundation\\Bonsai@InstallDir)Bonsai.exe",
"commandLineArgs": "--lib:\"$(TargetDir).\"",
"nativeDebugging": true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Collections.Generic;
using Bonsai;
using Bonsai.Design;
using Bonsai.ML.Visualizers;
using Bonsai.ML.Visualizers.LinearDynamicalSystems;
using Bonsai.ML.LinearDynamicalSystems;
using OxyPlot;
using System.Reactive;
Expand All @@ -12,7 +12,7 @@

[assembly: TypeVisualizer(typeof(StateComponentVisualizer), Target = typeof(StateComponent))]

namespace Bonsai.ML.Visualizers
namespace Bonsai.ML.Visualizers.LinearDynamicalSystems
{
/// <summary>
/// Provides a type visualizer to display the state components of a Kalman Filter kinematics model.
Expand Down
Loading
Loading