From c0906ebf013fec8d30df98ab6bd658f2159de05a Mon Sep 17 00:00:00 2001 From: Taro Kiritani Date: Sat, 6 May 2017 14:57:45 +0200 Subject: [PATCH 1/3] Returns position --- ijroi/ijroi.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ijroi/ijroi.py b/ijroi/ijroi.py index e079275..efcb634 100644 --- a/ijroi/ijroi.py +++ b/ijroi/ijroi.py @@ -3,6 +3,7 @@ # License: MIT import numpy as np +from collections import namedtuple def read_roi(fileobj): @@ -119,8 +120,11 @@ def getfloat(): if options & SUB_PIXEL_RESOLUTION == 0: points[:, 1] += left points[:, 0] += top - - return points + + Roi = namedtuple('Roi', 'points position') + roi = Roi(points=points, position=position) + + return roi def read_roi_zip(fname): From eb5dddd0152e5214afcf47fd0ba6f48da236df82 Mon Sep 17 00:00:00 2001 From: Taro Kiritani Date: Wed, 28 Jun 2017 17:17:26 +0200 Subject: [PATCH 2/3] fix test --- ijroi/tests/test_ijroi.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/ijroi/tests/test_ijroi.py b/ijroi/tests/test_ijroi.py index 852043f..5dc4466 100644 --- a/ijroi/tests/test_ijroi.py +++ b/ijroi/tests/test_ijroi.py @@ -29,46 +29,64 @@ def test_rectangle(): def test_freehand_circle(): fixture = get_fixture("freehand_circle.roi") with fixture.open("rb") as f: - circle = ijroi.read_roi(f) + circle_roi = ijroi.read_roi(f) + circle = circle_roi.points + position = circle_roi.position assert len(circle) == 100 assert abs(circle[:, 1].mean()-10) < 0.01 assert abs(circle[:, 0].mean()-15) < 0.01 + assert position == 0 def test_integer_freehand(): fixture = get_fixture("freehand_integer.roi") with fixture.open("rb") as f: - freehand = ijroi.read_roi(f) + freehand_roi = ijroi.read_roi(f) + freehand = freehand_roi.points + position = freehand_roi.position assert len(freehand) == 3 assert all(freehand[2, :] == [1, 10]) assert freehand.dtype == np.int16 + assert position == 0 def test_polygon(): fixture = get_fixture("polygon_circle.roi") with fixture.open("rb") as f: - circle = ijroi.read_roi(f) + circle_roi = ijroi.read_roi(f) + circle = circle_roi.points + position = circle_roi.position assert len(circle) == 100 assert abs(circle[:, 1].mean()-10) < 0.01 assert abs(circle[:, 0].mean()-15) < 0.01 + assert position == 0 fixture = get_fixture("polygon_integer.roi") with fixture.open("rb") as f: - polyint = ijroi.read_roi(f) + polyint_roi = ijroi.read_roi(f) + polyint = polyint_roi.points + position = polyint_roi.position assert len(polyint) == 3 assert all(polyint[2, :] == [1, 10]) assert polyint.dtype == np.int16 + assert position == 0 def test_point(): fixture = get_fixture("int_point.roi") with fixture.open("rb") as f: - point = ijroi.read_roi(f) + point_roi = ijroi.read_roi(f) + point = point_roi.points + position = point_roi.position assert point.ndim == 2 assert point[0,0] == 256 assert point[0,1] == 128 + assert position == 0 def test_float_point(): fixture = get_fixture("float_point.roi") with fixture.open("rb") as f: - point = ijroi.read_roi(f) + point_roi = ijroi.read_roi(f) + point = point_roi.points + position = point_roi.position assert point.ndim == 2 assert abs(point[0,0] - 567.8) < 0.01 assert abs(point[0,1] - 123.4) < 0.01 + assert position == 0 \ No newline at end of file From 4e5346b57685b874218c3795ac3275d00eadb634 Mon Sep 17 00:00:00 2001 From: Taro Kiritani Date: Wed, 28 Jun 2017 17:28:03 +0200 Subject: [PATCH 3/3] Changes the documentation, streamlined test_ijroi.py --- ijroi/ijroi.py | 5 +++-- ijroi/tests/test_ijroi.py | 24 ++++++------------------ 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/ijroi/ijroi.py b/ijroi/ijroi.py index efcb634..74cc7d2 100644 --- a/ijroi/ijroi.py +++ b/ijroi/ijroi.py @@ -8,10 +8,11 @@ def read_roi(fileobj): ''' - points = read_roi(fileobj) + points, position = read_roi(fileobj) Read ImageJ's ROI format. Points are returned in a nx2 array. Each row - is in [row, column] -- that is, (y,x) -- order. + is in [row, column] -- that is, (y,x) -- order. Position is the slice + number. ''' # This is based on: # http://rsbweb.nih.gov/ij/developer/source/ij/io/RoiDecoder.java.html diff --git a/ijroi/tests/test_ijroi.py b/ijroi/tests/test_ijroi.py index 5dc4466..1fc585e 100644 --- a/ijroi/tests/test_ijroi.py +++ b/ijroi/tests/test_ijroi.py @@ -29,9 +29,7 @@ def test_rectangle(): def test_freehand_circle(): fixture = get_fixture("freehand_circle.roi") with fixture.open("rb") as f: - circle_roi = ijroi.read_roi(f) - circle = circle_roi.points - position = circle_roi.position + circle, position = ijroi.read_roi(f) assert len(circle) == 100 assert abs(circle[:, 1].mean()-10) < 0.01 assert abs(circle[:, 0].mean()-15) < 0.01 @@ -40,9 +38,7 @@ def test_freehand_circle(): def test_integer_freehand(): fixture = get_fixture("freehand_integer.roi") with fixture.open("rb") as f: - freehand_roi = ijroi.read_roi(f) - freehand = freehand_roi.points - position = freehand_roi.position + freehand, position = ijroi.read_roi(f) assert len(freehand) == 3 assert all(freehand[2, :] == [1, 10]) assert freehand.dtype == np.int16 @@ -51,9 +47,7 @@ def test_integer_freehand(): def test_polygon(): fixture = get_fixture("polygon_circle.roi") with fixture.open("rb") as f: - circle_roi = ijroi.read_roi(f) - circle = circle_roi.points - position = circle_roi.position + circle, position = ijroi.read_roi(f) assert len(circle) == 100 assert abs(circle[:, 1].mean()-10) < 0.01 assert abs(circle[:, 0].mean()-15) < 0.01 @@ -61,9 +55,7 @@ def test_polygon(): fixture = get_fixture("polygon_integer.roi") with fixture.open("rb") as f: - polyint_roi = ijroi.read_roi(f) - polyint = polyint_roi.points - position = polyint_roi.position + polyint, position = ijroi.read_roi(f) assert len(polyint) == 3 assert all(polyint[2, :] == [1, 10]) assert polyint.dtype == np.int16 @@ -72,9 +64,7 @@ def test_polygon(): def test_point(): fixture = get_fixture("int_point.roi") with fixture.open("rb") as f: - point_roi = ijroi.read_roi(f) - point = point_roi.points - position = point_roi.position + point, position = ijroi.read_roi(f) assert point.ndim == 2 assert point[0,0] == 256 assert point[0,1] == 128 @@ -83,9 +73,7 @@ def test_point(): def test_float_point(): fixture = get_fixture("float_point.roi") with fixture.open("rb") as f: - point_roi = ijroi.read_roi(f) - point = point_roi.points - position = point_roi.position + point, position = ijroi.read_roi(f) assert point.ndim == 2 assert abs(point[0,0] - 567.8) < 0.01 assert abs(point[0,1] - 123.4) < 0.01