marekb-sci commited on
Commit
1a1bc96
·
verified ·
1 Parent(s): d463a70

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +241 -0
README.md ADDED
@@ -0,0 +1,241 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: ShimNet
3
+ emoji: 💻
4
+ colorFrom: indigo
5
+ colorTo: yellow
6
+ sdk: docker
7
+ pinned: false
8
+ license: mit
9
+ short_description: ShimNet Spectra Correction
10
+ ---
11
+
12
+ # ShimNet
13
+ ShimNet is a data-driven AI solution to improve high-resolution nuclear magnetic resonance (NMR) spectra
14
+ distorted by the inhomogeneous magnetic field (less than optimal shimming). To use it, the experimental training data has to be collected (see **Data collection** below).
15
+ Example data can also be downloaded (see below).
16
+
17
+ Paper: [ShimNet: A neural network for post-acquisition improvement of NMR spectra distorted by magnetic-field inhomogeneity](https://doi.org/10.1021/acs.jpcb.5c02632)
18
+
19
+ Web service: [![Open in Hugging Face Spaces](https://huggingface.co/datasets/huggingface/badges/resolve/main/open-in-hf-spaces-md.svg)](https://huggingface.co/spaces/NMR-CeNT-UW/ShimNet)
20
+
21
+ ## Installation
22
+
23
+ Python 3.10+ is required
24
+
25
+ You may install CPU-only version for inference only. If you need both training and inference, GPU version is strongly recommended.
26
+
27
+ In both CPU-only and GPU versions you may also install GUI (graphical user interface for inference)
28
+
29
+ - CPU-only version
30
+ - with GUI (recommended): `pip install .[cpu,gui] --extra-index-url https://download.pytorch.org/whl/cpu`
31
+ - without GUI: `pip install .[cpu] --extra-index-url https://download.pytorch.org/whl/cpu`
32
+ - GPU version (strongly recommended for training)
33
+ - with GUI: `pip install .[gpu,gui]`
34
+ - without GUI: `pip install .[gpu]`
35
+
36
+ ## Usage
37
+ To correct spectra presented in the paper:
38
+ 1. download weights (model parameters):
39
+ ```
40
+ python download_files.py
41
+ ```
42
+ or directly from [Google Drive 700MHz](https://drive.google.com/uc?export=download&id=17fTNWl7YW6mPbbZWga0EfdoF_6S8fCke) and [Google Drive 600MHz](https://drive.google.com/uc?export=download&id=1_VxOpFGJcFsOa5DHOW2GJbP8RvHCmC1N) and place it in `weights` directory
43
+
44
+
45
+ 2. : run correction (e.g. `Azarone_20ul_700MHz.csv`):
46
+ ```
47
+ python predict.py sample_data/Azarone_20ul_700MHz.csv -o output --config configs/shimnet_700.yaml --weights weights/shimnet_700MHz.pt
48
+ ```
49
+ The output will be `output/Azarone_20ul_700MHz_processed.csv` file
50
+
51
+ Multiple files may be processed using "*" syntax:
52
+ ```
53
+ python predict.py sample_data/*700MHz.csv -o output --config configs/shimnet_700.yaml --weights weights/shimnet_700MH
54
+ z.pt
55
+ ```
56
+
57
+ For 600 MHz data use `--config configs/shimnet_600.yaml` and `--weights weights/shimnet_600MHz.pt`, e.g.:
58
+
59
+ ```
60
+ python predict.py sample_data/CresolRed_after_styrene_600MHz.csv -o output --config configs/shimnet_600.yaml --weights weights/shimnet_600MHz.pt
61
+ ```
62
+
63
+ ### input format
64
+
65
+ The spectrum file for reconstruction should be in the format of two columns separated by a space and without the sign at the end of the line at the end of the file. The first column is frequency in ppm, the second is the intensity. The frequency values ​​should be in ascending order (example below):
66
+ ```csv
67
+ -1.97134 0.0167137
68
+ -1.97085 -0.00778748
69
+ -1.97036 -0.0109595
70
+ -1.96988 0.00825978
71
+ -1.96939 0.0133886
72
+ ```
73
+
74
+ ## Train on your data
75
+
76
+ For the model to function properly, it should be trained on calibration data from the spectrometer used for the measurements. To train a model on data from your spectrometer, please follow the instructions below.
77
+
78
+ ### Training data collection
79
+
80
+ Below we describe the training data collection for Agilent/Varian spectrometers. For machines of other vendors similar procedure can be implemented.
81
+ To collect ShimNet training data use Python script (sweep_shims_lineshape_Z1Z2.py) from the calibration_loop folder to drive the spectrometer:
82
+ 1. Install TReNDS package ( trends.spektrino.com )
83
+ 2. Open VnmrJ and type: 'listenon'
84
+ 3. Put the lineshape sample (1% CHCl3 in deuterated acetone), set standard PROTON parameters, and set nt=1 (do not modify sw and at!)
85
+ 4. Shim the sample and collect the data. Save the optimally shimmed dataset
86
+ 5. Edit the sweep_shims_lineshape_Z1Z2.py script
87
+ 6. Put optimum z1 and z2 shim values as optiz1 and optiz2 below
88
+ 7. Define the calibration range as range_z1 and range_z2 (default is ok)
89
+ 8. Start the python script:
90
+ ```
91
+ python3 ./sweep_shims_lineshape_Z1Z2.py
92
+ ```
93
+ The spectrometer will start collecting spectra
94
+
95
+ ### SCRF extraction
96
+ Shim Coil Response Functions (SCRF) should be extracted from the spectra with `extract_scrf_from_fids.py` script.
97
+ ```
98
+ python extract_scrf_from_fids.py
99
+ ```
100
+
101
+ The script uses hardcoded paths to the NMR signals (fid-s) in Agilent/Varian format: a directory with optimal measurement (`opti_fid_path` available) and a directory with calibration loop measurements (`data_dir`):
102
+ ```python
103
+ # input
104
+ data_dir = "../../sample_run/loop"
105
+ opti_fid_path = "../../sample_run/opti.fid"
106
+
107
+ ```
108
+
109
+ The output files are also hardcoded:
110
+ ```python
111
+ # output
112
+ spectra_file = "../../sample_run/total.npy"
113
+ spectra_file_names = "../../sample_run/total.csv"
114
+ opi_spectrum_file = "../../sample_run/opti.npy"
115
+ responses_file = "../../sample_run/scrf_61.pt"
116
+ ```
117
+ where only the `responses_file` is used in ShimNet training.
118
+
119
+ If the measurements are stored in a format other than Varian, you may need to change this line:
120
+ ```python
121
+ dic, data = ng.varian.read(varian_fid_path)
122
+ ```
123
+ (see nmrglue package documentation for details)
124
+
125
+ ### Training
126
+
127
+ 1. Download multiplets database:
128
+ ```
129
+ python download_files.py --multiplets
130
+ ```
131
+ 2. Configure run:
132
+ - create a run directory, e.g. `runs/my_lab_spectrometer_2025`
133
+ - create a configuration file:
134
+ 1. copy `configs/shimnet_template.py` to the run directory and rename it to `config.yaml`
135
+ ```bash
136
+ cp configs/shimnet_template.py runs/my_lab_spectrometer_2025/config.yaml
137
+ ```
138
+ 2. edit the SCRF in path in the config file:
139
+ ```yaml
140
+ response_functions_files:
141
+ - path/to/srcf_file
142
+ ```
143
+ e.g.
144
+ ```yaml
145
+ response_functions_files:
146
+ - ../../sample_run/scrf_61.pt
147
+ ```
148
+ 3. adjust spectrometer frequency step `frq_step` to match your data (spectrometer range in Hz divided by number of points in spectrum):
149
+ ```yaml
150
+ frq_step: 0.34059797
151
+ ```
152
+ 4. adjust spectromer frequency in the metadata
153
+ ```yaml
154
+ metadata: # additional metadata, not used in the training process
155
+ spectrometer_frequency: 700.0 # MHz
156
+ ```
157
+ 3. Run training:
158
+ ```
159
+ python train.py runs/my_lab_spectrometer_2025
160
+ ```
161
+ Training results will appear in `runs/my_lab_spectrometer_2025` directory.
162
+ Model parameters are stored in `runs/my_lab_spectrometer_2025/model.pt` file
163
+ 4. Use trained model:
164
+
165
+ use `--config runs/my_lab_spectrometer_2025/config.yaml` and `--weights runs/my_lab_spectrometer_2025/model.pt` flags, e.g.
166
+ ```
167
+ python predict.py my_sample1.csv -o my_output --config runs/my_lab_spectrometer_2025/config.yaml --weights runs/my_lab_spectrometer_2025/model.pt
168
+ ```
169
+
170
+ ## Repeat training on our data
171
+
172
+ If you want to train the network using the calibration data from our paper, follow the procedure below.
173
+
174
+ 1. Download multiplets database and our SCRF files:
175
+ ```
176
+ python download_files.py --multiplets --SCRF --no-weights
177
+ ```
178
+ or directly download from Google Drive and store in `data/` directory: [Response Functions 600MHz](https://drive.google.com/file/d/1J-DsPtaITXU3TFrbxaZPH800U1uIiwje/view?usp=sharing), [Response Functions 700MHz](https://drive.google.com/file/d/113al7A__yYALx_2hkESuzFIDU3feVtNY/view?usp=sharing), [Multiplets data](https://drive.google.com/file/d/1QGvV-Au50ZxaP1vFsmR_auI299Dw-Wrt/view?usp=sharing)
179
+
180
+ 2. Configure run
181
+ - For 600MHz spectrometer:
182
+ ```bash
183
+ mkdir -p runs/repeat_paper_training_600MHz
184
+ cp configs/shimnet_600.yaml runs/repeat_paper_training_600MHz/config.yaml
185
+ ```
186
+ - For 700 MHz spectrometer:
187
+ ```bash
188
+ mkdir -p runs/repeat_paper_training_700MHz
189
+ cp configs/shimnet_700.yaml runs/repeat_paper_training_700MHz/config.yaml
190
+ ```
191
+ 3. Run training:
192
+ ```
193
+ python train.py runs/repeat_paper_training_600MHz
194
+ ```
195
+ or
196
+ ```
197
+ python train.py runs/repeat_paper_training_700MHz
198
+ ```
199
+ Training results will appear in `runs/repeat_paper_training_600MHz` or `runs/repeat_paper_training_700MHz` directory.
200
+
201
+ ## GUI
202
+
203
+ ### Launching the GUI
204
+
205
+ The ShimNet GUI is built using Gradio. To start the application, run:
206
+
207
+ ```bash
208
+ python predict-gui.py
209
+ ```
210
+
211
+ Once the application starts, open your browser and navigate to:
212
+
213
+ ```
214
+ http://127.0.0.1:7860
215
+ ```
216
+
217
+ to access the GUI locally.
218
+
219
+ ### Sharing the GUI
220
+
221
+ To make the GUI accessible over the internet, use the `--share` flag:
222
+
223
+ ```bash
224
+ python predict-gui.py --share
225
+ ```
226
+
227
+ A public web address will be displayed in the terminal, which you can use to access the GUI remotely or share with others.
228
+
229
+ ### GUI inference with Docker
230
+
231
+ Create docker image:
232
+ ```bash
233
+ docker build -t shimnetgui .
234
+ ```
235
+
236
+ Run the container:
237
+ ```bash
238
+ docker run -it -p 7860:7860 shimnetgui
239
+ ```
240
+
241
+ The GUI should be working at `http://127.0.0.1:7860`