From 58f6cb7688e00b98ee2dbf85f1d8a9b2b95aa8b0 Mon Sep 17 00:00:00 2001 From: Zack Singer Date: Fri, 6 Feb 2026 12:24:54 -0500 Subject: [PATCH 1/8] Remove all used print() statements. Resolves #350 --- hexrd/core/instrument/hedm_instrument.py | 8 ++--- hexrd/core/material/material.py | 23 --------------- hexrd/core/material/symbols.py | 11 +++---- hexrd/core/valunits.py | 29 ------------------- scripts/install/install_build_dependencies.py | 2 +- tests/fit_grains_check.py | 4 --- 6 files changed, 11 insertions(+), 66 deletions(-) diff --git a/hexrd/core/instrument/hedm_instrument.py b/hexrd/core/instrument/hedm_instrument.py index bfc82ad2f..370f01bb0 100644 --- a/hexrd/core/instrument/hedm_instrument.py +++ b/hexrd/core/instrument/hedm_instrument.py @@ -2257,7 +2257,7 @@ def __init__(self, filename): self.fid = filename else: self.fid = open(filename, 'w') - print(self._header, file=self.fid) + logger.info(self._header, file=self.fid) def __del__(self): self.close() @@ -2294,7 +2294,7 @@ def dump_patch( self._delim.join(np.tile('{:<23.16e}', 10)).format(*res[7:]), ] ) - print(output_str, file=self.fid) + logger.info(output_str, file=self.fid) return output_str @@ -2349,7 +2349,7 @@ def __init__(self, filename=None, array=None): self.fid = filename else: self.fid = open(filename, 'w') - print(self._header, file=self.fid) + logger.info(self._header, file=self.fid) def __del__(self): self.close() @@ -2385,7 +2385,7 @@ def dump_grain(self, grain_id, completeness, chisq, grain_params): self._delim.join(np.tile('{:<23.16e}', len(res) - 3)).format(*res[3:]), ] ) - print(output_str, file=self.fid) + logger.info(output_str, file=self.fid) return output_str diff --git a/hexrd/core/material/material.py b/hexrd/core/material/material.py index 587178317..656068474 100644 --- a/hexrd/core/material/material.py +++ b/hexrd/core/material/material.py @@ -1562,26 +1562,3 @@ def get_hkl_strings(pdata): 'return_indices': True, } return np.intersect1d(**kwargs)[1:] - - -# -# ============================== Executable section for testing -# - - -if __name__ == '__main__': - # - # For testing - # - import sys - - if len(sys.argv) == 1: - print("need argument: materials.cfg") - sys.exit() - - ml = loadMaterialList(sys.argv[1]) - - print('MATERIAL LIST\n') - print((' from file: ', sys.argv[1])) - for m in ml: - print(m) diff --git a/hexrd/core/material/symbols.py b/hexrd/core/material/symbols.py index cbaf9e653..6339e7cfe 100644 --- a/hexrd/core/material/symbols.py +++ b/hexrd/core/material/symbols.py @@ -222,11 +222,12 @@ def PrintPossibleSG(xtal_sys): for i in range(sgmin, sgmax + 1): j = i - sgmin + 1 - pstr = f'{i}:{pstr_spacegroup[i - 1]}\t' - if j % 4 == 0 or j == sgmax: - print(pstr) - else: - print(pstr, end='') + pstr = f"{i}:{pstr_spacegroup[i - 1]}\t" + line += pstr + + if j % 4 == 0 or i == sgmax: + logger.info(line.rstrip()) + line = "" return sgmin, sgmax diff --git a/hexrd/core/valunits.py b/hexrd/core/valunits.py index f2d5f6be4..1eaf02894 100644 --- a/hexrd/core/valunits.py +++ b/hexrd/core/valunits.py @@ -34,7 +34,6 @@ """ -import doctest import math import numpy as np @@ -308,31 +307,3 @@ def _degrees(x: float) -> valWUnit: # Function alias _angstroms = _angstrom - -if __name__ == '__main__': # pragma: no cover - # - # doc testing - # - print("running doctest") - doctest.testmod() - - # - # other tests - # - def testConversions(): - print('===== Testing unit conversions ...') - print('..... angles:') - v = valWUnit('180d', 'angle', 180.0, 'degrees') - print(v) - print((' in degrees:', v.getVal('degrees'))) - print((' in radians: ', v.getVal('radians'))) - - print('..... lengths:') - ulist = ['m', 'mm', 'meter', 'angstrom'] - v = valWUnit('one meter', 'length', 1.0, 'meter') - print(v) - for u in ulist: - print((' in ', u, ': ', v.getVal(u))) - return - - testConversions() diff --git a/scripts/install/install_build_dependencies.py b/scripts/install/install_build_dependencies.py index 632855c22..217364fe5 100755 --- a/scripts/install/install_build_dependencies.py +++ b/scripts/install/install_build_dependencies.py @@ -183,5 +183,5 @@ def install(library, destination): library = sys.argv[1] destination = sys.argv[2] - print(f'Installing "{library}" to "{destination}"') + logger.debug(f'Installing "{library}" to "{destination}"') install(library, destination) diff --git a/tests/fit_grains_check.py b/tests/fit_grains_check.py index e74502bbd..7a27187aa 100755 --- a/tests/fit_grains_check.py +++ b/tests/fit_grains_check.py @@ -138,7 +138,3 @@ def compare_grain_fits( ctol=ctol, vtol=vtol, ) - if cresult: - print("test passed") - else: - print("test failed") From e634187fef7955e1fbf0ec0a6dbeb5d613e20617 Mon Sep 17 00:00:00 2001 From: Zack Singer Date: Fri, 6 Feb 2026 12:43:38 -0500 Subject: [PATCH 2/8] Simply remove the logging statement for installing deps --- scripts/install/install_build_dependencies.py | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/install/install_build_dependencies.py b/scripts/install/install_build_dependencies.py index 217364fe5..4ff4d4e68 100755 --- a/scripts/install/install_build_dependencies.py +++ b/scripts/install/install_build_dependencies.py @@ -183,5 +183,4 @@ def install(library, destination): library = sys.argv[1] destination = sys.argv[2] - logger.debug(f'Installing "{library}" to "{destination}"') install(library, destination) From 5061dace8ca3a523260fcf60ebeea8f3cf2f4511 Mon Sep 17 00:00:00 2001 From: Zack Date: Fri, 6 Feb 2026 14:56:16 -0500 Subject: [PATCH 3/8] Update hexrd/core/material/symbols.py Co-authored-by: Patrick Avery --- hexrd/core/material/symbols.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hexrd/core/material/symbols.py b/hexrd/core/material/symbols.py index 6339e7cfe..4f83d80fd 100644 --- a/hexrd/core/material/symbols.py +++ b/hexrd/core/material/symbols.py @@ -225,7 +225,7 @@ def PrintPossibleSG(xtal_sys): pstr = f"{i}:{pstr_spacegroup[i - 1]}\t" line += pstr - if j % 4 == 0 or i == sgmax: + if j % 4 == 0 or j == sgmax: logger.info(line.rstrip()) line = "" From ac91116c04bb765a0b88f18a7fbf07163e114442 Mon Sep 17 00:00:00 2001 From: Zack Singer Date: Mon, 23 Mar 2026 11:02:38 -0400 Subject: [PATCH 4/8] Remove fit_grains_check.py --- tests/fit_grains_check.py | 140 -------------------------------------- tests/test_fit-grains.py | 68 ++++++++++++++++-- 2 files changed, 64 insertions(+), 144 deletions(-) delete mode 100755 tests/fit_grains_check.py diff --git a/tests/fit_grains_check.py b/tests/fit_grains_check.py deleted file mode 100755 index 7a27187aa..000000000 --- a/tests/fit_grains_check.py +++ /dev/null @@ -1,140 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -Created on Mon Aug 10 10:53:26 2020 - -@author: joel -""" -import argparse -import logging - -import numpy as np - -from hexrd.hedm import config -from hexrd.hedm.fitgrains import fit_grains -from hexrd.core import matrixutil as mutil -from hexrd.core import rotations as rot - -logger = logging.getLogger(__name__) - - -def compare_grain_fits( - fit_grain_params, ref_grain_params, mtol=1.0e-4, ctol=1.0e-3, vtol=1.0e-4 -): - """ - Executes comparison between reference and fit grain parameters for ff-HEDM - for the same initial parameters. - - Parameters - ---------- - fit_grain_params : array_like, (n, 12) - The fit grain parameters to be tested. - ref_grain_params : array_like, (n, 12) - The reference grain parameters (see Notes below). - - Returns - ------- - bool - True is successful comparison - - Notes - ----- - The fitgrains action currently returns - grain_id, completeness, chisq, grain_params. - We will have to assume that the grain_ids are in the *same order* as the - reference, which can be enforces by running the comparison using the - reference orientation list. - """ - fit_grain_params = np.atleast_2d(fit_grain_params) - ref_grain_params = np.atleast_2d(ref_grain_params) - cresult = False - ii = 0 - for fg, rg in zip(fit_grain_params, ref_grain_params): - # test_orientation - quats = rot.quatOfExpMap(np.vstack([fg[:3], rg[:3]]).T) - ang, mis = rot.misorientation( - quats[:, 0].reshape(4, 1), quats[:, 1].reshape(4, 1) - ) - if ang <= mtol: - cresult = True - else: - logger.warning(f"orientations for grain {ii} do not agree.") - return cresult - - # test position - if np.linalg.norm(fg[3:6] - rg[3:6]) > ctol: - logger.warning(f"centroidal coordinates for grain {ii} do not agree.") - return False - - # test strain - vmat_fit = mutil.symmToVecMV( - np.linalg.inv(mutil.vecMVToSymm(fg[6:])), scale=False - ) - vmat_ref = mutil.symmToVecMV( - np.linalg.inv(mutil.vecMVToSymm(rg[6:])), scale=False - ) - if np.linalg.norm(vmat_fit - vmat_ref, ord=1) > vtol: - logger.warning(f"stretch components for grain {ii} do not agree.") - return False - - # index grain id - ii += 1 - return cresult - - -if __name__ == '__main__': - parser = argparse.ArgumentParser( - description="Montage of spot data for a specifed G-vector family" - ) - - parser.add_argument('cfg_file', help="yaml HEDM config filename", type=str) - parser.add_argument( - 'gt_ref', help="reference grain table filename", type=str - ) - - parser.add_argument( - '-m', - '--misorientation', - help="misorientation threshold", - type=float, - default=1.0e-4, - ) - - parser.add_argument( - '-c', - '--centroid', - help="centroid threshold", - type=float, - default=1.0e-3, - ) - - parser.add_argument( - '-v', '--stretch', help="stretch threshold", type=float, default=1.0e-4 - ) - - args = parser.parse_args() - - cfg_file = args.cfg_file - gt_ref = args.gt_ref - mtol = args.misorientation - ctol = args.centroid - vtol = args.stretch - - # load the config object - cfg = config.open(cfg_file)[0] - grains_table = np.loadtxt(gt_ref, ndmin=2) - ref_grain_params = grains_table[:, 3:15] - gresults = fit_grains( - cfg, - grains_table, - show_progress=False, - ids_to_refine=None, - write_spots_files=False, - ) - cresult = compare_grain_fits( - np.vstack([i[-1] for i in gresults]), - ref_grain_params, - mtol=mtol, - ctol=ctol, - vtol=vtol, - ) diff --git a/tests/test_fit-grains.py b/tests/test_fit-grains.py index 2f9506aa8..a900b64bc 100644 --- a/tests/test_fit-grains.py +++ b/tests/test_fit-grains.py @@ -7,14 +7,12 @@ import pytest import coloredlogs - +from hexrd.core import matrixutil as mutil +from hexrd.core import rotations as rot from hexrd.hedm import config from hexrd.hedm.fitgrains import fit_grains -from fit_grains_check import compare_grain_fits - - logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) @@ -73,6 +71,68 @@ def test_config(single_ge_config_path, single_ge_include_path): return conf +def compare_grain_fits( + fit_grain_params, ref_grain_params, mtol=1.0e-4, ctol=1.0e-3, vtol=1.0e-4 +): + """ + Executes comparison between reference and fit grain parameters for ff-HEDM + for the same initial parameters. + + Parameters + ---------- + fit_grain_params : array_like, (n, 12) + The fit grain parameters to be tested. + ref_grain_params : array_like, (n, 12) + The reference grain parameters (see Notes below). + + Returns + ------- + bool + True is successful comparison + + Notes + ----- + The fitgrains action currently returns + grain_id, completeness, chisq, grain_params. + We will have to assume that the grain_ids are in the *same order* as the + reference, which can be enforces by running the comparison using the + reference orientation list. + """ + fit_grain_params = np.atleast_2d(fit_grain_params) + ref_grain_params = np.atleast_2d(ref_grain_params) + cresult = False + ii = 0 + for fg, rg in zip(fit_grain_params, ref_grain_params): + # test_orientation + quats = rot.quatOfExpMap(np.vstack([fg[:3], rg[:3]]).T) + ang, mis = rot.misorientation( + quats[:, 0].reshape(4, 1), quats[:, 1].reshape(4, 1) + ) + if ang <= mtol: + cresult = True + else: + logger.warning(f"orientations for grain {ii} do not agree.") + return cresult + + # test position + if np.linalg.norm(fg[3:6] - rg[3:6]) > ctol: + logger.warning(f"centroidal coordinates for grain {ii} do not agree.") + return False + + # test strain + vmat_fit = mutil.symmToVecMV( + np.linalg.inv(mutil.vecMVToSymm(fg[6:])), scale=False + ) + vmat_ref = mutil.symmToVecMV( + np.linalg.inv(mutil.vecMVToSymm(rg[6:])), scale=False + ) + if np.linalg.norm(vmat_fit - vmat_ref, ord=1) > vtol: + logger.warning(f"stretch components for grain {ii} do not agree.") + return False + + # index grain id + ii += 1 + return cresult def test_fit_grains( single_ge_include_path, From e2d113d3bb12de1a33b4cd6358ce24d05b62c819 Mon Sep 17 00:00:00 2001 From: Zack Singer Date: Mon, 23 Mar 2026 11:10:31 -0400 Subject: [PATCH 5/8] info log -> file write for PatchDataWriter --- hexrd/core/instrument/hedm_instrument.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hexrd/core/instrument/hedm_instrument.py b/hexrd/core/instrument/hedm_instrument.py index 370f01bb0..2ac8ad116 100644 --- a/hexrd/core/instrument/hedm_instrument.py +++ b/hexrd/core/instrument/hedm_instrument.py @@ -2257,7 +2257,7 @@ def __init__(self, filename): self.fid = filename else: self.fid = open(filename, 'w') - logger.info(self._header, file=self.fid) + self.fid.write(self._header + '\n') def __del__(self): self.close() From 2690224a64e0d494c14693f5da7261e500a5be33 Mon Sep 17 00:00:00 2001 From: Zack Singer Date: Mon, 23 Mar 2026 13:09:52 -0400 Subject: [PATCH 6/8] Fix logging to file in writers --- hexrd/core/instrument/hedm_instrument.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hexrd/core/instrument/hedm_instrument.py b/hexrd/core/instrument/hedm_instrument.py index 2ac8ad116..a89f2c775 100644 --- a/hexrd/core/instrument/hedm_instrument.py +++ b/hexrd/core/instrument/hedm_instrument.py @@ -2349,7 +2349,7 @@ def __init__(self, filename=None, array=None): self.fid = filename else: self.fid = open(filename, 'w') - logger.info(self._header, file=self.fid) + self.fid.write(self._header + '\n') def __del__(self): self.close() @@ -2385,7 +2385,7 @@ def dump_grain(self, grain_id, completeness, chisq, grain_params): self._delim.join(np.tile('{:<23.16e}', len(res) - 3)).format(*res[3:]), ] ) - logger.info(output_str, file=self.fid) + self.fid.write(output_str + '\n') return output_str From 07d01c74de93855ba0a3139fbfa1bb97a9a3174b Mon Sep 17 00:00:00 2001 From: Zack Date: Wed, 25 Mar 2026 08:58:19 -0400 Subject: [PATCH 7/8] Update hexrd/core/instrument/hedm_instrument.py Co-authored-by: Patrick Avery --- hexrd/core/instrument/hedm_instrument.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hexrd/core/instrument/hedm_instrument.py b/hexrd/core/instrument/hedm_instrument.py index a89f2c775..fdb8327ef 100644 --- a/hexrd/core/instrument/hedm_instrument.py +++ b/hexrd/core/instrument/hedm_instrument.py @@ -2294,7 +2294,7 @@ def dump_patch( self._delim.join(np.tile('{:<23.16e}', 10)).format(*res[7:]), ] ) - logger.info(output_str, file=self.fid) + self.fid.write(output_str + '\n') return output_str From a51409c83cd5b041e2bc20b4ba19ba567a5817e8 Mon Sep 17 00:00:00 2001 From: Zack Singer Date: Wed, 25 Mar 2026 08:59:27 -0400 Subject: [PATCH 8/8] Add previously undefined line variable --- hexrd/core/material/symbols.py | 1 + 1 file changed, 1 insertion(+) diff --git a/hexrd/core/material/symbols.py b/hexrd/core/material/symbols.py index 4f83d80fd..5b9207ff0 100644 --- a/hexrd/core/material/symbols.py +++ b/hexrd/core/material/symbols.py @@ -220,6 +220,7 @@ def PrintPossibleSG(xtal_sys): sgmax = 2 sgmin = 1 + line = "" for i in range(sgmin, sgmax + 1): j = i - sgmin + 1 pstr = f"{i}:{pstr_spacegroup[i - 1]}\t"