Skip to content
Open
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
1 change: 1 addition & 0 deletions cdisc_rules_engine/models/operation_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ class OperationParams:
value_is_reference: bool = False
namespace: str = None
delimiter: str = None
define_xml_path: str = None
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,15 @@ def _execute_operation(self) -> bool:
whodrug_path=self.params.whodrug_path,
loinc_path=self.params.loinc_path,
)
define_path = (
self.params.define_xml_path
if self.params.define_xml_path
else os.path.join(self.params.directory_path, DEFINE_XML_FILE_NAME)
)
if not os.path.exists(define_path):
raise FileNotFoundError(f"Define XML file {define_path} not found")
define_contents = self.data_service.get_define_xml_contents(
dataset_name=os.path.join(self.params.directory_path, DEFINE_XML_FILE_NAME)
dataset_name=define_path
)
define_reader = DefineXMLReaderFactory.from_file_contents(define_contents)
define_dictionary_version = define_reader.get_external_dictionary_version(
Expand Down
14 changes: 11 additions & 3 deletions cdisc_rules_engine/operations/define_variable_metadata.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from cdisc_rules_engine.constants.define_xml_constants import DEFINE_XML_FILE_NAME
import os.path

from cdisc_rules_engine.services.define_xml.define_xml_reader_factory import (
DefineXMLReaderFactory,
)
from .base_operation import BaseOperation
import os
from cdisc_rules_engine.constants.define_xml_constants import DEFINE_XML_FILE_NAME


class DefineVariableMetadata(BaseOperation):
Expand Down Expand Up @@ -32,8 +33,15 @@ def _execute_operation(self):
...
}
"""
define_path = (
self.params.define_xml_path
if self.params.define_xml_path
else os.path.join(self.params.directory_path, DEFINE_XML_FILE_NAME)
)
if not os.path.exists(define_path):
raise FileNotFoundError(f"Define XML file {define_path} not found")
define_contents = self.data_service.get_define_xml_contents(
dataset_name=os.path.join(self.params.directory_path, DEFINE_XML_FILE_NAME)
dataset_name=define_path
)
define_reader = DefineXMLReaderFactory.from_file_contents(define_contents)
Comment on lines +36 to 46
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This chunk should be moved to a BaseOperation function so that it can be reused

variables_metadata = define_reader.extract_variables_metadata(
Expand Down
1 change: 1 addition & 0 deletions cdisc_rules_engine/rules_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ def execute_rule(
standard_substandard=self.standard_substandard,
external_dictionaries=self.external_dictionaries,
ct_packages=ct_packages,
define_xml_path=self.define_xml_path,
)
dataset_variable = DatasetVariable(
dataset,
Expand Down
6 changes: 4 additions & 2 deletions cdisc_rules_engine/utilities/rule_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,12 @@ def rule_applies_to_class(
excluded_classes = classes.get("Exclude", [])
is_included = True
is_excluded = False
dataset_name = dataset_metadata.full_path
if included_classes:
if ALL_KEYWORD in included_classes:
return True
variables = self.data_service.get_variables_metadata(
dataset_name=dataset_metadata.full_path, datasets=datasets
dataset_name=dataset_name, datasets=datasets
).data.variable_name
class_name = self.data_service.get_dataset_class(
variables,
Expand All @@ -252,7 +253,7 @@ def rule_applies_to_class(
is_included = False
if excluded_classes:
variables = self.data_service.get_variables_metadata(
dataset_name=dataset_metadata.full_path, datasets=datasets
dataset_name=dataset_name, datasets=datasets
).data.variable_name
class_name = self.data_service.get_dataset_class(
variables,
Expand Down Expand Up @@ -367,6 +368,7 @@ def perform_rule_operations(
for ct_package_type in operation.get("ct_package_types", [])
],
ct_version=operation.get("version"),
define_xml_path=kwargs.get("define_xml_path"),
dataframe=dataset_copy,
dataset_path=dataset_path,
datasets=datasets,
Expand Down
186 changes: 186 additions & 0 deletions tests/QARegressionTests/test_Issues/test_CoreIssue1248.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add a test with no -dxp flag supplied to verify the case of getting define from dataset path also.

Copy link
Collaborator Author

@alexfurmenkov alexfurmenkov Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not work without the -dxp flag until changes are made to ExcelDataService related to comment

or changes to self.rule_processor.perform_rule_operations call should be made, so dataset_metadata.full_path containing in our case relrec.xpt value will pass something else

Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import os
import subprocess

import pytest
import json
from conftest import get_python_executable


@pytest.mark.regression
class TestCoreIssue1248:
@pytest.mark.parametrize(
"command,rules_report,num_issues",
[
# define path provided will lead to successful execution
(
[
f"{get_python_executable()}",
"-m",
"core",
"validate",
"-s",
"sdtmig",
"-v",
"3-2",
"-of",
"JSON",
"-lr",
os.path.join("tests", "resources", "CoreIssue1248", "sample.yml"),
"-cs",
"-dxp",
os.path.join(
"tests",
"resources",
"CoreIssue1248",
"define_subfolder",
"define.xml",
),
"-ps",
"1",
"-dp",
os.path.join("tests", "resources", "CoreIssue1248", "data.xlsx"),
],
[
{
"core_id": "SD1129",
"version": "1",
"cdisc_rule_id": "",
"fda_rule_id": "",
"message": "TEST",
"status": "ISSUE REPORTED",
}
],
2,
),
# JSON data file and no define.xml in same folder and no -dxp param will provide error
(
[
f"{get_python_executable()}",
"-m",
"core",
"validate",
"-s",
"sdtmig",
"-v",
"3-2",
"-of",
"JSON",
"-lr",
os.path.join("tests", "resources", "CoreIssue1248", "sample.yml"),
"-cs",
"-dp",
os.path.join("tests", "resources", "CoreIssue1248", "relrec.json"),
"-ps",
"1",
],
[
{
"core_id": "SD1129",
"version": "1",
"cdisc_rule_id": "",
"fda_rule_id": "",
"message": "TEST",
"status": "EXECUTION ERROR",
}
],
1,
),
# no define.xml in same path as data.xlsx file will provide error
(
[
f"{get_python_executable()}",
"-m",
"core",
"validate",
"-s",
"sdtmig",
"-v",
"3-2",
"-of",
"JSON",
"-lr",
os.path.join("tests", "resources", "CoreIssue1248", "sample.yml"),
"-cs",
"-dp",
os.path.join("tests", "resources", "CoreIssue1248", "data.xlsx"),
"-ps",
"1",
],
[
{
"core_id": "SD1129",
"version": "1",
"cdisc_rule_id": "",
"fda_rule_id": "",
"message": "TEST",
"status": "EXECUTION ERROR",
}
],
1,
),
# define.xml in same folder as the data.xls and no -dxp provided will provide error until
# in ExcelDataService dataset metadata creation full_path=dataset_name
(
[
f"{get_python_executable()}",
"-m",
"core",
"validate",
"-s",
"sdtmig",
"-v",
"3-2",
"-of",
"JSON",
"-lr",
os.path.join("tests", "resources", "CoreIssue1248", "sample.yml"),
"-cs",
"-dp",
os.path.join(
"tests",
"resources",
"CoreIssue1248",
"data_and_define",
"data.xlsx",
),
"-ps",
"1",
],
[
{
"core_id": "SD1129",
"version": "1",
"cdisc_rule_id": "",
"fda_rule_id": "",
"message": "TEST",
"status": "EXECUTION ERROR",
}
],
1,
),
],
)
def test_define_path_used(self, command, rules_report, num_issues):
subprocess.run(command, check=True)

# Get the latest created report file
files = os.listdir()
json_files = [
file
for file in files
if file.startswith("CORE-Report-") and file.endswith(".json")
]
json_report_path = sorted(json_files)[-1]
# Open the JSON report file
json_report = json.load(open(json_report_path))
assert {
"Conformance_Details",
"Dataset_Details",
"Issue_Summary",
"Issue_Details",
"Rules_Report",
}.issubset(json_report.keys())
assert len(json_report["Issue_Details"]) == num_issues
assert json_report["Rules_Report"] == rules_report
if os.path.exists(json_report_path):
os.remove(json_report_path)
Binary file added tests/resources/CoreIssue1248/data.xlsx
Binary file not shown.
Binary file not shown.
Loading
Loading