Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getionimage performance issue for multiple mz values #29

Open
leorrose opened this issue Jan 1, 2022 · 1 comment
Open

getionimage performance issue for multiple mz values #29

leorrose opened this issue Jan 1, 2022 · 1 comment
Assignees

Comments

@leorrose
Copy link

leorrose commented Jan 1, 2022

I want to get ion image for multiple mz value (794.5, 834.5, 888.6) and not one mz value.

Using the existing getionimage method i need to do the following:

img_1= getionimage(p, 794.5, tol=0.5)
img_2 = getionimage(p, 834.5, tol=0.5)
img_3 = getionimage(p, 888.6, tol=0.5)

im_combined = img_1 + img_2 + img_3 

This works but the issue is that it takes a lot of time because we go over the imzML file 3 times.

Is there a better solution to get ion image for multiple mz values?

@leorrose
Copy link
Author

leorrose commented Jan 1, 2022

I found that this can be achieved by changing the geionimage to accept multiple mz values like this:

def getionimage(p, mz_values, tol=0.1, z=1, reduce_func=sum):
    """
    Get an image representation of the intensity distribution
    of the ion with specified m/z values.

    By default, the intensity values within the tolerance region are summed.

    :param p:
        the ImzMLParser (or anything else with similar attributes) for the desired dataset
    :param mz_values:
        m/z values for which the ion image shall be returned
    :param tol:
        Absolute tolerance for the m/z value, such that all ions with values
        mz_value-|tol| <= x <= mz_value+|tol| are included. Defaults to 0.1
    :param z:
        z Value if spectrogram is 3-dimensional.
    :param reduce_func:
        the bahaviour for reducing the intensities between mz_value-|tol| and mz_value+|tol| to a single value. Must
        be a function that takes a sequence as input and outputs a number. By default, the values are summed.

    :return:
        numpy matrix with each element representing the ion intensity in this
        pixel. Can be easily plotted with matplotlib
    """
    tol = abs(tol)
    im = np.zeros((p.imzmldict["max count of pixels y"], p.imzmldict["max count of pixels x"]))
    for i, (x, y, z_) in enumerate(p.coordinates):
        if z_ == 0:
            UserWarning("z coordinate = 0 present, if you're getting blank images set getionimage(.., .., z=0)")
        if z_ == z:
            mzs, ints = map(lambda x: np.asarray(x), p.getspectrum(i))
            partial_ints = []
            for mz_value in mz_values:
                min_i, max_i = _bisect_spectrum(mzs, mz_value, tol)
                partial_ints.append(ints[min_i:max_i + 1])
            im[y - 1, x - 1] = reduce_func(np.concatenate(partial_ints))
    return im

I added a pull request containing these changes if someone wants to check this out enable multiple MZ values in getionimage

@sergii-mamedov sergii-mamedov self-assigned this Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants