From d3feaece8c0888b95b859565d32c98f80f773f70 Mon Sep 17 00:00:00 2001 From: beckynevin Date: Tue, 23 Jul 2024 10:25:43 -0600 Subject: [PATCH] black and flake8 to models and data --- src/data/data.py | 59 ++++++++++++++++++++++---------------------- src/models/models.py | 21 ++++++++++------ 2 files changed, 42 insertions(+), 38 deletions(-) diff --git a/src/data/data.py b/src/data/data.py index 6055fc4..86e6528 100644 --- a/src/data/data.py +++ b/src/data/data.py @@ -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 = [] @@ -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 @@ -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) @@ -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": @@ -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) @@ -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 diff --git a/src/models/models.py b/src/models/models.py index f55bd09..bb46dd8 100644 --- a/src/models/models.py +++ b/src/models/models.py @@ -72,7 +72,7 @@ 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) @@ -80,7 +80,7 @@ def __init__(self): 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) @@ -88,13 +88,14 @@ def __init__(self): 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) @@ -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 @@ -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