Skip to content

Commit

Permalink
Rename get_next_sub_number and get_next_ses_number. (#359)
Browse files Browse the repository at this point in the history
* Rename 'get_next_sub_number' and 'get_next_ses_number'.

* Rename 'get_next_sub_or_ses_number' to 'get_next_sub_or_ses'.
  • Loading branch information
JoeZiminski authored Apr 10, 2024
1 parent 8c640a8 commit 85ee1cc
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 51 deletions.
12 changes: 6 additions & 6 deletions datashuttle/datashuttle.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,14 +844,14 @@ def get_existing_projects() -> List[Path]:
return getters.get_existing_project_paths()

@check_configs_set
def get_next_sub_number(
def get_next_sub(
self,
top_level_folder: TopLevelFolder,
return_with_prefix: bool = True,
local_only: bool = False,
) -> str:
"""
Convenience function for get_next_sub_or_ses_number
Convenience function for get_next_sub_or_ses
to find the next subject number.
Parameters
Expand All @@ -864,7 +864,7 @@ def get_next_sub_number(
If `True, only get names from `local_path`, otherwise from
`local_path` and `central_path`.
"""
return getters.get_next_sub_or_ses_number(
return getters.get_next_sub_or_ses(
self.cfg,
top_level_folder,
sub=None,
Expand All @@ -874,15 +874,15 @@ def get_next_sub_number(
)

@check_configs_set
def get_next_ses_number(
def get_next_ses(
self,
top_level_folder: TopLevelFolder,
sub: str,
return_with_prefix: bool = True,
local_only: bool = False,
) -> str:
"""
Convenience function for get_next_sub_or_ses_number
Convenience function for get_next_sub_or_ses
to find the next session number.
Parameters
Expand All @@ -901,7 +901,7 @@ def get_next_ses_number(
If `True, only get names from `local_path`, otherwise from
`local_path` and `central_path`.
"""
return getters.get_next_sub_or_ses_number(
return getters.get_next_sub_or_ses(
self.cfg,
top_level_folder,
sub=sub,
Expand Down
8 changes: 4 additions & 4 deletions datashuttle/tui/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,23 +372,23 @@ def get_textual_compatible_project_configs(self) -> Configs:
cfg_to_load.convert_str_and_pathlib_paths(cfg_to_load, "path_to_str")
return cfg_to_load

def get_next_sub_number(
def get_next_sub(
self, top_level_folder: TopLevelFolder
) -> InterfaceOutput:
try:
next_sub = self.project.get_next_sub_number(
next_sub = self.project.get_next_sub(
top_level_folder, return_with_prefix=True, local_only=True
)
return True, next_sub
except BaseException as e:
return False, str(e)

def get_next_ses_number(
def get_next_ses(
self, top_level_folder: TopLevelFolder, sub: str
) -> InterfaceOutput:

try:
next_ses = self.project.get_next_ses_number(
next_ses = self.project.get_next_ses(
top_level_folder, sub, return_with_prefix=True, local_only=True
)
return True, next_ses
Expand Down
6 changes: 2 additions & 4 deletions datashuttle/tui/tabs/create_folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,7 @@ def fill_input_with_next_sub_or_ses_template(
]["create_tab"]

if prefix == "sub":
success, output = self.interface.get_next_sub_number(
top_level_folder
)
success, output = self.interface.get_next_sub(top_level_folder)
if not success:
self.mainwindow.show_modal_error_dialog(output)
return
Expand Down Expand Up @@ -306,7 +304,7 @@ def fill_input_with_next_sub_or_ses_template(
else:
sub = sub_names[0]

success, output = self.interface.get_next_ses_number(
success, output = self.interface.get_next_ses(
top_level_folder, sub
)
if not success:
Expand Down
4 changes: 2 additions & 2 deletions datashuttle/utils/getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
)


def get_next_sub_or_ses_number(
def get_next_sub_or_ses(
cfg: Configs,
top_level_folder,
sub: Optional[str],
Expand Down Expand Up @@ -124,7 +124,7 @@ def get_max_sub_or_ses_num_and_value_length(
all_folders : List[str]
A list of BIDS-style formatted folder names.
see `get_next_sub_or_ses_number()` for other arguments.
see `get_next_sub_or_ses()` for other arguments.
Returns
-------
Expand Down
8 changes: 4 additions & 4 deletions docs/source/pages/tutorials/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,17 +344,17 @@ If the folder names will break with the specification, an error will be
raised and the folders will not be created.
```

Two useful methods to automate folder creation are `get_next_sub_number()` and
`get_next_ses_number()`. These can be used to automatically get the next subject
Two useful methods to automate folder creation are `get_next_sub()` and
`get_next_ses()`. These can be used to automatically get the next subject
and session names in a project.

For example, to get the next subject
in this project (`sub-002`) and the next session for that subject (in this case,
as it is the first session for `sub-002`, it will be `ses-001`) we can run

```python
next_sub = project.get_next_sub_number("rawdata", local_only=True) # returns "sub-001"
next_ses = project.get_next_ses_number("rawdata", sub=next_sub, local_only=True) # returns "ses-001"
next_sub = project.get_next_sub("rawdata", local_only=True) # returns "sub-001"
next_ses = project.get_next_ses("rawdata", sub=next_sub, local_only=True) # returns "ses-001"

project.create_folders(
"rawdata",
Expand Down
44 changes: 15 additions & 29 deletions tests/tests_integration/test_create_folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,7 @@ def test_all_top_level_folders(self, project, top_level_folder):

@pytest.mark.parametrize("top_level_folder", ["rawdata", "derivatives"])
@pytest.mark.parametrize("return_with_prefix", [True, False])
def test_get_next_sub_number(
self, project, return_with_prefix, top_level_folder
):
def test_get_next_sub(self, project, return_with_prefix, top_level_folder):
"""
Test that the next subject number is suggested correctly.
This takes the union of subjects available in the local and
Expand All @@ -297,9 +295,7 @@ def test_get_next_sub_number(
project, top_level_folder, ["001", "002", "003"]
)

new_num = project.get_next_sub_number(
top_level_folder, return_with_prefix
)
new_num = project.get_next_sub(top_level_folder, return_with_prefix)

assert new_num == "sub-004" if return_with_prefix else "004"

Expand All @@ -308,38 +304,32 @@ def test_get_next_sub_number(

shutil.rmtree(project.cfg["local_path"] / top_level_folder)

new_num = project.get_next_sub_number(
top_level_folder, return_with_prefix
)
new_num = project.get_next_sub(top_level_folder, return_with_prefix)
assert new_num == "sub-004" if return_with_prefix else "004"

# Add large-sub num folders to local and check all are detected.
project.create_folders(top_level_folder, ["004", "005"])

new_num = project.get_next_sub_number(
top_level_folder, return_with_prefix
)
new_num = project.get_next_sub(top_level_folder, return_with_prefix)
assert new_num == "sub-006" if return_with_prefix else "006"

# check `local_path` option
os.makedirs(project.cfg["central_path"] / top_level_folder / "sub-006")
new_num = project.get_next_sub_number(
new_num = project.get_next_sub(
top_level_folder, return_with_prefix, local_only=False
)
assert new_num == "sub-007" if return_with_prefix else "007"

new_num = project.get_next_sub_number(
new_num = project.get_next_sub(
top_level_folder, return_with_prefix, local_only=True
)
assert new_num == "sub-006" if return_with_prefix else "006"

@pytest.mark.parametrize("top_level_folder", ["rawdata", "derivatives"])
@pytest.mark.parametrize("return_with_prefix", [True, False])
def test_get_next_ses_number(
self, project, return_with_prefix, top_level_folder
):
def test_get_next_ses(self, project, return_with_prefix, top_level_folder):
"""
Almost identical to test_get_next_sub_number() but with calls
Almost identical to test_get_next_sub() but with calls
for searching sessions. This could be combined with
above but reduces readability, so leave with some duplication.
Expand All @@ -353,12 +343,10 @@ def test_get_next_ses_number(
)

# Test the next sub and ses number are correct
new_num = project.get_next_sub_number(
top_level_folder, return_with_prefix
)
new_num = project.get_next_sub(top_level_folder, return_with_prefix)
assert new_num == "sub-10" if return_with_prefix else "10"

new_num = project.get_next_ses_number(
new_num = project.get_next_ses(
top_level_folder, sub, return_with_prefix
)
assert new_num == "ses-004" if return_with_prefix else "004"
Expand All @@ -369,12 +357,10 @@ def test_get_next_ses_number(

shutil.rmtree(project.cfg["local_path"] / top_level_folder)

new_num = project.get_next_sub_number(
top_level_folder, return_with_prefix
)
new_num = project.get_next_sub(top_level_folder, return_with_prefix)
assert new_num == "sub-10" if return_with_prefix else "10"

new_num = project.get_next_ses_number(
new_num = project.get_next_ses(
top_level_folder, sub, return_with_prefix
)
assert new_num == "ses-004" if return_with_prefix else "004"
Expand All @@ -383,7 +369,7 @@ def test_get_next_ses_number(
# the next session is updated accordingly.
project.create_folders(top_level_folder, sub, ["004", "005"])

new_num = project.get_next_ses_number(
new_num = project.get_next_ses(
top_level_folder, sub, return_with_prefix
)
assert new_num == "ses-006" if return_with_prefix else "006"
Expand All @@ -392,12 +378,12 @@ def test_get_next_ses_number(
os.makedirs(
project.cfg["central_path"] / top_level_folder / sub / "ses-006"
)
new_num = project.get_next_ses_number(
new_num = project.get_next_ses(
top_level_folder, sub, return_with_prefix, local_only=False
)
assert new_num == "ses-007" if return_with_prefix else "007"

new_num = project.get_next_ses_number(
new_num = project.get_next_ses(
top_level_folder, sub, return_with_prefix, local_only=True
)
assert new_num == "ses-006" if return_with_prefix else "006"
Expand Down
4 changes: 2 additions & 2 deletions tests/tests_integration/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ def test_warning_non_consecutive_numbers(self, project, top_level_folder):
)

with pytest.warns(UserWarning) as w:
project.get_next_sub_number(top_level_folder)
project.get_next_sub(top_level_folder)
assert (
str(w[0].message) == "A subject number has been skipped, "
"currently used subject numbers are: [1, 2, 4]"
)

with pytest.warns(UserWarning) as w:
project.get_next_ses_number(top_level_folder, "sub-02")
project.get_next_ses(top_level_folder, "sub-02")
assert (
str(w[0].message)
== "A subject number has been skipped, currently "
Expand Down

0 comments on commit 85ee1cc

Please sign in to comment.