treesimi

Compute similarity between trees, e.g. dependency trees

https://github.com/ulf1/treesimi

Science Score: 36.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
    Links to: zenodo.org
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (6.9%) to scientific vocabulary
Last synced: 10 months ago · JSON representation

Repository

Compute similarity between trees, e.g. dependency trees

Basic Info
  • Host: GitHub
  • Owner: ulf1
  • License: apache-2.0
  • Language: Python
  • Default Branch: main
  • Size: 209 KB
Statistics
  • Stars: 8
  • Watchers: 2
  • Forks: 2
  • Open Issues: 38
  • Releases: 6
Created over 5 years ago · Last pushed about 1 year ago
Metadata Files
Readme Funding License Zenodo

README.md

PyPI version PyPi downloads DOI

treesimi: Shingling for measuring tree similarity

Compute similarity between trees, e.g. dependency trees

Convert an Adjacency List into a Nested Set Table

For example, CoNLL-U's ['id', 'head'] fields form an adjacency list of a dependency tree. Traversing an adjacency list is slower than reading a nested set. Thus, converting a adjacency list to a nested set table once, makes sense if we need to read the three several times lateron.

```py import treesimi as ts adjac = [(1, 0), (2, 1), (3, 1), (4, 2)] nested = ts.adjactonested(adjac)

columns: node id, left, right, depth

[[1, 1, 8, 0], [2, 2, 5, 1], [4, 3, 4, 2], [3, 6, 7, 1]]

```

Demo: Query a nested set table

To extract a subtree we just need to iterate through the list ($O(n)$)

```py _, lft0, rgt0, _ = nested[1] subtree = [(i, l, r, d) for i, l, r, d in nested if (l >= lft0) and (r <= rgt0)]

[[2, 2, 5, 1], [4, 3, 4, 2]]

```

or ts.get_subtree(nested, sub_id=2)

Set node attributes

```py import treesimi as ts nested = [[1, 1, 8, 0], [2, 2, 5, 1], [4, 3, 4, 2], [3, 6, 7, 1]] attrs = [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')] nested = ts.set_attr(nested, attrs)

columns: node id, left, right, depth, attributes

[[1, 1, 8, 0, 'a'], [2, 2, 5, 1, 'b'], [4, 3, 4, 2, 'd'], [3, 6, 7, 1, 'c']]

```

Convert Adjacency List with attributes

```py import treesimi as ts adjac = [(1, 0, 'dat1'), (2, 1, 'dat2'), (3, 1, 'dat3'), (4, 2, 'dat3')] nested = ts.adjactonestedwithattr(adjac)

columns: node id, left, right, depth

[[1, 1, 8, 0, 'dat1'], [2, 2, 5, 1, 'dat2'], [4, 3, 4, 2, 'dat2'], [3, 6, 7, 1, 'dat4']]

```

Extract subtree patterns

We can extract the following patterns from one tree:

  • Depth dimension
    • Full subtrees
    • Truncate leaves
  • Sibling dimension
    • All siblings
    • Drop siblings (and their subtree)
  • Placeholder attribute field

Full subtrees

The function extract_subtrees returns all subtrees of a tree. The depth information is adjusted accordingly for each subtree.

```py import treesimi as ts nested = [[1, 1, 8, 0, 'a'], [2, 2, 5, 1, 'b'], [4, 3, 4, 2, 'd'], [3, 6, 7, 1, 'c']] nested = ts.removenodeids(nested) subtrees = ts.extract_subtrees(nested)

[

[[1, 8, 0, 'a'], [2, 5, 1, 'b'], [3, 4, 2, 'd'], [6, 7, 1, 'c']],

[[1, 4, 0, 'b'], [2, 3, 1, 'd']],

[[1, 2, 0, 'd']],

[[1, 2, 0, 'c']]

]

```

Truncate leaves

In the first step, the function trunc_leaves removes leaves of the largest depth level. The result is always an incomplete tree, and the lft and rgt values are not adjusted to indicate that there is a missing node. In the next steps, the depth level is further removed down to depth=1.

```py import treesimi as ts nested = [[1, 1, 8, 0, 'a'], [2, 2, 5, 1, 'b'], [4, 3, 4, 2, 'd'], [3, 6, 7, 1, 'c']] nested = ts.removenodeids(nested) subtrees = ts.trunc_leaves(nested)

[

[[1, 8, 0, 'a'], [2, 5, 1, 'b'], [6, 7, 1, 'c']]

]

```

Hint: Run trunc_leaves for each subtree extracted by extract_subtrees. Call unique_trees after each step.

Drop sibling nodes

Generate variants of a tree by dropping each node once. Again, the result is always an incomplete tree, and the lft and rgt values are not adjusted to indicate that there is a missing node.

```py import treesimi as ts nested = [[1, 1, 8, 0, 'a'], [2, 2, 5, 1, 'b'], [4, 3, 4, 2, 'd'], [3, 6, 7, 1, 'c']] nested = ts.removenodeids(nested) subtrees = ts.drop_nodes(nested)

[

[[1, 8, 0, 'a']],

[[1, 8, 0, 'a'], [2, 5, 1, 'b']],

[[1, 8, 0, 'a']]

]

```

