def VegetationClassify(Elev_arr, River_arr): 

  from rpy2.robjects.packages import importr
  rpart = importr("rpart")

  # Read the dictionary from the pickle file
  pkl_file = open('decision_tree.pkl','rb')
  rpy2.set_default_mode(rpy2.NO_CONVERSION)
  traing_data = pickle.load(pkl_file)
  pkl_file.close()

  # Create Decision tree for predicting landcover class
  # create the decision tree using rpart 
  fit = rpart(formula='Class ~ Elevation + RiverDistance + Slope \
      + Aspect_x + Aspect_y',data = traing_data, method = "class")

  # calculate River distance using River_arr
  River_dist_arr = dist.CityBlock(River_arr)  
  # claculate slope and aspect
  (Slope_arr, Aspect_arr) = Slope_aspect.Slope_aspect(Elev_arr)

  (x_len, y_len) = Elev_arr.shape
  # Alloctae vegetation array for holding predicted landcover values
  Veg_arr = numpy.zeros((x_len, y_len), dtype = "uint8")

  # Normalize the elevation data
  minimum_elev = numpy.min(Elev_arr)
  factor = numpy.max(Elev_arr) - minimum_elev
  Elev_arr = (Elev_arr[:,:] - minimum_elev)*100/factor

  # Create various list to hold test data
  Elevation = []
  Slope = []
  RiverDistance = []
  Aspect_x = []
  Aspect_y = []

  # Append the data into respective list
  for i in range(0,x_len):
    for j in range(0,y_len):
      Elevation.append(int(Elev_arr[i][j]))
      Slope.append(int(Slope_arr[i][j]))
      RiverDistance.append(int(River_dist_arr[i][j]))
      Aspect_x.append(int(Aspect_arr[i][j][0]))
      Aspect_y.append(int(Aspect_arr[i][j][1]))

  # Create dictionary so as to apply R's predict command on it 
  Test_data ={'Elevation':Elevation ,'Slope':Slope ,'RiverDistance':RiverDistance,\
             'Aspect_x':Aspect_x,'Aspect_y':Aspect_y}

  rpy2.set_default_mode(rpy2.BASIC_CONVERSION)
  # values contain probability values of the predicted landcover classes
  values = rpy2.r.predict(fit,newdata=Test_data,method="class")
  for i in range(0,x_len):
    for j in range(0,y_len):
      # Get the class having max probability for each test data point
      a = ndimage.maximum_position(values[i*x_len + j])
      Veg_arr[i,j] = (a[0]*25) # Assign them some value to facilitate visualization
  return Veg_arr
Esempio n. 2
0
    def test_information_curve(self):

        if (self.__validate_instance() is False):
            return   dmn.helper.GLOBAL_INVALID_INSTANCE_ERR_MSG        

        rpy.set_default_mode(rpy.NO_CONVERSION)
        r.plot(self.__ltm_out__, type = "IIC", items = 0, lwd = 2,
               xlab = "Latent Trait",cex_main = 1.5, cex_lab = 1.3
               , cex_axis = 1.1)
Esempio n. 3
0
    def item_characterstic_curve(self):

        if (self.__validate_instance() is False):
            return   dmn.helper.GLOBAL_INVALID_INSTANCE_ERR_MSG        

        rpy.set_default_mode(rpy.NO_CONVERSION)
        r.plot(self.__ltm_out__, lwd = 2, cex = 1.2, legend = True, cx = "left",
               xlab = "Latent Trait", cex_main = 1.5, cex_lab = 1.3
               , cex_axis = 1.1)
Esempio n. 4
0
    def information(self,range_=None,items=None):

        if (self.__validate_instance() is False):
            return   dmn.helper.GLOBAL_INVALID_INSTANCE_ERR_MSG        

        rpy.set_default_mode(rpy.BASIC_CONVERSION)
        fit_information= r.information(self.__ltm_out__,range=range_
                                       ,items=items)
        rpy.set_default_mode(rpy.NO_CONVERSION)
    
        return fit_information
Esempio n. 5
0
    def margins(self,type_=None,rule=None,nprint=None):

        if (self.__validate_instance() is False):
            return   dmn.helper.GLOBAL_INVALID_INSTANCE_ERR_MSG        

        rpy.set_default_mode(rpy.BASIC_CONVERSION)
        fit_residuals= r.margins(self.__ltm_out__,type=type_,rule=rule
                                 ,nprint=nprint)
        rpy.set_default_mode(rpy.NO_CONVERSION)
    
        return fit_residuals
