diff --git a/hexrd/core/instrument/hedm_instrument.py b/hexrd/core/instrument/hedm_instrument.py index bfc82ad2f..fdb8327ef 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) + self.fid.write(self._header + '\n') 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) + self.fid.write(output_str + '\n') 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) + 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:]), ] ) - print(output_str, file=self.fid) + self.fid.write(output_str + '\n') 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..5b9207ff0 100644 --- a/hexrd/core/material/symbols.py +++ b/hexrd/core/material/symbols.py @@ -220,13 +220,15 @@ 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' + pstr = f"{i}:{pstr_spacegroup[i - 1]}\t" + line += pstr + if j % 4 == 0 or j == sgmax: - print(pstr) - else: - print(pstr, end='') + 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..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] - print(f'Installing "{library}" to "{destination}"') install(library, destination) diff --git a/tests/fit_grains_check.py b/tests/fit_grains_check.py deleted file mode 100755 index e74502bbd..000000000 --- a/tests/fit_grains_check.py +++ /dev/null @@ -1,144 +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, - ) - if cresult: - print("test passed") - else: - print("test failed") 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,