1
1
import itertools
2
2
import os
3
3
import re
4
+ from pathlib import Path
4
5
5
6
import matplotlib .pyplot as plt
6
7
import nmrglue as ng
13
14
######################################################################################
14
15
15
16
16
- def read_bruker_1d (data , process_number = 1 , verbose = True , sample_mass_mg = None ):
17
+ def read_bruker_1d (
18
+ data : Path | pd .DataFrame ,
19
+ process_number : int = 1 ,
20
+ verbose : bool = False ,
21
+ sample_mass_mg : float | None = None ,
22
+ ) -> tuple [pd .DataFrame | None , dict , str | None , tuple [int , ...]]:
17
23
"""Read a 1D bruker nmr spectrum and return it as a df.
18
24
19
- arguments:
25
+ Parameters:
26
+ data: The directory of the full bruker data file, or a pandas DataFrame which
27
+ will be returned without further processing.
28
+ process_number: The process number of the processed data you want to plot [default: 1].
29
+ verbose: Whether to print information such as the spectrum title to stdout.
30
+ sample_mass_mg: The (optional) sample mass. If provided, the resulting DataFrame will have a "intensity_per_scan_per_gram" column.
31
+
32
+ Returns:
33
+ df: A pandas DataFrame containing the spectrum data, or None if the reading failed.
34
+ a_dic: A dictionary containing the acquisition parameters.
35
+ topspin_title: The title of the spectrum, as stored in the topspin "title" file.
36
+ shape: The shape of the spectrum data array.
20
37
21
- data: The directory of the full bruker data file. You may also supply a df as this argument. In this case, the df is returned as is.
22
- process_number: The process number of the processed data you want to plot [default 1]
23
- verbose: Whether to print information such as the spectrum title to stdout (default True)
24
- sample_mass_mg: The (optional) sample mass. If provided, the resulting DataFrame will have a "intensity_per_scan_per_gram" column.
25
38
"""
26
39
27
40
# if df is provided, just return it as-is. This functionality is provided to make functions calling read_bruker_1d flexible by default.
@@ -32,12 +45,12 @@ def read_bruker_1d(data, process_number=1, verbose=True, sample_mass_mg=None):
32
45
print ("data frame provided to read_bruker_1d(). Returning it as is." )
33
46
return data
34
47
else :
35
- data_dir = data
48
+ data_dir = Path ( data )
36
49
37
- processed_data_dir = os . path . join ( data_dir , "pdata" , str (process_number ) )
50
+ processed_data_dir = data_dir / "pdata" / str (process_number )
38
51
39
- a_dic , a_data = ng .fileio .bruker .read (data_dir ) # aquisition_data
40
- p_dic , p_data = ng .fileio .bruker .read_pdata (processed_data_dir ) # processing data
52
+ a_dic , a_data = ng .fileio .bruker .read (str ( data_dir ) ) # aquisition_data
53
+ p_dic , p_data = ng .fileio .bruker .read_pdata (str ( processed_data_dir ) ) # processing data
41
54
42
55
try :
43
56
with open (os .path .join (processed_data_dir , "title" ), "r" ) as f :
@@ -46,7 +59,6 @@ def read_bruker_1d(data, process_number=1, verbose=True, sample_mass_mg=None):
46
59
topspin_title = None
47
60
48
61
if len (p_data .shape ) > 1 :
49
- print ("data is more than one dimensional - read failed" )
50
62
return None , a_dic , topspin_title , p_data .shape
51
63
52
64
nscans = a_dic ["acqus" ]["NS" ]
0 commit comments