<commit_before># ===============================================================================
# Copyright 2013 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===============================================================================

# ============= enthought library imports =======================
from chaco.abstract_overlay import AbstractOverlay
from chaco.array_data_source import ArrayDataSource
from chaco.data_label import DataLabel
from chaco.scatterplot import render_markers
from enable.colors import ColorTrait
from pyface.message_dialog import warning
from traits.api import Array
# ============= standard library imports ========================
from numpy import array, arange, \
    Inf, argmax
# ============= local library imports  ==========================
from uncertainties import nominal_value
from pychron.core.helpers.formatting import floatfmt
from pychron.core.stats.peak_detection import fast_find_peaks
from pychron.core.stats.probability_curves import cumulative_probability, kernel_density
from pychron.pipeline.plot.flow_label import FlowPlotLabel
from pychron.pipeline.plot.plotter.arar_figure import BaseArArFigure
from pychron.pipeline.plot.overlays.ideogram_inset_overlay import IdeogramInset, IdeogramPointsInset

from pychron.pipeline.plot.overlays.mean_indicator_overlay import MeanIndicatorOverlay
from pychron.pipeline.plot.point_move_tool import OverlayMoveTool
from pychron.graph.ticks import IntTickGenerator
from pychron.pychron_constants import PLUSMINUS, SIGMA

N = 500


class PeakLabel(DataLabel):
    pass


class LatestOverlay(AbstractOverlay):
    data_position = None
    color = ColorTrait('transparent')

    # The color of the outline to draw around the marker.
    outline_color = ColorTrait('orange')

    def overlay(self, other_component, gc, view_bounds=None, mode="normal"):
        with gc:
            pts = self.component.map_screen(self.data_position)
            render_markers(gc, pts, 'circle', 5, self.color_, 2, self.outline_color_)


class Ideogram(BaseArArFigure):
    xs = Array
    xes = Array
    # index_key = 'uage'
    ytitle = 'Relative Probability'
    # _reverse_sorted_analyses = True
    # _analysis_number_cnt = 1

    # x_grid_visible = False
    # y_grid_visible = False

    _omit_key = 'omit_ideo'

    # def get_update_dict(self):
    #     return {'_analysis_number_cnt': self._analysis_number_cnt}

    def plot(self, plots, legend=None):
        """
            plot data on plots
        """
        opt = self.options
        if opt.index_attr:
            index_attr = opt.index_attr
            if index_attr == 'uage' and opt.include_j_error:
                index_attr = 'uage_w_j_err'
        else:
            warning(None, 'X Value not set. Defaulting to Age')
            index_attr = 'uage'

        graph = self.graph

        try:
            self.xs, self.xes = array([(ai.nominal_value, ai.std_dev)
                                       for ai in self._get_xs(key=index_attr)]).T

        except (ValueError, AttributeError), e:
            print 'asdfasdf', e, index_attr
            return

        # omit = self._get_omitted(self.sorted_analyses)
        # omit = set(omit)
        if self.options.omit_by_tag:
            selection = self._get_omitted_by_tag(self.sorted_analyses)
        else:
            selection = []

        for pid, (plotobj, po) in enumerate(zip(graph.plots, plots)):
            try:
                args = getattr(self, '_plot_{}'.format(po.plot_name))(po, plotobj, pid)
            except AttributeError:
                import traceback

                traceback.print_exc()
                continue

            if args:
                scatter, aux_selection, invalid = args
                selection.extend(aux_selection)
                # print omit, selection, invalid
                # omit = omit.union(set(selection))

                # self.update_options_limits(pid)

                # for i, ai in enumerate(self.sorted_analyses):
                #     # print ai.record_id, i in omit
                #     ai.value_filter_omit = i in omit

                # self._asymptotic_limit_flag = True
                # opt = self.options
                # xmi, xma = self.xmi, self.xma
                # pad = '0.05'

                # if opt.use_asymptotic_limits:
                # xmi, xma = self.xmi, self.xma
                # elif opt.use_centered_range:
                # w2 = opt.centered_range / 2.0
                # r = self.center
                # xmi, xma = r - w2, w2 + r
                # pad=False
                # elif opt.xlow or opt.xhigh:
                # xmi, xma = opt.xlow, opt.xhigh
                # pad=False

        # print opt.use_centered_range, self.center, xmi, xma

        # self.xmi = min(xmi, self.xmi)
        # self.xma = max(xma, self.xma)
        # graph.set_x_limits(min_=xmi, max_=xma,pad=pad)

        t = index_attr
        if index_attr == 'uF':
            t = 'Ar40*/Ar39k'
        elif index_attr in ('uage', 'uage_w_j_err'):
            ref = self.analyses[0]
            age_units = ref.arar_constants.age_units
            t = 'Age ({})'.format(age_units)

        graph.set_x_title(t, plotid=0)

        # turn off ticks for prob plot by default
        plot = graph.plots[0]
        plot.value_axis.tick_label_formatter = lambda x: ''
        plot.value_axis.tick_visible = False

        if selection:
            print 'selection {}'.format(selection)
            self._rebuild_ideo(selection)

            # if omit:
            #     self._rebuild_ideo(list(omit))

    def mean_x(self, attr):
        # todo: handle other attributes
        return self.analysis_group.weighted_age.nominal_value

        # try:
        # return max([ai
        # for ai in self._unpack_attr(attr)])
        # except (AttributeError, ValueError):
        # return 0

    def max_x(self, attr):
        try:
            return max([ai.nominal_value + ai.std_dev
                        for ai in self._unpack_attr(attr) if ai is not None])
        except (AttributeError, ValueError), e:
            print 'max', e, 'attr={}'.format(attr)
            return 0

    def min_x(self, attr):
        try:
            return min([ai.nominal_value - ai.std_dev
                        for ai in self._unpack_attr(attr) if ai is not None])
        except (AttributeError, ValueError), e:
            print 'min', e
            return 0

    # ===============================================================================
    # plotters
    # ===============================================================================
    def _plot_aux(self, title, vk, ys, po, plot, pid,
                  es=None):
