コード例 #1
0
def metric(metric_name, st):
    # done: use st from DD, don't import
    # import settings

    if metric_name:

        # INT
        if type(metric_name) is int:
            if st.metric.metric_dict.get(metric_name) is None:
                error_handler.write(
                    f"ERROR:\t unresolved metric_name:: continue working as default::"
                    f"\n\tmetric type({metric_name}) is {type(metric_name)}")
                return st.metric.default_metric
            else:
                return metric_name

        # FLOAT
        elif type(metric_name) is float:
            return metric(int(metric_name), st)

        # STR
        elif type(metric_name) is str:
            metric_name = metric_name.lower()
            # for item in settings.metric.metric_dict.items():
            for item in st.metric.metric_dict.items():
                if metric_name == item[1]:
                    return item[0]
            else:  # if no matches
                error_handler.write(
                    f"ERROR:\t out of range:: continue working as default::"
                    f"\n\tmetric type({metric_name}) is {type(metric_name)}")
                return st.metric.default_metric

        # LIST or TUPLE
        elif (type(metric_name) is list
              or type(metric_name) is tuple) and metric_name:
            return metric(metric_name[0], st)

        # DICT or SET
        elif type(metric_name) is dict:
            for el in metric_name.items():
                return metric(el[1], st)
        elif type(metric_name) is set:
            for el in metric_name:
                return metric(el, st)

        # UNKNOWN type of value
        else:
            error_handler.write(
                f"ERROR:\t unresolved type of value::\n\t"
                f"metric type({metric_name}) is {type(metric_name)}")
    else:
        if metric_name is None:
            return st.metric.default_metric
        error_handler.write(
            f"ERROR:\t invalid value:: continue working as default::\n\t"
            f"metric type({metric_name}) is {type(metric_name)}")

    return st.metric.default_metric
コード例 #2
0
    def get_nearest_opponent(self, host_id, *_, rel_table=None):
            """Returns: list like [<r>, <opponent_id>, <opponent_class>]
                    r              === distance to other object
                    opponent_id    === other objects id
                    opponent_class === other objects class
            """

            rel_of = self.get_rel_of(host_id)

            for row in rel_of:
                if self.labels[host_id] != row[2]:
                    return row
            else:
                s = f"ERROR:     dd.get_nearest_opponent(host_id={host_id})\n" \
                    f"\tnearest_opponent not found:: rel_of({host_id})={rel_of}"
                error_handler.write(s, self.st)
            return None
コード例 #3
0
def __euclidean_distance(instance, host_id, other_id):
    """
    Calculates distance btw 2 obj using Euclidean metric sys

    :param instance:        # instance of DataDictionary for extracting obj features
    :param host_id:         # identifier of obj1
    :param other_id:        # identifier of obj2
    :return:                # distance btw obj1 & obj2
    """

    r = None
    try:
        r = sum((instance.df[host_id] - instance.df[other_id])**2)**.5
    except Exception as e:
        import error_handler
        s = f"ERROR:\t on calculate distance:: Euclidean ::" \
            f"\n\thost_id:({host_id}); other_id:{other_id}; error:{e.args}"
        error_handler.write(s)
    return r
コード例 #4
0
def __minkowski_distance(instance, host_id, other_id, p, w):
    """
    Calculates distance btw 2 obj using Minkowski's metric sys

    :param instance:        # instance of DataDictionary for extracting obj features
    :param host_id:         # identifier of obj1
    :param other_id:        # identifier of obj2
    :return:                # distance btw obj1 & obj2
    """

    r = None
    print(f"host:{instance.df[host_id]}; oth:{instance.df[other_id]}; p={p}")
    try:
        r = sum(abs(
            w * (instance.df[host_id] - instance.df[other_id]))**p)**(1 / p)
    except Exception as e:
        import error_handler
        s = f"ERROR:\t on calculate distance:: Euclidean ::" \
            f"\n\thost_id:({host_id}); other_id:{other_id}; error:{e.args}"
        error_handler.write(s)
    return r
コード例 #5
0
def __chebyshev_distance(instance, host_id, other_id):
    """
    Calculates distance btw 2 obj using chebyshev's metric sys

    :param instance:        # instance of DataDictionary for extracting obj features
    :param host_id:         # identifier of obj1
    :param other_id:        # identifier of obj2
    :return:                # distance btw obj1 & obj2
    """

    r = None
    try:
        x = instance.df[host_id]
        y = instance.df[other_id]
        return max(abs(x - y))
    except Exception as e:
        import error_handler
        s = f"ERROR:\t on calculate distance:: __chebyshev_distance ::" \
            f"\n\thost_id:({host_id}); other_id:{other_id}; error:{e.args}"
        error_handler.write(s)
    return r
コード例 #6
0
def get_border(instanse):
    """ identify BORDER objects

    :instance:          - instance of DD
    :return:            - set of border obj's id_number
    """

    st = instanse.st

    # region LOG_FILES
    c = 1
    while True:
        try:
            border_file = open(st.path.border_log[:-4] + str(c) + '.log',
                               mode='r')
        except Exception as e:
            border_file = open(st.path.border_log[:-4] + str(c) + '.log',
                               mode='w')
            break
        c += 1
    del c

    # border_file = open(st.path.border_before_log, mode='w')
    border_file.write(str(datetime.datetime.now()))
    border_file.write("\n\n")
    # endregion LOG_FILES

    link = dict()

    for host_id in instanse.ids:
        link[host_id] = 0

    near = None
    try:
        border = set()  # ids of border objects

        for host_id in instanse.ids:  # for each obj
            near = instanse.get_rel_of(host_id)  # get relative table of obj

            border_file.write(f"\n S[{host_id}];\t near:\n{near}\n")

            for row in near:  # looking for opponent on row host_id
                # if row[st.row.idn] in instanse.ids:
                if instanse.labels[host_id] != int(
                        row[st.rel_of.label]):  # opponent found
                    border.add(int(row[
                        st.rel_of.idn]))  # add opponent_id into border_dict
                    link[int(row[st.rel_of.idn])] += 1
                    border_file.write(
                        f"S[{int(row[st.rel_of.idn])}] now border obj\n")
                    break  # stop looking for opponent
            else:  # if no opponent found
                import error_handler
                s = f"ERROR:: no border obj found :: border_selection ::\n" \
                    f"\thost_id:{host_id}; row[{host_id}]:{near}\n"
                error_handler.write(s, st)
                border_file.write(s)
            border_file.write(f"border: {border}\n")

        border_file.write("\n" + '=' * 50 + '\n')
        border_file.write(f"border: len:{len(border)}\n{border}")
        return border.copy(), link.copy()

    except Exception as e:
        import error_handler
        s = f"ERROR:: can't set border :: border_selection ::\n" \
            f"\thost_id:{host_id};\trow[{host_id}]:{near};\n" \
            f"\terror_msg: {e.args}"
        error_handler.write(s, st)
        border_file.write(s)
        return set(), dict()