Skip to content

Commit f978e6b

Browse files
committed
Minor tweaks
1 parent 833f75d commit f978e6b

File tree

6 files changed

+34
-28
lines changed

6 files changed

+34
-28
lines changed

bidscoin/bcoin.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,11 @@ def install_plugins(filenames: List[str]=()) -> None:
275275
continue
276276

277277
# Add the Options and data format section of the plugin to the default template bidsmap
278-
if 'OPTIONS' in dir(module) or 'BIDSMAP' in dir(module):
279-
if 'OPTIONS' in dir(module):
278+
if hasattr(module, 'OPTIONS') or hasattr(module, 'BIDSMAP'):
279+
if hasattr(module, 'OPTIONS'):
280280
LOGGER.info(f"Adding default {file.name} bidsmap options to the {bidsmap_template.stem} template")
281281
template['Options']['plugins'][file.stem] = module.OPTIONS
282-
if 'BIDSMAP' in dir(module):
282+
if hasattr(module, 'BIDSMAP'):
283283
for key, value in module.BIDSMAP.items():
284284
LOGGER.info(f"Adding default {key} bidsmappings to the {bidsmap_template.stem} template")
285285
template[key] = value
@@ -331,11 +331,11 @@ def uninstall_plugins(filenames: List[str]=(), wipe: bool=True) -> None:
331331
if not module:
332332
LOGGER.warning(f"Cannot remove any {file.stem} bidsmap options from the {bidsmap_template.stem} template")
333333
continue
334-
if 'OPTIONS' in dir(module) or 'BIDSMAP' in dir(module):
335-
if 'OPTIONS' in dir(module):
334+
if hasattr(module, 'OPTIONS') or hasattr(module, 'BIDSMAP'):
335+
if hasattr(module, 'OPTIONS'):
336336
LOGGER.info(f"Removing default {file.stem} bidsmap options from the {bidsmap_template.stem} template")
337337
template['Options']['plugins'].pop(file.stem, None)
338-
if wipe and 'BIDSMAP' in dir(module):
338+
if wipe and hasattr(module, 'BIDSMAP'):
339339
for key, value in module.BIDSMAP.items():
340340
LOGGER.info(f"Removing default {key} bidsmappings from the {bidsmap_template.stem} template")
341341
template.pop(key, None)
@@ -376,7 +376,7 @@ def import_plugin(plugin: Union[Path,str], functions: tuple=()) -> Union[types.M
376376

377377
functionsfound = []
378378
for function in functions:
379-
if function not in dir(module):
379+
if not hasattr(module, function):
380380
LOGGER.verbose(f"Could not find '{function}' in the '{plugin}' plugin")
381381
elif not callable(getattr(module, function)):
382382
LOGGER.error(f"'The {function}' attribute in the '{plugin}' plugin is not callable")
@@ -405,13 +405,13 @@ def test_plugin(plugin: Union[Path,str], options: dict) -> int:
405405

406406
LOGGER.info(f"--------- Testing the '{plugin}' plugin ---------")
407407

408-
# First test to see if we can import the plugin
408+
# First test to see if we can import the core plugin methods
409409
module = import_plugin(plugin, ('bidsmapper_plugin','bidscoiner_plugin'))
410410
if module is None:
411411
return 1
412412

413413
# Then run the plugin's own 'test' routine (if implemented)
414-
if 'test' in dir(module) and callable(getattr(module, 'test')):
414+
if hasattr(module, 'test') and callable(getattr(module, 'test')):
415415
try:
416416
returncode = module.test(options)
417417
if returncode == 0:

bidscoin/bids.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ def eventstable(self) -> pd.DataFrame:
122122

123123
# Loop over the row groups to filter/edit the rows
124124
rows = pd.Series([len(self.rows) == 0] * len(df)).astype(bool) # Series with True values if no row expressions were specified
125-
rows.index = df.index # Make sure the indices align
126125
for group in self.rows:
127126

128127
for column, regex in group['include'].items():

bidscoin/bidseditor.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ def add_plugin(self):
820820
LOGGER.error(f"Cannot add the '{plugin}' plugin as it already exists in the bidsmap")
821821
return
822822
module = bcoin.import_plugin(plugin)
823-
options = self.input_bidsmap.plugins.get(plugin, self.template_bidsmap.plugins.get(plugin, module.OPTIONS if 'OPTIONS' in dir(module) else {}))
823+
options = self.input_bidsmap.plugins.get(plugin, self.template_bidsmap.plugins.get(plugin, getattr(module, 'OPTIONS', {})))
824824

825825
# Insert the selected plugin in the options_layout
826826
LOGGER.info(f"Adding the '{plugin}' plugin to bidsmap")
@@ -1256,8 +1256,6 @@ def fill_table(self, table: MyQTable, data: list):
12561256
table.clearContents()
12571257
table.setRowCount(len(data + extrarow) - header)
12581258
table.setColumnCount(ncols)
1259-
table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents)
1260-
table.horizontalHeader().setSectionResizeMode(ncols - 1, QHeaderView.ResizeMode.Stretch)
12611259

12621260
for i, row in enumerate(data + extrarow):
12631261