<commit_msg>address issue #992. ideogram min = min-2*sigma, max = max+2*sigma<commit_after># ===============================================================================
# Copyright 2013 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===============================================================================

# ============= enthought library imports =======================
from chaco.abstract_overlay import AbstractOverlay
from chaco.array_data_source import ArrayDataSource
from chaco.data_label import DataLabel
from chaco.scatterplot import render_markers
from enable.colors import ColorTrait
from numpy import array, arange, \
    Inf, argmax
from pyface.message_dialog import warning
from traits.api import Array
from uncertainties import nominal_value, std_dev

from pychron.core.helpers.formatting import floatfmt
from pychron.core.stats.peak_detection import fast_find_peaks
from pychron.core.stats.probability_curves import cumulative_probability, kernel_density
from pychron.graph.ticks import IntTickGenerator
from pychron.pipeline.plot.flow_label import FlowPlotLabel
from pychron.pipeline.plot.overlays.ideogram_inset_overlay import IdeogramInset, IdeogramPointsInset
from pychron.pipeline.plot.overlays.mean_indicator_overlay import MeanIndicatorOverlay
from pychron.pipeline.plot.plotter.arar_figure import BaseArArFigure
from pychron.pipeline.plot.point_move_tool import OverlayMoveTool
from pychron.pychron_constants import PLUSMINUS, SIGMA

N = 500


class PeakLabel(DataLabel):
    pass


class LatestOverlay(AbstractOverlay):
    data_position = None
    color = ColorTrait('transparent')

    # The color of the outline to draw around the marker.
    outline_color = ColorTrait('orange')

    def overlay(self, other_component, gc, view_bounds=None, mode="normal"):
        with gc:
            pts = self.component.map_screen(self.data_position)
            render_markers(gc, pts, 'circle', 5, self.color_, 2, self.outline_color_)


