def main():
    HOME_PATH = "D:\\SENTIFI_DATA\\filter\\"

    FILE_PATH_TO_JSON_MESSAGES = HOME_PATH + "messages.json"
    FILE_PATH_TO_JSON_CRITERIA = HOME_PATH + "criteria.json"

    json_messages = IOUtils().read_json_data_from_file(FILE_PATH_TO_JSON_MESSAGES)
    json_criteria = IOUtils().read_json_data_from_file(FILE_PATH_TO_JSON_CRITERIA)

    list_criteria = []
    for criteria in json_criteria:
        item = SentifiSearchItem(criteria)
        list_criteria.append(item)

    list_messages = []
    for json_item in json_messages:
        message = SentifiMessage(json_item)
        list_messages.append(message)

    #For each message, compare its soid to criteria's soid
    for message in list_messages:
        for criteria in list_criteria:
            if message.soid == criteria.soid:
                filter = Filter(message, criteria)
                filter.apply()

    for msg in list_messages:
        message.display()

    return list_messages
Exemple #2
0
    def update_anchor(self,
                      name: str,
                      distance: float,
                      robot_id,
                      options=None):
        """Updates distance between anchor and robot.
           Note that anchors names should be unique for this to work"""
        for anchor in self.anchors:
            if anchor.name == name:

                anchor.update_rangings(distance, robot_id)
                anchor.set_raw_distance(distance, robot_id)

                if (options == "SW"):
                    # sliding window enabled

                    sliding_windows = Filter(
                        "SW", [anchor.rangings[robot_id], 20, 12, 0])
                    filtered_distance = sliding_windows.apply()[
                        0]  # output(s) of filters are returned as a list

                    anchor.set_distance(filtered_distance, robot_id)
                else:
                    anchor.set_distance(distance, robot_id)

                # activates the anchor the first time it uploads a distance
                if (not (anchor.isActive) and distance > 0.0):

                    anchor.isActive = True
                return
def extract_serie(serie,real_value,correction_coeff = 1, correction_offset = 0):
    # creating sliding window
    sw_filter = Filter("SW")
    corrected_serie = []
    
    # calculating average value
    
    average = 0
    nb_values = 0
    nb_failed = 0
    for ranging in serie:
        corrected_ranging = (ranging - correction_offset) / correction_coeff
        corrected_serie.append(corrected_ranging)
        if ( (ranging < 0) or (ranging > 5 * real_value)):
        # measurements too far away from actual value are considered as failed
            nb_failed +=1
            nb_values += 1
        else:           
            average += corrected_ranging
            nb_values += 1
    if ( nb_values - nb_failed > 0 ):
        average = average / (nb_values - nb_failed)
    else:
        average = 0
        
    # calculating variance
    sd_list = []
    variance = 0

    if (THOLD_ON):
        threshold = THRESHOLD
    else:
        threshold = 0
    for ranging in serie:
        if not( (ranging < threshold) or (ranging > 5 * real_value)):
            variance += pow( (ranging - average), 2)
            sd_list.append(ranging)
            

    # set variance to 0 if there isn't correct measurements
    if (nb_values - nb_failed > 0):
        variance = variance / len(sd_list)
        
    else:
        variance = 0
    # calculating standard deviation
    abs_sd = math.sqrt(variance)
    #abs_sd = np.std(sd_list)    

    #calculating relative standard deviation
    if (average > 0):
        relative_sd = abs_sd / average
    else:
        relative_sd = 1

    # calculating fail ratio
    if (real_value > 0):
        if (ABS):
            abs_accuracy = abs(average - real_value)
        else:
            abs_accuracy = (average - real_value)
        relative_accuracy = average / real_value
        
    else:
        abs_accuracy = 0
        relative_accuracy = 1
    fail_ratio = nb_failed / nb_values

    if (FILTER_ON):
        #print("before " + str(average) )
        average = sw_filter.apply([corrected_serie,20,0])[0]
        #print("after " + str(average) )
        abs_accuracy = abs(average - real_value) 
        
   

    return(average,abs_sd,relative_sd,abs_accuracy,relative_accuracy,fail_ratio)
