<commit_before># -*- coding: utf-8 -*-
"""
@author: Sebi

File: czitools.py
Date: 18.05.2015
Version. 0.7
"""

import misctools as misc
from czifile import *
import numpy as np


def get_metainfo_channel_description(filename):

    try:
        czi = CziFile(filename)
        namelst = []
        valuelst = []
        # get root tree of CZI metadata (uses ElementTree)

        for elem in czi.metadata.getiterator():

            namelst.append(elem.tag)
            valuelst.append(elem.text)

        # get the channel descriptions
        ids = misc.find_index_byname(namelst, 'Description')
        chdescript = misc.get_entries(valuelst, ids)

        czi.close()

    except:
        chdescript = 'n.a.'

    return chdescript


def writexml_czi(filename):

    # write xml file to disk
    czi = CziFile(filename)

    # Change File name and write XML file to same folder
    xmlfile = filename.replace('.bfimage', '_MetaData.xml')
    tree = czi.metadata.getroottree()
    tree.write(xmlfile, encoding='utf-8', method='xml')
    print 'XML metadata : ', xmlfile

    czi.close()


def get_objective_name_cziread(filename):

    objname = 'n.a.'
    czi = CziFile(filename)
    # get root tree of CZI metadata (uses ElementTree)
    tree = czi.metadata.getroottree()
    # Iterate over the metadata
    for elem in czi.metadata.getiterator():

        if elem.tag == 'ObjectiveName':
            objname = elem.text

    czi.close()

    return objname



def read_dimensions_czi(filename):

    # Read the dimensions of the image stack and their order
    czi = CziFile(filename)
    czishape = czi.shape
    cziorder = czi.axes
    czi.close()

    return czishape, cziorder


def get_metainfo_cziread(filename):

    # define default values in case something is missing inside the metadata
    objNA = np.NaN
    objMag = np.NaN
    objName = 'n.a.'
    objImm = 'n.a.'
    CamName = 'n.a.'
    totalMag = np.NaN

    try:
        czi = CziFile(filename)
        czishape = czi.shape
        cziorder = czi.axes

        # Iterate over the metadata
        for elem in czi.metadata.getiterator():

            if elem.tag == 'LensNA':
                objNA = np.float(elem.text)

            if elem.tag == 'NominalMagnification':
                objMag = elem.text

            if elem.tag == 'ObjectiveName':
                objName = elem.text

            if elem.tag == 'Immersion':
                objImm = elem.text

            if elem.tag == 'CameraName':
                CamName = elem.text

            if elem.tag == 'TotalMagnification':
                totalMag = np.float(elem.text)

        czi.close()

    except:

        print 'czifile.py did not detect an CZI file.'
        czishape = 'unknown'
        cziorder = 'unknown'

    return objNA, objMag, objName, objImm, CamName, totalMag, czishape, cziorder


def get_metainfo_cziread_camera(filename):

    czi = CziFile(filename)

    # Iterate over the metadata
    for elem in czi.metadata.getiterator():

        if elem.tag == 'CameraName':
            CamName = elem.text

    czi.close()

    return CamName


def convert_dimension_string(cziorder):
    """

    EXPERIMENTAL !!!<commit_msg>Updates.<commit_after># -*- coding: utf-8 -*-
"""
@author: Sebi

File: czitools.py
Date: 09.06.2015
Version. 0.8
"""

import misctools as misc
from czifile import *
import numpy as np


def get_metainfo_channel_description(filename):

    try:
        czi = CziFile(filename)
        namelst = []
        valuelst = []
        # get root tree of CZI metadata (uses ElementTree)

        for elem in czi.metadata.getiterator():

            namelst.append(elem.tag)
            valuelst.append(elem.text)

        # get the channel descriptions
        ids = misc.find_index_byname(namelst, 'Description')
        chdescript = misc.get_entries(valuelst, ids)

        czi.close()

    except:
        chdescript = 'n.a.'

    return chdescript

def writexml_czi(filename):

    # write xml file to disk
    czi = CziFile(filename)

    # Change File name and write XML file to same folder
    xmlfile = filename.replace('.bfimage', '_MetaData.xml')
    tree = czi.metadata.getroottree()
    tree.write(xmlfile, encoding='utf-8', method='xml')
    print 'XML metadata : ', xmlfile

    czi.close()

def get_objective_name_cziread(filename):

    objname = 'n.a.'
    czi = CziFile(filename)
    # get root tree of CZI metadata (uses ElementTree)
    tree = czi.metadata.getroottree()
    # Iterate over the metadata
    for elem in czi.metadata.getiterator():

        if elem.tag == 'ObjectiveName':
            objname = elem.text

    czi.close()

    return objname

def read_dimensions_czi(filename):

    # Read the dimensions of the image stack and their order
    czi = CziFile(filename)
    czishape = czi.shape
    cziorder = czi.axes
    czi.close()

    return czishape, cziorder

def get_shapeinfo_cziread(filename):
    # get CZI shape and dimension order using czifile.py

    try:
        czi = CziFile(filename)
        czishape = czi.shape
        cziorder = czi.axes

        czi.close()

    except:

        print 'czifile.py did not detect an CZI file.'
        czishape = 'unknown'
        cziorder = 'unknown'

    return czishape, cziorder

def get_metainfo_cziread(filename):

    # define default values in case something is missing inside the metadata
    objNA = np.NaN
    objMag = np.NaN
    objName = 'n.a.'
    objImm = 'n.a.'
    CamName = 'n.a.'
    totalMag = np.NaN

    try:
        czi = CziFile(filename)

        # Iterate over the metadata
        for elem in czi.metadata.getiterator():

            if elem.tag == 'LensNA':
                objNA = np.float(elem.text)

            if elem.tag == 'NominalMagnification':
                objMag = elem.text

            if elem.tag == 'ObjectiveName':
                objName = elem.text

            if elem.tag == 'Immersion':
                objImm = elem.text

            if elem.tag == 'CameraName':
                CamName = elem.text

            if elem.tag == 'TotalMagnification':
                totalMag = np.float(elem.text)
                if totalMag == 0:
                    totalMag == 'n.a.'

        czi.close()

    except:

        print 'czifile.py did not detect an CZI file.'
        czishape = 'unknown'
        cziorder = 'unknown'

    return objNA, objMag, objName, objImm, CamName, totalMag

def get_metainfo_cziread_camera(filename):

    czi = CziFile(filename)

    # Iterate over the metadata
    for elem in czi.metadata.getiterator():

        if elem.tag == 'CameraName':
            CamName = elem.text

    czi.close()

    return CamName

def convert_dimension_string(cziorder):
    """

    EXPERIMENTAL !!!
{'fraction_non_alphanumeric': 0.09968754649605713, 'fraction_numerical': 0.0037196845707484004, 'mean_word_length': 2.1323392357875117, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '1026194', 'n_tokens_mistral': 2236, 'n_tokens_neox': 2100, 'n_words': 555}