<commit_before>
import time
import numpy as np

from . import io
from . import zwarning


class PlotSpec(object):
    def __init__(self, targets, templates, zscan, zfit, truth=None):
        '''
        TODO: document
        '''

        #- Isolate imports of optional dependencies
        import matplotlib.pyplot as plt

        self.targets = targets
        self.templates = templates
        self.zscan = zscan
        self.zfit = zfit
        self.itarget = 0
        self.znum = 0
        self.smooth = 1
        self.truth = truth

        self._fig = plt.figure()
        self._ax1 = self._fig.add_subplot(211)
        self._ax2 = self._fig.add_subplot(212)

        self._cid = self._fig.canvas.mpl_connect('key_press_event', self._onkeypress)

        #- Instructions
        print("-------------------------------------------------------------------------")
        print("Select window then use keyboard shortcuts to navigate:")
        print("    up/down arrow: previous/next target")
        print("    left/right arrow: previous/next redshift fit for this target")
        print("    (d)etails")
        print("-------------------------------------------------------------------------")

        #- Disable some default matplotlib key bindings so that we can use keys
        #- TODO: cache and reset when done
        plt.rcParams['keymap.forward'] = ''
        plt.rcParams['keymap.back'] = ''

        plt.ion()
        self.plot()
        plt.show()

    def _onkeypress(self, event):
        ### print('key', event.key)
        if event.key == 'right':
            self.znum = (self.znum + 1) % self.nznum
            self.plot(keepzoom=True)
        elif event.key == 'left':
            self.znum = (self.znum - 1) % self.nznum
            self.plot(keepzoom=True)
        elif event.key == 'down':
            if self.itarget == len(self.targets)-1:
                print('At last target')
            else:
                self.znum = 0
                self.itarget += 1
                self.plot()
        elif event.key == 'up':
            if self.itarget == 0:
                print('Already at first target')
            else:
                self.znum = 0
                self.itarget -= 1
                self.plot()
        elif event.key == 'd':
            target = self.targets[self.itarget]    
            zfit = self.zfit[self.zfit['targetid'] == target.id]
            print('target {}'.format(target.id))
            print(zfit['znum', 'spectype', 'z', 'zerr', 'zwarn', 'chi2'])

    def plot(self, keepzoom=False):

        #- Isolate imports of optional dependencies
        from scipy.signal import medfilt
        import matplotlib.pyplot as plt
        
        target = self.targets[self.itarget]    
        zfit = self.zfit[self.zfit['targetid'] == target.id]
        self.nznum = len(zfit)
        zz = zfit[zfit['znum'] == self.znum][0]
        coeff = zz['coeff']

        for tp in self.templates:
            if (tp.type == zz['spectype']) & (tp.subtype == zz['subtype']):
                break
    
        if tp.type != zz['spectype']:
            raise ValueError('spectype {} not in templates'.format(zz['spectype']))

        #----- zscan plot
        if keepzoom:
            force_xlim = self._ax1.get_xlim()<commit_msg>fixed list -> dict in plotspec<commit_after>
import time
import numpy as np

from . import io
from . import zwarning


class PlotSpec(object):
    def __init__(self, targets, templates, zscan, zfit, truth=None):
        '''
        TODO: document
        '''

        #- Isolate imports of optional dependencies
        import matplotlib.pyplot as plt

        self.targets = targets
        self.templates = templates
        self.zscan = zscan
        self.zfit = zfit
        self.itarget = 0
        self.znum = 0
        self.smooth = 1
        self.truth = truth

        self._fig = plt.figure()
        self._ax1 = self._fig.add_subplot(211)
        self._ax2 = self._fig.add_subplot(212)

        self._cid = self._fig.canvas.mpl_connect('key_press_event', self._onkeypress)

        #- Instructions
        print("-------------------------------------------------------------------------")
        print("Select window then use keyboard shortcuts to navigate:")
        print("    up/down arrow: previous/next target")
        print("    left/right arrow: previous/next redshift fit for this target")
        print("    (d)etails")
        print("-------------------------------------------------------------------------")

        #- Disable some default matplotlib key bindings so that we can use keys
        #- TODO: cache and reset when done
        plt.rcParams['keymap.forward'] = ''
        plt.rcParams['keymap.back'] = ''

        plt.ion()
        self.plot()
        plt.show()

    def _onkeypress(self, event):
        ### print('key', event.key)
        if event.key == 'right':
            self.znum = (self.znum + 1) % self.nznum
            self.plot(keepzoom=True)
        elif event.key == 'left':
            self.znum = (self.znum - 1) % self.nznum
            self.plot(keepzoom=True)
        elif event.key == 'down':
            if self.itarget == len(self.targets)-1:
                print('At last target')
            else:
                self.znum = 0
                self.itarget += 1
                self.plot()
        elif event.key == 'up':
            if self.itarget == 0:
                print('Already at first target')
            else:
                self.znum = 0
                self.itarget -= 1
                self.plot()
        elif event.key == 'd':
            target = self.targets[self.itarget]    
            zfit = self.zfit[self.zfit['targetid'] == target.id]
            print('target {}'.format(target.id))
            print(zfit['znum', 'spectype', 'z', 'zerr', 'zwarn', 'chi2'])

    def plot(self, keepzoom=False):

        #- Isolate imports of optional dependencies
        from scipy.signal import medfilt
        import matplotlib.pyplot as plt
        
        target = self.targets[self.itarget]    
        zfit = self.zfit[self.zfit['targetid'] == target.id]
        self.nznum = len(zfit)
        zz = zfit[zfit['znum'] == self.znum][0]
        coeff = zz['coeff']

        fulltype = zz['spectype']
        if zz['subtype'] != '':
            fulltype = fulltype+":"+zz['subtype']
        tp = self.templates[fulltype]

        if tp.type != zz['spectype']:
            raise ValueError('spectype {} not in templates'.format(zz['spectype']))

        #----- zscan plot
        if keepzoom:
            force_xlim = self._ax1.get_xlim()
{'fraction_non_alphanumeric': 0.1645703894529474, 'fraction_numerical': 0.0066676769207455674, 'mean_word_length': 1.5355359200922014, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 4, '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': '733610', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1874, 'n_words': 457}