Пример #1
0
 def __init__(self, parent=None):
     super(DataFrameDock, self).__init__(parent)
     self.view = DataFrameViewWidget(self)
     self.setWindowTitle("Data")
     self.setObjectName("Data")
     dockWidgetContents = QWidget()
     verticalLayout = QVBoxLayout(dockWidgetContents)
     verticalLayout.addWidget(self.view)
     self.setWidget(dockWidgetContents)
Пример #2
0
    def __init__(self, parent=None):
        super(ExploreDataWidget, self).__init__(parent)
        self.setStyleSheet(OfSs.dock_style)
        # Create geometry
        self.setObjectName("ExploreData")
        self.setWindowTitle("ExploreData")
        self.dockWidgetContents = QWidget()

        self.data_label = QLabel("Data", self.dockWidgetContents)

        self.add_btn = QPushButton(u"Ajouter variable",
                                   self.dockWidgetContents)
        self.remove_btn = QPushButton(u"Retirer variable",
                                      self.dockWidgetContents)
        self.datatables_choices = []
        self.datatable_combo = MyComboBox(self.dockWidgetContents,
                                          u'Choix de la table',
                                          self.datatables_choices)

        #        self.add_btn.setDisabled(True)
        #        self.remove_btn.setDisabled(True)

        spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding,
                                 QSizePolicy.Minimum)

        horizontalLayout = QHBoxLayout()
        horizontalLayout.addWidget(self.add_btn)
        horizontalLayout.addWidget(self.remove_btn)
        horizontalLayout.addWidget(self.datatable_combo)
        horizontalLayout.addItem(spacerItem)
        self.view = DataFrameViewWidget(self.dockWidgetContents)

        verticalLayout = QVBoxLayout(self.dockWidgetContents)
        verticalLayout.addWidget(self.data_label)

        verticalLayout.addLayout(horizontalLayout)
        verticalLayout.addWidget(self.view)
        self.setWidget(self.dockWidgetContents)

        # Initialize attributes
        self.parent = parent

        self.selected_vars = set()
        self.data = DataFrame()
        self.view_data = None
        self.dataframes = {}
        self.vars = set()

        self.connect(self.add_btn, SIGNAL('clicked()'), self.add_var)
        self.connect(self.remove_btn, SIGNAL('clicked()'), self.remove_var)
        self.connect(self.datatable_combo.box,
                     SIGNAL('currentIndexChanged(int)'), self.select_data)

        self.update_btns()
Пример #3
0
class DataFrameDock(QDockWidget):
    def __init__(self, parent = None):
        super(DataFrameDock, self).__init__(parent)
        self.view = DataFrameViewWidget(self)
        self.setWindowTitle("Data")
        self.setObjectName("Data")
        dockWidgetContents = QWidget()
        verticalLayout = QVBoxLayout(dockWidgetContents)
        verticalLayout.addWidget(self.view)
        self.setWidget(dockWidgetContents)
        
    def set_dataframe(self, dataframe):
        self.view.set_dataframe(dataframe)
    
    def clear(self):
        self.view.clear()
Пример #4
0
class DataFrameDock(QDockWidget):
    def __init__(self, parent=None):
        super(DataFrameDock, self).__init__(parent)
        self.view = DataFrameViewWidget(self)
        self.setWindowTitle("Data")
        self.setObjectName("Data")
        dockWidgetContents = QWidget()
        verticalLayout = QVBoxLayout(dockWidgetContents)
        verticalLayout.addWidget(self.view)
        self.setWidget(dockWidgetContents)

    def set_dataframe(self, dataframe):
        self.view.set_dataframe(dataframe)

    def clear(self):
        self.view.clear()
Пример #5
0
 def __init__(self, parent = None):
     super(DataFrameDock, self).__init__(parent)
     self.view = DataFrameViewWidget(self)
     self.setWindowTitle("Data")
     self.setObjectName("Data")
     dockWidgetContents = QWidget()
     verticalLayout = QVBoxLayout(dockWidgetContents)
     verticalLayout.addWidget(self.view)
     self.setWidget(dockWidgetContents)
Пример #6
0
    def __init__(self, parent=None):
        super(AggregateOutputWidget, self).__init__(parent)
        self.setStyleSheet(OfSs.dock_style)
        # Create geometry
        self.setObjectName("Aggregate_Output")
        self.setWindowTitle("Aggregate_Output")
        self.dockWidgetContents = QWidget()

        agg_label = QLabel(u"Résultat aggregé de la simulation",
                           self.dockWidgetContents)
        self.aggregate_view = DataFrameViewWidget(self.dockWidgetContents)

        self.distribution_combo = MyComboBox(self.dockWidgetContents,
                                             u"Distribution de l'impact par")
        self.distribution_combo.box.setSizeAdjustPolicy(
            self.distribution_combo.box.AdjustToContents)
        self.distribution_combo.box.setDisabled(True)

        spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding,
                                 QSizePolicy.Minimum)

        horizontalLayout = QHBoxLayout()
        #horizontalLayout.addWidget(dist_label)
        horizontalLayout.addWidget(self.distribution_combo)
        horizontalLayout.addItem(spacerItem)

        self.distribution_view = DataFrameViewWidget(self.dockWidgetContents)

        verticalLayout = QVBoxLayout(self.dockWidgetContents)
        verticalLayout.addWidget(agg_label)
        verticalLayout.addWidget(self.aggregate_view)
        verticalLayout.addLayout(horizontalLayout)
        verticalLayout.addWidget(self.distribution_view)
        self.setWidget(self.dockWidgetContents)

        # Initialize attributes
        self.parent = parent
        self.varlist = [
            'irpp', 'ppe', 'af', 'cf', 'ars', 'aeeh', 'asf', 'aspa', 'aah',
            'caah', 'rsa', 'aefa', 'api', 'logt'
        ]
        self.data = DataFrame()  # Pandas DataFrame
