Beispiel #1
0
def test_read_binary_sanity_check(dataset_master_simple):
    _, path = dataset_master_simple

    header_bytes, data_bytes = get_header_and_data_bytes(path)
    session_header = Header.from_bin_array(header_bytes[1:])

    sample_size = session_header.sample_size
    n_samples = session_header.n_samples

    data = read_binary_uint8(data_bytes, sample_size, n_samples)

    data = data[:, :-1]

    with pytest.raises(InvalidInputFileError):
        split_into_sensor_data(data, session_header)
Beispiel #2
0
def convert_18_0(in_path: path_t, out_path: path_t) -> None:
    """Convert a session recorded with a firmware version >0.13.255 and <0.17.255 to the most up-to-date format.

    This will update the firmware version to 0.17.255 to identify converted sessions.
    Potential version checks in the library will use <0.17.255 to check for compatibility.

    Parameters
    ----------
    in_path :
        path to 0.14.x - 0.17.x file
    out_path :
        path to converted 0.17.255 file

    """
    header, data_bytes = get_header_and_data_bytes(in_path)
    header, data_bytes = load_18_0(header, data_bytes)

    with open(out_path, "wb+") as f:
        f.write(bytearray(header))
        f.write(bytearray(data_bytes))
Beispiel #3
0
def convert_12_0(in_path: path_t, out_path: path_t) -> None:
    """Convert a session recorded with a firmware version >0.11.255 and <0.13.255 to the most up-to-date format.

    This will update the firmware version to 0.17.255 to identify converted sessions.
    Potential version checks in the library will use <0.17.255 to check for compatibility.

    .. warning:: After the update the following features will not work:
                    - The sync group was removed and hence can not be read anymore

    Parameters
    ----------
    in_path :
        path to 0.12.x / 0.13.x file
    out_path :
        path to converted 0.17.255 file

    """
    header, data_bytes = get_header_and_data_bytes(in_path)
    header, data_bytes = load_12_0(header, data_bytes)

    with open(out_path, "wb+") as f:
        f.write(bytearray(header))
        f.write(bytearray(data_bytes))
Beispiel #4
0
def simple_session_16_2():
    path = TEST_LEGACY_DATA_16_2 / "NilsPodX-6F13_20210109_121625.bin"
    header, data_bytes = get_header_and_data_bytes(path)
    return path, header, data_bytes
Beispiel #5
0
def simple_session_12_0():
    path = TEST_LEGACY_DATA_12 / "NilsPodX-7FAD_20190430_0933.bin"
    header, data_bytes = get_header_and_data_bytes(path)
    return path, header, data_bytes
Beispiel #6
0
def simple_session_11_2():
    path = TEST_LEGACY_DATA_11 / "NilsPodX-8433_20190412_172203.bin"
    header, data_bytes = get_header_and_data_bytes(path)
    return path, header, data_bytes
Beispiel #7
0
def parse_binary(
    path: path_t,
    legacy_support: str = "error",
    force_version: Optional[StrictVersion] = None,
    tz: Optional[str] = None
) -> Tuple[Dict[str, np.ndarray], np.ndarray, Header]:
    """Parse a binary NilsPod session file and read the header and the data.

    Parameters
    ----------
    path :
        Path to the file
    legacy_support :
        This indicates how to deal with old firmware versions.
        If `error`, An error is raised, if an unsupported version is detected.
        If `warn`, A warning is raised, but the file is parsed without modification
        If `resolve`, A legacy conversion is performed to load old files. If no suitable conversion is found,
        an error is raised. See the `legacy` package and the README to learn more about available conversions.
    force_version
        Instead of relying on the version provided in the session header, the legacy support will be determined based on
        the version provided here.
        This is only used, if `legacy_support="resolve"`.
        This option can be helpful, when testing with development firmware images that don't have official version
        numbers.
    tz
        Optional timezone str of the recording.
        This can be used to localize the start and end time.
        Note, this should not be the timezone of your current PC, but the timezone relevant for the specific
        recording.

    Returns
    -------
    sensor_data :
        The sensor data as dictionary
    counter :
        The counter values
    session_header :
        The session header

    Raises
    ------
    VersionError
        If unsupported FirmwareVersion is detected and `legacy_error` is `error`
    VersionError
        If `legacy_error` is `resolve`, but no suitable conversion is found.

    """
    header_bytes, data_bytes = get_header_and_data_bytes(path)

    version = get_strict_version_from_header_bytes(header_bytes)

    if legacy_support == "resolve":
        version = force_version or version
        header_bytes, data_bytes = find_conversion_function(
            version, in_memory=True)(header_bytes, data_bytes)
    elif legacy_support in ["error", "warn"]:
        legacy_support_check(version, as_warning=(legacy_support == "warn"))
    else:
        raise ValueError(
            "legacy_support must be one of 'resolve', 'error' or 'warn'")

    session_header = Header.from_bin_array(header_bytes[1:], tz=tz)

    sample_size = session_header.sample_size
    n_samples = session_header.n_samples

    data = read_binary_uint8(data_bytes, sample_size, n_samples)

    counter, sensor_data = split_into_sensor_data(data, session_header)

    if session_header.strict_version_firmware >= StrictVersion(
            "0.13.0") and len(counter) != session_header.n_samples:
        warnings.warn(
            "The number of samples in the dataset does not match the number indicated by the header."
            "This might indicate a corrupted file",
            LegacyWarning,
        )

    return sensor_data, counter, session_header