Project management

from pathlib import Path

[1]:
from spectrochempy import Project
from spectrochempy import pathclean
from spectrochempy import preferences as prefs
  SpectroChemPy's API - v.0.8.2.dev7
©Copyright 2014-2025 - A.Travert & C.Fernandez @ LCS
Running on GitHub Actions
MPL Configuration directory: /home/runner/.config/matplotlib
Stylelib directory: /home/runner/.config/matplotlib/stylelib

Project creation

We can easily create a new project to store various datasets

[2]:
proj = Project()

As we did not specify a name, a name has been attributed automatically:

[3]:
proj.name
[3]:
'Project-Project_61caa84e'

To get the signature of the object, one can use the usual ‘?’. Uncomment the following line to check

[4]:
# Project?

Let’s change this name

[5]:
proj.name = "myNMRdata"
proj
[5]:
Project myNMRdata:
    (empty project)

Now we will add a dataset to the project.

First we read the dataset (here some NMR data) and we give it some name (e.g. ‘nmr n°1’)

[6]:
datadir = pathclean(prefs.datadir)
path = datadir / "nmrdata" / "bruker" / "tests" / "nmr"

from spectrochempy import read_topspin

nd1 = read_topspin(
    path / "topspin_1d", expno=1, remove_digital_filter=True, name="NMR_1D"
)
nd2 = read_topspin(
    path / "topspin_2d", expno=1, remove_digital_filter=True, name="NMR_2D"
)
 WARNING | (UserWarning) (196608,)cannot be shaped into(147, 1024)

To add it to the project, we use the add_dataset function for a single dataset:

[7]:
proj.add_datasets(nd1)

or add_datasets for several datasets.

[8]:
proj.add_datasets(nd1, nd2)

Display its structure

[9]:
proj
[9]:
Project myNMRdata:
    ⤷ NMR_1D (dataset)
    ⤷ NMR_2D (dataset)

It is also possible to add other projects as sub-project (using the add_project )

Remove an element from a project

[10]:
proj.remove_dataset("NMR_1D")
proj
[10]:
Project myNMRdata:
    ⤷ NMR_2D (dataset)

Get project’s elements

[11]:
proj.add_datasets(nd1, nd2)
proj
[11]:
Project myNMRdata:
    ⤷ NMR_1D (dataset)
    ⤷ NMR_2D (dataset)

We can just use the name of the element as a project attribute.

[12]:
proj.NMR_1D
[12]:
NDDataset: [complex128] pp (size: 12411)[NMR_1D]
Summary
name
:
NMR_1D
author
:
runner@fv-az2211-104
created
:
2025-04-27 01:46:05+00:00
Data
title
:
intensity
values
:
...
R[ 1078 2284 ... 0.2342 -0.1008] pp
I[ -1037 -2200 ... 0.06203 -0.05273] pp
size
:
12411 (complex)
Dimension `x`
size
:
12411
title
:
F1 acquisition time
coordinates
:
[ 0 4 ... 4.964e+04 4.964e+04] µs
[13]:
proj.NMR_1D.plot()
[13]:
../../../_images/userguide_objects_project_project_25_1.png

However, this work only if the name contains no space, dot, comma, colon, etc. The only special character allowed is the underscore _ . If the name is not respecting this, then it is possible to use the following syntax (as a project behave as a dictionary). For example:

[14]:
proj["NMR_1D"].data
[14]:
array([1.08e+03-1.04e+03j, 2.28e+03-2.2e+03j, ...,    0.234+0.062j,   -0.101-0.0527j], shape=(12411,))
[15]:
proj.NMR_2D
[15]:
NDDataset: [quaternion] pp (shape: (y:96, x:948))[NMR_2D]
Summary
name
:
NMR_2D
author
:
runner@fv-az2211-104
created
:
2025-04-27 01:46:05+00:00
Data
title
:
intensity
values
:
...
RR[[ -0.2238 -0.1985 ... -0.1662 0.03262]
[-0.006566 0.0282 ... -0.02949 -0.06717]
...
[ 0 0 ... 0 0]
[ 0 0 ... 0 0]] pp
RI[[ 0.06219 0.1467 ... 0.04565 0.03068]
[-0.05969 -0.08752 ... -0.05134 -0.05994]
...
[ -0 -0 ... -0 -0]
[ -0 -0 ... -0 -0]] pp
IR[[ -0.1623 -0.0563 ... 0.02654 -0.01094]
[ 0.1344 -0.006515 ... -0.08239 0.00516]
...
[ 0 0 ... 0 0]
[ 0 0 ... 0 0]] pp
II[[-0.003312 -0.001535 ... 0.02067 -0.08058]
[-0.05685 0.1174 ... 0.05831 -0.003414]
...
[ -0 -0 ... -0 -0]
[ -0 -0 ... -0 -0]] pp
shape
:
(y:96(complex), x:948(complex))
Dimension `x`
size
:
948
title
:
F2 acquisition time
coordinates
:
[ 0 48 ... 4.541e+04 4.546e+04] µs
Dimension `y`
size
:
96
title
:
F1 acquisition time
coordinates
:
[ 0 74 ... 6956 7030] µs

Saving and loading projects

[16]:
proj
[16]:
Project myNMRdata:
    ⤷ NMR_1D (dataset)
    ⤷ NMR_2D (dataset)

Saving

[17]:
proj.save_as("NMR")
[17]:
PosixPath('/home/runner/work/spectrochempy/spectrochempy/docs/sources/userguide/objects/project/NMR.pscp')

Loading

[18]:
proj2 = Project.load("NMR")
[19]:
proj2
[19]:
Project NMR:
    ⤷ NMR_1D (dataset)
    ⤷ NMR_2D (dataset)
[20]:
proj2.NMR_1D.plot()
[20]:
../../../_images/userguide_objects_project_project_36_1.png
[21]:
proj2.NMR_2D
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File ~/work/spectrochempy/spectrochempy/.venv/lib/python3.13/site-packages/IPython/core/formatters.py:406, in BaseFormatter.__call__(self, obj)
    404     method = get_real_method(obj, self.print_method)
    405     if method is not None:
--> 406         return method()
    407     return None
    408 else:

File ~/work/spectrochempy/spectrochempy/src/spectrochempy/core/dataset/basearrays/ndarray.py:842, in NDArray._repr_html_(self)
    841 def _repr_html_(self):
--> 842     return convert_to_html(self)

File ~/work/spectrochempy/spectrochempy/src/spectrochempy/utils/print.py:168, in convert_to_html(obj, open, id)
    166 """Convert object representation to HTML with separate sections."""
    167 obj._html_output = True
--> 168 out = obj._cstr()
    170 # Split output into lines
    171 lines = out.split("\n")

File ~/work/spectrochempy/spectrochempy/src/spectrochempy/core/dataset/nddataset.py:619, in NDDataset._cstr(self)
    615 out += f"       author: {self.author}\n"
    616 out += f"      created: {self.created}\n"
    617 out += (
    618     f"     modified: {self.modified}\n"
--> 619     if (self._modified - self._created).seconds > 30
    620     else ""
    621 )
    623 wrapper1 = textwrap.TextWrapper(
    624     initial_indent="",
    625     subsequent_indent=" " * 15,
    626     replace_whitespace=True,
    627     width=self._text_width,
    628 )
    630 pars = self.description.strip().splitlines()

TypeError: can't subtract offset-naive and offset-aware datetimes
[21]:
NDDataset: [quaternion] pp (shape: (y:96, x:948))
[22]:
proj.NMR_2D.plot()
[22]:
../../../_images/userguide_objects_project_project_38_1.png
[ ]: