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

Introducing Online Inference with Hidden Markov Models (HMMs) #22

Merged
merged 173 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
173 commits
Select commit Hold shift + click to select a range
0ccc128
Created new HMM package and added to solution
ncguilbeault Jun 4, 2024
2bbab74
Added load HMM module workflow
ncguilbeault Jun 4, 2024
967f85e
Added model parameters class to be used for initializing HMM model
ncguilbeault Jun 4, 2024
5fe0ae3
Moved to Bonsai.ML core namespace
ncguilbeault Jun 5, 2024
4f43fed
Added image
ncguilbeault Jun 5, 2024
cab2f9d
Updated csproj
ncguilbeault Jun 5, 2024
3b76ee3
Updated load hmm module to use main script
ncguilbeault Jun 5, 2024
8d41b1d
Added main script
ncguilbeault Jun 5, 2024
40b4c73
Updated model params class
ncguilbeault Jun 5, 2024
bee4bac
added most likely state class
ncguilbeault Jun 5, 2024
201917e
Updated to remove pydantic data classes
ncguilbeault Jun 5, 2024
5a1a0bb
Added json serialization
ncguilbeault Jun 6, 2024
7674ed8
Updated main python script
ncguilbeault Jun 6, 2024
ac8b0e1
Added state class
ncguilbeault Jun 6, 2024
dd4ab96
updated model params class
ncguilbeault Jun 6, 2024
41f0ea1
Downgraded package version
ncguilbeault Jun 6, 2024
a72b0ce
Updated main python script format
ncguilbeault Jun 6, 2024
feb02fd
Added item to extract means and stds from observations
ncguilbeault Jun 6, 2024
49b283f
Added visualizer to show state observation statistics
ncguilbeault Jun 6, 2024
3db716d
Added update plot call
ncguilbeault Jun 6, 2024
64fb552
Added create HMM workflow
ncguilbeault Jun 12, 2024
d1cfe66
Updated main python script to correctly compute log_alpha for inferri…
ncguilbeault Jun 12, 2024
b688e6d
Added state observation statistics visualizer and workflow
ncguilbeault Jun 12, 2024
d91b117
Updated package to use correct function for online state inference
ncguilbeault Jun 13, 2024
5b787de
Updated bar series base
ncguilbeault Jun 13, 2024
5f496d3
Updated state observation statistics visualizer
ncguilbeault Jun 13, 2024
5e3128b
Added new state probability visualizer
ncguilbeault Jun 13, 2024
5dab879
Updated script to ensure state probabilities attribute
ncguilbeault Jun 13, 2024
fd2df97
Updated bar plot to allow optional stroke color
ncguilbeault Jun 13, 2024
3ca6ae4
index on hidden-markov-models-dev: 4efde25 Updated bar plot to allow …
ncguilbeault Jun 14, 2024
7d2c6c7
Updated create HMM model workflow
ncguilbeault Jun 20, 2024
86d9f19
Updated main python script for running fit asynchronously
ncguilbeault Jun 20, 2024
926e12b
Added fit async workflow
ncguilbeault Jun 20, 2024
59dfd11
Added check fit finished workflow
ncguilbeault Jun 20, 2024
64fce50
Updated model parameters to use shared subject instead of subscribing…
ncguilbeault Jun 20, 2024
ade63bf
Updated state probability visualizer for consistent state labels
ncguilbeault Jun 20, 2024
199ce3c
Added clusters visualizer
ncguilbeault Jun 26, 2024
618b2e9
Updated main python script for clusters visualizer
ncguilbeault Jun 26, 2024
70183ad
Added clusters visualizer include workflow
ncguilbeault Jun 26, 2024
45870a1
Updated run fit async workflow to include vars to estimate
ncguilbeault Jun 26, 2024
2344e2e
Added vars to estimate in fit async function
ncguilbeault Jun 26, 2024
2346fee
Added HMM project to solution
ncguilbeault Jul 1, 2024
9fb3775
Added new observations namespace and expanded observations
ncguilbeault Jun 26, 2024
8207f39
Added tuple type to python helper
ncguilbeault Jul 1, 2024
197da5d
Updated project to target net standard 2.0 fx
ncguilbeault Jul 1, 2024
9a8bc20
Modified observation classes
ncguilbeault Jul 1, 2024
79eed01
Updated create hmm model to accept different observation models
ncguilbeault Jul 1, 2024
3527fd7
Updated main python script for different observation models
ncguilbeault Jul 1, 2024
1b12e55
Removed debugging lines
ncguilbeault Jul 1, 2024
fdf4af6
Updated to string method to include keyword
ncguilbeault Jul 1, 2024
f066532
Removed autoregressive observations
ncguilbeault Jul 1, 2024
d67356e
Modified to use json string converter for enum types
ncguilbeault Jul 1, 2024
54e1d30
Updated main python script to accept observation kwargs
ncguilbeault Jul 9, 2024
30989d2
Updated model parameters to use custom observation type json converter
ncguilbeault Jul 9, 2024
a12d8d0
Added state parameters json converter
ncguilbeault Jul 9, 2024
d318bdf
Updated state parameters to construct observations with optional kwargs
ncguilbeault Jul 9, 2024
531d8cb
Modified if-else statement to use null-coalescing operator
ncguilbeault Jul 9, 2024
e3164b0
Added new autoregressive observations class
ncguilbeault Jul 9, 2024
0097502
Modified existing observation classes to only deserialize params obje…
ncguilbeault Jul 9, 2024
0e5ef41
Added autoregressive observations
ncguilbeault Jul 9, 2024
ac48eff
Added custom observation type json converter
ncguilbeault Jul 9, 2024
52cebf6
Removed version tag from csproj
ncguilbeault Jul 9, 2024
00eb572
Changed name of state observation clusters/statistics to gaussian obs…
ncguilbeault Jul 9, 2024
f56bcfe
Changed names from just observation to observations
ncguilbeault Jul 9, 2024
c0351b3
Modified observations to observations base
ncguilbeault Jul 9, 2024
a3287d6
Updated include workflows with new names
ncguilbeault Jul 9, 2024
d5ade3b
Set batch observations and most probable states to default on init
ncguilbeault Jul 9, 2024
624a441
Updated observations names
ncguilbeault Jul 9, 2024
21b03c3
Updated gaussian observations statistics class and visualizers
ncguilbeault Jul 9, 2024
13dfd5a
Modified python script to infer states from batch after fitting
ncguilbeault Jul 9, 2024
871cd4c
Fixed issue in calculating covariance matrices
ncguilbeault Jul 9, 2024
a3004ef
Update axes titles on change in dimension selection
ncguilbeault Jul 9, 2024
ee6bb67
Updated fitting include workflow to use generalized observation params
ncguilbeault Jul 9, 2024
347cf81
Updated script to use scipy implementation of logsumexp
ncguilbeault Jul 9, 2024
c7ad5d6
Updated language version in csproj
ncguilbeault Jul 9, 2024
1d1ae07
Updated observations to dissociate observations model from observatio…
ncguilbeault Jul 9, 2024
d13cb16
Updated include workflow
ncguilbeault Jul 9, 2024
a3f54a1
Update to only permute if observations are gaussian
ncguilbeault Jul 10, 2024
5ee0ec0
Added new python string builder class
ncguilbeault Jul 10, 2024
b12756b
Updated classes to inherit from python string builder
ncguilbeault Jul 10, 2024
b5b894f
Added XML documentation
ncguilbeault Jul 10, 2024
86a7290
Removed description attributes
ncguilbeault Jul 10, 2024
bf85461
Added categorical observations
ncguilbeault Jul 10, 2024
704d42e
Added transition matrices base class and first transitions model (sta…
ncguilbeault Jul 10, 2024
b396d21
Updated main python script to support custom transition models
ncguilbeault Jul 10, 2024
e164fc7
Updated state parameters to use transitions model
ncguilbeault Jul 10, 2024
1407393
Updated model parameters to use updated state parameters
ncguilbeault Jul 10, 2024
7419f07
Externalized transitions type property in create model include workflow
ncguilbeault Jul 10, 2024
035f8f4
Modified run fit async function call to use transition params keyword
ncguilbeault Jul 10, 2024
bcb437f
Added comma in string builder line
ncguilbeault Jul 10, 2024
143d96e
Updated naming ins state parameters
ncguilbeault Jul 10, 2024
1215592
Fixed LogPs data type
ncguilbeault Jul 10, 2024
4382d03
Updated python helper to handle case when object is none
ncguilbeault Jul 10, 2024
2eefa02
Updated with newest language features
ncguilbeault Jul 10, 2024
d7e3586
Updated main python script to automatically convert lists to numpy ar…
ncguilbeault Jul 10, 2024
9af1146
Added custom JSON converter for model parameters
ncguilbeault Jul 10, 2024
6d5a7bb
Improved document formatting
ncguilbeault Jul 10, 2024
2a520f2
Simplified state parameters object creation
ncguilbeault Jul 10, 2024
8978763
Removed json property attribute on lags property
ncguilbeault Jul 10, 2024
8580067
Changed properties from private set to public set
ncguilbeault Jul 10, 2024
6d94d06
Modified constructor to check args before setting
ncguilbeault Jul 10, 2024
83a16ff
Added check to set value to None if null
ncguilbeault Jul 10, 2024
ec6edd5
Moved read json and write json methods for consistency
ncguilbeault Jul 10, 2024
95c9578
Added quotes to native string data types.
ncguilbeault Jul 10, 2024
df1577c
Added constrained stationary transitions
ncguilbeault Jul 10, 2024
f6a554d
Added sticky transitions model
ncguilbeault Jul 10, 2024
e51d6e0
Added neural network recurrent transitions
ncguilbeault Jul 10, 2024
5bf4142
Updated stationary transitions for consistency
ncguilbeault Jul 10, 2024
39d5328
Added Bonsai scripting to package references
ncguilbeault Jul 11, 2024
a15bc38
Updated main readme to include HMM package
ncguilbeault Jul 11, 2024
40f3fd5
Added HMM docs and basic bonsai workflow
ncguilbeault Jul 11, 2024
787defe
Removed visual c++ redistributable dependency
ncguilbeault Jul 11, 2024
3827cbd
Removed the word powershell
ncguilbeault Jul 11, 2024
8f810e8
Removed unnecessary new keyword
ncguilbeault Jul 11, 2024
7d98c52
Updated bonsai workflow
ncguilbeault Jul 11, 2024
6c4ce19
Changed tip sections for improved readability
ncguilbeault Jul 11, 2024
c768bc9
Changed spaces to tabs
ncguilbeault Jul 11, 2024
00404f5
Modified getting started section to better communicate workflow
ncguilbeault Jul 11, 2024
ae79d49
Added pickle functions to save/load model
ncguilbeault Jul 12, 2024
4d47b1e
Added operator to format a data type to a python list string
ncguilbeault Jul 12, 2024
d48123e
Added comment that it does not support nested enumerables
ncguilbeault Jul 12, 2024
3208ee4
Set default timer frequency to 1s
ncguilbeault Jul 12, 2024
6c03c72
Changed type from long to int
ncguilbeault Jul 15, 2024
e5b6837
Updated HMM model implementation general workflow
ncguilbeault Jul 16, 2024
05007b9
Updated package version
ncguilbeault Jul 16, 2024
6593c57
Added bonsai scriting expressions package because of dependency in in…
ncguilbeault Jul 16, 2024
723a3a1
Added new Bonsai.ML.Python project and moved python helpers from the …
ncguilbeault Sep 2, 2024
c0cdca8
Updated docs with new names
ncguilbeault Sep 2, 2024
3f2b909
Updated project reference to Bonsai.ML.Python
ncguilbeault Sep 2, 2024
51cce87
Updated classes to use same naming convention as SSM package
ncguilbeault Sep 2, 2024
f9fcf50
Added Bonsai.ML.Python import
ncguilbeault Sep 2, 2024
1bb4560
Updated naming to ObservationsModel
ncguilbeault Sep 2, 2024
410b39f
Merged process logic into single class for all classes of observations
ncguilbeault Sep 2, 2024
36c266e
Updated naming to TransitionsModel
ncguilbeault Sep 2, 2024
9ae9ecb
Removed Transitions classes with previous naming convention
ncguilbeault Sep 2, 2024
11158ed
Merged process logic into single class for all types of transitions
ncguilbeault Sep 2, 2024
2eca793
Removed old transitions lookup
ncguilbeault Sep 2, 2024
634d6d5
Downgraded package reference for compatibility
ncguilbeault Sep 2, 2024
5e082ad
Update csproj to reference Bonsai.ML project
ncguilbeault Sep 2, 2024
9cd0ec6
Update include workflow to reference the correct class
ncguilbeault Sep 2, 2024
4834edd
Updated include workflow for correct property naming
ncguilbeault Sep 2, 2024
78aeaf1
Updated naming to avoid clashes with underlying ssm package
ncguilbeault Sep 2, 2024
9cd0de2
Updated to support recurrent neural networks properly
ncguilbeault Sep 2, 2024
458a200
Fixed issue in python class with nonlinearity naming clash
ncguilbeault Sep 3, 2024
349bc9e
Updated variable name
ncguilbeault Sep 3, 2024
9542837
Updated properties to hidden
ncguilbeault Sep 3, 2024
e64824e
Updated build string to fix issue when param is null
ncguilbeault Sep 3, 2024
53e9b77
Updated all names with s for consistency
ncguilbeault Sep 3, 2024
fd01db4
Added abstract python model
ncguilbeault Sep 3, 2024
0ecac05
Update include workflow for new naming
ncguilbeault Sep 3, 2024
c02d662
Fixed issue with retrieving nonlinearity type of nn_recurrent
ncguilbeault Sep 3, 2024
5da19bc
Fixed issue with deserializing observations
ncguilbeault Sep 3, 2024
f680093
Fixed JSON naming
ncguilbeault Sep 3, 2024
46795ec
Hide properties
ncguilbeault Sep 3, 2024
b17377e
Change base model classes to inherit from python model
ncguilbeault Sep 3, 2024
6505dd6
Update observations to reflect new model structure
ncguilbeault Sep 3, 2024
066d86e
Updated wording in README
ncguilbeault Sep 3, 2024
f2ee345
Updated naming to better reflect target type
ncguilbeault Sep 3, 2024
c59dc46
Fixed incorrect naming in XML doc
ncguilbeault Sep 3, 2024
e501bbe
Updated XML doc strings
ncguilbeault Sep 3, 2024
fb25c01
Updated readme
ncguilbeault Sep 4, 2024
585d275
Added Bonsai.ML.Data project for common data tools
ncguilbeault Sep 4, 2024
d1be315
Added array helper to data project
ncguilbeault Sep 4, 2024
a3197bf
Updated projects to utilize shared element icon
ncguilbeault Sep 4, 2024
ba56460
Updated to use Bonsai.ML.Data
ncguilbeault Sep 4, 2024
bdf325c
Moved numpy parser to array tools since this process is more generic …
ncguilbeault Sep 4, 2024
73aa221
Added XML docs
ncguilbeault Sep 4, 2024
2b6ba24
Moved formatter to Bonsai.ML.Python and added support for nesting dat…
ncguilbeault Sep 4, 2024
4be22ea
Removed depth argument
ncguilbeault Sep 4, 2024
028768b
Moved action definitions directly to CreateTypeHandler function.
ncguilbeault Sep 4, 2024
71d71ba
Updated Exception to ArgumentException
ncguilbeault Sep 5, 2024
b08e183
Updated to pass arguments in exception
ncguilbeault Sep 5, 2024
7e3e7cf
Update hmm docs
ncguilbeault Sep 5, 2024
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
Prev Previous commit
Next Next commit
Changed names from just observation to observations
  • Loading branch information
ncguilbeault committed Jul 9, 2024
commit f56bcfe11ccd8c7a3f603b571ea72013fd70ffaf
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Bonsai.ML.HiddenMarkovModels.Observations
[Description("")]
[WorkflowElementCategory(ElementCategory.Source)]
[JsonObject(MemberSerialization.OptIn)]
public class AutoRegressiveObservations : ObservationParams<AutoRegressiveObservations>
public class AutoRegressiveObservations : Observations<AutoRegressiveObservations>
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Bonsai.ML.HiddenMarkovModels.Observations
[Description("")]
[WorkflowElementCategory(ElementCategory.Transform)]
[JsonObject(MemberSerialization.OptIn)]
public class BernoulliObservations : ObservationParams<BernoulliObservations>
public class BernoulliObservations : Observations<BernoulliObservations>
{
/// <summary>
/// The logit P of the observations for each state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Bonsai.ML.HiddenMarkovModels.Observations
[Description("")]
[WorkflowElementCategory(ElementCategory.Transform)]
[JsonObject(MemberSerialization.OptIn)]
public class ExponentialObservations : ObservationParams<ExponentialObservations>
public class ExponentialObservations : Observations<ExponentialObservations>
{

/// <summary>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Bonsai.ML.HiddenMarkovModels.Observations
[Description("")]
[WorkflowElementCategory(ElementCategory.Transform)]
[JsonObject(MemberSerialization.OptIn)]
public class GaussianObservations : ObservationParams<GaussianObservations>
public class GaussianObservations : Observations<GaussianObservations>
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace Bonsai.ML.HiddenMarkovModels.Observations
{
public abstract class ObservationParams
public abstract class Observations
{
public abstract object[] Params { get; set; }
}

public abstract class ObservationParams<T> : ObservationParams where T : ObservationParams<T>
public abstract class Observations<T> : Observations where T : Observations<T>
{
public abstract override object[] Params { get; set; }
}
Expand Down
22 changes: 11 additions & 11 deletions src/Bonsai.ML.HiddenMarkovModels/Observations/ObservationsLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ namespace Bonsai.ML.HiddenMarkovModels.Observations
{
public static class ObservationsLookup
{
private static readonly Dictionary<ObservationType, (Type Type, string StringValue)> _lookup;
private static readonly Dictionary<ObservationsType, (Type Type, string StringValue)> _lookup;

static ObservationsLookup()
{
_lookup = new Dictionary<ObservationType, (Type, string)>
_lookup = new Dictionary<ObservationsType, (Type, string)>
{
{ ObservationType.Gaussian, (typeof(GaussianObservations), "gaussian") },
{ ObservationType.Exponential, (typeof(ExponentialObservations), "exponential") },
{ ObservationType.Bernoulli, (typeof(BernoulliObservations), "bernoulli") },
{ ObservationType.Poisson, (typeof(PoissonObservations), "poisson") },
{ ObservationType.AutoRegressive, (typeof(AutoRegressiveObservations), "autoregressive") }
{ ObservationsType.Gaussian, (typeof(GaussianObservations), "gaussian") },
{ ObservationsType.Exponential, (typeof(ExponentialObservations), "exponential") },
{ ObservationsType.Bernoulli, (typeof(BernoulliObservations), "bernoulli") },
{ ObservationsType.Poisson, (typeof(PoissonObservations), "poisson") },
{ ObservationsType.AutoRegressive, (typeof(AutoRegressiveObservations), "autoregressive") }
};
}

public static Type GetObservationsClassType(ObservationType type) => _lookup[type].Type;
public static string GetString(ObservationType type) => _lookup[type].StringValue;
public static ObservationType GetFromString(string value) => _lookup.First(x => x.Value.StringValue == value).Key;
public static ObservationType GetFromType(Type type) => _lookup.First(x => x.Value.Type == type).Key;
public static Type GetObservationsClassType(ObservationsType type) => _lookup[type].Type;
public static string GetString(ObservationsType type) => _lookup[type].StringValue;
public static ObservationsType GetFromString(string value) => _lookup.First(x => x.Value.StringValue == value).Key;
public static ObservationsType GetFromType(Type type) => _lookup.First(x => x.Value.Type == type).Key;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels it might make sense to rename this enum as ObservationType. The reason is one of generality. Wouldn't we be able to apply this enum to a single observation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can decide to change this but basically my sense is that everything is pluralized in the ssm package, including the names of the modules observations.py and transitions.py. You could perform inference on a single observation, but I don't think it makes sense to build a model with only a single observation, which is maybe the reason why the observations and transitions model is pluralized throughout the ssm package

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Bonsai.ML.HiddenMarkovModels.Observations
{
public enum ObservationType
public enum ObservationsType
{
Gaussian,
Exponential,
Expand Down
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we rename the enum we should rename this converter as well.

Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace Bonsai.ML.HiddenMarkovModels.Observations
{
public class ObservationTypeJsonConverter : JsonConverter<ObservationType>
public class ObservationsTypeJsonConverter : JsonConverter<ObservationsType>
{
public override void WriteJson(JsonWriter writer, ObservationType value, JsonSerializer serializer)
public override void WriteJson(JsonWriter writer, ObservationsType value, JsonSerializer serializer)
{
writer.WriteValue(GetString(value));
}

public override ObservationType ReadJson(JsonReader reader, Type objectType, ObservationType existingValue, bool hasExistingValue, JsonSerializer serializer)
public override ObservationsType ReadJson(JsonReader reader, Type objectType, ObservationsType existingValue, bool hasExistingValue, JsonSerializer serializer)
{
string stringValue = reader.Value?.ToString();
return GetFromString(stringValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Bonsai.ML.HiddenMarkovModels.Observations
[Description("")]
[WorkflowElementCategory(ElementCategory.Transform)]
[JsonObject(MemberSerialization.OptIn)]
public class PoissonObservations : ObservationParams<PoissonObservations>
public class PoissonObservations : Observations<PoissonObservations>
{

/// <summary>
Expand Down
Loading