Files
Chronos/tests/Chronos.Core.Tests/Analysis/RegionTests.cs
T
Alby96andClaude Opus 5.5 5f27a86406
CI / Build and test (pull_request) Failing after 2m3s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / Docker image (pull_request) Skipped
CI / Windows tests and installer (pull_request) Skipped
CI / YAML check (pull_request) Successful in 4s
refactor!: rename Titano to Chronos and move CI and releases to Gitea
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. 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) and
  pe-check.

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>
2026-10-23 11:34:48 +02:00

202 lines
7.3 KiB
C#

using System.Globalization;
using Chronos.Core.Analysis;
using Chronos.Core.Diagnostics.Synthetic;
using Chronos.Core.Imaging;
using Chronos.Testing;
namespace Chronos.Core.Tests.Analysis;
public sealed class RegionTests(TestContext context)
{
private static SyntheticSequence Landscape(int frames, double tilt, double wave, int seed = 5) => SyntheticSequence.Create(new SyntheticSequenceOptions
{
Width = 128,
Height = 80,
FrameCount = frames,
Seed = seed,
StartEv = -1,
EndEv = 0,
HorizonFraction = 0.45,
HorizonTilt = tilt,
HorizonWave = wave,
SkyFlickerEv = 0.15,
LandFlickerEv = 0.15,
});
private static List<ImageBuffer> RenderAll(SyntheticSequence sequence) => Enumerable.Range(0, sequence.Count).Select(sequence.Render).ToList();
[Test]
public void HorizonIsFoundPerColumnWithHighConfidence()
{
var sequence = Landscape(30, tilt: 0.08, wave: 3);
var frames = RenderAll(sequence);
try
{
var mask = RegionSegmenter.Segment(frames);
Assert.Equal(RegionMethod.Horizon, mask.Method);
Assert.Greater(mask.Confidence, 0.6);
var horizon = Assert.NotNull(mask.Horizon);
var worst = Enumerable.Range(0, mask.Width).Max(x => Math.Abs(horizon[x] - sequence.HorizonRow(x + 0.5)));
var agreement = Agreement(mask, sequence);
context.Log(string.Create(CultureInfo.InvariantCulture, $"confidence {mask.Confidence:F3}, worst column error {worst:F3} px, pixel agreement {agreement:P2}"));
Assert.LessOrEqual(worst, 1.0);
Assert.GreaterOrEqual(agreement, 0.98);
}
finally
{
frames.ForEach(f => f.Dispose());
}
}
[Test]
public void HorizonSurvivesPolarityChangesAcrossFrames()
{
var day = Landscape(10, tilt: 0, wave: 0);
var night = SyntheticSequence.Create(day.Options with { SkyLevel = 0.01, LandLevel = 0.2, Seed = 6 });
var frames = RenderAll(day).Concat(RenderAll(night)).ToList();
try
{
var mask = RegionSegmenter.Segment(frames);
Assert.Equal(RegionMethod.Horizon, mask.Method);
var horizon = Assert.NotNull(mask.Horizon);
Assert.True(horizon.All(h => Math.Abs(h - 0.45 * 80) <= 1), "horizon row");
}
finally
{
frames.ForEach(f => f.Dispose());
}
}
[Test]
public void CliffFallsBackToLuminance()
{
// Sky above row 20 on the left half and above row 60 on the right: no single row step explains it.
var frames = new List<ImageBuffer>();
for (var f = 0; f < 6; f++)
{
var image = new ImageBuffer(96, 80, 3);
for (var y = 0; y < 80; y++)
{
for (var x = 0; x < 96; x++)
{
var sky = y < (x < 48 ? 20 : 60);
var v = sky ? 0.4f * (1 + 0.02f * f) : 0.03f * (1 + ((x * 7 + y * 13) % 5) * 0.1f);
for (var c = 0; c < 3; c++) image[c, x, y] = v;
}
}
frames.Add(image);
}
try
{
var mask = RegionSegmenter.Segment(frames);
Assert.Equal(RegionMethod.Luminance, mask.Method);
Assert.Greater(mask.Confidence, 0.5);
var correct = 0;
for (var y = 0; y < 80; y++)
{
for (var x = 0; x < 96; x++)
{
var sky = y < (x < 48 ? 20 : 60);
if (mask.Sky[y * 96 + x] >= 0.5f == sky) correct++;
}
}
Assert.GreaterOrEqual(correct / (96.0 * 80), 0.97);
}
finally
{
frames.ForEach(f => f.Dispose());
}
}
[Test]
public void FeaturelessFramesHaveNoConfidence()
{
using var flat = new ImageBuffer(64, 48, 3);
flat.Data.Fill(0.2f);
var mask = RegionSegmenter.Segment([flat]);
Assert.Less(mask.Confidence, 0.05);
}
/// <summary>BRIEF §20.4: with independent sky and land flicker, the land residual after region deflicker is at
/// most 25 % of the residual left by one global curve.</summary>
[Test]
public void RegionDeflickerMeetsTheRegressionGate()
{
var sequence = Landscape(150, tilt: 0.05, wave: 2);
var frames = RenderAll(sequence);
RegionMask mask;
double[] sky, land, global;
try
{
mask = RegionSegmenter.Segment(frames);
sky = new double[frames.Count];
land = new double[frames.Count];
global = new double[frames.Count];
for (var i = 0; i < frames.Count; i++)
{
(sky[i], land[i]) = RegionDeflicker.Measure(frames[i], mask);
global[i] = FrameStatistics.MeasureLogLuminance(frames[i]);
}
}
finally
{
frames.ForEach(f => f.Dispose());
}
var options = new DeflickerOptions { Window = 31 };
var globalResult = DeflickerEngine.Compute(global, options);
var regionResult = RegionDeflicker.Compute(sky, land, options);
var trueLand = AnalysisFixtures.TrueLandMask(sequence);
var landAfterGlobal = AnalysisFixtures.MeasureSeries(sequence, (i, image) =>
{
ExposureProcessor.Apply(image, globalResult.CorrectionEv[i]);
return image;
}, trueLand);
var landAfterRegion = AnalysisFixtures.MeasureSeries(sequence, (i, image) =>
{
RegionDeflicker.Apply(image, mask, regionResult.Sky.CorrectionEv[i], regionResult.Land.CorrectionEv[i]);
return image;
}, trueLand);
var globalResidual = AnalysisFixtures.FrameToFrameResidualRms(landAfterGlobal, sequence.SceneEv);
var regionResidual = AnalysisFixtures.FrameToFrameResidualRms(landAfterRegion, sequence.SceneEv);
var ratio = regionResidual / globalResidual;
context.Log(string.Create(CultureInfo.InvariantCulture,
$"mask {mask.Method} confidence {mask.Confidence:F3}; land residual global {globalResidual:F4} EV, region {regionResidual:F4} EV, ratio {ratio:P1}"));
Assert.LessOrEqual(ratio, 0.25);
}
[Test]
public void ResampledMaskKeepsTheHorizon()
{
var sequence = Landscape(6, tilt: 0, wave: 0);
var frames = RenderAll(sequence);
try
{
var mask = RegionSegmenter.Segment(frames);
var big = mask.Resample(256, 160);
Assert.Equal(256 * 160, big.Sky.Length);
Assert.Approximately(2 * mask.Horizon![10], big.Horizon![20], 0.6);
Assert.True(big.Sky[5 * 256 + 100] > 0.99f && big.Sky[150 * 256 + 100] < 0.01f);
Assert.Same(mask, mask.Resample(mask.Width, mask.Height));
}
finally
{
frames.ForEach(f => f.Dispose());
}
}
private static double Agreement(RegionMask mask, SyntheticSequence sequence)
{
var correct = 0;
for (var y = 0; y < mask.Height; y++)
{
for (var x = 0; x < mask.Width; x++)
{
if (mask.Sky[y * mask.Width + x] >= 0.5f == sequence.IsSky(x, y)) correct++;
}
}
return correct / (double)(mask.Width * mask.Height);
}
}