Example #1
0
def evaluate_result(sys, label, m):
    # new_sys = np.argmax(sys, axis=-1)
    new_label = np.argmax(label, axis=-1)
    if m % 10000 == 0:
        print(new_label)
        print('\n')
    sys = sys.tolist()
    if m % 10000 == 0:
        print(sys)
        print('\n')
    # time.sleep(10000)
    new_sys = CYK.CYK_table(sys[0]).answer
    # print(new_sys)
    # print(new_label)
    b = len(new_sys)
    a = 0
    # print(new_sys)
    if m % 10000 == 0:
        print('system : ', new_sys)
        print('label : ', new_label[0])
        print('\n')
    if len(new_sys) != len(new_label[0]):
        print(new_sys)
        print(new_label)
        time.sleep(100000)
    for i, j in enumerate(new_sys):
        if int(new_label[0][i]) == int(j):
            a += 1
    # print(a, b)
    # time.sleep(10000)
    return a, b
Example #2
0
def _save_dataset(state, id):
    """Given a dataset identifier, return the text needed to
    re-create it.

    The data set design does not make it easy to tell:

    - if the data was read in from a file, or by load_arrays
      (and the name field set by the user)

    - if the data has been modified - e.g. by a call to set_counts -
      after it was loaded.

    - if in the correct directory (so paths may be wrong)

    """

    idstr = _id_to_str(id)
    dset = state.get_data(id)

    # For now assume that a missing name indicates the data
    # was created by the user, otherwise it's a file name.
    # The checks and error messages do not cover all situations,
    # but hopefully the common ones.
    #
    # This logic should be moved into the DataXXX objects, since
    # this is more extensible, and also the data object can
    # retain knowledge of where the data came from.
    #
    if dset.name.strip() == '':
        # Do not attempt to recreate all data sets at this
        # time. They could be serialized, but leave for a
        # later time (it may also make sense to actually
        # save the data to an external file).
        #
        if isinstance(dset, DataPHA):
            msg = "Unable to re-create PHA data set '{}'".format(id)
            warning(msg)
            return 'print("{}")'.format(msg)

        elif isinstance(dset, DataIMG):
            msg = "Unable to re-create image data set '{}'".format(id)
            warning(msg)
            return 'print("{}")'.format(msg)

        # Fall back to load_arrays. As using isinstance,
        # need to order the checks, since Data1DInt is
        # a subclass of Data1D.
        #
        xs = dset.get_indep()
        ys = dset.get_dep()
        stat = dset.get_staterror()
        sys = dset.get_syserror()

        need_sys = sys is not None
        if need_sys:
            sys = "{}".format(sys.tolist())
        else:
            sys = "None"

        need_stat = stat is not None or need_sys
        if stat is not None:
            stat = "{}".format(stat.tolist())
        else:
            stat = "None"

        ys = "{}".format(ys.tolist())

        out = 'load_arrays({},\n'.format(idstr)
        if isinstance(dset, Data1DInt):
            out += '            {},\n'.format(xs[0].tolist())
            out += '            {},\n'.format(xs[1].tolist())
            out += '            {},\n'.format(ys)
            if need_stat:
                out += '            {},\n'.format(stat)
            if need_sys:
                out += '            {},\n'.format(sys)
            out += '            Data1DInt)'

        elif isinstance(dset, Data1D):
            out += '            {},\n'.format(xs[0].tolist())
            out += '            {},\n'.format(ys)
            if need_stat:
                out += '            {},\n'.format(stat)
            if need_sys:
                out += '            {},\n'.format(sys)
            out += '            Data1D)'

        elif isinstance(dset, Data2DInt):
            msg = "Unable to re-create Data2DInt data set '{}'".format(id)
            warning(msg)
            out = 'print("{}")'.format(msg)

        elif isinstance(dset, Data2D):
            out += '            {},\n'.format(xs[0].tolist())
            out += '            {},\n'.format(xs[1].tolist())
            out += '            {},\n'.format(ys)
            out += '            {},\n'.format(dset.shape)
            if need_stat:
                out += '            {},\n'.format(stat)
            if need_sys:
                out += '            {},\n'.format(sys)
            out += '            Data2D)'

        else:
            msg = "Unable to re-create {} data set '{}'".format(dset.__class__,
                                                                id)
            warning(msg)
            out = 'print("{}")'.format(msg)

        return out

    # TODO: this does not handle options like selecting the columns
    #       from a file, or the number of columns.
    #
    if isinstance(dset, DataPHA):
        dtype = 'pha'
    elif isinstance(dset, DataIMG):
        dtype = 'image'
    else:
        dtype = 'data'

    return 'load_{}({}, "{}")'.format(dtype, idstr, dset.name)
