<commit_before>"""This manages the lookup table used to map values to colors.

"""
# Author: <NAME> <<EMAIL>>
# Copyright (c) 2005-2008, Enthought, Inc.
# License: BSD Style.

import os.path
import sys
import subprocess

# Enthought library imports.
from enthought.traits.api import Instance, Range, Bool, Array, \
     Str, Property, Enum, Button
from enthought.traits.ui.api import FileEditor, auto_close_message
from enthought.tvtk.api import tvtk

# Local imports.
from enthought.mayavi.core.base import Base
from enthought.mayavi.core.common import error
from enthought.mayavi.core.lut.pylab_luts import lut_dic as pylab_luts

from enthought.mayavi.core import lut 
lut_image_dir = os.path.dirname(lut.__file__)

#################################################################
# Utility functions.
#################################################################
def set_lut(vtk_lut, lut_lst):
    """Setup the tvtk.LookupTable (`vtk_lut`) using the passed list of
    lut values."""
    n_col = len(lut_lst)
    vtk_lut.number_of_colors = n_col
    vtk_lut.build()
    for i in range(0, n_col):
        lt = lut_lst[i]
        vtk_lut.set_table_value(i, lt[0], lt[1], lt[2], lt[3])

    return vtk_lut

def check_lut_first_line(line, file_name=''):
    """Check the line to see if this is a valid LUT file."""
    first = line.split()
    if first[0] != "LOOKUP_TABLE":
        errmsg = "Error: The input data file \"%s\"\n"%(file_name)
        errmsg = errmsg+ "is not a proper lookup table file."\
                 " No LOOKUP_TABLE tag in first line. Try again."
        raise IOError, errmsg
    try:
        n_color = first[2]
    except:
        
        raise IOError, "Error: No size for LookupTable specified."
    else:
        return n_color

def parse_lut_file(file_name):
    """Parse the file specified by its name `file_name` for a LUT and
    return the list of parsed values."""
    
    input = open(file_name, "r")

    line = input.readline()
    n_color = check_lut_first_line(line, file_name)

    lut = []
    for line in input.readlines():
        entr = line.split()
        if len(entr) != 4:
            errmsg="Error: insufficient or too much data in line "\
                    "-- \"%s\""%(entr)
            raise IOError, errmsg

        tmp = []
        for color in entr:
            try:
                tmp.append(float(color))
            except:
                raise IOError, \
                      "Unknown entry '%s'in lookup table input."%color
        lut.append(tmp)

    return lut


def lut_mode_list():
    """ Function to generate the list of acceptable lut_mode values.
    """
    lut_mode_list = ( ['blue-red', 'black-white', 'file', ] 
                            + [key for key in pylab_luts.keys() if 
                                                    not key[-2:]=='_r' ])
    lut_mode_list.sort()
    return lut_mode_list


######################################################################
# `LUTManager` class.
######################################################################
class LUTManager(Base):
    
    # The version of this class.  Used for persistence.
    __version__ = 0

    # The lookup table.
    lut = Instance(tvtk.LookupTable, (), record=False)
    # The scalar bar.
    scalar_bar = Instance(tvtk.ScalarBarActor, (), record=True)
    # The scalar_bar_widget
    scalar_bar_widget = Instance(tvtk.ScalarBarWidget, ())

    # The title text property of the axes.
    title_text_property = Property(record=True)
    
    # The label text property of the axes.
    label_text_property = Property(record=True)

    # The current mode of the LUT.
    lut_mode = Enum('blue-red', lut_mode_list(),
                     desc='the type of the lookup table')

    # File name of the LUT file to use.
    file_name = Str('', editor=FileEditor,
                    desc='the filename containing the LUT')

    # Reverse the colors of the LUT.
    reverse_lut = Bool(False, desc='if the lut is to be reversed')

    # Turn on/off the visibility of the scalar bar.
    show_scalar_bar = Bool(False,
                           desc='if scalar bar is shown or not')

    # The number of labels to use for the scalar bar.
    number_of_labels = Range(0, 64, 8,
                             desc='the number of labels to display')

    # Number of colors for the LUT.
    number_of_colors = Range(2, 2147483647, 256,
                             desc='the number of colors for the LUT')

    # Enable shadowing of the labels and text.
    shadow = Bool(False, desc='if the labels and text have shadows')

    # Use the default data name or the user specified one.
    use_default_name = Bool(True,
                            desc='if the default data name is to be used')

    # The default data name -- set by the module manager.
    default_data_name = Str('data', enter_set=True, auto_set=False, 
                            desc='the default data name')

    # The optionally user specified name of the data.
    data_name = Str('', enter_set=True, auto_set=False,
                    desc='the title of the legend')

    # Use the default range or user specified one.
    use_default_range = Bool(True,
                             desc='if the default data range is to be used')
    # The default data range -- this is computed and set by the
    # module manager.
    default_data_range = Array(shape=(2,), value=[0.0, 1.0],
                               dtype=float, enter_set=True, auto_set=False,
                               desc='the default range of the data mapped')
<commit_msg>ENH: Setting enter_set/auto_set for the range traits in the lut manager.<commit_after>"""This manages the lookup table used to map values to colors.

"""
# Author: <NAME> <<EMAIL>>
# Copyright (c) 2005-2008, Enthought, Inc.
# License: BSD Style.

