|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +"""Wrappers for bfconvert and bioformats2raw to be run from python environment. """ |
| 4 | + |
| 5 | +import subprocess, re, shlex |
| 6 | + |
| 7 | +intlist = lambda s: [int(x) for x in re.findall(r'\b\d+\b', s)] |
| 8 | + |
| 9 | +def get_bfconvert_cmd(*args, **kwargs): |
| 10 | + cmd = [] |
| 11 | + cmd += ["bfconvert"] |
| 12 | + keys = kwargs.keys() |
| 13 | + if "noflat" in keys: |
| 14 | + cmd += [" -noflat"] |
| 15 | + if "series" in keys: |
| 16 | + cmd += [" -series", ' %s' % kwargs['series']] |
| 17 | + if "timepoint" in keys: |
| 18 | + cmd += [" -timepoint", ' %s' % kwargs['timepoint']] |
| 19 | + if "channel" in keys: |
| 20 | + cmd += [" -channel", ' %s' % kwargs['channel']] |
| 21 | + if "z_slice" in keys: |
| 22 | + cmd += [" -z", ' %s' % kwargs['z_slice']] |
| 23 | + if "range" in keys: |
| 24 | + _range = intlist(kwargs['range']) |
| 25 | + if len(_range) != 2: |
| 26 | + raise TypeError('Range must have two integers specifying first and last indices of images.') |
| 27 | + else: |
| 28 | + cmd += [" -range"] |
| 29 | + for i in _range: |
| 30 | + cmd += [' %s' % i] |
| 31 | + if "autoscale" in keys: |
| 32 | + cmd += [" -autoscale"] |
| 33 | + if "crop" in keys: |
| 34 | + _crop = ''.join(kwargs['crop'][1:-1].split(' ')) |
| 35 | + cmd += [" -crop", ' %s' % _crop] |
| 36 | + if "compression" in keys: |
| 37 | + cmd += [" -compression", ' %s' % kwargs['compression']] |
| 38 | + if "resolution_scale" in keys: |
| 39 | + cmd += [" -pyramid-scale", ' %s' % kwargs['resolution_scale']] |
| 40 | + if "resolutions" in keys: |
| 41 | + cmd += [" -pyramid-resolutions", ' %s' % kwargs['resolutions']] |
| 42 | + # add here all params |
| 43 | + cmd.append(' %s' % args[0]) |
| 44 | + cmd.append(' %s' % args[1]) |
| 45 | + cmdstr = ''.join(cmd) |
| 46 | + return cmdstr |
| 47 | + |
| 48 | +def bfconvert(*args, shell = False, **kwargs): |
| 49 | + cmd = get_bfconvert_cmd(*args, **kwargs) |
| 50 | + if not shell: |
| 51 | + cmd = shlex.split(cmd) |
| 52 | + subprocess.run(cmd, shell = shell) |
| 53 | + return cmd |
| 54 | + |
| 55 | +def get_bf2raw_cmd(*args, **kwargs): |
| 56 | + cmd = [] |
| 57 | + cmd += ["bioformats2raw"] |
| 58 | + keys = kwargs.keys() |
| 59 | + if "resolutions" in keys: # The number of sub-resolutions created |
| 60 | + cmd += [" --resolutions", ' %s' % kwargs['resolutions']] |
| 61 | + if "min_image_size" in keys: # Will create sub-resolutions until the minimum image size is reached |
| 62 | + cmd += [" --target-min-size", ' %s' % kwargs['min_image_size']] |
| 63 | + if "chunk_h" in keys: |
| 64 | + cmd += [" --tile_height", ' %s' % kwargs['chunk_h']] |
| 65 | + if "chunk_w" in keys: |
| 66 | + cmd += [" --tile_width", ' %s' % kwargs['chunk_w']] |
| 67 | + if "chunk_d" in keys: |
| 68 | + cmd += [" --chunk_depth", ' %s' % kwargs['chunk_d']] |
| 69 | + if "downsample_type" in keys: |
| 70 | + cmd += [" --downsample-type", ' %s' % kwargs['downsample_type']] |
| 71 | + if "compression" in keys: |
| 72 | + cmd += [" --compression", ' %s' % kwargs['compression']] |
| 73 | + if "max_workers" in keys: |
| 74 | + cmd += [" --max_workers", ' %s' % kwargs['max_workers']] |
| 75 | + if "no_nested" in keys: |
| 76 | + cmd += [" --no-nested"] |
| 77 | + if "drop_series" in keys: |
| 78 | + cmd += [" --scale-format-string", ' %s' % "'%2$d'"] |
| 79 | + if "overwrite" in keys: |
| 80 | + cmd += [" --overwrite"] |
| 81 | + cmd.append(' %s' % args[0]) |
| 82 | + cmd.append(' %s' % args[1]) |
| 83 | + cmdstr = ''.join(cmd) |
| 84 | + return cmdstr |
| 85 | + |
| 86 | +def bioformats2raw(*args, shell = False, **kwargs): |
| 87 | + cmd = get_bf2raw_cmd(*args, **kwargs) |
| 88 | + if not shell: |
| 89 | + cmd = shlex.split(cmd) |
| 90 | + subprocess.run(cmd, shell = shell) |
| 91 | + return cmd |
| 92 | + |
0 commit comments