def test_weighted_features_elaborate():
    #Slightly more elaborate, similar to likelihood computations needed for
    # lung segmentation
    left_polynomial_feature_map = PolynomialFeatureMap( [I1,I2],[0,1,2] )  
    left_polynomial_feature_map.compute_num_terms()
    
    left_weights_temp = [0.002149, -0.002069, 5.258745]
    left_weights = [left_weights_temp[2]*left_weights_temp[2], 2*left_weights_temp[0]*left_weights_temp[2], \
                    2*left_weights_temp[1]*left_weights_temp[2], left_weights_temp[0]*left_weights_temp[0], \
                    2*left_weights_temp[0]*left_weights_temp[1], left_weights_temp[1]*left_weights_temp[1] ]
    left_lambda = 1.0
    left_weighted_density = ExpWeightedFeatureMapDensity([I1,I2], left_weights, left_polynomial_feature_map, left_lambda)
    left_likelihood = left_weighted_density.compute()
    
    #should be equivelent to: exp(-(alpha_est_non(1)*Ival + alpha_est_non(2)*Dval+alpha_est_non(3)))^2
    
    assert left_likelihood.all() == np.exp(-np.power(   (left_weights_temp[0]*I1 + left_weights_temp[1]*I2+ left_weights_temp[2])   , 2) ).all()
    
    
    
    
    
    
    
    
    
def test_weighted_features():
    sys.stderr.write(str(I1)+"\n")
    sys.stderr.write(str(I2)+"\n")

    my_polynomial_feature_map = PolynomialFeatureMap( [I1,I2],[2] )  
    my_polynomial_feature_map.compute_num_terms()
    
    assert my_polynomial_feature_map.num_terms == 3

    #define the weights
    
    #exp(-(alpha_est_non(1)*Ival + alpha_est_non(2)*Dval+alpha_est_non(3)))^2
    
    the_weights = [1.0, 0.0, 0.0 ]
    the_lambda = 1.0
    #(self, im_feature_vecs, weights, feature_map, lamda)
    my_weighted_density = ExpWeightedFeatureMapDensity([I1,I2], the_weights, my_polynomial_feature_map, the_lambda)
    my_likelihood = my_weighted_density.compute()
    
    assert my_likelihood.all() == np.exp(-np.power(I1,2)).all()
    

    the_weights = [0.0, 1.0, 0.0 ]
    
    my_weighted_density = ExpWeightedFeatureMapDensity([I1,I2], the_weights, my_polynomial_feature_map, the_lambda)
    my_likelihood = my_weighted_density.compute()
    
    assert my_likelihood.all() == np.exp(-np.power(I2,2)).all()
    
    the_weights = [0.3, 0.5, 1.3 ]
    
    my_weighted_density = ExpWeightedFeatureMapDensity([I1,I2], the_weights, my_polynomial_feature_map, 1.0)
    my_likelihood = my_weighted_density.compute()
    
    assert my_likelihood.all() == np.exp(-(the_weights[0]*np.power(I1,2) + the_weights[1]*I1*I2+ the_weights[2]*np.power(I2,2)) ).all()
