import numpy as np
import scipy as sp
from . import Measurement
import feature_extraction.util.cleanup as cleanup
from skimage.morphology import binary_erosion, disk
from centrosome .haralick import Haralick

class HaralickTexture(Measurement):
	default_options = {
		'haralick_scale': 10,
		'haralick_angle': 'average',

		'clip_cell_borders': True,
		'erode_cell': False,
		'erode_cell_amount': False,
	}

	def _get_haralick_scales(self, scale, angle):
		return {'vertical': (scale, 0),
				'horizontal': (0, scale),
				'diagonal': (scale, scale),
				'antidiagonal': (scale, -scale)}[angle]

	def compute(self, image):
		# -- preprocessing
		# by default, the AoI mask will include the whole image
		mask = np.ones_like(image).astype(bool)
		if self.options.clip_cell_borders:
			# get the cell boundary mask
			mask = cleanup.cell_boundary_mask(image)

			# if we're told to, erode the mask with a disk by some amount
			if self.options.erode_cell:
				mask = binary_erosion(cleanup.cell_boundary_mask(), disk(self.options.erode_cell_amount))

			# mask the image
			# TODO(liam): we can probably use scipy.MaskedArray to get a speedup here
			image = image.copy()
			image[~mask] = 0 # set everything *outside* the cell to 0

		# -- haralick setup and run
		# we're looking at the entire cell's haralick texture parameters,
		# so we'll generate a single label covering the entire AoI
		labels = mask*1 # convert the boolean mask into a zeros/ones label array

		if self.options.haralick_angle == 'average':
			fvecs = []
			for angle in ['vertical', 'horizontal', 'diagonal', 'antidiagonal']:
				scale_i, scale_j = self._get_haralick_scales(self.options.haralick_scale,
					angle)

				# hstack since .all() outputs an array of 1-arrays
				fvecs.append(np.hstack(Haralick(image, labels, scale_i, scale_j).all()))
			fvec = np.mean(fvecs, axis=0)
		else:
			scale_i, scale_j = self._get_haralick_scales(self.options.haralick_scale,
				self.options.haralick_angle)

			# hstack since .all() outputs an array of 1-arrays
			fvec = np.hstack(Haralick(image, labels, scale_i, scale_j).all())

		return fvec

@@ -262,25 +262,16 @@
  = %7B%0A%09%09'
-haralick_
 scale': 
@@ -277,25 +277,16 @@
  10,%0A%09%09'
-haralick_
 angle': 
@@ -1456,33 +1456,24 @@
 elf.options.
-haralick_
 angle == 'av
@@ -1624,33 +1624,24 @@
 elf.options.
-haralick_
 scale,%0A%09%09%09%09%09
@@ -1882,25 +1882,16 @@
 options.
-haralick_
 scale,%0A%09
@@ -1906,25 +1906,16 @@
 options.
-haralick_
 angle)%0A%0A

Remove haralick prefix on some HaralickTexture options