<commit_before>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 19 02:25:34 2019.

@author: mtageld
"""

import girder_client
import numpy as np

# %%===========================================================================
# Constants & prep work

APIURL = 'http://candygram.neurology.emory.edu:8080/api/v1/'
SAMPLE_SLIDE_ID = "5d586d76bd4404c6b1f286ae"

gc = girder_client.GirderClient(apiUrl=APIURL)
# gc.authenticate(interactive=True)
gc.authenticate(apiKey='<KEY>')


# %%===========================================================================

import matplotlib.pylab as plt
from matplotlib.colors import ListedColormap

from pandas import DataFrame, concat
from skimage.color import rgb2gray
from skimage.segmentation import slic
from skimage.transform import resize
from sklearn.mixture import GaussianMixture
from skimage.measure import regionprops
from matplotlib import cm

from histomicstk.utils.general_utils import Base_HTK_Class

from histomicstk.saliency.tissue_detection import (
    get_slide_thumbnail, get_tissue_mask, _deconv_color)
from histomicstk.annotations_and_masks.masks_to_annotations_handler import (
    get_contours_from_mask, get_annotation_documents_from_contours)
from histomicstk.annotations_and_masks.annotation_and_mask_utils import (
    get_image_from_htk_response)
from histomicstk.features.compute_intensity_features import (
    compute_intensity_features)
from histomicstk.features.compute_haralick_features import (
    compute_haralick_features)

# %%===========================================================================


class Cellularity_Detector_Superpixels(Base_HTK_Class):
    """Detect cellular regions in a slides by classifying superpixels."""

    def __init__(self, gc, slide_id, **kwargs):
        """Init Polygon_merger object.

        Arguments:
        -----------
        gc : object
            girder client object
        slide_id : str
            girder ID of slide
        MAG : float
            magnification at which to detect cellularity
        verbose : int
            0 - Do not print to screen
            1 - Print only key messages
            2 - Print everything to screen
        monitorPrefix : str
            text to prepend to printed statements

        """
        default_attr = {
            'MAG': 3.0,
            'verbose': 1,
            'monitorPrefix': "",
            'spixel_size_baseMag': 350 * 350,
            'compactness': 0.1,
            'use_grayscale': True,
            'use_intensity': True,
            'use_texture': False,
            # 'keep_feats': None,
            'keep_feats': [
                "Intensity.Mean", "Intensity.Median",
                "Intensity.Std", "Intensity.IQR",
                "Intensity.HistEntropy",
            ],
            'opacity': 0,
            'lineWidth': 3.0,
        }
        super().__init__(default_attr=default_attr)



    # %% =====================================================================



# %%

cds = Cellularity_Detector_Superpixels(gc, SAMPLE_SLIDE_ID)

# %%

a

# %%===========================================================================
# %%===========================================================================
# %%===========================================================================

slide_id = SAMPLE_SLIDE_ID

# %%===========================================================================

# get tissue mask
thumbnail_rgb = get_slide_thumbnail(gc, SAMPLE_SLIDE_ID)
labeled, _ = get_tissue_mask(
    thumbnail_rgb, deconvolve_first=False,
    n_thresholding_steps=1, sigma=1.5, min_size=500)

# Find size relative to WSI
slide_info = gc.get('item/%s/tiles' % SAMPLE_SLIDE_ID)
F_tissue = slide_info['sizeX'] / labeled.shape[1]

unique_tvals = list(set(np.unique(labeled)) - {0, })

tval = unique_tvals[1]
#for tval in unique_tvals:

# %%===========================================================================

tissue_mask = labeled == tval

def _get_tissue_rgb_from_small_mask(tissue_mask):
    # find coordinates at scan magnification
    tloc = np.argwhere(tissue_mask)
    ymin, xmin = [int(j) for j in np.min(tloc, axis=0) * F_tissue]
    ymax, xmax = [int(j) for j in np.max(tloc, axis=0) * F_tissue]
    # load RGB for this tissue piece at saliency magnification
    getStr = "/item/%s/tiles/region?left=%d&right=%d&top=%d&bottom=%d" % (
        slide_id, xmin, xmax, ymin, ymax) + "&magnification=%d" % MAG
    resp = gc.get(getStr, jsonResp=False)
    tissue = get_image_from_htk_response(resp)
    return tissue

<commit_msg>[WIP] - cellularity detector class<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 19 02:25:34 2019.

@author: mtageld
"""

import girder_client
import numpy as np

# %%===========================================================================
# Constants & prep work

