Skip to content

mef3io 2.4.0

Released 2026-11-18

This release adds remove_annotations, the first way to take a stored annotation back, and an optional Reader(open_files=) for viewers that page through a recording on a slow or network drive. mef3io.time_range also reads less. Nothing that worked in 2.3.0 changes unless you use the new calls, or a session is in the middle of an annotation edit (see Upgrading).

Highlights

  • mef3io.remove_annotations: take a stored annotation back (Python and C++). write_annotations is unchanged and still only appends; removal is a separate, explicitly named call. Name the records to remove by what Reader.records() returns (level, channel, segment, time, type and every body field) and, in the same call, optionally add= the corrected ones, so changing a note is one rewrite. See the Python guide.
notes = mef3io.Reader(path).records()
wrong = [n for n in notes if n["type"] == "Note" and n["text"] == "typo"]
mef3io.remove_annotations(path, remove=wrong,
                          add=[{"time": wrong[0]["time"], "text": "fixed"}])
  • A removed annotation is gone for good. There is no undo and no history; the old files are kept only until the swap is complete. Read the records first and keep a copy if you may need them back.
  • What is rewritten. Only the annotation files (.rdat + .ridx) of the levels named. Signal files and metadata are never opened for writing. Kept records are carried over byte for byte (including their encryption level); the headers are made right and stamped with the new provenance operation records-edit (code 10). When the last record of a level goes, its two files go (the state of a level that never had annotations).
  • What it refuses, and then removes nothing. A selector that matches nothing (no-match), more records than it expects (ambiguous: identical duplicates need expect= or all_copies=True), fewer (count-mismatch), leaves out a field the stored record has (selector-incomplete) or names a record twice; all levels of the call succeed or none does. Also refused: a .mefd.tar archive (tar-read-only), an annotation file that is torn or damaged (record-torn, record-damaged, ...: run recover_session first, nothing is ever trimmed silently), wrong passwords, a session that is being written or is mid-operation, and annotations that changed while the call worked (records-changed). Refusals about the session or the selection carry a stable .code; a wrongly typed or malformed argument (a fractional or non-numeric selector time, an unknown level, remove= given one dict) raises ValueError / TypeError as usual, without a code. Note text that is not valid UTF-8 cannot be selected.
  • Crash safe. The new files are written beside the old ones, flushed and read back, then swapped under the session's operation marker, which carries the plan. After a crash or power cut at any step the session is either as it was or carries the marker: opening it warns (SessionOperationWarning), writes are refused, the validator reports the new check records.interrupted-edit, and recover_session(path, apply=True) finishes the swap, or rolls it back when a new file is not intact (the report in RecoveryReport.record_edits then says the removal was NOT done, and python -m mef3io recover exits with status 1). The next remove_annotations settles it first. Leftovers that cannot be placed are moved to <session>.edit-leftovers, never deleted; a marker whose plan cannot be read is settled from the files alone, or refused (edit-unsettled, exit status 2) with nothing touched.
  • What a reader sees during an edit. While the marker is present every reader of annotations raises WriteConflictError with .code == "edit-interrupted" instead of returning a level that may be half swapped: Reader.records(), MultiReader.records(), MefReader.get_annotations(), the C API (so MATLAB's records) and C++ Session::read_records. An edit that is still running looks the same, so the message says "in progress or was interrupted". Samples stay readable. inspect still describes the session but reports each channel's records as None (null in JSON) with the warning annotation-edit-interrupted.
  • edit-in-progress and the override. recover_session(apply=True) and remove_annotations refuse with this code while the program that started the edit is still running on this machine (a zombie counts as dead), or while another thread of the same process is editing the session (running is set in a dry run); a dead, remote or unknown owner is settled. The owner is read from its own checked line at the top of the marker, so even an unreadable plan does not hide a running edit. A marker written by a newer mef3io is always refused (edit-newer-format, no override): settle it with a mef3io at least as new as the one that wrote it. If you are sure no program is editing (a reused process id, a zombie in a container), pass assume_owner_gone=True (recover_session, remove_annotations) or run python -m mef3io recover --owner-gone <session>; it skips only the live-owner refusal and the report records it (owner_override). If a program is in fact still editing, its edit can fail part-way or report success for a removal that was then rolled back, and a level's annotations can read as empty until the leftover .old files are put back by hand; nothing is deleted. The new validator warning records.edit-leftovers flags such .new / .old files beside record files when no marker is present.
  • What the marker holds. While an edit is in progress or interrupted, the marker holds the process id and the machine name of the editing program (and its start time where the system gives it); it is removed when the edit is settled. A session that carries it must be recovered before it can be copied, anonymized, merged or archived: every copy refuses it, so those names never reach a copy.
  • Password-protected sessions: pass both passwords as for Writer; added records are level-2 encrypted. Cancel and progress work until the swap starts.
  • Limits.
    • There is no cross-process lock. Stop other writers of the session first. write_annotations looks for the marker at its start, right before its append and once after; an edit that started during the append makes it raise (WriteConflictError, "may have been lost"), and an append that is already running when the marker appears and lands after the edit's last check of that file is lost.
    • Another machine's edit on a shared drive is not seen: the owner check works on one machine only (two machines with the same name look alike, and a marker written in a container counts as another machine), so on a network drive make sure no other machine is editing before you recover.
    • Where the system gives no process start time (macOS and Windows), a process id reused by an unrelated program after a crash can make recovery refuse falsely (edit-in-progress) until the override is used.
    • If the marker's owner line itself is torn or missing (a damaged file), the owner is unknown and recovery settles from the files: the one case on this machine in which recovery cannot protect a running edit.
    • Memory: the annotation files of the levels involved are held in memory roughly ten to twenty times over while the call works.
    • On Windows a record file another program holds open stops the swap (edit-failed; the old files are put back).
    • No MATLAB call, and no C API function to remove (like the copies and rename_session). MATLAB's recoverSession (the C API's recovery summary) settles an interrupted edit and now says how it ended: finished, rolled back ("the removal was NOT done"), settled from the files or not settled; a refusal arrives as the error message. It has no override argument: the way out from C and MATLAB is python -m mef3io recover --owner-gone <session> on the command line.
  • Optional: a Reader can keep data files open between calls (Reader(open_files=N), off by default: nothing changes unless you ask). Paging through a recording then no longer reopens the same .tdat for every read: up to N read-only files are kept, least recently used first, and read with positional reads. Each read first asks the file system for the file's size and identity, so an appended file is read to its new end and a file that was replaced, or deleted and recreated, is never served from the old open file. The samples are identical at every setting. Measured (local disk, request counts, option on): the 20 reads after the first of a 16-channel viewer window went from 400 opens + 400 closes to 96 opens + 0 closes; the saving on a given network drive depends on the drive and is not measured here. New: Reader.open_files (settable), Reader.release_files(), the same on MultiReader (ONE budget for all its recordings), and in C++ set_open_files / open_files / release_open_files on Session and Reader. A tar archive is read through one kept file. The C API and MATLAB have no such option and keep nothing open.
  • When to use it: one or a few Readers rereading the same files on a slow or network drive (a viewer, an analysis paging through days). Not for many Readers alive at once, or a fast local disk.
  • What it costs: descriptors held until release_files() / close() (if the process runs out, the library tries to recover by letting go of the kept files and retrying once; best effort, so keep it well under a quarter of the descriptor limit); on Windows a folder cannot be renamed or removed, nor a file replaced by renaming over it, while files are held (call release_files() first); and a single call that touches more files than open_files reopens them (raise open_files). What it cannot see: on Windows a replacement with the same creation time, last-write time and size, and on file systems with unstable inode numbers a reused number. See Paging through a recording on a network drive.
  • Not changed: the first read of a channel still reads the block index of every segment of that channel, because skipping a segment needs its index (a start or end time in the metadata can be stale).
  • New validator checks records.interrupted-edit (error, not repairable by repair_session: a session left in the middle of an annotation edit) and records.edit-leftovers (warning: temporary .new / .old files beside record files with no marker). See Validation.

