Skip to content

Commit

Permalink
Merge pull request #223 from MannLabs/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
sander-willems-bruker authored Sep 14, 2022
2 parents 1c06ff1 + c7f5c6d commit 4c51435
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 20 deletions.
2 changes: 1 addition & 1 deletion alphatims/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


__project__ = "alphatims"
__version__ = "1.0.0"
__version__ = "1.0.1"
__license__ = "Apache"
__description__ = "A Python package to index Bruker TimsTOF raw data for fast and easy accession and visualization"
__author__ = "Sander Willems, Eugenia Voytik"
Expand Down
73 changes: 71 additions & 2 deletions alphatims/bruker.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,11 @@ def mobility_values(self):
""": np.ndarray : np.float64[:] : The mobility values."""
return self._mobility_values

@property
def cycle(self):
""": np.ndarray : np.float64[:,:,:,:] : The quad values."""
return self._cycle

@property
def mz_values(self):
""": np.ndarray : np.float64[:] : The mz values."""
Expand Down Expand Up @@ -1134,6 +1139,14 @@ def _import_data_from_d_folder(
) / self.scan_max_index * np.arange(self.scan_max_index)
mz_min_value = float(self.meta_data["MzAcqRangeLower"])
mz_max_value = float(self.meta_data["MzAcqRangeUpper"])
if self.meta_data["AcquisitionSoftware"] == "Bruker otofControl":
logging.warning(
"WARNING: Acquisition software is Bruker otofControl, "
"mz min/max values are assumed to be 5 m/z wider than "
"defined in analysis.tdf!"
)
mz_min_value -= 5
mz_max_value += 5
tof_intercept = np.sqrt(mz_min_value)
tof_slope = (
np.sqrt(mz_max_value) - tof_intercept
Expand Down Expand Up @@ -1170,11 +1183,12 @@ def _import_data_from_d_folder(
self._parse_quad_indptr()
self._intensity_min_value = int(np.min(self.intensity_values))
self._intensity_max_value = int(np.max(self.intensity_values))
self.set_cycle()

def save_as_hdf(
self,
directory: str,
file_name: str,
directory: str = None,
file_name: str = None,
overwrite: bool = False,
compress: bool = False,
return_as_bytes_io: bool = False,
Expand All @@ -1186,9 +1200,11 @@ def save_as_hdf(
directory : str
The directory where to save the HDF file.
Ignored if return_as_bytes_io == True.
Default is None, meaning it is parsed from input file.
file_name : str
The file name of the HDF file.
Ignored if return_as_bytes_io == True.
Default is None, meaning it is parsed from input file.
overwrite : bool
If True, an existing file is truncated.
If False, the existing file is appended to only if the original
Expand All @@ -1213,6 +1229,10 @@ def save_as_hdf(
The full file name or a bytes stream containing the HDF file.
"""
import io
if directory is None:
directory = self.directory
if file_name is None:
file_name = f"{self.sample_name}.hdf"
if overwrite:
hdf_mode = "w"
else:
Expand Down Expand Up @@ -2250,6 +2270,55 @@ def use_calibrated_mz_values_as_default(
self._use_calibrated_mz_values_as_default = use_calibrated_mz_values


def set_cycle(self) -> None:
"""Set the quad cycle for diaPASEF data.
"""
last_window_group = -1
for max_index, (frame, window_group) in enumerate(
zip(
self.fragment_frames.Frame,
self.fragment_frames.Precursor
)
):
if window_group < last_window_group:
break
else:
last_window_group = window_group
frames = self.fragment_frames.Frame[max_index-1]
frames += self.fragment_frames.Frame[0] == int(self.zeroth_frame)
sub_cycles = frames - len(np.unique(self.fragment_frames.Frame[:max_index]))
cycle = np.zeros(
(
frames,
self.scan_max_index,
2,
)
)
# cycle[:] = -1
precursor_frames = np.ones(frames, dtype=np.bool_)
for index, row in self.fragment_frames[:max_index].iterrows():
frame = int(row.Frame - self.zeroth_frame)
scan_begin = int(row.ScanNumBegin)
scan_end = int(row.ScanNumEnd)
low_mz = row.IsolationMz - row.IsolationWidth / 2
high_mz = row.IsolationMz + row.IsolationWidth / 2
# print(low_mz, high_mz)
cycle[
frame,
scan_begin: scan_end,
] = (low_mz, high_mz)
precursor_frames[frame] = False
cycle[precursor_frames] = (-1, -1)
cycle = cycle.reshape(
(
sub_cycles,
frames // sub_cycles,
*cycle.shape[1:]
)
)
self._cycle = cycle


class PrecursorFloatError(TypeError):
"""Used to indicate that a precursor value is not an int but a float."""
pass
Expand Down
10 changes: 6 additions & 4 deletions alphatims/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ def line_plot(

def heatmap(
df,
x_axis_label: str,
y_axis_label: str,
x_axis_label: str = "rt",
y_axis_label: str = "mobility",
title: str = "",
z_axis_label: str = "intensity",
width: int = 1000,
Expand All @@ -150,14 +150,16 @@ def heatmap(
This should be obtained by slicing an alphatims.bruker.TimsTOF object.
x_axis_label : str
A label that is used for projection
(i.e. intensities are summed) on the x-axis. Options are:
(i.e. intensities are summed) on the x-axis.
Default is "rt". Options are:
- mz
- rt
- mobility
y_axis_label : str
A label that is used for projection
(i.e. intensities are summed) on the y-axis. Options are:
(i.e. intensities are summed) on the y-axis.
Default is "mobility". Options are:
- mz
- rt
Expand Down
2 changes: 1 addition & 1 deletion alphatims/tempmmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def empty(shape: tuple, dtype: np.dtype) -> np.ndarray:
np.ndarray
A writable temporary mmapped array.
"""
element_count = np.prod(shape)
element_count = np.prod(shape, dtype=np.int64)
if element_count <= 0:
raise ValueError(
f"Shape {shape} has an invalid element count of {element_count}"
Expand Down
59 changes: 59 additions & 0 deletions alphatims/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,65 @@ def njit(_func=None, *args, **kwargs):
return numba.njit(_func, *args, cache=cache, **kwargs)


def class_njit(
_func=None,
):
import numpy as np
import numba
import types
import ast
import textwrap
import inspect
import pandas as pd
def parse_dict(dict_to_parse):
result_dict = {}
for key, value in dict_to_parse.items():
if isinstance(value, pd.DataFrame):
key_module = types.ModuleType(f"{key}")
for column in value.columns:
key_module.__dict__[column] = np.array(
value[column].values,
copy=False
)
result_dict[key] = key_module
else:
result_dict[key] = value
return result_dict
def wrapper(func):
def inner_func(self, *args, **kwargs):
self_ = self
self = types.ModuleType(f"{self.__class__}_{id(self)}")
self.__dict__.update(parse_dict(self_.__dict__))
tree = ast.parse(
textwrap.dedent(
inspect.getsource(
func
)
)
)
# tree.body[0].decorator_list = [
# decorator for decorator in tree.body[0].decorator_list if decorator.id != "class_njit"
# ]
tree.body[0].decorator_list = []
tree.body[0].args.args = tree.body[0].args.args[1:]
tree.body[0].name = f"{func.__name__}_class_njit"
src = ast.unparse(tree)
env = dict(locals())
env.update(globals())
env["numba"] = numba
exec(src, env, env)
src = f"self_.{func.__name__} = numba.njit(env['{func.__name__}_class_njit'])"
# src = f"self_.{func.__name__} = env['{func.__name__}_performance']"
exec(src)
result = eval(f"self_.{func.__name__}(*args, **kwargs)")
return result
return inner_func
if _func is None:
return wrapper
else:
return wrapper(_func)


def pjit(
_func=None,
*,
Expand Down
2 changes: 1 addition & 1 deletion misc/bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 1.0.0
current_version = 1.0.1
commit = True
tag = False
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\-(?P<release>[a-z]+)(?P<build>\d+))?
Expand Down
2 changes: 1 addition & 1 deletion misc/one_click_linux/control
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: AlphaTims
Version: 1.0.0
Version: 1.0.1
Architecture: all
Maintainer: Mann Labs <[email protected]>
Description: AlphaTims GUI
Expand Down
2 changes: 1 addition & 1 deletion misc/one_click_linux/create_installer_linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ rm -rf dist
rm -rf build
python setup.py sdist bdist_wheel
cd misc/one_click_linux
pip install "../../dist/alphatims-1.0.0-py3-none-any.whl[plotting-stable,stable,legacy-stable]"
pip install "../../dist/alphatims-1.0.1-py3-none-any.whl[plotting-stable,stable,legacy-stable]"
pip install pyinstaller==4.10
pyinstaller ../pyinstaller/alphatims.spec -y
conda deactivate
Expand Down
4 changes: 2 additions & 2 deletions misc/one_click_macos/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<key>CFBundleIconFile</key>
<string>alpha_logo.icns</string>
<key>CFBundleIdentifier</key>
<string>alphatims.1.0.0</string>
<string>alphatims.1.0.1</string>
<key>CFBundleShortVersionString</key>
<string>1.0.0</string>
<string>1.0.1</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
6 changes: 3 additions & 3 deletions misc/one_click_macos/create_installer_macos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rm -rf build
python setup.py sdist bdist_wheel
cd misc/one_click_macos
pip install pyinstaller==4.10
pip install "../../dist/alphatims-1.0.0-py3-none-any.whl[plotting-stable,stable,legacy-stable]"
pip install "../../dist/alphatims-1.0.1-py3-none-any.whl[plotting-stable,stable,legacy-stable]"
conda list
pyinstaller ../pyinstaller/alphatims.spec -y
conda deactivate
Expand All @@ -34,7 +34,7 @@ if false; then
# https://scriptingosx.com/2019/09/notarize-a-command-line-tool/
for f in $(find dist/alphatims -name '*.so' -or -name '*.dylib'); do codesign --sign "Developer ID Application: Max-Planck-Gesellschaft zur Förderung der Wissenschaften e.V. (7QSY5527AQ)" $f; done
codesign --sign "Developer ID Application: Max-Planck-Gesellschaft zur Förderung der Wissenschaften e.V. (7QSY5527AQ)" dist/alphatims/Contents/MacOS/alphatims_gui --force --options=runtime --entitlements entitlements.xml
pkgbuild --root dist/alphatims --identifier de.mpg.biochem.alphatims.app --version 1.0.0 --install-location /Applications/AlphaTims.app --scripts scripts alphatims.pkg --sign "Developer ID Installer: Max-Planck-Gesellschaft zur Förderung der Wissenschaften e.V. (7QSY5527AQ)"
pkgbuild --root dist/alphatims --identifier de.mpg.biochem.alphatims.app --version 1.0.1 --install-location /Applications/AlphaTims.app --scripts scripts alphatims.pkg --sign "Developer ID Installer: Max-Planck-Gesellschaft zur Förderung der Wissenschaften e.V. (7QSY5527AQ)"
productbuild --distribution distribution.xml --resources Resources --package-path alphatims.pkg dist/alphatims_gui_installer_macos.pkg --sign "Developer ID Installer: Max-Planck-Gesellschaft zur Förderung der Wissenschaften e.V. (7QSY5527AQ)"
requestUUID=$(xcrun altool --notarize-app --primary-bundle-id "de.mpg.biochem.alphatims.app" --username "[email protected]" --password "@keychain:Alphatims-develop" --asc-provider 7QSY5527AQ --file dist/alphatims_gui_installer_macos.pkg 2>&1 | awk '/RequestUUID/ { print $NF; }')
request_status="in progress"
Expand All @@ -46,6 +46,6 @@ if false; then
xcrun altool --notarization-info "$requestUUID" --username "[email protected]" --password "@keychain:Alphatims-develop"
xcrun stapler staple dist/alphatims_gui_installer_macos.pkg
else
pkgbuild --root dist/alphatims --identifier de.mpg.biochem.alphatims.app --version 1.0.0 --install-location /Applications/AlphaTims.app --scripts scripts alphatims.pkg
pkgbuild --root dist/alphatims --identifier de.mpg.biochem.alphatims.app --version 1.0.1 --install-location /Applications/AlphaTims.app --scripts scripts alphatims.pkg
productbuild --distribution distribution.xml --resources Resources --package-path alphatims.pkg dist/alphatims_gui_installer_macos.pkg
fi
2 changes: 1 addition & 1 deletion misc/one_click_macos/distribution.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<installer-script minSpecVersion="1.000000">
<title>AlphaTims 1.0.0</title>
<title>AlphaTims 1.0.1</title>
<background mime-type="image/png" file="alpha_logo.png" scaling="proportional"/>
<welcome file="welcome.html" mime-type="text/html" />
<conclusion file="conclusion.html" mime-type="text/html" />
Expand Down
2 changes: 1 addition & 1 deletion misc/one_click_windows/alphatims_innoinstaller.iss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "AlphaTims"
#define MyAppVersion "1.0.0"
#define MyAppVersion "1.0.1"
#define MyAppPublisher "Max Planck Institute of Biochemistry, Mann department"
#define MyAppURL "https://github.com/MannLabs/alphatims"
#define MyAppExeName "alphatims_gui.exe"
Expand Down
2 changes: 1 addition & 1 deletion misc/one_click_windows/create_installer_windows.bat
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ call rmdir dist /s /q
call rmdir build /s /q
call python setup.py sdist bdist_wheel
call cd misc/one_click_windows
call pip install "../../dist/alphatims-1.0.0-py3-none-any.whl[plotting-stable,stable,legacy-stable]"
call pip install "../../dist/alphatims-1.0.1-py3-none-any.whl[plotting-stable,stable,legacy-stable]"
call pip install pyinstaller==4.10
call pyinstaller ../pyinstaller/alphatims.spec -y
call conda deactivate
Expand Down
2 changes: 1 addition & 1 deletion misc/one_click_windows/create_installer_windows.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ rm -rf dist
rm -rf build
python setup.py sdist bdist_wheel
cd misc/one_click_windows
pip install "../../dist/alphatims-1.0.0-py3-none-any.whl[plotting-stable,stable,legacy-stable]"
pip install "../../dist/alphatims-1.0.1-py3-none-any.whl[plotting-stable,stable,legacy-stable]"
pip install pyinstaller==4.10
# TODO https://stackoverflow.com/questions/54175042/python-3-7-anaconda-environment-import-ssl-dll-load-fail-error/60405693#60405693
pyinstaller ../pyinstaller/alphatims.spec -y
Expand Down

0 comments on commit 4c51435

Please sign in to comment.