Пример #7
0
    def __init__(self, parent = None):
        super(ExploreDataWidget, self).__init__(parent)
        self.setStyleSheet(OfSs.dock_style)
        # Create geometry
        self.setObjectName("ExploreData")
        self.setWindowTitle("ExploreData")
        self.dockWidgetContents = QWidget()


        self.data_label = QLabel("Data", self.dockWidgetContents)

        self.add_btn = QPushButton(u"Ajouter variable",self.dockWidgetContents)
        self.remove_btn = QPushButton(u"Retirer variable",self.dockWidgetContents)
        self.datatables_choices = []
        self.datatable_combo = MyComboBox(self.dockWidgetContents, u'Choix de la table', self.datatables_choices)

#        self.add_btn.setDisabled(True)
#        self.remove_btn.setDisabled(True)

        spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)

        horizontalLayout = QHBoxLayout()
        horizontalLayout.addWidget(self.add_btn)
        horizontalLayout.addWidget(self.remove_btn)
        horizontalLayout.addWidget(self.datatable_combo)
        horizontalLayout.addItem(spacerItem)
        self.view = DataFrameViewWidget(self.dockWidgetContents)

        verticalLayout = QVBoxLayout(self.dockWidgetContents)
        verticalLayout.addWidget(self.data_label)

        verticalLayout.addLayout(horizontalLayout)
        verticalLayout.addWidget(self.view)
        self.setWidget(self.dockWidgetContents)

        # Initialize attributes
        self.parent = parent

        self.selected_vars = set()
        self.data = DataFrame()
        self.view_data = None
        self.dataframes = {}
        self.vars = set()

        self.connect(self.add_btn, SIGNAL('clicked()'), self.add_var)
        self.connect(self.remove_btn, SIGNAL('clicked()'), self.remove_var)
        self.connect(self.datatable_combo.box, SIGNAL('currentIndexChanged(int)'), self.select_data)

        self.update_btns()
Пример #8
0
    def __init__(self, parent = None):
        super(AggregateOutputWidget, self).__init__(parent)
        self.setStyleSheet(OfSs.dock_style)
        # Create geometry
        self.setObjectName("Aggregate_Output")
        self.setWindowTitle("Aggregate_Output")
        self.dockWidgetContents = QWidget()
        
        agg_label = QLabel(u"Résultat aggregé de la simulation", self.dockWidgetContents)
        self.aggregate_view = DataFrameViewWidget(self.dockWidgetContents)


        self.distribution_combo = MyComboBox(self.dockWidgetContents, u"Distribution de l'impact par")
        self.distribution_combo.box.setSizeAdjustPolicy(self.distribution_combo.box.AdjustToContents)
        self.distribution_combo.box.setDisabled(True)
        
        spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)

        horizontalLayout = QHBoxLayout()
        #horizontalLayout.addWidget(dist_label)
        horizontalLayout.addWidget(self.distribution_combo)
        horizontalLayout.addItem(spacerItem)

        self.distribution_view = DataFrameViewWidget(self.dockWidgetContents)

        verticalLayout = QVBoxLayout(self.dockWidgetContents)
        verticalLayout.addWidget(agg_label)
        verticalLayout.addWidget(self.aggregate_view)
        verticalLayout.addLayout(horizontalLayout)
        verticalLayout.addWidget(self.distribution_view)
        self.setWidget(self.dockWidgetContents)

        # Initialize attributes
        self.parent = parent
        self.varlist = ['irpp', 'ppe', 'af', 'cf', 'ars', 'aeeh', 'asf', 'aspa', 'aah', 'caah', 'rsa', 'aefa', 'api', 'logt']
        self.data = DataFrame() # Pandas DataFrame
