Mercurial > repos > imgteam > points2binaryimage
comparison points2binaryimage.py @ 1:90385ec28b34 draft
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2binaryimage/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
| author | imgteam |
|---|---|
| date | Mon, 13 Nov 2023 22:11:42 +0000 |
| parents | dd513c9f5230 |
| children | 4c0f16fb9f8d |
comparison
equal
deleted
inserted
replaced
| 0:dd513c9f5230 | 1:90385ec28b34 |
|---|---|
| 1 import argparse | 1 import argparse |
| 2 import sys | |
| 3 import numpy as np | |
| 4 import skimage.io | |
| 5 import pandas as pd | |
| 6 import os | 2 import os |
| 7 import warnings | 3 import warnings |
| 4 | |
| 5 import numpy as np | |
| 6 import pandas as pd | |
| 7 import skimage.io | |
| 8 | 8 |
| 9 | 9 |
| 10 def points2binaryimage(point_file, out_file, shape=[500, 500], has_header=False, invert_xy=False): | 10 def points2binaryimage(point_file, out_file, shape=[500, 500], has_header=False, invert_xy=False): |
| 11 | 11 |
| 12 img = np.zeros(shape, dtype=np.int16) | 12 img = np.zeros(shape, dtype=np.int16) |
| 20 a_row = df.iloc[i] | 20 a_row = df.iloc[i] |
| 21 if int(a_row[0]) < 0 or int(a_row[1]) < 0: | 21 if int(a_row[0]) < 0 or int(a_row[1]) < 0: |
| 22 raise IndexError("Point {},{} is out of image with bounds {},{}.".format(int(a_row[0]), int(a_row[1]), shape[0], shape[1])) | 22 raise IndexError("Point {},{} is out of image with bounds {},{}.".format(int(a_row[0]), int(a_row[1]), shape[0], shape[1])) |
| 23 | 23 |
| 24 if invert_xy: | 24 if invert_xy: |
| 25 if img.shape[0]<=int(a_row[0]) or img.shape[1]<=int(a_row[1]): | 25 if img.shape[0] <= int(a_row[0]) or img.shape[1] <= int(a_row[1]): |
| 26 raise IndexError("Point {},{} is out of image with bounds {},{}.".format(int(a_row[0]), int(a_row[1]), shape[0], shape[1])) | 26 raise IndexError("Point {},{} is out of image with bounds {},{}.".format(int(a_row[0]), int(a_row[1]), shape[0], shape[1])) |
| 27 else: | 27 else: |
| 28 img[int(a_row[1]), int(a_row[0])] = 32767 | 28 img[int(a_row[1]), int(a_row[0])] = 32767 |
| 29 else: | 29 else: |
| 30 if img.shape[0]<=int(a_row[1]) or img.shape[1]<=int(a_row[0]): | 30 if img.shape[0] <= int(a_row[1]) or img.shape[1] <= int(a_row[0]): |
| 31 raise IndexError("Point {},{} is out of image with bounds {},{}.".format(int(a_row[1]), int(a_row[0]), shape[0], shape[1])) | 31 raise IndexError("Point {},{} is out of image with bounds {},{}.".format(int(a_row[1]), int(a_row[0]), shape[0], shape[1])) |
| 32 else: | 32 else: |
| 33 img[int(a_row[0]), int(a_row[1])] = 32767 | 33 img[int(a_row[0]), int(a_row[1])] = 32767 |
| 34 else: | 34 else: |
| 35 raise Exception("{} is empty or does not exist.".format(point_file)) # appropriate built-in error? | 35 raise Exception("{} is empty or does not exist.".format(point_file)) # appropriate built-in error? |
| 36 | 36 |
| 37 with warnings.catch_warnings(): | 37 with warnings.catch_warnings(): |
| 38 warnings.simplefilter("ignore") | 38 warnings.simplefilter("ignore") |
| 39 skimage.io.imsave(out_file, img, plugin='tifffile') # otherwise we get problems with the .dat extension | 39 skimage.io.imsave(out_file, img, plugin='tifffile') # otherwise we get problems with the .dat extension |
| 40 | |
| 40 | 41 |
| 41 if __name__ == "__main__": | 42 if __name__ == "__main__": |
| 42 parser = argparse.ArgumentParser() | 43 parser = argparse.ArgumentParser() |
| 43 parser.add_argument('point_file', type=argparse.FileType('r'), help='label file') | 44 parser.add_argument('point_file', type=argparse.FileType('r'), help='label file') |
| 44 parser.add_argument('out_file', type=str, help='out file (TIFF)') | 45 parser.add_argument('out_file', type=str, help='out file (TIFF)') |
| 47 parser.add_argument('--has_header', dest='has_header', default=False, help='set True if CSV has header') | 48 parser.add_argument('--has_header', dest='has_header', default=False, help='set True if CSV has header') |
| 48 parser.add_argument('--invert_xy', dest='invert_xy', default=False, help='invert x and y in CSV') | 49 parser.add_argument('--invert_xy', dest='invert_xy', default=False, help='invert x and y in CSV') |
| 49 | 50 |
| 50 args = parser.parse_args() | 51 args = parser.parse_args() |
| 51 | 52 |
| 52 #TOOL | 53 # TOOL |
| 53 points2binaryimage(args.point_file.name, args.out_file, [args.shapey, args.shapex], has_header=args.has_header, invert_xy=args.invert_xy) | 54 points2binaryimage(args.point_file.name, args.out_file, [args.shapey, args.shapex], has_header=args.has_header, invert_xy=args.invert_xy) |
