{"id":13562147,"url":"https://github.com/AndreasMadsen/python-textualheatmap","last_synced_at":"2025-04-03T18:32:48.136Z","repository":{"id":57474739,"uuid":"248770939","full_name":"AndreasMadsen/python-textualheatmap","owner":"AndreasMadsen","description":"Create interactive textual heat maps for Jupiter notebooks","archived":false,"fork":false,"pushed_at":"2024-05-30T22:39:21.000Z","size":6542,"stargazers_count":196,"open_issues_count":3,"forks_count":14,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-10-26T22:08:33.067Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AndreasMadsen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-03-20T14:07:29.000Z","updated_at":"2024-09-30T18:31:00.000Z","dependencies_parsed_at":"2024-01-14T03:47:05.991Z","dependency_job_id":"7a2c4175-8c0b-4b3b-95eb-bfa16e7938ef","html_url":"https://github.com/AndreasMadsen/python-textualheatmap","commit_stats":{"total_commits":17,"total_committers":2,"mean_commits":8.5,"dds":0.05882352941176472,"last_synced_commit":"2a6d4ee1c16fd126ed59911e81b6806737ef2108"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndreasMadsen%2Fpython-textualheatmap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndreasMadsen%2Fpython-textualheatmap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndreasMadsen%2Fpython-textualheatmap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndreasMadsen%2Fpython-textualheatmap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AndreasMadsen","download_url":"https://codeload.github.com/AndreasMadsen/python-textualheatmap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223010467,"owners_count":17072992,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T13:01:05.150Z","updated_at":"2024-11-04T14:30:50.049Z","avatar_url":"https://github.com/AndreasMadsen.png","language":"Jupyter Notebook","funding_links":[],"categories":["Jupyter Notebook"],"sub_categories":[],"readme":"# textualheatmap\n\n**Create interactive textual heatmaps for Jupiter notebooks.**\n\nI originally published this visualization method in my distill paper\nhttps://distill.pub/2019/memorization-in-rnns/. In this context, it is used\nas a saliency map for showing which parts of a sentence are used to predict\nthe next word. However, the visualization method is more general-purpose than\nthat and can be used for any kind of textual heatmap purposes.\n\n`textualheatmap` works with python 3.6 or newer and is distributed under the\nMIT license.\n\n![Gif of saliency in RNN models](gifs/show_meta.gif)\n\nAn end-to-end example of how to use the\n[HuggingFace 🤗 Transformers](https://github.com/huggingface/transformers) python\nmodule to create a textual saliency map for how each masked token is predicted.\n\n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/AndreasMadsen/python-textualheatmap/blob/master/notebooks/huggingface_bert_example.ipynb)\n\n\n![Gif of saliency in BERT models](gifs/huggingface_bert.gif)\n\n## Install\n\n```bash\npip install -U textualheatmap\n```\n\n## API\n\n* [`textualheatmap.TextualHeatmap`](textualheatmap/textual_heatmap.py)\n\n## Examples\n\n### Example of sequential-charecter model with metadata visible\n\n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/AndreasMadsen/python-textualheatmap/blob/master/notebooks/general_example.ipynb)\n\n```python\nfrom textualheatmap import TextualHeatmap\n\ndata = [[\n    # GRU data\n    {\"token\":\" \",\n     \"meta\":[\"the\",\"one\",\"of\"],\n     \"heat\":[1,0,0,0,0,0,0,0,0]},\n    {\"token\":\"c\",\n     \"meta\":[\"can\",\"called\",\"century\"],\n     \"heat\":[1,0.22,0,0,0,0,0,0,0]},\n    {\"token\":\"o\",\n     \"meta\":[\"country\",\"could\",\"company\"],\n     \"heat\":[0.57,0.059,1,0,0,0,0,0,0]},\n    {\"token\":\"n\",\n     \"meta\":[\"control\",\"considered\",\"construction\"],\n     \"heat\":[1,0.20,0.11,0.84,0,0,0,0,0]},\n    {\"token\":\"t\",\n     \"meta\":[\"control\",\"continued\",\"continental\"],\n     \"heat\":[0.27,0.17,0.052,0.44,1,0,0,0,0]},\n    {\"token\":\"e\",\n     \"meta\":[\"context\",\"content\",\"contested\"],\n     \"heat\":[0.17,0.039,0.034,0.22,1,0.53,0,0,0]},\n    {\"token\":\"x\",\n     \"meta\":[\"context\",\"contexts\",\"contemporary\"],\n     \"heat\":[0.17,0.0044,0.021,0.17,1,0.90,0.48,0,0]},\n    {\"token\":\"t\",\n     \"meta\":[\"context\",\"contexts\",\"contentious\"],\n     \"heat\":[0.14,0.011,0.034,0.14,0.68,1,0.80,0.86,0]},\n    {\"token\":\" \",\n     \"meta\":[\"of\",\"and\",\"the\"],\n     \"heat\":[0.014,0.0063,0.0044,0.011,0.034,0.10,0.32,0.28,1]},\n    # ...\n],[\n    # LSTM data\n    # ...\n]]\n\nheatmap = TextualHeatmap(\n    width = 600,\n    show_meta = True,\n    facet_titles = ['GRU', 'LSTM']\n)\n# Set data and render plot, this can be called again to replace\n# the data.\nheatmap.set_data(data)\n# Focus on the token with the given index. Especially useful when\n# `interactive=False` is used in `TextualHeatmap`.\nheatmap.highlight(159)\n```\n\n![Shows saliency with predicted words at metadata](gifs/show_meta.gif)\n\n### Example of sequential-charecter model without metadata\n\n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/AndreasMadsen/python-textualheatmap/blob/master/notebooks/general_example.ipynb)\n\nWhen `show_meta` is not `True`, the `meta` part of the `data` object has no effect.\n\n```python\nheatmap = TextualHeatmap(\n    facet_titles = ['LSTM', 'GRU'],\n    rotate_facet_titles = True\n)\nheatmap.set_data(data)\nheatmap.highlight(159)\n```\n\n![Shows saliency without metadata](gifs/no_meta_and_rotated.gif)\n\n### Example of non-sequential-word model\n\n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/AndreasMadsen/python-textualheatmap/blob/master/notebooks/bert_hardcoded_example.ipynb)\n\n`format = True` can be set in the `data` object to inducate tokens that are\nnot directly used by the model. This is useful if word or sub-word tokenization\nis used.\n\n\n```python\ndata = [[\n{'token': '[CLR]',\n 'meta': ['', '', ''],\n 'heat': [1, 0, 0, 0, 0, ...]},\n{'token': ' ',\n 'format': True},\n{'token': 'context',\n 'meta': ['today', 'and', 'thus'],\n 'heat': [0.13, 0.40, 0.23, 1.0, 0.56, ...]},\n{'token': ' ',\n 'format': True},\n{'token': 'the',\n 'meta': ['##ual', 'the', '##ually'],\n 'heat': [0.11, 1.0, 0.34, 0.58, 0.59, ...]},\n{'token': ' ',\n 'format': True},\n{'token': 'formal',\n 'meta': ['formal', 'academic', 'systematic'],\n 'heat': [0.13, 0.74, 0.26, 0.35, 1.0, ...]},\n{'token': ' ',\n 'format': True},\n{'token': 'study',\n 'meta': ['##ization', 'study', '##ity'],\n 'heat': [0.09, 0.27, 0.19, 1.0, 0.26, ...]}\n]]\n\nheatmap = TextualHeatmap(facet_titles = ['BERT'], show_meta=True)\nheatmap.set_data(data)\n```\n\n![Shows saliency in a BERT model, using sub-word tokenization](gifs/sub_word_tokenized.gif)\n\n## Citation\n\nIf you use this in a publication, please cite my [Distill publication](https://distill.pub/2019/memorization-in-rnns/) where I first demonstrated this visualization method.\n\n```bib\n@article{madsen2019visualizing,\n  author = {Madsen, Andreas},\n  title = {Visualizing memorization in RNNs},\n  journal = {Distill},\n  year = {2019},\n  note = {https://distill.pub/2019/memorization-in-rnns},\n  doi = {10.23915/distill.00016}\n}\n```\n\n## Sponsor\n\nSponsored by \u003ca href=\"https://www.nearform.com/research/\"\u003eNearForm Research\u003c/a\u003e.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAndreasMadsen%2Fpython-textualheatmap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAndreasMadsen%2Fpython-textualheatmap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAndreasMadsen%2Fpython-textualheatmap/lists"}