Skip to content

Commit ce23a28

Browse files
authored
Merge pull request #479 from zivy/optionForConsistentOutputOnAllPlatforms
Add option to specify floating point precision.
2 parents 5afc2c9 + 591e720 commit ce23a28

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

Python/scripts/characterize_data.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,12 @@ def xyz_to_index(x, y, z, thumbnail_size, tile_size):
993993
default="sitkNearestNeighbor",
994994
help="SimpleITK interpolator used to resize images when creating summary image",
995995
)
996+
opt_arg_parser.add_argument(
997+
"--float_precision",
998+
type=positive_int,
999+
default=None,
1000+
help="Precision for floating point numbers. Use only if exact numeric equality across platforms is required (i.e. for testing)",
1001+
)
9961002
# Use the function docstring as the text in the parser description and a custom
9971003
# RawDescriptionAndDefaultHelpFormatter so that the docstring layout
9981004
# is maintained, otherwise it is line-wrapped and the formatting is lost, and the
@@ -1141,7 +1147,21 @@ def xyz_to_index(x, y, z, thumbnail_size, tile_size):
11411147
if args.ignore_problems:
11421148
df.dropna(inplace=True, thresh=2)
11431149
# save the raw information, create directory structure if it doesn't exist
1144-
df.to_csv(args.output_file, index=False)
1150+
# if floating point precision was specified, convert the floating point tuples to the
1151+
# desired precision. the dataframe's to_csv method will format the columns with floating point type.
1152+
float_format_str = None
1153+
if args.float_precision:
1154+
float_format_str = f"%.{args.float_precision}f"
1155+
df["image spacing"] = df["image spacing"].apply(
1156+
lambda x: np.round(x, decimals=args.float_precision)
1157+
)
1158+
df["image origin"] = df["image origin"].apply(
1159+
lambda x: np.round(x, decimals=args.float_precision)
1160+
)
1161+
df["axis direction "] = df["axis direction"].apply(
1162+
lambda x: np.round(x, decimals=args.float_precision)
1163+
)
1164+
df.to_csv(args.output_file, index=False, float_format=float_format_str)
11451165

11461166
# minimal analysis on the image information, detect image duplicates and plot the image size,
11471167
# spacing and min/max intensity values of scalar image distributions as scatterplots.

tests/test_scripts.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ def files_md5(self, ascii_file_list, binary_file_list):
4747
"per_file_data_characteristics.csv",
4848
"per_file",
4949
"characterize_data_user_defaults.json",
50-
"fb0338866794ef68c5d5854399ccd22c",
50+
"561519272943948754e5a78a043b66dd",
5151
),
5252
(
5353
"per_series_data_characteristics.csv",
5454
"per_series",
5555
"characterize_data_user_defaults.json",
56-
"766184c8503a2f08cac6e3b6be57e346",
56+
"9c6aa0ff16e78f7e3d808531caf8cd91",
5757
),
5858
],
5959
)
@@ -79,6 +79,8 @@ def test_characterize_data(
7979
analysis_type,
8080
"--configuration_file",
8181
str(self.data_path / user_configuration),
82+
"--float_precision",
83+
"3",
8284
]
8385
)
8486
# csv files needs to be modified as follows before comparing to expected values:

0 commit comments

Comments
 (0)