working Scaling
This commit is contained in:
parent
ba3e6d1e52
commit
f32fe95d55
3 changed files with 48 additions and 11 deletions
31
CustomScaler.py
Normal file
31
CustomScaler.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
from typing import List
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
class CustomScaler:
|
||||
def __init__(self):
|
||||
self.means = None
|
||||
self.stds = None
|
||||
|
||||
def fit(self, data: np.ndarray) -> None:
|
||||
self.means = np.mean(data, 0)
|
||||
self.stds = np.std(data, 0)
|
||||
print(self.means)
|
||||
print(self.stds)
|
||||
|
||||
def _check_fitted(self):
|
||||
if (self.means is None) or (self.stds is None):
|
||||
raise Exception("you need to first fit data")
|
||||
|
||||
def transform_data(self, data: np.ndarray) -> np.ndarray:
|
||||
self._check_fitted()
|
||||
return (data - self.means) / self.stds
|
||||
# return data
|
||||
|
||||
def transform_parameters(self, parameters: List) -> List:
|
||||
self._check_fitted()
|
||||
if len(parameters) != len(self.means):
|
||||
raise ValueError("incorrect number of parameters")
|
||||
for index, parameter in enumerate(parameters):
|
||||
yield (parameter - self.means[index]) / self.stds[index]
|
|
@ -1,12 +1,12 @@
|
|||
from numpy import ndarray
|
||||
from numpy import ndarray, save
|
||||
from scipy.interpolate import griddata
|
||||
|
||||
from interpolators.base import BaseInterpolator
|
||||
|
||||
|
||||
class GriddataInterpolator(BaseInterpolator):
|
||||
def __init__(self,*args,**kwargs):
|
||||
super().__init__(*args,**kwargs)
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def interpolate(self, alpha, v, mcode, gamma, wt, wp) -> ndarray:
|
||||
return griddata(self.points, self.values, (alpha, v, mcode, gamma, wt, wp), method="linear")
|
||||
|
|
22
visualize.py
22
visualize.py
|
@ -1,27 +1,33 @@
|
|||
import numpy as np
|
||||
from matplotlib import pyplot as plt
|
||||
|
||||
from CustomScaler import CustomScaler
|
||||
from interpolators.griddata import GriddataInterpolator
|
||||
from interpolators.rbf import RbfInterpolator
|
||||
from simulation_list import SimulationList
|
||||
|
||||
mcode, gamma, wt, wp = [22.0, 1, 10.0, 10.0]
|
||||
mcode, gamma, wt, wp = [10**21, 1, 10.0, 10.0]
|
||||
simlist = SimulationList.jsonlines_load()
|
||||
simulations = simlist.simlist
|
||||
|
||||
data = np.array([[s.alphacode, s.vcode, s.mcode, s.gammacode, s.wtcode, s.wpcode] for s in simulations])
|
||||
data = np.array([[s.alphacode, s.vcode, 10**s.mcode, s.gammacode, s.wtcode, s.wpcode] for s in simulations])
|
||||
|
||||
scaler = CustomScaler()
|
||||
scaler.fit(data)
|
||||
scaled_data = scaler.transform_data(data)
|
||||
|
||||
values = np.array([s.water_retention_both for s in simulations])
|
||||
|
||||
alpharange = np.linspace(-0.5, 60.5, 100)
|
||||
vrange = np.linspace(0.5, 5.5, 100)
|
||||
grid_alpha, grid_v = np.meshgrid(alpharange, vrange)
|
||||
interpolator = RbfInterpolator(data, values)
|
||||
interpolator = GriddataInterpolator(scaled_data, values)
|
||||
|
||||
# print(grid_alpha)
|
||||
grid_result = interpolator.interpolate(grid_alpha, grid_v, mcode, gamma, wt, wp)
|
||||
print(grid_result[0][30])
|
||||
# print(grid_result.shape)
|
||||
# exit()
|
||||
parameters = [grid_alpha, grid_v, mcode, gamma, wt, wp]
|
||||
scaled_parameters = list(scaler.transform_parameters(parameters))
|
||||
|
||||
grid_result = interpolator.interpolate(*scaled_parameters)
|
||||
|
||||
print(grid_alpha.shape)
|
||||
print(grid_result.shape)
|
||||
plt.title("m={:3.0f}, gamma={:3.1f}, wt={:2.0f}, wp={:2.0f}\n".format(mcode, gamma, wt, wp))
|
||||
|
|
Reference in a new issue