No project description provided
Project description
Create Activation Graphs from the TG-CNN Model
ABOUT
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:
- Extract the filters from the 3D CNN layer of the TG-CNN model.
- Find the filter with the strongest differentiation of maximum activation between the positive and negative class amongst all the input graphs.
- Select the filter with the largest activation difference to show edge activation.
- Make the stride length the same as the number of timesteps in the filter.
- Do element-wise multiplication between the filter and the input graph, to get the edge activation tensor.
- 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.
- Observe which edges affect the prediction outcome. Nodes are named as e.g. 5_t1 (Read Code = 5, timestep = 1).
Equation
To get the edge weights: For a given filter $f_{k}$, patient $p$, and time step $i$, the process can be summarised as:
-
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}$.
-
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.
-
Apply the leaky ReLU activation function:
$A_{i}^{(p, k)}$ = $leaky ReLU(S_{i}^{(p, k)}, \alpha),$
where $\alpha$ is the leaky ReLU parameter.
-
Compute the maximum activation value ($M$) across all time steps:
$M_{(p, k)}$ = $\max_{i} (A_{i}^{(p, k)})$
-
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
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
- 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tgcnn_act_graph-0.3.tar.gz.
File metadata
- Download URL: tgcnn_act_graph-0.3.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3014b006c82e5a3caf7fd215dbbb5b163654dd9754d7ab0c58b64ffcf1a39d85
|
|
| MD5 |
23ee43d7596897c7ee59951480ade6c8
|
|
| BLAKE2b-256 |
cd9bb375a2127110d321d4299d6670d1924bbabe34977491842c9b4b8b4b138e
|
File details
Details for the file tgcnn_act_graph-0.3-py3-none-any.whl.
File metadata
- Download URL: tgcnn_act_graph-0.3-py3-none-any.whl
- Upload date:
- Size: 13.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.8.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0682b8c38ac33ad73edf1c09ee0f4e8b33e799f67e5e959261bacf9140d5c758
|
|
| MD5 |
1d1f1455f607e00bc773a8f03577a302
|
|
| BLAKE2b-256 |
3fb2a458c58acbef116d9a13662c28fdcc4163af8759a9e3ff6d5781b6cc2c74
|