Esempio n. 1
0
    def toggleValue(self, event):

        df = self.nmrproblem.df
        patoms = self.nmrproblem.protonAtoms
        catoms = self.nmrproblem.carbonAtoms

        if ipysheet.to_dataframe(self.sheet1).shape == (0, 0):
            return

        if event.name == 'value':

            self.old = event.old
            self.new = event.new
            # update backend dataframe

            # i
            if event.old == 'All':
                df.loc[:, :] = ipysheet.to_dataframe(self.sheet1).loc[:, :]

            elif event.old == 'COSY':
                rr = ['ppm'] + patoms
                cc = ['ppm'] + patoms
                df.loc[rr, cc] = ipysheet.to_dataframe(self.sheet1).loc[rr, cc]

            elif event.old == 'HSQC-HMBC':
                rr = ['ppm'] + catoms
                cc = ['ppm'] + patoms
                df.loc[rr, cc] = ipysheet.to_dataframe(self.sheet1).loc[rr, cc]

            elif event.old == 'integrals-ppm':
                rr = ['ppm', 'integral']
                cc = patoms + catoms

                df.loc[rr, cc] = ipysheet.to_dataframe(self.sheet1).loc[rr, cc]
                df.loc[cc, 'ppm'] = df.loc['ppm', cc]

            # update sheet1 display of dataframe
            if event.new == 'All':
                self.sheet1 = ipysheet.from_dataframe(df)

            elif event.new == 'COSY':
                rr = ['ppm'] + patoms[::-1]
                cc = ['ppm'] + patoms
                self.sheet1 = ipysheet.from_dataframe(df.loc[rr, cc])

            elif event.new == 'HSQC-HMBC':
                rr = ['ppm'] + catoms[::-1]
                cc = ['ppm'] + patoms
                self.sheet1 = ipysheet.from_dataframe(df.loc[rr, cc])

            elif event.new == 'integrals-ppm':
                rr = ['ppm', 'integral']
                cc = patoms + catoms

                self.sheet1 = ipysheet.from_dataframe(df.loc[rr, cc])

            self.dfLayout.children = [
                self.toggleDF, self.dfWarningTextW, self.sheet1,
                self.dfButtonsLayout
            ]
Esempio n. 2
0
    def _df_to_sheet(df: pd.DataFrame) -> sh.Sheet:
        """
        Transforms a pandas DataFrame into a ipysheet Sheet.
        The cells are set to read only except for the values.

        :param df: the pandas DataFrame to be converted
        :return the equivalent ipysheet Sheet
        """
        if not df.empty:
            sheet = sh.from_dataframe(df)
            column = df.columns.get_loc("Value")

            for cell in sheet.cells:
                if column not in (cell.column_start, cell.column_end):
                    cell.read_only = True
                else:
                    cell.type = "numeric"
                    # TODO: make the number of decimals depend on the module ?
                    # or chosen in the ui by the user
                    cell.numeric_format = "0.000"

            # Name, Value, Unit, Description
            sheet.column_width = [150, 50, 20, 150]

        else:
            sheet = sh.sheet()
        return sheet
Esempio n. 3
0
def test_from_dataframe():
    df = pd.DataFrame({
        'A': 1.,
        'B': pd.Timestamp('20130102'),
        'C': pd.Series(1, index=list(range(4)), dtype='float32'),
        'D': np.array([False, True, False, False], dtype='bool'),
        'S': pd.Categorical(["test", "train", "test", "train"]),
        'T': 'foo',
        'X': np.array([0, 3, 9, 18])
    })

    df.loc[[0, 2], ['B']] = np.nan

    sheet = ipysheet.from_dataframe(df)
    assert len(sheet.cells) == 7
    assert sheet.cells[0].value == [1., 1., 1., 1.]
    assert sheet.cells[0].type == 'numeric'
    assert sheet.cells[1].value == [None, '2013/01/02', None, '2013/01/02']
    assert sheet.cells[1].type == 'date'
    assert sheet.cells[2].value == [1., 1., 1., 1.]
    assert sheet.cells[2].type == 'numeric'
    assert sheet.cells[2].numeric_format == '0.000'
    assert sheet.cells[3].value == [False, True, False, False]
    assert sheet.cells[3].type == 'checkbox'
    assert sheet.cells[4].value == ['test', 'train', 'test', 'train']
    assert sheet.cells[4].type == 'text'
    assert sheet.cells[5].value == ['foo', 'foo', 'foo', 'foo']
    assert sheet.cells[5].type == 'text'
    assert sheet.cells[6].value == [0, 3, 9, 18]
    assert sheet.cells[6].type == 'numeric'
    assert sheet.cells[6].numeric_format == '0[.]0'
