Exemplo n.º 1
0
    def fillSetInfo(self, set='default'):

        databaseHandler = ProjectSQLiteHandler()
        # dictionary of set info
        setInfo = databaseHandler.getSetInfo(set)
        databaseHandler.closeDatabase()
        if type(setInfo['component_names']) == str:
            self.componentDefault = setInfo['component_names'].split(',')
        else:
            self.componentDefault = setInfo['component_names']
        start = datetime.datetime.strptime(setInfo['min_date'], '%Y-%m-%d')
        end = datetime.datetime.strptime(setInfo['max_date'], '%Y-%m-%d')

        #dates are strings here but they need to be datetimes
        self.startDate = setInfo.get('date_start')
        self.endDate = setInfo.get('date_end')
        self.getDefaultDates(start=self.startDate, end=self.endDate)
        #fillSetInfo the widget values

        self.setDateSelectorProperties(
            self.findChild(QtWidgets.QDateEdit, 'startDate'))
        self.setDateSelectorProperties(
            self.findChild(QtWidgets.QDateEdit, 'endDate'), False)
        self.findChild(QtWidgets.QDateEdit,
                       'startDate').setDateRange(start, end)
        self.findChild(QtWidgets.QDateEdit, 'endDate').setDateRange(start, end)
        self.findChild(QtWidgets.QLineEdit, 'componentNames').setText(','.join(
            self.componentDefault))
        self.updateComponentDelegate(self.componentDefault)

        return
Exemplo n.º 2
0
    def currentIndexChanged(self):

        self.commitData.emit(self.sender())
        #if a type from the components table was just set then fill in the component name unless it is already named
        if (self.name == 'component_type') & (self.parent.objectName()
                                              == 'components'):
            #get the table view object
            tv = self.parent
            #combo is the combo box that is sending these data
            combo = self.sender()
            #current row
            currentRow = tv.indexAt(combo.pos()).row()

            #check if there is already a component name
            currentName = tv.model().data(tv.model().index(currentRow, 4))
            if (currentName
                    == '') | (currentName is None) | (currentName == 'NA') | (
                        currentName[0:3] != self.sender().currentText()):
                #get the number of components of this type -
                handler = ProjectSQLiteHandler()
                i = handler.getTypeCount(self.sender().currentText())
                name = self.sender().currentText() + str(i)
                tv.model().setData(tv.model().index(currentRow, 4), name)
                tv.model().submitAll()
                tv.model().select()
                handler.closeDatabase()
            return
Exemplo n.º 3
0
    def getDefaultDates(self, **kwargs):
        #tuples
        start = kwargs.get('start')
        end = kwargs.get('end')

        handler = ProjectSQLiteHandler()
        if start == None:
            start = handler.cursor.execute(
                "select date_start from setup where set_name = 'default'"
            ).fetchone()
        if end == None:
            end = handler.cursor.execute(
                "select date_end from setup where set_name = 'default'"
            ).fetchone()
        handler.closeDatabase()

        #format the tuples from database output to datetime objects
        if type(start) == str:
            start = datetime.datetime.strptime(start, '%Y-%m-%d')
            end = datetime.datetime.strptime(end, '%Y-%m-%d')
        else:
            start = datetime.datetime.strptime(start[0], '%Y-%m-%d')
            end = datetime.datetime.strptime(end[0], '%Y-%m-%d')
        self.startDate = start
        self.endDate = end
        return
Exemplo n.º 4
0
    def createInputFiles(self):
        import os
        self.addProgressBar()
        self.progress.setRange(0, 0)
        self.sendSetupData()
        # check all the required fields are filled
        dbhandler = ProjectSQLiteHandler()
        if not dbhandler.dataComplete():
            #if required fields are not filled in return to setup page.
            msg = QtWidgets.QMessageBox(
                QtWidgets.QMessageBox.Warning, "Missing Required Fields",
                "Please fill in all required fields before generating input files."
            )
            msg.setStandardButtons(QtWidgets.QMessageBox.Ok)
            msg.exec()
            dbhandler.closeDatabase()
            return

        dbhandler.closeDatabase()
        # write all the xml files

        # start with the setupxml
        self.model.writeNewXML()

        # import datafiles
        handler = UIToHandler()
        cleaned_data, components = handler.loadFixData(
            os.path.join(model.setupFolder, model.project + 'Setup.xml'))
        self.updateModelPage(cleaned_data)
        # pickled data to be used later if needed
        handler.storeData(
            cleaned_data,
            os.path.join(model.setupFolder, model.project + 'Setup.xml'))
        handler.storeComponents(
            components,
            os.path.join(model.setupFolder, model.project + 'Setup.xml'))
        self.dataLoaded.setText('data loaded')
        self.progress.setRange(0, 1)
        # generate netcdf files
        msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning,
                                    "Time Series loaded",
                                    "Do you want to generate netcdf files?.")
        msg.setStandardButtons(QtWidgets.QMessageBox.Ok
                               | QtWidgets.QMessageBox.Cancel)
        result = msg.exec()

        # if yes create netcdf files, Otherwise this can be done after the data is reviewed.
        if result == QtWidgets.QMessageBox.Ok:
            d = {}
            for c in components:
                d[c.column_name] = c.toDictionary()
            handler.createNetCDF(
                cleaned_data.fixed, d,
                os.path.join(model.setupFolder, model.project + 'Setup.xml'))

        return
