# Data Exporting You can export most result data that `jcmsuite` parses in several formats. This section uses a simple {class}`PostprocessRecord ` as an example: ```python postprocess = results.postprocesses[0] ``` ## Mapping/Dictionary Use {meth}`postprocess.to_dict() ` to export the data to a dictionary. A dictionary stores named values. The older {meth}`to_mapping() ` name remains available during the compatibility period and emits a deprecation warning. {class}`PostprocessHeader ` also supports dictionary export. ```python print(postprocess.to_dict()) # { # 'title': 'ElectromagneticFieldEnergyFlux', # 'DomainIdFirst': array([1], dtype=int32), # 'DomainIdSecond': array([1], dtype=int32), # 'ElectromagneticFieldEnergyFlux': {0: array([2.12696711e-09-4.23655247e-11j])}, # 'AbsElectromagneticFieldEnergyFlux': {0: array([2.13399891e-09])} # } ``` ```{admonition} Migration note :class: note The {meth}`to_dict() ` method produces results that are most closely aligned with the results obtained using the legacy `jcmwave` Python package. ``` ## Pandas DataFrame ```{note} DataFrame exports require the pandas extra. Install it with `python -m pip install "jcmsuite[pandas]"`. See [Installation](../installation.md). ``` For numeric table collections, select a solver-parameter derivative from the postprocess record. The selected collection keeps the original table names. This includes duplicate-title suffixes and names generated for untitled tables. When a derivative is available for only some tables, the selected export contains only those tables. It does not fill missing derivatives with zero. ```python first_derivative = postprocess.derivatives["width_parameter"] second_derivative = first_derivative.derivatives["angle_of_incidence"] values = second_derivative.to_dict() frame = second_derivative.to_df() # Table collections use long form. tree = second_derivative.to_xarray() ``` Call {meth}`postprocess.derivatives.keys() ` to list derivative directions. A missing selection raises `KeyError` with the requested derivative path. Use {meth}`postprocess.to_df() ` to export the data to a `pandas` DataFrame. A DataFrame is a labeled table with rows and columns. Use the optional `orient=` keyword to request `"wide"` or `"long"` form. If omitted, the result data selects its default form: values normally use wide form, while metadata and batch exports use long form. ```python print(postprocess.to_df()) # row_index DomainIdFirst DomainIdSecond ElectromagneticFieldEnergyFlux.0 ... # 0 0 1 1 2.126967e-09-4.236552e-11j ... ``` ### Orientation Some result data types support long and wide DataFrame forms. Use `orient=` with `"long"` or `"wide"`. Long-form DataFrames are useful with plotting packages such as `seaborn`. ```python print(postprocess.to_df(orient="long")) # postprocess_title source_file ... value # 0 ElectromagneticFieldEnergyFlux /home/... ... 1.000000e+00+0.000000e+00j # 1 ElectromagneticFieldEnergyFlux /home/... ... 1.000000e+00+0.000000e+00j # 2 ElectromagneticFieldEnergyFlux /home/... ... 2.126967e-09-4.236552e-11j # 3 ElectromagneticFieldEnergyFlux /home/... ... 2.133999e-09+0.000000e+00j # # [4 rows x 6 columns] ``` ```{admonition} Limitations :class: note Some data types, including field exports, do not support pandas DataFrame export. ``` ## Xarray Dataset/Datatree ```{note} Xarray exports require the xarray extra. Install it with `python -m pip install "jcmsuite[xarray]"`. See [Installation](../installation.md). ``` An `xarray` DataTree stores labeled array data in a tree structure. Use this format when the result data has two or more dimensions, such as field data on a grid. Use {meth}`postprocess.to_xarray() ` to export the data to an `xarray` DataTree. The main dataset is typically accessible at the tree root. Leaf nodes can contain derivative information. DataTrees provide direct access to data in matrix or tensor form. ```python print(postprocess.to_xarray()) # # Group: / # Dimensions: (field_index: 1, sample_index: 1) # Coordinates: # * field_index (field_index) int64 8B 0 # * sample_index (sample_index) int64 8B 0 # DomainIdFirst (sample_index) int32 4B 1 # DomainIdSecond (sample_index) int32 4B 1 # Data variables: # ElectromagneticFieldEnergyFlux (field_index, sample_index) complex128 16B ... # AbsElectromagneticFieldEnergyFlux (field_index, sample_index) float64 8B ... # is_valid (field_index, sample_index) bool 1B True # is_padded (field_index, sample_index) bool 1B False # Attributes: # field_names: ('ElectromagneticFieldEnergyFlux', 'AbsElectromagneti... # postprocess_title: ElectromagneticFieldEnergyFlux # source_file: /home/... ``` Access data variables as you access dictionary values. ```python data = postprocess.to_xarray()["ElectromagneticFieldEnergyFlux"] print(data) # array([[2.12696711e-09-4.23655247e-11j]]) # Coordinates: # * field_index (field_index) int64 8B 0 # * sample_index (sample_index) int64 8B 0 # DomainIdFirst (sample_index) int32 4B 1 # DomainIdSecond (sample_index) int32 4B 1 ``` You can use the resulting data directly with `numpy`. ```python import numpy as np print(np.abs(data)) # array([[2.12738899e-09]]) ``` You can also convert the data to a `numpy` array. ```python print(data.to_numpy()) # [[2.12696711e-09-4.23655247e-11j]] ``` For more information about `xarray`, please visit [the `xarray` documentation](https://docs.xarray.dev/en/stable/).