APIURL = 'http://candygram.neurology.emory.edu:8080/api/v1/'
#SAMPLE_SLIDE_ID = "5d586d76bd4404c6b1f286ae"

#gc = girder_client.GirderClient(apiUrl=APIURL)
# gc.authenticate(interactive=True)
gc.authenticate(apiKey='<KEY>')


# %%===========================================================================

import matplotlib.pylab as plt
from matplotlib.colors import ListedColormap

from pandas import DataFrame, concat
from skimage.color import rgb2gray
from skimage.segmentation import slic
from skimage.transform import resize
from sklearn.mixture import GaussianMixture
from skimage.measure import regionprops
from matplotlib import cm

from histomicstk.utils.general_utils import Base_HTK_Class

from histomicstk.saliency.tissue_detection import (
    get_slide_thumbnail, get_tissue_mask, _deconv_color)
from histomicstk.annotations_and_masks.masks_to_annotations_handler import (
    get_contours_from_mask, get_annotation_documents_from_contours)
from histomicstk.annotations_and_masks.annotation_and_mask_utils import (
    get_image_from_htk_response)
from histomicstk.features.compute_intensity_features import (
    compute_intensity_features)
from histomicstk.features.compute_haralick_features import (
    compute_haralick_features)

# %%===========================================================================


class Cellularity_Detector_Superpixels(Base_HTK_Class):
    """Detect cellular regions in a slides by classifying superpixels."""

    def __init__(self, gc, slide_id, **kwargs):
        """Init Polygon_merger object.

        Arguments:
        -----------
        gc : object
            girder client object
        slide_id : str
            girder ID of slide
        MAG : float
            magnification at which to detect cellularity
        verbose : int
            0 - Do not print to screen
            1 - Print only key messages
            2 - Print everything to screen
        monitorPrefix : str
            text to prepend to printed statements

        """
        default_attr = {
            'verbose': 1,
            'monitorPrefix': "",
            'MAG': 3.0,
            'spixel_size_baseMag': 350 * 350,
            'compactness': 0.1,
            'use_grayscale': True,
            'use_intensity': True,
            'use_texture': False,
            # 'keep_feats': None,
            'keep_feats': [
                "Intensity.Mean", "Intensity.Median",
                "Intensity.Std", "Intensity.IQR",
                "Intensity.HistEntropy",
            ],
            'opacity': 0,
            'lineWidth': 3.0,
            'get_tissue_mask_kwargs': {
                'deconvolve_first': False, 'n_thresholding_steps': 1,
                'sigma': 1.5, 'min_size': 500,
            },
        }
        super().__init__(default_attr=default_attr)

        # set attribs
        self.gc = gc
        self.slide_id = slide_id



    # %% =====================================================================



# %%

cds = Cellularity_Detector_Superpixels(gc, SAMPLE_SLIDE_ID)

# %%

a

# %%===========================================================================
# %%===========================================================================
# %%===========================================================================


# %%===========================================================================

# get tissue mask
thumbnail_rgb = get_slide_thumbnail(self.gc, self.SAMPLE_SLIDE_ID)
labeled, _ = get_tissue_mask(thumbnail_rgb, self.get_tissue_mask_kwargs)

# Find size relative to WSI
slide_info = gc.get('item/%s/tiles' % self.slide_id)
F_tissue = slide_info['sizeX'] / labeled.shape[1]

unique_tvals = list(set(np.unique(labeled)) - {0, })

tval = unique_tvals[1]
#for tval in unique_tvals:

# %%===========================================================================

tissue_mask = labeled == tval

def _get_tissue_rgb_from_small_mask(tissue_mask):
    # find coordinates at scan magnification
    tloc = np.argwhere(tissue_mask)
    ymin, xmin = [int(j) for j in np.min(tloc, axis=0) * F_tissue]
    ymax, xmax = [int(j) for j in np.max(tloc, axis=0) * F_tissue]
    # load RGB for this tissue piece at saliency magnification
    getStr = "/item/%s/tiles/region?left=%d&right=%d&top=%d&bottom=%d" % (
        slide_id, xmin, xmax, ymin, ymax) + "&magnification=%d" % MAG
    resp = gc.get(getStr, jsonResp=False)
    tissue = get_image_from_htk_response(resp)
    return tissue


{'fraction_non_alphanumeric': 0.23210458636948136, 'fraction_numerical': 0.01350192884697814, 'mean_word_length': 3.6432835820895524, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1788113', 'n_tokens_mistral': 2951, 'n_tokens_neox': 2684, 'n_words': 578}