Skip to content

add pandas df conversion#5622

Merged
jpfeuffer merged 24 commits into
OpenMS:developfrom
axelwalter:feature/pandas-dataframes
Nov 9, 2021
Merged

add pandas df conversion#5622
jpfeuffer merged 24 commits into
OpenMS:developfrom
axelwalter:feature/pandas-dataframes

Conversation

@axelwalter

Copy link
Copy Markdown
Collaborator

Description

Add conversion for FeatureMap, ConsensusMap, MSExperiment and list of PeptideIdentification.

Checklist:

  • Make sure that you are listed in the AUTHORS file
  • Add relevant changes and new features to the CHANGELOG file
  • I have commented my code, particularly in hard-to-understand areas
  • New and existing unit tests pass locally with my changes
  • Updated or added python bindings for changed or new classes. (Tick if no updates were necessary.)

How can I get additional information on failed tests during CI:

If your PR is failing you can check out

Note:

  • Once you opened a PR try to minimize the number of pushes to it as every push will trigger CI (automated builds and test) and is rather heavy on our infrastructure (e.g., if several pushes per day are performed).

@jpfeuffer jpfeuffer added the NoJenkins Skips Jenkins tests for PRs label Oct 28, 2021

@jpfeuffer jpfeuffer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! A few changes would be nice.
And have a look at CodeFactor.

Comment thread src/pyOpenMS/pyopenms/python_extras.py Outdated
from . import Plotting as Plot

try:
from .Dataframes import ConsensusMap, FeatureMap, MSExperiment, DFConverter

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I don't think we should import this subpackage by default.

from .python_extras import *

IMHO users should do from pyopenms import Dataframes if they want that functionality, and only then it should try to import pandas.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea though if we would need to hide core classes under a separate subnamespace (E.g. core) or what happens if you do from pyopenms import *.
Needs to be tested.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also namespaces should be lowercase? So you don't confuse with classes?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I don't think we should import this subpackage by default.

from .python_extras import *

IMHO users should do from pyopenms import Dataframes if they want that functionality, and only then it should try to import pandas.

Hmm what would be the downside if we make pandas a requirement for pyOpenMS and by default include the Dataframes as Axel did?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kind of would like to make this as easy as possible for the user

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Downside is that people always need pandas.
And it would need to go into the setup.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am in the camp of "batteries included" and we already require numpy, so I think requiring pandas is not that big of a step.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Fine with me. I guess then we can leave those imports and just need to add it to setup.py.
And install it on the build slaves.

Comment thread src/pyOpenMS/pyopenms/Dataframes.py Outdated
Comment thread src/pyOpenMS/pyopenms/Dataframes.py Outdated
b'score_fit': 'f',
b'score_correlation': 'f',
b'FWHM': 'f',
b'spectrum_native_id': 'U30',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think that's enough?

Comment thread src/pyOpenMS/pyopenms/Dataframes.py Outdated
@hroest
hroest self-requested a review October 28, 2021 15:07

@hroest hroest left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add tests for this new functionality?

@jpfeuffer

jpfeuffer commented Oct 28, 2021

Copy link
Copy Markdown
Contributor

@hroest Regarding tests, when should we load pandas? What is your opinion?

E.g.:

  • check during cmake already?
  • check in setup.py?
  • check in nose/test/script?

@hroest

hroest commented Oct 28, 2021

Copy link
Copy Markdown
Contributor

If its required to run the tests, then I would say we should check in cmake.

@timosachsenberg

Copy link
Copy Markdown
Contributor

What is your opinion if someone takes a shortcut and does testing via the jupyter notebooks? At least I had the impression that people are more willing to write tests by creating an example in the readthedocs page. It now get's automatically tested. i.e., we get a good test coverage. Of course, the "right thing to do" is to do unit testing in the OpenMS repo.

@hroest

hroest commented Oct 29, 2021

Copy link
Copy Markdown
Contributor

What is your opinion if someone takes a shortcut and does testing via the jupyter notebooks?

do we check that these still work with every change in OpenMS? I see that happening with the documentation that requires updating for a new release...

@timosachsenberg

Copy link
Copy Markdown
Contributor

No I think Julianus added that it only tests the changed notebooks. Initially it tested all of manually triggered or at least one notebook changed

@timosachsenberg

Copy link
Copy Markdown
Contributor

@jpfeuffer would or make sense to regularly test all e.g. nightly?

@jpfeuffer

jpfeuffer commented Oct 29, 2021

Copy link
Copy Markdown
Contributor

Sure, just send a workflow dispatch trigger from the pyopenms builds to the "push action" of the binder branch.
https://github.community/t/triggering-by-other-repository/16163

@timosachsenberg

Copy link
Copy Markdown
Contributor

