Exemple #1
0
 def _read_array(f: tifffile.TiffFile) -> np.ndarray:
     image = f.asarray()
     assert image.dtype == uint16
     if image.ndim == 2:
         image = image[None, :, :]
     assert image.ndim == 3
     return image
def test(sample_path):


    detection_params = {'w_s': 11,
                        'peak_radius': 4.,
                        'threshold': 40.,
                        'max_peaks': 4
                        }

    sample = TiffFile(sample_path)

    curr_dir = os.path.dirname(__file__)
    fname = os.path.join(
        curr_dir, os.path.join(sample.fpath, sample.fname))

    arr = sample.asarray()
    peaks = detect_peaks(arr,
                         shape_label=('t', 'z', 'x', 'y'),
                         verbose=True,
                         show_progress=False,
                         parallel=True,
                         **detection_params)

    # del sample
    # sample = None
    gc.get_referrers(arr)
    del arr
    gc.collect()
Exemple #3
0
    def getarray(idx_buffer_filename):
        idx, buf, fname = idx_buffer_filename
        fbuf = BytesIO(buf)
        tfh = TiffFile(fbuf)
        ary = tfh.asarray()
        pageCount = ary.shape[0]
        if nplanes is not None:
            extra = pageCount % nplanes
            if extra:
                if discard_extra:
                    pageCount = pageCount - extra
                    logging.getLogger('thunder').warn(
                        'Ignored %d pages in file %s' % (extra, fname))
                else:
                    raise ValueError(
                        "nplanes '%d' does not evenly divide '%d in file %s'" %
                        (nplanes, pageCount, fname))
            values = [
                ary[i:(i + nplanes)] for i in range(0, pageCount, nplanes)
            ]
        else:
            values = [ary]
        tfh.close()

        if ary.ndim == 3:
            values = [val.squeeze() for val in values]

        nvals = len(values)
        keys = [(idx * nvals + timepoint, ) for timepoint in range(nvals)]
        return zip(keys, values)
Exemple #4
0
def getOrthoTif(zfBytes):
    bytesio = io.BytesIO(zfBytes)
    zfiles = zipfile.ZipFile(bytesio, "r")
    for fn in zfiles.namelist():
        if fn[-4:] == '.tif':
            tif = TiffFile(io.BytesIO(zfiles.open(fn).read()))
            return tif.asarray()
Exemple #5
0
class LocalTiff(LocalTarget):
    tif = None

    def open(self):
        self.tif = TiffFile(self.path)
        return self

    def close(self):
        if self.tif is not None:
            self.tif.close()

    def __len__(self):
        return len(self.tif.pages)

    @property
    def shape(self):
        return (len(self), *self.tif.pages[0].shape)

    def __getitem__(self, item):
        if isinstance(item, tuple):
            if isinstance(item[0], int):
                return self[item[0]][item[1:]]
            else:
                return self[item[0]][item]
        else:
            out = self.tif.asarray(key=item)
            if isinstance(item, list) and len(item) == 1:
                return out[None]  # Add axis
            elif isinstance(item, slice) and len(
                    range(*item.indices(len(self)))) == 1:
                return out[None]  # Add axis
            else:
                return out
Exemple #6
0
    def getarray(idx_buffer_filename):
        idx, buf, fname = idx_buffer_filename
        fbuf = BytesIO(buf)
        tfh = TiffFile(fbuf)
        ary = tfh.asarray()
        pageCount = ary.shape[0]
        if nplanes is not None:
            extra = pageCount % nplanes
            if extra:
                if discard_extra:
                    pageCount = pageCount - extra
                    logging.getLogger('thunder').warn('Ignored %d pages in file %s' % (extra, fname))
                else:
                    raise ValueError("nplanes '%d' does not evenly divide '%d in file %s'" % (nplanes, pageCount,
                                                                                              fname))
            values = [ary[i:(i+nplanes)] for i in range(0, pageCount, nplanes)]
        else:
            values = [ary]
        tfh.close()

        if ary.ndim == 3:
            values = [val.squeeze() for val in values]

        nvals = len(values)
        keys = [(idx*nvals + timepoint,) for timepoint in range(nvals)]
        return zip(keys, values)
class Viewer(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)

        path = QtWidgets.QFileDialog.getOpenFileName(self, 'Choose file', '/', "(*.tiff *.tif)")

        if not path[0]:
            return
        path = path[0]

        self.tif = TiffFile(path)

        self.widget = QtWidgets.QWidget(parent=self)
        self.vlayout = QtWidgets.QVBoxLayout(self.widget)

        iv = ImageView(parent=self)
        iv.setImage(self.tif.asarray(key=0))
        self.vlayout.addWidget(iv)

        self.slider = QtWidgets.QSlider(QtCore.Qt.Horizontal, parent=self)
        self.slider.setMaximum(len(self.tif.series) - 1)
        self.slider.setMinimum(0)
        self.slider.setSingleStep(1)
        self.slider.setPageStep(10)

        self.spinbox = QtWidgets.QSpinBox(parent=self)
        self.spinbox.setMaximum(len(self.tif.series) - 1)
        self.spinbox.setMinimum(0)
        self.spinbox.valueChanged.connect(self.slider.setValue)

        self.vlayout.addWidget(self.spinbox)
        self.vlayout.addWidget(self.slider)

        self.slider.valueChanged.connect(
            lambda i: iv.setImage(
                self.tif.asarray(key=i),
                autoRange=False,
                autoLevels=False,
                autoHistogramRange=False
            )
        )

        self.slider.valueChanged.connect(self.spinbox.setValue)

        self.setCentralWidget(self.widget)
Exemple #8
0
def getOrthoTif(zfBytes):
    #given a zipfile as bytes (i.e. from reading from a binary file),
    # return a np array of rgbx values for each pixel
    bytesio = io.BytesIO(zfBytes)
    zfiles = zipfile.ZipFile(bytesio, "r")
    #find tif:
    for fn in zfiles.namelist():
        if fn[-4:] == '.tif':  #found it, turn into array:
            tif = TiffFile(io.BytesIO(zfiles.open(fn).read()))
            return tif.asarray()
