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

Add functionality to HMM package to load and save HMMs from pickle files #36

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
120 changes: 120 additions & 0 deletions src/Bonsai.ML.HiddenMarkovModels/LoadHMMFromPkl.bonsai
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<WorkflowBuilder Version="2.8.5"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p1="clr-namespace:Bonsai.ML;assembly=Bonsai.ML"
xmlns:rx="clr-namespace:Bonsai.Reactive;assembly=Bonsai.Core"
xmlns:py="clr-namespace:Bonsai.Scripting.Python;assembly=Bonsai.Scripting.Python"
xmlns:p2="clr-namespace:Bonsai.ML.HiddenMarkovModels;assembly=Bonsai.ML.HiddenMarkovModels"
xmlns="https://bonsai-rx.org/2018/workflow">
<Workflow>
<Nodes>
<Expression xsi:type="ExternalizedMapping">
<Property Name="Name" Category="ModelReference" />
</Expression>
<Expression xsi:type="Combinator">
<Combinator xsi:type="p1:CreateModelReference">
<p1:Name>hmm</p1:Name>
</Combinator>
</Expression>
<Expression xsi:type="rx:BehaviorSubject">
<Name>hmm</Name>
</Expression>
<Expression xsi:type="MemberSelector">
<Selector>Name</Selector>
</Expression>
<Expression xsi:type="WorkflowInput">
<Name>Source1</Name>
</Expression>
<Expression xsi:type="ExternalizedMapping">
<Property Name="Value" DisplayName="PathToPklFile" />
</Expression>
<Expression xsi:type="Combinator">
<Combinator xsi:type="StringProperty">
<Value>model.pkl</Value>
</Combinator>
</Expression>
<Expression xsi:type="Combinator">
<Combinator xsi:type="rx:Zip" />
</Expression>
<Expression xsi:type="Combinator">
<Combinator xsi:type="py:ObserveOnGIL" />
</Expression>
<Expression xsi:type="Format">
<Format>{0}=HiddenMarkovModel.load_model("{1}")</Format>
<Selector>Item1,Item2</Selector>
</Expression>
<Expression xsi:type="InputMapping">
<PropertyMappings>
<Property Name="Script" Selector="it" />
</PropertyMappings>
</Expression>
<Expression xsi:type="SubscribeSubject">
<Name>HMMModule</Name>
</Expression>
<Expression xsi:type="PropertyMapping">
<PropertyMappings>
<Property Name="Module" />
</PropertyMappings>
</Expression>
<Expression xsi:type="Combinator">
<Combinator xsi:type="py:Exec">
<py:Script></py:Script>
</Combinator>
</Expression>
<Expression xsi:type="Combinator">
<Combinator xsi:type="rx:Zip" />
</Expression>
<Expression xsi:type="InputMapping">
<PropertyMappings>
<Property Name="VariableName" Selector="Item2" />
</PropertyMappings>
</Expression>
<Expression xsi:type="SubscribeSubject">
<Name>HMMModule</Name>
</Expression>
<Expression xsi:type="PropertyMapping">
<PropertyMappings>
<Property Name="Module" />
</PropertyMappings>
</Expression>
<Expression xsi:type="Combinator">
<Combinator xsi:type="py:Get">
<py:VariableName>hmm</py:VariableName>
</Combinator>
</Expression>
<Expression xsi:type="Combinator">
<Combinator xsi:type="p2:ModelParameters">
<p2:NumStates>2</p2:NumStates>
<p2:Dimensions>2</p2:Dimensions>
<p2:ObservationsModelType>Gaussian</p2:ObservationsModelType>
<p2:TransitionsModelType>Stationary</p2:TransitionsModelType>
</Combinator>
</Expression>
<Expression xsi:type="WorkflowOutput" />
</Nodes>
<Edges>
<Edge From="0" To="1" Label="Source1" />
<Edge From="0" To="2" Label="Source2" />
<Edge From="1" To="2" Label="Source1" />
<Edge From="2" To="3" Label="Source1" />
<Edge From="3" To="7" Label="Source1" />
<Edge From="3" To="14" Label="Source2" />
<Edge From="4" To="6" Label="Source1" />
<Edge From="5" To="6" Label="Source2" />
<Edge From="6" To="7" Label="Source2" />
<Edge From="7" To="8" Label="Source1" />
<Edge From="8" To="9" Label="Source1" />
<Edge From="9" To="10" Label="Source1" />
<Edge From="10" To="13" Label="Source1" />
<Edge From="11" To="12" Label="Source1" />
<Edge From="12" To="13" Label="Source2" />
<Edge From="13" To="14" Label="Source1" />
<Edge From="14" To="15" Label="Source1" />
<Edge From="15" To="18" Label="Source1" />
<Edge From="16" To="17" Label="Source1" />
<Edge From="17" To="18" Label="Source2" />
<Edge From="18" To="19" Label="Source1" />
<Edge From="19" To="20" Label="Source1" />
</Edges>
</Workflow>
</WorkflowBuilder>
81 changes: 81 additions & 0 deletions src/Bonsai.ML.HiddenMarkovModels/SaveHMMToPkl.bonsai
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="utf-8"?>
<WorkflowBuilder Version="2.8.5"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p1="clr-namespace:Bonsai.ML;assembly=Bonsai.ML"
xmlns:rx="clr-namespace:Bonsai.Reactive;assembly=Bonsai.Core"
xmlns:py="clr-namespace:Bonsai.Scripting.Python;assembly=Bonsai.Scripting.Python"
xmlns="https://bonsai-rx.org/2018/workflow">
<Workflow>
<Nodes>
<Expression xsi:type="ExternalizedMapping">
<Property Name="Name" Category="ModelReference" />
</Expression>
<Expression xsi:type="Combinator">
<Combinator xsi:type="p1:CreateModelReference">
<p1:Name>hmm</p1:Name>
</Combinator>
</Expression>
<Expression xsi:type="rx:BehaviorSubject">
<Name>hmm</Name>
</Expression>
<Expression xsi:type="MemberSelector">
<Selector>Name</Selector>
</Expression>
<Expression xsi:type="WorkflowInput">
<Name>Source1</Name>
</Expression>
<Expression xsi:type="ExternalizedMapping">
<Property Name="Value" DisplayName="PathToPklFile" />
</Expression>
<Expression xsi:type="Combinator">
<Combinator xsi:type="StringProperty">
<Value>model.pkl</Value>
</Combinator>
</Expression>
<Expression xsi:type="Combinator">
<Combinator xsi:type="rx:Zip" />
</Expression>
<Expression xsi:type="Combinator">
<Combinator xsi:type="py:ObserveOnGIL" />
</Expression>
<Expression xsi:type="Format">
<Format>{0}.save_model("{1}")</Format>
<Selector>Item1,Item2</Selector>
</Expression>
<Expression xsi:type="InputMapping">
<PropertyMappings>
<Property Name="Expression" Selector="it" />
</PropertyMappings>
</Expression>
<Expression xsi:type="SubscribeSubject">
<Name>HMMModule</Name>
</Expression>
<Expression xsi:type="PropertyMapping">
<PropertyMappings>
<Property Name="Module" />
</PropertyMappings>
</Expression>
<Expression xsi:type="Combinator">
<Combinator xsi:type="py:Eval" />
</Expression>
<Expression xsi:type="WorkflowOutput" />
</Nodes>
<Edges>
<Edge From="0" To="1" Label="Source1" />
<Edge From="0" To="2" Label="Source2" />
<Edge From="1" To="2" Label="Source1" />
<Edge From="2" To="3" Label="Source1" />
<Edge From="3" To="7" Label="Source1" />
<Edge From="4" To="6" Label="Source1" />
<Edge From="5" To="6" Label="Source2" />
<Edge From="6" To="7" Label="Source2" />
<Edge From="7" To="8" Label="Source1" />
<Edge From="8" To="9" Label="Source1" />
<Edge From="9" To="10" Label="Source1" />
<Edge From="10" To="13" Label="Source1" />
<Edge From="11" To="12" Label="Source1" />
<Edge From="12" To="13" Label="Source2" />
<Edge From="13" To="14" Label="Source1" />
</Edges>
</Workflow>
</WorkflowBuilder>
18 changes: 13 additions & 5 deletions src/Bonsai.ML.HiddenMarkovModels/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

npr.seed(0)

class CustomUnpickler(pickle.Unpickler):
def find_class(self, module, name):
if module == 'main' and name == 'HiddenMarkovModel':
return HiddenMarkovModel
return super().find_class(module, name)

class HiddenMarkovModel(HMM):

Expand Down Expand Up @@ -151,11 +156,14 @@ def compute_log_alpha(self, obs, log_alpha=None):

return log_alpha - logsumexp(log_alpha)

def save_model_to_pickle(self, path: str):
pickle.save(self, path)

def load_model_from_pickle(self, path: str):
self = pickle.load(path)
def save_model(self, path: str):
with open(path, "wb") as f:
pickle.dump(self, f)

@classmethod
def load_model(cls, path: str):
with open(path, 'rb') as f:
return CustomUnpickler(f).load()

def fit_async(self,
observation: list[float],
Expand Down