<commit_before>"""Holds all of the data for a single Exp
"""

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

from os import mkdir, path, link

from parse_int import index_tuple_map
from parse_int import mass_index_energy_map_map
from parse_int import mass_interaction_tuple_energy_map_map
from parse_int import mass_zero_body_term_map
from parse_int import name_from_filename
from parse_int import mass_number_from_filename
from parse_int import exp_from_filename
from parse_int import other_constants_from_filename
from parse import has_extension, get_files_r
from TwoBodyInteraction import TwoBodyInteraction
from QuantumNumbers import QuantumNumbers
from constants import DIR_FILES_ORG, ORG_FMT_DIR, ORG_FMT_FILE, FN_PARSE_INT_EXT


class ImsrgDatumInt:
    def __init__(self, directory, exp, std_io_map=None,
                 standardize_io_map=True, organize_files=True,
                 org_file_dir=DIR_FILES_ORG,
                 directory_format=ORG_FMT_DIR,
                 file_format=ORG_FMT_FILE,
                 file_extension=FN_PARSE_INT_EXT):
        self.exp = exp
        self.e, self.hw, self.base, self.rp = self.exp

        self.name = None
        self.dir = directory
        self._fname_filter = None
        self.standardized_indexing = False
        self.files_organized = False

        # Create maps initially empty
        self.standard_index_orbital_map = std_io_map
        self._particular_index_orbital_map = None
        self.index_orbital_map = dict()
        self.mass_index_energy_map = dict()
        self.mass_interaction_index_energy_map = dict()
        self.mass_zero_body_term_map = dict()
        self.other_constants = None
        self.files = None
        self._unorg_files = None

        # Perform setup methods
        self._set_fname_filter(file_extension)
        self._set_files()
        self._set_maps()
        self._set_name()
        self._set_other_constants()
        if self.standard_index_orbital_map is not None and standardize_io_map:
            self._standardize_indexing()
            self.standardized_indexing = True
        if organize_files:
            self._organize_files(org_file_dir, directory_format, file_format)

    def _set_maps(self):
        self._set_index_orbital_map()
        self._set_mass_index_energy_map()
        self._set_mass_interaction_index_energy_map()
        self._set_zero_body_term_map()

    def _set_index_orbital_map(self):
        """Retrieves the index -> orbital map from a file in the directory
        and stores it in an instance variable
        """
        # Assuming all files characteristic have the same indexing...
        f0 = next(iter(filter(self._fname_filter, self.files)))
        index_orbital_map = index_tuple_map(f0)

        # Turn each tuple in the map into a named tuple
        for k in index_orbital_map.keys():
            v = index_orbital_map[k]
            nextv = QuantumNumbers(*qnums_to_list(v))
            index_orbital_map[k] = nextv

        self.index_orbital_map = index_orbital_map

    def _set_mass_index_energy_map(self):
        """Retrieves the
            mass number -> orbital index -> energy
        mapping for the directory
        """
        self.mass_index_energy_map = (
            mass_index_energy_map_map(self.dir, self._fname_filter,
                                      filtered_files=self.files))

    def _set_mass_interaction_index_energy_map(self):
        """Retrieves the
            mass number -> (a, b, c, d, j) -> energy
        mapping for the directory
        """
        miiem = (
            mass_interaction_tuple_energy_map_map(
                self.dir, self._fname_filter, filtered_files=self.files))

        # Turn each tuple into a named tuple
        for A in miiem.keys():
            tuple_energy_map = miiem[A]
            next_tuple_energy_map = dict()
            for k in tuple_energy_map.keys():
                v = tuple_energy_map[k]
                nextk = list()
                for stritem in k:
                    nextk.append(int(stritem))
                nextk = TwoBodyInteraction(*nextk)
                next_tuple_energy_map[nextk] = v
            miiem[A] = next_tuple_energy_map

        self.mass_interaction_index_energy_map = miiem

    def _set_zero_body_term_map(self):
        self.mass_zero_body_term_map = (
            mass_zero_body_term_map(self.dir, self._fname_filter,
                                    filtered_files=self.files))

    def _set_name(self):
        """Sets the incidence name variable
        """
        f0 = next(iter(filter(self._fname_filter, self.files)))
        self.name = name_from_filename(f0)

    def _set_fname_filter(self, extension):
        """Returns a filter function that filters a set of filenames such that
        only those that have the same signature (e, hw, rp) values as self
        """

        def f(fname):
            return (exp_from_filename(fname) == self.exp and
                    has_extension(fname, ext=extension))

        self._fname_filter = f

    def _set_other_constants(self):
        """Sets other heading constants. Assumes all files in a given directory
        have the same constants
        """
        f0 = next(iter(filter(self._fname_filter, self.files)))
        self.other_constants = other_constants_from_filename(f0)

    def _set_files(self):
        self.files = get_files_r(self.dir, filterfn=self._fname_filter)

    def _organize_files(self, directory, dir_fmt, file_fmt):
        """Give the files standardized names and put them in a similarly-named
        directory
        :param dir_fmt: the string template for the directory name, should
        allow for the same number of arguments as the length of self.exp<commit_msg>Super is a super duper party pooper. Just kidding, it is actually quite useful here.<commit_after>"""Holds all of the data for a single Exp
"""

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

from os import mkdir, path, link

from ImsrgDatum import ImsrgDatum
from parse_int import index_tuple_map
from parse_int import mass_index_energy_map_map
from parse_int import mass_interaction_tuple_energy_map_map
from parse_int import mass_zero_body_term_map
from parse_int import name_from_filename
from parse_int import mass_number_from_filename
from parse_int import other_constants_from_filename
from TwoBodyInteraction import TwoBodyInteraction
from QuantumNumbers import QuantumNumbers
from constants import DIR_FILES_ORG, ORG_FMT_DIR, ORG_FMT_FILE


class ImsrgDatumInt(ImsrgDatum):
    def __init__(self, directory, exp, files, std_io_map=None,
                 standardize_io_map=True, organize_files=True,
                 org_file_dir=DIR_FILES_ORG,
                 directory_format=ORG_FMT_DIR,
                 file_format=ORG_FMT_FILE):
        super(ImsrgDatumInt, self).__init__(directory=directory, exp=exp,
                                            files=files)
        self.name = None
        self.standardized_indexing = False
        self.files_organized = False

        # Create maps initially empty
        self.standard_index_orbital_map = std_io_map
        self._particular_index_orbital_map = None
        self.index_orbital_map = dict()
        self.mass_index_energy_map = dict()
        self.mass_interaction_index_energy_map = dict()
        self.mass_zero_body_term_map = dict()
        self.other_constants = None
        self._unorg_files = None

        # Perform setup methods
        self._set_maps()
        self._set_name()
        self._set_other_constants()
        if self.standard_index_orbital_map is not None and standardize_io_map:
            self._standardize_indexing()
            self.standardized_indexing = True
        if organize_files:
            self._organize_files(org_file_dir, directory_format, file_format)

    def _set_maps(self):
        self._set_index_orbital_map()
        self._set_mass_index_energy_map()
        self._set_mass_interaction_index_energy_map()
        self._set_zero_body_term_map()

    def _set_index_orbital_map(self):
        """Retrieves the index -> orbital map from a file in the directory
        and stores it in an instance variable
        """
        # Assuming all files characteristic have the same indexing...
        index_orbital_map = index_tuple_map(self.files[0])

        # Turn each tuple in the map into a named tuple
        for k in index_orbital_map.keys():
            v = index_orbital_map[k]
            nextv = QuantumNumbers(*qnums_to_list(v))
            index_orbital_map[k] = nextv

        self.index_orbital_map = index_orbital_map

    def _set_mass_index_energy_map(self):
        """Retrieves the
            mass number -> orbital index -> energy
        mapping for the directory
        """
        self.mass_index_energy_map = (
            mass_index_energy_map_map(self.dir, filtered_files=self.files))

    def _set_mass_interaction_index_energy_map(self):
        """Retrieves the
            mass number -> (a, b, c, d, j) -> energy
        mapping for the directory
        """
        miiem = (
            mass_interaction_tuple_energy_map_map(self.dir,
                                                  filtered_files=self.files))

        # Turn each tuple into a named tuple
        for A in miiem.keys():
            tuple_energy_map = miiem[A]
            next_tuple_energy_map = dict()
            for k in tuple_energy_map.keys():
                v = tuple_energy_map[k]
                nextk = list()
                for stritem in k:
                    nextk.append(int(stritem))
                nextk = TwoBodyInteraction(*nextk)
                next_tuple_energy_map[nextk] = v
            miiem[A] = next_tuple_energy_map

        self.mass_interaction_index_energy_map = miiem

    def _set_zero_body_term_map(self):
        self.mass_zero_body_term_map = (
            mass_zero_body_term_map(self.dir, filtered_files=self.files))

    def _set_name(self):
        """Sets the incidence name variable
        """
        self.name = name_from_filename(self.files[0])

    def _set_other_constants(self):
        """Sets other heading constants. Assumes all files in a given directory
        have the same constants
        """
        self.other_constants = other_constants_from_filename(self.files[0])

    def _organize_files(self, directory, dir_fmt, file_fmt):
        """Give the files standardized names and put them in a similarly-named
        directory
        :param dir_fmt: the string template for the directory name, should
        allow for the same number of arguments as the length of self.exp
{'fraction_non_alphanumeric': 0.06449207828518173, 'fraction_numerical': 0.0008387698042870457, 'mean_word_length': 2.5334211392821864, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7446520', 'n_tokens_mistral': 3164, 'n_tokens_neox': 3094, 'n_words': 762}