Exemple #9
0
  def get_bedmap2():

    filename = inspect.getframeinfo(inspect.currentframe()).filename
    home     = os.path.dirname(os.path.abspath(filename))
    
    direc    = home + '/antarctica/bedmap2/bedmap2_tiff/' 
    files    = ['bedmap2_bed',
                'bedmap2_surface', 
                'bedmap2_thickness',
                'bedmap2_icemask_grounded_and_shelves', 
                'bedmap2_rockmask', 
                #'bedmap2_lakemask_vostok', 
                'bedmap2_grounded_bed_uncertainty', 
                #'bedmap2_thickness_uncertainty_5km', 
                'bedmap2_coverage',
                'gl04c_geiod_to_WGS84']
    vara     = dict()
     
    # extents of domain :
    dx    =  1000
    west  = -3333500.0
    east  =  3333500.0
    north =  3333500.0
    south = -3333500.0

    #projection info :
    proj   = 'stere'
    lat_0  = '-90'
    lat_ts = '-71'
    lon_0  = '0'
    
    names = ['b', 'h', 'H', 'mask', 'rock_mask', 'b_uncert', 
             'coverage', 'gl04c_to_WGS84']
   

    sys.path.append(home + '/external_import_scripts')
    from tifffile import TiffFile
    # retrieve data :
    for n, f in zip(names, files):
      data    = TiffFile(direc + f + '.tif')
      vara[n] = {'map_data'          : data.asarray(),
                 'map_western_edge'  : west,
                 'map_eastern_edge'  : east,  
                 'map_southern_edge' : south,
                 'map_northern_edge' : north,
                 'projection'        : proj,
                 'standard lat'      : lat_0,
                 'standard lon'      : lon_0,
                 'lat true scale'    : lat_ts}
    return vara 
Exemple #10
0
def getOrthoTif(filename, zfBytes):
    """
    Ans 1b.
    :param filename:
    :param zfBytes:
    :return:
    """
    #given a zipfile as bytes (i.e. from reading from a binary file),
    # return a np array of rgbx values for each pixel
    bytesio = io.BytesIO(zfBytes)
    zfiles = zipfile.ZipFile(bytesio, "r")
    #find tif:
    for fn in zfiles.namelist():
        if fn[-4:] == '.tif':  #found it, turn into array:
            tif = TiffFile(io.BytesIO(zfiles.open(fn).read()))

    return splitImage(filename.rsplit("/")[-1], tif.asarray(), 5)
def read_tiff_image(tiff_file, channel_names=None):
    from tifffile import TiffFile

    tiff_img = TiffFile(tiff_file)
    tiff_channels = tiff_img.asarray()

    n_channels = 1 if tiff_channels.ndim == 3 else tiff_channels.shape[1]

    if n_channels > 1:
        if channel_names is None:
            channel_names = ["CH" + str(i) for i in range(n_channels)]
        img = {}
        for i_channel, channel_name in enumerate(channel_names):
            img[channel_name] = SpatialImage(
                np.transpose(tiff_channels[:, i_channel], (1, 2, 0)))
    else:
        img = SpatialImage(np.transpose(tiff_channels, (1, 2, 0)))

    return img
Exemple #12
0
    def get_gre_measures():

        filename = inspect.getframeinfo(inspect.currentframe()).filename
        home = os.path.dirname(os.path.abspath(filename))

        sys.path.append(home + '/external_import_scripts')
        from tifffile import TiffFile

        direc = home + '/greenland/measures/greenland_vel_mosaic500_2008_2009_'
        files = ['sp', 'vx', 'vy', 'ex', 'ey']
        vara = dict()

        # extents of domain :
        nx = 3010
        ny = 5460
        dx = 500
        west = -645000.0
        east = west + nx * dx
        south = -3370000.0
        north = south + ny * dx

        #projection info :
        proj = 'stere'
        lat_0 = '90'
        lat_ts = '70'
        lon_0 = '-45'

        # retrieve data :
        vara['dataset'] = 'measures'
        for f in files:
            data = TiffFile(direc + f + '.tif')
            vara[f] = {
                'map_data': data.asarray()[::-1, :],
                'map_western_edge': west,
                'map_eastern_edge': east,
                'map_southern_edge': south,
                'map_northern_edge': north,
                'projection': proj,
                'standard lat': lat_0,
                'standard lon': lon_0,
                'lat true scale': lat_ts
            }
        return vara
    def open(self, filename):
        prefs = self.preferences
        # OPEN
        tif = TiffFile(str(filename))
        img = tif.asarray()
        # due to different conventions:
        #img = transpose(img)
        # crop
        if prefs.pCrop.value():
            r = (prefs.pCropX0.value(), prefs.pCropX1.value(),
                 prefs.pCropY0.value(), prefs.pCropY1.value())
            img = img[r[0]:r[1], r[2]:r[3]]
        # resize
        if prefs.pResize.value():
            img = cv2.resize(img,
                             (prefs.pResizeX.value(), prefs.pResizeY.value()))

        img = self.toFloat(img)

        try:
            # try to extract labels names set by imageJ:
            labels = tif.pages[0].imagej_tags['labels']
            # remove surplus information:
            for n, l in enumerate(labels):
                try:
                    i = l.index('\n')
                    if i != -1:
                        labels[n] = l[:i]
                except ValueError:
                    # no \n in label
                    pass

        except AttributeError:
            if img.ndim == 3:
                # color image
                labels = [str(r) for r in range(len(img))]
            else:
                labels = None

        tif.close()
        return img, labels
  def get_gre_measures():
    
    filename = inspect.getframeinfo(inspect.currentframe()).filename
    home     = os.path.dirname(os.path.abspath(filename))
    
    sys.path.append(home + '/external_import_scripts')
    from tifffile import TiffFile
    
    direc    = home + '/greenland/measures/greenland_vel_mosaic500_2008_2009_' 
    files    = ['sp', 'vx', 'vy', 'ex', 'ey']
    vara     = dict()
     
    # extents of domain :
    nx    =  3010
    ny    =  5460
    dx    =  500
    west  = -645000.0
    east  =  west  + nx*dx
    south = -3370000.0 
    north =  south + ny*dx

    #projection info :
    proj   = 'stere'
    lat_0  = '90'
    lat_ts = '70'
    lon_0  = '-45'
    
    # retrieve data :
    vara['dataset'] = 'measures'
    for f in files:
      data    = TiffFile(direc + f + '.tif')
      vara[f] = {'map_data'          : data.asarray()[::-1, :],
                 'map_western_edge'  : west,
                 'map_eastern_edge'  : east,  
                 'map_southern_edge' : south,
                 'map_northern_edge' : north,
                 'projection'        : proj,
                 'standard lat'      : lat_0,
                 'standard lon'      : lon_0,
                 'lat true scale'    : lat_ts}
    return vara
