tee-ar-ex / tee-ar-ex/trx-python

Option to deflate TRX files created with Python

Open
#85 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
26
Forks
20
Avg merge
1d 14h
Merged PRs (30d)
9

Description

I find the tff_convert_tractogram.py tool useful, and it provides options for both --positions_dtype and --offsets_dtype. However, the TRX zip files are always uncompressed (store) rather than using compression (some level of deflate). It might be nice to trade off file creation speed with file size (and transmission time).

I did write a little Python script to do this, but it would be nice to integrate this. My standalone script allows you to recompress any zip file (including trx) with a desired compression level:

python zip2zip.py -c 9 IFOF_L32.trx IFOF_L32_z9.trx

The code is:

import zipfile
import argparse
import sys

def recompress_zip(input_zip: str, output_zip: str, compression_level: int):
    """Recompress a ZIP file with a specified compression level (0-9)."""
    compression_type = zipfile.ZIP_DEFLATED if compression_level > 0 else zipfile.ZIP_STORED

    with zipfile.ZipFile(input_zip, 'r') as src_zip:
        with zipfile.ZipFile(output_zip, 'w', compression=compression_type, compresslevel=compression_level) as dst_zip:
            for file_info in src_zip.infolist():
                with src_zip.open(file_info.filename, 'r') as src_file:
                    dst_zip.writestr(file_info, src_file.read(), compress_type=compression_type)
    
    print(f"Recompressed {input_zip} -> {output_zip} with compression level {compression_level}")

def main():
    parser = argparse.ArgumentParser(description="Recompress a ZIP file with a specified compression level.")
    parser.add_argument("input_zip", help="Path to the input ZIP file")
    parser.add_argument("output_zip", help="Path to the output compressed ZIP file")
    parser.add_argument("-c", "--compression", type=int, choices=range(0, 10), default=9, 
                        help="Compression level (0-9, where 0 is uncompressed and 9 is max compression, default: 9)")
    
    args = parser.parse_args()
    
    try:
        recompress_zip(args.input_zip, args.output_zip, args.compression)
    except Exception as e:
        print(f"Error: {e}", file=sys.stderr)
        sys.exit(1)

if __name__ == "__main__":
    main()

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in tff_convert_tractogram.py, using the existing --positions_dtype and --offsets_dtype options to locate where TRX ZIP files are created. Review Python's zipfile compression options and the issue's zip2zip.py example. Done means the tool can choose stored or deflate compression with a configurable level while preserving existing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.