Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joleroi committed Mar 24, 2016
1 parent d6c9ebc commit 24b34bc
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 15 deletions.
17 changes: 9 additions & 8 deletions regions/io/ds9_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ def region_list_to_objects(region_list):
return output_list


def objects_to_ds9_string(obj_list, coordsys='fk5'):
def objects_to_ds9_string(obj_list, coordsys='fk5', fmt='.4f', radunit='arcsec'):
"""Take a list of regions and generate ds9 region strings"""

ids = {"<class 'regions.shapes.circle.CircleSkyRegion'>": 'circle',
"<class 'regions.shapes.ellipse.EllipseSkyRegion'>": 'ellipse',
"<class 'regions.shapes.polygon.PolygonSkyRegion'>": 'polygone'}

ds9_strings = {'circle': 'circle({x:.4f},{y:.4f},{r:.4f})\n',
'ellipse': 'ellipse({x:.4f},{y:.4f},{r1:.4f},{r2:.4f},{ang:.4f})\n',
ds9_strings = {'circle': 'circle({x:'+fmt+'},{y:'+fmt+'},{r:'+fmt+'}")\n',
'ellipse': 'ellipse({x:'+fmt+'},{y:'+fmt+'},{r1:'+fmt+'}",{r2:'+fmt+'}",{ang:'+fmt+'})\n',
'polygone': 'polygon({c})\n'}

output = '# Region file format: DS9 astropy/regions\n'
Expand All @@ -134,17 +134,18 @@ def objects_to_ds9_string(obj_list, coordsys='fk5'):
# TODO: Why is circle.center a list of SkyCoords?
x = reg.center.ra.to('deg').value[0]
y = reg.center.dec.to('deg').value[0]
r = reg.radius.to('deg').value
r = reg.radius.to(radunit).value
elif t == 'ellipse':
x = reg.center.ra.to('deg').value[0]
y = reg.center.dec.to('deg').value[0]
r1 = reg.major.to('deg').value
r2 = reg.minor.to('deg').value
r1 = reg.major.to(radunit).value
r2 = reg.minor.to(radunit).value
ang = reg.angle.to('deg').value
elif t == 'polygone':
v = reg.vertices
coords = [(x.to('deg').value, y.to('deg').value) for x in v.ra for y in v.dec]
temp = ["{:.4f}".format(x) for _ in coords for x in _]
coords = [(x.to('deg').value, y.to('deg').value) for x,y in zip(v.ra, v.dec)]
val = "{:"+fmt+"}"
temp = [val.format(x) for _ in coords for x in _]
c = ", ".join(temp)

output += ds9_strings[t].format(**locals())
Expand Down
3 changes: 3 additions & 0 deletions regions/io/setup_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@

def get_package_data():
parser_test = [os.path.join('data', 'ds9.fk5.reg'),
os.path.join('data', 'ds9.fk5.strip.reg'),
os.path.join('data', 'ds9.fk5.hms.reg'),
os.path.join('data', 'ds9.fk5.hms.strip.reg'),
os.path.join('data', 'fk5_reference.reg')]
return {'regions.io.tests': parser_test}
39 changes: 39 additions & 0 deletions regions/io/tests/data/ds9.fk5.hms.reg
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Region file format: DS9 version 4.1
global color=green dashlist=8 3 width=1 font="helvetica 10 normal roman" select=1 highlite=1 dash=0 fixed=0 edit=1 move=1 delete=1 include=1 source=1
fk5
circle(13:29:56.743,+47:12:30.42,3.964") # color=pink width=3 font="times 10 normal roman" text={Circle} tag={foo} tag={foo bar} This is a Comment
-ellipse(13:29:55.792,+47:12:16.94,7.928",3.964",2.39831) # color=#0ff font="helvetica 10 normal italic" text={Ellipse} background
-box(13:29:54.530,+47:12:04.57,15.856",7.928",2.39831) # color=yellow font="helvetica 10 bold roman" text={Box}
polygon(13:29:53.394,+47:11:56.88,13:29:52.357,+47:11:57.32,13:29:52.314,+47:11:46.75,13:29:53.351,+47:11:46.31) # font="courier 10 normal roman" text={Polygon} edit=0 rotate=0
-line(13:29:52.267,+47:11:39.58,13:29:50.737,+47:11:38.97) # line=1 1 color=cyan text={Line}
# vector(13:29:50.632,+47:11:30.18,7.928",2.39831) vector=1 color=red text={Vector}
# text(13:29:50.030,+47:11:20.75) color=magenta font="helvetica 14 bold roman" text={Region}
# ruler(13:29:55.474,+47:11:39.76,13:29:53.782,+47:11:39.78) ruler=physical physical color=white font="helvetica 12 normal roman" text={Ruler}
annulus(13:29:58.217,+47:12:14.07,1.982",3.964",5.946") # color=magenta font="helvetica 10 bold roman" text={Annulus}
ellipse(13:29:57.053,+47:12:02.09,2.973",1.4865",5.946",2.973",2.39831) # color=red width=3 font="helvetica 10 bold roman" text={Ellipse Annulus}
box(13:29:55.820,+47:11:50.27,7.928",3.964",11.892",5.946",2.39831) # font="helvetica 10 bold roman" text={Box Annulus}
point(13:29:59.541,+47:12:01.44) # point=circle text={Circle Point}
point(13:29:58.293,+47:11:49.21) # point=box color=red width=3 text={Box Point}
point(13:29:57.107,+47:11:38.64) # point=diamond text={Diamond Point}
point(13:29:59.971,+47:11:52.28) # point=cross color=blue text={Cross Point}
point(13:29:58.870,+47:11:42.23) # point=x text={X Point}
point(13:29:57.807,+47:11:31.47) # point=arrow color=magenta text={Arrow Point}
point(13:29:59.526,+47:11:34.38) # point=boxcircle text={BoxCircle Point}
# projection(13:29:54.118,+47:11:24.35,13:29:51.918,+47:11:21.50,3.964") text={Projection}
panda(13:29:55.690,+47:11:25.36,317.398,587.398,3,0",5.946",2) # text={Panda}
panda(13:29:56.859,+47:11:11.81,8.98021,47.3983,1,0",2.973",1) # panda=(8.98021 47.3983 137.398 227.398)(0" 2.973" 5.946") text={Panda 2}
panda(13:29:56.859,+47:11:11.81,8.98021,47.3983,1,2.973",5.946",1) # panda=ignore
panda(13:29:56.859,+47:11:11.81,47.3983,137.398,1,0",2.973",1) # panda=ignore
panda(13:29:56.859,+47:11:11.81,47.3983,137.398,1,2.973",5.946",1) # panda=ignore
panda(13:29:56.859,+47:11:11.81,137.398,227.398,1,0",2.973",1) # panda=ignore
panda(13:29:56.859,+47:11:11.81,137.398,227.398,1,2.973",5.946",1) # panda=ignore
# compass(13:29:52.095,+47:11:11.04,7.928") compass=physical {N} {E} 1 1 text={Compass}
epanda(13:29:54.622,+47:11:13.19,317.398,587.398,3,2.973",1.4865",5.946",2.973",1,2.39831) # text={Epanda}
epanda(13:29:55.750,+47:10:59.80,2.39831,47.3983,1,2.973",1.4865",5.946",2.973",1,2.39831) # epanda=(2.39831 47.3983 137.398 227.398)(2.973" 1.4865" 5.946" 2.973")(2.39831) text={Epanda 2}
epanda(13:29:55.750,+47:10:59.80,47.3983,137.398,1,2.973",1.4865",5.946",2.973",1,2.39831) # epanda=ignore
epanda(13:29:55.750,+47:10:59.80,137.398,227.398,1,2.973",1.4865",5.946",2.973",1,2.39831) # epanda=ignore
bpanda(13:29:53.377,+47:11:01.52,317.398,587.398,3,7.928",3.964",11.892",5.946",1,2.39831) # text={Bpanda}
bpanda(13:29:54.593,+47:10:49.22,2.39831,47.3983,1,7.928",3.964",11.892",5.946",1,2.39831) # bpanda=(2.39831 47.3983 137.398 227.398)(7.928" 3.964" 11.892" 5.946")(2.39831) text={Bpanda 2}
bpanda(13:29:54.593,+47:10:49.22,47.3983,137.398,1,7.928",3.964",11.892",5.946",1,2.39831) # bpanda=ignore
bpanda(13:29:54.593,+47:10:49.22,137.398,227.398,1,7.928",3.964",11.892",5.946",1,2.39831) # bpanda=ignore
# segment(13:29:49.122,+47:11:27.59,13:29:49.448,+47:11:19.88,13:29:49.076,+47:11:16.39) text={Segment}
1 change: 1 addition & 0 deletions regions/io/tests/data/ds9.fk5.hms.strip.reg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fk5;circle(13:29:56.743,+47:12:30.42,3.964");-ellipse(13:29:55.792,+47:12:16.94,7.928",3.964",2.39831);-box(13:29:54.530,+47:12:04.57,15.856",7.928",2.39831);polygon(13:29:53.394,+47:11:56.88,13:29:52.357,+47:11:57.32,13:29:52.314,+47:11:46.75,13:29:53.351,+47:11:46.31);-line(13:29:52.267,+47:11:39.58,13:29:50.737,+47:11:38.97);annulus(13:29:58.217,+47:12:14.07,1.982",3.964",5.946");ellipse(13:29:57.053,+47:12:02.09,2.973",1.4865",5.946",2.973",2.39831);box(13:29:55.820,+47:11:50.27,7.928",3.964",11.892",5.946",2.39831);point(13:29:59.541,+47:12:01.44);point(13:29:58.293,+47:11:49.21);point(13:29:57.107,+47:11:38.64);point(13:29:59.971,+47:11:52.28);point(13:29:58.870,+47:11:42.23);point(13:29:57.807,+47:11:31.47);point(13:29:59.526,+47:11:34.38);panda(13:29:55.690,+47:11:25.36,317.398,587.398,3,0",5.946",2);panda(13:29:56.859,+47:11:11.81,8.98021,47.3983,1,0",2.973",1);panda(13:29:56.859,+47:11:11.81,8.98021,47.3983,1,2.973",5.946",1);panda(13:29:56.859,+47:11:11.81,47.3983,137.398,1,0",2.973",1);panda(13:29:56.859,+47:11:11.81,47.3983,137.398,1,2.973",5.946",1);panda(13:29:56.859,+47:11:11.81,137.398,227.398,1,0",2.973",1);panda(13:29:56.859,+47:11:11.81,137.398,227.398,1,2.973",5.946",1);epanda(13:29:54.622,+47:11:13.19,317.398,587.398,3,2.973",1.4865",5.946",2.973",1,2.39831);epanda(13:29:55.750,+47:10:59.80,2.39831,47.3983,1,2.973",1.4865",5.946",2.973",1,2.39831);epanda(13:29:55.750,+47:10:59.80,47.3983,137.398,1,2.973",1.4865",5.946",2.973",1,2.39831);epanda(13:29:55.750,+47:10:59.80,137.398,227.398,1,2.973",1.4865",5.946",2.973",1,2.39831);bpanda(13:29:53.377,+47:11:01.52,317.398,587.398,3,7.928",3.964",11.892",5.946",1,2.39831);bpanda(13:29:54.593,+47:10:49.22,2.39831,47.3983,1,7.928",3.964",11.892",5.946",1,2.39831);bpanda(13:29:54.593,+47:10:49.22,47.3983,137.398,1,7.928",3.964",11.892",5.946",1,2.39831);bpanda(13:29:54.593,+47:10:49.22,137.398,227.398,1,7.928",3.964",11.892",5.946",1,2.39831);
1 change: 1 addition & 0 deletions regions/io/tests/data/ds9.fk5.strip.reg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fk5;circle(202.48643,47.208449,3.9640007");-ellipse(202.48247,47.204704,7.9280014",3.9640007",2.3983109);-box(202.47721,47.201269,15.856003",7.9280014",2.3983109);polygon(202.47248,47.199134,202.46815,47.199257,202.46797,47.19632,202.4723,47.196197);-line(202.46778,47.194329,202.4614,47.194158);annulus(202.49257,47.20391,1.9820003",3.9640007",5.946001");ellipse(202.48772,47.20058,2.9730005",1.4865003",5.946001",2.9730005",2.3983109);box(202.48258,47.197298,7.9280014",3.9640007",11.892002",5.946001",2.3983109);point(202.49809,47.200401);point(202.49289,47.197004);point(202.48795,47.194067);point(202.49988,47.197856);point(202.49529,47.195065);point(202.49086,47.192074);point(202.49802,47.192883);panda(202.48204,47.190378,317.39831,587.39831,3,0",5.946001",2);panda(202.48691,47.186615,8.9802109,47.398311,1,0",2.9730005",1);panda(202.48691,47.186615,8.9802109,47.398311,1,2.9730005",5.946001",1);panda(202.48691,47.186615,47.398311,137.39831,1,0",2.9730005",1);panda(202.48691,47.186615,47.398311,137.39831,1,2.9730005",5.946001",1);panda(202.48691,47.186615,137.39831,227.39831,1,0",2.9730005",1);panda(202.48691,47.186615,137.39831,227.39831,1,2.9730005",5.946001",1);epanda(202.47759,47.186997,317.39831,587.39831,3,2.9730005",1.4865003",5.946001",2.9730005",1,2.3983109);epanda(202.48229,47.183279,2.3983109,47.398311,1,2.9730005",1.4865003",5.946001",2.9730005",1,2.3983109);epanda(202.48229,47.183279,47.398311,137.39831,1,2.9730005",1.4865003",5.946001",2.9730005",1,2.3983109);epanda(202.48229,47.183279,137.39831,227.39831,1,2.9730005",1.4865003",5.946001",2.9730005",1,2.3983109);bpanda(202.47241,47.183755,317.39831,587.39831,3,7.9280014",3.9640007",11.892002",5.946001",1,2.3983109);bpanda(202.47747,47.180338,2.3983109,47.398311,1,7.9280014",3.9640007",11.892002",5.946001",1,2.3983109);bpanda(202.47747,47.180338,47.398311,137.39831,1,7.9280014",3.9640007",11.892002",5.946001",1,2.3983109);bpanda(202.47747,47.180338,137.39831,227.39831,1,7.9280014",3.9640007",11.892002",5.946001",1,2.3983109);
8 changes: 4 additions & 4 deletions regions/io/tests/data/fk5_reference.reg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Region file format: DS9 astropy/regions
fk5
circle(202.4864,47.2084,0.0011)
ellipse(202.4825,47.2047,0.0011,0.0022,2.3983)
polygon(202.4725, 47.1991, 202.4725, 47.1993, 202.4725, 47.1963, 202.4725, 47.1962, 202.4682, 47.1991, 202.4682, 47.1993, 202.4682, 47.1963, 202.4682, 47.1962, 202.4680, 47.1991, 202.4680, 47.1993, 202.4680, 47.1963, 202.4680, 47.1962, 202.4723, 47.1991, 202.4723, 47.1993, 202.4723, 47.1963, 202.4723, 47.1962)
ellipse(202.4877,47.2006,0.0004,0.0008,0.0017)
circle(202.49,47.21,3.96")
ellipse(202.48,47.20,3.96",7.93",2.40)
polygon(202.47, 47.20, 202.47, 47.20, 202.47, 47.20, 202.47, 47.20)
ellipse(202.49,47.20,1.49",2.97",0.00)
18 changes: 15 additions & 3 deletions regions/io/tests/test_ds9_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@

from ..ds9_language import ds9_parser, region_list_to_objects, objects_to_ds9_string
from astropy.utils.data import get_pkg_data_filename
from astropy.tests.helper import pytest


def test_fk5(tmpdir):
filename = get_pkg_data_filename('data/ds9.fk5.reg')
@pytest.mark.parametrize('filename',
['data/ds9.fk5.reg',
'data/ds9.fk5.hms.reg',
'data/ds9.fk5.hms.strip.reg',
'data/ds9.fk5.strip.reg'])

def test_fk5(filename):
filename = get_pkg_data_filename(filename)
temp = ds9_parser(filename)
regs = region_list_to_objects(temp)

actual = objects_to_ds9_string(regs, coordsys='fk5')
actual = objects_to_ds9_string(regs, coordsys='fk5', fmt='.2f')

# Use this to produce reference file for now
# print(actual)
# 1/0

reference_file = get_pkg_data_filename('data/fk5_reference.reg')
with open(reference_file, 'r') as fh:
desired = fh.read()
Expand Down

0 comments on commit 24b34bc

Please sign in to comment.