Example #1
0
def to_image_array_3D(image):
    """
    Extracts all existing images from 3D imzML

    Parameters
    ----------
    image: imzmlparser.ImzMLParser
        parser

    Returns
    ----------
    np.ndarray
        image array
    """
    x, y = image.getspectrum(0)
    image_list = []
    min_z = min(image.coordinates, key=lambda item: item[2])[2]
    max_z = max(image.coordinates, key=lambda item: item[2])[2]
    for mz in x:
        images_along_z = []
        for i in range(min_z, max_z + 1):
            im = imzmlparser.getionimage(image, mz, tol=0.01, z=i)
            images_along_z.append(im)
        image_list.append(images_along_z)
    img_array = np.transpose(np.asarray(image_list))
    return img_array
Example #2
0
def readImzML(filename):
    f_in = ImzMLParser.ImzMLParser(filename)
    for i, coords in enumerate(f_in.coordinates):
        mzs, ints = prepare(*f_in.getspectrum(i))
        if len(coords) == 2:
            coords = (coords[0], coords[1], 0)
        yield Spectrum(i, mzs, ints, map(lambda x: x - 1, coords))
Example #3
0
    def test_parse_partial_spectrum_metadata(self):
        TIC, POS_X, EXT_LEN, INVALID = 'MS:1000285', 'IMS:1000050', 'IMS:1000104', 'INVALID'
        ACCESSIONS = [TIC, POS_X, EXT_LEN, INVALID]
        for parse_lib, data_name, imzml_path, ibd_path in ALL_TEST_CASES:
            with self.subTest(parse_lib=parse_lib, data=data_name),\
                 imzmlp.ImzMLParser(imzml_path, parse_lib=parse_lib, include_spectra_metadata=ACCESSIONS) as parser:

                assert len(parser.spectrum_metadata_fields[TIC]) == len(
                    parser.coordinates)
                assert len(parser.spectrum_metadata_fields[POS_X]) == len(
                    parser.coordinates)
                assert len(parser.spectrum_metadata_fields[EXT_LEN]) == len(
                    parser.coordinates)
                assert len(parser.spectrum_metadata_fields[INVALID]) == len(
                    parser.coordinates)

                assert all(tic > 100
                           for tic in parser.spectrum_metadata_fields[TIC])
                assert all(
                    isinstance(pos_x, int)
                    for pos_x in parser.spectrum_metadata_fields[POS_X])
                assert all(
                    isinstance(ext_len, int)
                    for ext_len in parser.spectrum_metadata_fields[EXT_LEN])
                assert all(
                    invalid is None
                    for invalid in parser.spectrum_metadata_fields[INVALID])
Example #4
0
    def test_read_file(self):
        spectrum_idx = 4
        for parse_lib, data_name, imzml_path, ibd_path in ALL_TEST_CASES:
            with self.subTest(parse_lib=parse_lib, data=data_name),\
                 imzmlp.ImzMLParser(imzml_path, parse_lib=parse_lib) as normal_parser,\
                 open(ibd_path, 'rb') as ibd_file:

                normal_mzs, normal_ints = normal_parser.getspectrum(spectrum_idx)

                detached_parser = imzmlp.ImzMLParser(imzml_path, parse_lib=parse_lib, ibd_file=None)
                portable_reader = detached_parser.portable_spectrum_reader()
                # Pickle and unpickle to ensure it survives for its intended use case
                portable_reader = pickle.loads(pickle.dumps(portable_reader))
                portable_mzs, portable_ints = portable_reader.read_spectrum_from_file(ibd_file, spectrum_idx)

                assert np.all(normal_mzs == portable_mzs)
                assert np.all(normal_ints == portable_ints)
Example #5
0
 def test_bisect(self):
     mzs = [100., 201.89, 201.99, 202.0, 202.01, 202.10000001, 400.]
     test_mz = 202.0
     test_tol = 0.1
     ix_l, ix_u = imzmlp._bisect_spectrum(mzs, test_mz, test_tol)
     assert ix_l == 2
     assert ix_u == 4
     assert ix_l <= ix_u
     assert mzs[ix_l] >= test_mz - test_tol
     assert mzs[ix_u] <= test_mz + test_tol
