From f6d7ffaebcaed070f716cd2586151211baa25258 Mon Sep 17 00:00:00 2001 From: nanli-emory <40213692+nanli-emory@users.noreply.github.com> Date: Thu, 4 Jan 2024 21:58:58 -0600 Subject: [PATCH 1/2] verify the mask is empty or not before computation in LocalTextureEstimationModule.py --- histoqc/LocalTextureEstimationModule.py | 43 ++++++++++++++----------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/histoqc/LocalTextureEstimationModule.py b/histoqc/LocalTextureEstimationModule.py index 87186e2..39c738e 100644 --- a/histoqc/LocalTextureEstimationModule.py +++ b/histoqc/LocalTextureEstimationModule.py @@ -24,24 +24,29 @@ def estimateGreyComatrixFeatures(s, params): img = color.rgb2gray(img) mask = s[mask_name] if not invert else ~s[mask_name] - maskidx = mask.nonzero() - maskidx = np.asarray(maskidx).transpose() - idx = np.random.choice(maskidx.shape[0], npatches) - - results = [] - - for id in idx: - r, c = maskidx[id, :] - patch = img[r:r + patch_size, c:c + patch_size] - glcm = graycomatrix(np.digitize(patch,np.linspace(0,1,num=nlevels),right=True), distances=[5], - angles=[0], levels=nlevels, symmetric=True, normed=True) - - results.append([graycoprops(glcm, prop=feat) for feat in feats]) - - results = np.asarray(results).squeeze() - - for vals, feat in zip(results.transpose(), feats): - s.addToPrintList(f"{prefix}{feat}", str(vals.mean())) - s.addToPrintList(f"{prefix}{feat}_std", str(vals.std())) + if len(mask.nonzero()[0]) == 0: # add warning in case the no tissus detected in mask + msg = f"LocalTextureEstimationModule.estimateGreyComatrixFeatures:{prefix} Can not estimate the empty mask since NO tissue remains detectable in mask" + logging.warning(f"{s['filename']} - {msg}") + s["warnings"].append(msg) + else: + maskidx = mask.nonzero() + maskidx = np.asarray(maskidx).transpose() + idx = np.random.choice(maskidx.shape[0], npatches) + + results = [] + + for id in idx: + r, c = maskidx[id, :] + patch = img[r:r + patch_size, c:c + patch_size] + glcm = graycomatrix(np.digitize(patch,np.linspace(0,1,num=nlevels),right=True), distances=[5], + angles=[0], levels=nlevels, symmetric=True, normed=True) + + results.append([graycoprops(glcm, prop=feat) for feat in feats]) + + results = np.asarray(results).squeeze() + + for vals, feat in zip(results.transpose(), feats): + s.addToPrintList(f"{prefix}{feat}", str(vals.mean())) + s.addToPrintList(f"{prefix}{feat}_std", str(vals.std())) return From df80baff91ce4b72e5a36c2fa1beec5db81469a4 Mon Sep 17 00:00:00 2001 From: nanli-emory <40213692+nanli-emory@users.noreply.github.com> Date: Fri, 5 Jan 2024 11:20:25 -0600 Subject: [PATCH 2/2] improve code structure and make it easy to read and maintain --- histoqc/LocalTextureEstimationModule.py | 33 +++++++++++++------------ 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/histoqc/LocalTextureEstimationModule.py b/histoqc/LocalTextureEstimationModule.py index 39c738e..6d3733f 100644 --- a/histoqc/LocalTextureEstimationModule.py +++ b/histoqc/LocalTextureEstimationModule.py @@ -27,26 +27,27 @@ def estimateGreyComatrixFeatures(s, params): if len(mask.nonzero()[0]) == 0: # add warning in case the no tissus detected in mask msg = f"LocalTextureEstimationModule.estimateGreyComatrixFeatures:{prefix} Can not estimate the empty mask since NO tissue remains detectable in mask" logging.warning(f"{s['filename']} - {msg}") - s["warnings"].append(msg) - else: - maskidx = mask.nonzero() - maskidx = np.asarray(maskidx).transpose() - idx = np.random.choice(maskidx.shape[0], npatches) + s["warnings"].append(msg) + return - results = [] + maskidx = mask.nonzero() + maskidx = np.asarray(maskidx).transpose() + idx = np.random.choice(maskidx.shape[0], npatches) - for id in idx: - r, c = maskidx[id, :] - patch = img[r:r + patch_size, c:c + patch_size] - glcm = graycomatrix(np.digitize(patch,np.linspace(0,1,num=nlevels),right=True), distances=[5], - angles=[0], levels=nlevels, symmetric=True, normed=True) + results = [] - results.append([graycoprops(glcm, prop=feat) for feat in feats]) + for id in idx: + r, c = maskidx[id, :] + patch = img[r:r + patch_size, c:c + patch_size] + glcm = graycomatrix(np.digitize(patch,np.linspace(0,1,num=nlevels),right=True), distances=[5], + angles=[0], levels=nlevels, symmetric=True, normed=True) - results = np.asarray(results).squeeze() + results.append([graycoprops(glcm, prop=feat) for feat in feats]) - for vals, feat in zip(results.transpose(), feats): - s.addToPrintList(f"{prefix}{feat}", str(vals.mean())) - s.addToPrintList(f"{prefix}{feat}_std", str(vals.std())) + results = np.asarray(results).squeeze() + + for vals, feat in zip(results.transpose(), feats): + s.addToPrintList(f"{prefix}{feat}", str(vals.mean())) + s.addToPrintList(f"{prefix}{feat}_std", str(vals.std())) return