Skip to main content

To convert snirf file to bids format.

Project description

snirf2bids tests python

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

snirf2bids-0.3.0.tar.gz (7.7 MB view hashes)

Uploaded Source

Built Distribution

snirf2bids-0.3.0-py2.py3-none-any.whl (7.7 MB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page