Exemplo n.º 1
0
def genSingleOntologyStats(ontNext, ontLink, minLinkoSize,
                           maxLinkoSize, stepLinkoSize, modelNum,
                           runNum, precision=2, seeds=None):
    """Generate the stats on link models for a given ontology.

    inputs:

    ontNext: ontology used to generate Markov model that create the
    next state.

    ontLink: ontology used for constructing linkographs.

    minLinkoSize: the minimun number of nodes in the linkographs to
    consider.

    maxLinkoSize: the maximum number of nodes in the linkographs to
    consider. Note that the max is not included to match pythons
    convertions on lists and ranges.

    stepLinkoSize: the step size between minLinkoSize to maxLinkoSize
    for the number of linkographs to Consider.

    modelNum: the number of models.

    runNum: the number of linkographs to consider for each linkograph
    size.

    precision:  the number of decimals places to use for the Markov
    models.

    seeds: a list of seeds to use for the generated next Markov
    models. The size of the list should be the same as the number of
    runs.

    output:

    a modelNum x number_of_linkographs array that records the
    Frobenius norm of the average Markov model for each model and each
    linkograph size. The (i, j) entry uses i-th model and the n-th
    size linkograph, constructs runNum number of linkographs of that
    size, finds the average link Markov model, and records the norm of
    this average.

    """

    linkoSizes = range(minLinkoSize, maxLinkoSize, stepLinkoSize)

    ontSize = len(ontNext)
    absClasses = list(ontNext.keys())
    absClasses.sort()

    results = np.zeros((modelNum, len(linkoSizes)))

    if seeds is None:
        seeds = [time.time()*i for i in range(modelNum)]

    models = []
    # Create the generating models
    for i in range(modelNum):
        m = markel.genModelFromOntology(ontology=ontNext,
                                        precision=2,
                                        seed=seeds[i])

        # Storing the model and the current state
        models.append(m)

    # For each size linkograph, generate the runNum links and
    # caculate the needed statistics.
    for size in linkoSizes:

        print('size: {0}'.format(size))

        for modelIndex, m in enumerate(models):

            linkModels = np.zeros((ontSize, ontSize, runNum))

            for i in range(runNum):

                # Randomize the initial state
                m.state = m.random.randint(1, len(m.absClasses)) - 1

                linko = m.genLinkograph(size, ontology=ontLink)

                newModel = markel.genModelFromLinko(linko,
                                                    precision=precision,
                                                    ontology=None,
                                                    seed=None,
                                                    method='link_predictor',
                                                    linkNum=1)

                linkModels[:, :, i] = newModel.tMatrix

            # Find the matrix norm for the average.
            index = (size - minLinkoSize)//stepLinkoSize
            norm = np.linalg.norm(np.mean(linkModels, axis=-1),
                                  ord='fro')
            results[modelIndex][index] = norm

    return results
Exemplo n.º 2
0
llda.linkoPrint(linko8)

# We can save our Markov model using the json interface
markel.writeJson(model, 'resources/model.json')

# We can also read in the Markov model
model2 = markel.readJson('resources/model.json')

# We can use the distance function to verify that the transition
# matrices are the same. Note that this does not in itself show that
# the Markov models are the same since there is a lot more state than
# this.
model.dist(model2)

# We can also create a Markov model based on an ontology
modelOnt = markel.genModelFromOntology(ont, seed=42)
modelOnt.tMatrix

# We can also create Markov models based on linkographs
# First we create a linkograph of size 100
linko100 = model.genLinkograph(100)
linko100.labels

# Now we create a Markov model using the 'link' method which creates a
# Markov model such that he probability of the transition 'A' to 'B'
# is the same as the percentage of links that have a terminal node
# labeled 'B', provided the current node is labeled 'A'.
modelLinkoLink = markel.genModelFromLinko(linko100,
                                          ontology=ont,
                                          seed=42,
                                          method='link')
Exemplo n.º 3
0
def genSingleOntologyStats(metric,
                           ontNext,
                           ontLink,
                           minLinkoSize,
                           maxLinkoSize,
                           stepLinkoSize,
                           modelNum,
                           runNum,
                           precision=2,
                           seeds=None):
    """Generate the stats on link models for a given ontology.

    inputs:

    metric: the function to apply to the generated linkographs.

    ontNext: ontology used to generate Markov model that create the
    next state.

    ontLink: ontology used for constructing linkographs.

    minLinkoSize: the minimun number of nodes in the linkographs to
    consider.

    maxLinkoSize: the maximum number of nodes in the linkographs to
    consider. Note that the max is not included to match pythons
    convertions on lists and ranges.

    stepLinkoSize: the step size between minLinkoSize to maxLinkoSize
    for the number of linkographs to Consider.

    modelNum: the number of models.

    runNum: the number of linkographs to consider for each linkograph
    size.

    precision:  the number of decimals places to use for the Markov
    models.

    seeds: a list of seeds to use for the generated next Markov
    models. The size of the list should be the same as the number of
    runs.

    output:

    a modelNum x number_of _linkographs. The (i, j) entry provides the
    average shannon entropy for the i-th model and j-th size linkgraph
    considered.

    """

    linkoSizes = range(minLinkoSize, maxLinkoSize, stepLinkoSize)

    ontSize = len(ontNext)
    absClasses = list(ontNext.keys())
    absClasses.sort()

    results = np.zeros((modelNum, len(linkoSizes)))

    if seeds is None:
        seeds = [time.time() * i for i in range(modelNum)]

    models = []
    # Create the generating models
    for i in range(modelNum):
        m = markel.genModelFromOntology(ontology=ontNext,
                                        precision=2,
                                        seed=seeds[i])

        # Storing the model and the current state
        models.append(m)

    # For each size linkograph, generate the runNum links and
    # caculate the needed statistics.
    for size in linkoSizes:

        print('size: {0}'.format(size))

        for modelIndex, m in enumerate(models):

            # Collect entropy and complexity.
            metric_values = np.zeros(runNum)

            for i in range(runNum):

                # Randomize the initial state
                m.state = m.random.randint(1, len(m.absClasses)) - 1

                linko = m.genLinkograph(size, ontology=ontLink)

                value = metric(linko)

                metric_values[i] = value

            # Find the mean across the runs.
            index = (size - minLinkoSize) // stepLinkoSize
            results[modelIndex][index] = np.mean(metric_values)

    return results
