Metadata-Version: 2.4
Name: tdmslens
Version: 0.1.0
Summary: Inspect NI TDMS measurement files (LabVIEW, DAQmx) from the command line or any AI agent over MCP
Author: rodricksz4h5
License-Expression: MIT
Project-URL: Homepage, https://github.com/rodricksz4h5/tdmslens
Keywords: tdms,labview,national instruments,ni,daqmx,measurement,mcp,test and measurement
Classifier: Topic :: Scientific/Engineering
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: npTDMS<2,>=1.9
Requires-Dist: numpy>=1.22
Provides-Extra: mcp
Requires-Dist: mcp<3,>=2; extra == "mcp"
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == "dev"
Dynamic: license-file

# tdmslens

Inspect NI TDMS measurement files from the command line or from an AI agent over MCP.
TDMS is the format LabVIEW, NI-DAQmx, VeriStand and FlexLogger write test and DAQ data in.
tdmslens reads it without NI software and returns compact JSON: the file's layout, exact
statistics over every sample, and bounded slices of a channel. It can also write channels
to CSV.

A language model cannot read a 50 MB recording, and given a preview it will guess. tdmslens
streams the whole file and reports where things happen. For example, a strain channel with
a sensor stuck for half a second:

```console
$ tdmslens stats run42.tdms --group Rig --channel Strain --low -0.01 --high 0.01
```
```json
{
  "channel": "Strain", "count": 2000000, "nan": 0,
  "max": 0.25, "max_at": {"index": 1500000, "time_s": 150.0, "time": "2026-06-01T03:32:30.000000Z"},
  "mean": 0.000626, "std": 0.012524, "rms": 0.012540,
  "longest_constant_run": {"value": 0.25, "start": 1500000, "length": 5000, "time_s": 150.0},
  "range_check": {"above": 5000, "excursions": 1,
                  "excursions_listed": [{"start": 1500000, "length": 5000, "peak": 0.25, "time_s": 150.0}]}
}
```
(Output shortened and rounded.)

## Tools

| Tool | What it returns |
|---|---|
| `tdms_info` | File, group and channel properties; each channel's data type, length and unit; for waveform channels the sample rate, start time and duration. Reads no samples. Paged. |
| `tdms_stats` | Over every sample in a window. **Numeric:** count, NaN and infinity counts, min and max with the index and time where they occur, mean, population standard deviation, RMS, and the longest run of one repeated value (stuck or saturated sensor). With `low`/`high`: samples outside the range, the first and worst ones, and the excursions. **Timestamp:** order, backwards steps, nominal interval and gaps. **Boolean and string:** counts. |
| `tdms_read` | Samples of one channel, at most `max_points` (default 500, up to 5000). Longer windows are reduced to the min and max of each bucket, plus a `null` where the bucket has NaN, so spikes and dropouts survive (`minmax`), or to every k-th sample (`decimate`). |
| `tdms_export` | Channels of one group to a new `.csv`, `.tsv` or `.txt` file, with an index column and a `time_s` column when the channels share timing. |

Sample indices count from 0 within a channel. For waveform channels (`wf_increment`) every
index comes with `time_s` from the waveform start, and with an absolute UTC `time` when the
file stores `wf_start_time`. LabVIEW stores 1904-01-01 for relative-time waveforms; that is
treated as "no absolute time".

## Install

```console
pip install tdmslens            # CLI and Python API
pip install "tdmslens[mcp]"     # plus the MCP server
```

Python 3.10 or newer. Dependencies: [npTDMS](https://github.com/adamreeve/npTDMS)
(LGPL-3.0) and numpy. No NI software is needed.

### Claude Code, Cursor and other MCP clients

```console
claude mcp add tdmslens -- uvx --from "tdmslens[mcp]" tdmslens-mcp
```

Or in a client's JSON configuration:

```json
{"mcpServers": {"tdmslens": {"command": "uvx", "args": ["--from", "tdmslens[mcp]", "tdmslens-mcp"]}}}
```

The three reading tools are marked read-only; `tdms_export` is marked as writing.

### Hermes Agent

The `hermes/` folder is a Hermes plugin with the same four tools. It applies Hermes' read
denylist to every file it reads and Hermes' write denylist to every export.

### Python

```python
import tdmslens
tdmslens.info("run42.tdms")
tdmslens.stats("run42.tdms", group="Rig", low=-0.01, high=0.01)
tdmslens.read("run42.tdms", "Rig", "Pressure", max_points=1000)
tdmslens.export_csv("run42.tdms", "run42.csv", "Rig", step=10)
```

## What it handles

- Files from LabVIEW, the NI TDMS C library and DAQmx logging, TDMS 1.0 and 2.0, little and
  big endian, many segments, interleaved and defragmented data, and `.tdms_index` files.
- DAQmx raw data with NI scaling (`NI_Scale[n]_*`): values come out scaled, and the channel
  is marked `daqmx_scaled`.
- Recordings cut off by a crash or power loss: the samples present are read and the file is
  marked `incomplete_final_segment`. NI's own TDMS library refuses such a file.
- All TDMS data types: integers up to 64 bits, single and double floats, complex, booleans,
  strings and timestamps. Complex channels are counted but not summarised.

## Limits

Every result is size-capped: 100 channels per info page (up to 500), 60 properties per
object, 300 characters per string, 5000 points per read, 20 listed gaps or excursions per
channel, 200 channels per stats call. `tdms_stats` stops after 120 seconds and says which
channel and index it reached; `tdms_export` stops at `max_rows` (default 1,000,000) or after
600 seconds and says so. An export is written to a temporary file in the destination folder
and moved into place when complete; it never replaces a file unless `overwrite` is true, and
never the TDMS file it reads. NaN and infinite values appear as `null` in JSON (counted under
`nan` and `inf`). In a reduced `tdms_read`, a bucket containing NaN keeps a `null` at its
first NaN, so dropouts stay visible. Empty-string properties are left out of `tdms_info`,
and one `tdms_info` result returns at most about 200,000 characters of property text; past
that, properties are counted, not shown. Exported files are created with the user's normal
file permissions.

## How it is tested

- Unit tests compare every statistic with numpy over random data, read in blocks as small as
  one sample, so every chunk-boundary case is exercised.
- npTDMS's real example recordings: DAQmx raw data with scaling, DAQmx digital lines,
  big-endian, raw timestamps.
- `bench/ni/` writes a 56 MB corpus with NI's own TDMS library (four 2-million-sample
  channels with a spike, a NaN dropout and a stuck value, a timestamp channel with gaps, a
  defragmented copy and a crash-truncated copy) and checks tdmslens against NI's reader:
  every count, extreme, position, mean, standard deviation, range count and exported value
  matches.
- The MCP server is tested over stdio with the MCP client SDK, and the Hermes plugin passes
  `hermes plugins validate`.

## License

MIT. npTDMS, which tdmslens depends on, is LGPL-3.0.