Fixes

  • mef3io.time_range is faster. It looks only at the first and last complete segment of each channel (three existence checks, then two metadata reads) and no longer touches the other segments at all. Measured on 64 channels x 12 parts: 3272 -> 1352 system calls in total (-59%). The result is the same as before.
  • The copy checks, merge_sessions, transform_session and Reader open are unchanged: a cheaper way of asking which segment files exist (a folder listing) was tried and dropped because it made more system calls in total than asking for each file.
  • Regression tests for segment headers whose times are far from the data (the stale-header class fixed in 2.3.0).

Upgrading from 2.3.0

Python and C++ code need no change; nothing that worked is removed, and write_annotations behaves as before except in the cases below.

  • Reader(open_files=) is off by default. A Reader holds no file open between calls, exactly as in 2.3.0, until you pass it or set Reader.open_files.
  • During an interrupted or running annotation edit (only in a session that carries the marker of remove_annotations, which 2.3.0 never created):
  • Reader.records(), MultiReader.records() and MefReader.get_annotations() raise WriteConflictError (edit-interrupted). get_annotations used to print a warning and return None when a read failed; this one error is raised instead, so it cannot be mistaken for "no annotations". Code that expects None there should catch it.
  • Writer.write_annotations is refused while the marker is present, and can raise WriteConflictError after its append when an edit started during it (the records may have been lost; read them back and write them again).
  • inspect reports a channel's records as None (so ChannelSummary.records is int | None) and adds the warning annotation-edit-interrupted; the record tables are empty. Code that does arithmetic on the count should test for None.
  • recover_session examines the annotation files only after settling the edit; it gains the keyword assume_owner_gone (default off) and the CLI --owner-gone; RecoveryReport gains record_edits (with running and owner_override) and nothing_to_do accounts for it. copy-type calls (anonymize_session, transform_session, merge_sessions, archive_session) refuse a session that carries the marker (source-mid-operation). python -m mef3io recover exits with status 1 when a removal was rolled back. Opening such a session emits a SessionOperationWarning that names the edit; available_checks() lists one more check.
  • Provenance. Record files rewritten by remove_annotations carry operation records-edit (code 10; see the format reference).
  • New names. mef3io.remove_annotations, RemoveAnnotationsReport, RecordFileEdit and InterruptedRecordEdit are exported from mef3io.
  • MATLAB is unchanged apart from the edit-interrupted error its records raises while an edit is interrupted, and a recoverSession summary that says how an interrupted annotation edit ended.