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

import os
import time
import sys
import glob
import json
import logging
import argparse
import textwrap


if (sys.platform == 'darwin') and (os.name == 'posix'):
  # this is the only one that seems to work on Mac OSX with animation...
  import matplotlib
  matplotlib.use('TkAgg')

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mplticker
import matplotlib.animation as animation

import matplotlib.widgets

import selutil

# The directories to look in for json files.
TMPDIR = '/tmp/cal-dvmdostem'
YRTMPDIR = '/tmp/year-cal-dvmdostem'

#
# Disable some buttons on the default toobar that freeze the program.
# There might be a better way to do this. Inspired from here:
# http://matplotlib.1069221.n5.nabble.com/Overriding-Save-button-on-Toolbar-td40864.html
#
# More info here:
# http://stackoverflow.com/questions/14896580/matplotlib-hooking-in-to-home-back-forward-button-events
#
# This does not seem to work flawlessly - I am still getting freeze-ups!!
#
from matplotlib.backend_bases import NavigationToolbar2

def home_overload(self, *args, **kwargs):
  logging.info("HOME button pressed. DISABLED; doing nothing.")
  return 'break'

def back_overload(self, *args, **kwargs):
  logging.info("BACK button pressed. DISABLED; doing nothing.")
  return 'break'

def forward_overload(self, *args, **kwargs):
  logging.info("FORWARD button pressed. DISABLED; doing nothing.")
  return 'break'

NavigationToolbar2.home = home_overload
NavigationToolbar2.back = back_overload
NavigationToolbar2.forward = forward_overload


def log_file_stats(file_list):
  '''convenience function to write some info about files to the logs'''

  logging.info( "%i json files in %s" % (len(file_list), YRTMPDIR) )

  if len(file_list) > 0:
    ffy = int(os.path.basename(file_list[0])[0:4])
    lfy = int(os.path.basename(file_list[-1])[0:4])
    logging.debug( "First file: %s (year %s)" % (file_list[0], ffy) )
    logging.debug( "Last file: %s (year %s)" % (file_list[-1], lfy) )

    if lfy > 0:
      pc = 100 * len(file_list) / len(np.arange(ffy, lfy))
      logging.debug( "%s percent of range covered by existing files" % (pc) )
    else:
      logging.debug("Too few files to calculate % coverage.")

  else:
    logging.warning("No json files! Length of file list: %s." % len(file_list))


class ExpandingWindow(object):
  '''A set of expanding window plots that all share the x axis.
  '''

  def __init__(self, traceslist, figtitle="Expanding Window Plot",
      rows=2, cols=1, targets={}):

    logging.debug("Ctor for Expanding Window plot...")

    self.window_size_yrs = None

    self.traces = traceslist

    self.fig, self.axes = plt.subplots(rows, cols, sharex='all')
    self.fig.suptitle(figtitle)
    
    self.fig.canvas.mpl_connect('key_press_event', self.key_press_event)

    logging.debug("Setting up a radio button pft chooser...")
    #                           l    b    w     h
    self.pftradioax = plt.axes([0.9, 0.25, 0.1, 0.65 ], axisbg='lightgoldenrodyellow')
    self.pftradio = matplotlib.widgets.RadioButtons( self.pftradioax,
                                                     ['PFT%i'%(i) for i in range(0,10)],
                                                     active=int(self.get_currentpft()[-1])
                                                     )
    self.pftradio.on_clicked(self.pftchanger)

    # Set the x label for the last (lowest) subplot
    self.axes[-1].set_xlabel("Years")

    x = np.arange(0)
    y = x.copy() * np.nan
  
    logging.debug("Setting up empty x,y data for every trace...")
    for trace in self.traces:
      ax = self.axes[ trace['axesnum'] ]
      if 'pftpart' in trace.keys():
        lbl = '%s %s' % (trace['jsontag'], trace['pftpart'])
        trace['artists'] = ax.plot(x,y,label=lbl)
      else:
        trace['artists'] = ax.plot(x, y, label=trace['jsontag'])<commit_msg>Fix bug where suites with no pft varaibles would fail when building RadioButton.<commit_after>#!/usr/bin/env python

import os
import time
import sys
import glob
import json
import logging
import argparse
import textwrap