Exemple #15
0
    def load(
        cls,
        load_locations: typing.List[typing.Union[str, BytesIO, Path]],
        range_changed: typing.Callable[[int, int], typing.Any] = None,
        step_changed: typing.Callable[[int], typing.Any] = None,
        metadata: typing.Optional[dict] = None,
    ):
        image_file = TiffFile(load_locations[0])
        count_pages = [0]
        mutex = Lock()

        def report_func():
            mutex.acquire()
            count_pages[0] += 1
            step_changed(count_pages[0])
            mutex.release()

        range_changed(0, len(image_file.series[0]))
        image_file.report_func = report_func
        mask_data = image_file.asarray()
        return MaskInfo(load_locations[0], mask_data)
Exemple #16
0
  def get_study_region(self):
    """
    return:
      vara - dictionary - contains projection information as well as bed data
                         intended to be an input to utilites.DataInput
    """

    sys.path.append(self.home + '/external_import_scripts')
    from tifffile import TiffFile
    
    direc    = self.home + '/elevation_data/elevation/ASTGTM2_S78E161_dem' 
    vara     = dict()
     
    # extents of domain :
    nx    =  1049
    ny    =  1031
    dx    =  17.994319205518387
    west  =  423863.131
    east  =  west  + nx*dx
    south =  -1304473.006
    north =  south + ny*dx
    
    # projection info :
    proj   = 'stere'
    lat_0  = '-90'
    lon_0  = '0'
    lat_ts = '-71'
    
    # retrieve data :
    data    = TiffFile(direc + '.tif')
    vara['b'] = {'map_data'          : data.asarray(),
                 'map_western_edge'  : west,
                 'map_eastern_edge'  : east,  
                 'map_southern_edge' : south,
                 'map_northern_edge' : north,
                 'projection'        : proj,
                 'standard lat'      : lat_0,
                 'standard lon'      : lon_0,
                 'lat true scale'    : lat_ts}
    return vara
def getOrthoTif(filename, zfBytes):
    filename = filename[filename.rfind('/') + 1:]
    #given a zipfile as bytes (i.e. from reading from a binary file),
    # return a np array of rgbx values for each pixel
    bytesio = io.BytesIO(zfBytes)
    zfiles = zipfile.ZipFile(bytesio, "r")
    #find tif:
    for fn in zfiles.namelist():
        if fn[-4:] == '.tif':  #found it, turn into array:
            tif = TiffFile(io.BytesIO(zfiles.open(fn).read()))
            result = []
            tifArray = tif.asarray()
            splitBy = int(tifArray.shape[0] / 500)
            split1 = np.split(tifArray, splitBy, axis=0)
            arrayNumber = 0
            for array in split1:
                split2 = np.split(array, splitBy, axis=1)
                for output in split2:
                    outputFileName = filename + "-" + str(arrayNumber)
                    arrayNumber = arrayNumber + 1
                    result.append((outputFileName, output))
            return result
Exemple #18
0
    def __init__(self, filename, dtype=None):
        self.type = 2
        tf = TiffFile(filename)
        vb = tf.asarray()

        nc = 0
        if len(vb.shape) == 3:
            zdim, ydim, xdim = vb.shape
        else:
            zdim, ydim, xdim, nc = vb.shape

        if nc > 0 and vb.dtype == np.uint8:
            nc = 0

        dp = tf.pages[0]
        try:
            xs = float(dp.x_resolution[1]) / dp.x_resolution[0]
            ys = float(dp.y_resolution[1]) / dp.y_resolution[0]
        except Exception:
            xs = ys = 1.0
        zs = 1.0
        vu = 0
        if dp.is_imagej:
            if dp.imagej_tags.has_key('spacing'):
                zs = float(dp.imagej_tags.spacing)
            if dp.imagej_tags.has_key('unit') and dp.imagej_tags.unit != '':
                vu = Volume.VOXEL_UNITS[dp.imagej_tags.unit]
        vs = [xs, ys, zs]

        self.filename = filename
        self.data = vb if dtype is None or dtype is vb.dtype else vb.astype(dtype)
        self.xdim = xdim
        self.ydim = ydim
        self.zdim = zdim
        self.vdim = np.array([xdim, ydim, zdim])
        self.vunit = vu
        self.vsize = np.round(vs, 3)
        self.nchan = nc
        self.clist = [0] * 8
Exemple #19
0
def getOrthoTif(fileName, zfBytes):
    fileName = fileName[fileName.rfind('/') + 1:]
#    Given a zipfile as bytes (i.e. from reading from a binary file),
#    Return a np array of rgbx values for each pixel
    bytesio = io.BytesIO(zfBytes)
    zfiles = zipfile.ZipFile(bytesio, "r")
#    find tif:
    for fn in zfiles.namelist():
        if fn[-4:] == '.tif':
