Files
Chronos/tests/Chronos.Core.Tests/Analysis/DeflickerEngineTests.cs
T
Alby96andClaude Opus 5.5 f2b58522b9
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout and the system SQLite. The release cross-compiles the Windows
  programs, checks their icon, version and DPI manifest, packs installers
  and zips, publishes the Gitea release and pushes the server image
  (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows
  runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-23 12:45:18 +02:00

97 lines
4.4 KiB
C#

using System.Globalization;
using Chronos.Core.Analysis;
using Chronos.Core.Diagnostics.Synthetic;
using Chronos.Testing;
namespace Chronos.Core.Tests.Analysis;
public sealed class DeflickerEngineTests(TestContext context)
{
/// <summary>BRIEF §20.4: flicker reduction ≥ 60 % on the synthetic ramp with the ramp preserved within 0.05 EV,
/// measured end to end (measure → target curve → apply gains → re-measure).</summary>
[Test]
public void RampSequenceMeetsTheRegressionGate()
{
var sequence = SyntheticSequence.Create(new SyntheticSequenceOptions
{
FrameCount = 200,
Seed = 3,
StartEv = -2,
EndEv = 1,
FlickerEv = 0.1,
});
var measured = AnalysisFixtures.MeasureSeries(sequence);
var result = DeflickerEngine.Compute(measured, new DeflickerOptions { Window = 21 });
var corrected = AnalysisFixtures.MeasureSeries(sequence, (i, image) =>
{
ExposureProcessor.Apply(image, result.CorrectionEv[i]);
return image;
});
var before = AnalysisFixtures.FrameToFrameResidualRms(measured, sequence.SceneEv);
var after = AnalysisFixtures.FrameToFrameResidualRms(corrected, sequence.SceneEv);
var reduction = 1 - after / before;
var rampError = AnalysisFixtures.MaxDeviation(corrected, sequence.SceneEv);
context.Log(string.Create(CultureInfo.InvariantCulture,
$"flicker before {before:F4} EV, after {after:F4} EV, reduction {reduction:P1}, max ramp deviation {rampError:F4} EV, diagnostics {result.Diagnostics}"));
Assert.GreaterOrEqual(reduction, 0.60);
Assert.LessOrEqual(rampError, 0.05);
Assert.Approximately(after, result.Diagnostics.FlickerAfter, 0.01);
Assert.Approximately(before, result.Diagnostics.FlickerBefore, 0.02);
}
[Test]
public void IntentionalOffsetsAreNotTreatedAsFlicker()
{
var n = 120;
var ramp = Enumerable.Range(0, n).Select(i => -0.01 * i).ToArray();
var offsets = Enumerable.Range(0, n).Select(i => i >= 60 ? 1.0 : 0.0).ToArray();
var measured = ramp.Select((v, i) => v + offsets[i]).ToArray();
var withOffsets = DeflickerEngine.Compute(measured, intentionalOffsets: offsets);
for (var i = 0; i < n; i++) Assert.Approximately(measured[i], withOffsets.Target[i], 1e-9);
// Without them the step is smeared into a ramp over the window.
var naive = DeflickerEngine.Compute(measured);
Assert.Greater(Math.Abs(naive.CorrectionEv[59]), 0.2);
}
[Test]
public void OutliersArePulledOntoTheFitAndStrengthBlendsTheRest()
{
var random = new Random(8);
var n = 150;
var truth = Enumerable.Range(0, n).Select(i => 0.02 * i).ToArray();
var measured = truth.Select(v => v + 0.05 * (2 * random.NextDouble() - 1)).ToArray();
measured[70] += 2.5;
var half = DeflickerEngine.Compute(measured, new DeflickerOptions { Strength = 0.5 });
Assert.Less(half.RobustWeights[70], 0.01);
Assert.Approximately(truth[70], half.Target[70], 0.05);
Assert.Equal(1, half.Diagnostics.OutlierCount);
// An inlier keeps about half of its own deviation from the fit.
var inlier = Enumerable.Range(0, n).First(i => i is > 10 and < 140 && Math.Abs(measured[i] - half.Fitted[i]) > 0.02 && half.RobustWeights[i] > 0.8);
var kept = (half.Target[inlier] - half.Fitted[inlier]) / (measured[inlier] - half.Fitted[inlier]);
Assert.InRange(kept, 0.5 * half.RobustWeights[inlier] * half.SurvivingFraction[inlier] - 1e-9, 0.5 + 1e-9);
Assert.Greater(kept, 0.35);
}
[Test]
public void UnusableFramesGetTargetsButNoCorrection()
{
var measured = Enumerable.Range(0, 40).Select(i => 0.1 * i).ToArray();
measured[12] = double.NaN;
var result = DeflickerEngine.Compute(measured, new DeflickerOptions { Window = 9 });
Assert.Approximately(1.2, result.Target[12], 1e-9);
Assert.Equal(0.0, result.CorrectionEv[12]);
Assert.Throws<ArgumentOutOfRangeException>(() => DeflickerEngine.Compute(measured, new DeflickerOptions { Strength = 2 }));
}
[Test]
public void OffsetsFromExposureFollowMetadata()
{
double?[] ev = [null, 12, 12, 11.5, null, 13];
var offsets = DeflickerEngine.OffsetsFromExposure(ev);
Assert.SequenceEqual([0.0, 0, 0, 0.5, 0.5, -1], offsets);
}
}