<commit_before>from ScopeFoundry.data_browser import DataBrowserView
import pyqtgraph as pg
import numpy as np
from qtpy import QtWidgets
import h5py

class SyncRasterScanH5(DataBrowserView):

    name = 'sync_raster_scan_h5'
    
    def setup(self):
        
        self.settings.New('frame', dtype=int)
        self.settings.New('sub_frame', dtype=int)
        self.settings.New('channel', dtype=str, choices=('ai0', 'ai1', 'ctr0', 'ctr1'))
        self.settings.New('auto_level', dtype=bool, initial=True)
        for name in ['frame', 'sub_frame','channel', 'auto_level']:
            self.settings.get_lq(name).add_listener(self.update_display)
        
        self.ui = QtWidgets.QWidget()
        self.ui.setLayout(QtWidgets.QVBoxLayout())
        self.ui.layout().addWidget(self.settings.New_UI(), stretch=0)
        self.info_label = QtWidgets.QLabel()
        self.ui.layout().addWidget(self.info_label, stretch=0)
        self.imview = pg.ImageView()
        self.ui.layout().addWidget(self.imview, stretch=1)
        
        #self.graph_layout = pg.GraphicsLayoutWidget()
        #self.graph_layout.addPlot()
        
    def on_change_data_filename(self, fname):
        
        try:
            self.dat = h5py.File(fname)
            M = self.measurement = self.dat['measurement/sem_sync_raster_scan']
            nframe, nsubframe, ny, nx, nadc_chan = M['adc_map'].shape
            self.settings.frame.change_min_max(0, nframe-1)
            self.settings.sub_frame.change_min_max(0, nsubframe-1)
            self.update_display()
        except Exception as err:
            self.imview.setImage(np.zeros((10,10)))
            self.databrowser.ui.statusbar.showMessage("failed to load %s:\n%s" %(fname, err))
            raise(err)
        
    def is_file_supported(self, fname):
        return "sem_sync_raster_scan.h5" in fname
    
    def update_display(self):
        
        M = self.measurement
        
        ii = self.settings['frame']
        jj = self.settings['sub_frame']
        chan = self.settings['channel']
        
        if   chan == 'ai0':
            im = M['adc_map'][ii, jj, :,:, 0]
        elif chan == 'ai1':
            im = M['adc_map'][ii, jj, :,:, 1]
        elif chan == 'ctr0':
            im = M['ctr_map'][ii, jj, :,:, 0]
        elif chan == 'ctr1':
            im = M['ctr_map'][ii, jj, :,:, 1]

        self.imview.setImage(im.T, autoLevels=self.settings['auto_level'], )


        #self.info_label.setText("{} plane {}={} um (index={})".format(
        #    plane, other_ax, self.dat[other_ax+'_array'][ii], ii))
        <commit_msg>Viewer: sync_raster_scan_h5 autoscale using percentile<commit_after>from ScopeFoundry.data_browser import DataBrowserView
import pyqtgraph as pg
import numpy as np
from qtpy import QtWidgets
import h5py

class SyncRasterScanH5(DataBrowserView):

    name = 'sync_raster_scan_h5'
    
    def setup(self):
        
        self.settings.New('frame', dtype=int)
        self.settings.New('sub_frame', dtype=int)
        self.settings.New('channel', dtype=str, choices=('ai0', 'ai1', 'ctr0', 'ctr1'))
        self.settings.New('auto_level', dtype=bool, initial=True)
        for name in ['frame', 'sub_frame','channel', 'auto_level']:
            self.settings.get_lq(name).add_listener(self.update_display)
        
        self.ui = QtWidgets.QWidget()
        self.ui.setLayout(QtWidgets.QVBoxLayout())
        self.ui.layout().addWidget(self.settings.New_UI(), stretch=0)
        self.info_label = QtWidgets.QLabel()
        self.ui.layout().addWidget(self.info_label, stretch=0)
        self.imview = pg.ImageView()
        self.ui.layout().addWidget(self.imview, stretch=1)
        
        #self.graph_layout = pg.GraphicsLayoutWidget()
        #self.graph_layout.addPlot()
        
    def on_change_data_filename(self, fname):
        
        try:
            self.dat = h5py.File(fname)
            M = self.measurement = self.dat['measurement/sem_sync_raster_scan']
            nframe, nsubframe, ny, nx, nadc_chan = M['adc_map'].shape
            self.settings.frame.change_min_max(0, nframe-1)
            self.settings.sub_frame.change_min_max(0, nsubframe-1)
            self.update_display()
        except Exception as err:
            self.imview.setImage(np.zeros((10,10)))
            self.databrowser.ui.statusbar.showMessage("failed to load %s:\n%s" %(fname, err))
            raise(err)
        
    def is_file_supported(self, fname):
        return "sem_sync_raster_scan.h5" in fname
    
    def update_display(self):
        
        M = self.measurement
        
        ii = self.settings['frame']
        jj = self.settings['sub_frame']
        chan = self.settings['channel']
        
        if   chan == 'ai0':
            im = M['adc_map'][ii, jj, :,:, 0]
        elif chan == 'ai1':
            im = M['adc_map'][ii, jj, :,:, 1]
        elif chan == 'ctr0':
            im = M['ctr_map'][ii, jj, :,:, 0]
        elif chan == 'ctr1':
            im = M['ctr_map'][ii, jj, :,:, 1]

        self.imview.setImage(im.T, autoLevels=self.settings['auto_level'], )

        if self.settings['auto_level']:
            self.imview.setLevels(*np.percentile(im, (1,99) ))

        #self.info_label.setText("{} plane {}={} um (index={})".format(
        #    plane, other_ax, self.dat[other_ax+'_array'][ii], ii))
        
{'fraction_non_alphanumeric': 0.1435791838262823, 'fraction_numerical': 0.011231748408835642, 'mean_word_length': 2.390228426395939, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3843423', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1808, 'n_words': 302}