Example #6
0
    def test_files_instead_of_paths(self):
        for parse_lib, data_name, imzml_path, ibd_path in ALL_TEST_CASES:
            with self.subTest(parse_lib=parse_lib, data=data_name),\
                 open(imzml_path, 'rb') as imzml_file,\
                 open(ibd_path, 'rb') as ibd_file,\
                 imzmlp.ImzMLParser(imzml_file, parse_lib=parse_lib, ibd_file=ibd_file) as parser:

                mzs, ints = parser.getspectrum(4)

                assert len(parser.coordinates) == 9
                assert len(mzs) > 0
                assert len(ints) > 0
Example #7
0
    def test_parse_metadata(self):
        for parse_lib, data_name, imzml_path, ibd_path in ALL_TEST_CASES:
            with self.subTest(parse_lib=parse_lib, data=data_name),\
                 imzmlp.ImzMLParser(imzml_path, parse_lib=parse_lib) as parser:
                md = parser.metadata
                # fileDescription section
                assert md.file_description['MS:1000579'] == True
                assert 'ibd SHA-1' in md.file_description
                assert len(md.file_description.source_files) == 1
                assert md.file_description.source_files['sf1'][
                    'Thermo RAW format'] == True
                assert md.file_description.source_files['sf1'].attrs[
                    'name'] == 'Example.raw'
                assert len(md.file_description.contacts) == 1

                # referenceableParamGroupList section
                assert len(md.referenceable_param_groups) == 4
                assert md.referenceable_param_groups['scan1'][
                    'increasing m/z scan']

                # sampleList section
                assert len(md.samples) == 1
                assert md.samples['sample1']['sample number'] == '1'

                # softwareList section
                assert len(md.softwares) == 2
                assert md.softwares['Xcalibur']['Xcalibur']

                # scanSettingsList section
                assert len(md.scan_settings) == 1
                assert md.scan_settings['scansettings1'][
                    'pixel size (x)'] == 100.0

                # instrumentConfigurationList section
                assert len(md.instrument_configurations) == 1
                ic = md.instrument_configurations['LTQFTUltra0']
                assert ic.param_by_name['instrument serial number'] == 'none'
                assert len(ic.components) == 3
                assert ic.components[0].type == 'source'
                assert ic.components[1].type == 'analyzer'
                assert ic.components[2].type == 'detector'
                assert ic.software_ref == 'Xcalibur'

                # dataProcessingList section
                assert len(md.data_processings) == 2
                assert md.data_processings['XcaliburProcessing'].methods[
                    0].attrs['softwareRef'] == 'Xcalibur'
                assert md.data_processings['XcaliburProcessing'].methods[0][
                    'low intensity data point removal']
Example #8
0
def parse_imzml_index(imzml_filename):
    from pyimzml import ImzMLParser
    imzml = ImzMLParser.ImzMLParser(imzml_filename)
    imzml_idx = {
        'bin_filename': imzml.filename[:-5] + "ibd",
        'mzOffsets': imzml.mzOffsets,
        'mzLengths': imzml.mzLengths,
        'intensityOffsets': imzml.intensityOffsets,
        'intensityLengths': imzml.intensityLengths,
        'sizeDict': imzml.sizeDict,
        'mzPrecision': imzml.mzPrecision,
        'intensityPrecision': imzml.intensityPrecision,
        'coordinates': imzml.coordinates
    }
    return imzml_idx
