def test_encoder_header(self): roi_obj_write = ROIRect(20, 30, 40, 50, name='test_name_header') temp_path = 'temp_file.roi' roi_obj_write.header = { 'C_POSITION': 1, 'Z_POSITION': 2, 'T_POSITION': 3, 'POSITION': 4 } with ROIEncoder(temp_path, roi_obj_write) as roi: roi.write() with ROIDecoder(temp_path) as roi: roi_obj_read = roi.get_roi() self.assertIsInstance(roi_obj_read, ROIRect) self.assertEqual(roi_obj_read.top, 20) self.assertEqual(roi_obj_read.left, 30) self.assertEqual(roi_obj_read.bottom, 40) self.assertEqual(roi_obj_read.right, 50) self.assertEqual(roi_obj_read.name, 'test_name_header') #todo name not properly displayed if loaded into imagej (issue) self.assertEqual(roi_obj_read.header['C_POSITION'], 1) self.assertEqual(roi_obj_read.header['Z_POSITION'], 2) self.assertEqual(roi_obj_read.header['T_POSITION'], 3) self.assertEqual(roi_obj_read.header['POSITION'], 4)
def test_encoder_polyline(self): x_coords = np.array([1, 2, 3, 10, 15]) y_coords = np.array([4, 5, 6, 8, 20]) roi_obj = ROIPolyline(20, 40, 20 + x_coords.max(), 40 + y_coords.max(), x_coords, y_coords, name='polyline_test') temp_path = 'temp_file.roi' with ROIEncoder(temp_path, roi_obj) as roi: roi.write() with ROIDecoder(temp_path) as roi: roi_obj_read = roi.get_roi() self.assertIsInstance(roi_obj_read, ROIPolyline) self.assertEqual(roi_obj_read.top, 20) self.assertEqual(roi_obj_read.left, 40) self.assertTrue(np.allclose(roi_obj_read.x_coords, x_coords)) self.assertTrue(np.allclose(roi_obj_read.y_coords, y_coords)) self.assertEqual(roi_obj_read.name, 'polyline_test') os.remove(temp_path)
def test_encoder_polygon(self): y_coords = np.array([45, 30, 0, 12, 20]) x_coords = np.array([0, 13, 25, 60, 5]) left = 22 top = 30 right = left + np.max(x_coords) - np.min(x_coords) + 1 bottom = top + np.max(y_coords) - np.min(y_coords) + 1 roi_obj = ROIPolygon(top, left, bottom, right, x_coords, y_coords, name='polygon_test') temp_path = tempfile.mkstemp()[1] with ROIEncoder(temp_path, roi_obj) as roi: roi.write() with ROIDecoder(temp_path) as roi: roi_obj = roi.get_roi() self.assertIsInstance(roi_obj, ROIPolygon) self.assertEqual(roi_obj.top, top) self.assertEqual(roi_obj.left, left) self.assertEqual(roi_obj.bottom, bottom) self.assertEqual(roi_obj.right, right) self.assertTrue(np.allclose(x_coords, roi_obj.x_coords)) self.assertTrue(np.allclose(y_coords, roi_obj.y_coords)) self.assertEqual('polygon_test', roi_obj.name)
def test_encoder_header_imagej(self): roi_obj_write = ROIRect(20, 30, 40, 50, name='test_name_header') temp_path = 'temp_zpos_imagej.roi' roi_obj_write.header = { 'Z_POSITION': 2, } with ROIEncoder(temp_path, roi_obj_write) as roi: roi.write()
def test_encoder_oval(self): roi_obj = ROIOval(100, 25, 150, 50, name='oval_test') temp_path = tempfile.mkstemp()[1] with ROIEncoder(temp_path, roi_obj) as roi: roi.write() with ROIDecoder(temp_path) as roi: roi_obj = roi.get_roi() self.assertIsInstance(roi_obj, ROIOval) self.assertEqual(roi_obj.top, 100) self.assertEqual(roi_obj.left, 25) self.assertEqual(roi_obj.bottom, 150) self.assertEqual(roi_obj.right, 50) self.assertEqual(roi_obj.area, np.pi * (50 - 25) * (150 - 100) / 4) self.assertEqual(roi_obj.name, 'oval_test')
def test_encoder_rect(self): roi_obj = ROIRect(20, 30, 40, 50, name='rect_test') temp_path = tempfile.mkstemp()[1] with ROIEncoder(temp_path, roi_obj) as roi: roi.write() with ROIDecoder(temp_path) as roi: roi_out = roi.get_roi() self.assertIsInstance(roi_out, ROIRect) self.assertEqual(roi_out.top, 20) self.assertEqual(roi_out.left, 30) self.assertEqual(roi_out.bottom, 40) self.assertEqual(roi_out.right, 50) self.assertEqual(roi_out.area, 400) self.assertEqual(roi_out.name, 'rect_test')
def test_encoder_rect(self): roi_obj = ROIRect(20, 30, 40, 50) temp_path = tempfile.mkstemp()[1] with ROIEncoder(temp_path, roi_obj) as roi: roi.write() with ROIDecoder(temp_path) as roi: roi_obj = roi.get_roi() self.assertIsInstance(roi_obj, ROIRect) self.assertEqual(roi_obj.top, 20) self.assertEqual(roi_obj.left, 30) self.assertEqual(roi_obj.bottom, 40) self.assertEqual(roi_obj.right, 50) self.assertEqual(roi_obj.area, 400) os.remove(temp_path)
def test_encoder_polygon(self): x_coords = [1, 2, 3] y_coords = [4, 5, 6] roi_obj = ROIPolygon(20, 40, x_coords, y_coords, name='polygon_test') temp_path = 'temp_file.roi' with ROIEncoder(temp_path, roi_obj) as roi: roi.write() with ROIDecoder(temp_path) as roi: roi_obj_read = roi.get_roi() self.assertIsInstance(roi_obj_read, ROIPolygon) self.assertEqual(roi_obj_read.top, 20) self.assertEqual(roi_obj_read.left, 40) self.assertArrayEqual(roi_obj_read.x_coords, x_coords) self.assertArrayEqual(roi_obj_read.y_coords, y_coords) self.assertEqual(roi_obj_read.name, 'polygon_test')
def test_encoder_oval(self): top, left, bottom, right = 230, 240, 255, 280 roi_obj = ROIOval(top, left, bottom, right, name='Oval_test') temp_path = 'temp_file.roi' with ROIEncoder(temp_path, roi_obj) as roi: roi.write() with ROIDecoder(temp_path) as roi: roi_obj_read = roi.get_roi() self.assertIsInstance(roi_obj_read, ROIOval) self.assertEqual(roi_obj_read.top, top) self.assertEqual(roi_obj_read.left, left) self.assertArrayEqual(roi_obj_read.bottom, bottom) self.assertArrayEqual(roi_obj_read.right, right) self.assertEqual(roi_obj_read.name, 'Oval_test') self.assertEqual(roi_obj_read.area, roi_obj.area)
def test_encoder_line(self): x1, y1, x2, y2 = 23., 256., 200, 305. roi_obj = ROILine(x1, y1, x2, y2, name='Line_test') temp_path = tempfile.mkstemp()[1] with ROIEncoder(temp_path, roi_obj) as roi: roi.write() with ROIDecoder(temp_path) as roi: roi_obj_read = roi.get_roi() self.assertIsInstance(roi_obj_read, ROILine) self.assertEqual(roi_obj_read.x1, x1) self.assertEqual(roi_obj_read.x2, x2) self.assertEqual(roi_obj_read.y1, y1) self.assertEqual(roi_obj_read.y2, y2) self.assertEqual(roi_obj_read.name, 'Line_test') self.assertEqual(roi_obj_read.area, 0)
def test_encoder_rect(self): roi_obj_write = ROIRect(20, 30, 40, 50, name='test_name') temp_path = 'temp_file.roi' with ROIEncoder(temp_path, roi_obj_write) as roi: roi.write() with ROIDecoder(temp_path) as roi: roi_obj_read = roi.get_roi() self.assertIsInstance(roi_obj_read, ROIRect) self.assertEqual(roi_obj_read.top, 20) self.assertEqual(roi_obj_read.left, 30) self.assertEqual(roi_obj_read.bottom, 40) self.assertEqual(roi_obj_read.right, 50) self.assertEqual(roi_obj_read.area, 400) self.assertEqual(roi_obj_read.name, 'test_name') os.remove(temp_path)
def test_encoder_line(self): x1, y1, x2, y2 = 23.5, 256.8, 200, 305.756 roi_obj = ROILine(x1, y1, x2, y2, name='Line_test') temp_path = 'temp_file.roi' with ROIEncoder(temp_path, roi_obj) as roi: roi.write() with ROIDecoder(temp_path) as roi: roi_obj_read = roi.get_roi() self.assertIsInstance(roi_obj_read, ROILine) self.assertEqual(roi_obj_read.x1, x1) self.assertEqual(roi_obj_read.x2, x2) self.assertAlmostEqual(roi_obj_read.y1, y1, places=1) self.assertAlmostEqual(roi_obj_read.y2, y2, places=1) #todo warn about inaccuracy? self.assertEqual(roi_obj_read.name, 'Line_test') self.assertEqual(roi_obj_read.area, 0)
def test_encoder_freeline(self): x_coords = [1, 2, 3, 10, 15] y_coords = [4, 5, 6, 8, 20] roi_obj = ROIFreeLine(20, 40, x_coords, y_coords, name='freeline_test') temp_path = 'temp_file.roi' with ROIEncoder(temp_path, roi_obj) as roi: roi.write() with ROIDecoder(temp_path) as roi: roi_obj_read = roi.get_roi() self.assertIsInstance(roi_obj_read, ROIFreeLine) self.assertEqual(roi_obj_read.top, 20) self.assertEqual(roi_obj_read.left, 40) self.assertArrayEqual(roi_obj_read.x_coords, x_coords) self.assertArrayEqual(roi_obj_read.y_coords, y_coords) self.assertEqual(roi_obj_read.name, 'freeline_test') os.remove(temp_path)
def test_encoder_freehand(self): x_coords = np.array([1, 2, 3, 10, 15]) y_coords = np.array([4, 5, 6, 8, 20]) roi_obj = ROIFreehand(20, 40, 20 + x_coords.max(), 40 + y_coords.max(), x_coords, y_coords, name='freehand_test') temp_path = tempfile.mkstemp()[1] with ROIEncoder(temp_path, roi_obj) as roi: roi.write() with ROIDecoder(temp_path) as roi: roi_obj_read = roi.get_roi() self.assertIsInstance(roi_obj_read, ROIFreehand) self.assertEqual(roi_obj_read.top, 20) self.assertEqual(roi_obj_read.left, 40) self.assertTrue(np.allclose(roi_obj_read.x_coords, x_coords)) self.assertTrue(np.allclose(roi_obj_read.y_coords, y_coords)) self.assertEqual(roi_obj_read.name, 'freehand_test')