Esempio n. 6
0
    def residuals(self,resp_patterns=None,order=None):

        if (self.__validate_instance() is False):
            return dmn.helper.GLOBAL_INVALID_INSTANCE_ERR_MSG

        dmn.helper.set_default_mode(dmn.helper.BASIC_CONVERSION)
        fit_residuals= r.residuals(self.__ltm_out__,resp_patterns=resp_patterns
                                   ,order=order)
        rpy.set_default_mode(rpy.NO_CONVERSION)

        return fit_residuals
Esempio n. 7
0
    def item_fit(self,G=None,FUN=None,simulate_p_value=None,B=None):

        if (self.__validate_instance() is False):
            return   dmn.helper.GLOBAL_INVALID_INSTANCE_ERR_MSG        

        rpy.set_default_mode(rpy.BASIC_CONVERSION)
        fit_item_fit= r.item_fit(self.__ltm_out__,G=G,FUN=FUN,
                                 simulate_p_value=simulate_p_value,B=B)
        rpy.set_default_mode(rpy.NO_CONVERSION)
    
        return fit_item_fit
Esempio n. 8
0
    def person_fit(self,alternative=None,resp_patterns=None
                   ,simulate_p_value=None,FUN=None,B=None):

        if (self.__validate_instance() is False):
            return   dmn.helper.GLOBAL_INVALID_INSTANCE_ERR_MSG        

        rpy.set_default_mode(rpy.BASIC_CONVERSION)
        fit_person_fit= r.person_fit(self.__ltm_out__,alternative=alternative
                                     ,resp_patterns=resp_patterns,
                                     simulate_p_value=simulate_p_value,
                                     FUN=FUN,B=B)
        rpy.set_default_mode(rpy.NO_CONVERSION)
    
        return fit_person_fit
Esempio n. 9
0
    def factor_scores(self,resp_patterns=None,method=None,B=None,robust_se=None
                      ,prior=None,return_MIvalues=None):

        if (self.__validate_instance() is False):
            return   dmn.helper.GLOBAL_INVALID_INSTANCE_ERR_MSG        

        rpy.set_default_mode(rpy.BASIC_CONVERSION)
        fit_factor_scores= r.factor_scores(self.__ltm_out__
                                           ,resp_patterns=resp_patterns,
                                           method=method,B=B
                                           ,robust_se=robust_se,
                                           prior=prior
                                           ,return_MIvalues=return_MIvalues)
        rpy.set_default_mode(rpy.NO_CONVERSION)
    
        return fit_factor_scores
Esempio n. 10
0
def _grm(_locals):
    '''Supports method core_R.grm()'''

    # Get self
    self = _locals['damonObj']
    coredata = dmn.tools.get_damon_datadict(self)["coredata"]

    #convert all nanvals to nanvals of r environment.
    coredata[coredata == self.nanval] = r.NAN

    #convert coredata into a dictionary with keys equal to items
    data_dict = {
        str(x): coredata[:, x - 1]
        for x in range(1, len(self["collabels"][0]))
    }

    #this statement is not necessary ,  BASIC_CONVERSION is default..
    rpy.set_default_mode(rpy.NO_CONVERSION)

    #import ltm library using r object, (to install libraries in R, you have to go to R console. as much i know, there is no way to do it from python. )
    r.library("ltm")

    #make the input arguments suitable for r.
    if _locals['na_action'] != None:
        _locals['na_action'] = r[_locals['na_action']]

    #assign a object df to R environment.
    r.assign('df', data_dict)

    print _locals['control']

    #convert df to r data frame to be passed to ltm function
    r("df = data.frame(df)")

    #get the robj of data frame created in r.
    r_data_frame = r("df")

    #call r.ltm
    grm_out = r.grm(r_data_frame,
                    constrained=_locals['constrained'],
                    na_action=_locals['na_action'],
                    IRT_param=_locals['irt_param'],
                    Hessian=_locals['hessian'],
                    start_val=_locals['start_val'],
                    control=_locals['control'])

    return grm_out
