@@ -177,6 +177,7 @@ def test_normalize_raw_percentile_params(self):
177177
178178 def test_normalize_raw_per_channel (self ):
179179 from micro_sam .v2 .normalization import normalize_raw , to_image
180+ from micro_sam .util import _to_image
180181
181182 channel = np .arange (100 , dtype = "float32" ).reshape (10 , 10 )
182183 image = np .stack ([channel , channel * 100 + 42 ], axis = - 1 )
@@ -188,8 +189,9 @@ def test_normalize_raw_per_channel(self):
188189 self .assertEqual (rgb .dtype , np .uint8 )
189190 self .assertTrue (np .array_equal (rgb [..., 0 ], rgb [..., 1 ]))
190191 self .assertFalse (np .any (rgb [..., 2 ]))
192+ self .assertTrue (np .array_equal (rgb , _to_image (image )))
191193
192- def test_normalization_invalidates_old_embeddings (self ):
194+ def test_normalization_invalidates_incompatible_embeddings (self ):
193195 from types import SimpleNamespace
194196
195197 from micro_sam .util import _get_embedding_signature
@@ -200,22 +202,29 @@ def test_normalization_invalidates_old_embeddings(self):
200202 raw = np .arange (100 ).reshape (10 , 10 )
201203 signature = _get_embedding_signature (raw , predictor , tile_shape = None , halo = None )
202204 signature ["normalization" ] = RAW_NORMALIZATION
203- self .assertEqual (signature ["normalization" ], "percentile_2_98 " )
205+ self .assertEqual (signature ["normalization" ], "minmax_per_channel " )
204206
205207 attrs = {"input_size" : [10 , 10 ], ** signature }
206208 embeddings = SimpleNamespace (attrs = attrs )
207209 self .assertFalse (_check_saved_embeddings (raw , predictor , embeddings , "cache.zarr" , None , None ))
208210
209- del attrs ["normalization" ]
211+ attrs ["normalization" ] = "percentile_2_98"
210212 self .assertTrue (_check_saved_embeddings (raw , predictor , embeddings , "cache.zarr" , None , None ))
211213
214+ # Untagged caches predate percentile normalization and used the same min-max policy.
215+ del attrs ["normalization" ]
216+ self .assertFalse (_check_saved_embeddings (raw , predictor , embeddings , "cache.zarr" , None , None ))
217+
212218 class PartialEmbeddings (dict ):
213219 def __init__ (self , normalization = None ):
214220 super ().__init__ (features = object ())
215221 self .attrs = {} if normalization is None else {"normalization" : normalization }
216222
217223 legacy_partial = PartialEmbeddings ()
218- self .assertTrue (_check_saved_embeddings (raw , predictor , legacy_partial , "cache.zarr" , None , None ))
224+ self .assertFalse (_check_saved_embeddings (raw , predictor , legacy_partial , "cache.zarr" , None , None ))
225+
226+ percentile_partial = PartialEmbeddings ("percentile_2_98" )
227+ self .assertTrue (_check_saved_embeddings (raw , predictor , percentile_partial , "cache.zarr" , None , None ))
219228
220229 current_partial = PartialEmbeddings (RAW_NORMALIZATION )
221230 self .assertFalse (_check_saved_embeddings (raw , predictor , current_partial , "cache.zarr" , None , None ))
0 commit comments