def segment_lung_with_atlas(input_image, probabilistic_atlases, exponential_parameters): 
    #TODO: and command line option to this command
    
    """Segment lung using training labeled data. 

    Parameters
    ----------
    input_image : float array, shape (L, M, N)

    probabilistic_atlases : list of float arrays, shape (L, M, N)
        Atlas to use as segmentation prior. Each voxel should have a value in
        the interval [0, 1], indicating the probability of the class.
        
    exponential_parameters: Parameters of the exponential likelihood distribution
        ...
        
    Returns
    -------
    label_map : array, shape (L, M, N)
        Segmented image with labels adhering to CIP conventions
    """
    
    #compute feature vectors for left and right lungs
    ### TODO: replace with likelihood params for class 0.


    length  = np.shape(probabilistic_atlases[0])[0]
    width = np.shape(probabilistic_atlases[0])[1]
    
    #Define lung area to segment
    lungPrior = probabilistic_atlases[0] + probabilistic_atlases[1]
    zero_indeces_thresholding = lungPrior < 0.35 
    lungPriorSlicedialated = lungPrior
    lungPriorSlicedialated[zero_indeces_thresholding] = 0.0
    
    ones_indeces_thresholding = lungPrior > 0.34 
    lungPriorSlicedialated[ones_indeces_thresholding] = 1.0

    lungPriorSlicedialated = scipy.ndimage.binary_dilation(lungPriorSlicedialated, \
      iterations=2)
    scipy.ndimage.morphology.binary_fill_holes(lungPriorSlicedialated, \
      structure=None, output=lungPriorSlicedialated, origin=0)
    tozero_indeces_intensities = lungPriorSlicedialated < 1
    
    
    left_lung_distance_map = compute_distance_to_atlas(probabilistic_atlases[0])   
    right_lung_distance_map = compute_distance_to_atlas(probabilistic_atlases[1])
       
        
    left_polynomial_feature_map = PolynomialFeatureMap( [input_image, \
      left_lung_distance_map],[0,1,2] )  
    left_polynomial_feature_map.compute_num_terms()
    
    right_polynomial_feature_map = PolynomialFeatureMap( [input_image, \
      right_lung_distance_map],[0,1,2] )  
    right_polynomial_feature_map.compute_num_terms()

    #define the weights
    
    #exp(-(alpha(1)*Ival + alpha_(2)*Dval+alpha(3)))^2
    # = exp(-(alpha(1)^2*Ival^2 + alpha(1)*Ival*alpha(2)*Dval  \
    #    +    alpha(1)*Ival*alpha(3) +  alpha(2)*Dval * alpha(1)*Ival
    #    +alpha(2)*Dval*alpha(2)*Dval+alpha(2)*Dval *alpha(3)  \
    #    +  alpha(3)*alpha(1)*Ival+  alpha(3)*alpha(2)*Dval+alpha(3)*alpha(3)))
    
    # = exp(-(alpha(3)*alpha(3) + 2*alpha(1)*alpha(3)*Ival  \
    #    + 2*alpha(2)*alpha(3)*Dval + alpha(1)^2*Ival^2 \ 
    #    + 2* alpha(1)*alpha(2)*Ival*Dval + alpha(2)^2*dval^2 )) 
    
    
    #ExpWeightedFeatureMapDensity computations: 
    #accum = sum_d( \
    #  self.weights[d]*self.feature_map.get_mapped_feature_vec_element(d))
    #exponential_density = np.exp(-self.lamda*accum)*self.lamda
    
    #older weights
    #left_weights_temp = [0.002149, -0.002069, 5.258745] 
    #l_alpha_est_right=[0.001241, -0.025153, 4.609616]      
    #l_alpha_est_non=[-0.001929, 0.010123, 3.937502]  
    #
    #right_weights_temp = [0.002149, -0.002069, 5.258745] 
    #r_alpha_est_left=[0.001241, -0.025153, 4.609616]      
    #r_alpha_est_non=[-0.001929, 0.010123, 3.937502]  
        
    #r_alpha_est_left=[0.002500, -0.095894, 6.786622]
    #right_weights_temp=[0.001245, 0.066628, 4.269774]
    #r_alpha_est_non=[-0.001433, -0.005590, 4.143140]
    
    
    #newer 

