Exemple #1
0
def testGetBidsRunInheritedEvents(tmpdir, validBidsI, sampleBidsEntities):
    # Add an events file on the top level with data
    rootPath = os.path.join(tmpdir, 'dataset')
    incrementalDFDict = {col: [4] for col in DEFAULT_EVENTS_HEADERS}
    incrementalDF = pd.DataFrame.from_dict(incrementalDFDict)
    validBidsI.events = correctEventsFileDatatypes(incrementalDF)

    validBidsI.writeToDisk(rootPath)
    archive = BidsArchive(rootPath)

    newDFDict = {col: [1, 2, 3] for col in DEFAULT_EVENTS_HEADERS}
    newDF = pd.DataFrame.from_dict(newDFDict)
    newDF = correctEventsFileDatatypes(newDF)
    writeDataFrameToEvents(
        newDF, '{dirPath}/task-{taskName}_events.tsv'.format(
            dirPath=rootPath, taskName=sampleBidsEntities['task']))
    archive._updateLayout()

    # Get the BIDS run
    run = archive.getBidsRun(**sampleBidsEntities)

    # Ensure that the BIDS run has all of the data added at the top level
    combinedDF = newDF.append(incrementalDF)
    combinedDF.sort_values(by='onset', inplace=True, ignore_index=True)
    assert combinedDF.equals(run._events)
Exemple #2
0
def testAppendBidsRun(tmpdir, bidsArchive4D, bidsArchiveMultipleRuns,
                      sampleBidsEntities):
    archivePath = Path(tmpdir, "appendBidsRunArchive")
    archive = BidsArchive(archivePath)
    emptyRun = BidsRun()
    archive.appendBidsRun(emptyRun)

    run = bidsArchive4D.getBidsRun(**sampleBidsEntities)
    archive.appendBidsRun(run)

    assert archive.getBidsRun(**sampleBidsEntities) == run
Exemple #3
0
class BidsStream:
    """
    A class that opens a BIDS archive and prepares to stream the data as
    BIDS incrementals.
    """
    def __init__(self, archivePath, **entities):
        """
        Args:
            archivePath: Absolute path of the BIDS archive.
            entities: BIDS entities (subject, session, task, run, suffix, datatype) that
                define the particular subject/run of the data to stream
        """
        self.bidsArchive = BidsArchive(archivePath)
        self.bidsRun = self.bidsArchive.getBidsRun(**entities)
        self.numVolumes = self.bidsRun.numIncrementals()
        self.nextVol = 0

    def getNumVolumes(self) -> int:
        """Return the number of brain volumes in the run"""
        return self.numVolumes

    def getIncremental(self, volIdx=-1) -> BidsIncremental:
        """
        Get a BIDS incremental for the indicated index in the current subject/run
        VolIdx acts similar to a file_seek pointer. If a volIdx >= 0 is supplied
            the volume pointer is advanced to that position. If no volIdx or
            a volIdx < 0 is supplied, then the next image volume after the previous
            position is returned and the pointer is incremented.
        Args:
            volIdx: The volume index (or TR) within the run to retrieve.
        Returns:
            BidsIncremental of that volume index within this subject/run
        """
        if volIdx >= 0:
            # reset the next volume to the user specified volume
            self.nextVol = volIdx
        else:
            # use the default next volume
            pass

        if self.nextVol < self.numVolumes:
            incremental = self.bidsRun.getIncremental(self.nextVol)
            self.nextVol += 1
            return incremental
        else:
            return None
Exemple #4
0
def openNeuroStreamTest(bidsInterface):
    dsAccessionNumber = 'ds002338'
    dsSubject = 'xp201'
    datasetDir = tmpDownloadOpenNeuro(dsAccessionNumber, dsSubject, 1)
    localEntities = {'subject': dsSubject, 'run': 1, 'suffix': 'bold', 'datatype': 'func'}
    remoteEntities = {'subject': dsSubject, 'run': 1}
    localBidsArchive = BidsArchive(datasetDir)
    streamId = bidsInterface.initOpenNeuroStream(dsAccessionNumber, **remoteEntities)
    for idx in range(3):
        streamIncremental = bidsInterface.getIncremental(streamId)
        localIncremental = localBidsArchive._getIncremental(idx, **localEntities)
        print(f"OpenNeuro stream check: image {idx}")
        assert streamIncremental == localIncremental

    for idx in [5, 2, 7]:
        streamIncremental = bidsInterface.getIncremental(streamId, volIdx=idx)
        localIncremental = localBidsArchive._getIncremental(idx, **localEntities)
        print(f"OpenNeuro stream check: image {idx}")
        assert streamIncremental == localIncremental

    # Resume without specifying volumes
    for idx in [*range(8, 10)]:
        streamIncremental = bidsInterface.getIncremental(streamId)
        localIncremental = localBidsArchive._getIncremental(idx, **localEntities)
        print(f"OpenNeuro stream check: image {idx}")
        assert streamIncremental == localIncremental

    numVols = bidsInterface.getNumVolumes(streamId)
    assert numVols > 0 and numVols < 1000

    # Check with local bidsRun
    localBidsRun = localBidsArchive.getBidsRun(**localEntities)
    assert numVols == localBidsRun.numIncrementals()
    assert numVols > 10
    for idx in [*range(6, 10)]:
        streamIncremental = bidsInterface.getIncremental(streamId, volIdx=idx)
        localIncremental = localBidsRun.getIncremental(idx)
        print(f"OpenNeuro bidsRun check: image {idx}")
        assert streamIncremental == localIncremental