Exemplo n.º 5
0
        def makeComponentList():
            import pandas as pd

            sqlhandler = ProjectSQLiteHandler('project_manager')
            components = pd.read_sql_query(
                "select component_name from components", sqlhandler.connection)

            components = list(components['component_name'])
            sqlhandler.closeDatabase()
            return components
Exemplo n.º 6
0
def clearProjectDatabase(caller=None):
    handler = ProjectSQLiteHandler()
    # get the name of the last project worked on
    lastProjectPath = handler.getProjectPath()
    handler.makeDatabase()
    print(handler.dataCheck('components'))
    handler.closeDatabase()
    #the forms need to be cleared or data will get re-written to database
    if caller is not None:
        clearAppForms(caller)
    return lastProjectPath
Exemplo n.º 7
0
def makeAttributeXML(currentSet, compmodel):
    from UserInterface.ProjectSQLiteHandler import ProjectSQLiteHandler
    from PyQt5 import QtWidgets
    soup = readTemplateAttributeXML()

    #fillSetInfo the soup to reflect the model
    #for each row in model
    compName = ''
    compTag = ''
    compAttr = ''
    compValue = ''
    for i in range(compmodel.rowCount()):
        compName = ' '.join([compName, compmodel.data(compmodel.index(i, 2))])
        compTag = ' '.join([
            compTag,
            '.'.join(compmodel.data(compmodel.index(i, 3)).split('.')[:-1])
        ])
        compAttr = ' '.join(
            [compAttr,
             compmodel.data(compmodel.index(i, 3)).split('.')[-1]])
        compValue = ' '.join(
            [compValue, compmodel.data(compmodel.index(i, 4))])

    tag = soup.find('compName')
    tag.attrs['value'] = compName.lstrip()
    tag = soup.find('compTag')
    tag.attrs['value'] = compTag.lstrip()
    tag = soup.find('compAttr')
    tag.attrs['value'] = compAttr.lstrip()
    tag = soup.find('compValue')
    tag.attrs['value'] = compValue.lstrip()

    #fillSetInfo the set information
    handler = ProjectSQLiteHandler()
    dataTuple = handler.cursor.execute(
        "SELECT set_name, date_start, date_end, timestep, component_names from setup where set_name = '"
        + currentSet.lower() + "'").fetchone()

    tag = soup.find('setupTag')
    tag.attrs['value'] = "componentNames runTimeSteps timeStep"
    tag = soup.find('setupAttr')
    tag.attrs['value'] = "value value value"
    tag = soup.find('setupValue')
    df = compmodel.parent().window().findChild(QtWidgets.QWidget,
                                               'setupDialog').model.data.fixed
    tag.attrs['value'] = " ".join([
        dataTuple[4],
        timeStepsToInteger(dataTuple[1], dataTuple[2], df),
        str(dataTuple[3])
    ])

    return soup
Exemplo n.º 8
0
def replaceDefaultDatabase(projectdb):
    from UserInterface.ProjectSQLiteHandler import ProjectSQLiteHandler
    import pandas as pd
    tables = ['environment', 'components', 'sets', 'runs']
    for t in tables:
        h = ProjectSQLiteHandler(projectdb)
        # project data becomes a dataframe
        try:
            projectTable = pd.read_sql_query("select * from " + t,
                                             h.connection)
            h.closeDatabase()
            # the _id field is always the index for all tables
            projectTable.set_index(projectTable['_id'])
            projectTable = projectTable.drop('_id', 1)
            projectTable.index.names = ['_id']
            # connect to the active database and overwrite the table
            h = ProjectSQLiteHandler('project_manager')
            #data gets appended into empty tables created in default database
            projectTable.to_sql(t, h.connection, if_exists='append')
            h.closeDatabase()
        except:
            #TODO print some message to the console
            h.closeDatabase()