#            found it, turn into array:
            tif = TiffFile(io.BytesIO(zfiles.open(fn).read()))
            arr = tif.asarray();
#            Divide image in 500 * 500 size
            res = []
            factor = int(arr.shape[0]/500)
            hArr = np.split(arr, factor, axis=0)
            count = 0
            for h in hArr:
                vArr = np.split(h, factor, axis = 1)
                for v in vArr:
                    name = fileName + "-" + str(count)
                    res.append((name, v))
                    count += 1
            return res
import sys
sys.path.append("..")

from peak_detection import detect_peaks
from peak_detection import show_peaks

from tifffile import TiffFile

fname = 'sample.tif'

detection_parameters = {'w_s': 10,
                        'peak_radius': 4.,
                        'threshold': 60.,
                        'max_peaks': 10
                        }

sample = TiffFile(fname)
arr = sample.asarray()
peaks = detect_peaks(arr,
                     shape_label=('t', 'z', 'x', 'y'),
                     parallel=True,
                     verbose=True,
                     show_progress=False,
                     **detection_parameters)

for id, p in peaks.groupby(level="stacks"):
    print((p.shape[0]))

show_peaks(arr, peaks, 3)
Exemple #21
0
class TiffImageStack(ImageStack):
    extensions = (
        '.tif',
        '.tiff',
    )

    priority = 1000

    def open(self, location, **kwargs):
        self.tiff = TiffFile(location.path)

        self._tiff_first_series = self.tiff.series[0]

        self._tiff_axes = self._tiff_first_series.axes
        self._tiff_shape = self._tiff_first_series.shape
        self._tiff_memmap = self.tiff.asarray(out='memmap')

        if 'swap_axes' in self.parameters:
            from_, to_ = self.parameters['swap_axes'].split('..')
            from_, to_ = from_ + to_, to_ + from_

            self._tiff_axes = ''.join(
                to_[from_.index(axis)] if axis in from_ else axis
                for axis in self._tiff_axes)

        if 'S' in self._tiff_axes and 'C' not in self._tiff_axes:
            self._tiff_axes = self._tiff_axes.replace('S', 'C')

        assert self._tiff_axes.endswith('YX') or self._tiff_axes.endswith(
            'YXS') or self._tiff_axes.endswith('YXC')

        dim = []
        sizes = []

        if 'Q' in self._tiff_axes:
            # Q is "unknown"
            if 'T' not in self._tiff_axes:
                self._tiff_axes = self._tiff_axes.replace('Q', 'T')
            elif 'R' not in self._tiff_axes:
                self._tiff_axes = self._tiff_axes.replace('Q', 'R')
            elif 'Z' not in self._tiff_axes:
                self._tiff_axes = self._tiff_axes.replace('Q', 'Z')
            else:
                raise RuntimeError(
                    'There is an unknown axis (Q) in the TIFF file,'
                    'but as well T, R, Z axes. Please change the TIFF file.')

        for char, size in zip(self._tiff_axes, self._tiff_shape):
            if char in ('X', 'Y'):
                continue

            dim.append(Dimensions.by_char(char))
            sizes.append(size)

        self.set_dimensions_and_sizes(dim, sizes)

    # noinspection PyProtectedMember
    def notify_fork(self):
        self.tiff._fh.close()
        self.tiff._fh.open()

    def get_data(self, what):
        key = tuple([
            what.get(Dimensions.by_char(char), 0)
            if char not in ('X', 'Y') else slice(None, None, None)
            for char, size in zip(self._tiff_axes, self._tiff_shape)
        ])
        data = self._tiff_memmap.__getitem__(key)

        return data

    def get_meta(self, what):
        try:
            calibration = float(self.parameters['calibration'])
        except KeyError:
            try:
                tags = self.tiff.pages[0].tags
                assert tags['ResolutionUnit'].value == 1  # 1 == µm?
                x_resolution, y_resolution = tags['XResolution'].value, tags[
                    'YResolution'].value
                assert x_resolution == y_resolution
                calibration = x_resolution[1] / x_resolution[0]
            except (KeyError, AssertionError):
                calibration = 1.0

        try:
            interval = float(self.parameters['interval'])
        except KeyError:
            interval = 1.0

        try:
            time = what[Dimensions.Time]
        except KeyError:
            time = 1.0

        position = self.__class__.Position(x=0.0, y=0.0, z=0.0)
        meta = self.__class__.Metadata(time=interval * time,
                                       position=position,
                                       calibration=calibration)
        return meta
Exemple #22
0
class Intermediate_handler:
    """ Wrapper for saving intermediate results to file and keeping track of them """
    def __init__(self, filename):
        try:
            os.remove(filename)
        except:
            pass
        self.plane = 0
        self.tif = None
        self.filename = filename
        self.refs = []
        self.array = None

    def reader(self):
        self.read = True
        self.write = False
        return self

    def writer(self):
        self.write = True
        self.read = False
        return self

    def __enter__(self):
        if self.write:
            self.tif = TiffWriter(self.filename, bigtiff=True, append=True)
        if self.read:
            self.tif = TiffFile(self.filename)
            self.array = self.tif.asarray()  # memmap=True)

        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.array = None
        self.tif.close()

    def close(self):
        self.array = None

        if self.tif is not None:
            self.tif.close()

    def write_plane(self, data, t, z, f):
        # works plane by plane
        # print 'writing plane' + str((t,z,f))
        # print data.shape

        self.tif.save(data, compress=0, contiguous=True)

        if len(data.shape) > 2:
            for i in range(data.shape[0]):
                self.plane += 1
                self.refs = self.refs + [(t, z + i)]
        else:
            self.plane += 1
            self.refs = self.refs + [(t, z)]

    def read_plane(self, t, z, f):
        # works plane by plane
        planeno = self.refs.index((t, z))
        return np.squeeze(self.tif.asarray(planeno))
