AstroFrame/tests/AstroFrame.Validation/QualityRegression.cs
Elijah c0ef081e4d
All checks were successful
Release AstroFrame / packages (push) Successful in 56s
Release 1.1.7: reject moderate thin cloud and confirmed star elongation
Frames the sky-structure detector already flags become REJECT when
strong cloud texture is backed by two independent moderate dimming
measurements. Elongation confirmed on the same matched reference stars
(lower 3-sigma bound of the change >= 0.10) rejects as a roundness
outlier, following Siril's Seestar -filter-round practice. NGC 1432:
Review 36 -> 1; every Keep unchanged across the validation corpus.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 19:48:07 -07:00

89 lines
8.3 KiB
C#

using AstroFrame.Engine;
using System.Text.Json;
using System.Text.Json.Serialization;
static class QualityRegression
{
public static void Run(Action<bool,string> check)
{
var stars=Enumerable.Range(0,100).Select(i=>new Star(.08+(i%10)*.09,.08+(i/10)*.09,3000-i*10,4,.1)).ToList();
Frame Make(int n)=>new(){Id=n.ToString(),Path="quality"+n,Target="Quality",Exposure=30,Width=2160,Height=3840,
Timestamp=new DateTime(2026,9,20,8,0,0,DateTimeKind.Utc).AddSeconds(n*30),Measured=true,
StarCount=100,Stars=stars.ToList(),Fwhm=4,Ellipticity=.1,Background=1000,Noise=20,
CloudMask=new byte[SkyModel.Count],ReferenceConfidence=1,
AdditionalMeasurements=new(){{"Clear reference member",1},{"Forced flux samples",100},{"Matched shape reference frames",20}}};
var frames=Enumerable.Range(0,40).Select(Make).ToList();
var opaque=Make(40);opaque.Stars=stars.Take(25).ToList();opaque.StarCount=25;
var broad=Make(41);var gradient=Make(42);var uncertain=Make(43);var cohort=Make(44);var trailing=Make(45);
var subtleShape=Make(46);var insufficient=Make(47);var noisyFlux=Make(48);
var thinFlagged=Make(49);var thinUndimmed=Make(50);var thinClean=Make(51);var unconfirmedShape=Make(52);var moderateCloud=Make(53);
frames.AddRange([opaque,broad,gradient,uncertain,cohort,trailing,subtleShape,insufficient,noisyFlux,thinFlagged,thinUndimmed,thinClean,unconfirmedShape,moderateCloud]);
foreach(var f in frames)
{
Matching.Measure(frames[0],f);f.Transparency=1;f.RegionFlux=Enumerable.Repeat(1.0,12).ToArray();f.RegionMatches=Enumerable.Repeat(8,12).ToArray();
}
void Cloud(Frame f,double significance,double coverage,double ratio)
{
f.AdditionalMeasurements["Clear reference member"]=0;f.CloudSignificance=significance;f.CloudCoverage=coverage;
f.AdditionalMeasurements["Cloud connected coverage"]=coverage*.6;
f.AdditionalMeasurements["Cloud positive coverage"]=coverage*.6;
f.AdditionalMeasurements["Cloud positive connected coverage"]=coverage*.4;
f.AdditionalMeasurements["Cloud aperture samples"]=80;f.AdditionalMeasurements["Cloud aperture regions"]=6;
f.AdditionalMeasurements["Cloud aperture flux ratio"]=ratio;f.AdditionalMeasurements["Cloud aperture flux deviation"]=(1-ratio)/.03;
}
Cloud(opaque,100,.9,.05);opaque.Transparency=.05;
Cloud(broad,15,.5,.85);broad.Transparency=.90;
Cloud(gradient,15,.5,1);
Cloud(insufficient,15,.5,.85);insufficient.AdditionalMeasurements["Cloud aperture samples"]=19;
Cloud(noisyFlux,15,.5,.85);noisyFlux.AdditionalMeasurements["Cloud aperture flux deviation"]=2.9;
Cloud(uncertain,100,.9,.05);uncertain.Transparency=.05;uncertain.MatchFraction=.1;
uncertain.AdditionalMeasurements["Alignment verified"]=0;
cohort.Ellipticity=.3;cohort.AdditionalMeasurements["Matched shape samples"]=100;
cohort.AdditionalMeasurements["Matched shape change"]=.04;cohort.AdditionalMeasurements["Matched shape uncertainty"]=.01;
trailing.Ellipticity=.3;trailing.AdditionalMeasurements["Matched shape samples"]=100;
trailing.AdditionalMeasurements["Matched shape change"]=.2;trailing.AdditionalMeasurements["Matched shape uncertainty"]=.01;
subtleShape.Ellipticity=.3;subtleShape.AdditionalMeasurements["Matched shape samples"]=100;
subtleShape.AdditionalMeasurements["Matched shape change"]=.10;subtleShape.AdditionalMeasurements["Matched shape uncertainty"]=.005;
void Texture(Frame f,double ratio,double deviation,bool flagged)
{
f.AdditionalMeasurements["Clear reference member"]=0;
f.AdditionalMeasurements["Cloud texture significance"]=20;f.AdditionalMeasurements["Cloud texture coverage"]=.15;
f.AdditionalMeasurements["Cloud texture connected coverage"]=.10;f.AdditionalMeasurements["Cloud texture differential samples"]=60;
f.AdditionalMeasurements["Cloud texture differential flux ratio"]=ratio;f.AdditionalMeasurements["Cloud texture differential flux deviation"]=deviation;
if(!flagged)return;
// Weak, uncorroborated planar structure routes the frame to REVIEW.
Cloud(f,6,.05,1);
}
Texture(thinFlagged,.96,4,true);Texture(thinUndimmed,1,.3,true);Texture(thinClean,.96,4,false);
unconfirmedShape.Ellipticity=.3;unconfirmedShape.AdditionalMeasurements["Matched shape samples"]=30;
unconfirmedShape.AdditionalMeasurements["Matched shape change"]=.2;unconfirmedShape.AdditionalMeasurements["Matched shape uncertainty"]=.01;
// Flagged sky structure, strong texture, and two moderate, independent dimming measurements.
Texture(moderateCloud,.98,2,false);Cloud(moderateCloud,8,.10,.91);moderateCloud.AdditionalMeasurements["Cloud aperture flux deviation"]=2.9;
moderateCloud.AdditionalMeasurements["Cloud texture significance"]=30;moderateCloud.Transparency=1;
Classification.Run(frames);
check(thinFlagged.Automatic==Verdict.Reject&&thinFlagged.Evidence.Any(e=>e.Metric=="Cloud texture"),"flagged frame with thin cloud texture that dims stars beneath it rejects");
check(thinUndimmed.Automatic==Verdict.Review,"thin texture without differential stellar dimming stays REVIEW");
check(thinClean.Automatic==Verdict.Keep,"thin cloud texture alone never rejects an otherwise clean KEEP");
check(moderateCloud.Automatic==Verdict.Reject&&moderateCloud.Evidence.Any(e=>e.Metric=="Cloud texture"),"flagged sky structure with strong texture and two moderate dimming measurements rejects");
double[] curved=Enumerable.Range(0,SkyModel.Count).Select(i=>{double x=(i%SkyModel.Columns+.5)/SkyModel.Columns-.5,y=(i/SkyModel.Columns+.5)/SkyModel.Rows-.5;return 1000+40*x-30*y+200*x*x-150*x*y+120*y*y;}).ToArray();
check(SkyModel.Detrend(curved,true).All(v=>Math.Abs(v)<.01)&&SkyModel.Detrend(curved).Max(Math.Abs)>5,"quadratic detrending removes smooth sky curvature that a plane leaves behind");
double[] spike=new double[SkyModel.Count];spike[10*SkyModel.Columns+10]=9;var smoothed=SkyModel.Smooth(spike);
check(Math.Abs(smoothed[10*SkyModel.Columns+10]-1)<1e-9&&Math.Abs(smoothed[11*SkyModel.Columns+11]-1)<1e-9&&smoothed[12*SkyModel.Columns+12]==0,"texture smoothing averages each cell with its 3x3 neighbors");
check(opaque.Automatic==Verdict.Reject,"severe cloud with accurate geometry rejects despite missing reference stars");
check(broad.Automatic==Verdict.Reject,"broad coherent sky structure with local aperture attenuation rejects");
check(gradient.Automatic==Verdict.Review,"uncorroborated sky structure stays REVIEW rather than rejecting a possible gradient");
check(uncertain.Automatic==Verdict.Review,"severe apparent cloud with unverified geometry stays REVIEW");
check(cohort.Automatic==Verdict.Keep,"matched-star evidence can resolve an elongation flag caused by a changed detection cohort");
check(trailing.Automatic==Verdict.Reject&&trailing.Cause=="Likely tracking or focus failure","elongation confirmed by the same matched reference stars rejects as a roundness outlier");
check(unconfirmedShape.Automatic==Verdict.Review,"elongation without enough matched reference stars stays REVIEW");
check(subtleShape.Automatic==Verdict.Review,"matched elongation between the dismissal and rejection limits stays REVIEW");
check(insufficient.Automatic==Verdict.Review&&noisyFlux.Automatic==Verdict.Review,"too few or noisy affected apertures cannot authorize cloud rejection");
check(frames.Take(40).All(f=>f.Automatic==Verdict.Keep),"quality policy changes preserve unchanged clear comparisons");
var options=new JsonSerializerOptions{PropertyNameCaseInsensitive=true,Converters={new JsonStringEnumConverter()}};
var fixture=JsonSerializer.Deserialize<Fixture>(File.ReadAllText(Path.Combine(AppContext.BaseDirectory,"Fixtures","quality-real-frames.json")),options)!;
var real=fixture.Frames.ToList();Classification.Run(real);
foreach(var label in fixture.Expected)
check(real.Single(f=>f.Name==label.Key).Automatic==label.Value,$"real measured regression {label.Key}: {label.Value}");
}
sealed record Fixture(string Source,Frame[] Frames,Dictionary<string,Verdict> Expected);
}