using AstroFrame.Engine; using System.Buffers.Binary; using System.Globalization; using System.Text; /// Image-level invariants: persistent nebulosity, smooth sky trends, prolonged moving cloud. static class ImageRegression { public static async Task Run(Action check) { string directory=Path.Combine(Path.GetTempPath(),"AstroFrame-images-"+Guid.NewGuid());Directory.CreateDirectory(directory); try { const int width=256,height=448;Random random=new(452);var stars=Enumerable.Range(0,130).Select(_=>(X:14+random.NextDouble()*(width-28),Y:14+random.NextDouble()*(height-28),Flux:1800+random.NextDouble()*2200)).ToArray(); List frames=[]; for(int n=0;n<81;n++) { float[] pixels=new float[width*height];bool cloudy=n>=22&&n<60; for(int y=0;y(n<22||n>=60)&&n!=18).ToArray();var cloud=frames.Where((_,n)=>n>=22&&n<60).ToArray(); check(clear.All(f=>f.Automatic==Verdict.Keep),"persistent nebulosity and gradual sky/flux trends remain KEEP in image pipeline"); check(cloud.All(f=>f.Automatic==Verdict.Reject),"prolonged moving cloud does not become the local baseline"); check(cloud.All(f=>f.CloudMask.Any(v=>v>0)&&f.Evidence.Any(e=>e.Metric=="Cloud structure")),"cloud masks and spatial explanations survive the full image pipeline"); bool Localized(Frame f) { int n=frames.IndexOf(f);double center=.22+(n%9)*.045,aspect=(double)f.Height/f.Width;var a=f.Alignment; var cells=Enumerable.Range(0,SkyModel.Count).Select(i=> { double x=(i%SkyModel.Columns+.5)/SkyModel.Columns,y=(i/SkyModel.Columns+.5)/SkyModel.Rows; return(i,Y:(a[1]*x+a[0]*y*aspect+a[3])/aspect); }).Where(c=>c.Y>.05&&c.Y<.95).ToArray(); var core=cells.Where(c=>Math.Abs(c.Y-center)<.05).ToArray();var clearSky=cells.Where(c=>Math.Abs(c.Y-center)>.24).ToArray(); return f.ExcessSkyMask.Length==SkyModel.Count&&core.Length>0&&core.Count(c=>f.ExcessSkyMask[c.i]>=128)>=core.Length*.85&&clearSky.Length>0&&clearSky.Count(c=>f.ExcessSkyMask[c.i]>0)<=clearSky.Length*.1; } check(cloud.All(Localized),"image pipeline highlights physical cloud cores while leaving distant clear sky unshaded"); check(frames[18].Automatic==Verdict.Review,"modest transient sky structure without stellar loss routes to REVIEW, preserving conservative rejection"); var reference=frames.First(f=>f.Id==cloud[0].ReferenceId);var image=Fits.Read(cloud[0].Path); var vanished=reference.Stars[0] with{Flux=1};reference.Stars=[vanished];var forced=Matching.ForcedFlux(reference,cloud[0],image); check(forced.Length==1&&double.IsFinite(forced[0]),"forced apertures measure coordinates independently of detection in that exposure"); // With only unstable cloudy exposures, no apparent normal population may certify KEEP. await SpatialReference.RunAsync(cloud.ToList(),null,CancellationToken.None);Classification.Run(cloud.ToList()); check(cloud.All(f=>f.Automatic==Verdict.Review),"a wholly unstable cloudy session routes to REVIEW when no stable reference exists"); } finally{Directory.Delete(directory,true);} } internal static void WriteFits(string path,float[] pixels,int width,int height,int n) { string Card(string key,string value)=>(key.PadRight(8)+"= "+value).PadRight(80); string date=new DateTime(2026,9,20,8,0,0,DateTimeKind.Utc).AddSeconds(n*30).ToString("yyyy-MM-ddTHH:mm:ss",CultureInfo.InvariantCulture); string header=Card("SIMPLE","T")+Card("BITPIX","-32")+Card("NAXIS","2")+Card("NAXIS1",width.ToString())+Card("NAXIS2",height.ToString())+Card("EXPTIME","30")+Card("OBJECT","'Synthetic field'")+Card("DATE-OBS",$"'{date}'")+"END".PadRight(80); using var stream=File.Create(path);stream.Write(Encoding.ASCII.GetBytes(header.PadRight((header.Length+2879)/2880*2880))); byte[] row=new byte[width*4];for(int y=0;y