Skip to content

Commit 0a41eca

Browse files
committed
Log calibration to file
1 parent ee60896 commit 0a41eca

File tree

4 files changed

+178
-92
lines changed

4 files changed

+178
-92
lines changed

src/Extensions.csproj

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
3-
<PropertyGroup>
4-
<TargetFramework>net472</TargetFramework>
5-
</PropertyGroup>
6-
7-
<ItemGroup>
8-
<PackageReference Include="Bonsai.Core" Version="2.8.5" />
9-
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
10-
<PackageReference Include="MathNet.Numerics" Version="4.15.0" />
11-
</ItemGroup>
12-
2+
<PropertyGroup>
3+
<TargetFramework>net472</TargetFramework>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<PackageReference Include="Bonsai.Core" Version="2.8.5" />
7+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
8+
<PackageReference Include="MathNet.Numerics" Version="4.15.0" />
9+
</ItemGroup>
1310
</Project>

src/Extensions/Logging.bonsai

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
xmlns:p5="clr-namespace:AindBehaviorWaterTuner.TaskLogic;assembly=Extensions"
1212
xmlns:p6="clr-namespace:AindBehaviorWaterTuner.Rig;assembly=Extensions"
1313
xmlns:p7="clr-namespace:AllenNeuralDynamics.AindBehaviorServices.DataTypes;assembly=AllenNeuralDynamics.AindBehaviorServices"
14+
xmlns:p8="clr-namespace:AindBehaviorWaterTuner.DataTypes;assembly=Extensions"
15+
xmlns:io="clr-namespace:Bonsai.IO;assembly=Bonsai.System"
1416
xmlns="https://bonsai-rx.org/2018/workflow">
1517
<Workflow>
1618
<Nodes>
@@ -623,11 +625,48 @@
623625
</Edges>
624626
</Workflow>
625627
</Expression>
628+
<Expression xsi:type="SubscribeSubject">
629+
<Name>CalibrationModel</Name>
630+
</Expression>
631+
<Expression xsi:type="Combinator">
632+
<Combinator xsi:type="p8:SerializeToJson" />
633+
</Expression>
634+
<Expression xsi:type="Combinator">
635+
<Combinator xsi:type="io:WriteLine" />
636+
</Expression>
637+
<Expression xsi:type="ExternalizedMapping">
638+
<Property Name="Name" DisplayName="SubjectName" />
639+
</Expression>
640+
<Expression xsi:type="SubscribeSubject">
641+
<Name>LoggingRootPath</Name>
642+
</Expression>
643+
<Expression xsi:type="Format">
644+
<Format>{0}/water_calibration.json</Format>
645+
<Selector />
646+
</Expression>
647+
<Expression xsi:type="PropertyMapping">
648+
<PropertyMappings>
649+
<Property Name="Path" />
650+
</PropertyMappings>
651+
</Expression>
652+
<Expression xsi:type="Combinator">
653+
<Combinator xsi:type="io:WriteAllText">
654+
<io:Overwrite>false</io:Overwrite>
655+
<io:Append>false</io:Append>
656+
</Combinator>
657+
</Expression>
626658
</Nodes>
627659
<Edges>
628660
<Edge From="0" To="1" Label="Source1" />
629661
<Edge From="1" To="2" Label="Source1" />
630662
<Edge From="2" To="3" Label="Source1" />
663+
<Edge From="8" To="9" Label="Source1" />
664+
<Edge From="9" To="10" Label="Source1" />
665+
<Edge From="10" To="15" Label="Source1" />
666+
<Edge From="11" To="12" Label="Source1" />
667+
<Edge From="12" To="13" Label="Source1" />
668+
<Edge From="13" To="14" Label="Source1" />
669+
<Edge From="14" To="15" Label="Source2" />
631670
</Edges>
632671
</Workflow>
633672
</Expression>

src/Extensions/WaterCalibrationModel.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,39 @@
44
using System.Linq;
55
using System.Reactive.Linq;
66
using MathNet.Numerics;
7+
using AindBehaviorWaterTuner.DataTypes;
8+
using System.Collections.Generic;
79

810
[Combinator]
9-
[Description("")]
11+
[Description("Computes a water calibration model from a sequence of measurements.")]
1012
[WorkflowElementCategory(ElementCategory.Transform)]
1113
public class WaterCalibrationModel
1214
{
13-
public IObservable<LinearRegressionFit> Process(IObservable<Tuple<double, ScaleMessage, ScaleMessage>[]> source)
15+
public IObservable<WaterValveCalibration> Process(IObservable<IEnumerable<Measurement>> source)
1416
{
1517
return source.Select(value => {
16-
var delays = value.Select(x => x.Item1).ToArray();
17-
var weights = value.Select(x => x.Item3.DataFrame.Weight - x.Item2.DataFrame.Weight).ToArray();
18-
Tuple<double, double> p = Fit.Line(delays, weights);
19-
return new LinearRegressionFit(){
20-
Intercept = p.Item1,
18+
19+
var input = new WaterValveCalibrationInput(){
20+
Measurements = value.ToList()
21+
};
22+
23+
24+
var delays = value.Select(x => x.ValveOpenTime).ToArray();
25+
var weights = value.Select(x => x.WaterWeight.Average()).ToArray();
26+
var p = Fit.Line(delays, weights);
27+
28+
var output = new WaterValveCalibrationOutput(){
29+
Offset = p.Item1,
2130
Slope = p.Item2,
22-
R2 = GoodnessOfFit.RSquared(delays.Select(x => p.Item1+p.Item2*x), weights)
31+
R2 = GoodnessOfFit.RSquared(delays.Select(x => p.Item1+p.Item2*x), weights),
32+
IntervalAverage = value.ToDictionary(k => k.ValveOpenTime.ToString("0.0000"), v => v.WaterWeight.Average()),
33+
ValidDomain = delays.ToList()
34+
};
35+
36+
return new WaterValveCalibration(){
37+
Input = input,
38+
Output = output,
39+
Date = DateTimeOffset.Now,
2340
};
2441
});
2542
}

0 commit comments

Comments
 (0)