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 ]
def options_sheet_to_df(options_sheet): options_df = ipysheet.to_dataframe(options_sheet) options_df.columns = options_df.iloc[0] options_df = options_df[1:] for col in ['C', 'K', 'P']: options_df[col] = pd.to_numeric(options_df[col]) options_df = options_df[options_df.K != 0] return options_df
def _sheet_to_df(sheet: sh.Sheet) -> pd.DataFrame: """ Transforms a ipysheet Sheet into a pandas DataFrame. :param sheet: the ipysheet Sheet to be converted :return the equivalent pandas DataFrame """ df = sh.to_dataframe(sheet) return df
def load_data(X): df = ipysheet.to_dataframe(X['sheet']) B = np.array(df['Field [mT]']) B = B[~np.isnan(B)] M = np.array(df['Magnetization']) M = M[~np.isnan(M)] X['B'] = B X['M'] = M return X
def button_clicked(self, _): # "linking function with output" with out: self.view.display_output( self.model.runningHeader, ipysheet.to_dataframe(self.model.sheets[self.view.menu.value]), ) # runs the model enriched_label, labelled_data = self.mlmodel.run( self.model.datas[self.view.menu.value][['content' ]].copy(deep=True), ipysheet.to_dataframe(self.model.sheets[self.view.menu.value])) with out: self.view.display_output( self.model.datas[self.view.menu.value], self.model.recommendationHeader, self.model.recommendations[self.view.menu.value], self.model.labelHeader, self.model.sheets[self.view.menu.value], self.model.enrichedHeader, enriched_label, self.model.labelledHeader, labelled_data) return
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()
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
def test_to_dataframe(): sheet = ipysheet.sheet(rows=5, columns=4) ipysheet.cell(0, 0, value=True) ipysheet.row(1, value=[2, 34, 543, 23]) ipysheet.column(3, value=[1.2, 1.3, 1.4, 1.5, 1.6]) df = ipysheet.to_dataframe(sheet) assert np.all(df['A'].tolist() == [True, 2, None, None, None]) assert np.all(df['B'].tolist() == [None, 34, None, None, None]) assert np.all(df['C'].tolist() == [None, 543, None, None, None]) assert np.all(df['D'].tolist() == [1.2, 1.3, 1.4, 1.5, 1.6]) sheet = ipysheet.sheet(rows=4, columns=4, column_headers=['c0', 'c1', 'c2', 'c3'], row_headers=['r0', 'r1', 'r2', 'r3']) ipysheet.cell_range([ [2, 34, 543, 23], [1, 1, 1, 1], [2, 2, 222, 22], [2, 0, 111, 11], ], row_start=0, column_start=0, transpose=True) df = ipysheet.to_dataframe(sheet) assert np.all(df['c0'].tolist() == [2, 34, 543, 23]) assert np.all(df['c1'].tolist() == [1, 1, 1, 1]) assert np.all(df['c2'].tolist() == [2, 2, 222, 22]) assert np.all(df['c3'].tolist() == [2, 0, 111, 11]) sheet = ipysheet.sheet(rows=4, columns=4, column_headers=['t0', 't1', 't2', 't3']) ipysheet.cell_range([ [2, 34, 543, 23], [1, 1, 1, 1], [2, 2, 222, 22], [2, 0, 111, 11], ], row_start=0, column_start=0, transpose=False) df = ipysheet.to_dataframe(sheet) assert np.all(df['t0'].tolist() == [2, 1, 2, 2]) assert np.all(df['t1'].tolist() == [34, 1, 2, 0]) assert np.all(df['t2'].tolist() == [543, 1, 222, 111]) assert np.all(df['t3'].tolist() == [23, 1, 22, 11]) sheet = ipysheet.sheet(rows=0, columns=0) df = ipysheet.to_dataframe(sheet) assert np.all(df == pd.DataFrame()) sheet = ipysheet.sheet(rows=4, columns=1) ipysheet.column(0, ['2019/02/28', '2019/02/27', '2019/02/26', '2019/02/25'], type='date') df = ipysheet.to_dataframe(sheet) assert [_format_date(x) for x in df['A'].tolist() ] == ['2019/02/28', '2019/02/27', '2019/02/26', '2019/02/25']