Skip to content

Commit

Permalink
Merge pull request #627 from kayla-leonard/add_recursive_flag_to_data…
Browse files Browse the repository at this point in the history
…converter2

 Add optional recursive flag to data converter.
  • Loading branch information
kayla-leonard authored Nov 9, 2023
2 parents baf5e37 + 01cebf7 commit 3b32be9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/graphnet/data/dataconverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,22 @@ def __init__(
super().__init__(name=__name__, class_name=self.__class__.__name__)

@final
def __call__(self, directories: Union[str, List[str]]) -> None:
def __call__(
self,
directories: Union[str, List[str]],
recursive: Optional[bool] = True,
) -> None:
"""Convert I3-files in `directories.
Args:
directories: One or more directories, the I3 files within which
should be converted to an intermediate file format.
recursive: Whether or not to search the directories recursively.
"""
# Find all I3 and GCD files in the specified directories.
i3_files, gcd_files = find_i3_files(directories, self._gcd_rescue)
i3_files, gcd_files = find_i3_files(
directories, self._gcd_rescue, recursive
)
if len(i3_files) == 0:
self.error(f"No files found in {directories}.")
return
Expand Down
12 changes: 9 additions & 3 deletions src/graphnet/utilities/filesys.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def has_extension(filename: str, extensions: List[str]) -> bool:


def find_i3_files(
directories: Union[str, List[str]], gcd_rescue: Optional[str] = None
directories: Union[str, List[str]],
gcd_rescue: Optional[str] = None,
recursive: Optional[bool] = True,
) -> Tuple[List[str], List[str]]:
"""Find I3 files and corresponding GCD files in `directories`.
Expand All @@ -43,6 +45,7 @@ def find_i3_files(
directories: Directories to search recursively for I3 files.
gcd_rescue: Path to the GCD that will be default if no GCD is present
in the directory.
recursive: Whether or not to search the directories recursively.
Returns:
i3_list: Paths to I3 files in `directories`
Expand All @@ -57,11 +60,14 @@ def find_i3_files(

for directory in directories:

# Recursively find all I3-like files in `directory`.
# Find all I3-like files in `directory`, may or may not be recursively.
paths = []
i3_patterns = ["*.bz2", "*.zst", "*.gz"]
for i3_pattern in i3_patterns:
paths.extend(list(Path(directory).rglob(i3_pattern)))
if recursive:
paths.extend(list(Path(directory).rglob(i3_pattern)))
else:
paths.extend(list(Path(directory).glob(i3_pattern)))

# Loop over all folders containing such I3-like files.
folders = sorted(set([path.parent for path in paths]))
Expand Down

0 comments on commit 3b32be9

Please sign in to comment.