Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mobiusklein committed Nov 4, 2023
1 parent fa2a35b commit 7226550
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 70 deletions.
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
[build-system]
requires = ["setuptools",
"wheel",
"Cython"]
"Cython"]

[tool.ruff]
target-version = "py38"
line-length = 120
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def run_setup(include_cext=True):
packages=find_packages(where='src'),
package_dir={"": "src"},
description="Fast and efficient theoretical isotopic profile generation",
long_description='''
long_description="""
:mod:`brainpy` is a small Python library implementing the *B* afflingly *R* ecursive
*A* lgorithm for *I* sotopic Patter *N* generation [Dittwald2014]_. It includes three implementations,
a pure-Python object oriented implementation, a :title-reference:`Cython` accelerated
Expand Down Expand Up @@ -163,7 +163,7 @@ def run_setup(include_cext=True):
H. Hu, P. Dittwald, J. Zaia, and D. Valkenborg,
"Comment on 'Computation of isotopic peak center-mass distribution by fourier transform'.",
Anal. Chem., vol. 85, no. 24, pp. 12189–92, Dec. 2013.
''',
""",
long_description_content_type='text/markdown',
author=', '.join(["Joshua Klein", "Han Hu"]),
author_email="[email protected]",
Expand All @@ -190,7 +190,7 @@ def run_setup(include_cext=True):
if __name__ == '__main__':
try:
run_setup(True)
except Exception as exc:
except Exception:
run_setup(False)

status_msgs(
Expand Down
36 changes: 20 additions & 16 deletions src/brainpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'''
A Python Implementation of the Baffling Recursive Algorithm for Isotopic cluster distributioN
'''
"""
A Python Implementation of the
Baffling Recursive Algorithm for Isotopic cluster distributioN
"""
import os

from .brainpy import (isotopic_variants, IsotopicDistribution, periodic_table,
max_variants, calculate_mass, neutral_mass, mass_charge_ratio,
PROTON, _has_c, Peak)
PROTON, _has_c, Peak, max_variants_approx)

from .composition import parse_formula, PyComposition

Expand All @@ -14,20 +15,23 @@


def get_include():
"""Retrieve the path to compiled C extensions' source files to make linking simple.
This module contains two variants of the algorithm reimplimented using C and the Python-C API.
"""
Retrieve the path to compiled C extensions' source files to make linking simple.
The `_speedup` module is a direct translation of the pure Python implementation using Cython,
using static typing and `cdef class` versions of the existing classes. As this implementation still
spends a substantial amount of time in Python-space, it is slower than the option below, but is more
straight-forward to manipulate from Python.
This module contains two variants of the algorithm reimplimented using C and the
Python-C API.
The `_c` module is a complete rewrite of the algorithm directly in C, using Python only to load
mass configuration information and is fully usable. It exports an entrypoint function to Python
which replaces the :func:`isotopic_variants` function when available. Because almost all of the
action happens in C here, it's not possible to run individual parts of the process directly from
The `_speedup` module is a direct translation of the pure Python implementation
using Cython, using static typing and `cdef class` versions of the existing classes.
As this implementation still spends a substantial amount of time in Python-space,
it is slower than the option below, but is more straight-forward to manipulate from
Python.
The `_c` module is a complete rewrite of the algorithm directly in C, using Python
only to load mass configuration information and is fully usable. It exports an
entrypoint function to Python which replaces the :func:`isotopic_variants` function
when available. Because almost all of the action happens in C here, it's not
possible to run individual parts of the process directly from Python.
"""
return os.path.join(__path__[0], "_c")

