<commit_before># -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
from lib.analysis.AnalysisModule import AnalysisModule
#from flowchart import *
import os
from collections import OrderedDict
import debug
import numpy as np
#import FileLoader
#import DatabaseGui
#import FeedbackButton
from MosaicEditorTemplate import *
import DataManager
import lib.analysis.atlas as atlas

class MosaicEditor(AnalysisModule):
    def __init__(self, host):
        AnalysisModule.__init__(self, host)
        
        self.ctrl = QtGui.QWidget()
        self.ui = Ui_Form()
        self.ui.setupUi(self.ctrl)
        self.atlas = None
        
        self._elements_ = OrderedDict([
            ('File Loader', {'type': 'fileInput', 'size': (200, 300), 'host': self}),
            ('Mosaic', {'type': 'ctrl', 'object': self.ctrl, 'pos': ('right',), 'size': (600, 200)}),
            ('Canvas', {'type': 'canvas', 'pos': ('bottom', 'Mosaic'), 'size': (600, 800), 'args': {'name': 'MosaicEditor'}}),
        ])

        self.initializeElements()

        self.ui.canvas = self.getElement('Canvas', create=True)
        self.items = {}
        self.cells = {}
        
        #addScanImagesBtn = QtGui.QPushButton()
        #addScanImagesBtn.setText('Add Scan Image')
        self.ui.fileLoader = self.getElement('File Loader', create=True)
        self.ui.fileLoader.ui.fileTree.hide()
        #self.ui.fileLoader.ui.verticalLayout_2.addWidget(addScanImagesBtn)
        try:
            self.ui.fileLoader.setBaseClicked() # get the currently selected directory in the DataManager
        except:
            pass

        for a in atlas.listAtlases():
            self.ui.atlasCombo.addItem(a)

        #self.connect(self.ui.canvas, QtCore.SIGNAL('itemTransformChangeFinished'), self.itemMoved)
        self.ui.canvas.sigItemTransformChangeFinished.connect(self.itemMoved)
        #self.connect(addScanImagesBtn, QtCore.SIGNAL('clicked()'), self.loadScanImage)
        #self.ui.exportSvgBtn.clicked.connect(self.exportSvg)
        self.ui.atlasCombo.currentIndexChanged.connect(self.atlasComboChanged)
        #self.ui.normalizeBtn.clicked.connect(self.normalizeImages)

    def atlasComboChanged(self, ind):
        if ind == 0:
            self.closeAtlas()
            return
        name = self.ui.atlasCombo.currentText()
        self.loadAtlas(name)

    def closeAtlas(self):
        if self.atlas is not None:
            self.atlas.close()
            self.atlas = None
        while True:
            ch = self.ui.atlasLayout.takeAt(0)
            if ch is None:
                break
            ch = ch.widget()
            ch.hide()
            ch.setParent(None)
        
    def loadAtlas(self, name):
        name = str(name)
        self.closeAtlas()
        
        cls = atlas.getAtlasClass(name)
        obj = cls()
        ctrl = obj.ctrlWidget(host=self)
        self.ui.atlasLayout.addWidget(ctrl, 0, 0)
        self.atlas = ctrl
        

    def loadFileRequested(self, files):
        canvas = self.getElement('Canvas')
        if files is None:
            return

        for f in files:
            item = canvas.addFile(f)
            if isinstance(item, list):
                item = item[0]
            self.items[item] = f
            try:
                item.timestamp = f.info()['__timestamp__']
            except:
                item.timestamp = None

            if not item.hasUserTransform() and item.timestamp is not None:
                ## Record the timestamp for this file, see what is the most recent transformation to copy
                best = None
                for i2 in self.items:
                    if i2 is item:
                        continue
                    if i2.timestamp is None :
                        continue
                    if i2.timestamp < item.timestamp:
                        if best is None or i2.timestamp > best.timestamp:
                            best = i2
                            
                if best is None:
                    continue
                    
                trans = best.saveTransform()
                item.restoreTransform(trans)
                
        canvas.selectItem(item)
        canvas.autoRange()
    

    def itemMoved(self, canvas, item):
        """Save an item's transformation if the user has moved it. 
        This is saved in the 'userTransform' attribute; the original position data is not affected."""
        fh = self.items.get(item, None)
        if not hasattr(fh, 'setInfo'):
            fh = None
            
        try:
            item.storeUserTransform(fh)
        except Exception as ex:
            if len(ex.args) > 1 and ex.args[1] == 1:  ## this means the item has no file handle to store position
                return
            raise
<commit_msg>CN updates: - DB storage seems to be working - Fixes in atlas mapping<commit_after># -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
from lib.analysis.AnalysisModule import AnalysisModule
#from flowchart import *
import os
from collections import OrderedDict
import debug
import numpy as np
#import FileLoader
#import DatabaseGui
#import FeedbackButton
from MosaicEditorTemplate import *
import DataManager
import lib.analysis.atlas as atlas

