Skip to content

Commit

Permalink
Add ability to load *.csv files
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdulkadirA committed Mar 17, 2022
1 parent 101194e commit 66e0a51
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
16 changes: 10 additions & 6 deletions NiBAx/core/model/datamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@ def GetMUSEDictDataFrame(self):
def SetData(self,d):
"""Setter for data"""
self.data = d
logger.info('Data changed in datamodel')
logger.info('Data changed in datamodel. Signal emitted.')
self.data_changed.emit()


def SetHarmonizationModel(self,m):
"""Setter for neuroHarmonize model"""
self.harmonization_model = m
logger.info('neuroHarmonize model set.')


def SetSPAREModel(self,BrainAgeModel, ADModel):
Expand Down Expand Up @@ -148,15 +149,18 @@ def GetData(self,roi,hue):
return d


def IsValidData(self):
def IsValidData(self, data=None):
"""Checks if the data is valid or not."""
if not isinstance(self.data, pd.DataFrame):
if data is None:
data = self.data

if not isinstance(data, pd.DataFrame):
return False
if 'participant_id' not in self.data.columns:
if 'participant_id' not in data.columns:
return False
elif 'Age' not in self.data.columns:
elif 'Age' not in data.columns:
return False
elif 'Sex' not in self.data.columns:
elif 'Sex' not in data.columns:
return False
else:
return True
Expand Down
7 changes: 7 additions & 0 deletions NiBAx/plugins/loadsave/dataio.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ def __init__(self):


def ReadPickleFile(self,filename):
logger.info("Loading pickle file ...")
data = pd.read_pickle(filename)
return data


def ReadCSVFile(self,filename):
logger.info("Loading CSV file ...")
data = pd.read_csv(filename)
return data


def SavePickleFile(self,data,filename):
data.to_pickle(filename)

Expand Down
32 changes: 17 additions & 15 deletions NiBAx/plugins/loadsave/loadsave.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,14 @@ def OnSaveDataBtClicked(self):

def OnOpenDataFileBtnClicked(self):
filename = QtWidgets.QFileDialog.getOpenFileName(None,
'Open data file',
QtCore.QDir().homePath(),
"Pickle files (*.pkl.gz *.pkl)")
caption = 'Open data file',
directory = QtCore.QDir().homePath(),
filter = "Pickle/CSV files (*.pkl.gz *.pkl *.csv)")

if filename[0] == "":
print("No data was selected")
logger.warning("No file was selected")
else:
file = pd.read_pickle(filename[0])
if not (isinstance(file,pd.DataFrame)):
print('Selected file must be a dataframe.')
else:
self.ReadData(filename[0])
self.ReadData(filename[0])


def PopulateTable(self):
Expand All @@ -102,9 +98,12 @@ def OnDataChanged(self):
def ReadData(self,filename):
#read input data
dio = DataIO()
d = dio.ReadPickleFile(filename)

logger.info('New data read from file: %s', filename)
if filename.endswith('.pkl.gz') | filename[0].endswith('.pkl'):
d = dio.ReadPickleFile(filename)
elif filename.endswith('.csv'):
d = dio.ReadCSVFile(filename)
else:
d = None

#also read MUSE dictionary
MUSEDictNAMEtoID, MUSEDictIDtoNAME, MUSEDictDataFrame = dio.ReadMUSEDictionary()
Expand All @@ -115,6 +114,9 @@ def ReadData(self,filename):
self.datamodel.SetDerivedMUSEMap(DerivedMUSEMap)

#set data in model
self.datamodel.SetDataFilePath(filename)
self.datamodel.SetData(d)

if (d is not None) and self.datamodel.IsValidData(d):
logger.info('New data read from file: %s', filename)
self.datamodel.SetDataFilePath(filename)
self.datamodel.SetData(d)
else:
logger.warning('Loaded data was not valid.')

0 comments on commit 66e0a51

Please sign in to comment.