Example #1
0
    def read_spectra(self):
        ai = agilentImage(self.filename)
        info = ai.info
        X = ai.data

        try:
            features = info['wavenumbers']
        except KeyError:
            #just start counting from 0 when nothing is known
            features = np.arange(X.shape[-1])

        try:
            px_size = info['FPA Pixel Size'] * info['PixelAggregationSize']
        except KeyError:
            # Use pixel units if FPA Pixel Size is not known
            px_size = 1
        x_locs = np.linspace(0,
                             X.shape[1] * px_size,
                             num=X.shape[1],
                             endpoint=False)
        y_locs = np.linspace(0,
                             X.shape[0] * px_size,
                             num=X.shape[0],
                             endpoint=False)

        return _spectra_from_image(X, features, x_locs, y_locs)
Example #2
0
    def read_spectra(self):
        om = OmnicMap.OmnicMap(self.filename)
        info = om.info
        X = om.data

        try:
            lv = info['OmnicInfo']['Last X value']
            fv = info['OmnicInfo']['First X value']
            features = np.linspace(fv, lv, num=X.shape[-1])
        except KeyError:
            #just start counting from 0 when nothing is known
            features = np.arange(X.shape[-1])

        try:
            loc_first = info['OmnicInfo']["First map location"]
            loc_last = info['OmnicInfo']["Last map location"]
            x_locs = np.linspace(min(loc_first[0], loc_last[0]),
                                 max(loc_first[0], loc_last[0]), X.shape[1])
            y_locs = np.linspace(min(loc_first[1], loc_last[1]),
                                 max(loc_first[1], loc_last[1]), X.shape[0])
        except KeyError:
            x_locs = np.arange(X.shape[1])
            y_locs = np.arange(X.shape[0])

        return _spectra_from_image(X, features, x_locs, y_locs)
Example #3
0
    def read_spectra(self):

        import h5py as h5

        if self.sheet:
            cube_nb = int(self.sheet)
        else:
            cube_nb = 1

        with h5.File(self.filename, "r") as dataf:
            cube_h5 = dataf["data/cube_{:0>5d}".format(cube_nb)]

            # directly read into float64 so that Orange.data.Table does not
            # convert to float64 afterwards (if we would not read into float64,
            # the memory use would be 50% greater)
            cube_np = np.empty(cube_h5.shape, dtype=np.float64)
            cube_h5.read_direct(cube_np)

            energies = np.array(dataf['context/energies'])

        intensities = np.transpose(cube_np, (1, 2, 0))
        height, width, _ = np.shape(intensities)
        x_locs = np.arange(width)
        y_locs = np.arange(height)

        return _spectra_from_image(intensities, energies, x_locs, y_locs)
def process_stack(data,
                  xat,
                  yat,
                  upsample_factor=100,
                  use_sobel=False,
                  ref_frame_num=0):
    hypercube, lsx, lsy = get_hypercube(data, xat, yat)
    if bn.anynan(hypercube):
        raise NanInsideHypercube(True)

    calculate_shift = RegisterTranslation(upsample_factor=upsample_factor)
    filterfn = sobel if use_sobel else lambda x: x
    shifts, aligned_stack = alignstack(hypercube.T,
                                       shiftfn=calculate_shift,
                                       ref_frame_num=ref_frame_num,
                                       filterfn=filterfn)

    xmin, ymin = shifts[:, 0].min(), shifts[:, 1].min()
    xmax, ymax = shifts[:, 0].max(), shifts[:, 1].max()
    xmin, xmax = int(round(xmin)), int(round(xmax))
    ymin, ymax = int(round(ymin)), int(round(ymax))

    shape = hypercube.shape
    slicex = slice(max(xmax, 0), min(shape[1], shape[1] + xmin))
    slicey = slice(max(ymax, 0), min(shape[0], shape[0] + ymin))
    cropped = np.array(aligned_stack).T[slicey, slicex]

    # transform numpy array back to Orange.data.Table
    return shifts, build_spec_table(
        *_spectra_from_image(cropped, getx(data),
                             np.linspace(*lsx)[slicex],
                             np.linspace(*lsy)[slicey]))