Exemplo n.º 9
0
    def updateValues(self):
        #find the data input grid
        myGrid = self.findChild(QtWidgets.QWidget, 'inputGrid')
        #get a soup of values that have changed
        newSoup, changes = update(myGrid)

        #TODO something with new soup and changes
        #should this be written to an xml file for optimizer input?
        #write changes to the database
        dbHandler = ProjectSQLiteHandler()
        for k in changes.keys():
            #if we return false upldate the existing parameter
            if not dbHandler.insertRecord('optimize_input',
                                          ['parameter', 'parameter_value'],
                                          [k, changes[k]]):
                dbHandler.updateRecord('optimize_input', ['parameter'], [k],
                                       ['parameter_value'], [changes[k]])
        return
Exemplo n.º 10
0
def updateSetsSql(set, setupModel):
    uihandler = UIToHandler()
    xmlfile =  setupModel.getSetAttributeXML(set)
    soup = uihandler.getSetAttributeXML(xmlfile)
    setupTags = soup.findChild('setupTag')['value'].split(' ')
    setupValue = soup.findChild('setupValue')['value'].split(' ')
    if setupValue[setupTags.index("runTimeSteps")].split(',') != 'all':
        start = integerToTimeIndex(setupModel.data.fixed, setupValue[setupTags.index("runTimeSteps")].split(',')[0])
        end = integerToTimeIndex(setupModel.data.fixed, setupValue[setupTags.index("runTimeSteps")].split(',')[1])
    else:
        start = ''
        end = ''
    timestep = setupValue[setupTags.index("timeStep")]
    components = setupValue[setupTags.index("componentNames")]
    updateTuple = (start, end, timestep, components,  set)
    # check if the setup information exists in the database
    sqlHandler = ProjectSQLiteHandler()
    dataTuple = sqlHandler.cursor.execute("select date_start, date_end, timestep, component_names, _id from setup where set_name = '" + set + "'").fetchone()
    # update setup table database columns with xml attribute information if it exists otherwise create a record
    if dataTuple is not None:
        #update

        sqlHandler.cursor.execute("UPDATE setup set date_start = ?, date_end = ?, timestep = ?,component_names = ? where set_name = ?", updateTuple)
    else:
        #insert
        sqlHandler.cursor.execute(
            "INSERT INTO setup (date_start, date_end, timestep, component_names, set_name) Values(?,?,?,?,?) ", updateTuple)

    # update the set table also
    compNames = soup.findChild('compName')['value'].split(' ')
    compTags = soup.findChild('compTag')['value'].split(' ')
    compAttrs = soup.findChild('compAttr')['value'].split(' ')
    compValues = soup.findChild('compValue')['value'].split(' ')
    for i,c in enumerate(compNames):
        dataTuple = (set,c,compTags[i],compValues[i])
        #this will result in a new row if a value has changed directly in the xml but not in the project database
        if len(sqlHandler.cursor.execute("SELECT * from sets where set_name = ? AND component = ? AND change_tag = ? AND to_value = ?", dataTuple).fetchall()) < 1:
            sqlHandler.cursor.execute("INSERT INTO sets (set_name, component, change_tag, to_value) VALUES (?,?,?,?)", dataTuple)

    sqlHandler.connection.commit()
    sqlHandler.closeDatabase()

    return
Exemplo n.º 11
0
    def createFileTab(self):
        self.dbhandler = ProjectSQLiteHandler()
        windowLayout = QtWidgets.QVBoxLayout()
        self.createTopBlock('Setup', self.assignFileBlock)
        l = self.FileBlock.findChild(QtWidgets.QWidget, 'inputFileDirvalue')
        l.clicked.connect(self.lineclicked)
        windowLayout.addWidget(self.FileBlock)

        self.createTableBlock('Components', 'components',
                              self.assignComponentBlock)

        windowLayout.addWidget(self.componentBlock)

        # the bottom block is disabled until a setup file is created or loaded
        self.createTableBlock('Environment Data', 'environment',
                              self.assignEnvironmentBlock)

        windowLayout.addWidget(self.environmentBlock)
        return windowLayout
Exemplo n.º 12
0
    def componentCellClicked(self):
        from UserInterface.DialogComponentList import ComponentSetListForm
        from UserInterface.ProjectSQLiteHandler import ProjectSQLiteHandler

        import pandas as pd
        handler = ProjectSQLiteHandler('project_manager')

        # get the cell, and open a listbox of possible components for this project
        checked = pd.read_sql_query("select component_name from components",
                                    handler.connection)

        checked = list(checked['component_name'])
        handler.closeDatabase()
        # checked is a comma seperated string but we need a list
        #checked = checked.split(',')
        listDialog = ComponentSetListForm(checked)
        components = listDialog.checkedItems()
        # format the list to be inserted into a text field in a datatable
        str1 = ','.join(components)
        widg = self.findChild(QtWidgets.QLineEdit, 'componentNames')
        widg.setText(str1)
        self.updateComponentDelegate(components)
