예제 #1
0
    def handle_request(self, conn, addr):
        """
        general request handler
        return: Breaks user connection, in case batch request handling is enabled, else: ignored
        """
        # request_id = self.receive_uchar(conn, timeout=9999)

        byte = conn.recv(1)
        if not byte:
            log.info('server', "Client has disconnected")
            return False

        request_id = ord(byte)

        if request_id in self.req_lookup:
            req_type = self.req_lookup[request_id]
            log.info('server', "Incomming request: " + req_type)

            try:
                req = getattr(M_REQUESTS, req_type)
                # feedback handle
                handle = [True]
                # handle request
                req(self, conn, handle=handle)
                # batch mode: False: breaks client communication
                # regular mode: ignored
                return handle[0]
            except AttributeError:
                log.error("Request model '"+req_type+"' is not yet implemented or an Exception occurred.")
        else:
            log.error("Unsupported request type: " + str(request_id))

        # batch mode: communication continues (can be used to break client loop)
        # regular mode: ignored
        return True
예제 #2
0
    def start_server(self, one_req_per_conn=True, verbose=True):

        # settings
        self.ONE_REQ_PER_CONN = one_req_per_conn
        self.VERBOSE = verbose

        self.SERVER_SOCKET = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        # create socket
        try:
            self.SERVER_SOCKET.bind((self.HOST, self.PORT))
        except socket.error, msg:
            log.error('--- Bind failed. Error Code : ' + str(msg[0]) +
                      ' Message ' + msg[1])
            sys.exit()
예제 #3
0
    def handle_request(self, conn, addr):
        """general request handler"""
        request_id = self.receive_uchar(conn)

        # message routing
        req_lookup = ROUTING['REQUEST']['NAME']

        if request_id in req_lookup:
            req_type = req_lookup[request_id]

            try:
                req = getattr(M_REQUESTS, req_type)
                # handle request
                req(self, conn)
            except AttributeError:
                log.error("Request model '" + req_type +
                          "' is not yet implemented or an Exception occurred.")
        else:
            log.error("Unsupported request type: " + str(request_id))
예제 #4
0
    def filter_x_samples(self, nr_samples):

        if self.k != 3:
            log.error("METHOD NOT IMPLEMENTED FOR K != 3!")
            raise

        dist_sum = np.sum(self.dist, axis=1)

        # sort by sum value
        indices = np.arange(0, len(dist_sum))
        min_dist_sum, indices = zip(*sorted(zip(dist_sum, indices)))
        min_dist_sum = np.array(min_dist_sum)
        indices = np.array(indices)
        # sort values accordingly
        dist_sorted = np.array([self.dist[i, :] for i in indices])

        if self.__verbose:
            print self.dist
            print "---- Dist array sorted by sum:"
            print dist_sorted
            print "Indices sorted by sum: {}".format(indices)

        # sum of values below threshold
        mask1 = min_dist_sum < self.__thresh * 2
        if self.__verbose:
            print "Min dist sum: {}".format(min_dist_sum)
            print "Mask1: {}".format(mask1)

        # both values below single threshold
        diff = np.absolute(dist_sorted[:, 0] - dist_sorted[:, 1])
        mask2 = diff < self.__thresh

        comb_mask = mask1*mask2

        # filter indices in order of increasing sum
        indices_prefiltered = indices[comb_mask]

        indices_to_delete = indices_prefiltered[0:nr_samples]

        # filter first X samples
        return np.delete(self.__data, indices_to_delete, axis=0)
예제 #5
0
    def __angleBAC(self, A, B, C, AB, AC):  # AB AC mold
        """
        calculate <AB, AC>
        """
        vector_AB = B - A  # vector_AB = (x1, x2, ..., xn)
        vector_AC = C - A
        mul = vector_AB * vector_AC  # mul = (x1y1, x2y2, ..., xnyn)
        dotProduct = mul.sum()  # dotProduct = x1y1 + x2y2 + ... + xnyn

        try:
            cos_AB_AC_ = dotProduct / (AB * AC)  # cos<AB, AC>
        except ZeroDivisionError:
            sys.exit('ERROR\tangleBAC\tdistance can not be zero!')

        if math.fabs(cos_AB_AC_) > 1:
            if np.array_equal(B, C):
                log.error("Points are equal: B == C")
            elif np.array_equal(A, B):
                log.error("Points are equal: A == B")
            elif np.array_equal(A, C):
                log.error("Points are equal: A == C")

            print 'AB = %f, AC = %f' % (AB, AC)
            print 'AB * AC = ', dotProduct
            print '|AB| * |AC| = ', AB * AC
            sys.exit('ERROR\tangleBAC\tmath domain ERROR, |cos<AB, AC>| <= 1')
        angle = float(math.acos(cos_AB_AC_))  # <AB, AC> = arccos(cos<AB, AC>)
        return angle