Exemple #4
0
class Anchor:
    """Anchor class: represents an anchor"""
    def __init__(self, x, y, z, name="unnamed", color='red'):
        self.x = x
        self.y = y
        self.z = z
        self.color = color
        self.name = name
        self.shown = False
        self.isActive = False
        self.distance = {}
        self.raw_distance = {}
        self.rangings = {}
        self.correction_filter = Filter('COR')
        # initialization of the ranging lists for the two robots
        self.init_rangings()
        
        

        
        # genereates the visual representation of the anchor in the 3D engine
        self.create_sphere()

        # displays the name of the anchor in the 3D engine
        self.create_text()
        
        
    def init_rangings(self):
        """generates an entry for each robot id in the rangings dictionary"""
        for id_bot in bots_id:
            # adding an entry in the rangings dictionary for each robot id
            self.rangings[id_bot] = []
            self.raw_distance[id_bot] = 0
            
    def set_raw_distance(self,distance,robotname):
        """ sets the unfiltered distance between the anchor and the given robot"""
        self.raw_distance[robotname] = distance
        #print("distance set " + str(distance))

    def get_raw_distance(self,robotname):
        """ gets the unfiltered distance between the anchor and the given robot"""
        return(self.raw_distance[robotname])

    def update_rangings(self, distance,target):
        """updates the list of the last NB_RANGINGS rangings"""
        global correction_coeff
        global correction_offset
        
        

        # if this is the first ranging
        # writing NB_RANGINGS times the first distance to fill up the list
        
            
        corrected_distance = self.correction_filter.apply( [ distance, correction_coeff[self.name],correction_offset[self.name] ] )[0]
        
        if (self.rangings[target] == [] ):
            for i in range(1, NB_RANGINGS):
                
                self.rangings[target].append(corrected_distance)
        

       
        else:
            
            self.rangings[target].append(corrected_distance)
            # removes first element to maintain list size
            self.rangings[target].pop(0)
        

    def create_sphere(self):
        """Create the anchors representation for the 3D world"""
        self.model = loader.load_model("smiley")
        self.model.set_color(utils.colors[self.color])
        self.model.set_scale(0.1)
        self.model.set_pos(self.x, self.y, self.z)

    def create_text(self):
        """Create display text with anchor name"""
        self.label = TextNode('anchor label {}'.format(self.name))
        self.label.set_text(self.name)
        if (bots_id[0] in self.distance and bots_id[1] in self.distance):
            self.label.set_text(self.name + ": " + str(self.get_distance(bots_id[0])) + " / " + str(self.get_distance(bots_id[1])))
            
        self.label.set_card_decal(True)
        self.label.set_text_color(utils.colors[self.color])
        self.label_np = render.attach_new_node(self.label)
        self.label_np.set_pos(self.x - 0.1, self.y + 0.1, self.z)
        self.label_np.set_scale(0.2)
        self.label_np.look_at(-base.cam.get_x(), -base.cam.get_y(), -base.cam.get_z())
        taskMgr.add(self.update_text_task, 'update text {}'.format(self.name))

    def show(self):
        """Displays anchor"""
        if not self.shown:
            self.model.reparent_to(render)
            self.shown = True

    def hide(self):
        """Hides anchor"""
        if self.shown:
            self.model.detach_node()
            self.shown = False

    def get_distance(self, robot_id):
        """ gets the filtered distance between the anchor and the given robot"""
        if (robot_id in self.distance):
            
            return self.distance[robot_id]
        else:
            # unknown robot id 
            return(0)

    def set_distance(self, distance, robot_id):
        """ sets the filtered distance between the anchor and the given robot"""
        if robot_id in self.distance:
            self.distance[robot_id] = distance
        else:
            self.distance[robot_id] = distance
            print("new robot id added")
    
    
    def split_dist(self, distance):
        result = ""
        x = 0
        for i in distance:
            if x <= 4:
                result += i
                x += 1
            else:
                break
        return result

    def update_text_task(self, task):
        """Updates the text angle for it to always face the camera"""
        self.label_np.look_at(-base.cam.get_x(), -base.cam.get_y(), -base.cam.get_z())
        if (bots_id[0] in self.distance):
            self.label.set_text(self.name + ": " + self.split_dist( str( self.get_distance(bots_id[0]) ) ) )
        return task.cont