Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion brukerapi/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,14 @@ def __init__(self, path, **state):
raise FileNotFoundError(self.path)

# directory constructor
if self.path.is_dir() and state.get("load"):
if self.path.is_dir():
content = os.listdir(self.path)
if "fid" in content:
self.path = self.path / "fid"
elif "2dseq" in content:
self.path = self.path / "2dseq"
elif state.get("load") is LOAD_STAGES["empty"] and self.path.stem in DEFAULT_STATES:
pass
else:
raise NotADatasetDir(self.path)

Expand Down
15 changes: 8 additions & 7 deletions brukerapi/folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,18 +352,19 @@ def get_dataset(self, exp_id: str | None = None, proc_id: str | None = None) ->
:param proc_id: name of the processing folder
:return: fid, or 2dseq :obj:`.Dataset`
"""
if exp_id:
exp = self._get_exp(exp_id)
if exp_id is None:
raise ValueError("exp_id is required")

if proc_id:
exp = self._get_exp(exp_id)
if proc_id is not None:
return exp._get_proc(proc_id)["2dseq"]
return exp["fid"]

def _get_exp(self, exp_id):
for exp in self.experiment_list:
for exp in self.get_experiment_list():
if exp.path.name == exp_id:
return exp
return None
raise KeyError(f"Experiment '{exp_id}' not found in {self.path}")


class Experiment(Folder):
Expand Down Expand Up @@ -414,10 +415,10 @@ def validate(self):
raise NotExperimentFolder

def _get_proc(self, proc_id):
for proc in self.processing_list:
for proc in self.get_processing_list():
if proc.path.name == proc_id:
return proc
return None
raise KeyError(f"Processing '{proc_id}' not found in {self.path}")


class Processing(Folder):
Expand Down
21 changes: 10 additions & 11 deletions docs/source/tutorials/how-to-study.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ How to work with Bruker study?

.. code-block:: python

from brukerapi.study import Study
from brukerapi.folders import Study

study = Study('path_to_study')
study = Study('path_to_study')

#get list of scans (fid data sets) contained in the study
study.scans
# get list of experiments contained in the study
study.get_experiment_list()

#get list of recos (2dseq data sets) contained in the study
study.recos
# get list of processing folders contained in the study
study.get_processing_list()

#get data set from the study hierarchy
study.get_dataset(scan_id='2', reco_id='1')
# get a data set from the study hierarchy
study.get_dataset(exp_id='2', proc_id='1')

Data set obtained from ``Study`` object are empty by default, to access its content, the data set needs to be loaded. Either using the load function.

.. code-block:: python

dataset = study.get_dataset(scan_id='2', reco_id='1')
dataset = study.get_dataset(exp_id='2', proc_id='1')

dataset.load()
dataset.data
Expand All @@ -30,9 +30,8 @@ Or using context manager.

.. code-block:: python

with study.get_dataset(scan_id='2', reco_id='1') as dataset:
with study.get_dataset(exp_id='2', proc_id='1') as dataset:
dataset.data
dataset.get_value('VisuCoreSize')



14 changes: 14 additions & 0 deletions test/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ def test_unsupported_dataset_type(tmp_path):
Dataset(path)


@pytest.mark.parametrize(
("path", "dataset_type"),
[
("test/test_data/PV51/0.2H2/10", "fid"),
("test/test_data/PV51/0.2H2/10/pdata/1", "2dseq"),
],
)
def test_directory_constructor_uses_default_load(path, dataset_type):
dataset = Dataset(path)

assert dataset.type == dataset_type
assert dataset.data.size > 0


@pytest.mark.skip(reason="in progress")
def test_parameters(test_parameters):
dataset = Dataset(test_parameters[0], load=False)
Expand Down
21 changes: 20 additions & 1 deletion test/test_folders.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pathlib import Path

from brukerapi.dataset import Dataset
from brukerapi.folders import Folder, Processing
from brukerapi.folders import Folder, Processing, Study


def test_folder_traversal_skips_processed_spectra(tmp_path):
Expand Down Expand Up @@ -31,3 +33,20 @@ def test_folder_traversal_skips_processed_spectra(tmp_path):
processing = next(child for child in experiment.get_processing_list() if isinstance(child, Processing))
processing_datasets = {child.path.name for child in processing.children if isinstance(child, Dataset)}
assert processing_datasets == {"2dseq"}


def test_study_get_dataset_returns_fid_and_2dseq():
study = Study(
Path("test/test_data/PV51/0.2H2"),
dataset_state={"parameter_files": [], "property_files": [], "load": 0},
)

fid = study.get_dataset(exp_id="10")
reconstructed = study.get_dataset(exp_id="10", proc_id="1")

assert fid.path.name == "fid"
assert reconstructed.path.name == "2dseq"

with fid, reconstructed:
assert fid.data.size > 0
assert reconstructed.data.size > 0