From 0701157102b321cf38802530937d8e6f1b8632af Mon Sep 17 00:00:00 2001 From: Adam Fekete Date: Tue, 24 Jan 2017 15:22:00 +0000 Subject: [PATCH] adding datamodel class and future version of GP --- .../ML_of_Forces/DataModel/ForceConfs.py | 90 +++++++++++++++++++ .../ML_of_Forces/DataModel/__init__.py | 3 + .../ML_of_Forces/DataModel/baseclasses.py | 5 ++ .../GP/Extensions/BeakerSupport.py | 1 + .../GP/Extensions/PlotlySupport.py | 36 ++++++++ .../ML_of_Forces/GP/Extensions/__init__.py | 0 python-module/ML_of_Forces/GP/GPVec.py | 42 +++++++++ .../ML_of_Forces/GP/Optimizer/__init__.py | 0 python-module/ML_of_Forces/GP/__init__.py | 4 + python-module/ML_of_Forces/GP/baseclass.py | 22 +++++ .../ML_of_Forces/GP/feature/__init__.py | 2 + .../ML_of_Forces/GP/feature/cov_mat.py | 13 +++ .../ML_of_Forces/GP/kernels/__init__.py | 11 +++ python-module/ML_of_Forces/GP/kernels/id.py | 7 ++ .../ML_of_Forces/GP/tools/__init__.py | 9 ++ 15 files changed, 245 insertions(+) create mode 100644 python-module/ML_of_Forces/DataModel/ForceConfs.py create mode 100644 python-module/ML_of_Forces/DataModel/__init__.py create mode 100644 python-module/ML_of_Forces/DataModel/baseclasses.py create mode 100644 python-module/ML_of_Forces/GP/Extensions/BeakerSupport.py create mode 100644 python-module/ML_of_Forces/GP/Extensions/PlotlySupport.py create mode 100644 python-module/ML_of_Forces/GP/Extensions/__init__.py create mode 100644 python-module/ML_of_Forces/GP/GPVec.py create mode 100644 python-module/ML_of_Forces/GP/Optimizer/__init__.py create mode 100644 python-module/ML_of_Forces/GP/__init__.py create mode 100644 python-module/ML_of_Forces/GP/baseclass.py create mode 100644 python-module/ML_of_Forces/GP/feature/__init__.py create mode 100644 python-module/ML_of_Forces/GP/feature/cov_mat.py create mode 100644 python-module/ML_of_Forces/GP/kernels/__init__.py create mode 100644 python-module/ML_of_Forces/GP/kernels/id.py create mode 100644 python-module/ML_of_Forces/GP/tools/__init__.py diff --git a/python-module/ML_of_Forces/DataModel/ForceConfs.py b/python-module/ML_of_Forces/DataModel/ForceConfs.py new file mode 100644 index 0000000..cad83b5 --- /dev/null +++ b/python-module/ML_of_Forces/DataModel/ForceConfs.py @@ -0,0 +1,90 @@ +from DataModel.baseclasses import DataModel +from collections import namedtuple +import numpy as np + + +### Subsampling from big database ### + +### Spliting database in training/testing sets ### + + +class ForceConfs(DataModel): + + def __init__(self, confs=[], force=[]): + """ + + :param force: array of forces + :type force: np.array + :param confs: array of configurations + """ + assert len(confs) == len(force) + + self.confs = confs + self.force = force + + def __str__(self): + + out = 'ForceConfs model\n' + \ + ' # of configuration: {}\n'.format(len(self.confs)) +\ + ' range of configurations: mean={2}, min={0}, max={1}\n'.format(*self.range()) + + return out + + def __len__(self): + """ + + :return: length of the forces/configurations + """ + return len(self.confs) + + def range(self): + + """ + Determine the minimum and maximum length for a given list of configurations + :return: namedtuple('LengthRange', ['min', 'max', 'mean']) + """ + lengths = [len(conf) for conf in self.confs] + + LengthRange = namedtuple('LengthRange', ['min', 'max', 'mean']) + return LengthRange(min=np.min(lengths), max=np.max(lengths), mean=np.mean(lengths)) + + # return np.min(lengths), np.max(lengths), np.mean(lengths) + + def read_json_data(self, filename): + import json + + with open(filename, 'r') as jsonfile: + data = json.load(jsonfile)['data'] + + self.confs = [timestep['confs'] for timestep in data] + self.force = [timestep['force'] for timestep in data] + + self.confs = np.asarray(self.confs) + self.force = np.asarray(self.force) + + + def subsampling(self, train: int = 0, test: int = 0, random: bool = True) -> None: + + self.nsample = train + test + + if random: + inds = range(len(self)) + inds = np.random.choice(inds, size=self.nsample, replace=False) + + self.train_confs = self.confs[inds[0:train]] + self.train_force = self.force[inds[train:]] + + +if __name__ == '__main__': + + filename = '../Data/Si_TB_300K.json' + + data = ForceConfs() + data.read_json_data(filename) + print(data) + + data.subsampling(40,10) + + print(len(data.train_confs)) + print(len(data.train_force)) + print('END') diff --git a/python-module/ML_of_Forces/DataModel/__init__.py b/python-module/ML_of_Forces/DataModel/__init__.py new file mode 100644 index 0000000..9dd5dd5 --- /dev/null +++ b/python-module/ML_of_Forces/DataModel/__init__.py @@ -0,0 +1,3 @@ +from .ForceConfs import ForceConfs + +__all__ = ["ForceConfs"] \ No newline at end of file diff --git a/python-module/ML_of_Forces/DataModel/baseclasses.py b/python-module/ML_of_Forces/DataModel/baseclasses.py new file mode 100644 index 0000000..a7d6986 --- /dev/null +++ b/python-module/ML_of_Forces/DataModel/baseclasses.py @@ -0,0 +1,5 @@ +from abc import ABCMeta + + +class DataModel(metaclass=ABCMeta): + pass diff --git a/python-module/ML_of_Forces/GP/Extensions/BeakerSupport.py b/python-module/ML_of_Forces/GP/Extensions/BeakerSupport.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/python-module/ML_of_Forces/GP/Extensions/BeakerSupport.py @@ -0,0 +1 @@ + diff --git a/python-module/ML_of_Forces/GP/Extensions/PlotlySupport.py b/python-module/ML_of_Forces/GP/Extensions/PlotlySupport.py new file mode 100644 index 0000000..632bef1 --- /dev/null +++ b/python-module/ML_of_Forces/GP/Extensions/PlotlySupport.py @@ -0,0 +1,36 @@ +# from bokeh.plotting import figure, output_file, show +# output_file("test.html") +# p = figure() +# p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2) +# show(p) + + +# from plotly import __version__ +# +# from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot +# +# print(__version__) # requires version >= 1.9.0 +# +# from plotly.graph_objs import Scatter, Figure, Layout +# +# plot([Scatter(x=[1, 2, 3], y=[3, 1, 6])]) + +# +# +# import plotly +# from plotly.graph_objs import Scatter, Layout +# +# plotly.offline.plot({ +# "data": [Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])], +# "layout": Layout(title="hello world") +# }) + +import plotly +from plotly.graph_objs import Scatter, Layout + +plotly.offline.init_notebook_mode() + +plotly.offline.iplot({ + "data": [Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])], + "layout": Layout(title="hello world") +}) \ No newline at end of file diff --git a/python-module/ML_of_Forces/GP/Extensions/__init__.py b/python-module/ML_of_Forces/GP/Extensions/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/python-module/ML_of_Forces/GP/GPVec.py b/python-module/ML_of_Forces/GP/GPVec.py new file mode 100644 index 0000000..bb794f6 --- /dev/null +++ b/python-module/ML_of_Forces/GP/GPVec.py @@ -0,0 +1,42 @@ +from .baseclass import GaussianProcess + +from typing import Optional +from typing import Union, List, Dict + + +class GPVec(GaussianProcess): + + def __init__(self, + kernel: Optional[str] = None, + datamodel=None): + + """ + sdfdsfdf + :param kernel: Hello + :type kernel: Optional[str] + :param datamodel: + """ + + + super().__init__() + self.datamodel = datamodel # type: Optional[str] + self.kernel = kernel # kernel functions + + pass + + def fit(self, X, Y): + pass + + def predict(self): pass + + # def __repr__(self): + # return "%s(%r)" % (self.__class__, self.__dict__) + + def __str__(self): + + out = 'GaussianProcess:\n' +\ + ' datamodel: {}\n'.format(self.datamodel) +\ + ' kernel: {}\n'.format(self.kernel) + return out + + diff --git a/python-module/ML_of_Forces/GP/Optimizer/__init__.py b/python-module/ML_of_Forces/GP/Optimizer/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/python-module/ML_of_Forces/GP/__init__.py b/python-module/ML_of_Forces/GP/__init__.py new file mode 100644 index 0000000..b862cc4 --- /dev/null +++ b/python-module/ML_of_Forces/GP/__init__.py @@ -0,0 +1,4 @@ +from .GPVec import GPVec + + +__all__=['GPVec'] \ No newline at end of file diff --git a/python-module/ML_of_Forces/GP/baseclass.py b/python-module/ML_of_Forces/GP/baseclass.py new file mode 100644 index 0000000..64886e8 --- /dev/null +++ b/python-module/ML_of_Forces/GP/baseclass.py @@ -0,0 +1,22 @@ +from abc import ABCMeta, abstractmethod, abstractproperty + + +class GaussianProcess(metaclass=ABCMeta): + + + # @abstractproperty + # def kernel(self): pass + + @abstractmethod + def fit(self): pass + + @abstractmethod + def predict(self): pass + + + +class Kernel(metaclass=ABCMeta): + pass + +class FeatureSpace(metaclass=ABCMeta): + pass diff --git a/python-module/ML_of_Forces/GP/feature/__init__.py b/python-module/ML_of_Forces/GP/feature/__init__.py new file mode 100644 index 0000000..139597f --- /dev/null +++ b/python-module/ML_of_Forces/GP/feature/__init__.py @@ -0,0 +1,2 @@ + + diff --git a/python-module/ML_of_Forces/GP/feature/cov_mat.py b/python-module/ML_of_Forces/GP/feature/cov_mat.py new file mode 100644 index 0000000..c2fac9a --- /dev/null +++ b/python-module/ML_of_Forces/GP/feature/cov_mat.py @@ -0,0 +1,13 @@ +from GP.baseclass import FeatureSpace + + +class cov_mat(FeatureSpace): + + pass + + +if __name__ == '__main__': + + m = cov_mat() + + print(m) \ No newline at end of file diff --git a/python-module/ML_of_Forces/GP/kernels/__init__.py b/python-module/ML_of_Forces/GP/kernels/__init__.py new file mode 100644 index 0000000..b0e4ad0 --- /dev/null +++ b/python-module/ML_of_Forces/GP/kernels/__init__.py @@ -0,0 +1,11 @@ + +from .id import id + + +__all__ = ['id'] + + + + +if __name__ == '__main__': + pass diff --git a/python-module/ML_of_Forces/GP/kernels/id.py b/python-module/ML_of_Forces/GP/kernels/id.py new file mode 100644 index 0000000..db8db88 --- /dev/null +++ b/python-module/ML_of_Forces/GP/kernels/id.py @@ -0,0 +1,7 @@ +from GP.baseclass import Kernel + + +class id(Kernel): + + def __call__(self, *args, **kwargs): + return 1.0 diff --git a/python-module/ML_of_Forces/GP/tools/__init__.py b/python-module/ML_of_Forces/GP/tools/__init__.py new file mode 100644 index 0000000..a5700ee --- /dev/null +++ b/python-module/ML_of_Forces/GP/tools/__init__.py @@ -0,0 +1,9 @@ +import json + + +def read_json_data(filename): + + with open(filename, 'r') as jsonfile: + data = json.load(jsonfile) + + return data['data'] -- GitLab