<commit_before># -*- coding: utf-8 -*-
'''
Universe (Container)
======================
Like all secondary (meaning dependent on `exa`_) packages, atomic has a default,
data aware container: the :class:`~atomic.container.Universe`.

Conceptually, a universe is a collection of independent (or related) frames
of a given system. For example, a universe may contain only a
single frame with the geometry of the molecule (system) of interest, a set of
snapshot geometries obtained during the course of a geometry optimization (one
per frame), the same molecule's optimized geometry with each frame containing a
different level of theory, a properties calculation test set of small molecules
with one molecule per frame, a molecular dynamics simulation with each frame
corresponding to a snaphot in time, etc., without restrictions.

.. _exa: http://exa-analytics.github.io/website
'''
import pandas as pd
import numpy as np
from collections import OrderedDict
from sqlalchemy import Column, Integer, ForeignKey
from exa import Container, _conf
from exa.numerical import Field
from atomic.widget import UniverseWidget
from atomic.frame import minimal_frame, Frame
from atomic.atom import Atom, ProjectedAtom, UnitAtom
from atomic.two import Two, PeriodicTwo
from atomic.field import UField3D
from atomic.atom import compute_unit_atom as _cua
from atomic.atom import compute_projected_atom as _cpa
from atomic.two import max_frames_periodic as mfp
from atomic.two import max_atoms_per_frame_periodic as mapfp
from atomic.two import max_frames as mf
from atomic.two import max_atoms_per_frame as mapf
from atomic.two import compute_two_body as _ctb
from atomic.two import compute_bond_count as _cbc
from atomic.molecule import Molecule
from atomic.molecule import compute_molecule as _cm


class Universe(Container):
    '''
    A container for working with computational chemistry data.

    For a conceptual description of the universe, see the module's docstring.
    '''
    unid = Column(Integer, ForeignKey('container.pkid'), primary_key=True)
    frame_count = Column(Integer)
    _widget_class = UniverseWidget
    __mapper_args__ = {'polymorphic_identity': 'universe'}
    # The arguments here should match those of init (for dataframes)
    _df_types = OrderedDict([('frame', Frame), ('atom', Atom), ('two', Two),
                             ('unit_atom', UnitAtom), ('projected_atom', ProjectedAtom),
                             ('periodic_two', PeriodicTwo), ('molecule', Molecule),
                             ('field', UField3D)])

    @property
    def is_periodic(self):
        return self.frame.is_periodic

    @property
    def is_variable_cell(self):
        return self.frame.is_variable_cell

    @property
    def frame(self):
        if not self._is('_frame'):
            if self._is('_atom'):
                self._frame = minimal_frame(self.atom)
        return self._frame

    @property
    def atom(self):
        return self._atom

    @property
    def unit_atom(self):
        '''
        Updated atom table using only in-unit-cell positions.

        Note:
            This function returns a standard :class:`~pandas.DataFrame`
        '''
        if not self._is('_unit_atom'):
            self.compute_unit_atom()
        atom = self.atom.copy()
        atom.update(self._unit_atom)
        return atom

    @property
    def projected_atom(self):
        '''
        Projected (unit) atom positions into a 3x3x3 supercell.
        '''
        if self._projected_atom is None:
            self.compute_projected_atom()
        return self._projected_atom

    @property
    def two(self):
        if not self._is('_two') and not self._is('_periodic_two'):
            self.compute_two_body()
        if self._is('_periodic_two'):
            return self._periodic_two
        return self._two

    @property
    def periodic_two(self):
        if not self._is('_periodic_two'):
            self.compute_two_body()
        return self._periodic_two

    @property
    def molecule(self):
        if not self._is('_molecule'):
            self.compute_molecule()
        return self._molecule

    @property
    def field(self):
        return self._field

    @property
    def field_values(self):
        '''
        Retrieve values of a specific field.

        Args:
            field (int): Field index (corresponding to the fields dataframe)

        Returns:
            data: Series or dataframe object containing field values
        '''
        return self._field.field_values

    def compute_unit_atom(self):
        '''
        Compute the sparse unit atom dataframe.
        '''
        self._unit_atom = _cua(self)

    def compute_projected_atom(self):
        '''
        Compute the projected supercell from the unit atom coordinates.
        '''
        self._projected_atom = _cpa(self)

    def compute_bond_count(self):
        '''
        Compute the bond count and update the atom table.

        Note:
            If working with a periodic universe, the projected atom table will
            also be updated; an index of minus takes the usual convention of
            meaning not applicable or not calculated.
        '''
        bc = _cbc(self)
        return bc
        if self.is_periodic:
            self.projected_atom['bond_count'] = bc
            self.projected_atom['bond_count'].fillna(-1, inplace=True)
            bc = self.projected_atom[self.projected_atom['bond_count'] > 0].set_index('atom')['bond_count']<commit_msg>compute_molecule; Molecule; compute_bond_count implemented<commit_after># -*- coding: utf-8 -*-