Sure, just send a workflow dispatch trigger from the pyopenms builds to the "push action" of the binder branch. https://github.community/t/triggering-by-other-repository/16163

and create a new action for considering all files?

@jpfeuffer

Copy link
Copy Markdown
Contributor

It's there already. Just never gets triggered. See issues.

@jpfeuffer

Copy link
Copy Markdown
Contributor

But to be honest, since you have to add pandas in OpenMS now anyway, we can also just add a one liner to the tests to at least see that it works.
Maybe test the length of the dataframe.

@timosachsenberg

Copy link
Copy Markdown
Contributor

so how would that look like:
OpenMS get'a push -> trigger pyopenms and whl need to be build via pyopenms-extra? -> trigger the test action

@jpfeuffer

Copy link
Copy Markdown
Contributor

Yes but currently pyopenms only runs on pushes to nightly.

@timosachsenberg

Copy link
Copy Markdown
Contributor

But to be honest, since you have to add pandas in OpenMS now anyway, we can also just add a one liner to the tests to at least see that it works. Maybe test the length of the dataframe.

sure it is the better way to do it. still would be great to include the new documentation and example notebooks into the testing (e.g. only nightly)

@jpfeuffer

Copy link
Copy Markdown
Contributor

Problem is that pyopenms builds take 3.5 hours.

@jpfeuffer

Copy link
Copy Markdown
Contributor

Hmm wasn't enough.

@jpfeuffer

Copy link
Copy Markdown
Contributor

Feel free to add tests now. @timosachsenberg @axelwalter

@timosachsenberg

Copy link
Copy Markdown
Contributor

I think we can add code from the pyOpenMS examples on the readthedocs page here that also tests writing and reading of files as well as conversion of the resulting maps to pandas:

for featureXML
https://github.com/OpenMS/OpenMS/blob/develop/src/pyOpenMS/tests/unittests/test000.py#L2098
for consensusXML
https://github.com/OpenMS/OpenMS/blob/develop/src/pyOpenMS/tests/unittests/test000.py#L873
for mzML file
https://github.com/OpenMS/OpenMS/blob/develop/src/pyOpenMS/tests/unittests/test000.py#L3662
and idXML:
https://github.com/OpenMS/OpenMS/blob/develop/src/pyOpenMS/tests/unittests/test000.py#L2209

@axelwalter

Copy link
Copy Markdown
Collaborator Author

I will work on a readthedocs tutorial and add the examples as test cases.

@jpfeuffer

Copy link
Copy Markdown
Contributor

Hmm but wouldn't it be better to have it merged first with a very basic test?

@timosachsenberg

Copy link
Copy Markdown
Contributor

yep I agree.e.g. just adding one or two features and then test the df shape

@jpfeuffer

Copy link
Copy Markdown
Contributor

I added quite some Todos that should be addressed in the future, in addition to an idXML test. But from my side we could merge it after CI.

@timosachsenberg timosachsenberg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to merge if tests pass

@axelwalter

Copy link
Copy Markdown
Collaborator Author

Still need to fix handling missing values in meta values.

switchDict = {bool: '?', int: 'i', float: 'f', str: 'U100'}

# filter out PeptideIdentifications without PeptideHits if export_unidentified == False
if not export_unidentified:

@jpfeuffer jpfeuffer Nov 9, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking my suggestion, however, I would do it during the main iteration.
In C++ this would be fine, but in python this will do copies of each PeptideID.
I make a comment on where I would add it.

EDIT: Ah you might need the count, right? Maybe just use this pre-iteration for the count then.
EDIT2: Hmm maybe does not matter. Since we do not have a count function, we need to copy anyway.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First I put it in the main iteration (line 302), however, returning None (when there are no peptide hits and export_unidentified == False) does not work with np.fromiter(). Therefore I filtered them out in advance.

@jpfeuffer jpfeuffer Nov 9, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, I would just "continue" the loop without yielding, but I think we can leave it for now. Maybe add a TODO.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I might have gotten it wrong. Yes we would need to think about it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Is it possible to continue here since we pass only one PeptideIdentification at a time and it is not really a loop?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked and yes you can just do "return" without argument.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a commit

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if the iteration for counting is worth it, but we have to benchmark.

@jpfeuffer
jpfeuffer merged commit 84d15f1 into OpenMS:develop Nov 9, 2021
@timosachsenberg

Copy link
Copy Markdown
Contributor

woohhoo!

@timosachsenberg

Copy link
Copy Markdown
Contributor

I think peptide evidences are not exported (e.g, the protein accessions and start, end in the protein)

@axelwalter
axelwalter deleted the feature/pandas-dataframes branch March 17, 2022 08:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

NoJenkins Skips Jenkins tests for PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants