Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to Python 3 #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion segmentation-of-nuclei/color_norm/color_normalize.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import stainNorm_Reinhard
from . import stainNorm_Reinhard
from scipy import misc

class reinhard_normalizer():
Expand Down
4 changes: 2 additions & 2 deletions segmentation-of-nuclei/color_norm/stainNorm_Reinhard.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import division


import cv2 as cv
import numpy as np
import stain_utils as ut
from . import stain_utils as ut


### Some functions ###
Expand Down
2 changes: 1 addition & 1 deletion segmentation-of-nuclei/color_norm/stain_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Use with python via e.g https://anaconda.org/conda-forge/python-spams
"""

from __future__ import division


import numpy as np

Expand Down
2 changes: 1 addition & 1 deletion segmentation-of-nuclei/convert_polygon_to_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def process_poly(poly_path):
path_mkdir(path.join(cancer_type + '_mask', slide))
mask.save(output_path)
except:
print 'exception for', poly_path
print('exception for', poly_path)

poly_paths = glob('*_polygon/*/*-features.csv')

Expand Down
6 changes: 3 additions & 3 deletions segmentation-of-nuclei/data/nuclei_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import numpy as np

from utils import imread, imwrite
from .utils import imread, imwrite

DATA_FNAME = 'real_pair.npz'

Expand Down Expand Up @@ -56,7 +56,7 @@ def maybe_preprocess(config, data_path, sample_path=None):

def load(config, data_path, sample_path, rng):
if not os.path.exists(data_path):
print('creating folder', data_path)
print(('creating folder', data_path))
os.makedirs(data_path)

synthetic_image_path = maybe_preprocess(config, data_path, sample_path)
Expand All @@ -69,7 +69,7 @@ def load(config, data_path, sample_path, rng):
if not os.path.exists(sample_path):
os.makedirs(sample_path)

print("[*] Save samples images in {}".format(data_path))
print(("[*] Save samples images in {}".format(data_path)))
random_idxs = rng.choice(len(real_data), 100)
for idx, random_idx in enumerate(random_idxs):
image_path = os.path.join(sample_path, "real_{}.png".format(idx))
Expand Down
4 changes: 2 additions & 2 deletions segmentation-of-nuclei/data/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def img_tile(imgs, aspect_ratio=1.0, tile_shape=None, border=1,
def save_config(model_dir, config):
param_path = os.path.join(model_dir, "params.json")

print("[*] MODEL dir: %s" % model_dir)
print("[*] PARAM path: %s" % param_path)
print(("[*] MODEL dir: %s" % model_dir))
print(("[*] PARAM path: %s" % param_path))

with open(param_path, 'w') as fp:
json.dump(config.__dict__, fp, indent=4, sort_keys=True)
Expand Down
20 changes: 10 additions & 10 deletions segmentation-of-nuclei/extract_patch_segmentation_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ def patch_xy2mask(patch_xy, scale_to_40X=True):

def extract_segmentation_mask(
segmentation_polygon_folder, x, y, patch_width, scale_to_40X=True):
'''
Extract segmentation mask
'''
Extract segmentation mask

Args:
segmentation_polygon_folder: path to a segmentation output folder.
x: x coordinate of the patch you want to extract.
y: y coordinate of the patch.
patch_width: size of the patch.
Returns:
Instance-level mask as numpy array.
'''
Args:
segmentation_polygon_folder: path to a segmentation output folder.
x: x coordinate of the patch you want to extract.
y: y coordinate of the patch.
patch_width: size of the patch.
Returns:
Instance-level mask as numpy array.
'''

patch_xy = info2patch_xy(
segmentation_polygon_folder, x, y, patch_width)
Expand Down
2 changes: 1 addition & 1 deletion segmentation-of-nuclei/fix_old_data/one_pass_fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def fix_probability_map_png(in_folder):
im.save(fn)

if len(sys.argv) != 2:
print('Usage: python {} segmentation_folder'.format(sys.argv[0]))
print(('Usage: python {} segmentation_folder'.format(sys.argv[0])))
exit(1)
in_folder = sys.argv[1]
fix_polygon_csv(in_folder)
Expand Down
4 changes: 2 additions & 2 deletions segmentation-of-nuclei/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ def resnet_block(
inputs.get_shape()[3] == layer.get_shape()[3]:
outputs = tf.add(inputs, layer)
else:
print "Cannot tf.add: shapes do not match {}-{} {}-{} {}-{}".format(
print("Cannot tf.add: shapes do not match {}-{} {}-{} {}-{}".format(
inputs.get_shape()[1], layer.get_shape()[1],
inputs.get_shape()[2], layer.get_shape()[2],
inputs.get_shape()[3], layer.get_shape()[3]);
inputs.get_shape()[3], layer.get_shape()[3]));
outputs = layer;

_update_dict(layer_dict, scope, outputs)
Expand Down
2 changes: 1 addition & 1 deletion segmentation-of-nuclei/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def run(sess, feed_dict, fetch,
fetch['output'] = output_op

result = sess.run(fetch, feed_dict=feed_dict)
if result.has_key('summary'):
if 'summary' in result:
summary_writer.add_summary(result['summary'], result['step'])
summary_writer.flush()
return result
Expand Down
4 changes: 2 additions & 2 deletions segmentation-of-nuclei/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def touch(fname, times=None):
time.sleep(1)
else:
touch(os.path.join(folder, 'finish'))
print 'Segmentation finished under {}'.format(folder)
print('Segmentation finished under {}'.format(folder))


class MultiProcWatershed:
Expand All @@ -51,7 +51,7 @@ def __init__(self, n_proc):

def start(self):
self.workers = [Process(target=queue_waiter, args=(i, self.queue,))
for i in xrange(self.n_proc)]
for i in range(self.n_proc)]
for w in self.workers:
w.start()

Expand Down
4 changes: 2 additions & 2 deletions segmentation-of-nuclei/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def stain_normalized_tiling(slide_name, patch_size, do_actually_read_image=True)
width = oslide.dimensions[0]
height = oslide.dimensions[1]
except:
print 'Error in {}: exception caught exiting'.format(slide_name)
print('Error in {}: exception caught exiting'.format(slide_name))
raise Exception('{}: exception caught exiting'.format(slide_name))
return

Expand All @@ -65,7 +65,7 @@ def stain_normalized_tiling(slide_name, patch_size, do_actually_read_image=True)
try:
patch = oslide.read_region((x, y), 0, (pw_x, pw_y)).convert('RGB')
except:
print '{}: exception caught'.format(slide_name)
print('{}: exception caught'.format(slide_name))
continue
else:
patch = Image.new('RGB', (pw_x, pw_y), (255, 255, 255))
Expand Down
23 changes: 12 additions & 11 deletions segmentation-of-nuclei/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from utils import imwrite, imread, img_tile, synthetic_to_refer_paths
from preprocess import stain_normalized_tiling
from postprocess import MultiProcWatershed
from functools import reduce

class Trainer(object):
def __init__(self, config, rng):
Expand Down Expand Up @@ -169,7 +170,7 @@ def train_refiner(push_buffer=False):

def train_discrim():
a, b, c = self.history_buffer.sample()
d, e = self.data_loader.next()
d, e = next(self.data_loader)

feed_dict = {
self.model.synthetic_batch_size: self.data_loader.batch_size/2,
Expand Down Expand Up @@ -203,21 +204,21 @@ def train_learner():
train_discrim()

for step in trange(self.max_step_d_g, desc="Train refiner+discrim"):
for k in xrange(self.K_g):
for k in range(self.K_g):
train_refiner(push_buffer=True)

for k in xrange(self.K_d):
for k in range(self.K_d):
train_discrim()

for k in trange(self.initial_K_l, desc="Train learner"):
train_learner()

for step in trange(self.max_step_d_g_l, desc="Train all Three"):
for k in xrange(self.K_g):
for k in range(self.K_g):
train_refiner(push_buffer=True)
for k in xrange(self.K_l):
for k in range(self.K_l):
train_learner()
for k in xrange(self.K_d):
for k in range(self.K_d):
train_discrim()

for k in trange(self.after_K_l, desc="Train learner"):
Expand All @@ -238,8 +239,8 @@ def get_image_id(svs_path, delimitor='.'):
try:
self.cnn_pred_mask(wsi, image_id)
except Exception as e:
print traceback.format_exc()
print 'Segmentation failed for {}'.format(wsi)
print(traceback.format_exc())
print('Segmentation failed for {}'.format(wsi))
continue

if self.do_cpu_postprocess:
Expand Down Expand Up @@ -270,15 +271,15 @@ def cnn_pred_mask(self, wsi_path, image_id):

# Check if skip the CNN step
if self.do_gpu_process:
print "CNN segmentation on", outf
print("CNN segmentation on", outf)

pred_m = np.zeros((patch.shape[0], patch.shape[1], 3), dtype=np.float32);
num_m = np.zeros((patch.shape[0], patch.shape[1], 1), dtype=np.float32) + 4e-6;

net_inputs = [];
xy_indices = [];
for x in range(0, pred_m.shape[0]-PS+1, step_size) + [pred_m.shape[0]-PS,]:
for y in range(0, pred_m.shape[1]-PS+1, step_size) + [pred_m.shape[1]-PS,]:
for x in list(range(0, pred_m.shape[0]-PS+1, step_size)) + [pred_m.shape[0]-PS,]:
for y in list(range(0, pred_m.shape[1]-PS+1, step_size)) + [pred_m.shape[1]-PS,]:
pat = patch[x:x+PS, y:y+PS, :]
wh = pat[...,0].std() + pat[...,1].std() + pat[...,2].std();
if wh >= 0.18:
Expand Down
4 changes: 2 additions & 2 deletions segmentation-of-nuclei/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def img_tile(imgs, aspect_ratio=1.0, tile_shape=None, border=1,
def save_config(model_dir, config):
param_path = os.path.join(model_dir, "params.json")

print("[*] MODEL dir: %s" % model_dir)
print("[*] PARAM path: %s" % param_path)
print(("[*] MODEL dir: %s" % model_dir))
print(("[*] PARAM path: %s" % param_path))

with open(param_path, 'w') as fp:
json.dump(config.__dict__, fp, indent=4, sort_keys=True)
Expand Down
8 changes: 4 additions & 4 deletions segmentation-of-nuclei/watershed/seg_with_watershed.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from skimage.color import label2rgb
import time

import detection_binarize
from gen_json import gen_meta_json
from . import detection_binarize
from .gen_json import gen_meta_json

def apply_segmentation(in_path, image_id, wsi_width, wsi_height, method_description,
seg_thres=0.33, det_thres=0.07, win_size=200, min_nucleus_size=20, max_nucleus_size=65536):
Expand Down Expand Up @@ -70,7 +70,7 @@ def read_instance(im_file, resize_factor):

return det_seg[..., 0], det_seg[..., 1]

print "Watershed postprocessing on", in_path
print("Watershed postprocessing on", in_path)

file_id = os.path.basename(in_path)[:-len('_SEG.png')]
resize_factor = int(file_id.split('_')[5])
Expand Down Expand Up @@ -122,7 +122,7 @@ def read_instance(im_file, resize_factor):

time3 = time.time()

print "Time in watershed for ",in_path," Padding/Seed: ",(time1-time0)," Watershed: ",(time2-time1)," Fillholes: ",(time3-time2)
print("Time in watershed for ",in_path," Padding/Seed: ",(time1-time0)," Watershed: ",(time2-time1)," Fillholes: ",(time3-time2))

gen_meta_json(in_path, image_id, wsi_width, wsi_height, method_description,
seg_thres, det_thres, win_size, min_nucleus_size, max_nucleus_size)
10 changes: 5 additions & 5 deletions training-data-real-patch-extraction/extract_patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def poly2mask(vertex_row_coords, vertex_col_coords, mask):


for id in ids:
print 'converting xml to png. {}'.format(id)
print('converting xml to png. {}'.format(id))

tissue = Image.open('MoNuSeg Training Data/Tissue images/{}.tif'.format(id))
s0, s1 = tissue.size[0], tissue.size[1]
Expand All @@ -56,18 +56,18 @@ def poly2mask(vertex_row_coords, vertex_col_coords, mask):


for id in ids:
print 'extracting patches from png. {}'.format(id)
print('extracting patches from png. {}'.format(id))

he_image = np.array(Image.open('MoNuSeg Training Data/Tissue images/{}.tif'.format(id)).convert('RGB'))[15:-15, 15:-15, :]
seg_gt = misc.imread('MoNuSeg Training Data/Mask/{}.png'.format(id))[15:-15, 15:-15] / 40

for x in range(0, he_image.shape[0]-win_size, stride) + [he_image.shape[0]-win_size,]:
for y in range(0, he_image.shape[1]-win_size, stride) + [he_image.shape[1]-win_size,]:
for x in list(range(0, he_image.shape[0]-win_size, stride)) + [he_image.shape[0]-win_size,]:
for y in list(range(0, he_image.shape[1]-win_size, stride)) + [he_image.shape[1]-win_size,]:
he = he_image[x:x+win_size, y:y+win_size, :]
seg = seg_gt[x:x+win_size, y:y+win_size]
if he.shape[0] != win_size or he.shape[1] != win_size or \
seg.shape[0] != win_size or seg.shape[1] != win_size:
print 'some shape error', he.shape, seg.shape
print('some shape error', he.shape, seg.shape)
continue
misc.imsave('real_data_400x400/image_sup/{}.png'.format(patch_id), he)
misc.imsave('real_data_400x400/mask_sup/{}.png'.format(patch_id), seg)
Expand Down
2 changes: 1 addition & 1 deletion training-data-synthesis/draw_fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
try:
image, mask, detect, contour, refer, source, nucl, cyto, intp_mask, tile_path = get_new_fake_image();
except Exception as e:
print e;
print(e);
continue;
mask = (mask>0).astype(np.uint8);
detect = (detect>0).astype(np.uint8);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import numpy as np
import pickle
import png
from . import png
from PIL import Image, ImageDraw
from polygon import generatePolygon
from mask2image_otsu import Mask2Image
from .polygon import generatePolygon
from .mask2image_otsu import Mask2Image
from scipy import ndimage
from sys import stdout

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from os import listdir
from sys import stdout
from os.path import isfile, join
from my_canny import canny_edge_on_mask
from .my_canny import canny_edge_on_mask
from scipy import ndimage
from scipy import stats

Expand Down
14 changes: 7 additions & 7 deletions training-data-synthesis/nuclei_synthesis_40X_online/png.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python

from __future__ import print_function


# png.py - PNG encoder/decoder in pure Python
#
Expand Down Expand Up @@ -1608,7 +1608,7 @@ def asvalues(raw):
out.extend([mask&(o>>i) for i in shifts])
return out[:width]

return map(asvalues, rows)
return list(map(asvalues, rows))

def serialtoflat(self, bytes, width=None):
"""Convert serial format (byte stream) pixel data to flat row
Expand Down Expand Up @@ -1905,8 +1905,8 @@ def iterdecomp(idat):
arraycode = 'BH'[self.bitdepth>8]
# Like :meth:`group` but producing an array.array object for
# each row.
pixels = map(lambda *row: array(arraycode, row),
*[iter(self.deinterlace(raw))]*self.width*self.planes)
pixels = list(map(lambda *row: array(arraycode, row),
*[iter(self.deinterlace(raw))]*self.width*self.planes))
else:
pixels = self.iterboxed(self.iterstraight(raw))
meta = dict()
Expand Down Expand Up @@ -2042,11 +2042,11 @@ def itertrns(pixels):
# True/False to 0/maxval (by multiplication),
# and add it as the extra channel.
row = group(row, planes)
opa = map(it.__ne__, row)
opa = map(maxval.__mul__, opa)
opa = list(map(it.__ne__, row))
opa = list(map(maxval.__mul__, opa))
opa = list(zip(opa)) # convert to 1-tuples
yield array(typecode,
itertools.chain(*map(operator.add, row, opa)))
itertools.chain(*list(map(operator.add, row, opa))))
pixels = itertrns(pixels)
targetbitdepth = None
if self.sbit:
Expand Down