'''
Universe (Container)
======================
Like all secondary (meaning dependent on `exa`_) packages, atomic has a default,
data aware container: the :class:`~atomic.container.Universe`.

Conceptually, a universe is a collection of independent (or related) frames
of a given system. For example, a universe may contain only a
single frame with the geometry of the molecule (system) of interest, a set of
snapshot geometries obtained during the course of a geometry optimization (one
per frame), the same molecule's optimized geometry with each frame containing a
different level of theory, a properties calculation test set of small molecules
with one molecule per frame, a molecular dynamics simulation with each frame
corresponding to a snaphot in time, etc., without restrictions.

.. _exa: http://exa-analytics.github.io/website
'''
import pandas as pd
import numpy as np
from collections import OrderedDict
from sqlalchemy import Column, Integer, ForeignKey
from exa import Container, _conf
from exa.numerical import Field
from atomic.widget import UniverseWidget
from atomic.frame import minimal_frame, Frame
from atomic.atom import Atom, ProjectedAtom, UnitAtom
from atomic.two import Two, PeriodicTwo
from atomic.field import UField3D
from atomic.atom import compute_unit_atom as _cua
from atomic.atom import compute_projected_atom as _cpa
from atomic.two import max_frames_periodic as mfp
from atomic.two import max_atoms_per_frame_periodic as mapfp
from atomic.two import max_frames as mf
from atomic.two import max_atoms_per_frame as mapf
from atomic.two import compute_two_body as _ctb
from atomic.two import compute_bond_count as _cbc
from atomic.molecule import Molecule
from atomic.molecule import compute_molecule as _cm


class Universe(Container):
    '''
    A container for working with computational chemistry data.

    For a conceptual description of the universe, see the module's docstring.
    '''
    unid = Column(Integer, ForeignKey('container.pkid'), primary_key=True)
    frame_count = Column(Integer)
    _widget_class = UniverseWidget
    __mapper_args__ = {'polymorphic_identity': 'universe'}
    # The arguments here should match those of init (for dataframes)
    _df_types = OrderedDict([('frame', Frame), ('atom', Atom), ('two', Two),
                             ('unit_atom', UnitAtom), ('projected_atom', ProjectedAtom),
                             ('periodic_two', PeriodicTwo), ('molecule', Molecule),
                             ('field', UField3D)])

    @property
    def is_periodic(self):
        return self.frame.is_periodic

    @property
    def is_variable_cell(self):
        return self.frame.is_variable_cell

    @property
    def frame(self):
        if not self._is('_frame'):
            if self._is('_atom'):
                self._frame = minimal_frame(self.atom)
        return self._frame

    @property
    def atom(self):
        return self._atom

    @property
    def unit_atom(self):
        '''
        Updated atom table using only in-unit-cell positions.

        Note:
            This function returns a standard :class:`~pandas.DataFrame`
        '''
        if not self._is('_unit_atom'):
            self.compute_unit_atom()
        atom = self.atom.copy()
        atom.update(self._unit_atom)
        return atom

    @property
    def projected_atom(self):
        '''
        Projected (unit) atom positions into a 3x3x3 supercell.
        '''
        if self._projected_atom is None:
            self.compute_projected_atom()
        return self._projected_atom

    @property
    def two(self):
        if not self._is('_two') and not self._is('_periodic_two'):
            self.compute_two_body()
        if self._is('_periodic_two'):
            return self._periodic_two
        return self._two

    @property
    def periodic_two(self):
        if not self._is('_periodic_two'):
            self.compute_two_body()
        return self._periodic_two

    @property
    def molecule(self):
        if not self._is('_molecule'):
            self.compute_molecule()
        return self._molecule

    @property
    def field(self):
        return self._field

    @property
    def field_values(self):
        '''
        Retrieve values of a specific field.

        Args:
            field (int): Field index (corresponding to the fields dataframe)

        Returns:
            data: Series or dataframe object containing field values
        '''
        return self._field.field_values

    def compute_unit_atom(self):
        '''
        Compute the sparse unit atom dataframe.
        '''
        self._unit_atom = _cua(self)

    def compute_projected_atom(self):
        '''
        Compute the projected supercell from the unit atom coordinates.
        '''
        self._projected_atom = _cpa(self)

    def compute_bond_count(self):
        '''
        Compute the bond count and update the atom table.

        Note:
            If working with a periodic universe, the projected atom table will
            also be updated; an index of minus takes the usual convention of
            meaning not applicable or not calculated.
        '''
        bc = _cbc(self)
        if self.is_periodic:
            self.projected_atom['bond_count'] = bc
            self.projected_atom['bond_count'].fillna(-1, inplace=True)
            self.projected_atom['bond_count'] = self.projected_atom['bond_count'].astype(np.int64)
            bc = bc.to_frame().reset_index()
            bc['atom'] = bc['prjd_atom'].map(self.projected_atom['atom'])
            bc.sort_values('bond_count', ascending=False, inplace=True)
            bc = bc.drop_duplicates('atom').set_index('atom')['bond_count']
{'fraction_non_alphanumeric': 0.0853517877739331, 'fraction_numerical': 0.0015082956259426848, 'mean_word_length': 2.780013413816231, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '2311605', 'n_tokens_mistral': 3152, 'n_tokens_neox': 2997, 'n_words': 1012}