Skip to main content

To convert snirf file to bids format.

Project description

snirf2bids pysnirf2 tests python

Table of Contents

snirf2BIDS

Conveniently generate BIDS structure from .snirf files.
Developed by BU BME Senior Design Group 3 (2022): Christian Arthur, Jeonghoon Choi, Jiazhen Liu, Juncheng Zhang.
Will be maintained by Boston University Neurophotonics Center(BUNPC).
snirf2BIDS requires Python >3 and h5py >3.6.

Features

Create BIDS-compliant 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-compliant Metadata Directory

def directory_export(self, fpath: str): creats BIDS-compliant metadata 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-compliant 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

Code Generation

The fields and descriptions in JSON files are generated based on the latest Brain Imaging Data Structure v1.7.1-dev and SNIRF specification.

Maintainers

@Christian Arthur :melon:
@Juncheng Zhang :tangerine:
@Jeonghoon Choi :pineapple:
@Jiazhen Liu :grapes:
Boston University Neurophotonics Center(BUNPC)

Contributors

This project exsists thanks to all people who contribute.

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.2.0.tar.gz (7.7 MB view hashes)

Uploaded Source

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