-
Notifications
You must be signed in to change notification settings - Fork 5
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
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 2bbab74
Added load HMM module workflow
ncguilbeault 967f85e
Added model parameters class to be used for initializing HMM model
ncguilbeault 5fe0ae3
Moved to Bonsai.ML core namespace
ncguilbeault 4f43fed
Added image
ncguilbeault cab2f9d
Updated csproj
ncguilbeault 3b76ee3
Updated load hmm module to use main script
ncguilbeault 8d41b1d
Added main script
ncguilbeault 40b4c73
Updated model params class
ncguilbeault bee4bac
added most likely state class
ncguilbeault 201917e
Updated to remove pydantic data classes
ncguilbeault 5a1a0bb
Added json serialization
ncguilbeault 7674ed8
Updated main python script
ncguilbeault ac8b0e1
Added state class
ncguilbeault dd4ab96
updated model params class
ncguilbeault 41f0ea1
Downgraded package version
ncguilbeault a72b0ce
Updated main python script format
ncguilbeault feb02fd
Added item to extract means and stds from observations
ncguilbeault 49b283f
Added visualizer to show state observation statistics
ncguilbeault 3db716d
Added update plot call
ncguilbeault 64fb552
Added create HMM workflow
ncguilbeault d1cfe66
Updated main python script to correctly compute log_alpha for inferri…
ncguilbeault b688e6d
Added state observation statistics visualizer and workflow
ncguilbeault d91b117
Updated package to use correct function for online state inference
ncguilbeault 5b787de
Updated bar series base
ncguilbeault 5f496d3
Updated state observation statistics visualizer
ncguilbeault 5e3128b
Added new state probability visualizer
ncguilbeault 5dab879
Updated script to ensure state probabilities attribute
ncguilbeault fd2df97
Updated bar plot to allow optional stroke color
ncguilbeault 3ca6ae4
index on hidden-markov-models-dev: 4efde25 Updated bar plot to allow …
ncguilbeault 7d2c6c7
Updated create HMM model workflow
ncguilbeault 86d9f19
Updated main python script for running fit asynchronously
ncguilbeault 926e12b
Added fit async workflow
ncguilbeault 59dfd11
Added check fit finished workflow
ncguilbeault 64fce50
Updated model parameters to use shared subject instead of subscribing…
ncguilbeault ade63bf
Updated state probability visualizer for consistent state labels
ncguilbeault 199ce3c
Added clusters visualizer
ncguilbeault 618b2e9
Updated main python script for clusters visualizer
ncguilbeault 70183ad
Added clusters visualizer include workflow
ncguilbeault 45870a1
Updated run fit async workflow to include vars to estimate
ncguilbeault 2344e2e
Added vars to estimate in fit async function
ncguilbeault 2346fee
Added HMM project to solution
ncguilbeault 9fb3775
Added new observations namespace and expanded observations
ncguilbeault 8207f39
Added tuple type to python helper
ncguilbeault 197da5d
Updated project to target net standard 2.0 fx
ncguilbeault 9a8bc20
Modified observation classes
ncguilbeault 79eed01
Updated create hmm model to accept different observation models
ncguilbeault 3527fd7
Updated main python script for different observation models
ncguilbeault 1b12e55
Removed debugging lines
ncguilbeault fdf4af6
Updated to string method to include keyword
ncguilbeault f066532
Removed autoregressive observations
ncguilbeault d67356e
Modified to use json string converter for enum types
ncguilbeault 54e1d30
Updated main python script to accept observation kwargs
ncguilbeault 30989d2
Updated model parameters to use custom observation type json converter
ncguilbeault a12d8d0
Added state parameters json converter
ncguilbeault d318bdf
Updated state parameters to construct observations with optional kwargs
ncguilbeault 531d8cb
Modified if-else statement to use null-coalescing operator
ncguilbeault e3164b0
Added new autoregressive observations class
ncguilbeault 0097502
Modified existing observation classes to only deserialize params obje…
ncguilbeault 0e5ef41
Added autoregressive observations
ncguilbeault ac48eff
Added custom observation type json converter
ncguilbeault 52cebf6
Removed version tag from csproj
ncguilbeault 00eb572
Changed name of state observation clusters/statistics to gaussian obs…
ncguilbeault f56bcfe
Changed names from just observation to observations
ncguilbeault c0351b3
Modified observations to observations base
ncguilbeault a3287d6
Updated include workflows with new names
ncguilbeault d5ade3b
Set batch observations and most probable states to default on init
ncguilbeault 624a441
Updated observations names
ncguilbeault 21b03c3
Updated gaussian observations statistics class and visualizers
ncguilbeault 13dfd5a
Modified python script to infer states from batch after fitting
ncguilbeault 871cd4c
Fixed issue in calculating covariance matrices
ncguilbeault a3004ef
Update axes titles on change in dimension selection
ncguilbeault ee6bb67
Updated fitting include workflow to use generalized observation params
ncguilbeault 347cf81
Updated script to use scipy implementation of logsumexp
ncguilbeault c7ad5d6
Updated language version in csproj
ncguilbeault 1d1ae07
Updated observations to dissociate observations model from observatio…
ncguilbeault d13cb16
Updated include workflow
ncguilbeault a3f54a1
Update to only permute if observations are gaussian
ncguilbeault 5ee0ec0
Added new python string builder class
ncguilbeault b12756b
Updated classes to inherit from python string builder
ncguilbeault b5b894f
Added XML documentation
ncguilbeault 86a7290
Removed description attributes
ncguilbeault bf85461
Added categorical observations
ncguilbeault 704d42e
Added transition matrices base class and first transitions model (sta…
ncguilbeault b396d21
Updated main python script to support custom transition models
ncguilbeault e164fc7
Updated state parameters to use transitions model
ncguilbeault 1407393
Updated model parameters to use updated state parameters
ncguilbeault 7419f07
Externalized transitions type property in create model include workflow
ncguilbeault 035f8f4
Modified run fit async function call to use transition params keyword
ncguilbeault bcb437f
Added comma in string builder line
ncguilbeault 143d96e
Updated naming ins state parameters
ncguilbeault 1215592
Fixed LogPs data type
ncguilbeault 4382d03
Updated python helper to handle case when object is none
ncguilbeault 2eefa02
Updated with newest language features
ncguilbeault d7e3586
Updated main python script to automatically convert lists to numpy ar…
ncguilbeault 9af1146
Added custom JSON converter for model parameters
ncguilbeault 6d5a7bb
Improved document formatting
ncguilbeault 2a520f2
Simplified state parameters object creation
ncguilbeault 8978763
Removed json property attribute on lags property
ncguilbeault 8580067
Changed properties from private set to public set
ncguilbeault 6d94d06
Modified constructor to check args before setting
ncguilbeault 83a16ff
Added check to set value to None if null
ncguilbeault ec6edd5
Moved read json and write json methods for consistency
ncguilbeault 95c9578
Added quotes to native string data types.
ncguilbeault df1577c
Added constrained stationary transitions
ncguilbeault f6a554d
Added sticky transitions model
ncguilbeault e51d6e0
Added neural network recurrent transitions
ncguilbeault 5bf4142
Updated stationary transitions for consistency
ncguilbeault 39d5328
Added Bonsai scripting to package references
ncguilbeault a15bc38
Updated main readme to include HMM package
ncguilbeault 40f3fd5
Added HMM docs and basic bonsai workflow
ncguilbeault 787defe
Removed visual c++ redistributable dependency
ncguilbeault 3827cbd
Removed the word powershell
ncguilbeault 8f810e8
Removed unnecessary new keyword
ncguilbeault 7d98c52
Updated bonsai workflow
ncguilbeault 6c4ce19
Changed tip sections for improved readability
ncguilbeault c768bc9
Changed spaces to tabs
ncguilbeault 00404f5
Modified getting started section to better communicate workflow
ncguilbeault ae79d49
Added pickle functions to save/load model
ncguilbeault 4d47b1e
Added operator to format a data type to a python list string
ncguilbeault d48123e
Added comment that it does not support nested enumerables
ncguilbeault 3208ee4
Set default timer frequency to 1s
ncguilbeault 6c03c72
Changed type from long to int
ncguilbeault e5b6837
Updated HMM model implementation general workflow
ncguilbeault 05007b9
Updated package version
ncguilbeault 6593c57
Added bonsai scriting expressions package because of dependency in in…
ncguilbeault 723a3a1
Added new Bonsai.ML.Python project and moved python helpers from the …
ncguilbeault c0cdca8
Updated docs with new names
ncguilbeault 3f2b909
Updated project reference to Bonsai.ML.Python
ncguilbeault 51cce87
Updated classes to use same naming convention as SSM package
ncguilbeault f9fcf50
Added Bonsai.ML.Python import
ncguilbeault 1bb4560
Updated naming to ObservationsModel
ncguilbeault 410b39f
Merged process logic into single class for all classes of observations
ncguilbeault 36c266e
Updated naming to TransitionsModel
ncguilbeault 9ae9ecb
Removed Transitions classes with previous naming convention
ncguilbeault 11158ed
Merged process logic into single class for all types of transitions
ncguilbeault 2eca793
Removed old transitions lookup
ncguilbeault 634d6d5
Downgraded package reference for compatibility
ncguilbeault 5e082ad
Update csproj to reference Bonsai.ML project
ncguilbeault 9cd0ec6
Update include workflow to reference the correct class
ncguilbeault 4834edd
Updated include workflow for correct property naming
ncguilbeault 78aeaf1
Updated naming to avoid clashes with underlying ssm package
ncguilbeault 9cd0de2
Updated to support recurrent neural networks properly
ncguilbeault 458a200
Fixed issue in python class with nonlinearity naming clash
ncguilbeault 349bc9e
Updated variable name
ncguilbeault 9542837
Updated properties to hidden
ncguilbeault e64824e
Updated build string to fix issue when param is null
ncguilbeault 53e9b77
Updated all names with s for consistency
ncguilbeault fd01db4
Added abstract python model
ncguilbeault 0ecac05
Update include workflow for new naming
ncguilbeault c02d662
Fixed issue with retrieving nonlinearity type of nn_recurrent
ncguilbeault 5da19bc
Fixed issue with deserializing observations
ncguilbeault f680093
Fixed JSON naming
ncguilbeault 46795ec
Hide properties
ncguilbeault b17377e
Change base model classes to inherit from python model
ncguilbeault 6505dd6
Update observations to reflect new model structure
ncguilbeault 066d86e
Updated wording in README
ncguilbeault f2ee345
Updated naming to better reflect target type
ncguilbeault c59dc46
Fixed incorrect naming in XML doc
ncguilbeault e501bbe
Updated XML doc strings
ncguilbeault fb25c01
Updated readme
ncguilbeault 585d275
Added Bonsai.ML.Data project for common data tools
ncguilbeault d1be315
Added array helper to data project
ncguilbeault a3197bf
Updated projects to utilize shared element icon
ncguilbeault ba56460
Updated to use Bonsai.ML.Data
ncguilbeault bdf325c
Moved numpy parser to array tools since this process is more generic …
ncguilbeault 73aa221
Added XML docs
ncguilbeault 2b6ba24
Moved formatter to Bonsai.ML.Python and added support for nesting dat…
ncguilbeault 4be22ea
Removed depth argument
ncguilbeault 028768b
Moved action definitions directly to CreateTypeHandler function.
ncguilbeault 71d71ba
Updated Exception to ArgumentException
ncguilbeault b08e183
Updated to pass arguments in exception
ncguilbeault 7e3e7cf
Update hmm docs
ncguilbeault File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added operator to format a data type to a python list string
- Loading branch information
commit 4d47b1ea6645d88cb2073b62cf7d01a2cc6040d9
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System.Collections; | ||
using System.Linq; | ||
using System.Text; | ||
using System.ComponentModel; | ||
using System.Reactive.Linq; | ||
using System; | ||
|
||
namespace Bonsai.ML.HiddenMarkovModels | ||
{ | ||
/// <summary> | ||
/// Represents an operator that can convert an object into a properly formatted string that is consistent with a python list. | ||
/// For example, a tuple (1, 2, 3) will be converted to the string "[1, 2, 3]". | ||
/// </summary> | ||
[Combinator] | ||
[Description("Converts an object into a properly formatted string that is consistent with a python list.")] | ||
[WorkflowElementCategory(ElementCategory.Transform)] | ||
public class FormatToPythonList | ||
{ | ||
/// <summary> | ||
/// Transforms the elements of an observable sequence into a properly formatted string that is consistent with a python list. | ||
/// </summary> | ||
public IObservable<string> Process<TSource>(IObservable<TSource> source) | ||
{ | ||
return source.Select(value => PythonListHelper.ConvertToPythonListString(value)); | ||
} | ||
} | ||
|
||
internal static class PythonListHelper | ||
{ | ||
public static string ConvertToPythonListString(object obj) | ||
{ | ||
return $"[{ConvertToCommaSeparatedString(obj)}]"; | ||
} | ||
|
||
public static string ConvertToCommaSeparatedString(object obj) | ||
{ | ||
if (obj == null) return "None"; | ||
|
||
if (obj is string) | ||
{ | ||
return obj.ToString(); | ||
} | ||
|
||
if (obj is IEnumerable enumerable) | ||
{ | ||
var sb = new StringBuilder(); | ||
foreach (var item in enumerable) | ||
{ | ||
if (sb.Length > 0) | ||
{ | ||
sb.Append(", "); | ||
} | ||
sb.Append(ConvertToCommaSeparatedString(item)); | ||
} | ||
return sb.ToString(); | ||
} | ||
|
||
var tupleType = obj.GetType(); | ||
if (tupleType.IsGenericType && tupleType.FullName.StartsWith("System.Tuple")) | ||
{ | ||
var values = tupleType.GetProperties() | ||
.Where(p => p.Name.StartsWith("Item")) | ||
.Select(p => p.GetValue(obj)); | ||
|
||
var sb = new StringBuilder(); | ||
foreach (var value in values) | ||
{ | ||
if (sb.Length > 0) | ||
{ | ||
sb.Append(", "); | ||
} | ||
sb.Append(ConvertToCommaSeparatedString(value)); | ||
} | ||
return sb.ToString(); | ||
} | ||
|
||
if (tupleType.IsValueType && tupleType.FullName.StartsWith("System.ValueTuple")) | ||
{ | ||
var fields = tupleType.GetFields() | ||
.Where(f => f.Name.StartsWith("Item")) | ||
.Select(f => f.GetValue(obj)); | ||
|
||
var sb = new StringBuilder(); | ||
foreach (var field in fields) | ||
{ | ||
if (sb.Length > 0) | ||
{ | ||
sb.Append(", "); | ||
} | ||
sb.Append(ConvertToCommaSeparatedString(field)); | ||
} | ||
return sb.ToString(); | ||
} | ||
|
||
return obj.ToString(); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how critical performance might be in this path, but we could optimize this code to use generated code rather than runtime reflection. This would mean reflection is done only once at build time to generate the code for the conversion, which then can run as fast as manually typed code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with you and think we should optimize this. Performance could become important when dealing with large tuples/arrays. I'm curious what you have in mind. Would you do this using expressions? I can imagine using a formatted string for types where the number of items is known, but not sure how this would work with variable length types.