Пример #9
0
    def __init__(self, parent=None):
        super(ExploreDataWidget, self).__init__(parent)
        self.setStyleSheet(OfSs.dock_style)
        # Create geometry
        self.setObjectName("ExploreData")
        self.setWindowTitle("ExploreData")
        self.dockWidgetContents = QWidget()

        data_label = QLabel(u"Data", self.dockWidgetContents)

        self.add_btn = QPushButton(u"Ajouter variable", self.dockWidgetContents)
        self.remove_btn = QPushButton(u"Retirer variable", self.dockWidgetContents)
        self.add_btn.setDisabled(True)
        self.remove_btn.setDisabled(True)

        spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)

        horizontalLayout = QHBoxLayout()
        horizontalLayout.addWidget(self.add_btn)
        horizontalLayout.addWidget(self.remove_btn)
        horizontalLayout.addItem(spacerItem)
        self.view = DataFrameViewWidget(self.dockWidgetContents)

        verticalLayout = QVBoxLayout(self.dockWidgetContents)
        verticalLayout.addWidget(data_label)

        verticalLayout.addLayout(horizontalLayout)
        verticalLayout.addWidget(self.view)
        self.setWidget(self.dockWidgetContents)

        # Initialize attributes
        self.parent = parent
        self.selected_vars = set()
        self.data = DataFrame()  # Pandas DataFrame
        self.vars = set()

        self.connect(self.add_btn, SIGNAL("clicked()"), self.add_var)
        self.connect(self.remove_btn, SIGNAL("clicked()"), self.remove_var)

        self.update_btns()
Пример #10
0
class ExploreDataWidget(QDockWidget):
    def __init__(self, parent = None):
        super(ExploreDataWidget, self).__init__(parent)
        self.setStyleSheet(OfSs.dock_style)
        # Create geometry
        self.setObjectName("ExploreData")
        self.setWindowTitle("ExploreData")
        self.dockWidgetContents = QWidget()


        self.data_label = QLabel("Data", self.dockWidgetContents)

        self.add_btn = QPushButton(u"Ajouter variable",self.dockWidgetContents)
        self.remove_btn = QPushButton(u"Retirer variable",self.dockWidgetContents)
        self.datatables_choices = []
        self.datatable_combo = MyComboBox(self.dockWidgetContents, u'Choix de la table', self.datatables_choices)

#        self.add_btn.setDisabled(True)
#        self.remove_btn.setDisabled(True)

        spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)

        horizontalLayout = QHBoxLayout()
        horizontalLayout.addWidget(self.add_btn)
        horizontalLayout.addWidget(self.remove_btn)
        horizontalLayout.addWidget(self.datatable_combo)
        horizontalLayout.addItem(spacerItem)
        self.view = DataFrameViewWidget(self.dockWidgetContents)

        verticalLayout = QVBoxLayout(self.dockWidgetContents)
        verticalLayout.addWidget(self.data_label)

        verticalLayout.addLayout(horizontalLayout)
        verticalLayout.addWidget(self.view)
        self.setWidget(self.dockWidgetContents)

        # Initialize attributes
        self.parent = parent

        self.selected_vars = set()
        self.data = DataFrame()
        self.view_data = None
        self.dataframes = {}
        self.vars = set()

        self.connect(self.add_btn, SIGNAL('clicked()'), self.add_var)
        self.connect(self.remove_btn, SIGNAL('clicked()'), self.remove_var)
        self.connect(self.datatable_combo.box, SIGNAL('currentIndexChanged(int)'), self.select_data)

        self.update_btns()


    def set_year(self, year):
        '''
        Sets year in label
        '''
        self.data_label.setText("Survey data from year " + str(year))

    def update_btns(self):
        if (self.vars - self.selected_vars):
            self.add_btn.setEnabled(True)
        if self.selected_vars:
            self.remove_btn.setEnabled(True)

    def update_choices(self):
        box = self.datatable_combo.box
        box.clear()
        for name, key in self.datatables_choices:
            box.addItem(name, QVariant(key))

    def select_data(self):
        widget = self.datatable_combo.box
        dataframe_name = unicode(widget.itemData(widget.currentIndex()).toString())
        if dataframe_name: # to deal with the box.clear()
            self.set_dataframe(name = dataframe_name)
        self.update_btns()

    def add_dataframe(self, dataframe, name = None):
        '''
        Adds a dataframe to the list o the available dataframe
        '''
        if name == None:
            name = "dataframe" + len(self.dataframes.keys())

#        if not self.data:
#            self.dataframes = {}

        self.dataframes[name] = dataframe
        self.datatables_choices.append((name, name))
        self.update_choices()
        self.update_btns()

    def set_dataframe(self, dataframe = None, name = None):
        '''
        Sets the current dataframe
        '''
        if name is not None:
            self.data = self.dataframes[name]
        if dataframe is not None:
            self.data = dataframe

        self.vars = set(self.data.columns)
        self.update_btns()

    def ask(self, remove=False):
        if not remove:
            label = "Ajouter une variable"
            choices = self.vars - self.selected_vars
        else:
            choices =  self.selected_vars
            label = "Retirer une variable"

        var, ok = QInputDialog.getItem(self, label , "Choisir la variable",
                                       sorted(list(choices)))
        if ok and var in list(choices):
            return str(var)
        else:
            return None

    def add_var(self):
        var = self.ask()
        if var is not None:
            self.selected_vars.add(var)
            self.update_view()
        else:
            return

    def remove_var(self):
        var = self.ask(remove=True)
        if var is not None:
            self.selected_vars.remove(var)
            self.update_view()
        else:
            return

    def update_view(self):
        QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
        if not self.selected_vars:
            self.view.clear()
            QApplication.restoreOverrideCursor()
            return

        cols = self.selected_vars
        if self.view_data is None:
            self.view_data = self.data[list(cols)]

        new_col = cols - set(self.view_data)
        if new_col:
            self.view_data[list(new_col)] = self.data[list(new_col)]
            df = self.view_data
        else:
            df = self.view_data[list(cols)]

        self.view.set_dataframe(df)

        self.view.reset()
        self.update_btns()
        QApplication.restoreOverrideCursor()

    def calculated(self):
        self.emit(SIGNAL('calculated()'))

    def clear(self):
        self.view.clear()
        self.data = None
        self.datatables_choices = []
        self.dataframes = {}
        self.update_btns()