Esempio n. 11
0
def DecisionTree(output_dir, elev_filename, landcover_filename, river_filename):
  """
  This module generate decision tree used to allocate landcover classes.
  It imports rpart library from rpy package.
  Reads the training data, creates a sample data and use rpart libray to build decision tree. 
  """
  rpy2.r.library("rpart") # rpart library used for creating Decision tree
  #Read Elevation Data from ascii file
  file_name = "training_data/%s" % (elev_filename)
  Elev_arr = numpy.loadtxt(file_name, unpack=True)
  #Read Landcover Data from ascii file
  file_name = "training_data/%s" % (landcover_filename)
  Landcover = numpy.loadtxt(file_name, unpack=True)
  #Read River Data from ascii file
  file_name = "training_data/%s" % (river_filename)
  River     = numpy.loadtxt(file_name, unpack=True)
  #Compute City block distance from River data
  River_dist_arr = city_block_dist.CityBlock(River)
  #Compute Slope and Aspect from Elevation data
  (Slope_arr,Aspect_arr) = Slope_aspect.Slope_aspect(Elev_arr)

  (x_len,y_len) = Elev_arr.shape
  no_of_veg_class = 10 #no of vegetation class in Landcover matrix
  #Generating Lists for differnt Landcover classes
  # Create list of lists to hold pixels of each landcover class - no of list in 
  # list L is equal to no_of_veg_class  
  L = []
  for i in range(0,no_of_veg_class):
    L.append([]) 

  #Now append the pixel co-ordinates into respective list of lists
  for i in range(1,x_len-1):   # Ignoring boundary cells 
    for j in range(1,y_len-1): # because we don't have slope and aspect for them
      #nodata values already gets handled since we are ignoring it
      if Landcover[i][j] == 0:
        L[0].append( ( i,j ) )
      elif Landcover[i][j] == 1:
        L[1].append( ( i,j ) )
      elif Landcover[i][j] == 2:
        L[2].append( ( i,j ) )
      elif Landcover[i][j] == 3:
        L[3].append( ( i,j ) )
      elif Landcover[i][j] == 4:
        L[4].append( ( i,j ) )
      elif Landcover[i][j] == 5:
        L[5].append( ( i,j ) )
      elif Landcover[i][j] == 6:
        L[6].append( ( i,j ) )
      elif Landcover[i][j] == 7:
        L[7].append( ( i,j ) )
      elif Landcover[i][j] == 8:
        L[8].append( ( i,j ) )
      elif Landcover[i][j] == 9:
        L[9].append( ( i,j ) )

  #Sample Data for decision tree
  #normalizing elevation data
  minimum_elev = numpy.min(Elev_arr)
  factor = numpy.max(Elev_arr) - minimum_elev
  Elev_arr = (Elev_arr[:,:]-minimum_elev)*100/factor

  #Create various list to hold sample training data
  Elevation = []
  Slope = []
  RiverDistance = []
  Aspect_x = []
  Aspect_y = []
  Class = []

  #Now sampling the data
  for i in range(0,no_of_veg_class):  
    if len(L[i]) < 500:
      limit = len(L[i])
    else:
      limit = 500
    for j in range(0,limit):
      Elevation.append( int(Elev_arr[ L[i][j][0] ][ L[i][j][1] ]))
      Slope.append(int(Slope_arr[ L[i][j][0] ][ L[i][j][1] ]))
      RiverDistance.append(int(River_dist_arr[ L[i][j][0] ][ L[i][j][1] ]))
      Aspect_x.append(int(Aspect_arr[ L[i][j][0] ][ L[i][j][1] ][0]))
      Aspect_y.append(int(Aspect_arr[ L[i][j][0] ][ L[i][j][1] ][1]))
      Class.append(i)

  #create dictionary of sample data which will be needed to generate decision tree 
  traing_data = {'Elevation':Elevation,'Slope':Slope,'RiverDistance':RiverDistance,'Aspect_x':Aspect_x,'Aspect_y':Aspect_y,'Class':Class}

  #write dictionary into pickle file for further use(reusability) 
  output =  open('decision_tree.pkl','wb')
  pickle.dump(traing_data,output)
  output.close()

  rpy2.set_default_mode(rpy2.NO_CONVERSION)
  print "Creating Decision tree"
  #Using rpart create the decision tree
  fit = rpy2.r.rpart(formula='Class ~ Elevation + RiverDistance + Slope + Aspect_x + Aspect_y',data=traing_data,method="class")

  #output a png image of the decision tree
  file_name = "%s/DecisionTree.png" % (output_dir)
  rpy2.r.png(file_name)
  rpy2.r.plot(fit)
  rpy2.r.text(fit)
  rpy2.r.dev_off()