class MosaicEditor(AnalysisModule):
    def __init__(self, host):
        AnalysisModule.__init__(self, host)
        
        self.ctrl = QtGui.QWidget()
        self.ui = Ui_Form()
        self.ui.setupUi(self.ctrl)
        self.atlas = None
        
        self._elements_ = OrderedDict([
            ('File Loader', {'type': 'fileInput', 'size': (200, 300), 'host': self}),
            ('Mosaic', {'type': 'ctrl', 'object': self.ctrl, 'pos': ('right',), 'size': (600, 200)}),
            ('Canvas', {'type': 'canvas', 'pos': ('bottom', 'Mosaic'), 'size': (600, 800), 'args': {'name': 'MosaicEditor'}}),
        ])

        self.initializeElements()

        self.ui.canvas = self.getElement('Canvas', create=True)
        self.items = {}
        self.cells = {}
        #self.loaded = []
        
        #addScanImagesBtn = QtGui.QPushButton()
        #addScanImagesBtn.setText('Add Scan Image')
        self.ui.fileLoader = self.getElement('File Loader', create=True)
        self.ui.fileLoader.ui.fileTree.hide()
        #self.ui.fileLoader.ui.verticalLayout_2.addWidget(addScanImagesBtn)
        try:
            self.ui.fileLoader.setBaseClicked() # get the currently selected directory in the DataManager
        except:
            pass

        for a in atlas.listAtlases():
            self.ui.atlasCombo.addItem(a)

        #self.connect(self.ui.canvas, QtCore.SIGNAL('itemTransformChangeFinished'), self.itemMoved)
        self.ui.canvas.sigItemTransformChangeFinished.connect(self.itemMoved)
        #self.connect(addScanImagesBtn, QtCore.SIGNAL('clicked()'), self.loadScanImage)
        #self.ui.exportSvgBtn.clicked.connect(self.exportSvg)
        self.ui.atlasCombo.currentIndexChanged.connect(self.atlasComboChanged)
        #self.ui.normalizeBtn.clicked.connect(self.normalizeImages)

    def atlasComboChanged(self, ind):
        if ind == 0:
            self.closeAtlas()
            return
        name = self.ui.atlasCombo.currentText()
        self.loadAtlas(name)

    def closeAtlas(self):
        if self.atlas is not None:
            self.atlas.close()
            self.atlas = None
        while True:
            ch = self.ui.atlasLayout.takeAt(0)
            if ch is None:
                break
            ch = ch.widget()
            ch.hide()
            ch.setParent(None)
        
    def loadAtlas(self, name):
        name = str(name)
        self.closeAtlas()
        
        cls = atlas.getAtlasClass(name)
        obj = cls()
        ctrl = obj.ctrlWidget(host=self)
        self.ui.atlasLayout.addWidget(ctrl, 0, 0)
        self.atlas = ctrl
        

    def loadFileRequested(self, files):
        canvas = self.getElement('Canvas')
        if files is None:
            return

        for f in files:
            item = canvas.addFile(f)
            if isinstance(item, list):
                item = item[0]
            self.items[item] = f
            try:
                item.timestamp = f.info()['__timestamp__']
            except:
                item.timestamp = None

            #self.loaded.append(f)
            
            ## load or guess user transform for this item
            if not item.hasUserTransform() and item.timestamp is not None:
                ## Record the timestamp for this file, see what is the most recent transformation to copy
                best = None
                for i2 in self.items:
                    if i2 is item:
                        continue
                    if i2.timestamp is None :
                        continue
                    if i2.timestamp < item.timestamp:
                        if best is None or i2.timestamp > best.timestamp:
                            best = i2
                            
                if best is None:
                    continue
                    
                trans = best.saveTransform()
                item.restoreTransform(trans)
                
            
        canvas.selectItem(item)
        canvas.autoRange()
    

    def itemMoved(self, canvas, item):
        """Save an item's transformation if the user has moved it. 
        This is saved in the 'userTransform' attribute; the original position data is not affected."""
        fh = self.items.get(item, None)
        if not hasattr(fh, 'setInfo'):
            fh = None
            
        try:
            item.storeUserTransform(fh)
        except Exception as ex:
            if len(ex.args) > 1 and ex.args[1] == 1:  ## this means the item has no file handle to store position
                return
            raise

    def getLoadedFiles(self):
        """Return a list of all file handles that have been loaded"""
        return self.items.values()
        
        
{'fraction_non_alphanumeric': 0.10140562248995984, 'fraction_numerical': 0.007028112449799197, 'mean_word_length': 1.7615747158303299, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '408532', 'n_tokens_mistral': 2814, 'n_tokens_neox': 2696, 'n_words': 708}