Пример #11
0
class ExploreDataWidget(QDockWidget):
    def __init__(self, parent=None):
        super(ExploreDataWidget, self).__init__(parent)
        self.setStyleSheet(OfSs.dock_style)
        # Create geometry
        self.setObjectName("ExploreData")
        self.setWindowTitle("ExploreData")
        self.dockWidgetContents = QWidget()

        data_label = QLabel(u"Data", self.dockWidgetContents)

        self.add_btn = QPushButton(u"Ajouter variable", self.dockWidgetContents)
        self.remove_btn = QPushButton(u"Retirer variable", self.dockWidgetContents)
        self.add_btn.setDisabled(True)
        self.remove_btn.setDisabled(True)

        spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)

        horizontalLayout = QHBoxLayout()
        horizontalLayout.addWidget(self.add_btn)
        horizontalLayout.addWidget(self.remove_btn)
        horizontalLayout.addItem(spacerItem)
        self.view = DataFrameViewWidget(self.dockWidgetContents)

        verticalLayout = QVBoxLayout(self.dockWidgetContents)
        verticalLayout.addWidget(data_label)

        verticalLayout.addLayout(horizontalLayout)
        verticalLayout.addWidget(self.view)
        self.setWidget(self.dockWidgetContents)

        # Initialize attributes
        self.parent = parent
        self.selected_vars = set()
        self.data = DataFrame()  # Pandas DataFrame
        self.vars = set()

        self.connect(self.add_btn, SIGNAL("clicked()"), self.add_var)
        self.connect(self.remove_btn, SIGNAL("clicked()"), self.remove_var)

        self.update_btns()

    def update_btns(self):
        if self.vars - self.selected_vars:
            self.add_btn.setEnabled(True)
        if self.selected_vars:
            self.remove_btn.setEnabled(True)

    def set_dataframe(self, dataframe):
        self.data = dataframe
        self.vars = set(self.data.columns)
        self.selected_vars = set()
        self.update_btns()

    def ask(self, remove=False):
        if not remove:
            label = "Ajouter une variable"
            choices = self.vars - self.selected_vars
        else:
            choices = self.selected_vars
            label = "Retirer une variable"

        var, ok = QInputDialog.getItem(self, label, "Choisir la variable", sorted(list(choices)))
        if ok and var in list(choices):
            return str(var)
        else:
            return None

    def add_var(self):
        var = self.ask()
        if var is not None:
            self.selected_vars.add(var)
            self.update_view()
        else:
            return

    def remove_var(self):
        var = self.ask(remove=True)
        if var is not None:
            self.selected_vars.remove(var)
            self.update_view()
        else:
            return

    def set_choices(self, description):
        """
        Set the variables appearing in the add and remove dialogs 
        """
        data_vars = set(self.data.columns)
        label2var = {}
        var2label = {}
        var2enum = {}
        for var in description.col_names:
            varcol = description.get_col(var)
            if isinstance(varcol, EnumCol):
                var2enum[var] = varcol.enum
                if varcol.label:
                    label2var[varcol.label] = var
                    var2label[var] = varcol.label
                else:
                    label2var[var] = var
                    var2label[var] = var

        all_labels = set(label2var.values()).intersection(data_vars)

        self.var2label = var2label
        self.var2enum = var2enum

    def update_view(self):
        QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
        if not self.selected_vars:
            self.view.clear()
            QApplication.restoreOverrideCursor()
            return

        cols = self.selected_vars
        df = self.data[list(cols)]
        self.view.set_dataframe(df)

        #        by_var_label = self.var2label[by_var]
        #        dist_frame.insert(0,by_var_label,u"")
        #        enum = self.var2enum[by_var]
        #        dist_frame[by_var_label] = dist_frame[by_var].apply(lambda x: enum._vars[x])
        #        dist_frame.pop(by_var)
        self.view.reset()
        self.update_btns()
        QApplication.restoreOverrideCursor()

    def calculated(self):
        self.emit(SIGNAL("calculated()"))

    def clear(self):
        self.view.clear()
        self.data = None
