Project management
from pathlib import Path
[1]:
import spectrochempy as scp
Project creation
We can easily create a new project to store various datasets
[2]:
proj = scp.Project()
As we did not specify a name, a name has been attributed automatically:
[3]:
proj.name
[3]:
'Project-Project_8cdf0b9a'
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]:
(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’).
Requires the official spectrochempy-nmr plugin. Install with: pip install spectrochempy[nmr].
[6]:
datadir = scp.pathclean(scp.preferences.datadir)
path = datadir / "nmrdata" / "bruker" / "tests" / "nmr"
nd1 = scp.nmr.read_topspin(
path / "topspin_1d", expno=1, remove_digital_filter=True, name="NMR_1D"
)
# Use the same dataset twice for the example (real projects would use different data)
nd2 = nd1.copy()
nd2.name = "NMR_1D_copy"
WARNING | (UserWarning) No module named 'spectrochempy_hypercomplex'
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[6], line 8
4 nd1 = scp.nmr.read_topspin(
5 path / "topspin_1d", expno=1, remove_digital_filter=True, name="NMR_1D"
6 )
7 # Use the same dataset twice for the example (real projects would use different data)
----> 8 nd2 = nd1.copy()
9 nd2.name = "NMR_1D_copy"
AttributeError: 'NoneType' object has no attribute 'copy'
To add it to the project, we use the add_dataset function for a single dataset:
[7]:
proj.add_datasets(nd1)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[7], line 1
----> 1 proj.add_datasets(nd1)
File ~/work/spectrochempy/spectrochempy/.venv/lib/python3.13/site-packages/spectrochempy/core/project/project.py:436, in Project.add_datasets(self, *datasets)
411 """
412 Add several datasets to the current project.
413
(...) 433
434 """
435 for ds in datasets:
--> 436 self.add_dataset(ds)
File ~/work/spectrochempy/spectrochempy/.venv/lib/python3.13/site-packages/spectrochempy/core/project/project.py:463, in Project.add_dataset(self, dataset, name)
438 def add_dataset(self, dataset, name=None):
439 """
440 Add a single dataset to the current project.
441
(...) 461
462 """
--> 463 dataset.parent = self
464 if name is None:
465 name = dataset.name
AttributeError: 'NoneType' object has no attribute 'parent' and no __dict__ for setting new attributes
or add_datasets for several datasets.
[8]:
proj.add_datasets(nd1, nd2)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[8], line 1
----> 1 proj.add_datasets(nd1, nd2)
NameError: name 'nd2' is not defined
Display its structure
[9]:
proj
[9]:
(empty project)
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
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[10], line 1
----> 1 proj.remove_dataset("NMR_1D")
2 proj
File ~/work/spectrochempy/spectrochempy/.venv/lib/python3.13/site-packages/spectrochempy/core/project/project.py:486, in Project.remove_dataset(self, name)
476 def remove_dataset(self, name):
477 """
478 Remove a dataset from the project.
479
(...) 484
485 """
--> 486 self._datasets[name]._parent = None # remove the parent info
487 del self._datasets[name]
KeyError: 'NMR_1D'
Get project’s elements
[11]:
proj.add_datasets(nd1, nd2)
proj
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[11], line 1
----> 1 proj.add_datasets(nd1, nd2)
2 proj
NameError: name 'nd2' is not defined
We can just use the name of the element as a project attribute.
[12]:
proj.NMR_1D
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[12], line 1
----> 1 proj.NMR_1D
File ~/work/spectrochempy/spectrochempy/.venv/lib/python3.13/site-packages/spectrochempy/core/project/project.py:198, in Project.__getattr__(self, item)
194 if item in self.meta:
195 # return the attribute
196 return self.meta[item]
--> 198 raise AttributeError
AttributeError:
[13]:
_ = proj.NMR_1D.plot()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[13], line 1
----> 1 _ = proj.NMR_1D.plot()
File ~/work/spectrochempy/spectrochempy/.venv/lib/python3.13/site-packages/spectrochempy/core/project/project.py:198, in Project.__getattr__(self, item)
194 if item in self.meta:
195 # return the attribute
196 return self.meta[item]
--> 198 raise AttributeError
AttributeError:
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
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[14], line 1
----> 1 proj["NMR_1D"].data
File ~/work/spectrochempy/spectrochempy/.venv/lib/python3.13/site-packages/spectrochempy/core/project/project.py:160, in Project.__getitem__(self, key)
158 if key in self.scripts_names:
159 return self._scripts[key]
--> 160 raise KeyError(f"{key}: This object name does not exist in this project.")
KeyError: 'NMR_1D: This object name does not exist in this project.'
[15]:
proj.NMR_1D_copy
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[15], line 1
----> 1 proj.NMR_1D_copy
File ~/work/spectrochempy/spectrochempy/.venv/lib/python3.13/site-packages/spectrochempy/core/project/project.py:198, in Project.__getattr__(self, item)
194 if item in self.meta:
195 # return the attribute
196 return self.meta[item]
--> 198 raise AttributeError
AttributeError:
Saving and loading projects
[16]:
proj
[16]:
(empty project)
Saving
[17]:
proj.save_as("NMR")
[17]:
PosixPath('/home/runner/work/spectrochempy/spectrochempy/docs/sources/userguide/objects/project/NMR.pscp')
Loading
[18]:
proj2 = scp.Project.load("NMR")
[19]:
proj2
[19]:
(empty project)
[20]:
_ = proj2.NMR_1D.plot()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[20], line 1
----> 1 _ = proj2.NMR_1D.plot()
File ~/work/spectrochempy/spectrochempy/.venv/lib/python3.13/site-packages/spectrochempy/core/project/project.py:198, in Project.__getattr__(self, item)
194 if item in self.meta:
195 # return the attribute
196 return self.meta[item]
--> 198 raise AttributeError
AttributeError:
[21]:
proj2.NMR_1D_copy
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[21], line 1
----> 1 proj2.NMR_1D_copy
File ~/work/spectrochempy/spectrochempy/.venv/lib/python3.13/site-packages/spectrochempy/core/project/project.py:198, in Project.__getattr__(self, item)
194 if item in self.meta:
195 # return the attribute
196 return self.meta[item]
--> 198 raise AttributeError
AttributeError:
[22]:
_ = proj.NMR_1D_copy.plot()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[22], line 1
----> 1 _ = proj.NMR_1D_copy.plot()
File ~/work/spectrochempy/spectrochempy/.venv/lib/python3.13/site-packages/spectrochempy/core/project/project.py:198, in Project.__getattr__(self, item)
194 if item in self.meta:
195 # return the attribute
196 return self.meta[item]
--> 198 raise AttributeError
AttributeError:
[ ]: