Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement tmp_path #74

Merged
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
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[run]
concurrency = multiprocessing
parallel = true
Empty file added test/__init__.py
Empty file.
13 changes: 3 additions & 10 deletions test/basic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,11 @@ def run_experiment(image, roi, **kwargs):
return data


def test_xml_configurations():
def test_xml_configurations(tmp_path):
# Read the data settings xml file, and update path to image and mask.
from xml.etree import ElementTree as ElemTree
from mirp.extract_features_and_images import extract_features

# Remove temporary data xml file if it exists.
if os.path.exists(os.path.join(CURRENT_DIR, "data", "configuration_files", "temp_test_config_data.xml")):
os.remove(os.path.join(CURRENT_DIR, "data", "configuration_files", "temp_test_config_data.xml"))

# Load xml.
tree = ElemTree.parse(os.path.join(CURRENT_DIR, "data", "configuration_files", "test_config_data.xml"))
paths_branch = tree.getroot()
Expand All @@ -144,12 +140,12 @@ def test_xml_configurations():
mask.text = str(os.path.join(CURRENT_DIR, "data", "sts_images"))

# Save as temporary xml file.
tree.write(os.path.join(CURRENT_DIR, "data", "configuration_files", "temp_test_config_data.xml"))
tree.write(tmp_path / "temp_test_config_data.xml")

data = extract_features(
write_features=False,
export_features=True,
image=os.path.join(CURRENT_DIR, "data", "configuration_files", "temp_test_config_data.xml"),
image=str(tmp_path / "temp_test_config_data.xml"),
settings=os.path.join(CURRENT_DIR, "data", "configuration_files", "test_config_settings.xml")
)

Expand All @@ -162,9 +158,6 @@ def test_xml_configurations():
assert all(data["image_voxel_size_y"].values == 3.0)
assert all(data["image_voxel_size_z"].values == 3.0)

# Clean up
os.remove(os.path.join(CURRENT_DIR, "data", "configuration_files", "temp_test_config_data.xml"))