Example #3
0
def _save_dataset(state, id):
    """Given a dataset identifier, return the text needed to
    re-create it.

    The data set design does not make it easy to tell:

    - if the data was read in from a file, or by load_arrays
      (and the name field set by the user)

    - if the data has been modified - e.g. by a call to set_counts -
      after it was loaded.

    - if in the correct directory (so paths may be wrong)

    """

    idstr = _id_to_str(id)
    dset = state.get_data(id)

    # For now assume that a missing name indicates the data
    # was created by the user, otherwise it's a file name.
    # The checks and error messages do not cover all situations,
    # but hopefully the common ones.
    #
    # This logic should be moved into the DataXXX objects, since
    # this is more extensible, and also the data object can
    # retain knowledge of where the data came from.
    #
    if dset.name.strip() == '':
        # Do not attempt to recreate all data sets at this
        # time. They could be serialized, but leave for a
        # later time (it may also make sense to actually
        # save the data to an external file).
        #
        if isinstance(dset, DataPHA):
            msg = "Unable to re-create PHA data set '{}'".format(id)
            warning(msg)
            return 'print("{}")'.format(msg)

        elif isinstance(dset, DataIMG):
            msg = "Unable to re-create image data set '{}'".format(id)
            warning(msg)
            return 'print("{}")'.format(msg)

        # Fall back to load_arrays. As using isinstance,
        # need to order the checks, since Data1DInt is
        # a subclass of Data1D.
        #
        xs = dset.get_indep()
        ys = dset.get_dep()
        stat = dset.get_staterror()
        sys = dset.get_syserror()

        need_sys = sys is not None
        if need_sys:
            sys = "{}".format(sys.tolist())
        else:
            sys = "None"

        need_stat = stat is not None or need_sys
        if stat is not None:
            stat = "{}".format(stat.tolist())
        else:
            stat = "None"

        ys = "{}".format(ys.tolist())

        out = 'load_arrays({},\n'.format(idstr)
        if isinstance(dset, Data1DInt):
            out += '            {},\n'.format(xs[0].tolist())
            out += '            {},\n'.format(xs[1].tolist())
            out += '            {},\n'.format(ys)
            if need_stat:
                out += '            {},\n'.format(stat)
            if need_sys:
                out += '            {},\n'.format(sys)
            out += '            Data1DInt)'

        elif isinstance(dset, Data1D):
            out += '            {},\n'.format(xs[0].tolist())
            out += '            {},\n'.format(ys)
            if need_stat:
                out += '            {},\n'.format(stat)
            if need_sys:
                out += '            {},\n'.format(sys)
            out += '            Data1D)'

        elif isinstance(dset, Data2DInt):
            msg = "Unable to re-create Data2DInt data set '{}'".format(id)
            warning(msg)
            out = 'print("{}")'.format(msg)

        elif isinstance(dset, Data2D):
            out += '            {},\n'.format(xs[0].tolist())
            out += '            {},\n'.format(xs[1].tolist())
            out += '            {},\n'.format(ys)
            out += '            {},\n'.format(dset.shape)
            if need_stat:
                out += '            {},\n'.format(stat)
            if need_sys:
                out += '            {},\n'.format(sys)
            out += '            Data2D)'

        else:
            msg = "Unable to re-create {} data set '{}'".format(dset.__class__,
                                                                id)
            warning(msg)
            out = 'print("{}")'.format(msg)

        return out

    # TODO: this does not handle options like selecting the columns
    #       from a file, or the number of columns.
    #
    if isinstance(dset, DataPHA):
        dtype = 'pha'
    elif isinstance(dset, DataIMG):
        dtype = 'image'
    else:
        dtype = 'data'

    return 'load_{}({}, "{}")'.format(dtype, idstr, dset.name)