From 3a88a03328a4400aecbd50920430b9b1ab9cdf4c Mon Sep 17 00:00:00 2001
From: Christopher Rhodes <christopher.rhodes@embl.de>
Date: Thu, 28 Sep 2023 14:24:35 +0200
Subject: [PATCH] Basic support for bool data

---
 model_server/process.py | 52 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
 create mode 100644 model_server/process.py

diff --git a/model_server/process.py b/model_server/process.py
new file mode 100644
index 00000000..26fd3005
--- /dev/null
+++ b/model_server/process.py
@@ -0,0 +1,52 @@
+"""
+Image processing utility functions
+"""
+from math import ceil, floor
+
+import numpy as np
+from skimage.exposure import rescale_intensity
+
+
+def pad(im, mpx):  # now in model_server.batch
+    '''Pads and crops image width edge values to specified dimension'''
+    dh = 0.5 * (mpx - im.shape[0])
+    dw = 0.5 * (mpx - im.shape[1])
+
+    if dw < 0:
+        x0 = floor(-dw)
+        x1 = x0 + mpx
+        im = im[:, x0:x1]
+        dw = 0
+    if dh < 0:
+        y0 = floor(-dh)
+        y1 = y0 + mpx
+        im = im[y0:y1, :]
+        dh = 0
+
+    border = ((floor(dh), ceil(dh)), (floor(dw), ceil(dw)))
+    padded = np.pad(im, border, mode='constant')
+    if padded.shape != (mpx, mpx):
+        raise Exception(f'Incorrect image shape: {padded.shape} v. {(mpx, mpx)}')
+    return padded
+
+def pad_3d(im, mpx): # im: [z x h x w]
+    assert(len(im.shape) == 3)
+    nz, h, w = im.shape
+    padded = np.zeros((nz, mpx, mpx), dtype=im.dtype)
+    for zi in range(nz):
+        padded[zi, :, :] = pad(im[zi, :, :], mpx)
+    return padded
+
+def resample(nda, cmin=0, cmax=2**16): # now in model_server.batch
+    return rescale_intensity(
+        np.clip(nda, cmin, cmax),
+        in_range=(cmin, cmax + 1),
+        out_range=(0, 2**8)
+    ).astype('uint8')
+
+
+def rescale(nda, clip=0.0): # now in model_server.batch
+    clip_pct = (100.0 * clip, 100.0 * (1.0 - clip))
+    cmin, cmax = np.percentile(nda, clip_pct)
+    rescaled = rescale_intensity(nda, in_range=(cmin, cmax))
+    return rescaled
\ No newline at end of file
-- 
GitLab