Skip to content

Commit

Permalink
black and flake8 to models and data
Browse files Browse the repository at this point in the history
  • Loading branch information
beckynevin committed Jul 23, 2024
1 parent c4583a0 commit d3feaec
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 38 deletions.
59 changes: 29 additions & 30 deletions src/data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,18 @@ def image_gen(
noise_level=noise_level,
ellipse=0.5,
theta=theta,
radius=radius
).create_object(
center_x=center_x,
center_y=center_y
)
radius=radius,
).create_object(center_x=center_x, center_y=center_y)
return image

def simulate_data_2d(self,
size_df,
params,
sigma,
image_size=32,
inject_type="predictive",
):
def simulate_data_2d(
self,
size_df,
params,
sigma,
image_size=32,
inject_type="predictive",
):
image_size = 32
image_array = np.zeros((size_df, image_size, image_size))
total_brightness = []
Expand All @@ -145,15 +143,17 @@ def simulate_data_2d(self,
center_x=16,
center_y=16,
theta=params[i, 2],
noise_level=0)
noise_level=0,
)
if inject_type == "predictive":
image_array[i, :, :] = image
total_brightness.append(
np.sum(image) + np.random.normal(
loc=0, scale=sigma))
np.sum(image) + np.random.normal(loc=0, scale=sigma)
)
elif inject_type == "feature":
noisy_image = image + np.random.normal(
loc=0, scale=sigma, size=(image_size, image_size))
loc=0, scale=sigma, size=(image_size, image_size)
)
image_array[i, :, :] = noisy_image
total_brightness.append(np.sum(image))
# we'll need the noisy image summed if we want to
Expand Down Expand Up @@ -268,19 +268,18 @@ def simulate_data(
with noise injected type: {inject_type}."
)

def sample_params_from_prior(self,
n_samples,
low=[0, -10],
high=[10, 10],
n_params=2,
seed=42):
assert len(low) == len(high) == n_params, \
"the length of the bounds must match that of the n_params"
def sample_params_from_prior(
self, n_samples, low=[0, -10], high=[10, 10], n_params=2, seed=42
):
assert (
len(low) == len(high) == n_params
), "the length of the bounds must match that of the n_params"
low_bounds = torch.tensor(low, dtype=torch.float32)
high_bounds = torch.tensor(high, dtype=torch.float32)
rs = np.random.RandomState(seed) # 2147483648)#
prior = rs.uniform(
low=low_bounds, high=high_bounds, size=(n_samples, n_params))
low=low_bounds, high=high_bounds, size=(n_samples, n_params)
)
"""
the prior way of doing this (lol)
#print(np.shape(prior), prior)
Expand Down Expand Up @@ -327,14 +326,14 @@ def get_sigma(noise, inject_type="predictive", data_dimension="0D"):
sigma = 100
else:
print("cannot find a match for this noise", noise)
elif inject_type == "feature" and data_dimension=="0D":
elif inject_type == "feature" and data_dimension == "0D":
if noise == "low":
sigma = 1 / 5
elif noise == "medium":
sigma = 5 / 5
elif noise == "high":
sigma = 10 / 5
elif inject_type == "feature" and data_dimension=="2D":
elif inject_type == "feature" and data_dimension == "2D":
if noise == "low":
sigma = 1 / np.sqrt(32)
elif noise == "medium":
Expand All @@ -346,8 +345,8 @@ def get_sigma(noise, inject_type="predictive", data_dimension="0D"):
def normalize(inputs, ys_array, norm=False):
if norm:
# normalize everything before it goes into a network
inputmin = np.min(inputs)#, axis=0)
inputmax = np.max(inputs)#, axis=0)
inputmin = np.min(inputs) # , axis=0)
inputmax = np.max(inputs) # , axis=0)
outputmin = np.min(ys_array)
outputmax = np.max(ys_array)
model_inputs = (inputs - inputmin) / (inputmax - inputmin)
Expand All @@ -357,7 +356,7 @@ def normalize(inputs, ys_array, norm=False):
"inputmin": inputmin,
"inputmax": inputmax,
"outputmin": outputmin,
"outputmax": outputmax
"outputmax": outputmax,
}
else:
normalization_params = None
Expand Down
21 changes: 13 additions & 8 deletions src/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,30 @@ def __init__(self):
super(ConvLayers, self).__init__()
# a little strange = # of filters, usually goes from small to large
# double check on architecture decisions
'''
"""
self.conv1 = nn.Conv2d(1, 10, kernel_size=3, padding=1)
self.conv2 = nn.Conv2d(10, 10, kernel_size=3, padding=1)
self.pool1 = nn.AvgPool2d(kernel_size=2, stride=2, padding=1)
self.conv3 = nn.Conv2d(10, 10, kernel_size=3, padding=1)
self.pool2 = nn.AvgPool2d(kernel_size=2, stride=2, padding=1)
self.conv4 = nn.Conv2d(10, 5, kernel_size=3, padding=1)
self.conv5 = nn.Conv2d(5, 5, kernel_size=3, padding=1)
'''
"""
self.conv1 = nn.Conv2d(1, 5, kernel_size=7, padding=1)
self.conv2 = nn.Conv2d(5, 5, kernel_size=7, padding=1)
self.pool1 = nn.AvgPool2d(kernel_size=2, stride=2, padding=1)
self.conv3 = nn.Conv2d(5, 5, kernel_size=5, padding=1)
self.pool2 = nn.AvgPool2d(kernel_size=2, stride=2, padding=1)
self.conv4 = nn.Conv2d(5, 10, kernel_size=3, padding=1)
self.conv5 = nn.Conv2d(10, 10, kernel_size=3, padding=1)

self.flatten = nn.Flatten()

def forward(self, x):
assert x.dim() != 2, \
f"should enter here with a dimension of at least 3, " \
assert x.dim() != 2, (
f"should enter here with a dimension of at least 3, "
f"{x.dim()}, {x.shape}"
)
if x.dim() == 3: # Check if the input is of shape (batchsize, 32, 32)
# Add channel dimension, becomes (batchsize, 1, 32, 32)
x = x.unsqueeze(1)
Expand Down Expand Up @@ -171,7 +172,8 @@ def model_setup_DE(loss_type, DEVICE, n_hidden=64, data_type="0D"):
# model = de_var().to(DEVICE)
Layer = MuVarLayer
lossFn = torch.nn.GaussianNLLLoss(
full=False, eps=1e-06, reduction="mean")
full=False, eps=1e-06, reduction="mean"
)
if loss_type == "bnll_loss":
# model = de_var().to(DEVICE)
Layer = MuVarLayer
Expand Down Expand Up @@ -255,8 +257,11 @@ def loss_sder(y, y_pred, coeff):
)
u_ep = 1 / np.sqrt(nu.detach().numpy())

return torch.mean(torch.log(var) + (1.0 + coeff * nu) * error**2 / var), \
u_al, u_ep
return (
torch.mean(torch.log(var) + (1.0 + coeff * nu) * error**2 / var),
u_al,
u_ep,
)


# from martius lab
Expand Down

0 comments on commit d3feaec

Please sign in to comment.