Skip to content

Commit

Permalink
Fixed issues with plotting in routine notebook and made sure it works…
Browse files Browse the repository at this point in the history
… correctly
  • Loading branch information
silkemaes committed Oct 25, 2024
1 parent 874e512 commit fe16e5b
Show file tree
Hide file tree
Showing 10 changed files with 275 additions and 35,642 deletions.
17,998 changes: 0 additions & 17,998 deletions data/paths_data_C.txt

This file was deleted.

17,471 changes: 0 additions & 17,471 deletions data/paths_data_O.txt

This file was deleted.

17 changes: 10 additions & 7 deletions input/example.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ nb_epochs = 15
ini_epochs = 5
losstype = abs_idn
z_dim = 8
nb_samples = 4
nb_samples = 10
scheme = int
nb_evol = 16
nb_hidden = 1
ae_type = simple
nb_test = 1
nb_test = 3



Expand All @@ -25,12 +25,15 @@ elm = 0



Name = 20241021_165509

Name = 20241021_165509
Name = 20241025_120806

Name = 20241021_165509
Name = 20241025_120806

Name = 20241021_165509
Name = 20241025_123012

Name = 20241021_165509
Name = 20241025_123835

Name = 20241025_124525

Name = 20241025_125032
29 changes: 18 additions & 11 deletions load&test.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies = [
"scipy",
"torchode"
]
requires-python = ">=3.7"
requires-python = ">=3.8"

[project.optional-dependencies]
dev = ["black", "bumpver", "pip-tools", "pytest"]
Expand Down
359 changes: 222 additions & 137 deletions routine.ipynb

Large diffs are not rendered by default.