Exemple #23
0
    def get_bedmap2(thklim=0.0):

        filename = inspect.getframeinfo(inspect.currentframe()).filename
        home = os.path.dirname(os.path.abspath(filename))
        direc = home + '/antarctica/bedmap2/bedmap2_tiff/'

        sys.path.append(home + '/external_import_scripts')
        from tifffile import TiffFile

        b = TiffFile(direc + 'bedmap2_bed.tif')
        h = TiffFile(direc + 'bedmap2_surface.tif')
        H = TiffFile(direc + 'bedmap2_thickness.tif')
        mask = TiffFile(direc + 'bedmap2_icemask_grounded_and_shelves.tif')
        rock_mask = TiffFile(direc + 'bedmap2_rockmask.tif')
        b_uncert = TiffFile(direc + 'bedmap2_grounded_bed_uncertainty.tif')
        coverage = TiffFile(direc + 'bedmap2_coverage.tif')
        gl04c_WGS84 = TiffFile(direc + 'gl04c_geiod_to_WGS84.tif')

        b = b.asarray()
        h = h.asarray()
        H = H.asarray()
        mask = mask.asarray()
        rock_mask = rock_mask.asarray()
        b_uncert = b_uncert.asarray()
        coverage = coverage.asarray()
        gl04c_WGS84 = gl04c_WGS84.asarray()

        h[H < thklim] = b[H < thklim] + thklim
        H[H < thklim] = thklim

        vara = dict()

        # extents of domain :
        dx = 1000
        west = -3333500.0
        east = 3333500.0
        north = 3333500.0
        south = -3333500.0

        #projection info :
        proj = 'stere'
        lat_0 = '-90'
        lat_ts = '-71'
        lon_0 = '0'

        names = [
            'B', 'S', 'H', 'mask', 'rock_mask', 'b_uncert', 'coverage',
            'gl04c_WGS84'
        ]
        ftns = [b, h, H, mask, rock_mask, b_uncert, coverage, gl04c_WGS84]

        # retrieve data :
        vara['dataset'] = 'bedmap 2'
        for n, f in zip(names, ftns):
            vara[n] = {
                'map_data': f[::-1, :],
                'map_western_edge': west,
                'map_eastern_edge': east,
                'map_southern_edge': south,
                'map_northern_edge': north,
                'projection': proj,
                'standard lat': lat_0,
                'standard lon': lon_0,
                'lat true scale': lat_ts
            }
        return vara
  def get_bedmap2(thklim = 0.0):

    filename = inspect.getframeinfo(inspect.currentframe()).filename
    home     = os.path.dirname(os.path.abspath(filename))
    direc    = home + '/antarctica/bedmap2/bedmap2_tiff/' 

    sys.path.append(home + '/external_import_scripts')
    from tifffile import TiffFile
   
    b           = TiffFile(direc + 'bedmap2_bed.tif')
    h           = TiffFile(direc + 'bedmap2_surface.tif') 
    H           = TiffFile(direc + 'bedmap2_thickness.tif')
    mask        = TiffFile(direc + 'bedmap2_icemask_grounded_and_shelves.tif') 
    rock_mask   = TiffFile(direc + 'bedmap2_rockmask.tif') 
    b_uncert    = TiffFile(direc + 'bedmap2_grounded_bed_uncertainty.tif') 
    coverage    = TiffFile(direc + 'bedmap2_coverage.tif')
    gl04c_WGS84 = TiffFile(direc + 'gl04c_geiod_to_WGS84.tif')
   
    b           = b.asarray()
    h           = h.asarray()
    H           = H.asarray()
    mask        = mask.asarray()
    rock_mask   = rock_mask.asarray()
    b_uncert    = b_uncert.asarray()
    coverage    = coverage.asarray() 
    gl04c_WGS84 = gl04c_WGS84.asarray()
    
    h[H < thklim] = b[H < thklim] + thklim
    H[H < thklim] = thklim

    vara        = dict()
     
    # extents of domain :
    dx    =  1000
    west  = -3333500.0
    east  =  3333500.0
    north =  3333500.0
    south = -3333500.0

    #projection info :
    proj   = 'stere'
    lat_0  = '-90'
    lat_ts = '-71'
    lon_0  = '0'
    
    names = ['B', 'S', 'H', 'mask', 'rock_mask', 'b_uncert', 
             'coverage', 'gl04c_WGS84']
    ftns  = [b, h, H, mask, rock_mask, b_uncert, coverage, gl04c_WGS84]
   
    # retrieve data :
    vara['dataset'] = 'bedmap 2'
    for n, f in zip(names, ftns):
      vara[n] = {'map_data'          : f[::-1, :],
                 'map_western_edge'  : west,
                 'map_eastern_edge'  : east,  
                 'map_southern_edge' : south,
                 'map_northern_edge' : north,
                 'projection'        : proj,
                 'standard lat'      : lat_0,
                 'standard lon'      : lon_0,
                 'lat true scale'    : lat_ts}
    return vara 