Example #9
0
    def test_getspectrum(self):
        for parse_lib, data_name, imzml_path, ibd_path in ALL_TEST_CASES:
            with self.subTest(parse_lib=parse_lib, data=data_name),\
                 imzmlp.ImzMLParser(imzml_path, parse_lib=parse_lib) as parser:

                mzs, ints = parser.getspectrum(4)

                assert len(parser.coordinates) == 9
                assert mzs.dtype == np.float32
                assert ints.dtype == np.float32
                assert len(mzs) == 8399
                assert len(ints) == 8399
                assert np.all(mzs > 100.0)
                assert np.all(mzs < 800.0)
                assert np.all(ints >= 0.0)
                assert np.all(ints < 3.0)
Example #10
0
def open_imzml(filename):
    """
    Opens an imzML file

    Parameters
    ----------
    filename: string
        input file

    Returns
    ----------
    imzmlparser.ImzMLParser
        parser of file

    """
    return imzmlparser.ImzMLParser(filename)
Example #11
0
 def test_parse_full_spectrum_metadata(self):
     for parse_lib, data_name, imzml_path, ibd_path in ALL_TEST_CASES:
         with self.subTest(parse_lib=parse_lib, data=data_name),\
              imzmlp.ImzMLParser(imzml_path, parse_lib=parse_lib, include_spectra_metadata='full') as parser:
             assert len(parser.spectrum_full_metadata) == len(
                 parser.coordinates)
             spectrum = parser.spectrum_full_metadata[0]
             assert spectrum[
                 'ms level'] == 0  # comes from referenceable param group
             assert spectrum['total ion current'] > 100
             assert spectrum.scan_list_params['no combination']
             assert spectrum.scans[0].attrs[
                 'instrumentConfigurationRef'] == 'LTQFTUltra0'
             assert spectrum.scans[0]['position x'] == 1
             assert 'm/z array' in spectrum.binary_data_arrays[0]
             assert 'intensity array' in spectrum.binary_data_arrays[1]
Example #12
0
def get_image(imzml, mz, tol=0.01):
    """
    Extracts an ion image at a given m/z value
    and with a tolerance

    Wrapper function for imzmlparser.getionimage

    Parameters
    ----------
    imzml: imzmlparser.ImzMLParser
        parser
    mz: float
        m/z ratio of desired image
    tol: float
        tolerance on accepted m/z

    """
    return imzmlparser.getionimage(imzml, mz, tol)
Example #13
0
def generate_mean_spectrum(ds_id, minmz=100, maxmz=1000):
    info = get_ds_info(ds_id)
    inst = getattr(instrument,
                   info['instrument'])(resolving_power=info['resolving_power'],
                                       at_mz=info['at_mz'])
    hist_axis = inst.generate_mz_axis(minmz, maxmz)
    mz_axis = (hist_axis[0:-1] + hist_axis[1:]) / 2.
    # calculate mean along some m/z axis
    imzml = ImzMLParser.ImzMLParser(info['imzml'])
    mean_spec = np.zeros(np.shape(mz_axis))
    for ix, c in enumerate(imzml.coordinates):
        mzs, ints = map(np.asarray, imzml.getspectrum(ix))
        if any([minmz, maxmz]):
            ix_l = np.searchsorted(mzs, minmz)
            ix_u = np.searchsorted(mzs, maxmz)
            mzs = mzs[ix_l:ix_u]
            ints = mzs[ix_l:ix_u]
        mean_spec += np.bincount(np.digitize(mzs, hist_axis) - 1,
                                 weights=ints,
                                 minlength=len(mz_axis))
    mean_spec = mean_spec / (ix + 1.)
    return mz_axis, mean_spec
Example #14
0
def max_variance_sort(image_maldi):
    """
    Sort a stack image along the z-axis
    according to the maximum intensity variation

    Parameters
    ----------
    image_maldi: numpy.ndarray
        input image

    Returns
    ----------
    numpy.ndarray
        the sorted image stack

    """
    x, y = image_maldi.getspectrum(0)
    image_list = []
    for mz in x:
        im = imzmlparser.getionimage(image_maldi, mz, tol=0.1)
        image_list.append({"mz": mz, "im": im})
    image_list.sort(key=lambda elem: np.var(elem["im"]), reverse=True)
    return image_list