<commit_before># Copyright 2014 Diamond Light Source Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
.. module:: standard_loaders
   :platform: Unix
   :synopsis: Classes for different experimental setups containing standard
   data loaders.

.. moduleauthor:: <NAME> <<EMAIL>>

"""
import os.path as os
import h5py
import logging
from savu.test import test_utils as tu
import savu.data.data_structures as ds
#from savu.data.transport_data.hdf5_transport_data import SliceAlwaysAvailableWrapper

class _NXAppFinder(object):
    '''
    Returns a given application entry
    '''
    def __init__(self,application="NXfluo"):
        self.hits = []
        self.appl=application

    def _visit_NXapp(self, name, obj):
        if "NX_class" in obj.attrs.keys():
            if obj.attrs["NX_class"] in ["NXentry", "NXsubentry"]:
                if "definition" in obj.keys():
                    if obj["definition"].value == self.appl:
                        self.hits.append(obj)
    
    def get_NXapp(self, nx_file, entry):
        self.hits = []
        nx_file[entry].visititems(self._visit_NXapp)
        return self.hits

class TomographyLoaders(object):
    """
    This class is called from a tomography loader to use a standard loader. It 
    deals with loading of the data for different formats (e.g. hdf5, tiff,...)
    """
    
    def __init__(self, exp):
        self.loader_setup(exp)


    def loader_setup(self, exp):
        
        base_classes = [ds.TomoRaw]
        data_obj = exp.create_data_object("in_data", "tomo", base_classes)
        data_obj.meta_data.set_meta_data("base_classes", base_classes)
                
        data_obj.add_pattern("PROJECTION", core_dir = (1, 2), slice_dir = (0,))
        data_obj.add_pattern("SINOGRAM", core_dir = (0, -1), slice_dir = (1,))
        
    
    def load_from_nx_tomo(self, exp):
        """
         Define the input nexus file
        
        :param path: The full path of the NeXus file to load.
        :type path: str
        """
                
        data_obj = exp.index["in_data"]["tomo"]
        objInfo = data_obj.meta_data
        expInfo = exp.meta_data

        data_obj.backing_file = h5py.File(expInfo.get_meta_data("data_file"), 'r')
        #objInfo.set_meta_data("backing_file", data_obj.backing_file)
        logging.debug("Creating file '%s' '%s'", 'tomo_entry', data_obj.backing_file.filename)
        
        data_obj.data = data_obj.backing_file['entry1/tomo_entry/instrument/detector/data']

        data_obj.set_image_key(data_obj.backing_file\
                        ['entry1/tomo_entry/instrument/detector/image_key'])
                        
        objInfo.set_meta_data("image_key", data_obj.get_image_key())
        
        rotation_angle = data_obj.backing_file['entry1/tomo_entry/sample/rotation_angle']
        objInfo.set_meta_data("rotation_angle", rotation_angle[(objInfo.get_meta_data("image_key"))==0,...])

        control = data_obj.backing_file['entry1/tomo_entry/control/data']
        objInfo.set_meta_data("control", control[...])

        data_obj.set_shape(data_obj.data.shape)
            

#AARON DIT: I will refactor the following code in the future. At the moment it is massively redundant - This is unacceptable!

class FluorescenceLoaders(object):
    """
    This class is called from a fluorescence loader to use a standard loader. It 
    deals with loading of the data for different formats (e.g. hdf5, tiff,...)
    <NAME> 13th August 2015
    """
    
    def __init__(self, exp):
        self.loader_setup(exp)


    def loader_setup(self, exp):
        exp.create_data_object("in_data", "fluo")


    def load_from_nx_fluo(self, exp):
        """
         Define the input nexus file
        
        :param path: The full path of the NeXus file to load.
        :type path: str<commit_msg>Small update to the tomo loaders to work with i12 data<commit_after># Copyright 2014 Diamond Light Source Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
.. module:: standard_loaders
   :platform: Unix
   :synopsis: Classes for different experimental setups containing standard
   data loaders.

.. moduleauthor:: <NAME> <<EMAIL>>

"""
import os.path as os
import h5py
import logging
from savu.test import test_utils as tu
import savu.data.data_structures as ds
#from savu.data.transport_data.hdf5_transport_data import SliceAlwaysAvailableWrapper

class _NXAppFinder(object):
    '''
    Returns a given application entry
    '''
    def __init__(self,application="NXfluo"):
        self.hits = []
        self.appl=application

    def _visit_NXapp(self, name, obj):
        if "NX_class" in obj.attrs.keys():
            if obj.attrs["NX_class"] in ["NXentry", "NXsubentry"]:
                if "definition" in obj.keys():
                    if obj["definition"].value == self.appl:
                        self.hits.append(obj)
    
    def get_NXapp(self, nx_file, entry):
        self.hits = []
        nx_file[entry].visititems(self._visit_NXapp)
        return self.hits

class TomographyLoaders(object):
    """
    This class is called from a tomography loader to use a standard loader. It 
    deals with loading of the data for different formats (e.g. hdf5, tiff,...)
    """
    
    def __init__(self, exp):
        self.loader_setup(exp)


    def loader_setup(self, exp):
        
        base_classes = [ds.TomoRaw]
        data_obj = exp.create_data_object("in_data", "tomo", base_classes)
        data_obj.meta_data.set_meta_data("base_classes", base_classes)
                
        data_obj.add_pattern("PROJECTION", core_dir = (1, 2), slice_dir = (0,))
        data_obj.add_pattern("SINOGRAM", core_dir = (0, -1), slice_dir = (1,))


    def load_from_nx_tomo(self, exp):
        """
         Define the input nexus file

        :param exp: The Experiment object
        """

        data_obj = exp.index["in_data"]["tomo"]
        objInfo = data_obj.meta_data
        expInfo = exp.meta_data

        data_obj.backing_file = h5py.File(expInfo.get_meta_data("data_file"),
                                          'r')
        logging.debug("Creating file '%s' '%s'", 'tomo_entry',
                      data_obj.backing_file.filename)

        data_obj.data = data_obj.backing_file['entry1/tomo_entry/data/data']

        data_obj.set_image_key(data_obj.backing_file\
                               ['entry1/tomo_entry/instrument/detector/image_key'])

        objInfo.set_meta_data("image_key", data_obj.get_image_key())

        rotation_angle = \
            data_obj.backing_file['entry1/tomo_entry/data/rotation_angle']
        objInfo.set_meta_data("rotation_angle",
                              rotation_angle[(objInfo.get_meta_data("image_key"))==0,...])

        try:
            control = data_obj.backing_file['entry1/tomo_entry/control/data']
            objInfo.set_meta_data("control", control[...])
        except:
            logging.warn("No Control information available")

        data_obj.set_shape(data_obj.data.shape)


#<NAME>: I will refactor the following code in the future. At the moment it is massively redundant - This is unacceptable!

class FluorescenceLoaders(object):
    """
    This class is called from a fluorescence loader to use a standard loader. It 
    deals with loading of the data for different formats (e.g. hdf5, tiff,...)
    <NAME> 13th August 2015
    """
    
    def __init__(self, exp):
        self.loader_setup(exp)


    def loader_setup(self, exp):
        exp.create_data_object("in_data", "fluo")


    def load_from_nx_fluo(self, exp):
        """
         Define the input nexus file
        
        :param path: The full path of the NeXus file to load.
        :type path: str
{'fraction_non_alphanumeric': 0.10302891234511244, 'fraction_numerical': 0.007113354749885268, 'mean_word_length': 2.6549266247379455, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6710163', 'n_tokens_mistral': 2723, 'n_tokens_neox': 2619, 'n_words': 717}