<commit_before># -*- coding: utf-8 -*-
"""
Created on Tue Oct 20 17:40:07 2015

@author: casimp
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from six import string_types, binary_type
import h5py
import os
import numpy as np
from pyxpb.detectors import MonoDetector, EnergyDetector


def pyxe_to_hdf5(fname, pyxe, overwrite=False):
    """ Saves pyxe data object - specifically the analyzed data and basic
    detector/experimental setup - to a hdf5 file. Stores progression point
    so analysis can be continued where it was left off.

    Args:
        fname (str): File path.
        pyxe: pyXe data object
        overwrite (bool): Option to overwrite if same filename is specified.
    """
    data_ids = ['ndim', 'd1', 'd2', 'd3', 'T', 'q', 'I', 'phi',
                'peaks', 'peaks_err', 'fwhm', 'fwhm_err',
                'strain', 'strain_err', 'strain_tensor',
                'E', 'v', 'G', 'stress_state', 'analysis_state']

    detector_ids = ['method', '_det_param', '_back', 'materials']

    write = 'w' if overwrite else 'w-'
    with h5py.File(fname, write) as f:

        for name in data_ids:
            try:
                d_path = 'pyxe_analysis/%s' % name
                d = getattr(pyxe, name)
                d = d.encode() if isinstance(d, string_types) else d
                if d is not None:
                    if name == 'I':
                        f.create_dataset(d_path, data=d, compression='gzip')
                    else:
                        f.create_dataset(d_path, data=d)
            except AttributeError:
                pass

        for name in detector_ids:
            d_path = 'setup/%s' % name
            data = getattr(pyxe.detector, name)
            data = data.encode() if isinstance(data, string_types) else data
            if data is not None:
                if name == 'materials':
                    for mat in data:
                        d = [data[mat][x] for x in ['a', 'b', 'weight']]
                        d_path_new = '{}/{}'.format(d_path, mat)
                        f.create_dataset(d_path_new, data=np.array(d))
                elif name == '_det_param':
                    for param in data:
                        d = data[param]
                        d_path_new = '{}/{}'.format(d_path, param)
                        f.create_dataset(d_path_new, data=np.array(d))
                else:<commit_msg>Tweak to the data_io for temperature profiles.<commit_after># -*- coding: utf-8 -*-
"""
Created on Tue Oct 20 17:40:07 2015

@author: casimp
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from six import string_types, binary_type
import h5py
import os
import numpy as np
from pyxpb.detectors import MonoDetector, EnergyDetector


def pyxe_to_hdf5(fname, pyxe, overwrite=False):
    """ Saves pyxe data object - specifically the analyzed data and basic
    detector/experimental setup - to a hdf5 file. Stores progression point
    so analysis can be continued where it was left off.

    Args:
        fname (str): File path.
        pyxe: pyXe data object
        overwrite (bool): Option to overwrite if same filename is specified.
    """
    data_ids = ['ndim', 'd1', 'd2', 'd3', 'q', 'I', 'phi',
                'peaks', 'peaks_err', 'fwhm', 'fwhm_err',
                'strain', 'strain_err', 'strain_tensor',
                'E', 'v', 'G', 'T', 'stress_state', 'analysis_state']

    detector_ids = ['method', '_det_param', '_back', 'materials']

    write = 'w' if overwrite else 'w-'
    with h5py.File(fname, write) as f:

        for name in data_ids:
            
            try:
                d_path = 'pyxe_analysis/%s' % name
                d = getattr(pyxe, name)
                d = d.encode() if isinstance(d, string_types) else d
                if d is not None:
                    if name == 'I':
                        f.create_dataset(d_path, data=d, compression='gzip')
                    else:
                        f.create_dataset(d_path, data=d)
            except AttributeError:
                pass

        for name in detector_ids:
            d_path = 'setup/%s' % name
            data = getattr(pyxe.detector, name)
            data = data.encode() if isinstance(data, string_types) else data
            if data is not None:
                if name == 'materials':
                    for mat in data:
                        d = [data[mat][x] for x in ['a', 'b', 'weight']]
                        d_path_new = '{}/{}'.format(d_path, mat)
                        f.create_dataset(d_path_new, data=np.array(d))
                elif name == '_det_param':
                    for param in data:
                        d = data[param]
                        d_path_new = '{}/{}'.format(d_path, param)
                        f.create_dataset(d_path_new, data=np.array(d))
                else:
{'fraction_non_alphanumeric': 0.10840269520412207, 'fraction_numerical': 0.007927070947284977, 'mean_word_length': 1.808569838619922, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3047720', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1441, 'n_words': 459}