Skip to content
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

Address issue 44 and 63 #64

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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: 6 additions & 1 deletion src/gz21_ocean_momentum/analysis/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import pandas as pd
from analysis.analysis import TimeSeriesForPoint
import xarray as xr
from typing import Optional
from scipy.ndimage import gaussian_filter
from data.pangeo_catalog import get_patch, get_whole_data
from cartopy.crs import PlateCarree
Expand Down Expand Up @@ -165,7 +166,7 @@ def onClick(event):
fig.canvas.mpl_connect("button_press_event", onClick)


def sample(data: np.ndarray, step_time: int = 1, nb_per_time: int = 5):
def sample(data: np.ndarray, step_time: int = 1, nb_per_time: int = 5, random_state: Optional[int] = None):
"""Samples points from the data, where it is assumed that the data
is 4-D, with the first dimension representing time , the second
the channel, and the others representing spatial dimensions.
Expand All @@ -185,6 +186,9 @@ def sample(data: np.ndarray, step_time: int = 1, nb_per_time: int = 5):
:nb_per_time: int,
Number of points used (chosen randomly according to a uniform
distribution over the spatial domain) for each image.

:random_state: int, optional,
Random state used for the random number generator.


Returns
Expand All @@ -194,6 +198,7 @@ def sample(data: np.ndarray, step_time: int = 1, nb_per_time: int = 5):
"""
if data.ndim != 4:
raise ValueError("The data is expected to have 4 dimensions.")
np.random.seed(random_state)
n_times, n_channels, n_x, n_y = data.shape
time_indices = np.arange(0, n_times, step_time)
x_indices = np.random.randint(0, n_x, (time_indices.shape[0], 2, nb_per_time))
Expand Down
2 changes: 2 additions & 0 deletions src/gz21_ocean_momentum/models/fully_conv_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def __init__(
# store in_chans as attribute
self._n_in_channels = in_chans

self._final_transformation = lambda x: x


@staticmethod
def _process_padding(padding: Optional[str] = None) -> Tuple[int, int]:
Expand Down
15 changes: 15 additions & 0 deletions src/gz21_ocean_momentum/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
@author: arthur
"""

import os
import mlflow
from mlflow.tracking import client
import torch
import random
import numpy as np
import pandas as pd
import pickle
import gz21_ocean_momentum.models as models
Expand Down Expand Up @@ -126,3 +130,14 @@ def pickle_artifact(run_id: str, path: str):
file = client.download_artifacts(run_id, path)
f = open(file, "rb")
return pickle.load(f)


def seed_all(seed: int = 0):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
# seed torch
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
2 changes: 1 addition & 1 deletion tests/models/test_fully_conv_net.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from gz21_ocean_momentum.models.fully_conv_net import *
from gz21_ocean_momentum.utils import seed_all
import torch
import numpy as np

Expand All @@ -10,7 +11,6 @@ def test_construct_valid():
Simple check migrated from `models.models1`.
"""
net = FullyCNN()
net._final_transformation = lambda x: x
input_ = torch.randint(0, 10, (17, 2, 35, 30)).to(dtype=torch.float)
input_[0, 0, 0, 0] = np.nan
output = net(input_)
Expand Down