Exemplo n.º 1
0
def render_hausdorff_distance(shape_distance_id):
    Glyph1 = Glyph.alias()
    Glyph2 = Glyph.alias()
    query = (ShapeDistance
                    .select()
                    .join(Glyph1, on=ShapeDistance.glyph1)
                    .switch(ShapeDistance)
                    .join(Glyph2, on=ShapeDistance.glyph2)
                    .where(
                        ShapeDistance.id == shape_distance_id))
    result = query[0]
    points1 = json.loads(result.points1)
    points2 = json.loads(result.points2)
    bitmap1 = result.glyph1.bitmap
    bitmap2 = result.glyph2.bitmap
    
    bitmap = visualization.render_distance_overlay(
        result.glyph1.character, 
        result.glyph2.character, 
        bitmap1,
        bitmap2,
        points1,
        points2,
        result.distance,
        result.distance)
    
    return bitmap
Exemplo n.º 2
0
def get_correlation(glyph_set_id, sound_metric, shape_metric):
    # Fetch from db if it's already calculated
    query = (Correlation.select().where(
        (Correlation.glyph_set_id == glyph_set_id)
        & (Correlation.sound_metric == sound_metric)
        & (Correlation.shape_metric == shape_metric)))
    if len(query) > 0:
        return query.first()

    sound_query = (SoundDistance.select().where(
        SoundDistance.metric == sound_metric).order_by(SoundDistance.char1,
                                                       SoundDistance.char2))

    Glyph1 = Glyph.alias()
    Glyph2 = Glyph.alias()
    shape_query = (ShapeDistance.select().join(
        Glyph1, on=ShapeDistance.glyph1).switch(ShapeDistance).join(
            Glyph2, on=ShapeDistance.glyph2).where(
                (Glyph1.glyph_set_id == glyph_set_id)
                & (Glyph2.glyph_set_id == glyph_set_id)
                & (ShapeDistance.metric == shape_metric)).order_by(
                    Glyph1.character, Glyph2.character))

    sound_distances = [s.distance for s in sound_query]
    shape_distances = [s.distance for s in shape_query]

    if (len(sound_distances) != len(shape_distances)):
        raise Exception(
            "Numer of shape ({0}) and sound ({1}) distances are not equal for glyph set {2}, sound metric {3}, shape metric {4}"
            .format(len(shape_distances), len(sound_distances), glyph_set_id,
                    sound_metric, shape_metric))

    if np.std(shape_distances) == 0:
        raise Exception(
            "Unable to calculate correlation for glyph set {0}: standard deviation of shape distances is zero."
            .format(glyph_set_id))

    corr_value = pearsonr(shape_distances, sound_distances)

    correlation = Correlation(glyph_set=glyph_set_id,
                              shape_metric=shape_metric,
                              sound_metric=sound_metric,
                              r_value=corr_value[0],
                              p_value=corr_value[1])
    correlation.save()

    return correlation
Exemplo n.º 3
0
def get_and_save_shape_distances(glyph_set_id):
    glyph_query = Glyph.select().where(Glyph.glyph_set_id == glyph_set_id)
    glyphs = [glyph for glyph in glyph_query]

    # Get existing glyph distances
    Glyph1 = Glyph.alias()
    Glyph2 = Glyph.alias()

    shape_query = (ShapeDistance.select().join(
        Glyph1, on=ShapeDistance.glyph1).switch(ShapeDistance).join(
            Glyph2, on=ShapeDistance.glyph2).where(
                (Glyph1.glyph_set_id == glyph_set_id)
                & (Glyph2.glyph_set_id == glyph_set_id)))
    if len(shape_query) > 0:
        # distances already calculated, return existing values
        return [s for s in shape_query]

    shape_distances = get_shape_distances(glyphs)

    with data.db.atomic():
        ShapeDistance.bulk_create(shape_distances, batch_size=100)

    return shape_distances