def genSingleOntologyStats(ontNext,
                           ontLink,
                           minLinkoSize,
                           maxLinkoSize,
                           stepLinkoSize,
                           runNum,
                           precision=2,
                           seeds=None):
    """Generate the stats on link models for a given ontology.

    inputs:

    ontNext: ontology used to generate Markov model that create the
    next state.

    ontLink: ontology used for constructing linkographs.

    minLinkoSize: the minimun number of nodes in the linkographs to
    consider.

    maxLinkoSize: the maximum number of nodes in the linkographs to
    consider. Note that the max is not included to match pythons
    convertions on lists and ranges.

    stepLinkoSize: the step size between minLinkoSize to maxLinkoSize
    for the number of linkographs to Consider.

    runNum: the number of linkographs to consider for each linkograph
    size.

    precision:  the number of decimals places to use for the Markov
    models.

    seeds: a list of seeds to use for the generated next Markov
    models. The size of the list should be the same as the number of
    runs.

    output:

    a numLinkos x ontologySize x ontologySize x 2 array where
    numLinkos is to the floor of ((maxLinkoSize - 1) - minLinkoSize)
    // stepLinkoSize and ontologySize is the size of the ontology used
    by the given model. The first dimension is for the linkograph
    size. For example, an i in this dimension selects the linkograph
    of size minLinkoSize + i*stepLinkoSize. The second and third
    dimensions give the link in the link Markov model. Thus, a (j, k)
    in these two dimensions represent the link (j, k) in the tMatrix
    of the link Markov model. The fourth dimension selects the mean or
    standard deviation. A 0 is the mean and 1 is the standard
    devation. Thus, the (i, j, k, 0) entry is the mean over all the
    links from the ith abstraction class to the jth abstraction class
    for linkNum linkograph of size minLinkoSize + i*stepLinkoSize. A
    similar statement holds for the (i, j, k, 1) and the standard
    deviation.

    """

    linkoSizes = range(minLinkoSize, maxLinkoSize, stepLinkoSize)

    ontSize = len(ontNext)
    absClasses = list(ontNext.keys())
    absClasses.sort()

    results = np.zeros((len(linkoSizes), ontSize, ontSize, 2))

    if seeds is None:
        seeds = [time.time() for i in range(runNum)]

    models = []
    # Create the generating models
    for i in range(runNum):
        m = markel.genModelFromOntology(ontology=ontNext,
                                        precision=2,
                                        seed=seeds[i])

        # Storing the model and the current state
        models.append(m)

    # For each size linkograph, generate the runNum links and
    # caculate the needed statistics.
    for size in linkoSizes:

        # currentModels packs the transition matrix for each run into
        # a single matrix.
        linkModels = np.zeros((ontSize, ontSize, runNum))
        print('size: {0}'.format(size))

        for i in range(runNum):

            m = models[i]

            # Randomize the initial state
            m.state = m.random.randint(1, len(m.absClasses)) - 1

            linko = m.genLinkograph(size, ontology=ontLink)

            newModel = markel.genModelFromLinko(linko,
                                                precision=precision,
                                                ontology=None,
                                                seed=None,
                                                method='link_predictor',
                                                linkNum=1)

            linkModels[:, :, i] = newModel.tMatrix

        # Find the mean of each transition across the different runs.
        index = (size - minLinkoSize) // stepLinkoSize
        results[index, :, :, 0] = np.mean(linkModels, axis=-1)

        # Find the standard deviation across the difference runs.
        results[index, :, :, 1] = np.std(linkModels, axis=-1)

    return results
Exemplo n.º 5
0
    parser.add_argument('--graphGroupSize',
                        type=int,
                        default=10,
                        help='The number of graphs to group.')

    args = parser.parse_args()

    # Extract the ontology
    ont = None
    with open(args.ontology[0], 'r') as ontFile:
        ont = json.load(ontFile)

    seed = int(math.modf(time.time())[0] * (10**7))

    model = markel.genModelFromOntology(ont,
                                        precision=args.precision,
                                        seed=seed)

    results = genSingleOntologyStats(minLinkoSize=args.minimum,
                                     maxLinkoSize=args.maximum,
                                     stepLinkoSize=args.step,
                                     model=model,
                                     runNum=args.runs,
                                     precision=args.precision)

    # Create graphs for each of the transitions
    # legend = []
    # for initAbs in model.absClasses:
    #     for termAbs in model.absClasses:
    #         legend.append(initAbs + ' -> ' + termAbs)