Esempio n. 4
0
def test_from_dataframe():
    df = pd.DataFrame({
        'A': 1.,
        'B': pd.Timestamp('20130102'),
        'C': pd.Series(1, index=list(range(4)), dtype='float32'),
        'D': np.array([False, True, False, False], dtype='bool'),
        'S': pd.Categorical(["test", "train", "test", "train"]),
        'T': 'foo'
    })

    sheet = ipysheet.from_dataframe(df)
    assert len(sheet.cells) == 6
    assert sheet.column_headers == ['A', 'B', 'C', 'D', 'S', 'T']
    assert sheet.cells[0].value == [1., 1., 1., 1.]
    assert sheet.cells[0].type == 'numeric'
    assert sheet.cells[1].value == [
        '2013/01/02', '2013/01/02', '2013/01/02', '2013/01/02'
    ]
    assert sheet.cells[1].type == 'date'
    assert sheet.cells[2].value == [1., 1., 1., 1.]
    assert sheet.cells[2].type == 'numeric'
    assert sheet.cells[3].value == [False, True, False, False]
    assert sheet.cells[3].type == 'checkbox'
    assert sheet.cells[4].value == ['test', 'train', 'test', 'train']
    assert sheet.cells[4].type == 'text'
    assert sheet.cells[5].value == ['foo', 'foo', 'foo', 'foo']
    assert sheet.cells[5].type == 'text'
Esempio n. 5
0
    def on_upload_problemdir(self, change):

        files = change['new']
        file_names = list(files.keys())

        if len(file_names) == 0:
            return

        probdir_name, _ = file_names[0].split('.')
        print(probdir_name)

        with tempfile.TemporaryDirectory() as tmpdirname:

            print('created temporary directory', tmpdirname)
            problemDirectory = os.path.join(tmpdirname, probdir_name)
            os.mkdir(os.path.join(tmpdirname, probdir_name))

            for fname in files.keys():
                if "yml" in fname:
                    fp = open(os.path.join(problemDirectory, fname), 'wb')
                    fp.write(files[fname]["content"])
                    fp.close()

                elif "pkl" in fname:
                    fp = open(os.path.join(problemDirectory, fname), 'wb')
                    fp.write(files[fname]["content"])
                    fp.close()

            print(problemDirectory)
            self.nmrproblem = nmrProblem.NMRproblem(problemDirectory)

            if not isinstance(self.nmrproblem, type(None)):
                self.prDirW.value = self.nmrproblem.problemDirectoryPath

                # update other widgets based on contents of nmrproblem
                self.updateSpectraWidgetsFromNMRproblem()
                self.updateMoleculeWidgetsFromNMRproblem()

                # create a view of the dataframe in nmrproblem
                self.sheet1 = ipysheet.from_dataframe(self.nmrproblem.df)
                self.dfWarningTextW.value = "Table Messages: None"

                self.dfLayout.children = [
                    self.toggleDF, self.dfWarningTextW, self.sheet1,
                    self.dfButtonsLayout
                ]

                # create 1D 1H & C13 plots widget
                self.H1C131DplotsLayout.children = [
                    self.createH1C13interactivePlot(), self.saveProblemButtonW
                ]
Esempio n. 6
0
def test_from_to_dataframe():
    df = pd.DataFrame({
        'A': 1.,
        'B': pd.Timestamp('20130102'),
        'C': pd.Series(1, index=list(range(4)), dtype='float32'),
        'D': np.array([False, True, False, False], dtype='bool'),
        'S': pd.Categorical(["test", "train", "test", "train"]),
        'T': 'foo'
    })

    df.loc[[0, 2], ['B']] = np.nan

    sheet = ipysheet.from_dataframe(df)
    df2 = ipysheet.to_dataframe(sheet)

    a = np.array(df.values)
    b = np.array(df2.values)
    assert ((a == b) | (pd.isna(a) & pd.isna(b))).all()
