I am writing a data analysis pipeline. I have the following directory structure, where each folder (image1, image2, ... image100) within "data" contains information of a 3D image:
- data_pipeline.py
- data
image1
- raw_data
- slice1
- slice2
- ...
- processed_data
- image_specific_codes
- init.py
- open_slices.py (containing unpack_slices() function for image1)
- raw_data
image2
- raw_data
- slice1_to_5
- slice6_to_10
- ...
- processed_data
- image_specific_codes
- init.py
- open_slices.py (containing unpack_slices() function for image2)
- raw_data
The analysis pipeline is the same for each 3D image (image1, image2) for the most part, other than unpacking the raw data. I am looking for a way to create a general pipeline, data_pipeline.py, that loops through all the folders in "data" (image1, image2, etc.), unpacks them according to open_slices.py, and runs through the rest of the pipeline.
I have tried using importlib to dynamically import specific functions, for example:
import os
import importlib
for i_image in os.listdir():
os.chdir(i_image)
module = importlib.import_module('.open_slices', package='image_specific_codes')
unpack_fn = getattr(module, 'unpack_slices')
unpacked_image = unpack_fn()
# At this point, unpacked_image is in a consistent format for the analysis pipeline
# ...
but I am never able to import the modules successfully, getting errors such as the one below:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'image_specific_codes'