<commit_before>'''
Created on Jan 3, 2014

@author: noe
'''

import numpy as np
import emma2.util.pystallone as stallone

# Reader interface
class DataReader:
    """
    Class that accesses trajectory files and can read frames.
    Maps to stallone IDataReader and can currently only be initialized with java data readers.
    """
    _java_reader = None
    _selection = None
    
    def __init__(self, java_reader):
        """
        Initializes the reader
        """
        self._java_reader = java_reader
    
    def size(self):
        """
        Returns the number of data sets
        """
        return self._java_reader.size()
    
    def dimension(self):
        """
        Returns the dimension of each data set
        """
        return self._java_reader.dimension()

    def memory_size(self):
        """
        Returns the memory size needed when loading all the data
        """
        return self._java_reader.memorySize()

    def __select(self,selection = None):
        """
        Selection coordinates to be read
        
        By default (selection = None), all atoms / dimensions are read. 
        When set otherwise, a call to get() will load only a subset of rows 
        of each data set array. When the data is one-dimensional, 
        the corresponding data elements are selected. 
        For molecular data, instead of the full (N x 3) arrays, a (n x 3) subset
        will be returned.
        
        Parameters
        ----------
        select = None : list of integers
            atoms or dimension selection. 
        """
        # when a change is made:
        if (not np.array_equal(selection, self._selection)):
            self._selection = selection
            if (selection is None):
                self._java_reader.select()
            else:
                self._java_reader.select(stallone.jarray(selection))

    def get(self, index, select = None):
        """
        loads and returns a single data set as an appropriately shaped numpy array
        
        Parameters
        ----------
        index : int
            the index of the requested data set must be in [0,size()-1]
        select = None : list of integers
            atoms or dimension selection. By default, all atoms / dimensions 
            are read. When set, will load only a subset of rows of each data
            set array. When the data is one-dimensional, the corresponding
            data elements are selected. When the data is molecular data, i.e.
            (N x 3) arrays, a (n x 3) subset will be returned.
        """
        self.__select(select)
        #return stallone.stallone_array_to_ndarray(self._java_reader.get(index))
        return stallone.IDoubleArray2ndarray(self._java_reader.get(index))

    def load(self, select = None, frames = None):
        """
        loads the entire data set into a [K x shape] numpy array, where shape
        is the natural shape of a data set. 
        
        **WARNING:** This is currently **slow** due to the inefficient 
        conversion of JCC JArrays to numpy arrays via numpy.array(). This 
        bottleneck can probably be avoided by constructing the data field on 
        the python side, passing the pointer to it through the interface, and
        then filling it on the Java side. This would speed this function up by
        a factor of 20 or more. 
        
        Parameters
        ----------
        select = None : list of integers
            atoms or dimension selection. By default, all atoms / dimensions 
            are read. When set, will load only a subset of rows of each data
            set array. When the data is one-dimensional, the corresponding
            data elements are selected. When the data is molecular data, i.e.
            (N x 3) arrays, a (n x 3) subset will be returned.
        frames = None : list of integers
            frame selection. By default, all frames are read
        """<commit_msg>[coordinates/io/datareader] replaced IDoubleArray converter<commit_after>'''
Created on Jan 3, 2014

@author: noe
'''

import numpy as np
import emma2.util.pystallone as stallone

# Reader interface
class DataReader:
    """
    Class that accesses trajectory files and can read frames.
    Maps to stallone IDataReader and can currently only be initialized with java data readers.
    """
    _java_reader = None
    _selection = None
    
    def __init__(self, java_reader):
        """
        Initializes the reader
        """
        self._java_reader = java_reader
    
    def size(self):
        """
        Returns the number of data sets
        """
        return self._java_reader.size()
    
    def dimension(self):
        """
        Returns the dimension of each data set
        """
        return self._java_reader.dimension()

    def memory_size(self):
        """
        Returns the memory size needed when loading all the data
        """
        return self._java_reader.memorySize()

    def __select(self,selection = None):
        """
        Selection coordinates to be read
        
        By default (selection = None), all atoms / dimensions are read. 
        When set otherwise, a call to get() will load only a subset of rows 
        of each data set array. When the data is one-dimensional, 
        the corresponding data elements are selected. 
        For molecular data, instead of the full (N x 3) arrays, a (n x 3) subset
        will be returned.
        
        Parameters
        ----------
        select = None : list of integers
            atoms or dimension selection. 
        """
        # when a change is made:
        if (not np.array_equal(selection, self._selection)):
            self._selection = selection
            if (selection is None):
                self._java_reader.select()
            else:
                self._java_reader.select(stallone.jarray(selection))

    def get(self, index, select = None):
        """
        loads and returns a single data set as an appropriately shaped numpy array
        
        Parameters
        ----------
        index : int
            the index of the requested data set must be in [0,size()-1]
        select = None : list of integers
            atoms or dimension selection. By default, all atoms / dimensions 
            are read. When set, will load only a subset of rows of each data
            set array. When the data is one-dimensional, the corresponding
            data elements are selected. When the data is molecular data, i.e.
            (N x 3) arrays, a (n x 3) subset will be returned.
        """
        self.__select(select)
        return stallone.stallone_array_to_ndarray(self._java_reader.get(index))

    def load(self, select = None, frames = None):
        """
        loads the entire data set into a [K x shape] numpy array, where shape
        is the natural shape of a data set. 
        
        **WARNING:** This is currently **slow** due to the inefficient 
        conversion of JCC JArrays to numpy arrays via numpy.array(). This 
        bottleneck can probably be avoided by constructing the data field on 
        the python side, passing the pointer to it through the interface, and
        then filling it on the Java side. This would speed this function up by
        a factor of 20 or more. 
        
        Parameters
        ----------
        select = None : list of integers
            atoms or dimension selection. By default, all atoms / dimensions 
            are read. When set, will load only a subset of rows of each data
            set array. When the data is one-dimensional, the corresponding
            data elements are selected. When the data is molecular data, i.e.
            (N x 3) arrays, a (n x 3) subset will be returned.
        frames = None : list of integers
            frame selection. By default, all frames are read
        """
{'fraction_non_alphanumeric': 0.07124746450304259, 'fraction_numerical': 0.004183569979716024, 'mean_word_length': 1.9035701140964298, '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': '2117589', 'n_tokens_mistral': 1991, 'n_tokens_neox': 1909, 'n_words': 891}