Пример #12
0
class AggregateOutputWidget(QDockWidget):
    def __init__(self, parent = None):
        super(AggregateOutputWidget, self).__init__(parent)
        self.setStyleSheet(OfSs.dock_style)
        # Create geometry
        self.setObjectName("Aggregate_Output")
        self.setWindowTitle("Aggregate_Output")
        self.dockWidgetContents = QWidget()
        
        agg_label = QLabel(u"Résultat aggregé de la simulation", self.dockWidgetContents)
        self.aggregate_view = DataFrameViewWidget(self.dockWidgetContents)


        self.distribution_combo = MyComboBox(self.dockWidgetContents, u"Distribution de l'impact par")
        self.distribution_combo.box.setSizeAdjustPolicy(self.distribution_combo.box.AdjustToContents)
        self.distribution_combo.box.setDisabled(True)
        
        spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)

        horizontalLayout = QHBoxLayout()
        #horizontalLayout.addWidget(dist_label)
        horizontalLayout.addWidget(self.distribution_combo)
        horizontalLayout.addItem(spacerItem)

        self.distribution_view = DataFrameViewWidget(self.dockWidgetContents)

        verticalLayout = QVBoxLayout(self.dockWidgetContents)
        verticalLayout.addWidget(agg_label)
        verticalLayout.addWidget(self.aggregate_view)
        verticalLayout.addLayout(horizontalLayout)
        verticalLayout.addWidget(self.distribution_view)
        self.setWidget(self.dockWidgetContents)

        # Initialize attributes
        self.parent = parent
        self.varlist = ['irpp', 'ppe', 'af', 'cf', 'ars', 'aeeh', 'asf', 'aspa', 'aah', 'caah', 'rsa', 'aefa', 'api', 'logt']
        self.data = DataFrame() # Pandas DataFrame

    def dist_by_changed(self):    
        widget = self.distribution_combo.box
        if isinstance(widget, QComboBox):
            data = widget.itemData(widget.currentIndex())
            by_var = unicode(data.toString())
            self.distribution_by_var = by_var                
            self.update_output(self.data)
    
    def set_data(self, output_data):
        self.data = output_data
        self.wght = self.data['wprm']
                 
    def set_distribution_choices(self, description):
        '''
        Set the variables appearing in the ComboBox 
        '''
        
        combobox = self.distribution_combo.box
        combobox.setEnabled(True)
        self.disconnect(combobox, SIGNAL('currentIndexChanged(int)'), self.dist_by_changed)
         
        output_data_vars = set(self.data.columns)
        self.distribution_combo.box.clear()
        label2var = {}
        var2label = {}
        var2enum = {}
        for var in description.col_names:
            varcol  = description.get_col(var)
            if isinstance(varcol, EnumCol): 
                var2enum[var] = varcol.enum
#            if isinstance(varcol, BoolCol) or isinstance(varcol, agesCol):
                if varcol.label:
                    label2var[varcol.label] = var
                    var2label[var]          = varcol.label
                else:
                    label2var[var] = var
                    var2label[var] = var
        
        for var in set(label2var.values()).intersection(output_data_vars):
            combobox.addItem(var2label[var], var )

        self.var2label = var2label
        self.var2enum  = var2enum
        if hasattr(self, 'distribution_by_var'):
            index = combobox.findData(self.distribution_by_var)
            if index != -1:
                combobox.setCurrentIndex(index)
        
        self.connect(self.distribution_combo.box, SIGNAL('currentIndexChanged(int)'), self.dist_by_changed)
                

    def update_output(self, output_data, description = None):
        QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))

        if output_data is None:
            return
        self.set_data(output_data)        
        
        if description is not None:  
            self.set_distribution_choices(description)
            
        if not hasattr(self, 'distribution_by_var'):
            self.distribution_by_var = 'typmen15'
        
        by_var = self.distribution_by_var
        

        V = []
        M = []
        B = []
        for var in self.varlist:
            montant, benef = self.get_aggregate(var)
            V.append(var)
            M.append(montant)
            B.append(benef)
        
        items = [(u'Mesure', V), 
                 (u"Dépense\n(millions d'€)", M), 
                 (u"Bénéficiaires\n(milliers de ménages)", B)]
        aggr_frame = DataFrame.from_items(items)
        self.aggregate_view.set_dataframe(aggr_frame)

        dist_frame = self.group_by(['revdisp', 'nivvie'], by_var)
        by_var_label = self.var2label[by_var]
        dist_frame.insert(0,by_var_label,u"") 
        enum = self.var2enum[by_var]
        dist_frame[by_var_label] = dist_frame[by_var].apply(lambda x: enum._vars[x])
        
        dist_frame.pop(by_var)
                
        self.distribution_view.set_dataframe(dist_frame)
        self.distribution_view.reset()
        self.calculated()
        QApplication.restoreOverrideCursor()
        
    def calculated(self):
        self.emit(SIGNAL('calculated()'))
                
    def get_aggregate(self, var):
        '''
        returns aggregate spending, nb of beneficiaries
        '''
        montants = self.data[var]
        beneficiaires = self.data[var].values != 0
        return int(round(sum(montants*self.wght)/10**6)), int(round(sum(beneficiaires*self.wght)/10**3))
    
    def group_by(self, varlist, category):
        keep = [category, 'wprm']
        temp = []
        for var in varlist:
            self.data['__' + var] = self.wght*self.data[var]
            temp.append('__'+var)
            keep.append('__'+var)
        grouped = self.data[keep].groupby(category, as_index = False)
        aggr = grouped.aggregate(np.sum)
        total = self.data[keep].sum()

        for varname in temp:
            aggr[varname] = aggr[varname]/aggr['wprm']
            total[varname] = total[varname]/total['wprm']
        
        return aggr

    def clear(self):
        self.aggregate_view.clear()
        self.distribution_view.clear()
        self.data = None
        self.wght = None
