<commit_before>#! /usr/bin/env python

# $Id: utils.py 297 2007-03-30 11:25:28Z mhagger $

# Copyright (C) 1998-2003 <NAME> <<EMAIL>>
#
# This file is licensed under the GNU Lesser General Public License
# (LGPL).  See LICENSE.txt for details.

"""utils.py -- Utility functions used by Gnuplot.

This module contains utility functions used by Gnuplot.py which aren't
particularly gnuplot-related.

"""

import string
import numpy

def float_array(m):
    """Return the argument as a numpy array of type at least 'Float32'.

    Leave 'Float64' unchanged, but upcast all other types to
    'Float32'.  Allow also for the possibility that the argument is a
    python native type that can be converted to a numpy array using
    'numpy.asarray()', but in that case don't worry about
    downcasting to single-precision float.

    """

    try:
        # Try Float32 (this will refuse to downcast)
        return numpy.asarray(m, numpy.float32)
    except TypeError:
        # That failure might have been because the input array was
        # of a wider data type than float32; try to convert to the
        # largest floating-point type available:
        # NOTE TBD: I'm not sure float_ is the best data-type for this...
        try:
            return numpy.asarray(m, numpy.float_)
        except TypeError:
            # TBD: Need better handling of this error!
            print "Fatal: array dimensions not equal!"
            return None

def write_array(f, set,
                item_sep=' ',
                nest_prefix='', nest_suffix='\n', nest_sep=''):
    """Write an array of arbitrary dimension to a file.

    A general recursive array writer.  The last four parameters allow
    a great deal of freedom in choosing the output format of the
    array.  The defaults for those parameters give output that is
    gnuplot-readable.  But using '(",", "{", "}", ",\n")' would output
    an array in a format that Mathematica could read.  'item_sep'
    should not contain '%' (or if it does, it should be escaped to
    '%%') since it is put into a format string.

    The default 2-d file organization::

        set[0,0] set[0,1] ...
        set[1,0] set[1,1] ...

    The 3-d format::

        set[0,0,0] set[0,0,1] ...
        set[0,1,0] set[0,1,1] ...<commit_msg>utils.py: print call was fixed.<commit_after>#! /usr/bin/env python

# $Id: utils.py 297 2007-03-30 11:25:28Z mhagger $

# Copyright (C) 1998-2003 <NAME> <<EMAIL>>
#
# This file is licensed under the GNU Lesser General Public License
# (LGPL).  See LICENSE.txt for details.

"""utils.py -- Utility functions used by Gnuplot.

This module contains utility functions used by Gnuplot.py which aren't
particularly gnuplot-related.

"""

import string
import numpy

def float_array(m):
    """Return the argument as a numpy array of type at least 'Float32'.

    Leave 'Float64' unchanged, but upcast all other types to
    'Float32'.  Allow also for the possibility that the argument is a
    python native type that can be converted to a numpy array using
    'numpy.asarray()', but in that case don't worry about
    downcasting to single-precision float.

    """

    try:
        # Try Float32 (this will refuse to downcast)
        return numpy.asarray(m, numpy.float32)
    except TypeError:
        # That failure might have been because the input array was
        # of a wider data type than float32; try to convert to the
        # largest floating-point type available:
        # NOTE TBD: I'm not sure float_ is the best data-type for this...
        try:
            return numpy.asarray(m, numpy.float_)
        except TypeError:
            # TBD: Need better handling of this error!
            print ("Fatal: array dimensions not equal!")
            return None

def write_array(f, set,
                item_sep=' ',
                nest_prefix='', nest_suffix='\n', nest_sep=''):
    """Write an array of arbitrary dimension to a file.

    A general recursive array writer.  The last four parameters allow
    a great deal of freedom in choosing the output format of the
    array.  The defaults for those parameters give output that is
    gnuplot-readable.  But using '(",", "{", "}", ",\n")' would output
    an array in a format that Mathematica could read.  'item_sep'
    should not contain '%' (or if it does, it should be escaped to
    '%%') since it is put into a format string.

    The default 2-d file organization::

        set[0,0] set[0,1] ...
        set[1,0] set[1,1] ...

    The 3-d format::

        set[0,0,0] set[0,0,1] ...
        set[0,1,0] set[0,1,1] ...
{'fraction_non_alphanumeric': 0.09812062937062938, 'fraction_numerical': 0.025786713286713288, 'mean_word_length': 2.5898039215686275, 'pattern_counts': {'":': 0, '<': 9, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3760317', 'n_tokens_mistral': 1411, 'n_tokens_neox': 1312, 'n_words': 568}