Exemplo n.º 13
0
    def makeListWidget(self):
        import pandas as pd
        from UserInterface.ProjectSQLiteHandler import ProjectSQLiteHandler
        sqlhandler = ProjectSQLiteHandler('project_manager')
        self.components = pd.read_sql_query(
            "select component_name from components", sqlhandler.connection)

        self.components = list(self.components['component_name'])
        checked = [x in self.checked for x in self.components]
        sqlhandler.closeDatabase()
        listWidget = QtWidgets.QListWidget()
        for i in range(len(self.components)):
            item = QtWidgets.QListWidgetItem(self.components[i])
            item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
            if checked[i]:
                item.setCheckState(QtCore.Qt.Checked)
            else:
                item.setCheckState(QtCore.Qt.unchecked)
            listWidget.addItem(item)

        listWidget.itemClicked.connect(self.on_listWidget_itemClicked)
        return listWidget
Exemplo n.º 14
0
    def runSet(self):
        # currentSet
        currentSet = self.set
        #set info needs to be updated in the database
        setInfo = (currentSet, self.findChild(QtWidgets.QDateEdit,
                                              'startDate').text(),
                   self.findChild(QtWidgets.QDateEdit, 'endDate').text(),
                   self.findChild(QtWidgets.QLineEdit, 'timestep').text(),
                   self.findChild(QtWidgets.QLineEdit,
                                  'componentNames').text())
        sqlhandler = ProjectSQLiteHandler()
        try:
            sqlhandler.cursor.execute(
                "INSERT INTO setup(set_name, date_start, date_end, timestep, component_names) VALUES(?,?,?,?,?)",
                setInfo)
        except:
            sqlhandler.cursor.execute(
                "UPDATE setup set date_start = ?, date_end=?, timestep=?, component_names=? WHERE set_name = '"
                + setInfo[0] + "'", setInfo[1:])
        sqlhandler.connection.commit()
        sqlhandler.closeDatabase()
        uihandler = UIToHandler()

        # component table is the table associated with the button
        componentTable = self.findChild(SetTableView).model()
        if componentTable.rowCount() > 0:

            uihandler.runModels(
                currentSet, componentTable,
                self.window().findChild(QtWidgets.QWidget,
                                        'setupDialog').model)
        else:
            msg = QtWidgets.QMessageBox(
                QtWidgets.QMessageBox.Warning, "Add components",
                "You need to select component attributes to alter before running sets."
            )
            msg.setStandardButtons(QtWidgets.QMessageBox.Ok)
            msg.exec()
Exemplo n.º 15
0
 def getItems(self):
     dbHandler = ProjectSQLiteHandler()
     items = dbHandler.getCodes(self.d['reftable'])
     dbHandler.closeDatabase()
     return items
Exemplo n.º 16
0
    def initUI(self):
        self.dbHandler = ProjectSQLiteHandler()
        self.setObjectName("setupDialog")

        self.model = model

        #the main layout is oriented vertically
        windowLayout = QtWidgets.QVBoxLayout()

        # the top block is buttons to load setup xml and data files
        self.createButtonBlock()
        windowLayout.addWidget(self.ButtonBlock)
        self.tabs = Pages(self, '1', FileBlock)
        self.tabs.setDisabled(True)
        #each file type gets its own page to specify formats and headers to include
        # button to create a new set tab
        newTabButton = QtWidgets.QPushButton()
        newTabButton.setText(' + Input')
        newTabButton.setFixedWidth(100)
        newTabButton.clicked.connect(self.newTab)
        windowLayout.addWidget(newTabButton)

        windowLayout.addWidget(self.tabs, 3)

        #list of dictionaries containing information for wizard
        #this is the information that is not input file specific.

        dlist = [{
            'title': 'Dates to model',
            'prompt':
            'Enter the timespan you would like to include in the model.',
            'sqltable': None,
            'sqlfield': None,
            'reftable': None,
            'name': 'runTimesteps',
            'folder': False,
            'dates': True
        }, {
            'title': 'Timestep',
            'prompt': 'Enter desired timestep',
            'sqltable': None,
            'sqlfield': None,
            'reftable': 'ref_time_units',
            'name': 'timestep',
            'folder': False
        }, {
            'title': 'Project',
            'prompt': 'Enter the name of your project',
            'sqltable': None,
            'sqlfield': None,
            'reftable': None,
            'name': 'project',
            'folder': False
        }]

        self.WizardTree = self.buildWizardTree(dlist)
        self.createBottomButtonBlock()
        windowLayout.addWidget(self.BottomButtons)
        #set the main layout as the layout for the window

        self.setLayout(windowLayout)
        #title is setup
        self.setWindowTitle('Setup')
        self.setSizePolicy(QtWidgets.QSizePolicy.Expanding,
                           QtWidgets.QSizePolicy.Expanding)
        #show the form
        self.showMaximized()