2021-05-07 23:12:23 +05:30
|
|
|
|
"""Miscellaneous helper functionality."""
|
|
|
|
|
|
2022-06-10 15:30:54 +05:30
|
|
|
|
import sys as _sys
|
|
|
|
|
import datetime as _datetime
|
|
|
|
|
import os as _os
|
|
|
|
|
import subprocess as _subprocess
|
|
|
|
|
import shlex as _shlex
|
|
|
|
|
import re as _re
|
|
|
|
|
import signal as _signal
|
|
|
|
|
import fractions as _fractions
|
2023-12-23 11:34:04 +05:30
|
|
|
|
import contextlib as _contextlib
|
2023-03-03 00:16:00 +05:30
|
|
|
|
from collections import abc as _abc, OrderedDict as _OrderedDict
|
2023-02-24 00:19:08 +05:30
|
|
|
|
from functools import reduce as _reduce, partial as _partial, wraps as _wraps
|
|
|
|
|
import inspect
|
2022-11-23 02:56:15 +05:30
|
|
|
|
from typing import Optional as _Optional, Callable as _Callable, Union as _Union, Iterable as _Iterable, \
|
2022-12-14 00:02:19 +05:30
|
|
|
|
Dict as _Dict, List as _List, Tuple as _Tuple, Literal as _Literal, \
|
2023-12-23 11:34:04 +05:30
|
|
|
|
Any as _Any, TextIO as _TextIO, Generator as _Generator
|
2022-06-10 15:30:54 +05:30
|
|
|
|
from pathlib import Path as _Path
|
|
|
|
|
|
|
|
|
|
import numpy as _np
|
|
|
|
|
import h5py as _h5py
|
|
|
|
|
|
|
|
|
|
from . import version as _version
|
2022-12-14 00:02:19 +05:30
|
|
|
|
from ._typehints import FloatSequence as _FloatSequence, NumpyRngSeed as _NumpyRngSeed, FileHandle as _FileHandle
|
2020-04-10 16:02:33 +05:30
|
|
|
|
|
2021-03-27 12:05:49 +05:30
|
|
|
|
# https://svn.blender.org/svnroot/bf-blender/trunk/blender/build_files/scons/tools/bcolors.py
|
|
|
|
|
# https://stackoverflow.com/questions/287871
|
|
|
|
|
_colors = {
|
|
|
|
|
'header' : '\033[95m',
|
|
|
|
|
'OK_blue': '\033[94m',
|
|
|
|
|
'OK_green': '\033[92m',
|
|
|
|
|
'warning': '\033[93m',
|
|
|
|
|
'fail': '\033[91m',
|
|
|
|
|
'end_color': '\033[0m',
|
|
|
|
|
'bold': '\033[1m',
|
|
|
|
|
'dim': '\033[2m',
|
|
|
|
|
'underline': '\033[4m',
|
|
|
|
|
'crossout': '\033[9m'
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-10 16:02:33 +05:30
|
|
|
|
####################################################################################################
|
|
|
|
|
# Functions
|
|
|
|
|
####################################################################################################
|
2022-01-26 20:55:27 +05:30
|
|
|
|
def srepr(msg,
|
2022-11-12 04:37:48 +05:30
|
|
|
|
glue: str = '\n',
|
|
|
|
|
quote: bool = False) -> str:
|
2020-02-22 04:36:51 +05:30
|
|
|
|
r"""
|
2022-11-12 04:37:48 +05:30
|
|
|
|
Join (quoted) items with glue string.
|
2020-03-09 18:09:20 +05:30
|
|
|
|
|
2020-02-22 03:55:22 +05:30
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-11-14 19:39:45 +05:30
|
|
|
|
msg : (sequence of) object with __repr__
|
2020-03-15 02:23:48 +05:30
|
|
|
|
Items to join.
|
2020-02-22 03:55:22 +05:30
|
|
|
|
glue : str, optional
|
2022-01-22 04:20:16 +05:30
|
|
|
|
Glue used for joining operation. Defaults to '\n'.
|
2022-11-12 04:37:48 +05:30
|
|
|
|
quote : bool, optional
|
|
|
|
|
Quote items. Defaults to False.
|
2020-02-22 03:55:22 +05:30
|
|
|
|
|
2021-04-24 21:30:57 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
joined : str
|
2022-11-12 04:37:48 +05:30
|
|
|
|
String representation of the joined and quoted items.
|
2021-04-24 21:30:57 +05:30
|
|
|
|
|
2020-02-22 03:55:22 +05:30
|
|
|
|
"""
|
2022-11-12 04:37:48 +05:30
|
|
|
|
q = '"' if quote else ''
|
2022-01-22 04:20:16 +05:30
|
|
|
|
if (not hasattr(msg, 'strip') and
|
|
|
|
|
(hasattr(msg, '__getitem__') or
|
|
|
|
|
hasattr(msg, '__iter__'))):
|
2022-11-12 04:37:48 +05:30
|
|
|
|
return glue.join(q+str(x)+q for x in msg)
|
2021-04-05 20:02:28 +05:30
|
|
|
|
else:
|
2022-11-12 04:37:48 +05:30
|
|
|
|
return q+(msg if isinstance(msg,str) else repr(msg))+q
|
2020-02-22 03:55:22 +05:30
|
|
|
|
|
|
|
|
|
|
2022-01-22 04:20:16 +05:30
|
|
|
|
def emph(msg) -> str:
|
2021-04-24 21:30:57 +05:30
|
|
|
|
"""
|
|
|
|
|
Format with emphasis.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-11-14 19:39:45 +05:30
|
|
|
|
msg : (sequence of) object with __repr__
|
2021-04-24 21:30:57 +05:30
|
|
|
|
Message to format.
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
formatted : str
|
|
|
|
|
Formatted string representation of the joined items.
|
|
|
|
|
|
|
|
|
|
"""
|
2022-01-22 04:20:16 +05:30
|
|
|
|
return _colors['bold']+srepr(msg)+_colors['end_color']
|
2020-02-22 03:55:22 +05:30
|
|
|
|
|
2022-01-22 04:20:16 +05:30
|
|
|
|
def deemph(msg) -> str:
|
2021-04-24 21:30:57 +05:30
|
|
|
|
"""
|
|
|
|
|
Format with deemphasis.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-11-14 19:39:45 +05:30
|
|
|
|
msg : (sequence of) object with __repr__
|
2021-04-24 21:30:57 +05:30
|
|
|
|
Message to format.
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
formatted : str
|
|
|
|
|
Formatted string representation of the joined items.
|
|
|
|
|
|
|
|
|
|
"""
|
2022-01-22 04:20:16 +05:30
|
|
|
|
return _colors['dim']+srepr(msg)+_colors['end_color']
|
2020-02-22 03:55:22 +05:30
|
|
|
|
|
2022-01-22 04:20:16 +05:30
|
|
|
|
def warn(msg) -> str:
|
2021-04-24 21:30:57 +05:30
|
|
|
|
"""
|
|
|
|
|
Format for warning.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-11-14 19:39:45 +05:30
|
|
|
|
msg : (sequence of) object with __repr__
|
2021-04-24 21:30:57 +05:30
|
|
|
|
Message to format.
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
formatted : str
|
|
|
|
|
Formatted string representation of the joined items.
|
|
|
|
|
|
|
|
|
|
"""
|
2022-01-22 04:20:16 +05:30
|
|
|
|
return _colors['warning']+emph(msg)+_colors['end_color']
|
2016-08-25 21:29:04 +05:30
|
|
|
|
|
2022-01-22 04:20:16 +05:30
|
|
|
|
def strikeout(msg) -> str:
|
2021-04-24 21:30:57 +05:30
|
|
|
|
"""
|
|
|
|
|
Format as strikeout.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-11-14 19:39:45 +05:30
|
|
|
|
msg : (iterable of) object with __repr__
|
2021-04-24 21:30:57 +05:30
|
|
|
|
Message to format.
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
formatted : str
|
|
|
|
|
Formatted string representation of the joined items.
|
|
|
|
|
|
|
|
|
|
"""
|
2022-01-22 04:20:16 +05:30
|
|
|
|
return _colors['crossout']+srepr(msg)+_colors['end_color']
|
2019-05-28 06:44:09 +05:30
|
|
|
|
|
|
|
|
|
|
2022-01-26 20:55:27 +05:30
|
|
|
|
def run(cmd: str,
|
|
|
|
|
wd: str = './',
|
2022-11-23 02:56:15 +05:30
|
|
|
|
env: _Optional[_Dict[str, str]] = None,
|
|
|
|
|
timeout: _Optional[int] = None) -> _Tuple[str, str]:
|
2020-02-22 03:55:22 +05:30
|
|
|
|
"""
|
2021-08-31 10:41:30 +05:30
|
|
|
|
Run a command.
|
2020-02-22 03:55:22 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
cmd : str
|
2020-03-15 02:23:48 +05:30
|
|
|
|
Command to be executed.
|
2020-02-22 03:55:22 +05:30
|
|
|
|
wd : str, optional
|
2022-01-22 04:20:16 +05:30
|
|
|
|
Working directory of process. Defaults to './'.
|
2020-03-15 02:23:48 +05:30
|
|
|
|
env : dict, optional
|
|
|
|
|
Environment for execution.
|
2021-08-31 10:41:30 +05:30
|
|
|
|
timeout : integer, optional
|
|
|
|
|
Timeout in seconds.
|
2020-02-22 03:55:22 +05:30
|
|
|
|
|
2021-04-24 21:30:57 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
2021-08-31 10:41:30 +05:30
|
|
|
|
stdout, stderr : (str, str)
|
2021-04-24 21:30:57 +05:30
|
|
|
|
Output of the executed command.
|
|
|
|
|
|
2020-02-22 03:55:22 +05:30
|
|
|
|
"""
|
2022-03-07 01:39:46 +05:30
|
|
|
|
def pass_signal(sig,_,proc,default):
|
|
|
|
|
proc.send_signal(sig)
|
2022-06-10 15:30:54 +05:30
|
|
|
|
_signal.signal(sig,default)
|
|
|
|
|
_signal.raise_signal(sig)
|
2022-03-07 01:39:46 +05:30
|
|
|
|
|
2022-06-10 15:30:54 +05:30
|
|
|
|
signals = [_signal.SIGINT,_signal.SIGTERM]
|
2022-03-07 01:39:46 +05:30
|
|
|
|
|
2021-08-31 10:41:30 +05:30
|
|
|
|
print(f"running '{cmd}' in '{wd}'")
|
2022-06-10 15:30:54 +05:30
|
|
|
|
process = _subprocess.Popen(_shlex.split(cmd),
|
2022-06-10 17:33:43 +05:30
|
|
|
|
stdout = _subprocess.PIPE,
|
|
|
|
|
stderr = _subprocess.PIPE,
|
|
|
|
|
env = _os.environ if env is None else env,
|
|
|
|
|
cwd = wd,
|
|
|
|
|
encoding = 'utf-8')
|
2022-03-07 01:39:46 +05:30
|
|
|
|
# ensure that process is terminated (https://stackoverflow.com/questions/22916783)
|
2022-06-10 15:30:54 +05:30
|
|
|
|
sig_states = [_signal.signal(sig,_partial(pass_signal,proc=process,default=_signal.getsignal(sig))) for sig in signals]
|
2022-03-07 01:39:46 +05:30
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
stdout,stderr = process.communicate(timeout=timeout)
|
|
|
|
|
finally:
|
|
|
|
|
for sig,state in zip(signals,sig_states):
|
2022-06-10 15:30:54 +05:30
|
|
|
|
_signal.signal(sig,state)
|
2021-03-27 12:05:49 +05:30
|
|
|
|
|
2020-02-22 03:55:22 +05:30
|
|
|
|
if process.returncode != 0:
|
2022-03-07 01:39:46 +05:30
|
|
|
|
print(stdout)
|
|
|
|
|
print(stderr)
|
2020-09-19 12:03:15 +05:30
|
|
|
|
raise RuntimeError(f"'{cmd}' failed with returncode {process.returncode}")
|
2021-03-27 12:05:49 +05:30
|
|
|
|
|
2022-03-07 01:39:46 +05:30
|
|
|
|
return stdout, stderr
|
2020-02-22 03:55:22 +05:30
|
|
|
|
|
2023-12-23 11:34:04 +05:30
|
|
|
|
@_contextlib.contextmanager
|
2022-06-10 15:30:54 +05:30
|
|
|
|
def open_text(fname: _FileHandle,
|
2023-12-23 11:34:04 +05:30
|
|
|
|
mode: _Literal['r','w'] = 'r') -> _Generator[_TextIO, None, None]: # noqa
|
2022-03-27 12:33:51 +05:30
|
|
|
|
"""
|
2023-12-23 11:34:04 +05:30
|
|
|
|
Open a text file with Unix line endings
|
|
|
|
|
|
|
|
|
|
If a path or string is given, a context manager ensures that
|
|
|
|
|
the file handle is closed.
|
|
|
|
|
If a file handle is given, it remains unmodified.
|
2022-03-27 12:33:51 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
fname : file, str, or pathlib.Path
|
|
|
|
|
Name or handle of file.
|
|
|
|
|
mode: {'r','w'}, optional
|
|
|
|
|
Access mode: 'r'ead or 'w'rite, defaults to 'r'.
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
f : file handle
|
|
|
|
|
|
|
|
|
|
"""
|
2023-12-23 11:34:04 +05:30
|
|
|
|
if isinstance(fname, (str,_Path)):
|
|
|
|
|
fhandle = open(_Path(fname).expanduser(),mode,newline=('\n' if mode == 'w' else None))
|
|
|
|
|
yield fhandle
|
|
|
|
|
fhandle.close()
|
|
|
|
|
else:
|
|
|
|
|
yield fname
|
2021-08-31 10:41:30 +05:30
|
|
|
|
|
2023-12-17 17:35:11 +05:30
|
|
|
|
def time_stamp() -> str:
|
2023-12-19 04:32:02 +05:30
|
|
|
|
"""Provide current time as formatted string."""
|
2023-12-17 17:35:11 +05:30
|
|
|
|
return _datetime.datetime.now().astimezone().strftime('%Y-%m-%d %H:%M:%S%z')
|
2021-08-31 10:41:30 +05:30
|
|
|
|
|
2022-11-19 13:40:00 +05:30
|
|
|
|
def execution_stamp(class_name: str,
|
2022-11-23 02:56:15 +05:30
|
|
|
|
function_name: _Optional[str] = None) -> str:
|
2022-11-19 13:40:00 +05:30
|
|
|
|
"""Timestamp the execution of a (function within a) class."""
|
|
|
|
|
_function_name = '' if function_name is None else f'.{function_name}'
|
2023-12-17 17:35:11 +05:30
|
|
|
|
return f'damask.{class_name}{_function_name} v{_version} ({time_stamp()})'
|
2022-11-19 13:40:00 +05:30
|
|
|
|
|
|
|
|
|
|
2022-06-10 15:30:54 +05:30
|
|
|
|
def natural_sort(key: str) -> _List[_Union[int, str]]:
|
2021-05-07 23:12:23 +05:30
|
|
|
|
"""
|
|
|
|
|
Natural sort.
|
|
|
|
|
|
|
|
|
|
For use in python's 'sorted'.
|
|
|
|
|
|
|
|
|
|
References
|
|
|
|
|
----------
|
|
|
|
|
https://en.wikipedia.org/wiki/Natural_sort_order
|
|
|
|
|
|
|
|
|
|
"""
|
2021-04-03 14:38:22 +05:30
|
|
|
|
convert = lambda text: int(text) if text.isdigit() else text
|
2022-06-10 15:30:54 +05:30
|
|
|
|
return [ convert(c) for c in _re.split('([0-9]+)', key) ]
|
2021-04-03 14:38:22 +05:30
|
|
|
|
|
|
|
|
|
|
2022-06-10 15:30:54 +05:30
|
|
|
|
def show_progress(iterable: _Iterable,
|
2022-11-23 02:56:15 +05:30
|
|
|
|
N_iter: _Optional[int] = None,
|
2022-01-17 19:28:08 +05:30
|
|
|
|
prefix: str = '',
|
2022-06-10 15:30:54 +05:30
|
|
|
|
bar_length: int = 50) -> _Any:
|
2020-04-10 16:02:33 +05:30
|
|
|
|
"""
|
2021-05-11 00:14:58 +05:30
|
|
|
|
Decorate a loop with a progress bar.
|
2020-04-10 16:02:33 +05:30
|
|
|
|
|
|
|
|
|
Use similar like enumerate.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-01-22 12:20:52 +05:30
|
|
|
|
iterable : iterable
|
|
|
|
|
Iterable to be decorated.
|
2021-05-07 23:12:23 +05:30
|
|
|
|
N_iter : int, optional
|
2022-01-22 12:20:52 +05:30
|
|
|
|
Total number of iterations. Required if iterable is not a sequence.
|
2021-05-07 23:12:23 +05:30
|
|
|
|
prefix : str, optional
|
2020-04-10 16:02:33 +05:30
|
|
|
|
Prefix string.
|
|
|
|
|
bar_length : int, optional
|
2021-05-11 00:14:58 +05:30
|
|
|
|
Length of progress bar in characters. Defaults to 50.
|
2020-04-10 16:02:33 +05:30
|
|
|
|
|
|
|
|
|
"""
|
2022-06-10 15:30:54 +05:30
|
|
|
|
if isinstance(iterable,_abc.Sequence):
|
2022-01-30 03:08:17 +05:30
|
|
|
|
if N_iter is None:
|
|
|
|
|
N = len(iterable)
|
|
|
|
|
else:
|
|
|
|
|
raise ValueError('N_iter given for sequence')
|
2022-01-22 12:20:52 +05:30
|
|
|
|
else:
|
2022-01-30 03:08:17 +05:30
|
|
|
|
if N_iter is None:
|
|
|
|
|
raise ValueError('N_iter not given')
|
|
|
|
|
|
|
|
|
|
N = N_iter
|
2022-01-22 12:20:52 +05:30
|
|
|
|
|
|
|
|
|
if N <= 1:
|
2021-03-31 17:57:36 +05:30
|
|
|
|
for item in iterable:
|
|
|
|
|
yield item
|
|
|
|
|
else:
|
2022-01-22 12:20:52 +05:30
|
|
|
|
status = ProgressBar(N,prefix,bar_length)
|
2021-03-31 17:57:36 +05:30
|
|
|
|
for i,item in enumerate(iterable):
|
|
|
|
|
yield item
|
|
|
|
|
status.update(i)
|
2020-04-10 16:02:33 +05:30
|
|
|
|
|
|
|
|
|
|
2022-06-10 15:30:54 +05:30
|
|
|
|
def scale_to_coprime(v: _FloatSequence) -> _np.ndarray:
|
2020-11-15 17:36:26 +05:30
|
|
|
|
"""
|
|
|
|
|
Scale vector to co-prime (relatively prime) integers.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-01-22 04:20:16 +05:30
|
|
|
|
v : sequence of float, len (:)
|
2020-11-15 17:36:26 +05:30
|
|
|
|
Vector to scale.
|
|
|
|
|
|
2021-04-24 21:30:57 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
2022-01-22 04:20:16 +05:30
|
|
|
|
m : numpy.ndarray, shape (:)
|
2021-04-24 21:30:57 +05:30
|
|
|
|
Vector scaled to co-prime numbers.
|
|
|
|
|
|
2020-11-15 17:36:26 +05:30
|
|
|
|
"""
|
2020-06-25 11:59:36 +05:30
|
|
|
|
MAX_DENOMINATOR = 1000000
|
2020-04-10 16:02:33 +05:30
|
|
|
|
|
|
|
|
|
def get_square_denominator(x):
|
|
|
|
|
"""Denominator of the square of a number."""
|
2022-06-10 15:30:54 +05:30
|
|
|
|
return _fractions.Fraction(x ** 2).limit_denominator(MAX_DENOMINATOR).denominator
|
2020-04-10 16:02:33 +05:30
|
|
|
|
|
2021-03-27 14:40:35 +05:30
|
|
|
|
def lcm(a,b):
|
2020-04-10 16:02:33 +05:30
|
|
|
|
"""Least common multiple."""
|
2021-03-27 14:40:35 +05:30
|
|
|
|
try:
|
2022-06-10 15:30:54 +05:30
|
|
|
|
return _np.lcm(a,b) # numpy > 1.18
|
2021-03-27 14:40:35 +05:30
|
|
|
|
except AttributeError:
|
2022-06-10 15:30:54 +05:30
|
|
|
|
return a * b // _np.gcd(a, b)
|
2020-04-10 16:02:33 +05:30
|
|
|
|
|
2022-06-10 15:30:54 +05:30
|
|
|
|
v_ = _np.array(v)
|
|
|
|
|
m = (v_ * _reduce(lcm, map(lambda x: int(get_square_denominator(x)),v_))**0.5).astype(_np.int64)
|
|
|
|
|
m = m//_reduce(_np.gcd,m)
|
2020-06-25 04:07:33 +05:30
|
|
|
|
|
2022-06-10 15:30:54 +05:30
|
|
|
|
with _np.errstate(invalid='ignore'):
|
|
|
|
|
if not _np.allclose(_np.ma.masked_invalid(v_/m),v_[_np.argmax(abs(v_))]/m[_np.argmax(abs(v_))]):
|
2022-02-22 21:12:05 +05:30
|
|
|
|
raise ValueError(f'invalid result "{m}" for input "{v_}"')
|
2020-06-25 04:07:33 +05:30
|
|
|
|
|
|
|
|
|
return m
|
2020-04-10 16:02:33 +05:30
|
|
|
|
|
|
|
|
|
|
2022-06-10 15:30:54 +05:30
|
|
|
|
def project_equal_angle(vector: _np.ndarray,
|
2022-06-10 15:33:50 +05:30
|
|
|
|
direction: _Literal['x', 'y', 'z'] = 'z', # noqa
|
2022-01-22 04:20:16 +05:30
|
|
|
|
normalize: bool = True,
|
2022-06-10 15:30:54 +05:30
|
|
|
|
keepdims: bool = False) -> _np.ndarray:
|
2020-11-10 01:50:56 +05:30
|
|
|
|
"""
|
2021-12-28 15:49:17 +05:30
|
|
|
|
Apply equal-angle projection to vector.
|
2020-11-10 01:50:56 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-01-22 04:20:16 +05:30
|
|
|
|
vector : numpy.ndarray, shape (...,3)
|
2020-11-10 01:50:56 +05:30
|
|
|
|
Vector coordinates to be projected.
|
2022-01-22 04:20:16 +05:30
|
|
|
|
direction : {'x', 'y', 'z'}
|
|
|
|
|
Projection direction. Defaults to 'z'.
|
2021-02-28 05:02:53 +05:30
|
|
|
|
normalize : bool
|
|
|
|
|
Ensure unit length of input vector. Defaults to True.
|
|
|
|
|
keepdims : bool
|
2022-01-30 03:46:57 +05:30
|
|
|
|
Maintain three-dimensional output coordinates.
|
|
|
|
|
Defaults to False.
|
2020-11-10 01:50:56 +05:30
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
2021-12-28 15:49:17 +05:30
|
|
|
|
coordinates : numpy.ndarray, shape (...,2 | 3)
|
2020-11-10 01:50:56 +05:30
|
|
|
|
Projected coordinates.
|
|
|
|
|
|
2022-01-30 03:46:57 +05:30
|
|
|
|
Notes
|
|
|
|
|
-----
|
|
|
|
|
Two-dimensional output uses right-handed frame spanned by
|
|
|
|
|
the next and next-next axis relative to the projection direction,
|
|
|
|
|
e.g. x-y when projecting along z and z-x when projecting along y.
|
|
|
|
|
|
2021-02-28 05:02:53 +05:30
|
|
|
|
Examples
|
|
|
|
|
--------
|
2021-07-25 23:01:48 +05:30
|
|
|
|
>>> import damask
|
|
|
|
|
>>> import numpy as np
|
2021-12-28 15:49:17 +05:30
|
|
|
|
>>> project_equal_angle(np.ones(3))
|
2021-02-28 05:02:53 +05:30
|
|
|
|
[0.3660254, 0.3660254]
|
2021-12-28 15:49:17 +05:30
|
|
|
|
>>> project_equal_angle(np.ones(3),direction='x',normalize=False,keepdims=True)
|
2021-02-28 05:02:53 +05:30
|
|
|
|
[0, 0.5, 0.5]
|
2021-12-28 15:49:17 +05:30
|
|
|
|
>>> project_equal_angle([0,1,1],direction='y',normalize=True,keepdims=False)
|
2021-02-28 05:02:53 +05:30
|
|
|
|
[0.41421356, 0]
|
|
|
|
|
|
2020-11-10 01:50:56 +05:30
|
|
|
|
"""
|
2021-02-28 05:16:20 +05:30
|
|
|
|
shift = 'zyx'.index(direction)
|
2022-06-10 15:30:54 +05:30
|
|
|
|
v = _np.roll(vector/_np.linalg.norm(vector,axis=-1,keepdims=True) if normalize else vector,
|
2022-06-10 17:33:43 +05:30
|
|
|
|
shift,axis=-1)
|
2022-06-10 15:30:54 +05:30
|
|
|
|
return _np.roll(_np.block([v[...,:2]/(1.0+_np.abs(v[...,2:3])),_np.zeros_like(v[...,2:3])]),
|
2022-06-10 17:33:43 +05:30
|
|
|
|
-shift if keepdims else 0,axis=-1)[...,:3 if keepdims else 2]
|
2021-12-28 15:49:17 +05:30
|
|
|
|
|
2022-06-10 15:30:54 +05:30
|
|
|
|
def project_equal_area(vector: _np.ndarray,
|
2022-06-10 15:33:50 +05:30
|
|
|
|
direction: _Literal['x', 'y', 'z'] = 'z', # noqa
|
2022-01-17 19:28:08 +05:30
|
|
|
|
normalize: bool = True,
|
2022-06-10 15:30:54 +05:30
|
|
|
|
keepdims: bool = False) -> _np.ndarray:
|
2021-12-28 15:49:17 +05:30
|
|
|
|
"""
|
|
|
|
|
Apply equal-area projection to vector.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
vector : numpy.ndarray, shape (...,3)
|
|
|
|
|
Vector coordinates to be projected.
|
2022-01-22 04:20:16 +05:30
|
|
|
|
direction : {'x', 'y', 'z'}
|
|
|
|
|
Projection direction. Defaults to 'z'.
|
2021-12-28 15:49:17 +05:30
|
|
|
|
normalize : bool
|
|
|
|
|
Ensure unit length of input vector. Defaults to True.
|
|
|
|
|
keepdims : bool
|
2022-01-30 03:46:57 +05:30
|
|
|
|
Maintain three-dimensional output coordinates.
|
|
|
|
|
Defaults to False.
|
2021-12-28 15:49:17 +05:30
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
coordinates : numpy.ndarray, shape (...,2 | 3)
|
|
|
|
|
Projected coordinates.
|
|
|
|
|
|
2022-01-30 03:46:57 +05:30
|
|
|
|
Notes
|
|
|
|
|
-----
|
|
|
|
|
Two-dimensional output uses right-handed frame spanned by
|
|
|
|
|
the next and next-next axis relative to the projection direction,
|
|
|
|
|
e.g. x-y when projecting along z and z-x when projecting along y.
|
|
|
|
|
|
|
|
|
|
|
2021-12-28 15:49:17 +05:30
|
|
|
|
Examples
|
|
|
|
|
--------
|
|
|
|
|
>>> import damask
|
|
|
|
|
>>> import numpy as np
|
|
|
|
|
>>> project_equal_area(np.ones(3))
|
|
|
|
|
[0.45970084, 0.45970084]
|
|
|
|
|
>>> project_equal_area(np.ones(3),direction='x',normalize=False,keepdims=True)
|
|
|
|
|
[0.0, 0.70710678, 0.70710678]
|
|
|
|
|
>>> project_equal_area([0,1,1],direction='y',normalize=True,keepdims=False)
|
|
|
|
|
[0.5411961, 0.0]
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
shift = 'zyx'.index(direction)
|
2022-06-10 15:30:54 +05:30
|
|
|
|
v = _np.roll(vector/_np.linalg.norm(vector,axis=-1,keepdims=True) if normalize else vector,
|
2022-06-10 17:33:43 +05:30
|
|
|
|
shift,axis=-1)
|
2022-06-10 15:30:54 +05:30
|
|
|
|
return _np.roll(_np.block([v[...,:2]/_np.sqrt(1.0+_np.abs(v[...,2:3])),_np.zeros_like(v[...,2:3])]),
|
2022-06-10 17:33:43 +05:30
|
|
|
|
-shift if keepdims else 0,axis=-1)[...,:3 if keepdims else 2]
|
2020-11-10 01:50:56 +05:30
|
|
|
|
|
2020-08-24 02:53:23 +05:30
|
|
|
|
|
2022-11-25 11:30:15 +05:30
|
|
|
|
def hybrid_IA(dist: _FloatSequence,
|
2022-01-26 20:55:27 +05:30
|
|
|
|
N: int,
|
2022-11-23 02:56:15 +05:30
|
|
|
|
rng_seed: _Optional[_NumpyRngSeed] = None) -> _np.ndarray:
|
2020-11-15 17:36:26 +05:30
|
|
|
|
"""
|
|
|
|
|
Hybrid integer approximation.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
dist : numpy.ndarray
|
2022-11-25 11:30:15 +05:30
|
|
|
|
Distribution to be approximated.
|
2020-11-15 17:36:26 +05:30
|
|
|
|
N : int
|
|
|
|
|
Number of samples to draw.
|
|
|
|
|
rng_seed : {None, int, array_like[ints], SeedSequence, BitGenerator, Generator}, optional
|
|
|
|
|
A seed to initialize the BitGenerator. Defaults to None.
|
|
|
|
|
If None, then fresh, unpredictable entropy will be pulled from the OS.
|
|
|
|
|
|
2022-11-25 11:30:15 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
hist : numpy.ndarray, shape (N)
|
|
|
|
|
Integer approximation of the distribution.
|
|
|
|
|
|
2020-11-15 17:36:26 +05:30
|
|
|
|
"""
|
2022-11-25 11:30:15 +05:30
|
|
|
|
N_opt_samples = max(_np.count_nonzero(dist),N) # random subsampling if too little samples requested
|
|
|
|
|
N_inv_samples = 0
|
2020-09-28 11:10:43 +05:30
|
|
|
|
|
|
|
|
|
scale_,scale,inc_factor = (0.0,float(N_opt_samples),1.0)
|
2022-06-10 15:30:54 +05:30
|
|
|
|
while (not _np.isclose(scale, scale_)) and (N_inv_samples != N_opt_samples):
|
2022-11-25 11:30:15 +05:30
|
|
|
|
repeats = _np.rint(scale*_np.array(dist)).astype(_np.int64)
|
2022-06-10 15:30:54 +05:30
|
|
|
|
N_inv_samples = _np.sum(repeats)
|
2020-09-28 11:10:43 +05:30
|
|
|
|
scale_,scale,inc_factor = (scale,scale+inc_factor*0.5*(scale - scale_), inc_factor*2.0) \
|
|
|
|
|
if N_inv_samples < N_opt_samples else \
|
|
|
|
|
(scale_,0.5*(scale_ + scale), 1.0)
|
|
|
|
|
|
2022-06-10 15:30:54 +05:30
|
|
|
|
return _np.repeat(_np.arange(len(dist)),repeats)[_np.random.default_rng(rng_seed).permutation(N_inv_samples)[:N]]
|
2020-09-28 11:10:43 +05:30
|
|
|
|
|
|
|
|
|
|
2022-06-10 15:30:54 +05:30
|
|
|
|
def shapeshifter(fro: _Tuple[int, ...],
|
|
|
|
|
to: _Tuple[int, ...],
|
2022-06-10 15:33:50 +05:30
|
|
|
|
mode: _Literal['left','right'] = 'left', # noqa
|
2022-06-10 15:30:54 +05:30
|
|
|
|
keep_ones: bool = False) -> _Tuple[int, ...]:
|
2020-11-10 01:50:56 +05:30
|
|
|
|
"""
|
2021-07-25 23:01:48 +05:30
|
|
|
|
Return dimensions that reshape 'fro' to become broadcastable to 'to'.
|
2020-11-10 01:50:56 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
fro : tuple
|
|
|
|
|
Original shape of array.
|
|
|
|
|
to : tuple
|
|
|
|
|
Target shape of array after broadcasting.
|
|
|
|
|
len(to) cannot be less than len(fro).
|
2022-01-22 04:20:16 +05:30
|
|
|
|
mode : {'left', 'right'}, optional
|
2020-11-10 01:50:56 +05:30
|
|
|
|
Indicates whether new axes are preferably added to
|
2022-01-22 04:20:16 +05:30
|
|
|
|
either left or right of the original shape.
|
2020-11-10 01:50:56 +05:30
|
|
|
|
Defaults to 'left'.
|
|
|
|
|
keep_ones : bool, optional
|
|
|
|
|
Treat '1' in fro as literal value instead of dimensional placeholder.
|
|
|
|
|
Defaults to False.
|
|
|
|
|
|
2021-07-25 23:01:48 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
new_dims : tuple
|
|
|
|
|
Dimensions for reshape.
|
|
|
|
|
|
2022-07-27 01:25:17 +05:30
|
|
|
|
Examples
|
|
|
|
|
--------
|
2021-07-25 23:01:48 +05:30
|
|
|
|
>>> import numpy as np
|
|
|
|
|
>>> from damask import util
|
|
|
|
|
>>> a = np.ones((3,4,2))
|
|
|
|
|
>>> b = np.ones(4)
|
|
|
|
|
>>> b_extended = b.reshape(util.shapeshifter(b.shape,a.shape))
|
|
|
|
|
>>> (a * np.broadcast_to(b_extended,a.shape)).shape
|
|
|
|
|
(3,4,2)
|
|
|
|
|
|
2020-11-10 01:50:56 +05:30
|
|
|
|
"""
|
2022-08-13 00:15:40 +05:30
|
|
|
|
if len(fro) == 0 and len(to) == 0: return tuple()
|
|
|
|
|
_fro = [1] if len(fro) == 0 else list(fro)[::-1 if mode=='left' else 1]
|
|
|
|
|
_to = [1] if len(to) == 0 else list(to) [::-1 if mode=='left' else 1]
|
|
|
|
|
|
|
|
|
|
final_shape: _List[int] = []
|
|
|
|
|
index = 0
|
|
|
|
|
for i,item in enumerate(_to):
|
2022-11-19 13:40:00 +05:30
|
|
|
|
if item == _fro[index]:
|
2022-08-13 00:15:40 +05:30
|
|
|
|
final_shape.append(item)
|
|
|
|
|
index+=1
|
|
|
|
|
else:
|
|
|
|
|
final_shape.append(1)
|
2022-11-19 13:40:00 +05:30
|
|
|
|
if _fro[index] == 1 and not keep_ones:
|
2022-08-13 00:15:40 +05:30
|
|
|
|
index+=1
|
2022-11-19 13:40:00 +05:30
|
|
|
|
if index == len(_fro):
|
2022-08-13 00:15:40 +05:30
|
|
|
|
final_shape = final_shape+[1]*(len(_to)-i-1)
|
|
|
|
|
break
|
2022-11-19 13:40:00 +05:30
|
|
|
|
if index != len(_fro): raise ValueError(f'shapes cannot be shifted {fro} --> {to}')
|
|
|
|
|
return tuple(final_shape[::-1] if mode == 'left' else final_shape)
|
2020-11-10 01:50:56 +05:30
|
|
|
|
|
2022-06-10 15:30:54 +05:30
|
|
|
|
def shapeblender(a: _Tuple[int, ...],
|
2023-09-20 03:13:27 +05:30
|
|
|
|
b: _Tuple[int, ...],
|
2023-10-04 19:04:23 +05:30
|
|
|
|
keep_ones: bool = False) -> _Tuple[int, ...]:
|
2020-11-10 01:50:56 +05:30
|
|
|
|
"""
|
|
|
|
|
Return a shape that overlaps the rightmost entries of 'a' with the leftmost of 'b'.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
a : tuple
|
|
|
|
|
Shape of first array.
|
|
|
|
|
b : tuple
|
|
|
|
|
Shape of second array.
|
2023-09-20 03:13:27 +05:30
|
|
|
|
keep_ones : bool, optional
|
|
|
|
|
Treat innermost '1's as literal value instead of dimensional placeholder.
|
2023-10-04 19:04:23 +05:30
|
|
|
|
Defaults to False.
|
2020-11-10 01:50:56 +05:30
|
|
|
|
|
|
|
|
|
Examples
|
|
|
|
|
--------
|
2023-10-04 19:04:23 +05:30
|
|
|
|
>>> shapeblender((3,2),(3,2))
|
|
|
|
|
(3,2)
|
|
|
|
|
>>> shapeblender((4,3),(3,2))
|
|
|
|
|
(4,3,2)
|
|
|
|
|
>>> shapeblender((4,4),(3,2))
|
|
|
|
|
(4,4,3,2)
|
2020-11-10 01:50:56 +05:30
|
|
|
|
>>> shapeblender((1,2),(1,2,3))
|
|
|
|
|
(1,2,3)
|
2023-10-04 19:04:23 +05:30
|
|
|
|
>>> shapeblender((),(2,2,1))
|
|
|
|
|
(2,2,1)
|
2020-11-10 01:50:56 +05:30
|
|
|
|
>>> shapeblender((1,),(2,2,1))
|
2023-09-20 03:13:27 +05:30
|
|
|
|
(2,2,1)
|
2023-10-04 19:04:23 +05:30
|
|
|
|
>>> shapeblender((1,),(2,2,1),True)
|
|
|
|
|
(1,2,2,1)
|
2020-11-10 01:50:56 +05:30
|
|
|
|
|
|
|
|
|
"""
|
2023-09-20 03:13:27 +05:30
|
|
|
|
def is_broadcastable(a,b):
|
|
|
|
|
try:
|
|
|
|
|
_np.broadcast_shapes(a,b)
|
|
|
|
|
return True
|
|
|
|
|
except ValueError:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
a_,_b = a,b
|
|
|
|
|
if keep_ones:
|
|
|
|
|
i = min(len(a_),len(_b))
|
|
|
|
|
while i > 0 and a_[-i:] != _b[:i]: i -= 1
|
|
|
|
|
return a_ + _b[i:]
|
|
|
|
|
else:
|
|
|
|
|
a_ += max(0,len(_b)-len(a_))*(1,)
|
|
|
|
|
while not is_broadcastable(a_,_b):
|
|
|
|
|
a_ = a_ + ((1,) if len(a_)<=len(_b) else ())
|
|
|
|
|
_b = ((1,) if len(_b)<len(a_) else ()) + _b
|
|
|
|
|
return _np.broadcast_shapes(a_,_b)
|
2020-11-10 01:50:56 +05:30
|
|
|
|
|
|
|
|
|
|
2022-11-19 13:40:00 +05:30
|
|
|
|
def _docstringer(docstring: _Union[str, _Callable],
|
2023-03-03 00:16:00 +05:30
|
|
|
|
adopted_parameters: _Union[None, str, _Callable] = None,
|
|
|
|
|
adopted_return: _Union[None, str, _Callable] = None,
|
|
|
|
|
adopted_notes: _Union[None, str, _Callable] = None,
|
|
|
|
|
adopted_examples: _Union[None, str, _Callable] = None,
|
|
|
|
|
adopted_references: _Union[None, str, _Callable] = None) -> str:
|
2020-11-15 15:23:23 +05:30
|
|
|
|
"""
|
2022-11-19 13:40:00 +05:30
|
|
|
|
Extend a docstring.
|
2020-11-15 15:23:23 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-11-19 13:40:00 +05:30
|
|
|
|
docstring : str or callable, optional
|
|
|
|
|
Docstring (of callable) to extend.
|
2023-03-03 00:16:00 +05:30
|
|
|
|
adopted_* : str or callable, optional
|
|
|
|
|
Additional information to insert into/append to respective section.
|
|
|
|
|
|
|
|
|
|
Notes
|
|
|
|
|
-----
|
|
|
|
|
adopted_return fetches the typehint of a passed function instead of the docstring
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
docstring_: str = str( docstring if isinstance(docstring,str)
|
|
|
|
|
else docstring.__doc__ if callable(docstring) and docstring.__doc__
|
|
|
|
|
else '').rstrip()+'\n'
|
|
|
|
|
sections = _OrderedDict(
|
|
|
|
|
Parameters=adopted_parameters,
|
|
|
|
|
Returns=adopted_return,
|
|
|
|
|
Examples=adopted_examples,
|
|
|
|
|
Notes=adopted_notes,
|
|
|
|
|
References=adopted_references)
|
|
|
|
|
|
|
|
|
|
for i, (key, adopted) in [(i,(k,v)) for (i,(k,v)) in enumerate(sections.items()) if v is not None]:
|
|
|
|
|
section_regex = fr'^([ ]*){key}\s*\n\1*{"-"*len(key)}\s*\n'
|
|
|
|
|
if key=='Returns':
|
|
|
|
|
if callable(adopted):
|
|
|
|
|
return_class = adopted.__annotations__.get('return','')
|
|
|
|
|
return_type_ = (_sys.modules[adopted.__module__].__name__.split('.')[0]
|
|
|
|
|
+'.'
|
|
|
|
|
+(return_class.__name__ if not isinstance(return_class,str) else return_class))
|
|
|
|
|
else:
|
|
|
|
|
return_type_ = adopted
|
|
|
|
|
docstring_ = _re.sub(fr'(^[ ]*{key}\s*\n\s*{"-"*len(key)}\s*\n[ ]*[A-Za-z0-9_ ]*: )(.*)\n',
|
|
|
|
|
fr'\1{return_type_}\n',
|
|
|
|
|
docstring_,flags=_re.MULTILINE)
|
2022-11-19 13:40:00 +05:30
|
|
|
|
else:
|
2023-03-03 00:16:00 +05:30
|
|
|
|
section_content_regex = fr'{section_regex}(?P<content>.*?)\n *(\n|\Z)'
|
|
|
|
|
adopted_: str = adopted.__doc__ if callable(adopted) else adopted #type: ignore
|
|
|
|
|
try:
|
|
|
|
|
if _re.search(fr'{section_regex}', adopted_, flags=_re.MULTILINE):
|
|
|
|
|
adopted_ = _re.search(section_content_regex, #type: ignore
|
|
|
|
|
adopted_,
|
|
|
|
|
flags=_re.MULTILINE|_re.DOTALL).group('content')
|
|
|
|
|
except AttributeError:
|
2023-12-19 04:25:38 +05:30
|
|
|
|
raise RuntimeError(f"function docstring passed for docstring section '{key}' is invalid:\n{docstring}")
|
2023-03-03 00:16:00 +05:30
|
|
|
|
|
|
|
|
|
docstring_indent, adopted_indent = (min([len(line)-len(line.lstrip()) for line in section.split('\n') if line.strip()])
|
|
|
|
|
for section in [docstring_, adopted_])
|
|
|
|
|
shift = adopted_indent - docstring_indent
|
|
|
|
|
adopted_content = '\n'.join([(line[shift:] if shift > 0 else
|
|
|
|
|
f'{" "*-shift}{line}') for line in adopted_.split('\n') if line.strip()])
|
|
|
|
|
|
|
|
|
|
if _re.search(section_regex, docstring_, flags=_re.MULTILINE):
|
|
|
|
|
docstring_section_content = _re.search(section_content_regex, # type: ignore
|
|
|
|
|
docstring_,
|
|
|
|
|
flags=_re.MULTILINE|_re.DOTALL).group('content')
|
|
|
|
|
a_items, d_items = (_re.findall('^[ ]*([A-Za-z0-9_ ]*?)[ ]*:',content,flags=_re.MULTILINE)
|
|
|
|
|
for content in [adopted_content,docstring_section_content])
|
|
|
|
|
for item in a_items:
|
|
|
|
|
if item in d_items:
|
|
|
|
|
adopted_content = _re.sub(fr'^([ ]*){item}.*?(?:(\n)\1([A-Za-z0-9_])|([ ]*\Z))',
|
|
|
|
|
r'\1\3',
|
|
|
|
|
adopted_content,
|
|
|
|
|
flags=_re.MULTILINE|_re.DOTALL).rstrip(' \n')
|
|
|
|
|
docstring_ = _re.sub(fr'(^[ ]*{key}\s*\n\s*{"-"*len(key)}\s*\n.*?)\n *(\Z|\n)',
|
|
|
|
|
fr'\1\n{adopted_content}\n\2',
|
|
|
|
|
docstring_,
|
|
|
|
|
flags=_re.MULTILINE|_re.DOTALL)
|
|
|
|
|
else:
|
|
|
|
|
section_title = f'{" "*(shift+docstring_indent)}{key}\n{" "*(shift+docstring_indent)}{"-"*len(key)}\n'
|
|
|
|
|
section_matches = [_re.search(
|
|
|
|
|
fr'[ ]*{list(sections.keys())[index]}\s*\n\s*{"-"*len(list(sections.keys())[index])}\s*', docstring_)
|
|
|
|
|
for index in range(i,len(sections))]
|
|
|
|
|
subsequent_section = '\\Z' if not any(section_matches) else \
|
|
|
|
|
'\n'+next(item for item in section_matches if item is not None).group(0)
|
|
|
|
|
docstring_ = _re.sub(fr'({subsequent_section})',
|
|
|
|
|
fr'\n{section_title}{adopted_content}\n\1',
|
|
|
|
|
docstring_)
|
|
|
|
|
return docstring_
|
2020-11-15 15:23:23 +05:30
|
|
|
|
|
|
|
|
|
|
2022-11-23 02:56:15 +05:30
|
|
|
|
def extend_docstring(docstring: _Union[None, str, _Callable] = None,
|
2023-03-03 00:16:00 +05:30
|
|
|
|
**kwargs) -> _Callable:
|
2020-11-15 15:23:23 +05:30
|
|
|
|
"""
|
2022-11-19 13:40:00 +05:30
|
|
|
|
Decorator: Extend the function's docstring.
|
2020-11-15 00:21:15 +05:30
|
|
|
|
|
2020-11-15 15:23:23 +05:30
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-11-19 13:40:00 +05:30
|
|
|
|
docstring : str or callable, optional
|
|
|
|
|
Docstring to extend. Defaults to that of decorated function.
|
2023-03-03 00:16:00 +05:30
|
|
|
|
adopted_* : str or callable, optional
|
|
|
|
|
Additional information to insert into/append to respective section.
|
2022-11-19 13:40:00 +05:30
|
|
|
|
|
|
|
|
|
Notes
|
|
|
|
|
-----
|
|
|
|
|
Return type will become own type if docstring is callable.
|
2020-11-15 00:21:15 +05:30
|
|
|
|
|
2020-11-15 15:23:23 +05:30
|
|
|
|
"""
|
|
|
|
|
def _decorator(func):
|
2023-03-03 00:16:00 +05:30
|
|
|
|
if 'adopted_return' not in kwargs: kwargs['adopted_return'] = func
|
2022-11-19 13:40:00 +05:30
|
|
|
|
func.__doc__ = _docstringer(func.__doc__ if docstring is None else docstring,
|
2023-03-03 00:16:00 +05:30
|
|
|
|
**kwargs)
|
2020-11-15 15:23:23 +05:30
|
|
|
|
return func
|
|
|
|
|
return _decorator
|
2020-11-15 00:21:15 +05:30
|
|
|
|
|
2023-02-24 00:19:08 +05:30
|
|
|
|
def pass_on(keyword: str,
|
|
|
|
|
target: _Callable,
|
|
|
|
|
wrapped: _Callable = None) -> _Callable: # type: ignore
|
|
|
|
|
"""
|
|
|
|
|
Decorator: Combine signatures of 'wrapped' and 'target' functions and pass on output of 'target' as 'keyword' argument.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
keyword : str
|
|
|
|
|
Keyword added to **kwargs of the decorated function
|
|
|
|
|
passing on the result of 'target'.
|
|
|
|
|
target : callable
|
|
|
|
|
The output of this function is passed to the
|
|
|
|
|
decorated function as 'keyword' argument.
|
|
|
|
|
wrapped: callable, optional
|
|
|
|
|
Signature of 'wrapped' function combined with
|
|
|
|
|
that of 'target' yields the overall signature of decorated function.
|
|
|
|
|
|
|
|
|
|
Notes
|
|
|
|
|
-----
|
|
|
|
|
The keywords used by 'target' will be prioritized
|
|
|
|
|
if they overlap with those of the decorated function.
|
|
|
|
|
Functions 'target' and 'wrapped' are assumed to only have keyword arguments.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def decorator(func):
|
|
|
|
|
@_wraps(func)
|
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
|
kw_wrapped = set(kwargs.keys()) - set(inspect.getfullargspec(target).args)
|
|
|
|
|
kwargs_wrapped = {kw: kwargs.pop(kw) for kw in kw_wrapped}
|
|
|
|
|
kwargs_wrapped[keyword] = target(**kwargs)
|
|
|
|
|
return func(*args, **kwargs_wrapped)
|
|
|
|
|
args_ = [] if wrapped is None or 'self' not in inspect.signature(wrapped).parameters \
|
|
|
|
|
else [inspect.signature(wrapped).parameters['self']]
|
|
|
|
|
for f in [target] if wrapped is None else [target,wrapped]:
|
|
|
|
|
for param in inspect.signature(f).parameters.values():
|
|
|
|
|
if param.name != keyword \
|
2023-03-03 00:16:00 +05:30
|
|
|
|
and param.name not in [p.name for p in args_]+['self','cls', 'args', 'kwargs']:
|
|
|
|
|
args_.append(param.replace(kind=inspect._ParameterKind.KEYWORD_ONLY))
|
2023-02-24 00:19:08 +05:30
|
|
|
|
wrapper.__signature__ = inspect.Signature(parameters=args_,return_annotation=inspect.signature(func).return_annotation)
|
|
|
|
|
return wrapper
|
|
|
|
|
return decorator
|
2020-11-15 00:21:15 +05:30
|
|
|
|
|
2023-09-22 18:44:54 +05:30
|
|
|
|
def DREAM3D_base_group(fname: _Union[str, _Path, _h5py.File]) -> str:
|
2021-03-23 18:58:56 +05:30
|
|
|
|
"""
|
|
|
|
|
Determine the base group of a DREAM.3D file.
|
|
|
|
|
|
|
|
|
|
The base group is defined as the group (folder) that contains
|
|
|
|
|
a 'SPACING' dataset in a '_SIMPL_GEOMETRY' group.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2023-09-22 18:44:54 +05:30
|
|
|
|
fname : str, pathlib.Path, or _h5py.File
|
2021-03-23 18:58:56 +05:30
|
|
|
|
Filename of the DREAM.3D (HDF5) file.
|
|
|
|
|
|
2021-04-24 21:30:57 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
path : str
|
|
|
|
|
Path to the base group.
|
|
|
|
|
|
2021-03-23 18:58:56 +05:30
|
|
|
|
"""
|
2023-09-22 18:44:54 +05:30
|
|
|
|
def get_base_group(f: _h5py.File) -> str:
|
2021-03-20 04:19:41 +05:30
|
|
|
|
base_group = f.visit(lambda path: path.rsplit('/',2)[0] if '_SIMPL_GEOMETRY/SPACING' in path else None)
|
2023-09-22 18:44:54 +05:30
|
|
|
|
if base_group is None:
|
|
|
|
|
raise ValueError(f'could not determine base group in file "{fname}"')
|
|
|
|
|
return base_group
|
2021-03-23 18:58:56 +05:30
|
|
|
|
|
2023-09-22 18:44:54 +05:30
|
|
|
|
if isinstance(fname,_h5py.File):
|
|
|
|
|
return get_base_group(fname)
|
2021-03-23 18:58:56 +05:30
|
|
|
|
|
2023-09-22 18:44:54 +05:30
|
|
|
|
with _h5py.File(_Path(fname).expanduser(),'r') as f:
|
|
|
|
|
return get_base_group(f)
|
2021-03-20 04:19:41 +05:30
|
|
|
|
|
2023-09-22 18:44:54 +05:30
|
|
|
|
def DREAM3D_cell_data_group(fname: _Union[str, _Path, _h5py.File]) -> str:
|
2021-03-23 18:58:56 +05:30
|
|
|
|
"""
|
|
|
|
|
Determine the cell data group of a DREAM.3D file.
|
|
|
|
|
|
|
|
|
|
The cell data group is defined as the group (folder) that contains
|
|
|
|
|
a dataset in the base group whose length matches the total number
|
|
|
|
|
of points as specified in '_SIMPL_GEOMETRY/DIMENSIONS'.
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2023-09-22 18:44:54 +05:30
|
|
|
|
fname : str, pathlib.Path, or h5py.File
|
2021-03-23 18:58:56 +05:30
|
|
|
|
Filename of the DREAM.3D (HDF5) file.
|
|
|
|
|
|
2021-04-24 21:30:57 +05:30
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
path : str
|
|
|
|
|
Path to the cell data group.
|
|
|
|
|
|
2021-03-23 18:58:56 +05:30
|
|
|
|
"""
|
2023-09-22 18:44:54 +05:30
|
|
|
|
def get_cell_data_group(f: _h5py.File) -> str:
|
|
|
|
|
base_group = DREAM3D_base_group(f)
|
2021-04-05 13:43:08 +05:30
|
|
|
|
cells = tuple(f['/'.join([base_group,'_SIMPL_GEOMETRY','DIMENSIONS'])][()][::-1])
|
2021-03-23 18:58:56 +05:30
|
|
|
|
cell_data_group = f[base_group].visititems(lambda path,obj: path.split('/')[0] \
|
2022-06-10 15:30:54 +05:30
|
|
|
|
if isinstance(obj,_h5py._hl.dataset.Dataset) and _np.shape(obj)[:-1] == cells \
|
2021-03-23 18:58:56 +05:30
|
|
|
|
else None)
|
2023-09-22 18:44:54 +05:30
|
|
|
|
if cell_data_group is None:
|
|
|
|
|
raise ValueError(f'could not determine cell-data group in file "{fname}/{base_group}"')
|
|
|
|
|
return cell_data_group
|
2021-03-23 18:58:56 +05:30
|
|
|
|
|
2023-09-22 18:44:54 +05:30
|
|
|
|
if isinstance(fname,_h5py.File):
|
|
|
|
|
return get_cell_data_group(fname)
|
2021-03-23 18:58:56 +05:30
|
|
|
|
|
2023-09-22 18:44:54 +05:30
|
|
|
|
with _h5py.File(_Path(fname).expanduser(),'r') as f:
|
|
|
|
|
return get_cell_data_group(f)
|
2021-03-23 18:58:56 +05:30
|
|
|
|
|
2021-03-31 14:29:21 +05:30
|
|
|
|
|
2022-01-26 20:55:27 +05:30
|
|
|
|
def Bravais_to_Miller(*,
|
2022-11-23 02:56:15 +05:30
|
|
|
|
uvtw: _Optional[_np.ndarray] = None,
|
|
|
|
|
hkil: _Optional[_np.ndarray] = None) -> _np.ndarray:
|
2021-06-02 00:59:35 +05:30
|
|
|
|
"""
|
|
|
|
|
Transform 4 Miller–Bravais indices to 3 Miller indices of crystal direction [uvw] or plane normal (hkl).
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-01-22 04:20:16 +05:30
|
|
|
|
uvtw|hkil : numpy.ndarray, shape (...,4)
|
2021-06-02 00:59:35 +05:30
|
|
|
|
Miller–Bravais indices of crystallographic direction [uvtw] or plane normal (hkil).
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
2022-01-22 04:20:16 +05:30
|
|
|
|
uvw|hkl : numpy.ndarray, shape (...,3)
|
2021-06-02 00:59:35 +05:30
|
|
|
|
Miller indices of [uvw] direction or (hkl) plane normal.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
if (uvtw is not None) ^ (hkil is None):
|
2022-02-22 21:12:05 +05:30
|
|
|
|
raise KeyError('specify either "uvtw" or "hkil"')
|
2022-06-10 15:30:54 +05:30
|
|
|
|
axis,basis = (_np.array(uvtw),_np.array([[1,0,-1,0],
|
2022-06-10 17:33:43 +05:30
|
|
|
|
[0,1,-1,0],
|
|
|
|
|
[0,0, 0,1]])) \
|
2021-06-02 00:59:35 +05:30
|
|
|
|
if hkil is None else \
|
2022-06-10 15:30:54 +05:30
|
|
|
|
(_np.array(hkil),_np.array([[1,0,0,0],
|
2022-06-10 17:33:43 +05:30
|
|
|
|
[0,1,0,0],
|
|
|
|
|
[0,0,0,1]]))
|
2022-06-10 15:30:54 +05:30
|
|
|
|
return _np.einsum('il,...l',basis,axis)
|
2021-06-02 00:59:35 +05:30
|
|
|
|
|
2022-01-26 20:55:27 +05:30
|
|
|
|
def Miller_to_Bravais(*,
|
2022-11-23 02:56:15 +05:30
|
|
|
|
uvw: _Optional[_np.ndarray] = None,
|
|
|
|
|
hkl: _Optional[_np.ndarray] = None) -> _np.ndarray:
|
2021-06-02 00:59:35 +05:30
|
|
|
|
"""
|
|
|
|
|
Transform 3 Miller indices to 4 Miller–Bravais indices of crystal direction [uvtw] or plane normal (hkil).
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
2022-01-22 04:20:16 +05:30
|
|
|
|
uvw|hkl : numpy.ndarray, shape (...,3)
|
2021-06-02 00:59:35 +05:30
|
|
|
|
Miller indices of crystallographic direction [uvw] or plane normal (hkl).
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
2022-01-22 04:20:16 +05:30
|
|
|
|
uvtw|hkil : numpy.ndarray, shape (...,4)
|
2021-06-02 00:59:35 +05:30
|
|
|
|
Miller–Bravais indices of [uvtw] direction or (hkil) plane normal.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
if (uvw is not None) ^ (hkl is None):
|
2022-02-22 21:12:05 +05:30
|
|
|
|
raise KeyError('specify either "uvw" or "hkl"')
|
2022-06-10 15:30:54 +05:30
|
|
|
|
axis,basis = (_np.array(uvw),_np.array([[ 2,-1, 0],
|
2022-06-10 17:33:43 +05:30
|
|
|
|
[-1, 2, 0],
|
|
|
|
|
[-1,-1, 0],
|
|
|
|
|
[ 0, 0, 3]])/3) \
|
2021-06-02 00:59:35 +05:30
|
|
|
|
if hkl is None else \
|
2022-06-10 15:30:54 +05:30
|
|
|
|
(_np.array(hkl),_np.array([[ 1, 0, 0],
|
2022-06-10 17:33:43 +05:30
|
|
|
|
[ 0, 1, 0],
|
|
|
|
|
[-1,-1, 0],
|
|
|
|
|
[ 0, 0, 1]]))
|
2022-06-10 15:30:54 +05:30
|
|
|
|
return _np.einsum('il,...l',basis,axis)
|
2021-06-02 00:59:35 +05:30
|
|
|
|
|
|
|
|
|
|
2022-06-10 15:30:54 +05:30
|
|
|
|
def dict_prune(d: _Dict) -> _Dict:
|
2021-03-31 14:29:21 +05:30
|
|
|
|
"""
|
2021-04-02 03:03:45 +05:30
|
|
|
|
Recursively remove empty dictionaries.
|
2021-03-31 14:29:21 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
d : dict
|
2021-04-02 03:03:45 +05:30
|
|
|
|
Dictionary to prune.
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
pruned : dict
|
|
|
|
|
Pruned dictionary.
|
2021-03-31 14:29:21 +05:30
|
|
|
|
|
|
|
|
|
"""
|
2021-03-30 23:11:36 +05:30
|
|
|
|
# https://stackoverflow.com/questions/48151953
|
2021-03-31 14:29:21 +05:30
|
|
|
|
new = {}
|
|
|
|
|
for k,v in d.items():
|
2021-03-30 23:11:36 +05:30
|
|
|
|
if isinstance(v, dict):
|
2021-04-02 03:03:45 +05:30
|
|
|
|
v = dict_prune(v)
|
2021-03-30 23:11:36 +05:30
|
|
|
|
if not isinstance(v,dict) or v != {}:
|
2021-03-31 14:29:21 +05:30
|
|
|
|
new[k] = v
|
2021-05-07 23:12:23 +05:30
|
|
|
|
|
2021-03-31 14:29:21 +05:30
|
|
|
|
return new
|
2021-03-30 23:11:36 +05:30
|
|
|
|
|
2022-06-10 15:30:54 +05:30
|
|
|
|
def dict_flatten(d: _Dict) -> _Dict:
|
2021-03-31 14:29:21 +05:30
|
|
|
|
"""
|
2021-04-02 03:03:45 +05:30
|
|
|
|
Recursively remove keys of single-entry dictionaries.
|
2021-03-31 14:29:21 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
d : dict
|
2021-04-02 03:03:45 +05:30
|
|
|
|
Dictionary to flatten.
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
flattened : dict
|
|
|
|
|
Flattened dictionary.
|
2021-03-31 14:29:21 +05:30
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
if isinstance(d,dict) and len(d) == 1:
|
2021-03-31 17:57:36 +05:30
|
|
|
|
entry = d[list(d.keys())[0]]
|
2021-04-02 03:03:45 +05:30
|
|
|
|
new = dict_flatten(entry.copy()) if isinstance(entry,dict) else entry
|
2021-03-31 01:09:14 +05:30
|
|
|
|
else:
|
2021-04-02 03:03:45 +05:30
|
|
|
|
new = {k: (dict_flatten(v) if isinstance(v, dict) else v) for k,v in d.items()}
|
2021-03-31 17:57:36 +05:30
|
|
|
|
|
2021-03-31 14:29:21 +05:30
|
|
|
|
return new
|
2021-03-30 23:11:36 +05:30
|
|
|
|
|
|
|
|
|
|
2020-04-10 16:02:33 +05:30
|
|
|
|
####################################################################################################
|
|
|
|
|
# Classes
|
|
|
|
|
####################################################################################################
|
2022-01-22 12:20:52 +05:30
|
|
|
|
class ProgressBar:
|
2020-03-09 18:09:20 +05:30
|
|
|
|
"""
|
|
|
|
|
Report progress of an interation as a status bar.
|
|
|
|
|
|
|
|
|
|
Works for 0-based loops, ETA is estimated by linear extrapolation.
|
2019-09-20 01:02:15 +05:30
|
|
|
|
"""
|
2020-03-09 18:09:20 +05:30
|
|
|
|
|
2022-01-26 20:55:27 +05:30
|
|
|
|
def __init__(self,
|
|
|
|
|
total: int,
|
|
|
|
|
prefix: str,
|
|
|
|
|
bar_length: int):
|
2020-03-09 18:09:20 +05:30
|
|
|
|
"""
|
2023-01-24 12:45:03 +05:30
|
|
|
|
New progress bar.
|
2020-03-09 18:09:20 +05:30
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
----------
|
|
|
|
|
total : int
|
2020-03-15 02:23:48 +05:30
|
|
|
|
Total # of iterations.
|
2020-03-09 18:09:20 +05:30
|
|
|
|
prefix : str
|
2020-03-15 02:23:48 +05:30
|
|
|
|
Prefix string.
|
2020-03-09 18:09:20 +05:30
|
|
|
|
bar_length : int
|
2020-03-15 02:23:48 +05:30
|
|
|
|
Character length of bar.
|
2020-03-09 18:09:20 +05:30
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
self.total = total
|
|
|
|
|
self.prefix = prefix
|
|
|
|
|
self.bar_length = bar_length
|
2022-06-10 15:30:54 +05:30
|
|
|
|
self.time_start = self.time_last_update = _datetime.datetime.now()
|
2021-07-09 14:59:52 +05:30
|
|
|
|
self.fraction_last = 0.0
|
2020-03-09 18:09:20 +05:30
|
|
|
|
|
2023-12-09 19:10:58 +05:30
|
|
|
|
if _sys.stdout.isatty():
|
|
|
|
|
_sys.stdout.write(f"{self.prefix} {'░'*self.bar_length} 0% ETA n/a")
|
2020-03-09 18:09:20 +05:30
|
|
|
|
|
2022-01-26 20:55:27 +05:30
|
|
|
|
def update(self,
|
|
|
|
|
iteration: int) -> None:
|
2020-03-09 18:09:20 +05:30
|
|
|
|
|
|
|
|
|
fraction = (iteration+1) / self.total
|
|
|
|
|
|
2022-02-11 01:58:48 +05:30
|
|
|
|
if (filled_length := int(self.bar_length * fraction)) > int(self.bar_length * self.fraction_last) or \
|
2022-06-10 15:30:54 +05:30
|
|
|
|
_datetime.datetime.now() - self.time_last_update > _datetime.timedelta(seconds=10):
|
|
|
|
|
self.time_last_update = _datetime.datetime.now()
|
2020-06-24 20:32:15 +05:30
|
|
|
|
bar = '█' * filled_length + '░' * (self.bar_length - filled_length)
|
2022-06-10 15:30:54 +05:30
|
|
|
|
remaining_time = (_datetime.datetime.now() - self.time_start) \
|
2021-07-09 14:59:52 +05:30
|
|
|
|
* (self.total - (iteration+1)) / (iteration+1)
|
2023-12-09 19:10:58 +05:30
|
|
|
|
remaining_time -= _datetime.timedelta(microseconds=remaining_time.microseconds) # remove μs
|
|
|
|
|
if _sys.stdout.isatty():
|
2023-12-09 21:04:10 +05:30
|
|
|
|
_sys.stdout.write(f'\r{self.prefix} {bar} {fraction:>4.0%} ETA {remaining_time}')
|
2020-03-09 18:09:20 +05:30
|
|
|
|
|
2021-07-09 14:59:52 +05:30
|
|
|
|
self.fraction_last = fraction
|
2020-03-09 18:09:20 +05:30
|
|
|
|
|
2023-12-09 19:10:58 +05:30
|
|
|
|
if iteration == self.total - 1 and _sys.stdout.isatty():
|
|
|
|
|
_sys.stdout.write('\n')
|