예제 #6
0
    def angleBAC(A, B, C, AB, AC):  # AB AC mold
        """
        calculate: <AB, AC> = |AB||AC|*cos(AB,AC)
		then divide by length and take cos-1
        """
        vector_AB = B - A  # vector_AB = (x1, x2, ..., xn)
        vector_AC = C - A
        # fast np implementation
        dotProduct = np.dot(vector_AB, vector_AC)
        # dotProduct = fast_dot(vector_AB, vector_AC)

        try:
            cos_AB_AC_ = dotProduct / (AB * AC)  # cos<AB, AC>
        except ZeroDivisionError:
            sys.exit('ERROR\tangleBAC\tdistance can not be zero!')

        # alternative: clip(cos_AB_AC_, -1, 1)
        cos_AB_AC_ = clip(cos_AB_AC_, -1, 1)

        # K(X, Y) = < X, Y > / ( | | X | | * | | Y | |)
        # a lot slower:
        # cos_AB_AC_ = cosine_similarity(vector_AB.reshape(1,-1), vector_AC.reshape(1,-1))

        if math.fabs(cos_AB_AC_) > 1:

            print "cos<AB, AC> = ", cos_AB_AC_
            if np.array_equal(B, C):
                log.error("Points are equal: B == C")
            elif np.array_equal(A, B):
                log.error("Points are equal: A == B")
            elif np.array_equal(A, C):
                log.error("Points are equal: A == C")

            print 'AB = %f, AC = %f' % (AB, AC)
            print 'AB * AC = ', dotProduct
            print '|AB| * |AC| = ', AB * AC
            sys.exit('ERROR\tmath domain ERROR, |cos<AB, AC>| <= 1')

        # faster than np.arccos
        angle = float(math.acos(cos_AB_AC_))  # <AB, AC> = arccos(cos<AB, AC>)
        return angle
예제 #7
0
    def __abof_multi(self, samples, knn=None, cosine_weighting=False):
        """
        calculate the ABOF of A = (x1, x2, ..., xn)
        pt_list = self.data (cluster)
        """
        # Todo: fix cosine dist weighting
        pt_list = self.data

        if knn is not None and knn < len(self.data):
            pt_list = random.sample(pt_list, knn)

        dist_lookup = pairwise_distances(samples, pt_list, metric='euclidean')

        if cosine_weighting:
            cos_dist_lookup = pairwise_distances(samples,
                                                 pt_list,
                                                 metric='cosine')

        # print np.shape(dist_lookup[0])
        factors = []

        # if only one sample: cannot calculate abof
        if len(pt_list) < 2:
            log.severe(
                'Cannot calculate ABOF with {} reference samples'.format(
                    len(pt_list)))
            fake_abod = 0
            if dist_lookup[0][0] < 0.3:
                fake_abod = 3
            else:
                fake_abod = 0.1

            factors.append(fake_abod)
            return factors

        for i_sample, A in enumerate(samples):
            varList = []
            for i in range(len(pt_list)):
                B = pt_list[i]
                AB = dist_lookup[i_sample][i]
                j = 0
                for j in range(i + 1):
                    if j == i:  # ensure B != C
                        continue

                    C = pt_list[j]
                    AC = dist_lookup[i_sample][j]

                    if np.array_equal(B, C):
                        log.error(
                            "Points are equal: B == C! Assuming classification of training point (ABOD 1000)"
                        )
                        varList.append(1000)
                        print "Bi/Cj: {}/{}".format(i, j)
                        # sys.exit('ERROR\tangleBAC\tmath domain ERROR, |cos<AB, AC>| <= 1')
                        continue

                    angle_BAC = self.__angleBAC(A, B, C, AB, AC)
                    # compute each element of variance list
                    try:
                        # apply weighting
                        if cosine_weighting:
                            tmp = angle_BAC / float(
                                math.pow(
                                    (2.0 - cos_dist_lookup[i_sample][i]) *
                                    (2.0 - cos_dist_lookup[i_sample][j]), 2))
                        else:
                            tmp = angle_BAC / float(math.pow(AB * AC, 2))
                    except ZeroDivisionError:
                        log.severe(
                            "ERROR\tABOF\tfloat division by zero! Trying to predict training point?'"
                        )
                        tmp = 500
                        # sys.exit('ERROR\tABOF\tfloat division by zero! Trying to predict training point?')
                    varList.append(tmp)
            factors.append(np.var(varList))
        return factors
예제 #8
0
    def get_score(test_samples, reference_set):

        assert test_samples.ndim == 2
        assert reference_set.ndim == 2

        dist_lookup = pairwise_distances(test_samples,
                                         reference_set,
                                         metric='euclidean')

        # print np.shape(dist_lookup[0])
        factors = []

        # if only one sample: cannot calculate abof
        if len(reference_set) < 3:
            log.severe(
                'Cannot calculate ABOF with {} reference samples (variance calculation needs at least 3 reference points)'
                .format(len(reference_set)))
            raise Exception

        for i_sample, A in enumerate(test_samples):
            factor_list = []
            for i in range(len(reference_set)):
                # select first point in reference set
                B = reference_set[i]
                # distance
                AB = dist_lookup[i_sample][i]

                for j in range(i + 1):
                    if j == i:  # ensure B != C
                        continue
                    # select second point in reference set
                    C = reference_set[j]
                    # distance
                    AC = dist_lookup[i_sample][j]

                    if np.array_equal(B, C):
                        print "Bi/Cj: {}/{}".format(i, j)
                        log.error(
                            "Points are equal: B == C! Assuming classification of training point"
                        )
                        sys.exit(
                            "Points are equal: B == C! Reference Set contains two times the same samples"
                        )
                        factor_list.append(1000)
                        # sys.exit('ERROR\tangleBAC\tmath domain ERROR, |cos<AB, AC>| <= 1')
                        continue

                    # angle_BAC = ABOD.angleBAC(A, B, C, AB, AC)
                    # angle_BAC = ABOD.angleFast(A-B, A-C)

                    vector_AB = B - A
                    vector_AC = C - A

                    # compute each element of variance list
                    try:
                        cos_similarity = np.dot(vector_AB,
                                                vector_AC) / (AB * AC)
                        # apply weighting
                        tmp = cos_similarity / float(math.pow(AB * AC, 2))
                    except ZeroDivisionError:
                        log.severe(
                            "ERROR\tABOF\tfloat division by zero! Trying to predict training point?'"
                        )
                        tmp = 500
                        # sys.exit('ERROR\tABOF\tfloat division by zero! Trying to predict training point?')
                    factor_list.append(tmp)
            factors.append(np.var(factor_list))
        return np.array(factors)