Hints: Create subtrees with extract_subtrees and trunc_leaves, and run drop_nodes on these subtrees. If you want to drop N nodes/leaves of a tree, then call the function twice, e.g. drop_nodes(drop_nodes(...)).

Placeholder attribute field

The replace_attr removes the data attribute of a node with a generic placeholder.

```py import treesimi as ts nested = [[1, 1, 8, 0, 'a'], [2, 2, 5, 1, 'b'], [4, 3, 4, 2, 'd'], [3, 6, 7, 1, 'c']] nested = ts.removenodeids(nested) subtrees = ts.replace_attr(nested, placeholder='[MASK]')

[

[[1, 8, 0, '[MASK]'], [2, 5, 1, 'b'], [3, 4, 2, 'd'], [6, 7, 1, 'c']],

[[1, 8, 0, 'a'], [2, 5, 1, '[MASK]'], [3, 4, 2, 'd'], [6, 7, 1, 'c']],

[[1, 8, 0, 'a'], [2, 5, 1, 'b'], [3, 4, 2, '[MASK]'], [6, 7, 1, 'c']],

[[1, 8, 0, 'a'], [2, 5, 1, 'b'], [3, 4, 2, 'd'], [6, 7, 1, '[MASK]']]

]

```

Demo Notebooks about Shingling for MinHash

We recommend using the mmh3 hash function, and 32 permutations in datasketch.MinHash.

Start jupyter to run the demo notebook

sh source .venv/bin/activate jupyter lab

Appendix

Installation

The treesimi git repo is available as PyPi package

sh pip install treesimi pip install git+ssh://git@github.com/ulf1/treesimi.git

Commands

Install a virtual environment

sh python3 -m venv .venv source .venv/bin/activate pip install --upgrade pip pip install -r requirements.txt --no-cache-dir pip install -r requirements-dev.txt --no-cache-dir pip install -r requirements-demo.txt --no-cache-dir

(If your git repo is stored in a folder with whitespaces, then don't use the subfolder .venv. Use an absolute path without whitespaces.)

Python commands

  • Check syntax: flake8 --ignore=F401 --exclude=$(grep -v '^#' .gitignore | xargs | sed -e 's/ /,/g')
  • Run Unit Tests: pytest

Publish

sh python setup.py sdist twine upload -r pypi dist/*

Clean up

sh find . -type f -name "*.pyc" | xargs rm find . -type d -name "__pycache__" | xargs rm -r rm -r .pytest_cache rm -r .venv

Support

Please open an issue for support.

Contributing

Please contribute using Github Flow. Create a branch, add commits, and open a pull request.

Acknowledgements

The "Evidence" project was funded by the Deutsche Forschungsgemeinschaft (DFG, German Research Foundation) - 433249742 (GU 798/27-1; GE 1119/11-1).

Maintenance

  • till 31.Aug.2023 (v0.2.0) the code repository was maintained within the DFG project 433249742
  • since 01.Sep.2023 (v0.3.0) the code repository is maintained by Ulf Hamster.

Owner

  • Name: Ulf Hamster
  • Login: ulf1
  • Kind: user

1x developer

GitHub Events

Total
  • Watch event: 1
  • Push event: 5
  • Pull request event: 4
  • Create event: 4
Last Year
  • Watch event: 1
  • Push event: 5
  • Pull request event: 4
  • Create event: 4

Committers

Last synced: over 1 year ago

All Time
  • Total Commits: 102
  • Total Committers: 2
  • Avg Commits per committer: 51.0
  • Development Distribution Score (DDS): 0.029
Past Year
  • Commits: 0
  • Committers: 0
  • Avg Commits per committer: 0.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
UH 5****6@g****m 99
luise koehler k****t@g****e 3
Committer Domains (Top 20 + Academic)
gmx.de: 1

Issues and Pull Requests

Last synced: 11 months ago

All Time
  • Total issues: 68
  • Total pull requests: 96
  • Average time to close issues: about 18 hours
  • Average time to close pull requests: 18 days
  • Total issue authors: 2
  • Total pull request authors: 3
  • Average comments per issue: 0.21
  • Average comments per pull request: 0.04
  • Merged pull requests: 30
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 21
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 1
  • Average comments per issue: 0
  • Average comments per pull request: 0.0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
  • ulf1 (49)
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 35 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 1
  • Total versions: 8
  • Total maintainers: 1
pypi.org: treesimi

Compute similarity between netsted set based trees.

  • Versions: 8
  • Dependent Packages: 0
  • Dependent Repositories: 1
  • Downloads: 35 Last month
Rankings
Dependent packages count: 10.1%
Average: 20.6%
Stargazers count: 21.6%
Dependent repos count: 21.6%
Forks count: 22.7%
Downloads: 26.8%
Maintainers (1)
Last synced: 11 months ago

Dependencies

requirements-demo.txt pypi
  • conllu >=4.2.1
  • datasketch >=1.5.1
  • jupyterlab >=2.2.9
  • matplotlib >=3.3.3
requirements-dev.txt pypi
  • flake8 >=3.8.4 development
  • pypandoc >=1.5 development
  • pytest >=6.2.1 development
  • setuptools >=56.0.0 development
  • twine ==3.3.0 development
  • wheel >=0.31.0 development