Expand All @@ -41,7 +45,7 @@ def get_include():
__all__ = [
"isotopic_variants", "IsotopicDistribution", "periodic_table",
"max_variants", "calculate_mass", "neutral_mass", "mass_charge_ratio",
"PROTON", "_has_c", "Peak",
"PROTON", "_has_c", "Peak", "max_variants_approx",

"parse_formula", "PyComposition", "SimpleComposition",

Expand Down
46 changes: 24 additions & 22 deletions src/brainpy/_c/composition.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,9 @@ def show_element(str element):
# Composition Methods

cdef Composition* make_composition() nogil:
'''
"""
Create a new, empty Composition struct
'''
"""
cdef:
Composition* composition
composition = <Composition*>malloc(sizeof(Composition))
Expand All @@ -521,9 +521,9 @@ cdef Composition* make_composition() nogil:
return composition

cdef int composition_eq(Composition* composition_1, Composition* composition_2) nogil:
'''
"""
Test two Composition instances for element-wise equality
'''
"""
cdef:
int status
size_t i
Expand All @@ -548,10 +548,10 @@ cdef int composition_eq(Composition* composition_1, Composition* composition_2)
return 1

cdef Composition* copy_composition(Composition* composition) nogil:
'''
"""
Create a new Composition instance whose element counts are copied from
`composition`
'''
"""
cdef:
Composition* result
int status
Expand Down Expand Up @@ -600,14 +600,14 @@ cdef void print_composition(Composition* composition) nogil:
printf("}\n\n")

cdef int composition_set_element_count(Composition* composition, char* element, count_type count) nogil:
'''
"""
Set the count for `element` in `composition`
Return Values:
0: Success
1: General Failure
-1: Failure due to Out-of-Memory
'''
"""
cdef:
size_t i
int status
Expand All @@ -633,13 +633,13 @@ cdef int composition_set_element_count(Composition* composition, char* element,
return 1

cdef int composition_get_element_count(Composition* composition, char* element, count_type* count) nogil:
'''
"""
Get the count of `element` in `composition`. The count is 0 if `element` is not in `composition`
Return Values:
0: Success
1: Not Found
'''
"""
cdef:
size_t i
int status
Expand All @@ -658,14 +658,14 @@ cdef int composition_get_element_count(Composition* composition, char* element,
return 1

cdef int composition_inc_element_count(Composition* composition, char* element, count_type increment) nogil:
'''
"""
Increase the count for `element` in `composition` by `increment`.
Return Values:
0: Success
1: General Failure
-1: Failure due to Out-of-Memory
'''
"""
cdef:
size_t i
int status
Expand All @@ -691,13 +691,13 @@ cdef int composition_inc_element_count(Composition* composition, char* element,
return 1

cdef int composition_resize(Composition* composition) nogil:
'''
"""
Increases the size of the parallel arrays in `composition`, doubling them in length
Return Values:
0: Success
-1: Failure due to Out-of-Memory
'''
"""
composition.elements = <char**>realloc(composition.elements, sizeof(char*) * composition.size * 2)
composition.counts = <count_type*>realloc(composition.counts, sizeof(count_type) * composition.size * 2)
composition.size *= 2
Expand All @@ -706,13 +706,13 @@ cdef int composition_resize(Composition* composition) nogil:
return 0

cdef int composition_resize_to(Composition* composition, size_t size) nogil:
'''
"""
Increases the size of the parallel arrays in `composition`, doubling them in length
Return Values:
0: Success
-1: Failure due to Out-of-Memory
'''
"""
composition.elements = <char**>realloc(composition.elements, sizeof(char*) * size)
composition.counts = <count_type*>realloc(composition.counts, sizeof(count_type) * size)
composition.size = size
Expand All @@ -721,9 +721,9 @@ cdef int composition_resize_to(Composition* composition, size_t size) nogil:
return 0

cdef double composition_mass(Composition* composition) nogil:
'''
"""
Calculates the monoisotopic mass of `composition`
'''
"""
cdef:
double mass
Element* element
Expand Down Expand Up @@ -983,13 +983,14 @@ def isotope_parse_test(str element_symbol):


cdef class PyComposition(object):
'''A mapping representing a chemical composition.
"""
A mapping representing a chemical composition.
Implements arithmetic operations, +/- is defined
between a :class:`PyComposition` and a :class:`Mapping`-like
object, and * is defined between a :class:`PyComposition` and
an integer.
'''
"""

@staticmethod
cdef PyComposition _create(Composition* base):
Expand Down Expand Up @@ -1306,12 +1307,13 @@ cdef class PyComposition(object):
return iter(self.keys())

cpdef double mass(self):
'''Calculate the monoisotopic mass of this chemical composition
"""
Calculate the monoisotopic mass of this chemical composition
Returns
-------
float
'''
"""
if self._clean:
return self.cached_mass
else:
Expand Down
12 changes: 6 additions & 6 deletions src/brainpy/_c/isotopic_distribution.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ cdef int guess_npeaks(Composition* composition_struct, size_t max_npeaks, Elemen

def pyisotopic_variants(object composition not None, object npeaks=None, int charge=0,
double charge_carrier=PROTON):
'''
"""
Compute a peak list representing the theoretical isotopic cluster for `composition`.
Parameters
Expand All @@ -695,12 +695,12 @@ def pyisotopic_variants(object composition not None, object npeaks=None, int cha
Returns
-------
list of TheoreticalPeak
'''
"""
return _isotopic_variants(composition, npeaks, charge, charge_carrier)


cpdef list _isotopic_variants(object composition, object npeaks=None, int charge=0, double charge_carrier=PROTON):
'''
"""
Compute a peak list representing the theoretical isotopic cluster for `composition`.
Parameters
Expand All @@ -721,7 +721,7 @@ cpdef list _isotopic_variants(object composition, object npeaks=None, int charge
Returns
-------
list of TheoreticalPeak
'''
"""
cdef:
Composition* composition_struct
list peaklist
Expand Down Expand Up @@ -889,7 +889,7 @@ def test(object composition, int max_npeaks=300):

cdef size_t max_variants_approx(double mass, double lambda_factor=1800.0, size_t maxiter=255,
double threshold=0.9999) nogil:
'''Approximate the maximum number of isotopic peaks to include in an isotopic distribution
"""Approximate the maximum number of isotopic peaks to include in an isotopic distribution
approximation for biomolecules using the Poisson distribution, using the method described
in Bellew et al [1].
Expand Down Expand Up @@ -920,7 +920,7 @@ cdef size_t max_variants_approx(double mass, double lambda_factor=1800.0, size_t
[1] Bellew, M., Coram, M., Fitzgibbon, M., Igra, M., Randolph, T., Wang, P., May, D., Eng, J., Fang, R., Lin, C., Chen, J.,
Goodlett, D., Whiteaker, J., Paulovich, A., & Mcintosh, M. (2006). A suite of algorithms for the comprehensive analysis
of complex protein mixtures using high-resolution LC-MS. 22(15), 1902–1909. https://doi.org/10.1093/bioinformatics/btl276
'''
"""
cdef:
double lmbda = mass / lambda_factor
double p_i = 1.0
Expand Down
8 changes: 4 additions & 4 deletions src/brainpy/_speedup.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ cdef class PhiConstants(object):


cdef class Isotope(object):
'''
"""
Isotope represents an elenent with an integer number of neutrons specified.
Attributes
Expand All @@ -134,7 +134,7 @@ cdef class Isotope(object):
neutron_shift: int
The number of neutrons different between this isotope and the "normal" form. May be 0
if this represents that normal form.
'''
"""
cdef:
public double mass
public double abundance
Expand Down Expand Up @@ -607,11 +607,11 @@ cdef class IsotopicDistribution(object):
return mass_vector

def aggregated_isotopic_variants(self, int charge=0, double charge_carrier=PROTON):
'''
"""
Compute the m/z (or neutral mass when `charge` == 0) for each
aggregated isotopic peak and their intensity relative to
the monoisotopic peak.
'''
"""
cdef:
list probability_vector
list center_mass_vector
Expand Down
Loading

0 comments on commit 7226550

Please sign in to comment.