Example #5
0
    def read_spectra(self):
        am = agilentMosaic(self.filename, dtype=np.float64)
        info = am.info
        X = am.data
        visible_images = am.vis

        try:
            features = info['wavenumbers']
        except KeyError:
            #just start counting from 0 when nothing is known
            features = np.arange(X.shape[-1])

        try:
            px_size = info['FPA Pixel Size'] * info['PixelAggregationSize']
        except KeyError:
            # Use pixel units if FPA Pixel Size is not known
            px_size = 1
        x_locs = np.linspace(0,
                             X.shape[1] * px_size,
                             num=X.shape[1],
                             endpoint=False)
        y_locs = np.linspace(0,
                             X.shape[0] * px_size,
                             num=X.shape[0],
                             endpoint=False)

        features, data, additional_table = _spectra_from_image(
            X, features, x_locs, y_locs)

        if visible_images:
            additional_table.attributes['visible_images'] = visible_images
        return features, data, additional_table
Example #6
0
 def read_spectra(self):
     import h5py
     hdf5_file = h5py.File(self.filename)
     if 'entry1/collection/beamline' in hdf5_file and \
             hdf5_file['entry1/collection/beamline'][()].astype('str') == 'Hermes':
         x_locs = np.array(hdf5_file['entry1/Counter0/sample_x'])
         y_locs = np.array(hdf5_file['entry1/Counter0/sample_y'])
         energy = np.array(hdf5_file['entry1/Counter0/energy'])
         intensities = np.array(hdf5_file['entry1/Counter0/data']).T
         return _spectra_from_image(intensities, energy, x_locs, y_locs)
     else:
         raise IOError("Not an HDF5 HERMES file")
Example #7
0
    def read_spectra(self):
        a = spectral.io.envi.open(self.filename)
        X = np.array(a.load())
        try:
            lv = a.metadata["wavelength"]
            features = np.array(list(map(float, lv)))
        except KeyError:
            #just start counting from 0 when nothing is known
            features = np.arange(X.shape[-1])

        x_locs = np.arange(X.shape[1])
        y_locs = np.arange(X.shape[0])

        return _spectra_from_image(X, features, x_locs, y_locs)
Example #8
0
    def read_spectra(self):
        ai = agilentImageIFG(self.filename)
        info = ai.info
        X = ai.data

        features = np.arange(X.shape[-1])

        try:
            px_size = info['FPA Pixel Size'] * info['PixelAggregationSize']
        except KeyError:
            # Use pixel units if FPA Pixel Size is not known
            px_size = 1
        x_locs = np.linspace(0,
                             X.shape[1] * px_size,
                             num=X.shape[1],
                             endpoint=False)
        y_locs = np.linspace(0,
                             X.shape[0] * px_size,
                             num=X.shape[0],
                             endpoint=False)

        features, data, additional_table = _spectra_from_image(
            X, features, x_locs, y_locs)

        import_params = [
            'Effective Laser Wavenumber',
            'Under Sampling Ratio',
        ]
        new_attributes = []
        new_columns = []
        for param_key in import_params:
            try:
                param = info[param_key]
            except KeyError:
                pass
            else:
                new_attributes.append(ContinuousVariable.make(param_key))
                new_columns.append(np.full((len(data), ), param))

        domain = Domain(additional_table.domain.attributes,
                        additional_table.domain.class_vars,
                        additional_table.domain.metas + tuple(new_attributes))
        table = additional_table.transform(domain)
        with table.unlocked():
            table[:, new_attributes] = np.asarray(new_columns).T

        return (features, data, table)
Example #9
0
 def read_spectra(self):
     import h5py
     hdf5_file = h5py.File(self.filename, mode='r')
     if 'entry1/definition' in hdf5_file and \
             hdf5_file['entry1/definition'][()].astype('str') == 'NXstxm':
         grp = hdf5_file['entry1/Counter1']
         x_locs = np.array(grp['sample_x'])
         y_locs = np.array(grp['sample_y'])
         energy = np.array(grp['photon_energy'])
         order = [
             grp[n].attrs['axis'] - 1
             for n in ['sample_y', 'sample_x', 'photon_energy']
         ]
         intensities = np.array(grp['data']).transpose(order)
         return _spectra_from_image(intensities, energy, x_locs, y_locs)
     else:
         raise IOError("Not an NXS HDF5 @I08/Diamond file")
