71 lines
5.6 KiB
C#
71 lines
5.6 KiB
C#
using AstroFrame.Engine;
|
|
using System.Buffers.Binary;
|
|
using System.Globalization;
|
|
using System.Text;
|
|
|
|
/// <summary>Image-level invariants: persistent nebulosity, smooth sky trends, prolonged moving cloud.</summary>
|
|
static class ImageRegression
|
|
{
|
|
public static async Task Run(Action<bool,string> 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<Frame> 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<height;y++)for(int x=0;x<width;x++)
|
|
{
|
|
double u=(double)x/width,v=(double)y/height;
|
|
double staticNebula=80*Math.Exp(-((u-.42)*(u-.42)+(v-.58)*(v-.58))*35);
|
|
double cloudGlow=cloudy?450*Math.Exp(-Math.Pow((v-(.22+(n%9)*.045))/.10,2)):n==18?35*Math.Exp(-Math.Pow((v-.72)/.10,2)):0;
|
|
pixels[y*width+x]=(float)(1000+n*2+75*u+n*v+staticNebula+cloudGlow+(random.NextDouble()-.5)*60);
|
|
}
|
|
foreach(var s in stars)
|
|
{
|
|
double localCloud=cloudy?Math.Exp(-Math.Pow((s.Y/height-(.22+(n%9)*.045))/.10,2)):0;
|
|
double flux=s.Flux*(1-n*.0015)*(1-.75*localCloud);
|
|
for(int dy=-4;dy<=4;dy++)for(int dx=-4;dx<=4;dx++)
|
|
{int x=(int)s.X+dx,y=(int)s.Y+dy;pixels[y*width+x]+=(float)(flux*Math.Exp(-((x-s.X)*(x-s.X)+(y-s.Y)*(y-s.Y))/(2*.85*.85)));}
|
|
}
|
|
string path=Path.Combine(directory,$"image{n:000}.fit");WriteFits(path,pixels,width,height,n);
|
|
var f=Fits.Inspect(path);new StellarDetector().Measure(f,Fits.Read(path),CancellationToken.None);frames.Add(f);
|
|
}
|
|
await SpatialReference.RunAsync(frames,null,CancellationToken.None);Classification.Run(frames);
|
|
var clear=frames.Where((_,n)=>(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<height;y++){for(int x=0;x<width;x++)BinaryPrimitives.WriteInt32BigEndian(row.AsSpan(x*4,4),BitConverter.SingleToInt32Bits(pixels[y*width+x]));stream.Write(row);}
|
|
}
|
|
}
|