Пример #13
0
class ExploreDataWidget(QDockWidget):
    def __init__(self, parent=None):
        super(ExploreDataWidget, self).__init__(parent)
        self.setStyleSheet(OfSs.dock_style)
        # Create geometry
        self.setObjectName("ExploreData")
        self.setWindowTitle("ExploreData")
        self.dockWidgetContents = QWidget()

        self.data_label = QLabel("Data", self.dockWidgetContents)

        self.add_btn = QPushButton(u"Ajouter variable",
                                   self.dockWidgetContents)
        self.remove_btn = QPushButton(u"Retirer variable",
                                      self.dockWidgetContents)
        self.datatables_choices = []
        self.datatable_combo = MyComboBox(self.dockWidgetContents,
                                          u'Choix de la table',
                                          self.datatables_choices)

        #        self.add_btn.setDisabled(True)
        #        self.remove_btn.setDisabled(True)

        spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding,
                                 QSizePolicy.Minimum)

        horizontalLayout = QHBoxLayout()
        horizontalLayout.addWidget(self.add_btn)
        horizontalLayout.addWidget(self.remove_btn)
        horizontalLayout.addWidget(self.datatable_combo)
        horizontalLayout.addItem(spacerItem)
        self.view = DataFrameViewWidget(self.dockWidgetContents)

        verticalLayout = QVBoxLayout(self.dockWidgetContents)
        verticalLayout.addWidget(self.data_label)

        verticalLayout.addLayout(horizontalLayout)
        verticalLayout.addWidget(self.view)
        self.setWidget(self.dockWidgetContents)

        # Initialize attributes
        self.parent = parent

        self.selected_vars = set()
        self.data = DataFrame()
        self.view_data = None
        self.dataframes = {}
        self.vars = set()

        self.connect(self.add_btn, SIGNAL('clicked()'), self.add_var)
        self.connect(self.remove_btn, SIGNAL('clicked()'), self.remove_var)
        self.connect(self.datatable_combo.box,
                     SIGNAL('currentIndexChanged(int)'), self.select_data)

        self.update_btns()

    def set_year(self, year):
        '''
        Sets year in label
        '''
        self.data_label.setText("Survey data from year " + str(year))

    def update_btns(self):
        if (self.vars - self.selected_vars):
            self.add_btn.setEnabled(True)
        if self.selected_vars:
            self.remove_btn.setEnabled(True)

    def update_choices(self):
        box = self.datatable_combo.box
        box.clear()
        for name, key in self.datatables_choices:
            box.addItem(name, QVariant(key))

    def select_data(self):
        widget = self.datatable_combo.box
        dataframe_name = unicode(
            widget.itemData(widget.currentIndex()).toString())
        if dataframe_name:  # to deal with the box.clear()
            self.set_dataframe(name=dataframe_name)
        self.update_btns()

    def add_dataframe(self, dataframe, name=None):
        '''
        Adds a dataframe to the list o the available dataframe
        '''
        if name == None:
            name = "dataframe" + len(self.dataframes.keys())

#        if not self.data:
#            self.dataframes = {}

        self.dataframes[name] = dataframe
        self.datatables_choices.append((name, name))
        self.update_choices()
        self.update_btns()

    def set_dataframe(self, dataframe=None, name=None):
        '''
        Sets the current dataframe
        '''
        if name is not None:
            self.data = self.dataframes[name]
        if dataframe is not None:
            self.data = dataframe

        self.vars = set(self.data.columns)
        self.update_btns()

    def ask(self, remove=False):
        if not remove:
            label = "Ajouter une variable"
            choices = self.vars - self.selected_vars
        else:
            choices = self.selected_vars
            label = "Retirer une variable"

        var, ok = QInputDialog.getItem(self, label, "Choisir la variable",
                                       sorted(list(choices)))
        if ok and var in list(choices):
            return str(var)
        else:
            return None

    def add_var(self):
        var = self.ask()
        if var is not None:
            self.selected_vars.add(var)
            self.update_view()
        else:
            return

    def remove_var(self):
        var = self.ask(remove=True)
        if var is not None:
            self.selected_vars.remove(var)
            self.update_view()
        else:
            return

    def update_view(self):
        QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
        if not self.selected_vars:
            self.view.clear()
            QApplication.restoreOverrideCursor()
            return

        cols = self.selected_vars
        if self.view_data is None:
            self.view_data = self.data[list(cols)]

        new_col = cols - set(self.view_data)
        if new_col:
            self.view_data[list(new_col)] = self.data[list(new_col)]
            df = self.view_data
        else:
            df = self.view_data[list(cols)]

        self.view.set_dataframe(df)

        self.view.reset()
        self.update_btns()
        QApplication.restoreOverrideCursor()

    def calculated(self):
        self.emit(SIGNAL('calculated()'))

    def clear(self):
        self.view.clear()
        self.data = None
        self.datatables_choices = []
        self.dataframes = {}
        self.update_btns()
