Skip to main content

No project description provided

Project description

Create Activation Graphs from the TG-CNN Model

ABOUT

Tests DOI

Produce graphs using the 3D CNN layers from the trained TG-CNN model. These graphs show which edges or timesteps are the most important during model prediction.

[!IMPORTANT] Please note that the examples provided in this repository are fictitious and do not contain any real patient data.

Activation mapping graphs for edges steps:

The GIF below shows how the filters from the 3D CNN layer are used to show edge activation per input graph:

Edge activation graph

  1. Extract the filters from the 3D CNN layer of the TG-CNN model.
  2. Find the filter with the strongest differentiation of maximum activation between the positive and negative class amongst all the input graphs.
  3. Select the filter with the largest activation difference to show edge activation.
  4. Make the stride length the same as the number of timesteps in the filter.
  5. Do element-wise multiplication between the filter and the input graph, to get the edge activation tensor.
  6. Use the edge activation tensor to get weights for the edges. Edges with zero activation are grey, edges with activation are red. The higher the edge activation weight the thicker the edge.
  7. Observe which edges affect the prediction outcome. Nodes are named as e.g. 5_t1 (Read Code = 5, timestep = 1).

Edge activation graph plot from code


Equation

To get the edge weights: For a given filter $f_{k}$, patient $p$, and time step $i$, the process can be summarised as:

  1. Extract the slice:

    $W_{i}^{(p, k)}$ = $G_{p}[i:i+F, :, :]$,

    where $F$ is the size of the filter in the time dimension, $G_{p}$ as the input tensor for patient $p$, $f_{k}$ as the $k$-th filter, and $W_{i}^{(p, k)}$ is the slice of the tensor $G_{p}$ at time step $i$ with the same dimensions as the filter $f_{k}$.

  2. Apply the filter and sum the result:

    $S_{i}^{(p, k)}$ = $\sum (W_{i}^{(p, k)} \odot f_{k}),$

    where $\odot$ denotes element-wise multiplication.

  3. Apply the leaky ReLU activation function:

    $A_{i}^{(p, k)}$ = $leaky ReLU(S_{i}^{(p, k)}, \alpha),$

    where $\alpha$ is the leaky ReLU parameter.

  4. Compute the maximum activation value ($M$) across all time steps:

    $M_{(p, k)}$ = $\max_{i} (A_{i}^{(p, k)})$

  5. Combine these into one formula:

    $M_{(p, k)}$ = $\max_{i} \left( leaky ReLU \left( \sum (G_{p}[i:i+F, :, :] \odot f_{k}), \alpha \right) \right)$

PROJECT STRUCTURE

The main code is found in the tgcnn_act_graph folder of the repository. See Usage below for more information.

|-- documentation             # Images and other background files
|-- test_graphs               # Tests for tgcnn_act_graph
|-- tgcnn_act_graph           # Source files
|-- create_graphs.ipynb       # Example of how to use this code locally
|-- LICENSE.txt
|-- README.md
|__ requirements.txt          # Which packages are required to run this code

BUILT WITH

Python v3.8

INSTALLATION

To clone the repo:

To clone this repository:

  • Open Git Bash, your Command Prompt or Powershell
  • Navigate to the directory where you want to clone this repository: cd/path/to/directory
  • Run git clone command: git clone https://github.com/ZoeHancox/tgcnn_activation_graphs

To create a suitable environment we suggest using Anaconda:

  • Build conda environment: conda create --name graph_viz python=3.8
  • Activate environment: conda activate graph_viz
  • Install requirements: python -m pip install -r ./requirements.txt

To install the package:

  • Build conda environment: conda create --name graph_viz python=3.8
  • Activate environment: conda activate graph_viz
  • Install package: pip install tgcnn-act-graph

USAGE

See examples in create_graphs.ipynb for how to use this code.

If you have installed the package you can:

from tgcnn_act_graph.figures import edge_activated_graph
import numpy as np

# Load or create your 4D filters
filters = np.array([[[[0, 0], 
                      [0, 1]], [[1, 1], 
                                [0, 0]]], 
                     [[[0, 1], 
                        [0, 1]], [[1, 0], 
                                 [0, 0]]]])

# Load or create your 4D patient graphs
input_tensors = np.array([[[[0, 0], 
                          [0, 3]], [[0, 0],
                                    [4, 0]], [[8, 8], 
                                             [0, 0]]],                        
                        [[[7, 0], 
                          [0, 0]], [[9, 0],
                                    [0, 0]], [[4, 4], 
                                             [0, 0]]]])


labels = [0, 1] # positive or negative labels

edge_activated_graph(input_tensors=input_tensors, patient_number=1,  filters=filters, labels=labels, verbose=False, show_plot=False)

ROADMAP

Features to come:

  • Show edge activation using NetworkX
  • Add a list of your own node names rather than using ints
  • Rules so that only fully connected graphs can be input
  • Show time step activation using NetworkX

SUPPORT

See the Issues in GitHub for a list of proposed features and known issues. Contact Zoe Hancox for further support.

TESTING

Run tests by using pytest test_graphs/test_calculations.py in the top directory.

LICENSE

Unless stated otherwise, the codebase is released under the BSD Licence. This covers both the codebase and any sample code in the documentation.

See LICENCE for more information.

ACKNOWLEDGEMENTS

The TG-CNN model was developed using data provided by patients and collected by the NHS as part of their care and support.

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

tgcnn_act_graph-0.3.1.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

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

tgcnn_act_graph-0.3.1-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file tgcnn_act_graph-0.3.1.tar.gz.

File metadata

  • Download URL: tgcnn_act_graph-0.3.1.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.8.19

File hashes

Hashes for tgcnn_act_graph-0.3.1.tar.gz
Algorithm Hash digest
SHA256 1293a27420020ab032eb217b1d538b8e0dbbcca5dc0cc25cf67908a1569a0190
MD5 5d5f7343f5069d4c200dc56cf0527319
BLAKE2b-256 c6a629f098d354f68167a3686a1921474d324fd227923d116060484d29f489ad

See more details on using hashes here.

File details

Details for the file tgcnn_act_graph-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for tgcnn_act_graph-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4dbf8544ff4b1cb567aaf7c54b1013fcaa26e1573609df20da98ccf444456f3a
MD5 b66032be550e8093b8a2fabf03cdc309
BLAKE2b-256 0a22a005feabc48a63f77567a99b50d74391465a065137ad98f99db9702b277c

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