To convert snirf file to bids format.
Project description
Installation
pip install snirf2bids
Table of Contents
snirf2BIDS
Conveniently generate BIDS structures from SNIRF files.
snirf2BIDS requires Python >3 and h5py >3.6.
Features
Create BIDS Structures
def snirf2bids(inputpath: str, outputpath: str = None):
creates a BIDS structure from a SNIRF file.
inputpath
: The file path to the reference SNIRF file.
outputpath
: The file path/directory for the created BIDS metadata files.
def snirf2bids(inputpath: str, outputpath: str = None):
if os.path.isfile(inputpath):
subj = Subject(inputpath)
else:
return ValueError('Invalid directory to SNIRF file.')
if outputpath is None: # if output directory is not specified, it will be same as input
outputpath = os.path.dirname(inputpath)
if os.path.isdir(outputpath):
subjpath = 'sub' + str(subj.subinfo['sub-'])
outputpath = os.path.join(outputpath, subjpath)
if not os.path.exists(outputpath):
os.mkdir(outputpath)
if subj.subinfo['ses-'] is not None:
sespath = 'ses' + str(subj.subinfo['ses-'])
outputpath = os.path.join(outputpath, sespath)
if not os.path.exists(outputpath):
os.mkdir(outputpath)
outputpath = os.path.join(outputpath, 'nirs')
if not os.path.exists(outputpath):
os.mkdir(outputpath)
else:
return ValueError('Invalid directory to build BIDS folder.')
subj.directory_export(outputpath)
# re-create source snirf file
snirfoutput = os.path.join(outputpath,os.path.basename(inputpath))
if not os.path.isfile(snirfoutput):
subj.SNIRF.save(snirfoutput)
# This will probably work only with a single SNIRF file for now
fname = outputpath + '/participants.tsv'
with open(fname, 'w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=list(subj.participants.keys()), delimiter="\t", quotechar='"')
writer.writeheader()
writer.writerow(subj.participants)
f.close()
# scans.tsv output, same thing as participants for scans
fname = outputpath + '/scans.tsv'
with open(fname, 'w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=list(subj.scans.keys()), delimiter="\t", quotechar='"')
writer.writeheader()
writer.writerow(subj.scans)
f.close()
Create BIDS Metadata Directory
def directory_export(self, fpath: str):
creates BIDS-compliant metadata text files based on information stored in subject
class.
fpath
: The file path that points to the folder where we intend to save the metadata files in.
def directory_export(self, fpath: str):
self.coordsystem.save_to_json(self.subinfo, fpath)
self.optodes.save_to_tsv(self.subinfo, fpath)
self.optodes.export_sidecar(self.subinfo, fpath)
self.channels.save_to_tsv(self.subinfo, fpath)
self.channels.export_sidecar(self.subinfo, fpath)
self.sidecar.save_to_json(self.subinfo, fpath)
self.events.save_to_tsv(self.subinfo, fpath)
self.events.export_sidecar(self.subinfo, fpath)
Create BIDS Metadata Files in JSON Format
def json_export(self):
creats BIDS-compliant metadata files in JSON format. Returns a string containing the metadata file names and its content.
def json_export(self):
subj = self.subinfo
subjnames= self.pull_fnames()
# coordsystem.json
name = 'coordsystem'
temp = self.coordsystem.get_all_fields()
subj[subjnames[name]] = temp
# optodes.tsv + json sidecar
name = 'optodes'
fieldnames, valfiltered, temp = self.optodes.get_all_fields()
subj[subjnames[name]] = temp
sidecarname = _make_filename(name, subj, 'sidecar')
subj[sidecarname] = self.optodes._sidecar
# channels.tsv + json sidecar
name = 'channels'
fieldnames, valfiltered, temp = self.channels.get_all_fields()
subj[subjnames[name]] = temp
sidecarname = _make_filename(name, subj, 'sidecar')
subj[sidecarname] = self.channels._sidecar
# nirs sidecar
name = 'sidecar'
temp = self.sidecar.get_all_fields()
subj[subjnames[name]] = temp
# event.tsv + json sidecar
name = 'events'
fieldnames, valfiltered, temp = self.events.get_all_fields()
subj[subjnames[name]] = temp
sidecarname = _make_filename(name, subj, 'sidecar')
subj[sidecarname] = self.events._sidecar
# participant.tsv
fields = self.participants
text = _tsv_to_json(fields)
subj['participants.tsv'] = text
# scans.tsv
fields = self.scans
text = _tsv_to_json(fields)
subj['scans.tsv'] = text
text = json.dumps(subj)
return text
BIDS implementation
The fields and descriptions in JSON files are based on the latest Brain Imaging Data Structure v1.7.1-dev and SNIRF specification.
Contributors
Developed by BU BME Senior Design Group 3 (2022): Christian Arthur, Jeonghoon Choi, Jiazhen Liu, Juncheng Zhang and the Boston University Neurophotonics Center.
@Christian Arthur :melon:
@Juncheng Zhang :tangerine:
@Jeonghoon Choi :pineapple:
@Jiazhen Liu :grapes:
This project exists thanks to:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for snirf2bids-0.3.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3e935224b5a1bfc1e1ee4d519ecb313b66ba0afe97eaa50cb7eb46b20691969 |
|
MD5 | a257f739b79360bf8206fb54156da768 |
|
BLAKE2b-256 | 71cf99640c88e7c670020db0fbc62fcda807789a1ab09cf00e25f782d12bc599 |