io
ascii
Helpers for loading numeric data from ASCII files.
extract_data_paths_from_dir(dir_path, file_pattern='*')
List data files in a directory and return their sorted paths.
Hidden files (names starting with '.' or '__') are excluded.
The returned paths are sorted lexicographically by file name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dir_path
|
str | Path
|
Path to the directory containing data files. |
required |
file_pattern
|
str
|
Glob pattern to filter files (e.g. |
'*'
|
Returns:
| Type | Description |
|---|---|
list[str]
|
Sorted absolute paths to the matching data files. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If dir_path does not exist or is not a directory. |
ValueError
|
If no matching data files are found. |
Source code in src/easydiffraction/io/ascii.py
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 | |
extract_data_paths_from_zip(zip_path, destination=None)
Extract all files from a ZIP archive and return their paths.
Files are extracted into destination when provided, or into a
temporary directory that persists for the lifetime of the process.
The returned paths are sorted lexicographically by file name so that
numbered data files (e.g. scan_001.dat, scan_002.dat) appear
in natural order. Hidden files and directories (names starting with
'.' or '__') are excluded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
zip_path
|
str | Path
|
Path to the ZIP archive. |
required |
destination
|
str | Path | None
|
Directory to extract files into. When |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
Sorted absolute paths to the extracted data files. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If zip_path does not exist. |
ValueError
|
If the archive contains no usable data files. |
Source code in src/easydiffraction/io/ascii.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
extract_metadata(file_path, pattern)
Extract a single numeric value from a file using a regex pattern.
The entire file content is searched (not just the header). The
first match is used. The regex must contain exactly one capture
group whose match is convertible to float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
Path to the input file. |
required |
pattern
|
str
|
Regex with one capture group that matches the numeric value. |
required |
Returns:
| Type | Description |
|---|---|
float | None
|
The extracted value, or |
Source code in src/easydiffraction/io/ascii.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
extract_project_from_zip(zip_path, destination=None)
Extract a project directory from a ZIP archive.
The archive must contain exactly one directory with a
project.cif file. Files are extracted into destination when
provided, or into a temporary directory that persists for the
lifetime of the process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
zip_path
|
str | Path
|
Path to the ZIP archive containing the project. |
required |
destination
|
str | Path | None
|
Directory to extract into. When |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Absolute path to the extracted project directory (the directory
that contains |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If zip_path does not exist. |
ValueError
|
If the archive does not contain a |
Source code in src/easydiffraction/io/ascii.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 | |
load_numeric_block(data_path)
Load a numeric block from an ASCII file, skipping non-numeric lines.
Each line is tested individually: lines whose whitespace-separated tokens are all valid floats are kept; everything else (headers, footers, comment lines) is silently discarded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_path
|
str | Path
|
Path to the ASCII data file. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
2-D array of the parsed numeric data. |
Raises:
| Type | Description |
|---|---|
OSError
|
If no numeric lines can be found in the file. |
Source code in src/easydiffraction/io/ascii.py
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 | |
cif
handler
Minimal CIF tag handler used by descriptors/parameters.
CifHandler
Canonical CIF handler used by descriptors/parameters.
Holds CIF tags (names) and attaches to an owning descriptor so it can derive a stable uid if needed.
Source code in src/easydiffraction/io/cif/handler.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
attach(owner)
Attach to a descriptor or parameter instance.
Source code in src/easydiffraction/io/cif/handler.py
20 21 22 | |
names
property
List of CIF tag names associated with the owner.
uid
property
Unique identifier taken from the owner, if attached.
parse
document_from_path(path)
Read a CIF document from a file path.
Source code in src/easydiffraction/io/cif/parse.py
12 13 14 | |
document_from_string(text)
Read a CIF document from a raw text string.
Source code in src/easydiffraction/io/cif/parse.py
17 18 19 | |
name_from_block(block)
Extract a model name from the CIF block name.
Source code in src/easydiffraction/io/cif/parse.py
27 28 29 30 | |
pick_sole_block(doc)
Pick the sole data block from a CIF document.
Source code in src/easydiffraction/io/cif/parse.py
22 23 24 | |
read_cif_str(block, tag)
Read a single string value from a CIF block by tag.
Strips surrounding single or double quotes when present, and returns
None for absent tags or CIF unknown/inapplicable markers (?
/ .).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block
|
Block
|
Parsed CIF data block to read from. |
required |
tag
|
str
|
CIF tag to look up (e.g. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Unquoted string value, or |
Source code in src/easydiffraction/io/cif/parse.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
serialize
analysis_from_cif(analysis, cif_text)
Populate an Analysis instance from CIF text.
Reads the fitting engine, fit mode, aliases, constraints, and joint-fit experiment weights from the given CIF string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
analysis
|
object
|
The |
required |
cif_text
|
str
|
CIF text content of |
required |
Source code in src/easydiffraction/io/cif/serialize.py
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 | |
analysis_to_cif(analysis)
Render analysis metadata, aliases, and constraints to CIF.
Source code in src/easydiffraction/io/cif/serialize.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
category_collection_from_cif(self, block)
Populate a CategoryCollection from a CIF loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
self
|
CategoryCollection
|
The collection instance to populate. |
required |
block
|
Block
|
Parsed CIF block to read the loop from. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the collection has no |
Source code in src/easydiffraction/io/cif/serialize.py
640 641 642 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 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 | |
category_collection_to_cif(collection, max_display=None)
Render a CategoryCollection-like object to CIF text.
Uses first item to build loop header, then emits rows for each item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collection
|
object
|
A |
required |
max_display
|
int | None
|
When set to a positive integer, truncate the output to at most
this many rows (half from the start, half from the end) with an
|
None
|
Returns:
| Type | Description |
|---|---|
str
|
CIF text representing the collection as a loop. |
Source code in src/easydiffraction/io/cif/serialize.py
189 190 191 192 193 194 195 196 197 198 199 200 201 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 | |
category_item_from_cif(self, block, idx=0)
Populate each parameter from CIF block at given loop index.
Source code in src/easydiffraction/io/cif/serialize.py
563 564 565 566 567 568 569 570 | |
category_item_to_cif(item)
Render a CategoryItem-like object to CIF text.
Expects item.parameters iterable of params with
_cif_handler.names and value.
Source code in src/easydiffraction/io/cif/serialize.py
138 139 140 141 142 143 144 145 146 | |
datablock_collection_to_cif(collection)
Render a collection of datablocks by joining their CIF blocks.
Source code in src/easydiffraction/io/cif/serialize.py
289 290 291 | |
datablock_item_to_cif(datablock, max_loop_display=None)
Render a DatablockItem-like object to CIF text.
Emits a data_ header and then concatenates category CIF sections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
datablock
|
object
|
A |
required |
max_loop_display
|
int | None
|
When set, truncate loop categories to this many rows. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
CIF text representing the datablock as a loop. |
Source code in src/easydiffraction/io/cif/serialize.py
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 | |
experiment_to_cif(experiment)
Render an experiment: datablock part plus measured data.
Source code in src/easydiffraction/io/cif/serialize.py
339 340 341 | |
format_param_value(param)
Format a parameter value for CIF output, encoding the free flag.
CIF convention for numeric parameters:
- Fixed or constrained parameter: plain value, e.g.
3.89090000 - Free parameter without uncertainty: value with empty brackets,
e.g.
3.89090000() - Free parameter with uncertainty: value with esd in brackets,
e.g.
3.89090000(200000)
Constrained (dependent) parameters are always written without
brackets, even if their free flag is True, because they are
not independently varied by the minimizer.
Non-numeric parameters and descriptors without a free attribute
are formatted with :func:format_value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
param
|
object
|
A descriptor or parameter exposing |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted CIF value string. |
Source code in src/easydiffraction/io/cif/serialize.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 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 119 120 121 122 | |
format_value(value)
Format a single CIF value for output.
.. note:: The precision must be high enough so that the minimizer's finite-difference Jacobian probes (typically ~1e-8 relative) survive the float→string→float round-trip through CIF.
Source code in src/easydiffraction/io/cif/serialize.py
31 32 33 34 35 36 37 38 39 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 | |
param_from_cif(self, block, idx=0)
Populate a single descriptor from a CIF block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
self
|
GenericDescriptorBase
|
The descriptor instance to populate. |
required |
block
|
Block
|
Parsed CIF block to read values from. |
required |
idx
|
int
|
Row index used when the tag belongs to a loop. |
0
|
Source code in src/easydiffraction/io/cif/serialize.py
503 504 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 | |
param_to_cif(param)
Render a single descriptor/parameter to a CIF line.
Expects param to expose _cif_handler.names and value.
Free parameters are written with uncertainty brackets (see
:func:format_param_value).
Source code in src/easydiffraction/io/cif/serialize.py
125 126 127 128 129 130 131 132 133 134 135 | |
project_info_from_cif(info, cif_text)
Populate a ProjectInfo instance from CIF text.
Reads _project.id, _project.title, and
_project.description from the given CIF string and sets them on
the info object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
info
|
object
|
The |
required |
cif_text
|
str
|
CIF text content of |
required |
Source code in src/easydiffraction/io/cif/serialize.py
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 | |
project_info_to_cif(info)
Render ProjectInfo to CIF text (id, title, description).
Source code in src/easydiffraction/io/cif/serialize.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | |
project_to_cif(project)
Render a whole project by concatenating sections when present.
Source code in src/easydiffraction/io/cif/serialize.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |
summary_to_cif(_summary)
Render a summary CIF block (placeholder for now).
Source code in src/easydiffraction/io/cif/serialize.py
362 363 364 | |