def test_edge_cases_basic_pipeline():
"""
Expand Down
84 changes: 21 additions & 63 deletions test/configuration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@ def _type_converter(type_str: str):
raise ValueError(f"type could not be linked to an object type: {type_str}")


def test_general_settings_configuration():
def test_general_settings_configuration(tmp_path):
from xml.etree import ElementTree as ElemTree
from mirp import get_settings_xml
from mirp.settings.general_parameters import get_general_settings
from mirp.settings.import_config_parameters import create_settings_object
from mirp.settings.generic import SettingsClass

temp_file = os.path.join(CURRENT_DIR, "data", "configuration_files", "settings.xml")
temp_file = tmp_path / "settings.xml"

# Remove temporary data xml file if it exists.
if os.path.exists(temp_file):
os.remove(temp_file)
get_settings_xml(os.path.join(CURRENT_DIR, "data", "configuration_files"))
get_settings_xml(tmp_path)

settings_definitions = get_general_settings()

Expand Down Expand Up @@ -80,23 +77,17 @@ def test_general_settings_configuration():
assert getattr(settings_keyword.general, class_key) == test_value
assert isinstance(test_value, value_type)

if os.path.exists(temp_file):
os.remove(temp_file)


def test_post_processing_settings_configuration():
def test_post_processing_settings_configuration(tmp_path):
from xml.etree import ElementTree as ElemTree
from mirp import get_settings_xml
from mirp.settings.image_processing_parameters import get_post_processing_settings
from mirp.settings.import_config_parameters import create_settings_object
from mirp.settings.generic import SettingsClass

temp_file = os.path.join(CURRENT_DIR, "data", "configuration_files", "settings.xml")
temp_file = tmp_path / "settings.xml"

# Remove temporary data xml file if it exists.
if os.path.exists(temp_file):
os.remove(temp_file)
get_settings_xml(os.path.join(CURRENT_DIR, "data", "configuration_files"))
get_settings_xml(tmp_path)

settings_definitions = get_post_processing_settings()

Expand Down Expand Up @@ -157,23 +148,17 @@ def test_post_processing_settings_configuration():
assert getattr(settings_keyword.post_process, class_key) == test_value
assert isinstance(test_value, value_type)

if os.path.exists(temp_file):
os.remove(temp_file)


def test_interpolation_settings_configuration():
def test_interpolation_settings_configuration(tmp_path):
from xml.etree import ElementTree as ElemTree
from mirp import get_settings_xml
from mirp.settings.interpolation_parameters import get_image_interpolation_settings, get_mask_interpolation_settings
from mirp.settings.import_config_parameters import create_settings_object
from mirp.settings.generic import SettingsClass

temp_file = os.path.join(CURRENT_DIR, "data", "configuration_files", "settings.xml")
temp_file = tmp_path / "settings.xml"

# Remove temporary data xml file if it exists.
if os.path.exists(temp_file):
os.remove(temp_file)
get_settings_xml(os.path.join(CURRENT_DIR, "data", "configuration_files"))
get_settings_xml(tmp_path)

# All default settings.
tree = ElemTree.parse(temp_file)
Expand Down Expand Up @@ -270,23 +255,17 @@ def test_interpolation_settings_configuration():
assert getattr(settings_keyword.roi_interpolate, class_key) == test_value
assert isinstance(test_value, value_type)

if os.path.exists(temp_file):
os.remove(temp_file)


def test_perturbation_settings_configuration():
def test_perturbation_settings_configuration(tmp_path):
from xml.etree import ElementTree as ElemTree
from mirp import get_settings_xml
from mirp.settings.perturbation_parameters import get_perturbation_settings
from mirp.settings.import_config_parameters import create_settings_object
from mirp.settings.generic import SettingsClass

temp_file = os.path.join(CURRENT_DIR, "data", "configuration_files", "settings.xml")
temp_file = tmp_path / "settings.xml"

# Remove temporary data xml file if it exists.
if os.path.exists(temp_file):
os.remove(temp_file)
get_settings_xml(os.path.join(CURRENT_DIR, "data", "configuration_files"))
get_settings_xml(tmp_path)

settings_definitions = get_perturbation_settings()

Expand Down Expand Up @@ -347,23 +326,17 @@ def test_perturbation_settings_configuration():
assert getattr(settings_keyword.perturbation, class_key) == test_value
assert isinstance(test_value, value_type)

if os.path.exists(temp_file):
os.remove(temp_file)


def test_mask_resegmentation_settings_configuration():
def test_mask_resegmentation_settings_configuration(tmp_path):
from xml.etree import ElementTree as ElemTree
from mirp import get_settings_xml
from mirp.settings.resegmentation_parameters import get_mask_resegmentation_settings
from mirp.settings.import_config_parameters import create_settings_object
from mirp.settings.generic import SettingsClass

temp_file = os.path.join(CURRENT_DIR, "data", "configuration_files", "settings.xml")
temp_file = tmp_path / "settings.xml"

# Remove temporary data xml file if it exists.
if os.path.exists(temp_file):
os.remove(temp_file)
get_settings_xml(os.path.join(CURRENT_DIR, "data", "configuration_files"))
get_settings_xml(tmp_path)

settings_definitions = get_mask_resegmentation_settings()

Expand Down Expand Up @@ -424,23 +397,17 @@ def test_mask_resegmentation_settings_configuration():
assert getattr(settings_keyword.roi_resegment, class_key) == test_value
assert isinstance(test_value, value_type)

if os.path.exists(temp_file):
os.remove(temp_file)


def test_feature_extraction_settings_configuration():
def test_feature_extraction_settings_configuration(tmp_path):
from xml.etree import ElementTree as ElemTree
from mirp import get_settings_xml
from mirp.settings.feature_parameters import get_feature_extraction_settings
from mirp.settings.import_config_parameters import create_settings_object
from mirp.settings.generic import SettingsClass

temp_file = os.path.join(CURRENT_DIR, "data", "configuration_files", "settings.xml")
temp_file = tmp_path / "settings.xml"

# Remove temporary data xml file if it exists.
if os.path.exists(temp_file):
os.remove(temp_file)
get_settings_xml(os.path.join(CURRENT_DIR, "data", "configuration_files"))
get_settings_xml(tmp_path)

settings_definitions = get_feature_extraction_settings()

Expand Down Expand Up @@ -504,23 +471,17 @@ def test_feature_extraction_settings_configuration():
assert getattr(settings_keyword.feature_extr, class_key) == test_value
assert isinstance(test_value, value_type)

if os.path.exists(temp_file):
os.remove(temp_file)


def test_image_transformation_settings_configuration():
def test_image_transformation_settings_configuration(tmp_path):
from xml.etree import ElementTree as ElemTree
from mirp import get_settings_xml
from mirp.settings.transformation_parameters import get_image_transformation_settings
from mirp.settings.import_config_parameters import create_settings_object
from mirp.settings.generic import SettingsClass

temp_file = os.path.join(CURRENT_DIR, "data", "configuration_files", "settings.xml")
temp_file = tmp_path / "settings.xml"

# Remove temporary data xml file if it exists.
if os.path.exists(temp_file):
os.remove(temp_file)
get_settings_xml(os.path.join(CURRENT_DIR, "data", "configuration_files"))
get_settings_xml(tmp_path)

settings_definitions = get_image_transformation_settings()

Expand Down Expand Up @@ -597,6 +558,3 @@ def test_image_transformation_settings_configuration():
else:
assert getattr(settings_keyword.img_transform, class_key) == test_value
assert isinstance(test_value, value_type)

if os.path.exists(temp_file):
os.remove(temp_file)
17 changes: 4 additions & 13 deletions test/export_image_and_mask_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,28 @@
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))


def test_basic_ct_feature_extraction():
import shutil

write_dir = os.path.join(CURRENT_DIR, "data", "temp_data")
if os.path.exists(write_dir):
shutil.rmtree(write_dir)

def test_basic_ct_feature_extraction(tmp_path):
image = np.zeros((128, 128), dtype=float)
image[32:64, 32:64] = 1.0

mask = np.zeros((128, 128), dtype=bool)
mask[48:64, 48:64] = True

data = extract_features_and_images(
extract_features_and_images(
write_features=True,
export_features=True,
write_images=True,
export_images=False,
write_dir=write_dir,
write_dir=tmp_path,
image=image,
mask=mask,
base_feature_families="statistics"
)

# Check that files exist.
file_names = [file for file in os.listdir(write_dir) if os.path.isfile(os.path.join(write_dir, file))]
file_names = [file for file in os.listdir(tmp_path) if (tmp_path / file).is_file()]

assert len(file_names) == 3
assert len([file for file in file_names if file.endswith(".csv")]) == 1
assert len([file for file in file_names if file.endswith(".nii.gz")]) == 2
assert len([file for file in file_names if "mask" in file]) == 1

if os.path.exists(write_dir):
shutil.rmtree(write_dir)
Loading