Exemple #25
0
	def get_gre_measures():
		"""
		`Greenland Measures <https://nsidc.org/data/NSIDC-0478/versions/2#>`_
		surface velocity data.  This function creates a new data field with
		key ``mask`` that is 1 where velocity measurements are present
		and 0 where they are not.

		You will have to download the data from the authors and manually place
		it in the ``cslvr_root_dir/data/greenland`` directory.

		The keys of the dictionary returned by this function are :

		* ``vx``  -- :math:`x`-component of velocity
		* ``vy``  -- :math:`y`-component of velocity
		* ``ex``  -- :math:`x`-component of velocity error
		* ``ey``  -- :math:`y`-component of velocity error
		* ``mask`` -- observation mask

		:rtype: dict
		"""

		s    = "::: getting Greenland 'Measures' data from DataFactory :::"
		print_text(s, DataFactory.color)

		global home

		#direc    = home + '/greenland/measures/greenland_vel_mosaic500_2008_2009'
		direc = home + '/greenland/greenland_vel_mosaic500_2016_2017_'
		#TODO: find a way to intelligently leave out the error if you don't want
		#      to download them:
		files    = ['mask', 'vx_v2', 'vy_v2']#, '_ex_v2', '_ey_v2']
		vara     = dict()

		d    = TiffFile(direc + 'vx_v2.tif')
		mask = (d.asarray() != -2e9).astype('i')

		ftns = [mask]
		for n in files[1:]:
			data    = TiffFile(direc + n + '.tif')
			ftns.append(data.asarray())
			print_text('      Measures : %-*s key : "%s" '%(30,n,n), '230')
		print_text('      Measures : %-*s key : "%s"'%(30,files[0],files[0]), '230')

		#projection info :
		proj   = 'stere'
		lat_0  = '90'
		lat_ts = '70'
		lon_0  = '-45'

		# create projection :
		txt  =   " +proj="   + proj \
		       + " +lat_0="  + lat_0 \
		       + " +lat_ts=" + lat_ts \
		       + " +lon_0="  + lon_0 \
		       + " +k=1 +x_0=0 +y_0=0 +no_defs +a=6378137 +rf=298.257223563" \
		       + " +towgs84=0.000,0.000,0.000 +to_meter=1"
		p    = Proj(txt)

		# extents of domain :
		ny,nx       =  shape(d.asarray())
		dx          = 500
		lon_min     = -75.0
		lon_max     = -14.0
		lat_min     =  60.0
		lat_max     =  83.0

		# old v1 values :
		# FIXME: no projection extents provided, only longitude ranges which
		#        do not mach the data.  What a f*****g disappointment.
		west  = -645000.0
		east  =  west  + nx*dx
		south = -3370000.0
		north =  south + ny*dx

		# set up a dictionary for use with cslvr::DataInput class :
		vara['pyproj_Proj']       = p
		vara['map_western_edge']  = west
		vara['map_eastern_edge']  = east
		vara['map_southern_edge'] = south
		vara['map_northern_edge'] = north
		vara['nx']                = nx
		vara['ny']                = ny
		vara['dx']                = dx

		# retrieve data :
		vara['dataset']   = 'measures'
		vara['continent'] = 'greenland'
		for f,n in zip(ftns, files):
			vara[n] = f[::-1, :]
		return vara
Exemple #26
0
    def __init__(self, fname):
        tif = TiffFile(fname)
        # This class is only for ImageJ tiffs.
        assert tif.is_imagej is True

        # 'axes' defines the data arrangement in the image array, e.g.
        # 'ZCYX' for mutliple-stacks, mulitple-channels images,
        # 'CYX' for multiple-channels, single stack images, etc.
        axes = tif.series[0].axes

        # Images with z-stacks are not yet supported.
        if 'Z' in axes:
            raise NotImplementedError("Z-stacks are not supported")

        # Shape of file will only reflect (x, y) dimensions. Number
        # of channels is stored elsewhere.
        x_indx = axes.find('X')
        y_indx = axes.find('Y')
        shape = tif.series[0].shape
        self.shape = (shape[y_indx], shape[x_indx])

        # Read tags and extract relevant information.
        self.fname = tif.filename
        self.fpath = tif.filehandle.path
        self.nchannels = tif.imagej_metadata['channels']
        # If the image contains its own luts, use them. (Is this the
        # case with all images where 'mode' is 'composite'?)
        if 'luts' in tif.imagej_metadata:
            luts = tif.imagej_metadata['luts']
            cmaps = [lut_to_cmap(lut) for lut in luts]
        # If there are no luts, use a standard colours for each
        # channel. (Is this the case with all images
        # tif.imagej_metadata['mode'] == 'color'?)
        else:
            colours = ('b', 'g', 'r', 'm', 'c')[:self.nchannels]
            cmaps = [std_cmap(colour) for colour in colours]
        ranges = tif.imagej_metadata['Ranges']
        ranges = np.array(ranges).reshape(self.nchannels, -1)

        # Read image data.
        im = tif.asarray()
        # Single-channel images.
        if im.ndim == 2:
            self.__dict__['chan1'] = Channel(1, im, cmaps, ranges)
        # Multi-channel images.
        elif im.ndim == 3:
            for n in range(self.nchannels):
                # Multi-channel images are of type 'CYX' (first
                # dimension of the data array is 'channel'), where each
                # channel is in its own tiff page, or of type 'YXS'
                # (third dimension is 'sample per pixel', in practice
                # equivalent to channel), where all channels are
                # contained in the same tiff page and the page's
                # 'samples_per_pixel' is > 1.
                if axes == 'YXS':
                    yx = im[:, :, n]
                elif axes == 'CYX':
                    yx = im[n, :, :]
                # Create the channel. A unit is added to the channel
                # number's name to keep in line with the way channels
                # are displayed/named in ImageJ (i.e first channel is
                # number one, not 0.
                self.__dict__['chan%i' % (n + 1)] = Channel(
                    n + 1, yx, cmaps[n], ranges[n])
        else:
            raise NotImplementedError("Images with more than " +
                                      "3 dimensions are not supported.")

        # A 'tags' dictionary will hold relevant data.
        self.tags = {
            'image_name': tif.filename,
            'image_width': tif.pages[0].imagewidth,
            'image_length': tif.pages[0].imagelength,
            'unit': tif.imagej_metadata['unit'],
            'x_resolution': tif.pages[0].tags['XResolution'].value,
            'y_resolution': tif.pages[0].tags['YResolution'].value
        }
Exemple #27
0
import sys
sys.path.append("..")

from peak_detection import detect_peaks
from tifffile import TiffFile

fname = 'sample.tif'

detection_parameters = {'w_s': 10,
                        'peak_radius': 4.,
                        'threshold': 60.,
                        'max_peaks': 10
                        }

sample = TiffFile(fname)
peaks = detect_peaks(sample.asarray(),
                     shape_label=('t', 'z', 'x', 'y'),
                     parallel=True,
                     verbose=True,
                     show_progress=False,
                     **detection_parameters)

for id, p in peaks.groupby(level="stacks"):
    print p.shape[0]
Exemple #28
0
	def get_bedmap2(thklim = 0.0):
		"""
		Antarctica `Bedmap 2 <https://www.bas.ac.uk/project/bedmap-2/>`_
		topography data.

		This data is downloaded by executing the script
		``cslvr_root/scripts/download_antarctica_data.py``

		The keys of the dictionary returned by this function are :

		* ``B``  -- basal topography height
		* ``S``  -- surface topography height
		* ``H``  -- ice thickness
		* ``mask`` -- ice shelf mask
		* ``rock_mask`` -- rock outcrop mask
		* ``b_uncert`` -- basal-topography uncertainty
		* ``coverage`` -- is a binary grid showing the distribution of ice thickness data used in the grid of ice thickness
		* ``gl04c_WGS84`` -- gives the values (as floating point) used to convert from heights relative to WGS84 datum to heights relative to EIGEN-GL04C geoid (to convert back to WGS84, add this grid)

		:param thklim: minimum-allowed ice thickness
		:type thklim: float
		:rtype: dict
		"""

		s    = "::: getting 'Bedmap 2' data from DataFactory :::"
		print_text(s, DataFactory.color)

		global home
		direc    = home + '/antarctica/bedmap2/'

		B           = TiffFile(direc + 'bedmap2_bed.tif')
		S           = TiffFile(direc + 'bedmap2_surface.tif')
		H           = TiffFile(direc + 'bedmap2_thickness.tif')
		mask        = TiffFile(direc + 'bedmap2_icemask_grounded_and_shelves.tif')
		rock_mask   = TiffFile(direc + 'bedmap2_rockmask.tif')
		b_uncert    = TiffFile(direc + 'bedmap2_grounded_bed_uncertainty.tif')
		coverage    = TiffFile(direc + 'bedmap2_coverage.tif')
		gl04c_WGS84 = TiffFile(direc + 'gl04c_geiod_to_WGS84.tif')

		B           = B.asarray()
		S           = S.asarray()
		H           = H.asarray()
		mask        = mask.asarray()
		rock_mask   = rock_mask.asarray()
		b_uncert    = b_uncert.asarray()
		coverage    = coverage.asarray()
		gl04c_WGS84 = gl04c_WGS84.asarray()

		# format the mask for cslvr :
		mask[mask == 1]   = 2
		mask[mask == 0]   = 1
		mask[mask == 127] = 0

		# remove the junk data and impose thickness limit :
		B = S - H
		H[H == 32767]  = thklim
		H[H <= thklim] = thklim
		S = B + H

		vara        = dict()

		# extents of domain :
		nx    =  6667
		ny    =  6667
		dx    =  1000.0
		west  = -3333500.0
		east  =  3333500.0
		north =  3333500.0
		south = -3333500.0

		#projection info :
		proj   = 'stere'
		lat_0  = '-90'
		lat_ts = '-71'
		lon_0  = '0'

		# create projection :
		txt  =   " +proj="   + proj \
		       + " +lat_0="  + lat_0 \
		       + " +lat_ts=" + lat_ts \
		       + " +lon_0="  + lon_0 \
		       + " +k=1 +x_0=0 +y_0=0 +no_defs +a=6378137 +rf=298.257223563" \
		       + " +towgs84=0.000,0.000,0.000 +to_meter=1"
		p    = Proj(txt)

		# save the data in matlab format :
		vara['pyproj_Proj']       = p
		vara['map_western_edge']  = west
		vara['map_eastern_edge']  = east
		vara['map_southern_edge'] = south
		vara['map_northern_edge'] = north
		vara['nx']                = nx
		vara['ny']                = ny
		vara['dx']                = dx

		names = ['B', 'S', 'H', 'mask', 'rock_mask', 'b_uncert',
		         'coverage', 'gl04c_WGS84']
		ftns  = [B, S, H, mask, rock_mask, b_uncert, coverage, gl04c_WGS84]

		for n in names:
			print_text('      Bedmap 2 : %-*s key : "%s" '%(30,n,n), '230')

		# retrieve data :
		vara['dataset']   = 'bedmap 2'
		vara['continent'] = 'antarctica'
		for n, f in zip(names, ftns):
			vara[n] = f[::-1, :]
		return vara
Exemple #29
0
import sys
sys.path.append("..")

from peak_detection import detect_peaks
from tifffile import TiffFile

fname = 'sample.tif'

detection_parameters = {
    'w_s': 10,
    'peak_radius': 4.,
    'threshold': 60.,
    'max_peaks': 10
}

sample = TiffFile(fname)
peaks = detect_peaks(sample.asarray(),
                     shape_label=('t', 'z', 'x', 'y'),
                     parallel=True,
                     verbose=True,
                     show_progress=False,
                     **detection_parameters)

for id, p in peaks.groupby(level="stacks"):
    print p.shape[0]
