Skip to content

Commit

Permalink
Added XML documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ncguilbeault committed Jul 10, 2024
1 parent b12756b commit b5b894f
Show file tree
Hide file tree
Showing 24 changed files with 295 additions and 81 deletions.
35 changes: 26 additions & 9 deletions src/Bonsai.ML.HiddenMarkovModels/ModelParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@

namespace Bonsai.ML.HiddenMarkovModels
{
/// <summary>
/// Represents the model parameters of a Hidden Markov Model (HMM).
/// </summary>
[Combinator]
[Description("")]
[Description("Model parameters of a Hidden Markov Model (HMM).")]
[WorkflowElementCategory(ElementCategory.Source)]
public class ModelParameters : PythonStringBuilder
{
Expand All @@ -28,15 +31,15 @@ public class ModelParameters : PythonStringBuilder
/// </summary>
[JsonProperty("num_states")]
[Description("The number of discrete latent states of the HMM model")]
[Category("InitialParameters")]
[Category("ModelSpecification")]
public int NumStates { get => numStates; set { numStates = value; UpdateString(); } }

/// <summary>
/// The dimensionality of the observations into the HMM model.
/// </summary>
[JsonProperty("dimensions")]
[Description("The dimensionality of the observations into the HMM model")]
[Category("InitialParameters")]
[Category("ModelSpecification")]
public int Dimensions { get => dimensions; set { dimensions = value; UpdateString(); } }

/// <summary>
Expand All @@ -45,7 +48,7 @@ public class ModelParameters : PythonStringBuilder
[JsonProperty("observation_type")]
[JsonConverter(typeof(ObservationsTypeJsonConverter))]
[Description("The type of distribution that the HMM will use to model the emission of data observations.")]
[Category("InitialParameters")]
[Category("ModelSpecification")]
public ObservationsType ObservationsType { get => observationsType; set { observationsType = value; UpdateString(); } }

/// <summary>
Expand All @@ -60,7 +63,10 @@ public StateParameters StateParameters
set
{
stateParameters = value;
ObservationsType = stateParameters.Observations.ObservationsType;
if (value != null)
{
ObservationsType = stateParameters.Observations.ObservationsType;
}
UpdateString();
}
}
Expand All @@ -75,6 +81,9 @@ public ModelParameters()
ObservationsType = ObservationsType.Gaussian;
}

/// <summary>
/// Returns an observable sequence of <see cref="ModelParameters"/> objects.
/// </summary>
public IObservable<ModelParameters> Process()
{
return Observable.Return(
Expand All @@ -87,6 +96,10 @@ public IObservable<ModelParameters> Process()
});
}

/// <summary>
/// Takes an observable seqence and returns an observable sequence of <see cref="ModelParameters"/>
/// objects that are emitted every time the input sequence emits a new element.
/// </summary>
public IObservable<ModelParameters> Process<TSource>(IObservable<TSource> source)
{
return Observable.Select(source, item =>
Expand All @@ -101,17 +114,21 @@ public IObservable<ModelParameters> Process<TSource>(IObservable<TSource> source
});
}

