parameters: (in_img_data, ref_img_data)
return_statement: return out_img_data
docstring: Function to piecewise linearly scale image intensities to training data landmarks
docstring_summary: Function to piecewise linearly scale image intensities to training data landmarks
docstring_tokens: Function to piecewise linearly scale image intensities to training data landmarks
function: def piecewise_linear_normalize(in_img_data, ref_img_data):
      """Function to piecewise linearly scale image intensities to training data landmarks"""
  
      in_img_flat = np.ravel(in_img_data, 'C')
      in_img_fg = in_img_flat[in_img_flat > 0].reshape(-1, 1)
      clf_in = mixture.GaussianMixture(n_components=3, covariance_type='full')
      clf_in.fit(in_img_fg)
  
      ref_img_flat = np.ravel(ref_img_data, 'C')
      ref_img_fg = ref_img_flat[ref_img_flat > 0].reshape(-1, 1)
      clf_ref = mixture.GaussianMixture(n_components=3, covariance_type='full')
      clf_ref.fit(ref_img_fg)
  
      in_landmarks = np.asarray(sorted(clf_in.means_.squeeze()))
      in_wm_std = np.sqrt(clf_in.covariances_[np.argmax(clf_in.means_)])
      in_wm_threshold = in_landmarks[2] + 2*in_wm_std[0]
      in_landmarks = np.append(np.asarray([0]), np.append(in_landmarks, in_wm_threshold))
  
      ref_landmarks = np.asanyarray(sorted(clf_ref.means_.squeeze()))
      ref_wm_std = np.sqrt(clf_in.covariances_[np.argmax(clf_in.means_)])
      ref_wm_threshold = 255
      ref_landmarks = np.append(np.asarray([0]), np.append(ref_landmarks, ref_wm_threshold))
      print(ref_landmarks)
      print(in_landmarks)
      out_img_data = np.zeros(in_img_data.shape)
      # map intensities using these landmarks
      for i in range(len(in_landmarks)-1):
          m = (ref_landmarks[i+1] - ref_landmarks[i])/(in_landmarks[i+1] - in_landmarks[i])
          c = (in_landmarks[i+1]*ref_landmarks[i] - in_landmarks[i]*ref_landmarks[i+1])/(in_landmarks[i+1] - in_landmarks[i])
  
          out_img_data[(in_img_data > in_landmarks[i]) & (in_img_data <= in_landmarks[i+1])] = \
              m*in_img_data[(in_img_data > in_landmarks[i]) & (in_img_data <= in_landmarks[i+1])] + c
  
      out_img_data[(in_img_data > in_landmarks[-1])] = 255
      return out_img_data
function_tokens: def piecewise_linear_normalize ( in_img_data , ref_img_data ) : in_img_flat = np . ravel ( in_img_data , 'C' ) in_img_fg = in_img_flat [ in_img_flat > 0 ] . reshape ( - 1 , 1 ) clf_in = mixture . GaussianMixture ( n_components = 3 , covariance_type = 'full' ) clf_in . fit ( in_img_fg ) ref_img_flat = np . ravel ( ref_img_data , 'C' ) ref_img_fg = ref_img_flat [ ref_img_flat > 0 ] . reshape ( - 1 , 1 ) clf_ref = mixture . GaussianMixture ( n_components = 3 , covariance_type = 'full' ) clf_ref . fit ( ref_img_fg ) in_landmarks = np . asarray ( sorted ( clf_in . means_ . squeeze ( ) ) ) in_wm_std = np . sqrt ( clf_in . covariances_ [ np . argmax ( clf_in . means_ ) ] ) in_wm_threshold = in_landmarks [ 2 ] + 2 * in_wm_std [ 0 ] in_landmarks = np . append ( np . asarray ( [ 0 ] ) , np . append ( in_landmarks , in_wm_threshold ) ) ref_landmarks = np . asanyarray ( sorted ( clf_ref . means_ . squeeze ( ) ) ) ref_wm_std = np . sqrt ( clf_in . covariances_ [ np . argmax ( clf_in . means_ ) ] ) ref_wm_threshold = 255 ref_landmarks = np . append ( np . asarray ( [ 0 ] ) , np . append ( ref_landmarks , ref_wm_threshold ) ) print ( ref_landmarks ) print ( in_landmarks ) out_img_data = np . zeros ( in_img_data . shape ) # map intensities using these landmarks for i in range ( len ( in_landmarks ) - 1 ) : m = ( ref_landmarks [ i + 1 ] - ref_landmarks [ i ] ) / ( in_landmarks [ i + 1 ] - in_landmarks [ i ] ) c = ( in_landmarks [ i + 1 ] * ref_landmarks [ i ] - in_landmarks [ i ] * ref_landmarks [ i + 1 ] ) / ( in_landmarks [ i + 1 ] - in_landmarks [ i ] ) out_img_data [ ( in_img_data > in_landmarks [ i ] ) & ( in_img_data <= in_landmarks [ i + 1 ] ) ] = m * in_img_data [ ( in_img_data > in_landmarks [ i ] ) & ( in_img_data <= in_landmarks [ i + 1 ] ) ] + c out_img_data [ ( in_img_data > in_landmarks [ - 1 ] ) ] = 255 return out_img_data