@@ -1314,6 +1312,8 @@ def fill_table(self, table: MyQTable, data: list):
13141312
myitem.setToolTip(get_eventshelp(itemvalue))
13151313
table.setItem(i, j, myitem)
13161314

1315+
table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents)
1316+
table.horizontalHeader().setSectionResizeMode(ncols - 1, QHeaderView.ResizeMode.Stretch)
13171317
table.show()
13181318
table.blockSignals(False)
13191319

@@ -2267,7 +2267,7 @@ def bidseditor(bidsfolder: str, bidsmap: str='', template: str=bidsmap_template)
22672267

22682268
# Start the Qt-application
22692269
app = QApplication(sys.argv)
2270-
app.setApplicationName(f"{bidsmapfile} - BIDS editor {__version__}")
2270+
app.setApplicationName(f"{input_bidsmap.filepath} - BIDS editor {__version__}")
22712271
mainwin = MainWindow(bidsfolder, input_bidsmap, template_bidsmap, datasaved=True)
22722272
mainwin.show()
22732273
app.exec()

bidscoin/plugins/dcm2niix2bids.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test(options: Plugin=OPTIONS) -> int:
4646

4747
LOGGER.info('Testing the dcm2niix2bids installation:')
4848

49-
if 'readphysio' not in dir(physio) or 'physio2tsv' not in dir(physio):
49+
if not hasattr(physio, 'readphysio') or not hasattr(physio, 'physio2tsv'):
5050
LOGGER.error("Could not import the expected 'readphysio' and/or 'physio2tsv' from the physio.py utility")
5151
return 1
5252
if 'command' not in {**OPTIONS, **options}:

bidscoin/plugins/events2bids.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -242,28 +242,35 @@ def __init__(self, sourcefile: Path, _data: dict, options: dict):
242242
# Read the log-tables from the Presentation logfile
243243
self._sourcetable = pd.read_csv(self.sourcefile, sep='\t', skiprows=3, skip_blank_lines=True)
244244
"""The Presentation log-tables (https://www.neurobs.com/pres_docs/html/03_presentation/07_data_reporting/01_logfiles/index.html)"""
245+
self._columns = self._sourcetable.columns
246+
"""Store the original column names"""
245247

246248
@property
247249
def logtable(self) -> pd.DataFrame:
248250
"""Returns a Presentation log-table"""
249251

250-
nrows = len(self._sourcetable)
251-
stimulus_start = (self._sourcetable.iloc[:, 0] == 'Event Type').idxmax() or nrows
252-
video_start = (self._sourcetable.iloc[:, 0] == 'filename').idxmax() or nrows
253-
survey_start = (self._sourcetable.iloc[:, 0] == 'Time').idxmax() or nrows
252+
nrows = len(self._sourcetable)
253+
stimulus_header = (self._sourcetable.iloc[:, 0] == 'Event Type').idxmax() or nrows
254+
video_header = (self._sourcetable.iloc[:, 0] == 'filename').idxmax() or nrows
255+
survey_header = (self._sourcetable.iloc[:, 0] == 'Time').idxmax() or nrows
254256

255-
# Drop the stimulus, video and survey tables
257+
# Keep only the event, stimulus, video or survey table
258+
self._sourcetable.columns = self._columns
256259
if self.options['table'] == 'event':
257260
begin = 0
258-
end = min(stimulus_start, video_start, survey_start)
261+
end = min(stimulus_header, video_header, survey_header)
259262
elif self.options['table'] == 'stimulus':
260-
self._sourcetable.columns = self._sourcetable.iloc[stimulus_start]
261-
begin = stimulus_start + 1
262-
end = min(video_start, survey_start)
263+
self._sourcetable.columns = self._sourcetable.iloc[stimulus_header]
264+
begin = stimulus_header + 1
265+
end = min(video_header, survey_header)
263266
elif self.options['table'] == 'video':
264-
self._sourcetable.columns = self._sourcetable.iloc[video_start]
265-
begin = video_start + 1
266-
end = survey_start
267+
self._sourcetable.columns = self._sourcetable.iloc[video_header]
268+
begin = video_header + 1
269+
end = survey_header
270+
elif self.options['table'] == 'survey':
271+
self._sourcetable.columns = self._sourcetable.iloc[survey_header]
272+
begin = survey_header + 1
273+
end = nrows
267274
else:
268275
begin = 0
269276
end = nrows

bidscoin/plugins/spec2nii2bids.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test(options: Plugin=OPTIONS) -> int:
3333

3434
LOGGER.info('Testing the spec2nii2bids installation:')
3535

36-
if 'get_twixfield' not in dir(bids) or 'get_sparfield' not in dir(bids) or 'get_p7field' not in dir(bids):
36+
if not all(hasattr(bids, name) for name in ('get_twixfield', 'get_sparfield', 'get_p7field')):
3737
LOGGER.error("Could not import the expected 'get_twixfield', 'get_sparfield' and/or 'get_p7field' from the bids.py library")
3838
return 1
3939
if 'command' not in {**OPTIONS, **options}:

0 commit comments

Comments
 (0)