/// <summary>
/// Transforms an observable sequence of <see cref="PyObject"/> into an observable sequence
/// of <see cref="ModelParameters"/> objects by accessing internal attributes of the <see cref="PyObject"/>.
/// </summary>
public IObservable<ModelParameters> Process(IObservable<PyObject> source)
{
var sharedSource = source.Publish().RefCount();
var stateParametersObservable = new StateParameters().Process(sharedSource);
return sharedSource.Select(pyObject =>
{
NumStates = pyObject.GetAttr<int>("num_states");
Dimensions = pyObject.GetAttr<int>("dimensions");
numStates = pyObject.GetAttr<int>("num_states");
dimensions = pyObject.GetAttr<int>("dimensions");
var observationsTypeStrPyObj = pyObject.GetAttr<string>("observation_type");

ObservationsType = GetFromString(observationsTypeStrPyObj);
observationsType = GetFromString(observationsTypeStrPyObj);

return new ModelParameters()
{
Expand All @@ -126,12 +143,12 @@ public IObservable<ModelParameters> Process(IObservable<PyObject> source)
});
}

/// <inheritdoc/>
protected override string BuildString()
{
StringBuilder.Clear();
StringBuilder.Append($"num_states={numStates},")
.Append($"dimensions={dimensions},");

if (stateParameters == null)
{
StringBuilder.Append($"observation_type=\"{GetString(observationsType)}\"");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System.ComponentModel;
using System.Collections.Generic;
using Newtonsoft.Json;
using System;

namespace Bonsai.ML.HiddenMarkovModels.Observations
{
/// <summary>
/// Represents an autoregressive observations model.
/// </summary>
[JsonObject(MemberSerialization.OptIn)]
public class AutoRegressiveObservations : ObservationsModel
{
Expand Down Expand Up @@ -43,6 +47,7 @@ public class AutoRegressiveObservations : ObservationsModel

/// <inheritdoc/>
[JsonProperty]
[JsonConverter(typeof(ObservationsTypeJsonConverter))]
public override ObservationsType ObservationsType => ObservationsType.AutoRegressive;

/// <inheritdoc/>
Expand All @@ -67,9 +72,12 @@ public override object[] Params
["lags"] = Lags,
};

/// <summary>
/// Initializes a new instance of the <see cref="AutoRegressiveObservations"/> class.
/// </summary>
public AutoRegressiveObservations (params object[] args)
{
Lags = (int)args[0];
Lags = Convert.ToInt32(args[0]);
UpdateString();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@

namespace Bonsai.ML.HiddenMarkovModels.Observations
{
/// <summary>
/// Represents an operator that is used to create and transform an observable sequence
/// of <see cref="AutoRegressiveObservations"/> objects.
/// </summary>
[Combinator]
[Description("")]
[Description("Creates an observable sequence of AutoRegressiveObservations objects.")]
[WorkflowElementCategory(ElementCategory.Source)]
[JsonObject(MemberSerialization.OptIn)]
public class AutoRegressiveObservationsModel
Expand Down Expand Up @@ -48,6 +52,9 @@ public class AutoRegressiveObservationsModel
[Description("The square root sigmas of the observations for each state.")]
public double[,,] SqrtSigmas { get; private set; } = null;

/// <summary>
/// Returns an observable sequence of <see cref="AutoRegressiveObservations"/> objects.
/// </summary>
public IObservable<AutoRegressiveObservations> Process()
{
return Observable.Return(
Expand All @@ -56,6 +63,10 @@ public IObservable<AutoRegressiveObservations> Process()
});
}

/// <summary>
/// Transforms an observable sequence of <see cref="PyObject"/> into an observable sequence
/// of <see cref="AutoRegressiveObservations"/> objects by accessing internal attributes of the <see cref="PyObject"/>.
/// </summary>
public IObservable<AutoRegressiveObservations> Process(IObservable<PyObject> source)
{
return Observable.Select(source, pyObject =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.ComponentModel;
using Newtonsoft.Json;
using static Bonsai.ML.HiddenMarkovModels.Observations.ObservationsLookup;

namespace Bonsai.ML.HiddenMarkovModels.Observations
{
/// <summary>
/// Represents an bernoulli observations model.
/// </summary>
[JsonObject(MemberSerialization.OptIn)]
public class BernoulliObservations : ObservationsModel
{
Expand All @@ -15,6 +17,7 @@ public class BernoulliObservations : ObservationsModel

/// <inheritdoc/>
[JsonProperty]
[JsonConverter(typeof(ObservationsTypeJsonConverter))]
public override ObservationsType ObservationsType => ObservationsType.Bernoulli;

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@

namespace Bonsai.ML.HiddenMarkovModels.Observations
{
/// <summary>
/// Represents an operator that is used to create and transform an observable sequence
/// of <see cref="BernoulliObservations"/> objects.
/// </summary>
[Combinator]
[Description("")]
[Description("Creates an observable sequence of BernoulliObservations objects.")]
[WorkflowElementCategory(ElementCategory.Source)]
[JsonObject(MemberSerialization.OptIn)]
public class BernoulliObservationsModel
Expand All @@ -20,6 +24,9 @@ public class BernoulliObservationsModel
[Description("The logit P of the observations for each state.")]
public double[,] LogitPs { get; private set; } = null;

/// <summary>
/// Returns an observable sequence of <see cref="BernoulliObservations"/> objects.
/// </summary>
public IObservable<BernoulliObservations> Process()
{
return Observable.Return(
Expand All @@ -28,6 +35,10 @@ public IObservable<BernoulliObservations> Process()
});
}

/// <summary>
/// Transforms an observable sequence of <see cref="PyObject"/> into an observable sequence
/// of <see cref="BernoulliObservations"/> objects by accessing internal attributes of the <see cref="PyObject"/>.
/// </summary>
public IObservable<BernoulliObservations> Process(IObservable<PyObject> source)
{
return Observable.Select(source, pyObject =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System.ComponentModel;
using System.Text;
using Newtonsoft.Json;
using static Bonsai.ML.HiddenMarkovModels.Observations.ObservationsLookup;

namespace Bonsai.ML.HiddenMarkovModels.Observations
{
/// <summary>
/// Represents an exponential observations model.
/// </summary>
[JsonObject(MemberSerialization.OptIn)]
public class ExponentialObservations : ObservationsModel
{

/// <summary>
/// The log lambdas of the observations for each state.
/// </summary>
Expand All @@ -17,6 +17,7 @@ public class ExponentialObservations : ObservationsModel

/// <inheritdoc/>
[JsonProperty]
[JsonConverter(typeof(ObservationsTypeJsonConverter))]
public override ObservationsType ObservationsType => ObservationsType.Exponential;

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@

namespace Bonsai.ML.HiddenMarkovModels.Observations
{
/// <summary>
/// Represents an operator that is used to create and transform an observable sequence
/// of <see cref="ExponentialObservations"/> objects.
/// </summary>
[Combinator]
[Description("")]
[Description("Creates an observable sequence of ExponentialObservations objects.")]
[WorkflowElementCategory(ElementCategory.Source)]
[JsonObject(MemberSerialization.OptIn)]
public class ExponentialObservationsModel
Expand All @@ -21,6 +25,9 @@ public class ExponentialObservationsModel
[Description("The log lambdas of the observations for each state.")]
public double[,] LogLambdas { get; private set; } = null;

/// <summary>
/// Returns an observable sequence of <see cref="ExponentialObservations"/> objects.
/// </summary>
public IObservable<ExponentialObservations> Process()
{
return Observable.Return(
Expand All @@ -29,6 +36,10 @@ public IObservable<ExponentialObservations> Process()
});
}

/// <summary>
/// Transforms an observable sequence of <see cref="PyObject"/> into an observable sequence
/// of <see cref="ExponentialObservations"/> objects by accessing internal attributes of the <see cref="PyObject"/>.
/// </summary>
public IObservable<ExponentialObservations> Process(IObservable<PyObject> source)
{
return Observable.Select(source, pyObject =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System;
using Python.Runtime;
using System.Reactive.Linq;
using System.ComponentModel;
using System.Xml.Serialization;
using Newtonsoft.Json;
using System.Linq;

namespace Bonsai.ML.HiddenMarkovModels.Observations
{
/// <summary>
/// Represents a gaussian observations model.
/// </summary>
[JsonObject(MemberSerialization.OptIn)]
public class GaussianObservations : ObservationsModel
{
Expand All @@ -26,6 +24,7 @@ public class GaussianObservations : ObservationsModel

/// <inheritdoc/>
[JsonProperty]
[JsonConverter(typeof(ObservationsTypeJsonConverter))]
public override ObservationsType ObservationsType => ObservationsType.Gaussian;

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
using System.ComponentModel;
using System.Xml.Serialization;
using Newtonsoft.Json;
using System.Linq;

namespace Bonsai.ML.HiddenMarkovModels.Observations
{
/// <summary>
/// Represents an operator that is used to create and transform an observable sequence
/// of <see cref="GaussianObservations"/> objects.
/// </summary>
[Combinator]
[Description("")]
[Description("Creates an observable sequence of GaussianObservations objects.")]
[WorkflowElementCategory(ElementCategory.Source)]
[JsonObject(MemberSerialization.OptIn)]
public class GaussianObservationsModel
Expand All @@ -29,6 +32,9 @@ public class GaussianObservationsModel
[Description("The standard deviations of the observations for each state.")]
public double[,,] SqrtSigmas { get; private set; } = null;

/// <summary>
/// Returns an observable sequence of <see cref="GaussianObservations"/> objects.
/// </summary>
public IObservable<GaussianObservations> Process()
{
return Observable.Return(
Expand All @@ -37,6 +43,10 @@ public IObservable<GaussianObservations> Process()
});
}

/// <summary>
/// Transforms an observable sequence of <see cref="PyObject"/> into an observable sequence
/// of <see cref="GaussianObservations"/> objects by accessing internal attributes of the <see cref="PyObject"/>.
/// </summary>
public IObservable<GaussianObservations> Process(IObservable<PyObject> source)
{
return Observable.Select(source, pyObject =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using Bonsai;
using System;
using System.ComponentModel;
using System.Reactive.Linq;
using Python.Runtime;
using System.Xml.Serialization;

namespace Bonsai.ML.HiddenMarkovModels.Observations
{
/// <summary>
/// Represents summary statistics and related data of an HMM model with gaussian observations.
/// </summary>
public class GaussianObservationsStatistics
{
/// <summary>
Expand Down
Loading

0 comments on commit b5b894f

Please sign in to comment.