Skip to content

Commit

Permalink
Update versioning system (#252)
Browse files Browse the repository at this point in the history
* Draft for new versioning scheme

* Update build system

* Testing file

* Fix build error

* Use build

* Python version

* Importlib in Python 3.8

* Add back requirements_test.txt

* Rename

* Bad torch version

* Delete version file

* Ignore version

* No need to build

* Delete test requirements

* Apply isort

* Fix Python version
  • Loading branch information
mmuckley authored Jun 17, 2022
1 parent 265c565 commit 684fb12
Show file tree
Hide file tree
Showing 50 changed files with 211 additions and 178 deletions.
5 changes: 0 additions & 5 deletions .flake8

This file was deleted.

17 changes: 8 additions & 9 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
max-parallel: 4
matrix:
platform: [ubuntu-latest]
python-version: [3.7, 3.8]
python-version: [3.8]

runs-on: ${{ matrix.platform }}

Expand All @@ -28,8 +28,9 @@ jobs:
- name: Install
run: |
python -m pip install --upgrade pip
pip install --upgrade wheel
pip install -e .
pip install --upgrade wheel build setuptools
python -m build .
pip install dist/*.whl
- name: Test Import
run: |
python -c 'import fastmri'
Expand All @@ -40,7 +41,7 @@ jobs:
max-parallel: 4
matrix:
platform: [ubuntu-latest]
python-version: [3.7, 3.8]
python-version: [3.8]

runs-on: ${{ matrix.platform }}

Expand All @@ -65,7 +66,7 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install --upgrade wheel
pip install -r dev-requirements.txt
pip install --editable ".[tests]"
- name: Check Formatting and Lint
run: |
python --version
Expand All @@ -75,10 +76,8 @@ jobs:
mypy fastmri
flake8 --version
flake8 fastmri_examples fastmri tests
- name: Install fastMRI
run: |
python setup.py bdist_wheel
pip install dist/*.whl
isort --version
isort --check-only fastmri tests fastmri_examples
- name: Run pytest
run: |
echo -e "PyTorch \c" && pip show torch | grep Version
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ lightning_logs/
*.pt
build/
dist/
fastmri/_version.py
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ help(SliceDataset)

**Note:** Contributions to the code are continuously tested via GitHub actions.
If you encounter an issue, the best first thing to do is to try to match the
test environments in `requirements.txt` and `dev-requirements.txt`.
`tests` environment in `setup.cfg`, e.g., `pip install --editable ".[tests]"`
when installing from source.

**Note:** As documented in [Issue 215](https://github.com/facebookresearch/fastMRI/issues/215),
there is currently a memory leak when using `h5py` installed from `pip` and
Expand Down
10 changes: 0 additions & 10 deletions dev-requirements.txt

This file was deleted.

16 changes: 9 additions & 7 deletions fastmri/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
LICENSE file in the root directory of this source tree.
"""

__version__ = "0.1.2a20220121"
__author__ = "Facebook/NYU fastMRI Team"
__author_email__ = "[email protected]"
__license__ = "MIT"
__homepage__ = "https://fastmri.org/"
from importlib.metadata import PackageNotFoundError, version

try:
__version__ = version("fastmri")
except PackageNotFoundError:
# package is not installed
import warnings

warnings.warn("Could not retrieve fastmri version!")

import torch
from packaging import version

from .coil_combine import rss, rss_complex
from .fftc import fft2c_new as fft2c
Expand Down
2 changes: 1 addition & 1 deletion fastmri/coil_combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def rss(data: torch.Tensor, dim: int = 0) -> torch.Tensor:
Returns:
The RSS value.
"""
return torch.sqrt((data ** 2).sum(dim))
return torch.sqrt((data**2).sum(dim))


def rss_complex(data: torch.Tensor, dim: int = 0) -> torch.Tensor:
Expand Down
2 changes: 1 addition & 1 deletion fastmri/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
LICENSE file in the root directory of this source tree.
"""

from .mri_data import SliceDataset, CombinedSliceDataset
from .mri_data import CombinedSliceDataset, SliceDataset
from .volume_sampler import VolumeSampler
4 changes: 2 additions & 2 deletions fastmri/data/mri_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ def __init__(
if dataset_cache.get(root) is None and use_dataset_cache:
dataset_cache[root] = self.examples
logging.info(f"Saving dataset cache to {self.dataset_cache_file}.")
with open(self.dataset_cache_file, "wb") as f:
pickle.dump(dataset_cache, f)
with open(self.dataset_cache_file, "wb") as cache_f:
pickle.dump(dataset_cache, cache_f)
else:
logging.info(f"Using dataset cache from {self.dataset_cache_file}.")
self.examples = dataset_cache[root]
Expand Down
3 changes: 2 additions & 1 deletion fastmri/data/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

from typing import Dict, NamedTuple, Optional, Sequence, Tuple, Union

import fastmri
import numpy as np
import torch

import fastmri

from .subsample import MaskFunc


Expand Down
3 changes: 2 additions & 1 deletion fastmri/data/volume_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

import torch
import torch.distributed as dist
from fastmri.data.mri_data import CombinedSliceDataset, SliceDataset
from torch.utils.data import Sampler

from fastmri.data.mri_data import CombinedSliceDataset, SliceDataset


class VolumeSampler(Sampler):
"""
Expand Down
2 changes: 1 addition & 1 deletion fastmri/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def mse(gt: np.ndarray, pred: np.ndarray) -> np.ndarray:

def nmse(gt: np.ndarray, pred: np.ndarray) -> np.ndarray:
"""Compute Normalized Mean Squared Error (NMSE)"""
return np.linalg.norm(gt - pred) ** 2 / np.linalg.norm(gt) ** 2
return np.array(np.linalg.norm(gt - pred) ** 2 / np.linalg.norm(gt) ** 2)


def psnr(
Expand Down
6 changes: 3 additions & 3 deletions fastmri/losses.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def __init__(self, win_size: int = 7, k1: float = 0.01, k2: float = 0.03):
super().__init__()
self.win_size = win_size
self.k1, self.k2 = k1, k2
self.register_buffer("w", torch.ones(1, 1, win_size, win_size) / win_size ** 2)
NP = win_size ** 2
self.register_buffer("w", torch.ones(1, 1, win_size, win_size) / win_size**2)
NP = win_size**2
self.cov_norm = NP / (NP - 1)

def forward(
Expand All @@ -52,7 +52,7 @@ def forward(
A1, A2, B1, B2 = (
2 * ux * uy + C1,
2 * vxy + C2,
ux ** 2 + uy ** 2 + C1,
ux**2 + uy**2 + C1,
vx + vy + C2,
)
D = B1 * B2
Expand Down
4 changes: 2 additions & 2 deletions fastmri/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def complex_abs(data: torch.Tensor) -> torch.Tensor:
if not data.shape[-1] == 2:
raise ValueError("Tensor does not have separate complex dim.")

return (data ** 2).sum(dim=-1).sqrt()
return (data**2).sum(dim=-1).sqrt()


def complex_abs_sq(data: torch.Tensor) -> torch.Tensor:
Expand All @@ -83,7 +83,7 @@ def complex_abs_sq(data: torch.Tensor) -> torch.Tensor:
if not data.shape[-1] == 2:
raise ValueError("Tensor does not have separate complex dim.")

return (data ** 2).sum(dim=-1)
return (data**2).sum(dim=-1)


def tensor_to_complex_np(data: torch.Tensor) -> np.ndarray:
Expand Down
17 changes: 6 additions & 11 deletions fastmri/models/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

import functools
import operator
from typing import List
from typing import Tuple
from typing import List, Tuple

import torch
import torch.nn as nn
Expand Down Expand Up @@ -64,14 +63,10 @@ def forward(self, mask: torch.Tensor, kspace: torch.Tensor):
if self.use_softplus:
# Softplus to make positive
prob_mask = F.softplus(sampler_out, beta=self.slope)
prob_mask = (
prob_mask
/ torch.max(
(1 - mask.reshape(prob_mask.shape[0], prob_mask.shape[1]))
* prob_mask,
dim=1,
)[0].reshape(-1, 1)
)
prob_mask = prob_mask / torch.max(
(1 - mask.reshape(prob_mask.shape[0], prob_mask.shape[1])) * prob_mask,
dim=1,
)[0].reshape(-1, 1)
else:
# Sigmoid to make positive
prob_mask = torch.sigmoid(self.slope * sampler_out)
Expand Down Expand Up @@ -419,7 +414,7 @@ def __init__(
self.down_sample_layers = nn.ModuleList(
[
SingleConvBlock(
chans * 2 ** i,
chans * 2**i,
chans * 2 ** (i + 1),
drop_prob,
pool_size=self.pool_size,
Expand Down
5 changes: 3 additions & 2 deletions fastmri/models/varnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
"""

import math
from typing import List, Tuple, Optional
from typing import List, Optional, Tuple

import fastmri
import torch
import torch.nn as nn
import torch.nn.functional as F

import fastmri
from fastmri.data import transforms

from .unet import Unet
Expand Down
2 changes: 1 addition & 1 deletion fastmri/pl_modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
LICENSE file in the root directory of this source tree.
"""

from .data_module import FastMriDataModule
from .mri_module import MriModule
from .unet_module import UnetModule
from .varnet_module import VarNetModule
from .data_module import FastMriDataModule
7 changes: 4 additions & 3 deletions fastmri/pl_modules/data_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
from pathlib import Path
from typing import Callable, Optional, Union

import fastmri
import pytorch_lightning as pl
import torch

import fastmri
from fastmri.data import CombinedSliceDataset, SliceDataset


Expand Down Expand Up @@ -52,13 +53,13 @@ def worker_init_fn(worker_id):
+ worker_info.id * len(data.datasets)
+ i
)
dataset.transform.mask_func.rng.seed(seed_i % (2 ** 32 - 1))
dataset.transform.mask_func.rng.seed(seed_i % (2**32 - 1))
elif data.transform.mask_func is not None:
if is_ddp: # DDP training: unique seed is determined by worker and device
seed = base_seed + torch.distributed.get_rank() * worker_info.num_workers
else:
seed = base_seed
data.transform.mask_func.rng.seed(seed % (2 ** 32 - 1))
data.transform.mask_func.rng.seed(seed % (2**32 - 1))


def _check_both_not_none(val1, val2):
Expand Down
5 changes: 3 additions & 2 deletions fastmri/pl_modules/mri_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
from argparse import ArgumentParser
from collections import defaultdict

import fastmri
import numpy as np
import pytorch_lightning as pl
import torch
from fastmri import evaluate
from torchmetrics.metric import Metric

import fastmri
from fastmri import evaluate


class DistributedMetricSum(Metric):
def __init__(self, dist_sync_on_step=True):
Expand Down
3 changes: 2 additions & 1 deletion fastmri/pl_modules/unet_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
from argparse import ArgumentParser

import torch
from fastmri.models import Unet
from torch.nn import functional as F

from fastmri.models import Unet

from .mri_module import MriModule


Expand Down
3 changes: 2 additions & 1 deletion fastmri/pl_modules/varnet_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

from argparse import ArgumentParser

import fastmri
import torch

import fastmri
from fastmri.data import transforms
from fastmri.models import VarNet

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import numpy as np
import pytorch_lightning as pl
import torch
from pl_modules import AdaptiveVarNetModule, VarNetModule
from subsample import create_mask_for_mask_type

from fastmri import evaluate
from fastmri.data.mri_data import fetch_dir
from fastmri.data.transforms import MiniCoilTransform
from fastmri.pl_modules import FastMriDataModule

from pl_modules import AdaptiveVarNetModule, VarNetModule
from subsample import create_mask_for_mask_type


def str2bool(v):
if isinstance(v, bool):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
from collections import defaultdict
from typing import Tuple

import fastmri
import numpy as np
import torch

import fastmri
from fastmri import evaluate
from fastmri.data import transforms
from fastmri.models import AdaptiveVarNet
Expand Down
5 changes: 3 additions & 2 deletions fastmri_examples/adaptive_varnet/pl_modules/varnet_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
from collections import defaultdict
from typing import Optional

import fastmri
import numpy as np
import torch
import torch.nn as nn

import fastmri
from fastmri import evaluate
from fastmri.data import transforms
from fastmri.data.transforms import VarNetSample
from fastmri.models.adaptive_varnet import AdaptiveSensitivityModel, AdaptiveVarNetBlock
from fastmri.models.varnet import NormUnet
from fastmri.pl_modules.mri_module import MriModule

from .metrics import DistributedMetricSum, DistributedArraySum
from .metrics import DistributedArraySum, DistributedMetricSum


class VarNet(nn.Module):
Expand Down
1 change: 1 addition & 0 deletions fastmri_examples/adaptive_varnet/subsample.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Optional, Sequence

import numpy as np

from fastmri.data.subsample import MaskFunc, RandomMaskFunc


Expand Down
Loading

0 comments on commit 684fb12

Please sign in to comment.