|
| 1 | +using Distributions |
| 2 | + |
| 3 | +@testset "ImgNormalData" begin |
| 4 | + # Load test data to get a working model |
| 5 | + _, vis, amp, lcamp, cphase = load_data() |
| 6 | + |
| 7 | + # Create a simple image grid |
| 8 | + g = imagepixels(μas2rad(150.0), μas2rad(150.0), 32, 32) |
| 9 | + skym = SkyModel(test_model, test_prior(), g) |
| 10 | + |
| 11 | + # Create a base posterior to sample from |
| 12 | + base_post = VLBIPosterior(skym, vis) |
| 13 | + |
| 14 | + # Test with a simple reduction function (total flux) |
| 15 | + @testset "Total Flux Measurement" begin |
| 16 | + # Create reduction function for total flux |
| 17 | + reduction = flux |
| 18 | + |
| 19 | + # Get a sample image to create mock measurement |
| 20 | + x0 = prior_sample(base_post) |
| 21 | + m0 = skymodel(base_post, x0) |
| 22 | + img0 = intensitymap(m0, g) |
| 23 | + |
| 24 | + # Create mock measurement with noise |
| 25 | + true_flux = flux(img0) |
| 26 | + measurement = [true_flux] |
| 27 | + noise = [0.1] |
| 28 | + |
| 29 | + # Create ImgNormalData object |
| 30 | + imgdata = ImgNormalData(reduction, measurement, noise) |
| 31 | + |
| 32 | + # Test that fields are correctly stored |
| 33 | + @test imgdata.reduction === reduction |
| 34 | + @test imgdata.measurement == measurement |
| 35 | + @test imgdata.noise == noise |
| 36 | + |
| 37 | + # Create likelihood from the image data |
| 38 | + ℓ = Comrade.makelikelihood(imgdata) |
| 39 | + |
| 40 | + # Test that we can evaluate the likelihood |
| 41 | + @test ℓ isa Comrade.ConditionedLikelihood |
| 42 | + |
| 43 | + # Test likelihood evaluation with the image |
| 44 | + lp = logdensityof(ℓ, img0) |
| 45 | + @test lp isa Real |
| 46 | + @test isfinite(lp) |
| 47 | + |
| 48 | + # Test that likelihood is higher when image flux matches measurement |
| 49 | + # Create image with exact flux |
| 50 | + img_exact = img0 .* (measurement[1] / flux(img0)) |
| 51 | + lp_exact = logdensityof(ℓ, img_exact) |
| 52 | + |
| 53 | + # The exact match should have higher likelihood |
| 54 | + @test lp_exact >= lp |
| 55 | + end |
| 56 | + |
| 57 | + @testset "Centroid Measurement" begin |
| 58 | + # Create reduction function for centroid |
| 59 | + reduction = img -> begin |
| 60 | + cx, cy = centroid(img) |
| 61 | + return [cx, cy] |
| 62 | + end |
| 63 | + |
| 64 | + # Get a sample image |
| 65 | + x0 = prior_sample(base_post) |
| 66 | + m0 = skymodel(base_post, x0) |
| 67 | + img0 = intensitymap(m0, g) |
| 68 | + |
| 69 | + # Create mock measurement |
| 70 | + cx0, cy0 = centroid(img0) |
| 71 | + measurement = [cx0, cy0] |
| 72 | + noise = [μas2rad(5.0), μas2rad(5.0)] |
| 73 | + |
| 74 | + # Create ImgNormalData object |
| 75 | + imgdata = ImgNormalData(reduction, measurement, noise) |
| 76 | + |
| 77 | + # Create likelihood |
| 78 | + ℓ = Comrade.makelikelihood(imgdata) |
| 79 | + |
| 80 | + # Test likelihood evaluation |
| 81 | + lp = logdensityof(ℓ, img0) |
| 82 | + @test lp isa Real |
| 83 | + @test isfinite(lp) |
| 84 | + |
| 85 | + # Test with a different image |
| 86 | + x1 = prior_sample(base_post) |
| 87 | + m1 = skymodel(base_post, x1) |
| 88 | + img1 = intensitymap(m1, g) |
| 89 | + lp1 = logdensityof(ℓ, img1) |
| 90 | + @test lp1 isa Real |
| 91 | + @test isfinite(lp1) |
| 92 | + end |
| 93 | + |
| 94 | + @testset "Multiple Measurements" begin |
| 95 | + # Create reduction function that returns multiple quantities |
| 96 | + reduction = img -> begin |
| 97 | + f = flux(img) |
| 98 | + cx, cy = centroid(img) |
| 99 | + return [f, cx, cy] |
| 100 | + end |
| 101 | + |
| 102 | + # Get a sample image |
| 103 | + x0 = prior_sample(base_post) |
| 104 | + m0 = skymodel(base_post, x0) |
| 105 | + img0 = intensitymap(m0, g) |
| 106 | + |
| 107 | + # Create mock measurements |
| 108 | + f0 = flux(img0) |
| 109 | + cx0, cy0 = centroid(img0) |
| 110 | + measurement = [f0, cx0, cy0] |
| 111 | + noise = [0.1, μas2rad(5.0), μas2rad(5.0)] |
| 112 | + |
| 113 | + # Create ImgNormalData object |
| 114 | + imgdata = ImgNormalData(reduction, measurement, noise) |
| 115 | + |
| 116 | + # Create likelihood |
| 117 | + ℓ = Comrade.makelikelihood(imgdata) |
| 118 | + |
| 119 | + # Test likelihood evaluation |
| 120 | + lp = logdensityof(ℓ, img0) |
| 121 | + @test lp isa Real |
| 122 | + @test isfinite(lp) |
| 123 | + |
| 124 | + # Test that the likelihood is composed correctly |
| 125 | + # The reduction should return a vector |
| 126 | + red_result = reduction(img0) |
| 127 | + @test length(red_result) == 3 |
| 128 | + @test red_result[1] ≈ f0 |
| 129 | + @test red_result[2] ≈ cx0 |
| 130 | + @test red_result[3] ≈ cy0 |
| 131 | + end |
| 132 | + |
| 133 | + @testset "Combining ImgNormalData with VLBI data" begin |
| 134 | + # Create image measurement data |
| 135 | + reduction = flux |
| 136 | + measurement = [1.0] |
| 137 | + noise = [0.1] |
| 138 | + imgdata = ImgNormalData(reduction, measurement, noise) |
| 139 | + |
| 140 | + # Create posterior with both VLBI and image data using keyword argument |
| 141 | + post = VLBIPosterior(skym, vis; imgdata = (imgdata,)) |
| 142 | + |
| 143 | + # Test that we can evaluate the posterior |
| 144 | + x_prior = prior_sample(post) |
| 145 | + lp_post = logdensityof(post, x_prior) |
| 146 | + @test lp_post isa Real |
| 147 | + @test isfinite(lp_post) |
| 148 | + |
| 149 | + # Test dataproducts |
| 150 | + dp = dataproducts(post) |
| 151 | + @test dp isa Tuple |
| 152 | + @test vis in dp |
| 153 | + end |
| 154 | +end |
0 commit comments