spectrochempy.Projectο
- class Project(*args, argnames=None, name=None, **meta)[source]ο
A manager for projects, datasets and scripts.
It can handle multiple datasets, subprojects, and scripts in a main project.
- Parameters:
*args (Series of objects, optional) β Argument type will be interpreted correctly if they are of type
NDDataset
,Project
, or other objects such asScript
. This is optional, as they can be added later.argnames (list, optional) β If not None, this list gives the names associated to each object passed as args. It MUST be the same length that the number of args, or an error will be raised. If None, the internal name of each object will be used instead.
name (str, optional) β The name of the project. If the name is not provided, it will be generated automatically.
**meta (dict) β Any other attributes to describe the project.
Examples
>>> myproj = scp.Project(name='project_1') >>> ds = scp.NDDataset([1., 2., 3.], name='dataset_1') >>> myproj.add_dataset(ds) >>> print(myproj) Project project_1: β€· dataset_1 (dataset)
Attributes Summary
All items contained in this project (list).
Names of all objects contained in this project (list).
Datasets included in this project excluding those located in subprojects.
Names of all dataset included in this project.
Get current filename for this dataset.
Type of current file.
Readonly object identifier (str).
Metadata for the project (meta).
A user-friendly name for the project.
Instance of the Project which is the parent (if any) of the current project (project).
Subprojects included in this project (list).
Names of all subprojects included in this project (list).
Scripts included in this project (list).
Names of all scripts included in this project (list).
Filename suffix.
Methods Summary
add_dataset
(dataset[,Β name])Add a single dataset to the current project.
add_datasets
(*datasets)Add several datasets to the current project.
add_project
(proj[,Β name])Add one project to the current project.
add_projects
(*projects)Add one or a series of projects to the current project.
add_script
(script[,Β name])Add one script to the current project.
add_scripts
(*scripts)Add one or a series of scripts to the current project.
copy
()Make an exact copy of the current project.
dump
(filename,Β **kwargs)Save the current object into compressed native spectrochempy format.
load
(filename,Β **kwargs)Open data from a '.scp' (NDDataset) or '.pscp' (Project) file.
loads
(js,Β Any])Deserialize dataset from JSON.
Remove all dataset from the project.
Remove all projects from the current project.
remove_dataset
(name)Remove a dataset from the project.
remove_project
(name)Remove one project from the current project.
save
(**kwargs)Save dataset in native .scp format.
save_as
(filename,Β **kwargs)Save the current NDDataset in SpectroChemPy format (.scp).
Attributes Documentation
- allitemsο
All items contained in this project (list).
- allnamesο
Names of all objects contained in this project (list).
- datasetsο
Datasets included in this project excluding those located in subprojects.
(list).
- datasets_namesο
Names of all dataset included in this project.
(does not return those located in sub-folders) (list).
- filenameο
Get current filename for this dataset.
- filetypeο
Type of current file.
- idο
Readonly object identifier (str).
- metaο
Metadata for the project (meta).
meta contains all attribute except the name, id and parent of the current project.
- nameο
A user-friendly name for the project.
The default is automatically generated (str).
- parentο
Instance of the Project which is the parent (if any) of the current project (project).
- projectsο
Subprojects included in this project (list).
- projects_namesο
Names of all subprojects included in this project (list).
- scriptsο
Scripts included in this project (list).
- scripts_namesο
Names of all scripts included in this project (list).
- suffixο
Filename suffix.
Read Only property - automatically set when the filename is updated if it has a suffix, else give the default suffix for the given type of object.
Methods Documentation
- add_dataset(dataset, name=None)[source]ο
Add a single dataset to the current project.
- Parameters:
See also
add_datasets
Add several datasets to the current project.
Examples
>>> ds1 = scp.NDDataset([1, 2, 3]) >>> proj = scp.Project() >>> proj.add_dataset(ds1, name='Toto')
- add_datasets(*datasets)[source]ο
Add several datasets to the current project.
- Parameters:
*datasets (series of
NDDataset
) β Datasets to add to the current project. The name of the entries in the project will be identical to the names of the datasets.
See also
add_dataset
Add a single dataset to the current project.
Examples
>>> ds1 = scp.NDDataset([1, 2, 3]) >>> ds2 = scp.NDDataset([4, 5, 6]) >>> ds3 = scp.NDDataset([7, 8, 9]) >>> proj = scp.Project() >>> proj.add_datasets(ds1, ds2, ds3)
- add_project(proj, name=None)[source]ο
Add one project to the current project.
- Parameters:
proj (a project instance) β A project to add to the current one.
- add_projects(*projects)[source]ο
Add one or a series of projects to the current project.
- Parameters:
projects (project instances) β The projects to add to the current ones.
- add_script(script, name=None)[source]ο
Add one script to the current project.
- Parameters:
script (a
Script
instance)name (str)
- add_scripts(*scripts)[source]ο
Add one or a series of scripts to the current project.
- Parameters:
scripts (
Script
instances)
- dump(filename, **kwargs)[source]ο
Save the current object into compressed native spectrochempy format.
- Parameters:
filename (str of
pathlib
object) β File name where to save the current object.
- classmethod load(filename: str | pathlib.Path | BinaryIO, **kwargs: Any) -> Any: """ Open data from a '*.scp' (NDDataset) or '*.pscp' (Project) file. Parameters ---------- filename : `str`, `pathlib` or `file` objects The name of the file to read (or a file objects). **kwargs Optional keyword parameters (see Other Parameters). Other Parameters ---------------- content : str, optional The optional content of the file(s) to be loaded as a binary string. See Also -------- read : Import dataset from various orgines. save : Save the current dataset. Notes ----- Adapted from `numpy.load` . Examples -------- >>> nd1 = scp.read('irdata/nh4y-activation.spg') >>> f = nd1.save() >>> f.name 'nh4y-activation.scp' >>> nd2 = scp.load(f) Alternatively, this method can be called as a class method of NDDataset or Project object: >>> from spectrochempy import * >>> nd2 = NDDataset.load(f) """ content = kwargs.get("content") if content: fid = io.BytesIO(content) else: # be sure to convert filename to a pathlib object with the # default suffix filename = pathclean(filename) suffix = cls().suffix filename = filename.with_suffix(suffix) if kwargs.get("directory") is not None: filename = pathclean(kwargs.get("directory")) / filename if not filename.exists()[source]ο
Open data from a β.scpβ (NDDataset) or β.pscpβ (Project) file.
- Parameters:
- Other Parameters:
content (str, optional) β The optional content of the file(s) to be loaded as a binary string.
Notes
Adapted from
numpy.load
.Examples
>>> nd1 = scp.read('irdata/nh4y-activation.spg') >>> f = nd1.save() >>> f.name 'nh4y-activation.scp' >>> nd2 = scp.load(f)
Alternatively, this method can be called as a class method of NDDataset or Project object:
>>> from spectrochempy import * >>> nd2 = NDDataset.load(f)
- classmethod loads(js: dict[str, Any]) -> Any: """ Deserialize dataset from JSON. Parameters ---------- js : dict[str, Any] JSON object to deserialize Returns ------- Any Deserialized dataset object Raises ------ TypeError If JSON cannot be properly deserialized """ from spectrochempy.core.dataset.coord import Coord from spectrochempy.core.dataset.coordset import CoordSet from spectrochempy.core.dataset.nddataset import NDDataset from spectrochempy.core.project.project import Project from spectrochempy.core.script import Script # ......................... def item_to_attr(obj: Any, dic: dict[str, Any]) -> Any: for key, val in dic.items()[source]ο
Deserialize dataset from JSON.
- Parameters:
js (dict[str, Any]) β JSON object to deserialize
- Returns:
Any β Deserialized dataset object
- Raises:
TypeError β If JSON cannot be properly deserialized
- remove_dataset(name)[source]ο
Remove a dataset from the project.
- Parameters:
name (str) β Name of the dataset to remove.
- remove_project(name)[source]ο
Remove one project from the current project.
- Parameters:
name (str) β Name of the project to remove.
- save(**kwargs: Any)[source]ο
Save dataset in native .scp format.
- Parameters:
**kwargs (Any) β Optional arguments passed to save_as()
- Returns:
Optional[pathlib.Path] β Path to saved file if successful, None if save failed
- save_as(filename: str = "", **kwargs: Any) -> pathlib.Path | None: """ Save the current NDDataset in SpectroChemPy format (.scp). Parameters ---------- filename : str The filename of the file where to save the current dataset. **kwargs Optional keyword parameters (see Other Parameters). Other Parameters ---------------- directory : str, optional If specified, the given `directory` and the `filename` will be appended. See Also -------- save : Save current dataset. write : Export current dataset to different format. Notes ----- Adapted from :class:`numpy.savez` . Examples -------- Read some data from an OMNIC file >>> nd = scp.read_omnic('wodger.spg') >>> assert nd.name == 'wodger' Write it in SpectroChemPy format (.scp) (return a `pathlib` object) >>> filename = nd.save_as('new_wodger') Check the existence of the scp file >>> assert filename.is_file() >>> assert filename.name == 'new_wodger.scp' Remove this file >>> filename.unlink() """ if filename: # we have a filename # by default it use the saved directory filename = pathclean(filename) if self.directory and self.directory != filename.parent: filename = self.directory / filename else: filename = self.directory # suffix must be specified which correspond to the type of the # object to save default_suffix = SCPY_SUFFIX[self._implements()] if filename is not None and not filename.is_dir()[source]ο
Save the current NDDataset in SpectroChemPy format (.scp).
- Parameters:
filename (str) β The filename of the file where to save the current dataset.
**kwargs β Optional keyword parameters (see Other Parameters).
- Other Parameters:
directory (str, optional) β If specified, the given
directory
and thefilename
will be appended.
Notes
Adapted from
numpy.savez
.Examples
Read some data from an OMNIC file
>>> nd = scp.read_omnic('wodger.spg') >>> assert nd.name == 'wodger'
Write it in SpectroChemPy format (.scp) (return a
pathlib
object)>>> filename = nd.save_as('new_wodger')
Check the existence of the scp file
>>> assert filename.is_file() >>> assert filename.name == 'new_wodger.scp'
Remove this file
>>> filename.unlink()
Examples using spectrochempy.Project