Skip to content

Updates for newest scipy and numpy #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
1a694ad
Update setup.py
Andrewwango Jul 10, 2024
b0ecd4b
Update README.md
Andrewwango Jul 10, 2024
f6023e8
Update utility.py
Andrewwango Jul 10, 2024
595449a
Update expm.py
Andrewwango Jul 10, 2024
2d2a1c6
Update functions.py
Andrewwango Jul 10, 2024
2cf086d
Update functions.py
Andrewwango Jul 11, 2024
26f63d3
Update cpab.py
Andrewwango Jul 11, 2024
5e753b1
Update cpab.py
Andrewwango Jul 11, 2024
57ab649
Update cpab.py
Andrewwango Jul 11, 2024
e76d6cc
Update cpab.py
Andrewwango Jul 11, 2024
66a3384
Update functions.py
Andrewwango Jul 11, 2024
1eddac2
Update functions.py
Andrewwango Jul 11, 2024
e4ddc3b
Delete data directory
Andrewwango Dec 8, 2024
03c0f82
Delete examples directory
Andrewwango Dec 8, 2024
7ea2866
Delete libcpab/tensorflow directory
Andrewwango Dec 8, 2024
5077d38
remove numpy, alignment and seqential
Andrewwango Dec 8, 2024
5e8f0e5
backend pytorch only
Andrewwango Dec 8, 2024
daa940f
remove main
Andrewwango Dec 8, 2024
6fcd0c9
simplify functions, remove backends and remove utilities
Andrewwango Dec 8, 2024
02dc248
remove core folder
Andrewwango Dec 8, 2024
6719f38
move Cpp and Cuda files to one folder
Andrewwango Dec 8, 2024
edbd830
move pytorch files out of folder
Andrewwango Dec 8, 2024
446968a
fix refs
Andrewwango Dec 8, 2024
13b47ec
move c files
Andrewwango Dec 8, 2024
1eedef0
simplify readme and imports
Andrewwango Dec 8, 2024
b4acdb6
update cuda and c refs
Andrewwango Dec 8, 2024
392181f
cpab basis file ext
Andrewwango Dec 9, 2024
a9b765b
update license
Andrewwango Dec 11, 2024
4fb20ee
prep packaging
Andrewwango Dec 11, 2024
1f59a90
update refs
Andrewwango Dec 11, 2024
4920a10
update gitignore
Andrewwango Dec 11, 2024
ff0dab8
update readme
Andrewwango Dec 11, 2024
d815c0c
update pyproject
Andrewwango Dec 11, 2024
f3c90e6
remove plt.figure from plotting kwargs
Andrewwango Apr 13, 2025
d1b5e6c
0.1.2
Andrewwango Apr 13, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
.spyproject
__pycache__
basis_files/
ana_small.pkl
*.cpab_basis
*.so
*.o
*.pyc

dist/*
*.egg-info
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright (c) 2016 Nicki Skafte Detlefsen.
Each contributor holds copyright over his own contributions. The project
versioning keep tracks of such information.

By contributing to the ddtn repository, the contributor releases their
By contributing to the libcpab repository, the contributor releases their
content to the license and copyright terms herein.

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
163 changes: 7 additions & 156 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,162 +1,13 @@
# libcpab
CPAB Transformations [1]: finite-dimensional spaces of simple, fast, and
highly-expressive diffeomorphisms derived from parametric,
continuously-defined, velocity fields in **Numpy**, **Tensorflow** and **Pytorch**.

The main idea behind this library is to offer a simple way to use diffiomorphic
transformations and incorporate them into existing software. The diffiomorphic
transformations are based on the work of
[Freifeld et al.](https://www.cs.bgu.ac.il/~orenfr/papers/freifeld_etal_PAMI_2017).
The library supports diffiomorphic transformations in 1D (time series),
2D (images) and 3D (volumetric images).
Maintained fork of libcpab for PyTorch.

This code is based on the original implementation CPAB transformations by
Oren Freifeld (Github repo: [cpabDiffeo](https://github.com/freifeld/cpabDiffeo)).
Install with `pip`: `pip install libcpab`

<p float="center">
<img src="data/cat.jpg" width="280" />
<img src="data/velocity_field.jpg" width="280" />
<img src="data/deform_cat.jpg" width="280" />
</p>
This fork of [libcpab](https://github.com/SkafteNicki/libcpab):

## Author of this software
- Removes numpy and tensorflow backends to only allow pytorch;
- Simplifies file structure;
- Maintenance: remove deprecated numpy and scipy dependencies and update to latest functions.

Nicki Skafte Detlefsen (email: [email protected])

Thanks to Tobias Slott Jensen and Asger Ougaard for suppling the base code
for the 1D and 3D cases. Thanks to Pola Schwoebel for suppling one of the demos.

This software is released under the MIT License (included with the software).
Note, however, that using this code (and/or the results of running it) to support
any form of publication (e.g., a book, a journal paper, a conference papar, a patent
application ect.) we request you to cite [1] and [2].

## Requirements

* Generic python packages: numpy, scipy, matplotlib, pickle
* To use the Tensorflow backend, install [tensorflow](https://www.tensorflow.org/install/)
* To use the Pytorch backend, install [pytorch](https://pytorch.org/)

To use the GPU implementation (tensorflow or pytorch backend), you need a nvidia
GPU and CUDA + cuDNN installed. I recommend going through these
[installation instructions](https://www.tensorflow.org/install/) for tensorflow.
If you can get tensorflow working on your machine, pytorch should also work.

## Installation

Clone this reposatory to a directory of your choice
```
git clone https://github.com/SkafteNicki/libcpab
```
Add this directory to your PYTHONPATH
```
export PYTHONPATH=$PYTHONPATH:$YOUR_FOLDER_PATH/libcpab
```

or use the supplied setup script
```
python setup.py install
```

### Note to ensorflow users
To run the fast c++ or cuda version using the tensorflow backend, you probably
need to compile the source files yourself into a dynamic library. The repo only contains
the dynamic library compiled on my own desktop, so there is a good chance it wont
work on yours. After installing the library navigate to ```libcpab/libcpab/tensorflow```
folder and type ```make```. This requires you to have a newer version of ```gcc``` and ```nvcc``` installed.

## How to use
The interface is simple to use and only have a couple of different methods that
should get you started with diffiomorphic transformations. You have the choice
to choose between a numpy, tensorflow or pytorch backend. Regardless of backend
the main class to interact with is imported as

```
from libcpab import cpab
T = cpab(tess_size, backend, device, zero_boundary, volume_perservation)
```

The arguments are:
* tess_size: list, with the number of cells in each dimension
* backend: string, computational backend to use. Choose between "numpy" (default),
"pytorch" or "tensorflow"
* device: string, either "cpu" (default) or "gpu". For the numpy backend only
the "cpu" option is valid
* zero_boundary: bool, determines is the velocity at the boundary is zero
* volume_perservation: bool, determine if the transformation is volume perservating

The class have a number of methods

```
# Import library
from libcpab import Cpab

# Define a transformation class
T = Cpab(tess_size, backend, device, zero_boundary, volume_perservation)

# Important methods
g = T.uniform_meshgrid(...) # sample uniform grid of points in transformer domain
theta = T.sample_transformation(...) # sample random normal transformation vectors
theta = T.identity(...) # get the identity transformation
dim = T.get_theta_size() # get dimensionality of transformation parametrization
params = T.get_params() # get different transformer parameters
basis = T.get_basis() # get the basis for the transformation
g_t = T.transform_grid(g, theta) # transform a grid of points using theta
data_t1 = T.interpolate(data, g_t) # interpolate some data using the transformed grid
data_t2 = T.transform_data(data, theta) # combination of the two last methods
plot1 = T.visualize_vectorfield(theta) # visualize the vectorfield of a given parametrization
plot2 = visualize_tesselation(...) # visualize the chosen tesselation
```

Additionally we supply two other main classes, a ```CpabSequential``` class that can be used to efficiently
do multiple wraps in a sequential manner and a ```CpabAlignment``` class for easy use of the CPAB-transformations
to do data alignment.

We supply 4 demo files::
* demo1.py: simple use of the library to transform data
* demo2.py: demonstration of the ```CpabSequential``` class.
* demo3.py: demonstration of the ```CpabAlignment``` class.
* time_demo.ipyht: a jupyter notebook, that goes through a data analysis case using libcpab

For a specific use of the transformations in a greater context,
see this [paper](http://www2.compute.dtu.dk/~sohau/papers/cvpr2018/detlefsen_cvpr_2018.pdf)[3]
and this [github repo](https://github.com/SkafteNicki/ddtn).

## References
```
[1] @article{freifeld2017transformations,
title={Transformations Based on Continuous Piecewise-Affine Velocity Fields},
author={Freifeld, Oren and Hauberg, Soren and Batmanghelich, Kayhan and Fisher, John W},
journal={IEEE Transactions on Pattern Analysis and Machine Intelligence},
year={2017},
publisher={IEEE}
}

[2] @misc{detlefsen2018,
author = {Detlefsen, Nicki S.},
title = {libcpab},
year = {2018},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {\url{https://github.com/SkafteNicki/libcpab}},
}

[3] @article{detlefsen2018transformations,
title = {Deep Diffeomorphic Transformer Networks},
author = {Nicki Skafte Detlefsen and Oren Freifeld and S{\o}ren Hauberg},
journal = {Conference on Computer Vision and Pattern Recognition (CVPR)},
year={2018},
publisher={IEEE}
}

```

## Versions

* ----------, Version 2.0 - Major overhaul of the library, many changes
* 05/12/2018, Version 1.5 - Fix the broken 3D pipline
* 15/11/2018, Version 1.4 - Various bugfixes
* 30/10/2018, Version 1.3 - Numpy backend support
* 22/10/2018, Version 1.2 - Various bugfixes
* 28/09/2018, Version 1.1 - Pytorch backend support
* 23/08/2018, Version 1.0 - First working version of libcpab
Requirements remain as `numpy`, `scipy`, `matplotlib` and `pytorch`.
Binary file removed data/BirdChicken
Binary file not shown.
Binary file removed data/cat.jpg
Binary file not shown.
Binary file removed data/deform_cat.jpg
Binary file not shown.
Binary file removed data/velocity_field.jpg
Binary file not shown.
Loading