#    left_weights_temp=[0.002242, 0.002139, 5.305966]
#    l_alpha_est_right=[0.001987, -0.054164, 5.415881]
#    l_alpha_est_non=[-0.001288, -0.034694, 4.225687]
#
#    r_alpha_est_left=[0.002209, -0.094936, 6.731629]
#    right_weights_temp=[0.001689, 0.072709, 4.398574]
#    r_alpha_est_non=[-0.000816, -0.035418, 4.499488]
    

    #newer, 80 bins instead of 50: left works, right doesnt
    #left_weights_temp=[0.002312, 0.002666, 5.806209]
    #l_alpha_est_right=[0.001729, -0.029253, 5.383404]
    #l_alpha_est_non=[-0.001127, 0.009051, 4.617099]
    #
    #r_alpha_est_left=[0.001914, -0.060901, 6.623247]
    #right_weights_temp=[0.001878, 0.073107, 5.053620]
    #r_alpha_est_non=[-0.000779, -0.029794, 5.143489]




    #l_alpha_est_right=[0.001816, 0.001857, 5.921779]
    #left_weights_temp=[0.002463, 0.118871, 5.827013]
    #l_alpha_est_non=[-0.000091, 0.012917, 4.446526]
    #right_weights_temp=[0.001594, 0.030955, 5.626165] #this is the source of the problemmmm
    #r_alpha_est_left=[0.001946, 0.009239, 5.685427]
    #r_alpha_est_non=[-0.000090, 0.014221, 4.432606]

    #below working
    #alpha_dleft_given_left = [0.002149, -0.002069, 5.258745] 
    #alpha_dleft_given_right=[0.001241, -0.025153, 4.609616]      
    #alpha_dleft_given_non=[-0.001929, 0.010123, 3.937502]  
    ##
    #alpha_dright_given_right = [0.002149, -0.002069, 5.258745] 
    #alpha_dright_given_left=[0.001241, -0.025153, 4.609616]      
    #alpha_dright_given_non=[-0.001929, 0.010123, 3.937502]  
        
    alpha_dleft_given_left = exponential_parameters[0]
    alpha_dleft_given_right=exponential_parameters[1]      
    alpha_dleft_given_non=exponential_parameters[2] 
    #
    alpha_dright_given_right = exponential_parameters[3]
    alpha_dright_given_left=exponential_parameters[4]     
    alpha_dright_given_non=exponential_parameters[5]  


    left_weights = [alpha_dleft_given_left[2]*alpha_dleft_given_left[2], \
                   2*alpha_dleft_given_left[0]*alpha_dleft_given_left[2], \
                    2*alpha_dleft_given_left[1]*alpha_dleft_given_left[2], \
                    alpha_dleft_given_left[0]*alpha_dleft_given_left[0], \
                    2*alpha_dleft_given_left[0]*alpha_dleft_given_left[1], \
                    alpha_dleft_given_left[1]*alpha_dleft_given_left[1] ]
    left_lambda = 1.0
    left_weighted_density = ExpWeightedFeatureMapDensity([\
       input_image.astype(np.float),left_lung_distance_map], left_weights, \
       left_polynomial_feature_map, left_lambda)
    left_likelihood = left_weighted_density.compute()
    
    

    left_weights_given_right = [alpha_dleft_given_right[2]*alpha_dleft_given_right[2], \
                    2*alpha_dleft_given_right[0]*alpha_dleft_given_right[2], \
                    2*alpha_dleft_given_right[1]*alpha_dleft_given_right[2], \
                    alpha_dleft_given_right[0]*alpha_dleft_given_right[0], \
                    2*alpha_dleft_given_right[0]*alpha_dleft_given_right[1], \
                    alpha_dleft_given_right[1]*alpha_dleft_given_right[1] ]
    left_given_right_weighted_density = ExpWeightedFeatureMapDensity([\
       input_image.astype(np.float),left_lung_distance_map], 
       left_weights_given_right, left_polynomial_feature_map, left_lambda)
    LdIgivenRlung = left_given_right_weighted_density.compute()
    
    left_weights_given_nonlung = [alpha_dleft_given_non[2]*alpha_dleft_given_non[2], \
                    2*alpha_dleft_given_non[0]*alpha_dleft_given_non[2], \
                    2*alpha_dleft_given_non[1]*alpha_dleft_given_non[2], \
                    alpha_dleft_given_non[0]*alpha_dleft_given_non[0], \
                    2*alpha_dleft_given_non[0]*alpha_dleft_given_non[1], \
                    alpha_dleft_given_non[1]*alpha_dleft_given_non[1] ]
    left_given_nonlung_weighted_density = ExpWeightedFeatureMapDensity([ \
      input_image.astype(np.float),left_lung_distance_map], \
      left_weights_given_nonlung, left_polynomial_feature_map, left_lambda)
    LdIgivenNlung = left_given_nonlung_weighted_density.compute()
    
    
    
    right_weights = [alpha_dright_given_right[2]*alpha_dright_given_right[2], \
                    2*alpha_dright_given_right[0]*alpha_dright_given_right[2], \
                    2*alpha_dright_given_right[1]*alpha_dright_given_right[2], \
                    alpha_dright_given_right[0]*alpha_dright_given_right[0], \
                    2*alpha_dright_given_right[0]*alpha_dright_given_right[1], \
                    alpha_dright_given_right[1]*alpha_dright_given_right[1] ]
    right_lambda = 1.0
    right_weighted_density = ExpWeightedFeatureMapDensity([input_image, \
           right_lung_distance_map], right_weights, \
           right_polynomial_feature_map, right_lambda)
    right_likelihood = right_weighted_density.compute()
    
    
    right_weights_given_left = [alpha_dright_given_left[2]*alpha_dright_given_left[2], \
                    2*alpha_dright_given_left[0]*alpha_dright_given_left[2], \
                    2*alpha_dright_given_left[1]*alpha_dright_given_left[2], \
                    alpha_dright_given_left[0]*alpha_dright_given_left[0], \
                    2*alpha_dright_given_left[0]*alpha_dright_given_left[1], \
                    alpha_dright_given_left[1]*alpha_dright_given_left[1] ]
    right_lambda = 1.0
    right_given_leftlung_weighted_density = ExpWeightedFeatureMapDensity( \
          [input_image,right_lung_distance_map], right_weights_given_left, \
          right_polynomial_feature_map, right_lambda)
    RdIgivenLlung = right_given_leftlung_weighted_density.compute()
    
    right_weights_given_non = [alpha_dright_given_non[2]*alpha_dright_given_non[2], \
                    2*alpha_dright_given_non[0]*alpha_dright_given_non[2], \
                    2*alpha_dright_given_non[1]*alpha_dright_given_non[2], \
                    alpha_dright_given_non[0]*alpha_dright_given_non[0], \
                    2*alpha_dright_given_non[0]*alpha_dright_given_non[1], \
                    alpha_dright_given_non[1]*alpha_dright_given_non[1] ]
    right_lambda = 1.0
    right_given_nonlung_weighted_density = ExpWeightedFeatureMapDensity( \
         [input_image,right_lung_distance_map], right_weights_given_non, \
         right_polynomial_feature_map, right_lambda)
    RdIgivenNlung = right_given_nonlung_weighted_density.compute()

    
    notLungPrior = np.ones((length, width,np.shape( \
         probabilistic_atlases[0])[2])).astype(np.float)
    notLungPrior = notLungPrior - np.add(probabilistic_atlases[0], \
         probabilistic_atlases[1]);
    
    p_I_dleft = np.add(np.multiply(left_likelihood.astype(np.float), \
         probabilistic_atlases[0].astype(np.float)),np.multiply( \
         LdIgivenRlung.astype(np.float),probabilistic_atlases[1].astype( \
         np.float)),np.multiply(LdIgivenNlung.astype(np.float), \
         notLungPrior.astype(np.float)))  
         
         
    p_I_dright = np.add(np.multiply(RdIgivenLlung.astype(np.float), \
         probabilistic_atlases[0].astype(np.float)),np.multiply( \
         right_likelihood.astype(np.float), \
         probabilistic_atlases[1].astype(np.float)),np.multiply( \
         RdIgivenNlung.astype(np.float),notLungPrior.astype(np.float)))  
    
    zero_indeces = (p_I_dleft == 0)
    p_I_dleft[zero_indeces] = 0.000000000000000000000001
   
    zero_indeces2 = (p_I_dright == 0)
    p_I_dright[zero_indeces2] = 0.000000000000000000000001
    
    left_likelihood[tozero_indeces_intensities]=0
    right_likelihood[tozero_indeces_intensities]=0
    

    #segment given feature vectors
    segmented_labels = segment_chest_with_atlas([left_likelihood.astype( \
       np.float), right_likelihood.astype(np.float)], probabilistic_atlases, \
       [p_I_dleft.astype(np.float), p_I_dright.astype(np.float)])
    
    return segmented_labels