Exemple #30
0
class TiffImageReader(BaseImageReader):
    """
    TIFF/LSM files reader. Base reading with :py:meth:`BaseImageReader.read_image`

    image_file: TiffFile
    mask_file: TiffFile
    """
    def __init__(self, callback_function=None):
        super().__init__(callback_function)
        self.image_file = None
        self.mask_file: typing.Optional[TiffFile] = None
        self.colors = None
        self.labels = None
        self.ranges = None

    def read(self,
             image_path: typing.Union[str, BytesIO, Path],
             mask_path=None,
             ext=None) -> Image:
        """
        Read tiff image from tiff_file
        """
        self.spacing, self.colors, self.labels, self.ranges = self.default_spacing, None, None, None
        self.image_file = TiffFile(image_path)
        total_pages_num = len(self.image_file.series[0])
        if mask_path is not None:
            self.mask_file = TiffFile(mask_path)
            total_pages_num += len(self.mask_file.series[0])
            self.verify_mask()
        else:
            self.mask_file = None

        # shape = self.image_file.series[0].shape
        axes = self.image_file.series[0].axes
        self.callback_function("max", total_pages_num)

        if self.image_file.is_lsm:
            self.read_lsm_metadata()
        elif self.image_file.is_imagej:
            self.read_imagej_metadata()
        elif self.image_file.is_ome:
            self.read_ome_metadata()
        else:
            x_spac, y_spac = self.read_resolution_from_tags()
            self.spacing = self.default_spacing[0], y_spac, x_spac
        mutex = Lock()
        count_pages = [0]

        def report_func():
            mutex.acquire()
            count_pages[0] += 1
            self.callback_function("step", count_pages[0])
            mutex.release()

        self.image_file.report_func = report_func
        try:
            image_data = self.image_file.asarray()
        except ValueError as e:  # pragma: no cover
            raise TiffFileException(*e.args)
        image_data = self.update_array_shape(image_data, axes)
        if self.mask_file is not None:
            self.mask_file.report_func = report_func
            mask_data = self.mask_file.asarray()
            mask_data = self.update_array_shape(
                mask_data, self.mask_file.series[0].axes)[..., 0]
        else:
            mask_data = None
        self.image_file.close()
        if self.mask_file is not None:
            self.mask_file.close()
        if not isinstance(image_path, str):
            image_path = ""
        return self.image_class(
            image_data,
            self.spacing,
            mask=mask_data,
            default_coloring=self.colors,
            labels=self.labels,
            ranges=self.ranges,
            file_path=os.path.abspath(image_path),
            axes_order=self.return_order(),
        )

    def verify_mask(self):
        """
        verify if mask fit to image. Raise ValueError exception on error
        :return:
        """
        if self.mask_file is None:  # pragma: no cover
            return
        image_series = self.image_file.pages[0]
        mask_series = self.mask_file.pages[0]
        for i, pos in enumerate(mask_series.axes):
            if mask_series.shape[i] == 1:  # pragma: no cover
                continue
            try:
                j = image_series.axes.index(pos)
            except ValueError:
                raise ValueError("Incompatible shape of mask and image (axes)")
                # TODO add verification if problem with T/Z/I
            if image_series.shape[j] != mask_series.shape[i]:
                raise ValueError("Incompatible shape of mask and image")
            # TODO Add verification if mask have to few dimensions

    @staticmethod
    def decode_int(val: int):
        """
        This function split 32 bits int on 4 8-bits ints

        :param val: value to decode
        :return: list of four numbers with values from [0, 255]
        """
        return [(val >> x) & 255 for x in [24, 16, 8, 0]]

    def read_resolution_from_tags(self):
        tags = self.image_file.pages[0].tags
        try:

            if self.image_file.is_imagej:
                scalar = name_to_scalar[
                    self.image_file.imagej_metadata["unit"]]
            else:
                unit = tags["ResolutionUnit"].value
                if unit == 3:
                    scalar = name_to_scalar["centimeter"]
                elif unit == 2:
                    scalar = name_to_scalar["cal"]
                else:
                    raise KeyError(
                        f"wrong scalar {tags['ResolutionUnit']}, {tags['ResolutionUnit'].value}"
                    )

            x_spacing = tags["XResolution"].value[1] / tags[
                "XResolution"].value[0] * scalar
            y_spacing = tags["YResolution"].value[1] / tags[
                "YResolution"].value[0] * scalar
        except (KeyError, ZeroDivisionError):
            x_spacing, y_spacing = self.default_spacing[
                2], self.default_spacing[1]
        return x_spacing, y_spacing

    def read_imagej_metadata(self):
        try:
            z_spacing = (
                self.image_file.imagej_metadata["spacing"] *
                name_to_scalar[self.image_file.imagej_metadata["unit"]])
        except KeyError:
            z_spacing = self.default_spacing[0]
        x_spacing, y_spacing = self.read_resolution_from_tags()
        self.spacing = z_spacing, y_spacing, x_spacing
        self.colors = self.image_file.imagej_metadata.get("LUTs")
        self.labels = self.image_file.imagej_metadata.get("Labels")
        if "Ranges" in self.image_file.imagej_metadata:
            ranges = self.image_file.imagej_metadata["Ranges"]
            self.ranges = list(zip(ranges[::2], ranges[1::2]))

    def read_ome_metadata(self):
        if isinstance(self.image_file.ome_metadata, str):
            if hasattr(tifffile, "xml2dict"):
                meta_data = tifffile.xml2dict(
                    self.image_file.ome_metadata)["OME"]["Image"]["Pixels"]
            else:
                return
        else:
            meta_data = self.image_file.ome_metadata["Image"]["Pixels"]
        try:
            self.spacing = [
                meta_data[f"PhysicalSize{x}"] *
                name_to_scalar[meta_data[f"PhysicalSize{x}Unit"]]
                for x in ["Z", "Y", "X"]
            ]
        except KeyError:  # pragma: no cover
            pass
        if "Channel" in meta_data and isinstance(meta_data["Channel"],
                                                 (list, tuple)):
            try:
                self.labels = [ch["Name"] for ch in meta_data["Channel"]]
            except KeyError:
                pass
            try:
                self.colors = [
                    self.decode_int(ch["Color"])[:-1]
                    for ch in meta_data["Channel"]
                ]
            except KeyError:
                pass

    def read_lsm_metadata(self):
        self.spacing = [
            self.image_file.lsm_metadata[f"VoxelSize{x}"]
            for x in ["Z", "Y", "X"]
        ]
        if "ChannelColors" in self.image_file.lsm_metadata:
            if "Colors" in self.image_file.lsm_metadata["ChannelColors"]:
                self.colors = [
                    x[:3] for x in
                    self.image_file.lsm_metadata["ChannelColors"]["Colors"]
                ]
            if "ColorNames" in self.image_file.lsm_metadata["ChannelColors"]:
                self.labels = self.image_file.lsm_metadata["ChannelColors"][
                    "ColorNames"]