Spaces:
Paused
Paused
Download tests/test_learned_evaluation.py from modventures/iris: direct link, hf CLI and curl.
- Browser
- Download file 3.38 kB
-
https://huggingface.co/spaces/modventures/iris/resolve/main/tests/test_learned_evaluation.py
- Command line
-
hf download hf://spaces/modventures/iris/tests/test_learned_evaluation.py
-
curl -L -o test_learned_evaluation.py https://huggingface.co/spaces/modventures/iris/resolve/main/tests/test_learned_evaluation.py
3.38 kB
| import cv2 | |
| import numpy as np | |
| from fastapi.testclient import TestClient | |
| from backend.app import app,processing_slots | |
| from backend.experimental.evaluation import evaluate_correspondences | |
| from backend.review import review_pair | |
| def test_cross_validation_smooth_known_motion(): | |
| y,x=np.mgrid[80:520:15,80:520:15] | |
| a=np.stack([x.ravel(),y.ravel()],-1).astype(np.float32) | |
| b=a+np.stack([3*np.sin(a[:,1]/180),2*np.sin(a[:,0]/160)],-1) | |
| result=evaluate_correspondences(a,b,(600,600)) | |
| assert result['scored_count']==len(a) | |
| assert not result['failed_folds'] | |
| assert result['within_px_percent']['1']>95 | |
| assert result['p95_px']<1 | |
| def test_insufficient_correspondences_not_assigned_percentage(): | |
| result=evaluate_correspondences(np.zeros((29,2),np.float32),np.zeros((29,2),np.float32),(600,600)) | |
| assert result['within_px_percent'] is None | |
| assert result['p95_px'] is None | |
| def test_unscored_folds_count_against_percentage(): | |
| a=np.tile(np.array([[200,200]],np.float32),(80,1)) | |
| result=evaluate_correspondences(a,a,(600,600)) | |
| assert result['p95_px'] is None | |
| assert result['within_px_percent']['5']==0 | |
| def test_learned_requires_crop_confirmation(): | |
| data=cv2.imencode('.png',np.zeros((400,400,3),np.uint8))[1].tobytes() | |
| response=TestClient(app).post('/api/register',data={'engine':'learned'},files={'reference':('a.png',data),'moving':('b.png',data)}) | |
| assert response.status_code==422 | |
| assert 'Confirm' in response.json()['detail'] | |
| def test_learned_invalid_shape_preserves_originals_without_loading_model(): | |
| a=np.zeros((500,600,3),np.uint8) | |
| result=review_pair(a,a,engine='learned') | |
| assert result['metrics']['status']=='unaligned' | |
| assert np.array_equal(result['reference'],a) | |
| def test_busy_service_returns_retry_not_a_false_review(): | |
| data=cv2.imencode('.png',np.zeros((100,100,3),np.uint8))[1].tobytes() | |
| processing_slots.acquire() | |
| try: | |
| response=TestClient(app).post('/api/register',files={'reference':('a.png',data),'moving':('b.png',data)}) | |
| assert response.status_code==503 | |
| assert response.headers['retry-after']=='10' | |
| finally:processing_slots.release() | |
| def test_learned_output_is_one_sample_of_original_and_preserves_untrusted_pixels(monkeypatch): | |
| from backend.learned_registration import register_learned | |
| from backend.experimental.mesh import dense_mesh | |
| image=cv2.resize(cv2.imread('fixtures/synthetic-iris.png'),(600,600)) | |
| y,x=np.mgrid[90:520:15,90:520:15] | |
| points=np.stack([x.ravel(),y.ravel()],-1).astype(np.float32) | |
| r=np.linalg.norm(points-300,axis=1);points=points[(r>100)&(r<235)] | |
| monkeypatch.setattr('backend.learned_registration.get_matcher',lambda:lambda a,b:(points,points+np.float32([8,-6]))) | |
| monkeypatch.setattr('backend.learned_registration.validate_patches',lambda a,b,p,q:(p,q)) | |
| moving=cv2.warpAffine(image,np.float32([[1,0,8],[0,1,-6]]),(600,600)) | |
| result=register_learned(image,moving,exposure=False) | |
| m=result['metrics'];mapping,_,_=dense_mesh(np.array(m['mesh_coefficients'],np.float32),np.array(m['transform']),(600,600)) | |
| expected=cv2.remap(moving,mapping[...,0],mapping[...,1],cv2.INTER_LINEAR) | |
| assert np.array_equal(result['aligned'],expected) | |
| untouched=result['contribution']==0 | |
| assert np.array_equal(result['composite'][untouched],image[untouched]) | |
| assert m['exposure_gain']==1 | |