34 changes: 20 additions & 14 deletions src/mace/CSE_0D/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CSEdata(Dataset):
More specifically, this Dataset uses 1D CSE models, and splits them in 0D models.
'''
def __init__(self, nb_samples,dt_fract, nb_test, train=True, fraction=0.7, cutoff = 1e-20, scale = 'norm'):
def __init__(self, nb_samples,dt_fract, nb_test, inpackage = False, train=True, datapath = 'train', fraction=0.7, cutoff = 1e-20):
'''
Initialising the attributes of the dataset.
Expand Down Expand Up @@ -92,13 +92,14 @@ def __init__(self, nb_samples,dt_fract, nb_test, train=True, fraction=0.7, cutof
np.random.seed(0)
self.idxs = utils.generate_random_numbers(nb_samples, 0, len(paths))
self.path = paths[self.idxs]
print('Selected paths:', len(self.path))


## select a random test path, that is not in the training set
# self.test_idx = utils.generate_random_numbers(1, 0, len(paths))
self.testpath = list()
# self.testpath.append(paths[self.test_idx][0])
self.nb_test = nb_test
print('number of test paths:', nb_test)
count = 0
while count < nb_test:
self.test_idx = utils.generate_random_numbers(1, 0, len(paths))
Expand All @@ -107,7 +108,6 @@ def __init__(self, nb_samples,dt_fract, nb_test, train=True, fraction=0.7, cutof
self.testpath.append(paths[self.test_idx][0])
print('count:',count, '\r', end = '')
print('Selected test paths:', len(self.testpath))
print('\n')

# print('test path:', self.testpath)

Expand All @@ -134,13 +134,18 @@ def __init__(self, nb_samples,dt_fract, nb_test, train=True, fraction=0.7, cutof
self.cutoff = cutoff
self.fraction = fraction
self.train = train
self.inpackage = inpackage
self.datapath = datapath

## Split in train and test set
N = int(self.fraction*len(self.path))
if self.train:
self.path = self.path[:N]
else:
self.path = self.path[N:]

print('Selected paths:', len(self.path))
print('\n')


def __len__(self):
Expand Down Expand Up @@ -169,7 +174,7 @@ def __getitem__(self, idx):
Returns the preprocessed data in torch tensors.
'''

mod = CSEmod(self.path[idx],inpackage = True, train = True)
mod = CSEmod(self.path[idx], inpackage = self.inpackage, data = self.datapath)

Δt, n, p = mod.split_in_0D()

Expand All @@ -190,7 +195,7 @@ def __getitem__(self, idx):
return torch.from_numpy(n_transf), torch.from_numpy(p_transf), torch.from_numpy(Δt_transf)


def get_data( nb_samples, dt_fract, nb_test, batch_size, kwargs):
def get_data( nb_samples, dt_fract, nb_test,inpackage, batch_size, kwargs):
'''
Prepare the data for training and validating the emulator.
Expand All @@ -206,8 +211,8 @@ def get_data( nb_samples, dt_fract, nb_test, batch_size, kwargs):
kwargs = {'num_workers': 1, 'pin_memory': True} for the DataLoader
'''
## Make PyTorch dataset
train = CSEdata(nb_samples=nb_samples, dt_fract=dt_fract, nb_test = nb_test , train = True)
valid = CSEdata(nb_samples=nb_samples, dt_fract=dt_fract, nb_test = nb_samples, train = False)
train = CSEdata(nb_samples=nb_samples, dt_fract=dt_fract, nb_test = nb_test, inpackage = inpackage, train = True , datapath='train')
valid = CSEdata(nb_samples=nb_samples, dt_fract=dt_fract, nb_test = nb_test, inpackage = inpackage, train = False, datapath='train')

print('Dataset:')
print('------------------------------')
Expand All @@ -223,7 +228,7 @@ def get_data( nb_samples, dt_fract, nb_test, batch_size, kwargs):
return train, valid, data_loader, test_loader


def get_test_data(testpath, meta, inpackage = False, train = False):
def get_test_data(testpath, meta, inpackage = False, train = False, datapath = 'test'):
'''
Get the data of the test 1D model, given a path and meta-data from a training setup.
Expand All @@ -236,9 +241,9 @@ def get_test_data(testpath, meta, inpackage = False, train = False):
- meta [dict]: meta data from the training setup
'''

data = CSEdata(nb_samples=meta['nb_samples'],dt_fract=meta['dt_fract'],nb_test= 100, train=True, fraction=0.7, cutoff = 1e-20, scale = 'norm')
data = CSEdata(nb_samples=meta['nb_samples'],dt_fract=meta['dt_fract'],nb_test= meta['nb_test'], train=train, fraction=0.7, cutoff = 1e-20, inpackage=inpackage)

mod = CSEmod(testpath, inpackage, train)
mod = CSEmod(testpath, inpackage, datapath)

if inpackage:
input = mod.get_input()
Expand Down Expand Up @@ -297,7 +302,7 @@ class CSEmod():
Class to load a 1D CSE model, calculated with the classical fortan code.
For more info on this model, see https://github.com/MarieVdS/rate22_cse_code.
'''
def __init__(self, path, inpackage = False, train = False):
def __init__(self, path, inpackage = False, data = 'test'):
'''
Load the 1D CSE model, given a path.
Expand All @@ -324,13 +329,14 @@ def __init__(self, path, inpackage = False, train = False):
inp_path = self.path[:-26]+ 'inputChemistry_'+self.name+'.txt'

if inpackage:
if train == False:
if data == 'test':
parentpath = str(Path(__file__).parent)[:-15]
print(parentpath)
self.path = parentpath + 'data/test/' + path +'/'
self.model = path[-62:-1]
self.name = path
inp_path = self.path+'input.txt'
if train == True:
if data == 'train':
parentpath = str(Path(__file__).parent)[:-15]
self.path = parentpath + 'data/train/' + path[:-18] +'/'
self.model = self.path
Expand Down Expand Up @@ -446,7 +452,7 @@ def split_in_0D(self):
y = 1.e-100 ## this number is added to xi, since it contains zeros
p = np.array([self.get_dens()[:-1], self.get_temp()[:-1], self.get_xi()[:-1]+y, self.get_Av()[:-1]])

return Δt.astype(np.float64), n_0D.astype(np.float64), p.T.astype(np.float64)
return Δt.astype(np.float64), n_0D.astype(np.float64), p.T.astype(np.float64) # type: ignore

def get_input(self):
print('-------------------')
Expand Down
1 change: 1 addition & 0 deletions src/mace/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def update_meta(self, traindata, train_time, overhead_time, path):
'nb_evol' : self.nb_evol,
'nb_hidden' : self.nb_hidden,
'ae_type' : self.ae_type,
'nb_test' : self.nb_test,
'done' : 'true'
}

Expand Down
4 changes: 2 additions & 2 deletions src/mace/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def test_evolution(model, input, printing = True, start_idx=0):
return np.array(n_evol).reshape(-1,468), np.array(mace_time)


def test_model(model, testpath, meta, specs=[], inpackage = False, train = False, printing = True, plotting = False, save = False):
def test_model(model, testpath, meta, specs=[], inpackage = False, datapath = 'test', printing = True, plotting = False, save = False):
'''
Test the model on a test set.
Expand All @@ -144,7 +144,7 @@ def test_model(model, testpath, meta, specs=[], inpackage = False, train = False
- plotting: plot the results, default = False
'''

model1D, input, info = ds.get_test_data(testpath, meta, inpackage = inpackage, train = train)
model1D, input, info = ds.get_test_data(testpath, meta, inpackage = inpackage, datapath = datapath)
id = info['path'] +'_'+ info['name']

n, n_hat, t, step_time = test_step(model, input, printing = printing)
Expand Down
2 changes: 1 addition & 1 deletion src/mace/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def train(model,
trainloss.save(trainpath)
testloss.save(testpath)
## plot, every save this fig is updated
loss.plot(trainloss, testloss, log = log, show = show)
fig = loss.plot(trainloss, testloss, ylim = False, log = log, show = False)
plt.savefig(path+'/loss.png')

# print(trainloss.get_loss('tot'))
Expand Down

0 comments on commit fe16e5b

Please sign in to comment.