if (sys.platform == 'darwin') and (os.name == 'posix'):
  # this is the only one that seems to work on Mac OSX with animation...
  import matplotlib
  matplotlib.use('TkAgg')

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mplticker
import matplotlib.animation as animation

import matplotlib.widgets

import selutil

# The directories to look in for json files.
TMPDIR = '/tmp/cal-dvmdostem'
YRTMPDIR = '/tmp/year-cal-dvmdostem'

#
# Disable some buttons on the default toobar that freeze the program.
# There might be a better way to do this. Inspired from here:
# http://matplotlib.1069221.n5.nabble.com/Overriding-Save-button-on-Toolbar-td40864.html
#
# More info here:
# http://stackoverflow.com/questions/14896580/matplotlib-hooking-in-to-home-back-forward-button-events
#
# This does not seem to work flawlessly - I am still getting freeze-ups!!
#
from matplotlib.backend_bases import NavigationToolbar2

def home_overload(self, *args, **kwargs):
  logging.info("HOME button pressed. DISABLED; doing nothing.")
  return 'break'

def back_overload(self, *args, **kwargs):
  logging.info("BACK button pressed. DISABLED; doing nothing.")
  return 'break'

def forward_overload(self, *args, **kwargs):
  logging.info("FORWARD button pressed. DISABLED; doing nothing.")
  return 'break'

NavigationToolbar2.home = home_overload
NavigationToolbar2.back = back_overload
NavigationToolbar2.forward = forward_overload


def log_file_stats(file_list):
  '''convenience function to write some info about files to the logs'''

  logging.info( "%i json files in %s" % (len(file_list), YRTMPDIR) )

  if len(file_list) > 0:
    ffy = int(os.path.basename(file_list[0])[0:4])
    lfy = int(os.path.basename(file_list[-1])[0:4])
    logging.debug( "First file: %s (year %s)" % (file_list[0], ffy) )
    logging.debug( "Last file: %s (year %s)" % (file_list[-1], lfy) )

    if lfy > 0:
      pc = 100 * len(file_list) / len(np.arange(ffy, lfy))
      logging.debug( "%s percent of range covered by existing files" % (pc) )
    else:
      logging.debug("Too few files to calculate % coverage.")

  else:
    logging.warning("No json files! Length of file list: %s." % len(file_list))


class ExpandingWindow(object):
  '''A set of expanding window plots that all share the x axis.
  '''

  def __init__(self, traceslist, figtitle="Expanding Window Plot",
      rows=2, cols=1, targets={}):

    logging.debug("Ctor for Expanding Window plot...")

    self.window_size_yrs = None

    self.traces = traceslist

    self.fig, self.axes = plt.subplots(rows, cols, sharex='all')
    self.fig.suptitle(figtitle)
    
    self.fig.canvas.mpl_connect('key_press_event', self.key_press_event)

    # build a list of the pft specific traces
    pfttraces = []
    for trace in self.traces:
      if 'pft' in trace.keys():
        pfttraces.append(trace['jsontag'])

    if len(pfttraces) > 0:
      logging.debug("Setting up a radio button pft chooser...")
      # left, bottom, width, height 
      self.pftradioax = plt.axes([0.9, 0.25, 0.1, 0.65 ], axisbg='lightgoldenrodyellow')
      self.pftradio = matplotlib.widgets.RadioButtons(
          self.pftradioax,
          ['PFT%i'%(i) for i in range(0,10)],
          active=int(self.get_currentpft()[-1])
      )
      self.pftradio.on_clicked(self.pftchanger)

    # Set the x label for the last (lowest) subplot
    self.axes[-1].set_xlabel("Years")

    x = np.arange(0)
    y = x.copy() * np.nan
  
    logging.debug("Setting up empty x,y data for every trace...")
    for trace in self.traces:
      ax = self.axes[ trace['axesnum'] ]
      if 'pftpart' in trace.keys():
        lbl = '%s %s' % (trace['jsontag'], trace['pftpart'])
        trace['artists'] = ax.plot(x,y,label=lbl)
      else:
        trace['artists'] = ax.plot(x, y, label=trace['jsontag'])
{'fraction_non_alphanumeric': 0.1290929619872036, 'fraction_numerical': 0.014176389411617111, 'mean_word_length': 3.3115197404002163, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '792528', 'n_tokens_mistral': 2772, 'n_tokens_neox': 2668, 'n_words': 734}