Пример #14
0
class AggregateOutputWidget(QDockWidget):
    def __init__(self, parent=None):
        super(AggregateOutputWidget, self).__init__(parent)
        self.setStyleSheet(OfSs.dock_style)
        # Create geometry
        self.setObjectName("Aggregate_Output")
        self.setWindowTitle("Aggregate_Output")
        self.dockWidgetContents = QWidget()

        agg_label = QLabel(u"Résultat aggregé de la simulation",
                           self.dockWidgetContents)
        self.aggregate_view = DataFrameViewWidget(self.dockWidgetContents)

        self.distribution_combo = MyComboBox(self.dockWidgetContents,
                                             u"Distribution de l'impact par")
        self.distribution_combo.box.setSizeAdjustPolicy(
            self.distribution_combo.box.AdjustToContents)
        self.distribution_combo.box.setDisabled(True)

        spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding,
                                 QSizePolicy.Minimum)

        horizontalLayout = QHBoxLayout()
        #horizontalLayout.addWidget(dist_label)
        horizontalLayout.addWidget(self.distribution_combo)
        horizontalLayout.addItem(spacerItem)

        self.distribution_view = DataFrameViewWidget(self.dockWidgetContents)

        verticalLayout = QVBoxLayout(self.dockWidgetContents)
        verticalLayout.addWidget(agg_label)
        verticalLayout.addWidget(self.aggregate_view)
        verticalLayout.addLayout(horizontalLayout)
        verticalLayout.addWidget(self.distribution_view)
        self.setWidget(self.dockWidgetContents)

        # Initialize attributes
        self.parent = parent
        self.varlist = [
            'irpp', 'ppe', 'af', 'cf', 'ars', 'aeeh', 'asf', 'aspa', 'aah',
            'caah', 'rsa', 'aefa', 'api', 'logt'
        ]
        self.data = DataFrame()  # Pandas DataFrame

    def dist_by_changed(self):
        widget = self.distribution_combo.box
        if isinstance(widget, QComboBox):
            data = widget.itemData(widget.currentIndex())
            by_var = unicode(data.toString())
            self.distribution_by_var = by_var
            self.update_output(self.data)

    def set_data(self, output_data):
        self.data = output_data
        self.wght = self.data['wprm']

    def set_distribution_choices(self, description):
        '''
        Set the variables appearing in the ComboBox 
        '''

        combobox = self.distribution_combo.box
        combobox.setEnabled(True)
        self.disconnect(combobox, SIGNAL('currentIndexChanged(int)'),
                        self.dist_by_changed)

        output_data_vars = set(self.data.columns)
        self.distribution_combo.box.clear()
        label2var = {}
        var2label = {}
        var2enum = {}
        for var in description.col_names:
            varcol = description.get_col(var)
            if isinstance(varcol, EnumCol):
                var2enum[var] = varcol.enum
                #            if isinstance(varcol, BoolCol) or isinstance(varcol, agesCol):
                if varcol.label:
                    label2var[varcol.label] = var
                    var2label[var] = varcol.label
                else:
                    label2var[var] = var
                    var2label[var] = var

        for var in set(label2var.values()).intersection(output_data_vars):
            combobox.addItem(var2label[var], var)

        self.var2label = var2label
        self.var2enum = var2enum
        if hasattr(self, 'distribution_by_var'):
            index = combobox.findData(self.distribution_by_var)
            if index != -1:
                combobox.setCurrentIndex(index)

        self.connect(self.distribution_combo.box,
                     SIGNAL('currentIndexChanged(int)'), self.dist_by_changed)

    def update_output(self, output_data, description=None):
        QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))

        if output_data is None:
            return
        self.set_data(output_data)

        if description is not None:
            self.set_distribution_choices(description)

        if not hasattr(self, 'distribution_by_var'):
            self.distribution_by_var = 'typmen15'

        by_var = self.distribution_by_var

        V = []
        M = []
        B = []
        for var in self.varlist:
            montant, benef = self.get_aggregate(var)
            V.append(var)
            M.append(montant)
            B.append(benef)

        items = [(u'Mesure', V), (u"Dépense\n(millions d'€)", M),
                 (u"Bénéficiaires\n(milliers de ménages)", B)]
        aggr_frame = DataFrame.from_items(items)
        self.aggregate_view.set_dataframe(aggr_frame)

        dist_frame = self.group_by(['revdisp', 'nivvie'], by_var)
        by_var_label = self.var2label[by_var]
        dist_frame.insert(0, by_var_label, u"")
        enum = self.var2enum[by_var]
        dist_frame[by_var_label] = dist_frame[by_var].apply(
            lambda x: enum._vars[x])

        dist_frame.pop(by_var)

        self.distribution_view.set_dataframe(dist_frame)
        self.distribution_view.reset()
        self.calculated()
        QApplication.restoreOverrideCursor()

    def calculated(self):
        self.emit(SIGNAL('calculated()'))

    def get_aggregate(self, var):
        '''
        returns aggregate spending, nb of beneficiaries
        '''
        montants = self.data[var]
        beneficiaires = self.data[var].values != 0
        return int(round(sum(montants * self.wght) / 10**6)), int(
            round(sum(beneficiaires * self.wght) / 10**3))

    def group_by(self, varlist, category):
        keep = [category, 'wprm']
        temp = []
        for var in varlist:
            self.data['__' + var] = self.wght * self.data[var]
            temp.append('__' + var)
            keep.append('__' + var)
        grouped = self.data[keep].groupby(category, as_index=False)
        aggr = grouped.aggregate(np.sum)
        total = self.data[keep].sum()

        for varname in temp:
            aggr[varname] = aggr[varname] / aggr['wprm']
            total[varname] = total[varname] / total['wprm']

        return aggr

    def clear(self):
        self.aggregate_view.clear()
        self.distribution_view.clear()
        self.data = None
        self.wght = None