Esempio n. 7
0
    def onButtonClicked(self, bttn):

        self.debugLabel.value = bttn.description

        #     print("bttn", bttn, type(bttn), bttn.description)
        if "Start New" in bttn.description:
            self.debugLabel.value = self.problemNameW.value

            # remove spaces from name
            probdirname = self.problemNameW.value.replace(" ", "")

            tmpdir = tempfile.TemporaryDirectory()
            os.mkdir(os.path.join(tmpdir.name, probdirname))
            self.nmrproblem = nmrProblem.NMRproblem(
                os.path.join(tmpdir.name, probdirname))

            # create a temporary problem directory
        elif "Directory" in bttn.description:
            self.nmrproblem = nmrProblem.NMRproblem.from_guidata()
            if not isinstance(self.nmrproblem, type(None)):
                self.prDirW.value = self.nmrproblem.problemDirectoryPath

                # update other widgets based on contents of nmrproblem
                self.updateSpectraWidgetsFromNMRproblem()
                self.updateMoleculeWidgetsFromNMRproblem()

                # create a view of the dataframe in nmrproblem
                self.sheet1 = ipysheet.from_dataframe(self.nmrproblem.df)
                self.dfWarningTextW.value = "Table Messages: None"

                #                 dfLayout.children = [dfWarningTextW, qgrid1, dfButtonsLayout]

                # create 1D 1H & C13 plots widget
                self.H1C131DplotsLayout.children = [
                    self.createH1C13interactivePlot()
                ]

        elif "update table" in bttn.description:
            print("update table")
            self.toggleDF.value = 'All'
            ok = self.nmrproblem.updateDFtable(
                ipysheet.to_dataframe(self.sheet1))

            if ok:
                self.nmrproblem.convertHSQCHMBCCOSYtoLists()
                self.nmrproblem.update_attachedprotons_c13hyb()
                self.sheet1 = ipysheet.from_dataframe(self.nmrproblem.df)
                self.dfWarningTextW.value = "Table Messages: None"
            else:
                self.dfWarningTextW.value = "Table Messages: problems in table, please check it"

            self.dfLayout.children = [
                self.toggleDF, self.dfWarningTextW, self.sheet1,
                self.dfButtonsLayout
            ]

        elif "update and run" in bttn.description:

            self.toggleDF.value = 'All'
            ok = self.nmrproblem.updateDFtable(
                ipysheet.to_dataframe(self.sheet1))

            if ok:
                self.sheet1 = ipysheet.from_dataframe(self.nmrproblem.df)
                self.dfWarningTextW.value = "Table Messages: None"

                self.nmrproblem.dfToNumbers()
                self.nmrproblem.convertHSQCHMBCCOSYtoLists()
                self.nmrproblem.convertJHzToLists()

                self.nmrproblem.update_attachedprotons_c13hyb()

                #                 H1df_orig, C13df_orig = nmrProblem.readinChemShiftTables()
                #                 H1df_orig, C13df_orig = read_in_cs_tables()

                self.nmrproblem.calcProbDistFunctions(H1df_orig, C13df_orig)

                self.nmrproblem.identify1HC13peaks()

                self.nmrproblem.udic[0]['df'] = self.nmrproblem.H1df
                self.nmrproblem.udic[1]['df'] = self.nmrproblem.C13df

                # self.nmrproblem.iprobs = iprobs

                self.nmrproblem.createInfoDataframes()

                self.nmrproblem.calculate1H13CSpectra1D()

                udic = self.nmrproblem.udic

                self.nmrproblem.save1DspecInfotoUdic()

                self.nmrproblem.create1H13Clabels(num_poss=3)

                self.dfLayout.children = [
                    self.toggleDF, self.dfWarningTextW, self.sheet1,
                    self.dfButtonsLayout
                ]

                # create 1D 1H & C13 plots widget

                self.H1C131DplotsLayout.children = [
                    self.createH1C13interactivePlot(), self.saveProblemButtonW
                ]
            else:
                self.dfWarningTextW.value = "Table Messages: problems in table, please check it"

            self.dfLayout.children = [
                self.toggleDF, self.dfWarningTextW, self.sheet1,
                self.dfButtonsLayout
            ]

        elif "Spectra" in bttn.description:
            self.nmrproblem.createInfoDataframes()
            self.nmrproblem.save1DspecInfotoUdic()

            self.updateSpectralInformationWidgetChanged()
            self.nmrproblem.calculate1H13CSpectra1D()
            self.H1C131DplotsLayout.children = [
                self.createH1C13interactivePlot(), self.saveProblemButtonW
            ]

        elif "Molecule" in bttn.description:
            print("Molecule")

            self.nmrproblem.update_molecule(self.moleculeAtomsW.value,
                                            self.pGrpsW.value,
                                            self.cGrpsW.value)

            # create a view of the dataframe in nmrproblem
            self.sheet1 = ipysheet.from_dataframe(self.nmrproblem.df)
            self.dfWarningTextW.value = "Table Messages: None"

            self.dfLayout.children = [
                self.toggleDF, self.dfWarningTextW, self.sheet1,
                self.dfButtonsLayout
            ]

        elif "Save Problem" in bttn.description:
            yml_dict = self.nmrproblem.save_problem()
            yml_str = yaml.dump(yml_dict, indent=4)
            self.ymlText.value = yml_str
