<commit_before>#!/usr/bin/python

'''This module provides functions for outputting results of macroeco 
analyses'''


from __future__ import division
import matplotlib.pyplot as plt
import numpy as np
import logging
from macroeco.utils.form_func import output_form, add_field

__author__ = "<NAME>"
__copyright__ = "Copyright 2012, Regents of the University of California"
__license__ = None
__version__ = "0.5"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Development"

class DistOutput(object):
    '''
    This formats and outputs analyses on distributions

    '''

    def __init__(self, out_dir, name):
        '''
        Parameters
        ----------
        out_dir : string
            Output directory of object
        name : string
            Either 'sad' or 'ssad'. This argument dicatates how plots and files
            are named in the output. 
        '''

        self.out_dir = out_dir
        if name == 'sad':
            self.urns = 'S'
            self.balls = 'N'
        if name == 'ssad':
            self.urns = 'Cells'
            self.balls = 'Individuals'

    def write_summary_table(self, smry, criteria=None):
        '''
        Parameters
        ---------
        smry : tuple
            A tuple of length two in which the first object is a dictionary as
            returned by the fucntion compare_summary within the 
            CompareDistribution class.  The second object is  dictionary with
            the keyword 'mins' that refers to the abundance that the 'tot_min'
            keyword is less than OR equal to. If mins = 1, tot_min desribes the
            number of items with counts <= 1. 

        criteria : array-like object
            An array-like object in which contains either string or dicts that
            tell how each dataset was generated.  Describes the subsetting of
            an sad and the species ID of an ssad.

        Notes
        -----
        Writes out a formatted txt file to self.out_dir 

        '''
        logging.info('Writing summary table')

        tot_sad = len(smry['observed']['balls'])
        if criteria != None:
            assert len(criteria) == tot_sad, "len(criteria) must  equal" + \
                                   " number of data arrays under consideration"
        ob = smry['observed']
        for i in xrange(tot_sad):
            if criteria != None and np.all([type(crt) != dict for crt in
                                                                  criteria]):
                fout = open(self.out_dir + '_summary_table_' +
                                                str(criteria[i]) + '.txt', 'w')
            else:
                fout = open(self.out_dir + '_summary_table_' + str(i) + '.txt',
                                                                           'w')
            if criteria != None:
                fout.write('CRITERIA: ' + str(criteria[i]) + '\n\n')
            else:
                fout.write('CRITERIA: NONE ' + str(i) + '\n\n')

            # Getting rarity 
            ob_rare = {} 
            for mins in ob['tot_min'].iterkeys():
                ob_rare['<=' + str(mins)] = ob['tot_min'][mins][i]

            fout.write('EMPIRICAL VALUES:\n' + self.urns + ' = ' + 
                    str(ob['urns'][i]) + '\n' + self.balls + ' = ' + 
                    str(ob['balls'][i]) + '\nObserved Nmax= ' + 
                    str(ob['max'][i]) + '\nObserved Rarity = ' +
                    str(ob_rare) + '\n\n')
            for kw in smry.iterkeys():
                if kw != 'observed':
                    dt= smry[kw]
                    <commit_msg>Made output more explicit in output.py. Issue #280<commit_after>#!/usr/bin/python

'''This module provides functions for outputting results of macroeco 
analyses'''


from __future__ import division
import matplotlib.pyplot as plt
import numpy as np
import logging
from macroeco.utils.form_func import output_form, add_field

__author__ = "<NAME>"
__copyright__ = "Copyright 2012, Regents of the University of California"
__license__ = None
__version__ = "0.5"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Development"

class DistOutput(object):
    '''
    This formats and outputs analyses on distributions

    '''

    def __init__(self, out_dir, name):
        '''
        Parameters
        ----------
        out_dir : string
            Output directory of object
        name : string
            Either 'sad' or 'ssad'. This argument dicatates how plots and files
            are named in the output. 
        '''

        self.out_dir = out_dir
        if name == 'sad':
            self.urns = 'S'
            self.balls = 'N'
        if name == 'ssad':
            self.urns = 'Cells'
            self.balls = 'Individuals'

    def write_summary_table(self, smry, criteria=None):
        '''
        Parameters
        ---------
        smry : tuple
            A tuple of length two in which the first object is a dictionary as
            returned by the fucntion compare_summary within the 
            CompareDistribution class.  The second object is  dictionary with
            the keyword 'mins' that refers to the abundance that the 'tot_min'
            keyword is less than OR equal to. If mins = 1, tot_min desribes the
            number of items with counts <= 1. 

        criteria : array-like object
            An array-like object in which contains either string or dicts that
            tell how each dataset was generated.  Describes the subsetting of
            an sad and the species ID of an ssad.

        Notes
        -----
        Writes out a formatted txt file to self.out_dir 

        '''

        tot_sad = len(smry['observed']['balls'])
        if criteria != None:
            assert len(criteria) == tot_sad, "len(criteria) must  equal" + \
                                   " number of data arrays under consideration"
        ob = smry['observed']
        for i in xrange(tot_sad):
            if criteria != None and np.all([type(crt) != dict for crt in
                                                                  criteria]):
                filename = self.out_dir + '_summary_table_' + str(criteria[i])\
                            + '.txt.'
                
            else:
                filename = self.out_dir + '_summary_table_' + str(i) + '.txt'

            fout = open(filename, 'w')
            logging.info('Writing summary table %s' % filename)


            if criteria != None:
                fout.write('CRITERIA: ' + str(criteria[i]) + '\n\n')
            else:
                fout.write('CRITERIA: NONE ' + str(i) + '\n\n')

            # Getting rarity 
            ob_rare = {} 
            for mins in ob['tot_min'].iterkeys():
                ob_rare['<=' + str(mins)] = ob['tot_min'][mins][i]

            fout.write('EMPIRICAL VALUES:\n' + self.urns + ' = ' + 
                    str(ob['urns'][i]) + '\n' + self.balls + ' = ' + 
                    str(ob['balls'][i]) + '\nObserved Nmax= ' + 
                    str(ob['max'][i]) + '\nObserved Rarity = ' +
                    str(ob_rare) + '\n\n')
            for kw in smry.iterkeys():
                if kw != 'observed':
                    dt= smry[kw]
                    
{'fraction_non_alphanumeric': 0.1071084834318713, 'fraction_numerical': 0.0026124020349236905, 'mean_word_length': 1.5612676056338028, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6784597', 'n_tokens_mistral': 2071, 'n_tokens_neox': 1940, 'n_words': 622}