mne_bids/read.py
def _handle_info_reading(sidecar_fname, raw):
    """Read associated sidecar JSON and populate raw.

    Handle PowerLineFrequency of recording.
    """
    with open(sidecar_fname, 'r', encoding='utf-8-sig') as fin:
        sidecar_json = json.load(fin)

    # read in the sidecar JSON's and raw object's line frequency
    json_linefreq = sidecar_json.get("PowerLineFrequency")
    raw_linefreq = raw.info["line_freq"]

    # If both are defined, warn if there is a conflict, else all is fine
    if (json_linefreq is not None) and (raw_linefreq is not None):
        if json_linefreq != raw_linefreq:

            msg = (
                f"Line frequency in sidecar JSON does not match the info "
                f"data structure of the mne.Raw object:\n"
                f"Sidecar JSON is -> {json_linefreq}\n"
                f"Raw is -> {raw_linefreq}\n\n")

            if json_linefreq == "n/a":
                msg += "Defaulting to the info from mne.Raw object."
                raw.info["line_freq"] = raw_linefreq
            else:
                msg += "Defaulting to the info from sidecar JSON."
                raw.info["line_freq"] = json_linefreq

            warn(msg)

    # Else, try to use JSON, fall back on mne.Raw
    elif (json_linefreq is not None) and (json_linefreq != "n/a"):
        raw.info["line_freq"] = json_linefreq
    else:
        pass  # line freq is either defined or None in mne.Raw

    # get cHPI info
    chpi = sidecar_json.get('ContinuousHeadLocalization')
    if chpi is None:
        # no cHPI info in the sidecar – leave raw.info unchanged
        pass
    elif chpi is True:
        from mne.io.ctf import RawCTF
        from mne.io.kit.kit import RawKIT

        msg = ('Cannot verify that the cHPI frequencies from '
               'the MEG JSON sidecar file correspond to the raw data{}')

        if isinstance(raw, RawCTF):
            # Pick channels corresponding to the cHPI positions
            hpi_picks = pick_channels_regexp(raw.info['ch_names'],
                                             'HLC00[123][123].*')
            if len(hpi_picks) != 9:
                raise ValueError(
                    f'Could not find all cHPI channels that we expected for '
                    f'CTF data. Expected: 9, found: {len(hpi_picks)}'
                )
            logger.info(msg.format(" for CTF files."))

        elif isinstance(raw, RawKIT):
            logger.info(msg.format(" for KIT files."))

        elif 'HeadCoilFrequency' in sidecar_json:
            hpi_freqs_json = sidecar_json['HeadCoilFrequency']
            try:
                hpi_freqs_raw, _, _ = mne.chpi.get_chpi_info(raw.info)
            except ValueError:
                logger.info(msg.format("."))
            else:
                # XXX: Set chpi info in mne.Raw to what is in the sidecar
                if not np.allclose(hpi_freqs_json, hpi_freqs_raw):
                    warn(
                        f'The cHPI coil frequencies in the sidecar file '
                        f'{sidecar_fname}:\n    {hpi_freqs_json}\n '
                        f'differ from what is stored in the raw data:\n'
                        f'    {hpi_freqs_raw}.\n'
                        f'Defaulting to the info from mne.Raw object.'
                    )
        else:
            addmsg = (".\n(Because no 'HeadCoilFrequency' data "
                      "was found in the sidecar.)")
            logger.info(msg.format(addmsg))

    else:
        if raw.info['hpi_subsystem']:
            logger.info('Dropping cHPI information stored in raw data, '
                        'following specification in sidecar file')
        with raw.info._unlock():
            raw.info['hpi_subsystem'] = None
            raw.info['hpi_meas'] = []

    return raw
Read associated sidecar JSON and populate raw.

    Handle PowerLineFrequency of recording.
    