Esempio n. 8
0
    def __init__(self, nmrproblem=None):

        super().__init__()

        if not isinstance(nmrproblem, nmrProblem.NMRproblem):
            self.nmrproblem = nmrproblem
            self.df = pd.DataFrame()
        else:
            self.nmrproblem = nmrproblem
            self.df = nmrproblem.df

        # create debug label widget for output
        self.debugLabel = widgets.Label(value="",
                                        layout=widgets.Layout(width="400px"))

        # create save problem widgets
        self.saveProblemButtonW = widgets.Button(description="Save Problem")

        # widgets to obtain problem working directory
        self.prDirW = widgets.Text(value='',
                                   placeholder='problem directory',
                                   description='problem directory',
                                   disabled=False)

        self.prDirB = widgets.Button(description='Set Directory')

        self.upload_problemdir = ipywidgets.widgets.FileUpload(
            multiple=True,
            description="Open Existing Problem ",
            description_tooltip="choose all files in problem directory",
            layout=widgets.Layout(width='300px'))

        self.problemNameL = widgets.Label(value="    Problem Name",
                                          layout=widgets.Layout(width='100px'))

        self.spacerL = widgets.Label(value="    ",
                                     layout=widgets.Layout(width='50px'))

        self.problemNameW = widgets.Text(value="Problem Name",
                                         description="",
                                         layout=widgets.Layout(width='150px'))

        self.newproblemB = widgets.Button(description="Start New Problem")

        self.prDirLayout = widgets.HBox([
            self.upload_problemdir, self.spacerL, self.problemNameL,
            self.problemNameW, self.spacerL, self.newproblemB
        ])

        # widgets to obtain info on the molecule
        # number and tye of atoms in molecule
        # number of proton resonances in molecule
        # number of carbon resonance in molecule
        self.moleculeAtomsW = widgets.Text(value='',
                                           placeholder='atoms in molecule',
                                           description='atoms',
                                           disabled=False)

        self.pGrpsW = widgets.IntText(value=1,
                                      placeholder='H1 groups in spectrum',
                                      description='H1 groups',
                                      disabled=False)

        self.cGrpsW = widgets.IntText(value=1,
                                      description='C13 groups',
                                      disabled=False)

        self.moleculesSubmitB = widgets.Button(description="Update Molecule")

        self.moleculeLayout = widgets.VBox([
            self.moleculeAtomsW, self.pGrpsW, self.cGrpsW,
            self.moleculesSubmitB
        ])

        # widgets to set 1D spectral parameters for proton and carbon
        self.pLabelW = widgets.Label("$^{1}H$")

        self.pSpecWidthW = widgets.FloatText(value=12.0,
                                             tooltip='proton spectral width',
                                             description='sw (ppm)',
                                             disabled=False)

        self.pObsFreqW = widgets.FloatText(value=400.0,
                                           description='obs (MHz)',
                                           disabled=False)

        self.pTofW = widgets.FloatText(value=5.0,
                                       description='tof (ppm)',
                                       diabled=False)

        self.pSizeW = widgets.IntText(value=32768,
                                      description='size (pts)',
                                      disabled=False)

        self.pLineBroadeningW = widgets.FloatText(value=0.5,
                                                  description='lb (Hz)',
                                                  disabled=False)

        self.cLabelW = widgets.Label("$^{13}C$")

        self.cSpecWidthW = widgets.FloatText(value=210.0,
                                             description='sw (ppm)',
                                             disabled=False)

        self.cObsFreqW = widgets.FloatText(value=100.0,
                                           description='obs (MHz)',
                                           disabled=False)

        self.cTofW = widgets.FloatText(value=5.0,
                                       description='tof (ppm)',
                                       diabled=False)

        self.cSizeW = widgets.IntText(value=32768,
                                      description='size (pts)',
                                      disabled=False)

        self.cLineBroadeningW = widgets.FloatText(value=0.5,
                                                  description='lb (Hz)',
                                                  disabled=False)

        self.specSubmitB = widgets.Button(description="Update Spectra")

        self.specLayout = widgets.HBox([
            widgets.VBox([
                self.pLabelW, self.pObsFreqW, self.pSpecWidthW, self.pTofW,
                self.pSizeW, self.pLineBroadeningW, self.specSubmitB
            ]),
            widgets.VBox([
                self.cLabelW, self.cObsFreqW, self.cSpecWidthW, self.cTofW,
                self.cSizeW, self.cLineBroadeningW
            ])
        ])

        self.old = 'All'
        self.new = 'ALL'

        self.toggleDF = widgets.ToggleButtons(
            options=['All', 'integrals-ppm', 'COSY', 'HSQC-HMBC'],
            description='Display:',
            disabled=False,
            button_style='',
            tooltips=[
                'Show full Dataframe', 'Show COSY Input',
                'Show HSQC/HMBC Input'
            ])

        self.sheet1 = ipysheet.from_dataframe(self.df)

        self.toggleDF.observe(self.toggleValue)

        self.dfWarningTextW = widgets.Label("Table Messages:  OK")
        self.dfUpdateTableB = widgets.Button(description="update table")
        self.dfRunAnalysisB = widgets.Button(description="update and run")

        self.dfButtonsLayout = widgets.HBox(
            [self.dfUpdateTableB, self.dfRunAnalysisB])

        self.dfLayout = widgets.VBox([
            self.toggleDF, self.dfWarningTextW, self.sheet1,
            self.dfButtonsLayout
        ])

        self.accordion = widgets.Accordion(children=[
            self.prDirLayout, self.moleculeLayout, self.specLayout,
            self.dfLayout
        ])

        self.accordion.set_title(0, "Problem Directory")
        self.accordion.set_title(1, "Molecule")
        self.accordion.set_title(2, "Spectroscopy")
        self.accordion.set_title(3, "DataSet")
        self.page1 = widgets.VBox(
            [self.accordion, self.saveProblemButtonW, self.debugLabel])

        self.H1C131DplotsLayout = widgets.VBox(
            [widgets.Output(), self.saveProblemButtonW])

        self.ymlTitle = widgets.HTML("yml description of problem")
        self.ymlText = widgets.Textarea(
            layout=widgets.Layout(width="400px", height="500px"))
        self.problemYML = widgets.VBox([self.ymlTitle, self.ymlText])

        self.children = [self.page1, self.H1C131DplotsLayout, self.problemYML]

        self.set_title(0, 'Problem Setup')
        self.set_title(1, 'Problem Plots')
        self.set_title(2, 'Problem YML')

        self.upload_problemdir.observe(
            lambda change: self.on_upload_problemdir(change), names='value')
        self.moleculesSubmitB.on_click(self.onButtonClicked)
        self.specSubmitB.on_click(self.onButtonClicked)
        self.dfUpdateTableB.on_click(self.onButtonClicked)
        self.dfRunAnalysisB.on_click(self.onButtonClicked)
        self.saveProblemButtonW.on_click(self.onButtonClicked)
        self.newproblemB.on_click(self.onButtonClicked)
Esempio n. 9
0
 def create_sheet(self, label_path: str):
     """ creates ipysheet from file path to csv file
     """
     df = pd.read_csv(label_path)
     sheet = ipysheet.from_dataframe(df)
     return sheet
Esempio n. 10
0
#       format_version: '1.4'
#       jupytext_version: 1.2.1
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np
from RiverNetwork import RiverNetwork
from ipysheet import from_dataframe
import ipysheet

from IPython.core.interactiveshell import InteractiveShell

InteractiveShell.ast_node_interactivity = "all"
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt

# %load_ext autoreload
# %autoreload 2

df = pd.read_excel('data/network-structure-1.xlsx')
df

sheet = from_dataframe(df)
display(sheet)

ipysheet.pandas_loader.to_dataframe(sheet)