Пример #15
0
class ExploreDataWidget(QDockWidget):
    def __init__(self, parent=None):
        super(ExploreDataWidget, self).__init__(parent)
        self.setStyleSheet(OfSs.dock_style)
        # Create geometry
        self.setObjectName("ExploreData")
        self.setWindowTitle("ExploreData")
        self.dockWidgetContents = QWidget()

        data_label = QLabel(u"Data", self.dockWidgetContents)

        self.add_btn = QPushButton(u"Ajouter variable",
                                   self.dockWidgetContents)
        self.remove_btn = QPushButton(u"Retirer variable",
                                      self.dockWidgetContents)
        self.add_btn.setDisabled(True)
        self.remove_btn.setDisabled(True)

        spacerItem = QSpacerItem(40, 20, QSizePolicy.Expanding,
                                 QSizePolicy.Minimum)

        horizontalLayout = QHBoxLayout()
        horizontalLayout.addWidget(self.add_btn)
        horizontalLayout.addWidget(self.remove_btn)
        horizontalLayout.addItem(spacerItem)
        self.view = DataFrameViewWidget(self.dockWidgetContents)

        verticalLayout = QVBoxLayout(self.dockWidgetContents)
        verticalLayout.addWidget(data_label)

        verticalLayout.addLayout(horizontalLayout)
        verticalLayout.addWidget(self.view)
        self.setWidget(self.dockWidgetContents)

        # Initialize attributes
        self.parent = parent
        self.selected_vars = set()
        self.data = DataFrame()  # Pandas DataFrame
        self.vars = set()

        self.connect(self.add_btn, SIGNAL('clicked()'), self.add_var)
        self.connect(self.remove_btn, SIGNAL('clicked()'), self.remove_var)

        self.update_btns()

    def update_btns(self):
        if (self.vars - self.selected_vars):
            self.add_btn.setEnabled(True)
        if self.selected_vars:
            self.remove_btn.setEnabled(True)

    def set_dataframe(self, dataframe):
        self.data = dataframe
        self.vars = set(self.data.columns)
        self.selected_vars = set()
        self.update_btns()

    def ask(self, remove=False):
        if not remove:
            label = "Ajouter une variable"
            choices = self.vars - self.selected_vars
        else:
            choices = self.selected_vars
            label = "Retirer une variable"

        var, ok = QInputDialog.getItem(self, label, "Choisir la variable",
                                       sorted(list(choices)))
        if ok and var in list(choices):
            return str(var)
        else:
            return None

    def add_var(self):
        var = self.ask()
        if var is not None:
            self.selected_vars.add(var)
            self.update_view()
        else:
            return

    def remove_var(self):
        var = self.ask(remove=True)
        if var is not None:
            self.selected_vars.remove(var)
            self.update_view()
        else:
            return

    def set_choices(self, description):
        '''
        Set the variables appearing in the add and remove dialogs 
        '''
        data_vars = set(self.data.columns)
        label2var = {}
        var2label = {}
        var2enum = {}
        for var in description.col_names:
            varcol = description.get_col(var)
            if isinstance(varcol, EnumCol):
                var2enum[var] = varcol.enum
                if varcol.label:
                    label2var[varcol.label] = var
                    var2label[var] = varcol.label
                else:
                    label2var[var] = var
                    var2label[var] = var

        all_labels = set(label2var.values()).intersection(data_vars)

        self.var2label = var2label
        self.var2enum = var2enum

    def update_view(self):
        QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
        if not self.selected_vars:
            self.view.clear()
            QApplication.restoreOverrideCursor()
            return

        cols = self.selected_vars
        df = self.data[list(cols)]
        self.view.set_dataframe(df)

        #        by_var_label = self.var2label[by_var]
        #        dist_frame.insert(0,by_var_label,u"")
        #        enum = self.var2enum[by_var]
        #        dist_frame[by_var_label] = dist_frame[by_var].apply(lambda x: enum._vars[x])
        #        dist_frame.pop(by_var)
        self.view.reset()
        self.update_btns()
        QApplication.restoreOverrideCursor()

    def calculated(self):
        self.emit(SIGNAL('calculated()'))

    def clear(self):
        self.view.clear()
        self.data = None