Example #10
0
    def read_tile(self):
        am = agilentMosaicTiles(self.filename)
        info = am.info
        tiles = am.tiles
        ytiles = am.tiles.shape[0]

        features = info['wavenumbers']

        attrs = [
            Orange.data.ContinuousVariable.make("%f" % f) for f in features
        ]
        domain = Orange.data.Domain(
            attrs,
            None,
            metas=[
                Orange.data.ContinuousVariable.make("map_x"),
                Orange.data.ContinuousVariable.make("map_y")
            ])

        try:
            px_size = info['FPA Pixel Size'] * info['PixelAggregationSize']
        except KeyError:
            # Use pixel units if FPA Pixel Size is not known
            px_size = 1

        for (x, y) in np.ndindex(tiles.shape):
            tile = tiles[x, y]()
            x_size, y_size = tile.shape[1], tile.shape[0]
            x_locs = np.linspace(x * x_size * px_size,
                                 (x + 1) * x_size * px_size,
                                 num=x_size,
                                 endpoint=False)
            y_locs = np.linspace((ytiles - y - 1) * y_size * px_size,
                                 (ytiles - y) * y_size * px_size,
                                 num=y_size,
                                 endpoint=False)

            _, data, additional_table = _spectra_from_image(
                tile, None, x_locs, y_locs)
            data = np.asarray(
                data, dtype=np.float64)  # Orange assumes X to be float64
            tile_table = Orange.data.Table.from_numpy(
                domain, X=data, metas=additional_table.metas)
            yield tile_table
Example #11
0
    def test_hypercube_roundtrip(self):
        d = self.mosaic
        xat = [v for v in d.domain.metas if v.name == "map_x"][0]
        yat = [v for v in d.domain.metas if v.name == "map_y"][0]
        hypercube, lsx, lsy = get_hypercube(d, xat, yat)

        features = getx(d)
        ndom = Orange.data.Domain([xat, yat])
        datam = d.transform(ndom)
        coorx = datam.X[:, 0]
        coory = datam.X[:, 1]
        coords = np.ones((lsx[2], lsy[2], 2))
        coords[index_values(coorx, lsx), index_values(coory, lsy)] = datam.X
        x_locs = coords[:, 0, 0]
        y_locs = coords[0, :, 1]

        features, spectra, data = _spectra_from_image(hypercube, features,
                                                      x_locs, y_locs)
        nd = build_spec_table(features, spectra, data)

        np.testing.assert_equal(d.X, nd.X)
        np.testing.assert_equal(d.Y, nd.Y)
        np.testing.assert_equal(d.metas, nd.metas)
        self.assertEqual(d.domain, nd.domain)
Example #12
0
    def read_spectra(self):
        with open(self.filename, 'r') as f:
            # Parse file contents into dictionaries/lists
            self._lex = shlex.shlex(instream=f)
            try:
                hdrdata = self.read_hdr_dict(inner=False)
            except AssertionError as e:
                raise IOError('Error parsing hdr file ' + self.filename) from e
        regions = hdrdata['ScanDefinition']['Regions'][0]
        axes = [
            regions['QAxis'], regions['PAxis'],
            hdrdata['ScanDefinition']['StackAxis']
        ]
        dims = [len(ax['Points']) for ax in axes]
        spectra = np.empty(dims)
        for nf in range(dims[2]):
            ximname = '%s_a%03d.xim' % (self.filename[:-4], nf)
            xim = np.loadtxt(ximname)
            spectra[..., nf] = xim

        x_loc = axes[1]['Points']
        y_loc = axes[0]['Points']
        features = np.asarray(axes[2]['Points'])
        return _spectra_from_image(spectra, features, x_loc, y_loc)
def orange_table_from_3d(image3d):
    info = _spectra_from_image(image3d, range(5),
                               range(image3d[:, :, 0].shape[1]),
                               range(image3d[:, :, 0].shape[0]))
    data = build_spec_table(*info)
    return data
Example #14
0
 def read_spectra(self):
     X, XRr, YRr = reader_gsf(self.filename)
     data = _spectra_from_image(X, [1], XRr, YRr)
     return data
