I/O Operations Module

The I/O module handles file operations, data import/export, and format conversions.

XRayLabTool I/O Module.

This module contains input/output operations, file handling, and data persistence.

File Operations

File operations for XRayLabTool.

This module contains functions for loading and saving data files, including atomic scattering factor data and calculation results.

xraylabtool.io.file_operations.load_data_file(filename)[source]

Load data file with error handling.

Parameters:

filename (str) – Path to the data file

Return type:

ndarray

Returns:

Numpy array containing the loaded data

Raises:

DataFileError – If file cannot be loaded or parsed

xraylabtool.io.file_operations.save_calculation_results(results, filename, format_type='csv')[source]

Save calculation results to file.

Parameters:
  • results (Any) – Calculation results to save

  • filename (str) – Output file path

  • format_type (str) – Output format (‘csv’, ‘json’)

Return type:

None

xraylabtool.io.file_operations.export_to_csv(data, filename, **kwargs)[source]

Export data to CSV format.

Return type:

None

Parameters:
xraylabtool.io.file_operations.export_to_json(data, filename, **kwargs)[source]

Export data to JSON format.

Return type:

None

Parameters:

Data Export and Formatting

Data export and formatting utilities for XRayLabTool.

This module contains functions for formatting and exporting calculation results in various formats suitable for different use cases.

xraylabtool.io.data_export.format_xray_result(result, format_type='table', fields=None, precision=6)[source]

Format XRayResult for display or export.

Parameters:
  • result (Any) – XRayResult object to format

  • format_type (str) – Output format (‘table’, ‘csv’, ‘json’)

  • fields (list[str] | None) – Specific fields to include (None for all)

  • precision (int) – Decimal precision for numeric values

Return type:

str

Returns:

Formatted string representation

xraylabtool.io.data_export.format_calculation_summary(results, format_type='table')[source]

Format a summary of multiple calculation results.

Parameters:
  • results (list[Any]) – List of XRayResult objects

  • format_type (str) – Output format type

Return type:

str

Returns:

Formatted summary string

Available Functions

File Loading and Saving

xraylabtool.io.file_operations.load_data_file(filename)[source]

Load data file with error handling.

Parameters:

filename (str) – Path to the data file

Return type:

ndarray

Returns:

Numpy array containing the loaded data

Raises:

DataFileError – If file cannot be loaded or parsed

Load data file with error handling.

Example:

from xraylabtool.io.file_operations import load_data_file

data = load_data_file("atomic_data.nff")
print(data.head())
xraylabtool.io.file_operations.save_calculation_results(results, filename, format_type='csv')[source]

Save calculation results to file.

Parameters:
  • results (Any) – Calculation results to save

  • filename (str) – Output file path

  • format_type (str) – Output format (‘csv’, ‘json’)

Return type:

None

Save calculation results to file.

Example:

from xraylabtool.io.file_operations import save_calculation_results

# Save as CSV
save_calculation_results(results, "output.csv", format_type="csv")

# Save as JSON
save_calculation_results(results, "output.json", format_type="json")

Export Functions

xraylabtool.io.file_operations.export_to_csv(data, filename, **kwargs)[source]

Export data to CSV format.

Return type:

None

Parameters:

Export data to CSV format.

Example:

from xraylabtool.io.file_operations import export_to_csv

export_to_csv(calculation_results, "results.csv")
xraylabtool.io.file_operations.export_to_json(data, filename, **kwargs)[source]

Export data to JSON format.

Return type:

None

Parameters:

Export data to JSON format.

Example:

from xraylabtool.io.file_operations import export_to_json

export_to_json(calculation_results, "results.json")

Data Formatting

xraylabtool.io.data_export.format_xray_result(result, format_type='table', fields=None, precision=6)[source]

Format XRayResult for display or export.

Parameters:
  • result (Any) – XRayResult object to format

  • format_type (str) – Output format (‘table’, ‘csv’, ‘json’)

  • fields (list[str] | None) – Specific fields to include (None for all)

  • precision (int) – Decimal precision for numeric values

Return type:

str

Returns:

Formatted string representation

Format XRayResult for display or export.

Example:

from xraylabtool.io.data_export import format_xray_result

# Format as table
table_output = format_xray_result(result, format_type="table")
print(table_output)

# Format as JSON
json_output = format_xray_result(result, format_type="json")

# Format as CSV
csv_output = format_xray_result(result, format_type="csv")
xraylabtool.io.data_export.format_calculation_summary(results, format_type='table')[source]

Format a summary of multiple calculation results.

Parameters:
  • results (list[Any]) – List of XRayResult objects

  • format_type (str) – Output format type

Return type:

str

Returns:

Formatted summary string

Format a summary of multiple calculation results.

Example:

from xraylabtool.io.data_export import format_calculation_summary

summary = format_calculation_summary(results_list, format_type="table")
print(summary)

Supported File Formats

Input Formats: - Space-separated (.nff): Atomic scattering factor data files - CSV: General comma-separated data files

Output Formats: - CSV: Standard comma-separated format - JSON: Pretty-printed JSON with proper numeric precision

Usage Examples

Basic File Operations:

from xraylabtool.io.file_operations import load_data_file, save_calculation_results
from xraylabtool.io.data_export import format_xray_result

# Load atomic data
atomic_data = load_data_file("element.nff")

# Save calculation results
save_calculation_results(results, "output.csv")

# Format results for display
formatted_result = format_xray_result(result, format_type="table", precision=4)
print(formatted_result)

Error Handling

The I/O module provides error handling:

from xraylabtool.io.file_operations import load_data_file
from xraylabtool.exceptions import DataFileError

try:
    data = load_data_file("data.nff")
except FileNotFoundError:
    print("Input file not found")
except DataFileError as e:
    print(f"Error loading data file: {e}")
except Exception as e:
    print(f"Unexpected error: {e}")

Performance Considerations

File Loading: - Automatic detection of .nff format for space-separated data - Efficient pandas-based CSV parsing - Proper error handling for malformed files

Data Export: - Configurable numeric precision for output formatting - Support for both single results and result collections - Memory-efficient processing for large datasets