Skip to main content

A Python package for computing krippendorffs alpha for graph (modified from https://github.com/grrrr/krippendorff-alpha/blob/master/krippendorff_alpha.py)

Project description

Krippendorff-alpha-for-graph

Compute Krippendorff's alpha for graph, modified from https://github.com/grrrr/krippendorff-alpha/

Changes

  1. Used Networkx to instantiate graph
  2. Added custom node/edge and graph metrics (see below)
  3. Forced a pre-computation of distance matrix to boost efficiency for computing, and store it as .npy
    • within-units disagreement (Do)
    • within- and between-units expected total disagreement (De)
  4. Not properly tested, but as long as you have a pandas dataframe that satisfies the following shape, it works.
    • the df has a feature column storing annotated graphs (list of tuples, such as [("subject_1", "predicate_1", "object_1"), ("subject_2", "predicate_2", "object_2")])
    • feature column can also be nodes or edges (tuple of strings)
    • a column indicating annotator id
    • annotation id is ordered the same way for all annotator
  5. Note that, distance metric interacts with the networkx graph type when calling instantiate_networkx_graph(). There are the following graph types,
    • nx.Graph
    • nx.DiGraph
    • nx.MultiGraph
    • nx.MultiDiGraph
  6. Two categories of distance metric are implemented.
    • Lenient metric: node/edge or graph overlap
    • Strict metric: nominal metric, graph edit distance
  7. Depending on your how many graphs you have, computation of graph distance matrix can take a long time.

Python installation

Open your terminal, activate your preferred environment, then type in

pip install krippendorff_graph

Node/edge Metrics

Lenient metric

  1. Node overlap metric: if two sets of nodes or edges overlap, the distance between these two sets is 0; else 1.

Strict metric

  1. Nominal metric: exact match of two sets of ndoes or edges.

Graph Metrics

Lenient metric

  1. Graph overlap metric: if two graphs overlap, the distance between these two sets is 0; else 1.

Strict metric

  1. Normalized graph edit distance
    • normalized by computing distance between g1 and g0 and between g2 and g0
    • g0 is an empty graph

Example Usage

Compute distance matrix of graphs
import pandas as pd
from krippendorff_graph import compute_alpha, compute_distance_matrix, graph_edit_distance, graph_overlap_metric, nominal_metric

df = pd.DataFrame.from_dict({"annotator": [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4],
                             "narrative": [
                                       ["bla, ela, pla, mla."],["bla, ela, pla, mla."],["bla, ela, pla, mla."],["bla, ela, pla, mla."],
                                       ["bla, ela, pla, mla."],["bla, ela, pla, mla."],["bla, ela, pla, mla."],["bla, ela, pla, mla."], 
                                       ["bla, ela, pla, mla."],["bla, ela, pla, mla."],["bla, ela, pla, mla."],["bla, ela, pla, mla."],
                                       ["bla, ela, pla, mla."],["bla, ela, pla, mla."],["bla, ela, pla, mla."],["bla, ela, pla, mla."]
                             ],
                             "graph_feature": [
                                       {("sub", "pre", "obj")},{("sub1", "pre1", "obj1"), ("sub2", "pre2", "obj2")},{("sub", "pre", "obj")},{("sub", "pre", "obj")},
                                       *,{("sub", "pre", "obj")},{("sub", "pre", "obj")},{("sub", "pre", "obj")}, 
                                       {("sub", "pre", "obj")},{("sub1", "pre1", "obj1"), ("sub2", "pre2", "obj2")},{("sub", "pre", "obj")},{("sub1", "pre1", "obj1"), ("sub2", "pre2", "obj2")},
                                       *,{("sub", "pre", "obj")},{("sub", "pre", "obj")},{("sub1", "pre1", "obj1"), ("sub2", "pre2", "obj2")}
                             ]
                             })
data = [
    df[df["annotator"]==1].graph_feature.to_list(),
    df[df["annotator"]==2].graph_feature.to_list(),
    df[df["annotator"]==3].graph_feature.to_list(),
    df[df["annotator"]==4].graph_feature.to_list()
]

empty_graph_indicator = "*" # indicator for missing values
save_path = "./lenient_distance_matrix.npy"
feature_column="graph_feature"

graph_distance_metric= node_overlap_metric
forced = True

if not Path(save_path).exists() or forced:
    distance_matrix = compute_distance_matrix(df_task2_annotation, feature_column=feature_column, graph_distance_metric=graph_distance_metric, 
                                              empty_graph_indicator=empty_graph_indicator, save_path=save_path, graph_type=nx.Graph)
else: 
    distance_matrix = np.load(save_path)
    
print("Lenient node metric: %.3f" % compute_alpha(data, distance_matrix=distance_matrix, missing_items=empty_graph_indicator))

(Please help contributing by making a PR - it will be faster than reporting an issue since the maintainer might be slower than you.)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

krippendorff_graph-0.1.4.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

krippendorff_graph-0.1.4-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file krippendorff_graph-0.1.4.tar.gz.

File metadata

  • Download URL: krippendorff_graph-0.1.4.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.21

File hashes

Hashes for krippendorff_graph-0.1.4.tar.gz
Algorithm Hash digest
SHA256 756628d4f529465967b0de184e6873f55a3e0e8e3acac0fa97d296108d0a0b27
MD5 6acb1579b6b078f9dbde3f7d7e3889d3
BLAKE2b-256 7ffa7643443c751d4833939b00552e54a7011ff36d67bb73d1e7aeefa3b13b32

See more details on using hashes here.

File details

Details for the file krippendorff_graph-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for krippendorff_graph-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 87d3142bf81d2df8ac4273c4d338a94c0b1e9786864e52f96e5f6ac4261e9ea0
MD5 f13695e45e07d27851a8d745c886c165
BLAKE2b-256 b80c995d02539ff0220f0e600e84e9d3e799792ec18e2c6bffd94ea7de40e19b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page