Read associated sidecar JSON and populate raw.
Handle PowerLineFrequency of recording.
Read associated sidecar JSON and populate raw . Handle PowerLineFrequency of recording .
def _handle_info_reading(sidecar_fname, raw):
    with open(sidecar_fname, 'r', encoding='utf-8-sig') as fin:
        sidecar_json = json.load(fin)
    json_linefreq = sidecar_json.get("PowerLineFrequency")
    raw_linefreq = raw.info["line_freq"]
    if (json_linefreq is not None) and (raw_linefreq is not None):
        if json_linefreq != raw_linefreq:
            msg = (
                f"Line frequency in sidecar JSON does not match the info "
                f"data structure of the mne.Raw object:\n"
                f"Sidecar JSON is -> {json_linefreq}\n"
                f"Raw is -> {raw_linefreq}\n\n")
            if json_linefreq == "n/a":
                msg += "Defaulting to the info from mne.Raw object."
                raw.info["line_freq"] = raw_linefreq
            else:
                msg += "Defaulting to the info from sidecar JSON."
                raw.info["line_freq"] = json_linefreq
            warn(msg)
    elif (json_linefreq is not None) and (json_linefreq != "n/a"):
        raw.info["line_freq"] = json_linefreq
    else:
        pass  
    chpi = sidecar_json.get('ContinuousHeadLocalization')
    if chpi is None:
        pass
    elif chpi is True:
        from mne.io.ctf import RawCTF
        from mne.io.kit.kit import RawKIT
        msg = ('Cannot verify that the cHPI frequencies from '
               'the MEG JSON sidecar file correspond to the raw data{}')
        if isinstance(raw, RawCTF):
            hpi_picks = pick_channels_regexp(raw.info['ch_names'],
                                             'HLC00[123][123].*')
            if len(hpi_picks) != 9:
                raise ValueError(
                    f'Could not find all cHPI channels that we expected for '
                    f'CTF data. Expected: 9, found: {len(hpi_picks)}'
                )
            logger.info(msg.format(" for CTF files."))
        elif isinstance(raw, RawKIT):
            logger.info(msg.format(" for KIT files."))
        elif 'HeadCoilFrequency' in sidecar_json:
            hpi_freqs_json = sidecar_json['HeadCoilFrequency']
            try:
                hpi_freqs_raw, _, _ = mne.chpi.get_chpi_info(raw.info)
            except ValueError:
                logger.info(msg.format("."))
            else:
                if not np.allclose(hpi_freqs_json, hpi_freqs_raw):
                    warn(
                        f'The cHPI coil frequencies in the sidecar file '
                        f'{sidecar_fname}:\n    {hpi_freqs_json}\n '
                        f'differ from what is stored in the raw data:\n'
                        f'    {hpi_freqs_raw}.\n'
                        f'Defaulting to the info from mne.Raw object.'
                    )
        else:
            addmsg = (".\n(Because no 'HeadCoilFrequency' data "
                      "was found in the sidecar.)")
            logger.info(msg.format(addmsg))
    else:
        if raw.info['hpi_subsystem']:
            logger.info('Dropping cHPI information stored in raw data, '
                        'following specification in sidecar file')
        with raw.info._unlock():
            raw.info['hpi_subsystem'] = None
            raw.info['hpi_meas'] = []
    return raw
def _handle_info_reading ( sidecar_fname , raw ) : with open ( sidecar_fname , 'r' , encoding = 'utf-8-sig' ) as fin : sidecar_json = json . load ( fin ) json_linefreq = sidecar_json . get ( "PowerLineFrequency" ) raw_linefreq = raw . info [ "line_freq" ] if ( json_linefreq is not None ) and ( raw_linefreq is not None ) : if json_linefreq != raw_linefreq : msg = ( f"Line frequency in sidecar JSON does not match the info " f"data structure of the mne.Raw object:\n" f"Sidecar JSON is -> {json_linefreq}\n" f"Raw is -> {raw_linefreq}\n\n" ) if json_linefreq == "n/a" : msg += "Defaulting to the info from mne.Raw object." raw . info [ "line_freq" ] = raw_linefreq else : msg += "Defaulting to the info from sidecar JSON." raw . info [ "line_freq" ] = json_linefreq warn ( msg ) elif ( json_linefreq is not None ) and ( json_linefreq != "n/a" ) : raw . info [ "line_freq" ] = json_linefreq else : pass chpi = sidecar_json . get ( 'ContinuousHeadLocalization' ) if chpi is None : pass elif chpi is True : from mne . io . ctf import RawCTF from mne . io . kit . kit import RawKIT msg = ( 'Cannot verify that the cHPI frequencies from ' 'the MEG JSON sidecar file correspond to the raw data{}' ) if isinstance ( raw , RawCTF ) : hpi_picks = pick_channels_regexp ( raw . info [ 'ch_names' ] , 'HLC00[123][123].*' ) if len ( hpi_picks ) != 9 : raise ValueError ( f'Could not find all cHPI channels that we expected for ' f'CTF data. Expected: 9, found: {len(hpi_picks)}' ) logger . info ( msg . format ( " for CTF files." ) ) elif isinstance ( raw , RawKIT ) : logger . info ( msg . format ( " for KIT files." ) ) elif 'HeadCoilFrequency' in sidecar_json : hpi_freqs_json = sidecar_json [ 'HeadCoilFrequency' ] try : hpi_freqs_raw , _ , _ = mne . chpi . get_chpi_info ( raw . info ) except ValueError : logger . info ( msg . format ( "." ) ) else : if not np . allclose ( hpi_freqs_json , hpi_freqs_raw ) : warn ( f'The cHPI coil frequencies in the sidecar file ' f'{sidecar_fname}:\n    {hpi_freqs_json}\n ' f'differ from what is stored in the raw data:\n' f'    {hpi_freqs_raw}.\n' f'Defaulting to the info from mne.Raw object.' ) else : addmsg = ( ".\n(Because no 'HeadCoilFrequency' data " "was found in the sidecar.)" ) logger . info ( msg . format ( addmsg ) ) else : if raw . info [ 'hpi_subsystem' ] : logger . info ( 'Dropping cHPI information stored in raw data, ' 'following specification in sidecar file' ) with raw . info . _unlock ( ) : raw . info [ 'hpi_subsystem' ] = None raw . info [ 'hpi_meas' ] = [ ] return raw
Read associated sidecar JSON and populate raw.
Read associated sidecar JSON and populate raw .
"""Read associated sidecar JSON and populate raw.

    Handle PowerLineFrequency of recording.
    """ # read in the sidecar JSON's and raw object's line frequency # If both are defined, warn if there is a conflict, else all is fine # Else, try to use JSON, fall back on mne.Raw # line freq is either defined or None in mne.Raw # get cHPI info # no cHPI info in the sidecar – leave raw.info unchanged # Pick channels corresponding to the cHPI positions # XXX: Set chpi info in mne.Raw to what is in the sidecar
sidecar_fname
raw