From e3afa1973ab96db0cf2b37858ba8c59b324a3a02 Mon Sep 17 00:00:00 2001 From: Johannes King Date: Thu, 18 May 2017 08:19:24 +0200 Subject: [PATCH] fix #134 --- regions/io/ds9/read.py | 39 +++++++++++------------ regions/io/ds9/tests/test_ds9_language.py | 10 ++---- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/regions/io/ds9/read.py b/regions/io/ds9/read.py index 9e2fdcc9..fa2a33ab 100644 --- a/regions/io/ds9/read.py +++ b/regions/io/ds9/read.py @@ -228,8 +228,9 @@ def parse_line(self, line): "the known region types.".format(region_type)) return else: - # Found region specification - self.parse_region(include, region_type, line) + # Found region specification, + region_end = region_type_search.span()[1] + self.parse_region(include, region_type, region_end, line) def _raise_error(self, msg): if self.errors == 'warn': @@ -264,14 +265,18 @@ def parse_meta(meta_str): return result - def parse_region(self, include, region_type, line): + def parse_region(self, include, region_type, region_end, line): """Extract a Shape from a region string""" if self.coordsys is None: raise DS9RegionParserError("No coordinate system specified and a" " region has been found.") else: - helper = DS9RegionParser(self.coordsys, region_type, - self.global_meta, line) + helper = DS9RegionParser(coordsys=self.coordsys, + include=include, + region_type=region_type, + region_end=region_end, + global_meta=self.global_meta, + line=line) helper.parse() self.shapes.append(helper.shape) @@ -297,6 +302,9 @@ class DS9RegionParser(object): Flag at the beginning of the line region_type : str Region type + region_end : int + Coordinate of the end of the regions name, this is passed in order to + handle whitespaces correctly global_meta : dict Global meta data line : str @@ -328,16 +336,17 @@ class DS9RegionParser(object): } """DS9 language specification. This defines how a certain region is read""" - def __init__(self, coordsys, region_type, global_meta, line): + def __init__(self, coordsys, include, region_type, region_end, global_meta, line): self.coordsys = coordsys + self.include = include self.region_type = region_type + self.region_end = region_end self.global_meta = global_meta self.line = line self.meta_str = None self.coord_str = None - self.include = None self.composite = None self.coord = None self.meta = None @@ -346,6 +355,7 @@ def __init__(self, coordsys, region_type, global_meta, line): def __str__(self): ss = self.__class__.__name__ ss += '\nLine : {}'.format(self.line) + ss += '\nRegion end : {}'.format(self.region_end) ss += '\nMeta string : {}'.format(self.meta_str) ss += '\nCoord string: {}'.format(self.coord_str) ss += '\nShape: {}'.format(self.shape) @@ -357,7 +367,6 @@ def parse(self): log.debug(self) self.parse_composite() - self.parse_include() self.split_line() self.convert_coordinates() self.convert_meta() @@ -368,21 +377,11 @@ def parse_composite(self): """Determine whether the region is composite""" self.composite = "||" in self.line - def parse_include(self): - """Determine wether to include/exclude the region""" - if self.line[0] == '-': - self.include = False - else: - self.include = True - def split_line(self): """Split line into coordinates and meta string""" - end_of_region_name = len(self.region_type) - if not self.include: - end_of_region_name += 1 # coordinate of the # symbol or end of the line (-1) if not found hash_or_end = self.line.find("#") - temp = self.line[end_of_region_name:hash_or_end].strip(" |") + temp = self.line[self.region_end:hash_or_end].strip(" |") self.coord_str = regex_paren.sub("", temp) self.meta_str = self.line[hash_or_end:] @@ -423,5 +422,5 @@ def make_shape(self): coord=self.coord, meta=self.meta, composite=self.composite, - include=self.include + include=self.include != '-', ) diff --git a/regions/io/ds9/tests/test_ds9_language.py b/regions/io/ds9/tests/test_ds9_language.py index b037a64f..48e2e162 100644 --- a/regions/io/ds9/tests/test_ds9_language.py +++ b/regions/io/ds9/tests/test_ds9_language.py @@ -121,16 +121,10 @@ def test_ds9_string_to_objects(): def test_ds9_string_to_objects_whitespace(): """Simple test case for ds9_string_to_objects """ - ds9_str = '# Region file format: DS9 astropy/regions\nfk5\n circle(42.0000,43.0000,3.0000)\n' + ds9_str = '# Region file format: DS9 astropy/regions\nfk5\n -circle(42.0000,43.0000,3.0000)\n' parser = DS9Parser(ds9_str) parser.run() - regions = parser.shapes.to_region() - reg = regions[0] - - assert_allclose(reg.center.ra.deg, 42) - assert_allclose(reg.center.dec.deg, 43) - assert_allclose(reg.radius.value, 3) - + assert parser.shapes[0].include == False def test_ds9_io(tmpdir): """Simple test case for write_ds9 and read_ds9.