AstroFrame/tests/AstroFrame.Validation/QualityRegression.cs
Elijah ef7805055c
All checks were successful
Release AstroFrame / packages (push) Successful in 1m17s
Release 1.1.6: reject corroborated thin cloud instead of reviewing it
Add a quadratic-detrended, smoothed sky texture measurement scaled by
clear reference scatter, and corroborate it with same-exposure
differential forced photometry (stars inside vs. outside the texture,
normalized by nearby clear frames). Frames already routed to REVIEW
become REJECT only when both agree; otherwise-clean KEEPs are never
affected. NGC 1432: Review 48 -> 36, Reject 100 -> 112, Keep unchanged.

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

82 lines
7.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);
frames.AddRange([opaque,broad,gradient,uncertain,cohort,trailing,subtleShape,insufficient,noisyFlux,thinFlagged,thinUndimmed,thinClean]);
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"]=.12;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;
f.Ellipticity=.3;f.AdditionalMeasurements["Matched shape samples"]=100;
f.AdditionalMeasurements["Matched shape change"]=.2;f.AdditionalMeasurements["Matched shape uncertainty"]=.01;
}
Texture(thinFlagged,.96,4,true);Texture(thinUndimmed,1,.3,true);Texture(thinClean,.96,4,false);
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");
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.Review,"matched-star elongation remains REVIEW when other measurements are normal");
check(subtleShape.Automatic==Verdict.Review,"measurable sub-threshold matched elongation is not promoted to KEEP");
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);
}