import os.path
import sys
import subprocess

# Enthought library imports.
from enthought.traits.api import Instance, Range, Bool, Array, \
     Str, Property, Enum, Button
from enthought.traits.ui.api import FileEditor, auto_close_message
from enthought.tvtk.api import tvtk

# Local imports.
from enthought.mayavi.core.base import Base
from enthought.mayavi.core.common import error
from enthought.mayavi.core.lut.pylab_luts import lut_dic as pylab_luts

from enthought.mayavi.core import lut 
lut_image_dir = os.path.dirname(lut.__file__)

#################################################################
# Utility functions.
#################################################################
def set_lut(vtk_lut, lut_lst):
    """Setup the tvtk.LookupTable (`vtk_lut`) using the passed list of
    lut values."""
    n_col = len(lut_lst)
    vtk_lut.number_of_colors = n_col
    vtk_lut.build()
    for i in range(0, n_col):
        lt = lut_lst[i]
        vtk_lut.set_table_value(i, lt[0], lt[1], lt[2], lt[3])

    return vtk_lut

def check_lut_first_line(line, file_name=''):
    """Check the line to see if this is a valid LUT file."""
    first = line.split()
    if first[0] != "LOOKUP_TABLE":
        errmsg = "Error: The input data file \"%s\"\n"%(file_name)
        errmsg = errmsg+ "is not a proper lookup table file."\
                 " No LOOKUP_TABLE tag in first line. Try again."
        raise IOError, errmsg
    try:
        n_color = first[2]
    except:
        
        raise IOError, "Error: No size for LookupTable specified."
    else:
        return n_color

def parse_lut_file(file_name):
    """Parse the file specified by its name `file_name` for a LUT and
    return the list of parsed values."""
    
    input = open(file_name, "r")

    line = input.readline()
    n_color = check_lut_first_line(line, file_name)

    lut = []
    for line in input.readlines():
        entr = line.split()
        if len(entr) != 4:
            errmsg="Error: insufficient or too much data in line "\
                    "-- \"%s\""%(entr)
            raise IOError, errmsg

        tmp = []
        for color in entr:
            try:
                tmp.append(float(color))
            except:
                raise IOError, \
                      "Unknown entry '%s'in lookup table input."%color
        lut.append(tmp)

    return lut


def lut_mode_list():
    """ Function to generate the list of acceptable lut_mode values.
    """
    lut_mode_list = ( ['blue-red', 'black-white', 'file', ] 
                            + [key for key in pylab_luts.keys() if 
                                                    not key[-2:]=='_r' ])
    lut_mode_list.sort()
    return lut_mode_list


######################################################################
# `LUTManager` class.
######################################################################
class LUTManager(Base):
    
    # The version of this class.  Used for persistence.
    __version__ = 0

    # The lookup table.
    lut = Instance(tvtk.LookupTable, (), record=False)
    # The scalar bar.
    scalar_bar = Instance(tvtk.ScalarBarActor, (), record=True)
    # The scalar_bar_widget
    scalar_bar_widget = Instance(tvtk.ScalarBarWidget, ())

    # The title text property of the axes.
    title_text_property = Property(record=True)
    
    # The label text property of the axes.
    label_text_property = Property(record=True)

    # The current mode of the LUT.
    lut_mode = Enum('blue-red', lut_mode_list(),
                     desc='the type of the lookup table')

    # File name of the LUT file to use.
    file_name = Str('', editor=FileEditor,
                    desc='the filename containing the LUT')

    # Reverse the colors of the LUT.
    reverse_lut = Bool(False, desc='if the lut is to be reversed')

    # Turn on/off the visibility of the scalar bar.
    show_scalar_bar = Bool(False,
                           desc='if scalar bar is shown or not')

    # The number of labels to use for the scalar bar.
    number_of_labels = Range(0, 64, 8, enter_set=True, auto_set=False,
                             desc='the number of labels to display')

    # Number of colors for the LUT.
    number_of_colors = Range(2, 2147483647, 256, enter_set=True,
                             auto_set=False,
                             desc='the number of colors for the LUT')

    # Enable shadowing of the labels and text.
    shadow = Bool(False, desc='if the labels and text have shadows')

    # Use the default data name or the user specified one.
    use_default_name = Bool(True,
                            desc='if the default data name is to be used')

    # The default data name -- set by the module manager.
    default_data_name = Str('data', enter_set=True, auto_set=False, 
                            desc='the default data name')

    # The optionally user specified name of the data.
    data_name = Str('', enter_set=True, auto_set=False,
                    desc='the title of the legend')

    # Use the default range or user specified one.
    use_default_range = Bool(True,
                             desc='if the default data range is to be used')
    # The default data range -- this is computed and set by the
    # module manager.
    default_data_range = Array(shape=(2,), value=[0.0, 1.0],
                               dtype=float, enter_set=True, auto_set=False,
                               desc='the default range of the data mapped')

{'fraction_non_alphanumeric': 0.13920353982300884, 'fraction_numerical': 0.007256637168141593, 'mean_word_length': 2.395733173076923, 'pattern_counts': {'":': 2, '<': 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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5984104', 'n_tokens_mistral': 3398, 'n_tokens_neox': 3216, 'n_words': 1074}