class Ideogram(BaseArArFigure):
    xs = Array
    xes = Array
    # index_key = 'uage'
    ytitle = 'Relative Probability'
    # _reverse_sorted_analyses = True
    # _analysis_number_cnt = 1

    # x_grid_visible = False
    # y_grid_visible = False

    _omit_key = 'omit_ideo'

    # def get_update_dict(self):
    #     return {'_analysis_number_cnt': self._analysis_number_cnt}

    def plot(self, plots, legend=None):
        """
            plot data on plots
        """
        opt = self.options
        if opt.index_attr:
            index_attr = opt.index_attr
            if index_attr == 'uage' and opt.include_j_error:
                index_attr = 'uage_w_j_err'
        else:
            warning(None, 'X Value not set. Defaulting to Age')
            index_attr = 'uage'

        graph = self.graph

        try:
            self.xs, self.xes = array([(ai.nominal_value, ai.std_dev)
                                       for ai in self._get_xs(key=index_attr)]).T

        except (ValueError, AttributeError), e:
            print 'asdfasdf', e, index_attr
            return

        # omit = self._get_omitted(self.sorted_analyses)
        # omit = set(omit)
        if self.options.omit_by_tag:
            selection = self._get_omitted_by_tag(self.sorted_analyses)
        else:
            selection = []

        for pid, (plotobj, po) in enumerate(zip(graph.plots, plots)):
            try:
                args = getattr(self, '_plot_{}'.format(po.plot_name))(po, plotobj, pid)
            except AttributeError:
                import traceback

                traceback.print_exc()
                continue

            if args:
                scatter, aux_selection, invalid = args
                selection.extend(aux_selection)
                # print omit, selection, invalid
                # omit = omit.union(set(selection))

                # self.update_options_limits(pid)

                # for i, ai in enumerate(self.sorted_analyses):
                #     # print ai.record_id, i in omit
                #     ai.value_filter_omit = i in omit

                # self._asymptotic_limit_flag = True
                # opt = self.options
                # xmi, xma = self.xmi, self.xma
                # pad = '0.05'

                # if opt.use_asymptotic_limits:
                # xmi, xma = self.xmi, self.xma
                # elif opt.use_centered_range:
                # w2 = opt.centered_range / 2.0
                # r = self.center
                # xmi, xma = r - w2, w2 + r
                # pad=False
                # elif opt.xlow or opt.xhigh:
                # xmi, xma = opt.xlow, opt.xhigh
                # pad=False

        # print opt.use_centered_range, self.center, xmi, xma

        # self.xmi = min(xmi, self.xmi)
        # self.xma = max(xma, self.xma)
        # graph.set_x_limits(min_=xmi, max_=xma,pad=pad)

        t = index_attr
        if index_attr == 'uF':
            t = 'Ar40*/Ar39k'
        elif index_attr in ('uage', 'uage_w_j_err'):
            ref = self.analyses[0]
            age_units = ref.arar_constants.age_units
            t = 'Age ({})'.format(age_units)

        graph.set_x_title(t, plotid=0)

        # turn off ticks for prob plot by default
        plot = graph.plots[0]
        plot.value_axis.tick_label_formatter = lambda x: ''
        plot.value_axis.tick_visible = False

        if selection:
            print 'selection {}'.format(selection)
            self._rebuild_ideo(selection)

            # if omit:
            #     self._rebuild_ideo(list(omit))

    def mean_x(self, attr):
        # todo: handle other attributes
        return self.analysis_group.weighted_age.nominal_value

        # try:
        # return max([ai
        # for ai in self._unpack_attr(attr)])
        # except (AttributeError, ValueError):
        # return 0

    def max_x(self, attr):
        try:
            return max([nominal_value(ai) + std_dev(ai)*2
                        for ai in self._unpack_attr(attr) if ai is not None])
        except (AttributeError, ValueError), e:
            print 'max', e, 'attr={}'.format(attr)
            return 0

    def min_x(self, attr):
        try:
            return min([nominal_value(ai) - std_dev(ai)*2
                        for ai in self._unpack_attr(attr) if ai is not None])
        except (AttributeError, ValueError), e:
            print 'min', e
            return 0

    # ===============================================================================
    # plotters
    # ===============================================================================
    def _plot_aux(self, title, vk, ys, po, plot, pid,
                  es=None):

{'fraction_non_alphanumeric': 0.13674023532688157, 'fraction_numerical': 0.004943256979739609, 'mean_word_length': 2.3529411764705883, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1677868', 'n_tokens_mistral': 4305, 'n_tokens_neox': 4023, 'n_words': 1056}