def __init__(self, distance=0, reference_level=None, *args, **kwargs): """Initialize `ReferencesAtlas` """ PyMVPAAtlas.__init__(self, *args, **kwargs) # sanity checks if not ('reference-atlas' in XMLBasedAtlas._children_tags(self.header)): raise XMLAtlasException( "ReferencesAtlas must refer to a some other atlas") referenceAtlasName = self.header["reference-atlas"].text # uff -- another evil import but we better use the factory method from mvpa2.atlases.warehouse import Atlas self.__referenceAtlas = Atlas(filename=reuse_absolute_path( self._filename, referenceAtlasName)) if self.__referenceAtlas.space != self.space or \ self.__referenceAtlas.space_flavor != self.space_flavor: raise XMLAtlasException( "Reference and original atlases should be in the same space") self.__referenceLevel = None # pylint shut up if reference_level is not None: self.set_reference_level(reference_level) self.set_distance(distance)
def __init__(self, distance=0, reference_level=None, *args, **kwargs): """Initialize `ReferencesAtlas` """ PyMVPAAtlas.__init__(self, *args, **kwargs) # sanity checks if not ('reference-atlas' in XMLBasedAtlas._children_tags( self.header)): raise XMLAtlasException( "ReferencesAtlas must refer to a some other atlas") referenceAtlasName = self.header["reference-atlas"].text # uff -- another evil import but we better use the factory method from mvpa2.atlases.warehouse import Atlas self.__referenceAtlas = Atlas( filename=reuse_absolute_path(self._filename, referenceAtlasName)) if self.__referenceAtlas.space != self.space or \ self.__referenceAtlas.space_flavor != self.space_flavor: raise XMLAtlasException( "Reference and original atlases should be in the same space") self.__referenceLevel = None # pylint shut up if reference_level is not None: self.set_reference_level(reference_level) self.set_distance(distance)
def _load_images(self): # shortcut imagefile = self.header.images.imagefile #self.nlevels = len(self._levels_by_id) # Set offset if defined in XML file # XXX: should just take one from the qoffset... now that one is # defined... this origin might be misleading actually self._origin = np.array( (0, 0, 0) ) if imagefile.attrib.has_key('offset'): self._origin = np.array( [int(x) for x in imagefile.get('offset').split(',')] ) # Load the image file which has labels if self._force_image_file is not None: imagefilename = self._force_image_file else: imagefilename = imagefile.text imagefilename = reuse_absolute_path(self._filename, imagefilename) try: self._image = None for ext in ['', '.nii.gz']: try: self._image = nb.load(imagefilename + ext) break except Exception, e: pass if self._image is None: raise e
def _load_images(self): # shortcut imagefile = self.header.images.imagefile #self.nlevels = len(self._levels_by_id) # Set offset if defined in XML file # XXX: should just take one from the qoffset... now that one is # defined... this origin might be misleading actually self._origin = np.array((0, 0, 0)) if imagefile.attrib.has_key('offset'): self._origin = np.array( [int(x) for x in imagefile.get('offset').split(',')]) # Load the image file which has labels if self._force_image_file is not None: imagefilename = self._force_image_file else: imagefilename = imagefile.text imagefilename = reuse_absolute_path(self._filename, imagefilename) try: self._image = None for ext in ['', '.nii.gz']: try: self._image = nb.load(imagefilename + ext) break except Exception, e: pass if self._image is None: raise e
def _load_images(self): resolution = self._resolution header = self.header images = header.images # Load present images # XXX might be refactored to avoid duplication of # effort with PyMVPAAtlas ni_image = None resolutions = [] if self._force_image_file is None: imagefile_candidates = [ reuse_absolute_path(self._filename, i.imagefile.text, force=True) for i in images] else: imagefile_candidates = [self._force_image_file] for imagefilename in imagefile_candidates: try: if not os.path.exists(imagefilename): # try with extension if filename doesn't exist imagefilename += '.nii.gz' ni_image_ = nb.load(imagefilename) except RuntimeError as e: raise RuntimeError(" Cannot open file " + imagefilename) resolution_ = ni_image_.header.get_zooms()[0] if resolution is None: # select this one if the best if ni_image is None or \ resolution_ < ni_image.header.get_zooms()[0]: ni_image = ni_image_ self._image_file = imagefilename else: if resolution_ == resolution: ni_image = ni_image_ self._image_file = imagefilename break else: resolutions += [resolution_] # TODO: also make use of summaryimagefile may be? if ni_image is None: msg = "Could not find an appropriate atlas among %d atlases." \ % len(imagefile_candidates) if resolution is not None: msg += " Atlases had resolutions %s" % \ (resolutions,) raise RuntimeError(msg) if __debug__: debug('ATL__', "Loading atlas data from %s" % self._image_file) self._image = ni_image self._resolution = ni_image.header.get_zooms()[0] self._origin = np.abs(ni_image.header.get_qform()[:3,3]) # XXX self._data = self._image.get_data() if len(self._data.shape) == 4: # want to have volume axis first self._data = np.rollaxis(self._data, -1)
def _load_images(self): # shortcut imagefile = self.header.images.imagefile #self.nlevels = len(self._levels_by_id) # Set offset if defined in XML file # XXX: should just take one from the qoffset... now that one is # defined... this origin might be misleading actually self._origin = np.array((0, 0, 0)) if 'offset' in imagefile.attrib: self._origin = np.array( [int(x) for x in imagefile.get('offset').split(',')]) # Load the image file which has labels if self._force_image_file is not None: imagefilename = self._force_image_file else: imagefilename = imagefile.text imagefilename = reuse_absolute_path(self._filename, imagefilename) try: self._image = None for ext in ['', '.nii.gz']: try: self._image = nb.load(imagefilename + ext) break except Exception as e: pass if self._image is None: raise e except RuntimeError as e: raise RuntimeError(" Cannot open file %s due to %s" % (imagefilename, e)) self._data = self._image.get_data() # we get the data as x,y,z[,t] but we want to have the time axis first # if any if len(self._data.shape) == 4: self._data = np.rollaxis(self._data, -1) # remove bogus dimensions on top of 4th if len(self._data.shape[0:-4]) > 0: bogus_dims = self._data.shape[0:-4] if max(bogus_dims) > 1: raise RuntimeError("Atlas %s has more than 4 of non-singular" \ "dimensions" % imagefilename) new_shape = self._data.shape[-4:] self._data.reshape(new_shape)
def _load_images(self): resolution = self._resolution header = self.header images = header.images # Load present images # XXX might be refactored to avoid duplication of # effort with PyMVPAAtlas ni_image = None resolutions = [] if self._force_image_file is None: imagefile_candidates = [ reuse_absolute_path(self._filename, i.imagefile.text, force=True) for i in images ] else: imagefile_candidates = [self._force_image_file] for imagefilename in imagefile_candidates: try: if not os.path.exists(imagefilename): # try with extension if filename doesn't exist imagefilename += '.nii.gz' ni_image_ = nb.load(imagefilename) except RuntimeError, e: raise RuntimeError, " Cannot open file " + imagefilename resolution_ = ni_image_.get_header().get_zooms()[0] if resolution is None: # select this one if the best if ni_image is None or \ resolution_ < ni_image.get_header().get_zooms()[0]: ni_image = ni_image_ self._image_file = imagefilename else: if resolution_ == resolution: ni_image = ni_image_ self._image_file = imagefilename break else: resolutions += [resolution_]
def _load_images(self): resolution = self._resolution header = self.header images = header.images # Load present images # XXX might be refactored to avoid duplication of # effort with PyMVPAAtlas ni_image = None resolutions = [] if self._force_image_file is None: imagefile_candidates = [ reuse_absolute_path(self._filename, i.imagefile.text, force=True) for i in images] else: imagefile_candidates = [self._force_image_file] for imagefilename in imagefile_candidates: try: if not os.path.exists(imagefilename): # try with extension if filename doesn't exist imagefilename += '.nii.gz' ni_image_ = nb.load(imagefilename) except RuntimeError, e: raise RuntimeError, " Cannot open file " + imagefilename resolution_ = ni_image_.get_header().get_zooms()[0] if resolution is None: # select this one if the best if ni_image is None or \ resolution_ < ni_image.get_header().get_zooms()[0]: ni_image = ni_image_ self._image_file = imagefilename else: if resolution_ == resolution: ni_image = ni_image_ self._image_file = imagefilename break else: resolutions += [resolution_]