Example #15
0
    def read_spectra(self):
        channels = self.get_channels()

        for c, label in channels.items():
            if label.decode("utf-8") == self.sheet:
                self.data_signal = c
                break
        else:
            self.data_signal = list(channels.keys())[0]

        import h5py
        hdf5_file = h5py.File(self.filename, 'r')
        keys = list(hdf5_file.keys())

        hyperspectra = False
        intensities = []
        wavenumbers = []
        x_locs = []
        y_locs = []

        # load measurements
        for meas_name in filter(lambda s: s.startswith('Measurement'), keys):
            hdf5_meas = hdf5_file[meas_name]

            meas_keys = list(hdf5_meas.keys())
            meas_attrs = hdf5_meas.attrs

            # check if this measurement contains the selected data channel
            selected_signal = False
            for chan_name in filter(lambda s: s.startswith('Channel'),
                                    meas_keys):
                hdf5_chan = hdf5_meas[chan_name]
                if hdf5_chan.attrs.keys().__contains__(
                        'DataSignal'
                ) and hdf5_chan.attrs['DataSignal'] == self.data_signal:
                    selected_signal = True
                    break
            if not selected_signal:
                continue

            # build range arrays
            spec_vals = []
            try:
                if meas_attrs.keys().__contains__('RangeWavenumberStart'):
                    wn_start = meas_attrs['RangeWavenumberStart'][0]
                    wn_end = meas_attrs['RangeWavenumberEnd'][0]
                    wn_points = meas_attrs['RangeWavenumberPoints'][0]
                    spec_vals = np.linspace(wn_start, wn_end, wn_points)
            except:
                raise IOError("Error reading wavenumber range from " +
                              self.filename)

            pos_vals = []
            try:
                if meas_attrs.keys().__contains__('RangeXStart'):
                    x_start = meas_attrs['RangeXStart'][0]
                    x_points = meas_attrs['RangeXPoints'][0]
                    x_incr = meas_attrs['RangeXIncrement'][0]
                    x_end = x_start + x_incr * (x_points - 1)
                    x_min = min(x_start, x_end)
                    if meas_attrs.keys().__contains__('RangeYStart'):
                        y_start = meas_attrs['RangeYStart'][0]
                        y_points = meas_attrs['RangeYPoints'][0]
                        y_incr = meas_attrs['RangeYIncrement'][0]
                        y_end = y_start + y_incr * (y_points - 1)
                        y_min = min(y_start, y_end)

                        # construct the positions array
                        for iY in range(int(y_points)):
                            y = y_min + iY * abs(y_incr)
                            for iX in range(int(x_points)):
                                x = x_min + iX * abs(x_incr)
                                pos_vals.append([x, y])
                        pos_vals = np.array(pos_vals)
                else:
                    pos_vals = np.array([1])
            except:
                raise IOError("Error reading position data from " +
                              self.filename)

            hyperspectra = pos_vals.shape[0] > 1

            # ignore backgrounds and unchecked data
            if not hyperspectra:
                if meas_attrs.keys().__contains__(
                        'IsBackground') and meas_attrs['IsBackground'][0]:
                    continue
                if meas_attrs.keys().__contains__(
                        'Checked') and not meas_attrs['Checked'][0]:
                    continue

            if len(wavenumbers) == 0:
                wavenumbers = spec_vals

            if hyperspectra:
                x_len = meas_attrs['RangeXPoints'][0]
                y_len = meas_attrs['RangeYPoints'][0]
                x_locs = pos_vals[:x_len, 0]
                y_indices = np.round(
                    np.linspace(0, pos_vals.shape[0] - 1, y_len)).astype(int)
                y_locs = pos_vals[y_indices, 1]
            else:
                x_locs.append(meas_attrs['LocationX'][0])
                y_locs.append(meas_attrs['LocationY'][0])

            # load channels
            for chan_name in filter(lambda s: s.startswith('Channel'),
                                    meas_keys):
                hdf5_chan = hdf5_meas[chan_name]
                chan_attrs = hdf5_chan.attrs

                signal = chan_attrs['DataSignal']
                if signal != self.data_signal:
                    continue

                data = hdf5_chan['Raw_Data']
                if hyperspectra:
                    rows = meas_attrs['RangeYPoints'][0]
                    cols = meas_attrs['RangeXPoints'][0]
                    intensities = np.reshape(data, (
                        rows, cols,
                        data.shape[1]))  # organized rows, columns, wavelengths
                    break
                else:
                    intensities.append(data[0, :])

        intensities = np.array(intensities)
        features = np.array(wavenumbers)
        x_locs = np.array(x_locs).flatten()
        y_locs = np.array(y_locs).flatten()
        if hyperspectra:
            return _spectra_from_image(intensities, features, x_locs, y_locs)
        else:
            spectra = intensities

            # locations
            x_loc = y_loc = np.arange(spectra.shape[0])
            metas = np.array([x_locs[x_loc], y_locs[y_loc]]).T

            domain = Orange.data.Domain(
                [],
                None,
                metas=[
                    Orange.data.ContinuousVariable.make("map_x"),
                    Orange.data.ContinuousVariable.make("map_y")
                ])
            data = Orange.data.Table.from_numpy(domain,
                                                X=np.zeros((len(spectra), 0)),
                                                metas=np.asarray(metas,
                                                                 dtype=object))
            return features, spectra, data