utils
environment
can_update_ipython_display()
Return True if IPython HTML display utilities are available.
This indicates we can safely construct IPython.display.HTML and
update a display handle.
Source code in src/easydiffraction/utils/environment.py
117 118 119 120 121 122 123 124 125 126 127 128 | |
can_use_ipython_display(handle)
Return True if we can update the given IPython DisplayHandle.
Combines type checking of the handle with availability of IPython HTML utilities.
Source code in src/easydiffraction/utils/environment.py
131 132 133 134 135 136 137 138 139 140 | |
in_colab()
Determines if the current environment is Google Colab.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if running in Google Colab, False otherwise. |
Source code in src/easydiffraction/utils/environment.py
28 29 30 31 32 33 34 35 36 37 | |
in_github_ci()
Return True when running under GitHub Actions CI.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if env var |
bool
|
otherwise. |
Source code in src/easydiffraction/utils/environment.py
78 79 80 81 82 83 84 85 | |
in_jupyter()
Return True when running inside a Jupyter Notebook.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if inside a Jupyter Notebook, False otherwise. |
Source code in src/easydiffraction/utils/environment.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
in_pycharm()
Determines if the current environment is PyCharm.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if running inside PyCharm, False otherwise. |
Source code in src/easydiffraction/utils/environment.py
19 20 21 22 23 24 25 | |
is_ipython_display_handle(obj)
Return True if obj is an IPython DisplayHandle instance.
Tries to import IPython.display.DisplayHandle and uses
isinstance when available. Falls back to a conservative
module name heuristic if IPython is missing. Any errors result
in False.
Source code in src/easydiffraction/utils/environment.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
logging
Project-wide logging utilities built on top of Rich.
Provides a shared Rich console, a compact/verbose logger with consistent formatting, Jupyter traceback handling, and a small printing façade tailored to the configured console.
ConsoleManager
Central provider for shared Rich Console instance.
Source code in src/easydiffraction/utils/logging.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
get()
classmethod
Return a shared Rich Console instance.
Source code in src/easydiffraction/utils/logging.py
110 111 112 113 114 115 116 117 118 | |
ConsolePrinter
Printer utility that prints objects to the shared console with left padding.
Source code in src/easydiffraction/utils/logging.py
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | |
chapter(title)
classmethod
Formats a chapter header with bold magenta text, uppercase, and padding.
Source code in src/easydiffraction/utils/logging.py
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | |
print(*objects, **kwargs)
classmethod
Print objects to the console with left padding.
- Renderables (Rich types like Text, Table, Panel, etc.) are kept as-is.
- Non-renderables (ints, floats, Path, etc.) are converted to str().
Source code in src/easydiffraction/utils/logging.py
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | |
section(title)
classmethod
Formats a section header with bold green text.
Source code in src/easydiffraction/utils/logging.py
553 554 555 556 557 558 559 560 561 | |
ExceptionHookManager
Handles installation and restoration of exception hooks.
Source code in src/easydiffraction/utils/logging.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |
install_compact_hook(logger)
staticmethod
Install a compact exception hook that logs message-only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger
|
Logger used to emit the error message. |
required |
Source code in src/easydiffraction/utils/logging.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |
install_jupyter_traceback_suppressor(logger)
staticmethod
Install a Jupyter/IPython custom exception handler that suppresses tracebacks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger
|
Logger used to emit error messages. |
required |
Source code in src/easydiffraction/utils/logging.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |
install_verbose_hook(logger)
staticmethod
Install a verbose exception hook that prints rich tracebacks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger
|
Logger used to emit the exception information. |
required |
Source code in src/easydiffraction/utils/logging.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
restore_original_hook()
staticmethod
Restore the original sys.excepthook if it was overridden.
Source code in src/easydiffraction/utils/logging.py
254 255 256 257 258 | |
IconifiedRichHandler
Bases: RichHandler
RichHandler that uses icons for log levels in compact mode, Rich default in verbose mode.
Source code in src/easydiffraction/utils/logging.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
Logger
Centralized logging with Rich formatting and two modes.
Environment variables: ED_LOG_MODE: set default mode ('verbose' or 'compact') ED_LOG_LEVEL: set default level ('DEBUG', 'INFO', etc.)
Source code in src/easydiffraction/utils/logging.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 | |
Level
Bases: IntEnum
Mirror stdlib logging levels.
Source code in src/easydiffraction/utils/logging.py
332 333 334 335 336 337 338 339 340 341 342 343 | |
Mode
Bases: Enum
Output modes (see :class:Logger).
Source code in src/easydiffraction/utils/logging.py
322 323 324 325 326 327 328 329 330 | |
Reaction
Bases: Enum
Reaction to errors (see :class:Logger).
Source code in src/easydiffraction/utils/logging.py
345 346 347 348 349 350 351 352 353 | |
configure(*, mode=None, level=None, reaction=None, rich_tracebacks=None)
classmethod
Configure logger.
mode: default COMPACT in Jupyter else VERBOSE level: minimum log level rich_tracebacks: override automatic choice
Environment variables: ED_LOG_MODE: set default mode ('verbose' or 'compact') ED_LOG_LEVEL: set default level ('DEBUG', 'INFO', etc.)
Source code in src/easydiffraction/utils/logging.py
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |
handle(*messages, level=Level.ERROR, exc_type=AttributeError)
classmethod
Route a log message (see class docs for policy).
Source code in src/easydiffraction/utils/logging.py
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 | |
LoggerConfig
Facade for logger configuration, delegates to helpers.
Source code in src/easydiffraction/utils/logging.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
configure(logger, *, mode, level, rich_tracebacks)
staticmethod
Configure the logger with RichHandler and exception hooks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger
|
Logger instance to configure. |
required |
mode
|
'Logger.Mode'
|
Output mode (compact or verbose). |
required |
level
|
'Logger.Level'
|
Minimum log level to emit. |
required |
rich_tracebacks
|
bool
|
Whether to enable Rich tracebacks. |
required |
Source code in src/easydiffraction/utils/logging.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
setup_handlers(logger, *, level, rich_tracebacks, mode='compact')
staticmethod
Install Rich handler and optional Jupyter traceback support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger
|
Logger instance to attach handlers to. |
required |
level
|
int
|
Minimum log level to emit. |
required |
rich_tracebacks
|
bool
|
Whether to enable Rich tracebacks. |
required |
mode
|
str
|
Output mode name ("compact" or "verbose"). |
'compact'
|
Source code in src/easydiffraction/utils/logging.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
stripped_package_version(package_name)
Get the installed version of the specified package, stripped of any local version part.
Returns only the public version segment (e.g., '1.2.3' or '1.2.3.post4'), omitting any local segment (e.g., '+d136').
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package_name
|
str
|
The name of the package to query. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: The public version string, or None if the package |
str | None
|
is not installed. |
Source code in src/easydiffraction/utils/utils.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
utils
download_all_tutorials(destination='tutorials', overwrite=False)
Download all available tutorial notebooks.
Example
paths = download_all_tutorials(destination="tutorials")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
destination
|
str
|
Directory to save the files into (created if missing). |
'tutorials'
|
overwrite
|
bool
|
Whether to overwrite files if they already exist. |
False
|
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: List of full paths to the downloaded files. |
Source code in src/easydiffraction/utils/utils.py
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | |
download_data(id, destination='data', overwrite=False)
Download a dataset by numeric ID using the remote diffraction index.
Example
path = download_data(id=12, destination="data")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
int | str
|
Numeric dataset id (e.g. 12). |
required |
destination
|
str
|
Directory to save the file into (created if missing). |
'data'
|
overwrite
|
bool
|
Whether to overwrite the file if it already exists. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Full path to the downloaded file as string. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the id is not found in the index. |
ValueError
|
If the resolved URL is not HTTP/HTTPS. |
Source code in src/easydiffraction/utils/utils.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
download_tutorial(id, destination='tutorials', overwrite=False)
Download a tutorial notebook by numeric ID.
Example
path = download_tutorial(id=1, destination="tutorials")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
int | str
|
Numeric tutorial id (e.g. 1). |
required |
destination
|
str
|
Directory to save the file into (created if missing). |
'tutorials'
|
overwrite
|
bool
|
Whether to overwrite the file if it already exists. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Full path to the downloaded file as string. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the id is not found in the index. |
ValueError
|
If the resolved URL is not HTTP/HTTPS. |
Source code in src/easydiffraction/utils/utils.py
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |
get_value_from_xye_header(file_path, key)
Extracts a floating point value from the first line of the file, corresponding to the given key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to the input file. |
required |
key
|
str
|
The key to extract ('DIFC' or 'two_theta'). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
The extracted value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the key is not found. |
Source code in src/easydiffraction/utils/utils.py
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 | |
list_tutorials()
Display a table of available tutorial notebooks.
Shows tutorial ID, filename, title, and description for all tutorials available for the current version of easydiffraction.
Source code in src/easydiffraction/utils/utils.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
package_version(package_name)
Get the installed version string of the specified package.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package_name
|
str
|
The name of the package to query. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: The raw version string (may include local part, |
str | None
|
e.g., '1.2.3+abc123'), or None if the package is not installed. |
Source code in src/easydiffraction/utils/utils.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
render_cif(cif_text)
Display the CIF text as a formatted table in Jupyter Notebook or terminal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cif_text
|
The CIF text to display. |
required |
Source code in src/easydiffraction/utils/utils.py
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 | |
show_version()
Print the installed version of the easydiffraction package.
Source code in src/easydiffraction/utils/utils.py
461 462 463 464 465 466 467 468 | |
str_to_ufloat(s, default=None)
Parse a CIF-style numeric string into a ufloat with an optional
uncertainty.
Examples of supported input: - "3.566" → ufloat(3.566, nan) - "3.566(2)" → ufloat(3.566, 0.002) - None → ufloat(default, nan)
Behavior:
- If the input string contains a value with parentheses (e.g.
"3.566(2)"), the number in parentheses is interpreted as an
estimated standard deviation (esd) in the last digit(s).
- If the input string has no parentheses, an uncertainty of NaN is
assigned to indicate "no esd provided".
- If parsing fails, the function falls back to the given default
value with uncertainty NaN.
Parameters
s : str or None
Numeric string in CIF format (e.g. "3.566", "3.566(2)") or None.
default : float or None, optional
Default value to use if s is None or parsing fails.
Defaults to None.
Returns:
UFloat
An uncertainties.UFloat object with the parsed value and
uncertainty. The uncertainty will be NaN if not specified or
parsing failed.
Source code in src/easydiffraction/utils/utils.py
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 | |
stripped_package_version(package_name)
Get the installed version of the specified package, stripped of any local version part.
Returns only the public version segment (e.g., '1.2.3' or '1.2.3.post4'), omitting any local segment (e.g., '+d136').
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package_name
|
str
|
The name of the package to query. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: The public version string, or None if the package |
str | None
|
is not installed. |
Source code in src/easydiffraction/utils/utils.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
tof_to_d(tof, offset, linear, quad, quad_eps=1e-20)
Convert time-of-flight (TOF) to d-spacing using a quadratic calibration.
Model
TOF = offset + linear * d + quad * d²
The function
- Uses a linear fallback when the quadratic term is effectively zero.
- Solves the quadratic for d and selects the smallest positive, finite root.
- Returns NaN where no valid solution exists.
- Expects
tofas a NumPy array; output matches its shape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tof
|
ndarray
|
Time-of-flight values (µs). Must be a NumPy array. |
required |
offset
|
float
|
Calibration offset (µs). |
required |
linear
|
float
|
Linear calibration coefficient (µs/Å). |
required |
quad
|
float
|
Quadratic calibration coefficient (µs/Ų). |
required |
quad_eps
|
float
|
Threshold to treat |
1e-20
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: d-spacing values (Å), NaN where invalid. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in src/easydiffraction/utils/utils.py
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 | |
twotheta_to_d(twotheta, wavelength)
Convert 2-theta to d-spacing using Bragg's law.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
twotheta
|
float or ndarray
|
2-theta angle in degrees. |
required |
wavelength
|
float
|
Wavelength in Å. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
d |
float or ndarray
|
d-spacing in Å. |
Source code in src/easydiffraction/utils/utils.py
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 | |