Exemple #4
0
def segment_lung_with_atlas(input_image, probabilistic_atlases,
                            exponential_parameters):
    #TODO: and command line option to this command
    """Segment lung using training labeled data. 

    Parameters
    ----------
    input_image : float array, shape (L, M, N)

    probabilistic_atlases : list of float arrays, shape (L, M, N)
        Atlas to use as segmentation prior. Each voxel should have a value in
        the interval [0, 1], indicating the probability of the class.
        
    exponential_parameters: Parameters of the exponential likelihood distribution
        ...
        
    Returns
    -------
    label_map : array, shape (L, M, N)
        Segmented image with labels adhering to CIP conventions
    """

    #compute feature vectors for left and right lungs
    ### TODO: replace with likelihood params for class 0.

    length = np.shape(probabilistic_atlases[0])[0]
    width = np.shape(probabilistic_atlases[0])[1]

    #Define lung area to segment
    lungPrior = probabilistic_atlases[0] + probabilistic_atlases[1]
    zero_indeces_thresholding = lungPrior < 0.35
    lungPriorSlicedialated = lungPrior
    lungPriorSlicedialated[zero_indeces_thresholding] = 0.0

    ones_indeces_thresholding = lungPrior > 0.34
    lungPriorSlicedialated[ones_indeces_thresholding] = 1.0

    lungPriorSlicedialated = scipy.ndimage.binary_dilation(lungPriorSlicedialated, \
      iterations=2)
    scipy.ndimage.morphology.binary_fill_holes(lungPriorSlicedialated, \
      structure=None, output=lungPriorSlicedialated, origin=0)
    tozero_indeces_intensities = lungPriorSlicedialated < 1

    left_lung_distance_map = compute_distance_to_atlas(
        probabilistic_atlases[0])
    right_lung_distance_map = compute_distance_to_atlas(
        probabilistic_atlases[1])


    left_polynomial_feature_map = PolynomialFeatureMap( [input_image, \
      left_lung_distance_map],[0,1,2] )
    left_polynomial_feature_map.compute_num_terms()

    right_polynomial_feature_map = PolynomialFeatureMap( [input_image, \
      right_lung_distance_map],[0,1,2] )
    right_polynomial_feature_map.compute_num_terms()

    #define the weights

    #exp(-(alpha(1)*Ival + alpha_(2)*Dval+alpha(3)))^2
    # = exp(-(alpha(1)^2*Ival^2 + alpha(1)*Ival*alpha(2)*Dval  \
    #    +    alpha(1)*Ival*alpha(3) +  alpha(2)*Dval * alpha(1)*Ival
    #    +alpha(2)*Dval*alpha(2)*Dval+alpha(2)*Dval *alpha(3)  \
    #    +  alpha(3)*alpha(1)*Ival+  alpha(3)*alpha(2)*Dval+alpha(3)*alpha(3)))

    # = exp(-(alpha(3)*alpha(3) + 2*alpha(1)*alpha(3)*Ival  \
    #    + 2*alpha(2)*alpha(3)*Dval + alpha(1)^2*Ival^2 \
    #    + 2* alpha(1)*alpha(2)*Ival*Dval + alpha(2)^2*dval^2 ))

    #ExpWeightedFeatureMapDensity computations:
    #accum = sum_d( \
    #  self.weights[d]*self.feature_map.get_mapped_feature_vec_element(d))
    #exponential_density = np.exp(-self.lamda*accum)*self.lamda

    #older weights
    #left_weights_temp = [0.002149, -0.002069, 5.258745]
    #l_alpha_est_right=[0.001241, -0.025153, 4.609616]
    #l_alpha_est_non=[-0.001929, 0.010123, 3.937502]
    #
    #right_weights_temp = [0.002149, -0.002069, 5.258745]
    #r_alpha_est_left=[0.001241, -0.025153, 4.609616]
    #r_alpha_est_non=[-0.001929, 0.010123, 3.937502]

    #r_alpha_est_left=[0.002500, -0.095894, 6.786622]
    #right_weights_temp=[0.001245, 0.066628, 4.269774]
    #r_alpha_est_non=[-0.001433, -0.005590, 4.143140]

    #newer

    #    left_weights_temp=[0.002242, 0.002139, 5.305966]
    #    l_alpha_est_right=[0.001987, -0.054164, 5.415881]
    #    l_alpha_est_non=[-0.001288, -0.034694, 4.225687]
    #
    #    r_alpha_est_left=[0.002209, -0.094936, 6.731629]
    #    right_weights_temp=[0.001689, 0.072709, 4.398574]
    #    r_alpha_est_non=[-0.000816, -0.035418, 4.499488]

    #newer, 80 bins instead of 50: left works, right doesnt
    #left_weights_temp=[0.002312, 0.002666, 5.806209]
    #l_alpha_est_right=[0.001729, -0.029253, 5.383404]
    #l_alpha_est_non=[-0.001127, 0.009051, 4.617099]
    #
    #r_alpha_est_left=[0.001914, -0.060901, 6.623247]
    #right_weights_temp=[0.001878, 0.073107, 5.053620]
    #r_alpha_est_non=[-0.000779, -0.029794, 5.143489]

    #l_alpha_est_right=[0.001816, 0.001857, 5.921779]
    #left_weights_temp=[0.002463, 0.118871, 5.827013]
    #l_alpha_est_non=[-0.000091, 0.012917, 4.446526]
    #right_weights_temp=[0.001594, 0.030955, 5.626165] #this is the source of the problemmmm
    #r_alpha_est_left=[0.001946, 0.009239, 5.685427]
    #r_alpha_est_non=[-0.000090, 0.014221, 4.432606]

    #below working
    #alpha_dleft_given_left = [0.002149, -0.002069, 5.258745]
    #alpha_dleft_given_right=[0.001241, -0.025153, 4.609616]
    #alpha_dleft_given_non=[-0.001929, 0.010123, 3.937502]
    ##
    #alpha_dright_given_right = [0.002149, -0.002069, 5.258745]
    #alpha_dright_given_left=[0.001241, -0.025153, 4.609616]
    #alpha_dright_given_non=[-0.001929, 0.010123, 3.937502]

    alpha_dleft_given_left = exponential_parameters[0]
    alpha_dleft_given_right = exponential_parameters[1]
    alpha_dleft_given_non = exponential_parameters[2]
    #
    alpha_dright_given_right = exponential_parameters[3]
    alpha_dright_given_left = exponential_parameters[4]
    alpha_dright_given_non = exponential_parameters[5]


    left_weights = [alpha_dleft_given_left[2]*alpha_dleft_given_left[2], \
                   2*alpha_dleft_given_left[0]*alpha_dleft_given_left[2], \
                    2*alpha_dleft_given_left[1]*alpha_dleft_given_left[2], \
                    alpha_dleft_given_left[0]*alpha_dleft_given_left[0], \
                    2*alpha_dleft_given_left[0]*alpha_dleft_given_left[1], \
                    alpha_dleft_given_left[1]*alpha_dleft_given_left[1] ]
    left_lambda = 1.0
    left_weighted_density = ExpWeightedFeatureMapDensity([\
       input_image.astype(np.float),left_lung_distance_map], left_weights, \
       left_polynomial_feature_map, left_lambda)
    left_likelihood = left_weighted_density.compute()



    left_weights_given_right = [alpha_dleft_given_right[2]*alpha_dleft_given_right[2], \
                    2*alpha_dleft_given_right[0]*alpha_dleft_given_right[2], \
                    2*alpha_dleft_given_right[1]*alpha_dleft_given_right[2], \
                    alpha_dleft_given_right[0]*alpha_dleft_given_right[0], \
                    2*alpha_dleft_given_right[0]*alpha_dleft_given_right[1], \
                    alpha_dleft_given_right[1]*alpha_dleft_given_right[1] ]
    left_given_right_weighted_density = ExpWeightedFeatureMapDensity([\
       input_image.astype(np.float),left_lung_distance_map],
       left_weights_given_right, left_polynomial_feature_map, left_lambda)
    LdIgivenRlung = left_given_right_weighted_density.compute()

    left_weights_given_nonlung = [alpha_dleft_given_non[2]*alpha_dleft_given_non[2], \
                    2*alpha_dleft_given_non[0]*alpha_dleft_given_non[2], \
                    2*alpha_dleft_given_non[1]*alpha_dleft_given_non[2], \
                    alpha_dleft_given_non[0]*alpha_dleft_given_non[0], \
                    2*alpha_dleft_given_non[0]*alpha_dleft_given_non[1], \
                    alpha_dleft_given_non[1]*alpha_dleft_given_non[1] ]
    left_given_nonlung_weighted_density = ExpWeightedFeatureMapDensity([ \
      input_image.astype(np.float),left_lung_distance_map], \
      left_weights_given_nonlung, left_polynomial_feature_map, left_lambda)
    LdIgivenNlung = left_given_nonlung_weighted_density.compute()



    right_weights = [alpha_dright_given_right[2]*alpha_dright_given_right[2], \
                    2*alpha_dright_given_right[0]*alpha_dright_given_right[2], \
                    2*alpha_dright_given_right[1]*alpha_dright_given_right[2], \
                    alpha_dright_given_right[0]*alpha_dright_given_right[0], \
                    2*alpha_dright_given_right[0]*alpha_dright_given_right[1], \
                    alpha_dright_given_right[1]*alpha_dright_given_right[1] ]
    right_lambda = 1.0
    right_weighted_density = ExpWeightedFeatureMapDensity([input_image, \
           right_lung_distance_map], right_weights, \
           right_polynomial_feature_map, right_lambda)
    right_likelihood = right_weighted_density.compute()


    right_weights_given_left = [alpha_dright_given_left[2]*alpha_dright_given_left[2], \
                    2*alpha_dright_given_left[0]*alpha_dright_given_left[2], \
                    2*alpha_dright_given_left[1]*alpha_dright_given_left[2], \
                    alpha_dright_given_left[0]*alpha_dright_given_left[0], \
                    2*alpha_dright_given_left[0]*alpha_dright_given_left[1], \
                    alpha_dright_given_left[1]*alpha_dright_given_left[1] ]
    right_lambda = 1.0
    right_given_leftlung_weighted_density = ExpWeightedFeatureMapDensity( \
          [input_image,right_lung_distance_map], right_weights_given_left, \
          right_polynomial_feature_map, right_lambda)
    RdIgivenLlung = right_given_leftlung_weighted_density.compute()

    right_weights_given_non = [alpha_dright_given_non[2]*alpha_dright_given_non[2], \
                    2*alpha_dright_given_non[0]*alpha_dright_given_non[2], \
                    2*alpha_dright_given_non[1]*alpha_dright_given_non[2], \
                    alpha_dright_given_non[0]*alpha_dright_given_non[0], \
                    2*alpha_dright_given_non[0]*alpha_dright_given_non[1], \
                    alpha_dright_given_non[1]*alpha_dright_given_non[1] ]
    right_lambda = 1.0
    right_given_nonlung_weighted_density = ExpWeightedFeatureMapDensity( \
         [input_image,right_lung_distance_map], right_weights_given_non, \
         right_polynomial_feature_map, right_lambda)
    RdIgivenNlung = right_given_nonlung_weighted_density.compute()


    notLungPrior = np.ones((length, width,np.shape( \
         probabilistic_atlases[0])[2])).astype(np.float)
    notLungPrior = notLungPrior - np.add(probabilistic_atlases[0], \
         probabilistic_atlases[1])

    p_I_dleft = np.add(np.multiply(left_likelihood.astype(np.float), \
         probabilistic_atlases[0].astype(np.float)),np.multiply( \
         LdIgivenRlung.astype(np.float),probabilistic_atlases[1].astype( \
         np.float)),np.multiply(LdIgivenNlung.astype(np.float), \
         notLungPrior.astype(np.float)))


    p_I_dright = np.add(np.multiply(RdIgivenLlung.astype(np.float), \
         probabilistic_atlases[0].astype(np.float)),np.multiply( \
         right_likelihood.astype(np.float), \
         probabilistic_atlases[1].astype(np.float)),np.multiply( \
         RdIgivenNlung.astype(np.float),notLungPrior.astype(np.float)))

    zero_indeces = (p_I_dleft == 0)
    p_I_dleft[zero_indeces] = 0.000000000000000000000001

    zero_indeces2 = (p_I_dright == 0)
    p_I_dright[zero_indeces2] = 0.000000000000000000000001

    left_likelihood[tozero_indeces_intensities] = 0
    right_likelihood[tozero_indeces_intensities] = 0

    #segment given feature vectors
    segmented_labels = segment_chest_with_atlas([left_likelihood.astype( \
       np.float), right_likelihood.astype(np.float)], probabilistic_atlases, \
       [p_I_dleft.astype(np.float), p_I_dright.astype(np.float)])

    return segmented_labels