Skip to content

Commit

Permalink
Merge branch 'css-materialdesign' of https://github.com/AaltoPML/VAI-Lab
Browse files Browse the repository at this point in the history
  • Loading branch information
sevisal committed Oct 5, 2023
2 parents 89b615a + 1551095 commit f7ac460
Show file tree
Hide file tree
Showing 13 changed files with 586 additions and 1 deletion.
66 changes: 66 additions & 0 deletions src/vai_lab/DataProcessing/plugins/RGBcalculation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import numpy as np
from sklearn.base import BaseEstimator
from vai_lab._plugin_templates import DataProcessingT

_PLUGIN_READABLE_NAMES = {"RGBcalculation":"default"} # type:ignore
_PLUGIN_MODULE_OPTIONS = {"Type": "Other"} # type:ignore
_PLUGIN_REQUIRED_SETTINGS = {"Data": "str"} # type:ignore
_PLUGIN_OPTIONAL_SETTINGS = {} # type:ignore
_PLUGIN_REQUIRED_DATA = {} # type:ignore
_PLUGIN_OPTIONAL_DATA = {"X","Y","X_tst", 'Y_tst'} # type:ignore

class RGBcalculation(DataProcessingT):

def __init__(self):
"""Initialises parent class.
Passes `globals` dict of all current variables
"""
super().__init__(globals())
self.proc = model()

# def configure(self, config: dict):
# """Sets and parses plugin configurations options
# :param config: dict of internal tags set in the XML config file
# """
# super().configure(config)

# def set_data_in(self, data_in):
# """Sets and parses incoming data
# :param data_in: saves data as class variable
# expected type: aidesign.Data.Data_core.Data
# """
# super().set_data_in(data_in)

# def fit(self):
# cleaned_options = self._clean_solver_options()
# self.proc.set_params(**cleaned_options)
# self.proc.fit(self.X)

# def transform(self, data: DataInterface) -> DataInterface:
# data.append_data_column("X", pd.DataFrame(self.proc.transform(data)))
# # if self.X_tst is not None:
# # data.append_data_column("X_test", pd.DataFrame(self.proc.transform(self.X_tst)))
# print(data)
# return data

class model(BaseEstimator):
def __init__(self):
return

def fit(self, X):
return self

def transform(self, X):
""" Calculates the mean RGB value of an image.
Parameters
----------
X : {array-like, sparse matrix} of shape (pixels, pixels, RGB)
The image data with the RGB information.
Returns
-------
mean_RGB : {int, float}
Mean RGB value of the input image.
"""
mean_RGB = np.mean(X)
return mean_RGB
124 changes: 124 additions & 0 deletions src/vai_lab/DataProcessing/plugins/RectangleDetection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import numpy as np
from sklearn.base import BaseEstimator
from vai_lab._plugin_templates import DataProcessingT
import matplotlib.pyplot as plt

_PLUGIN_READABLE_NAMES = {"RectangleDetection":"default",
"RectDet": "alias",
"rectangledetection": "alias"} # type:ignore
_PLUGIN_MODULE_OPTIONS = {"Type": "Other"} # type:ignore
_PLUGIN_REQUIRED_SETTINGS = {"Data": "str"} # type:ignore
_PLUGIN_OPTIONAL_SETTINGS = {"r": "int",
"c": "int",
"h":"float",
"w": "float"} # type:ignore
_PLUGIN_REQUIRED_DATA = {} # type:ignore
_PLUGIN_OPTIONAL_DATA = {"X","Y","X_tst","Y_tst"} # type:ignore


class RectangleDetection(DataProcessingT):
"""
Red rectangle detection in an image
"""
def __init__(self):
"""Initialises parent class.
Passes `globals` dict of all current variables
"""
super().__init__(globals())
self.proc = model()

# def configure(self, config: dict):
# """Sets and parses plugin configurations options
# :param config: dict of internal tags set in the XML config file
# """
# super().configure(config)

# def set_data_in(self, data_in):
# """Sets and parses incoming data
# :param data_in: saves data as class variable
# expected type: aidesign.Data.Data_core.Data
# """
# super().set_data_in(data_in)

# def fit(self):
# cleaned_options = self._clean_solver_options()
# self.proc.set_params(**cleaned_options)
# """
# # TODO: Temporary solution until input data module is cofigured."""

# # dst_img = os.path.join(
# # get_lib_parent_dir(),
# # 'examples',
# # 'crystalDesign')
# # #iterating over dst_image to get the images as arrays
# # # for image in sorted(os.listdir(dst_img)):
# # arr = np.array(Image.open(os.path.join(dst_img, '00007.png')))
# # self.proc.fit(arr)
# """"""
# self.proc.fit(self.X)

# def transform(self, data: DataInterface) -> DataInterface:
# """
# # TODO: Temporary solution until input data module is cofigured."""
# # dst_img = os.path.join(
# # get_lib_parent_dir(),
# # 'examples',
# # 'crystalDesign')
# # arr = np.array(Image.open(os.path.join(dst_img, '00007.png')))
# # self.proc.transform(arr)
# """"""
# data.append_data_column("X", pd.DataFrame(self.proc.transform(data)))
# return data

class model(BaseEstimator):
def __init__(self, optional=False):
self.optional = optional

def fit(self, X, r=7, c=4, h=35, w=79, hs=34, vs=23, buffer=5):
"""Defines the number of samples in the design, their placement
and their size.
Parameters
----------
X : {array-like, sparse matrix} of shape (pixels, pixels, RGB)
The image data with the samples with RGB information.
Samples are expected to be marked with a red rectangle.
r : int
Number of rows with samples.
c : int
Number of columns with samples.
h : float
Sample height.
w : int
Sample width.
hs : float
Horizontal spacing.
vs : int
Vertical spacing.
buffer : int
Pixel buffer. Inidcates the margin for the inner rectangle.
Returns
-------
self : object
Fitted model.
"""
self.r = r
self.c = c
self.h = h
self.w = w
self.hs = hs
self.vs = vs
self.buffer = buffer
mask = (X[:,:,0] > 120) * (X[:,:,1] < 80) * (X[:,:,2] < 80)
self.ii_ini, self.jj_ini = np.unravel_index(mask.argmax(), mask.shape)
# return self

def transform(self, X):
X_dict = {}
fig, axs = plt.subplots(self.r, self.c)
for i in np.arange(self.r):
for j in np.arange(self.c):
X_dict[i,j] = X[self.ii_ini+self.h*i+self.vs*i+self.buffer*(1+i):self.ii_ini+self.h*(i+1)+self.vs*i-self.buffer*(1-i),
self.jj_ini+self.w*j+self.hs*j+self.buffer*(1+j):self.jj_ini+self.w*(j+1)+self.hs*j-self.buffer*(1-j),:]
axs[i, j].imshow(X_dict[i,j])
return X_dict
35 changes: 35 additions & 0 deletions src/vai_lab/DataProcessing/plugins/argopt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from distutils.command.config import config
from numpy import argmin, argmax
from vai_lab._plugin_templates import DataProcessingT
import pandas as pd

_PLUGIN_READABLE_NAMES = {"argopt": "default",
"argmax": "alias",
"argmin": "alias"} # type:ignore
_PLUGIN_MODULE_OPTIONS = {"Type": "math operator"} # type:ignore
_PLUGIN_REQUIRED_SETTINGS = {"Data": "str"} # type:ignore
_PLUGIN_OPTIONAL_SETTINGS = {'min/max': "str"} # type:ignore
_PLUGIN_REQUIRED_DATA = {} # type:ignore
_PLUGIN_OPTIONAL_DATA = {"X","Y","X_tst", 'Y_tst'} # type:ignore

class argopt(DataProcessingT):
"""
Calculate the optimum argument
"""

def __init__(self):
"""Initialises parent class.
Passes `globals` dict of all current variables
"""
super().__init__(globals())

def fit(self):
self.cleaned_options = self._clean_solver_options()
return

def transform(self,data):
if config['min/max'] == 'max':
data.append_data_column("X", pd.DataFrame(argmax(self.X)))
else:
data.append_data_column("X", pd.DataFrame(argmin(self.X)))
return data
51 changes: 51 additions & 0 deletions src/vai_lab/DataProcessing/plugins/imgtofeatvect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from vai_lab._plugin_templates import DataProcessingT
from sklearn.base import BaseEstimator
import pandas as pd
import numpy as np

_PLUGIN_READABLE_NAMES = {"ImgToFeatVect":"default",
"imgtofeatvect": "alias",
"ImageToFeatureVector": "alias"} # type:ignore
_PLUGIN_MODULE_OPTIONS = {"Type": "Other"} # type:ignore
_PLUGIN_REQUIRED_SETTINGS = {"Data": "str"} # type:ignore
_PLUGIN_OPTIONAL_SETTINGS = {} # type:ignore
_PLUGIN_REQUIRED_DATA = {} # type:ignore
_PLUGIN_OPTIONAL_DATA = {"X","Y","X_tst","Y_tst"} # type:ignore


class ImgToFeatVect(DataProcessingT):

def __init__(self):
"""Initialises parent class.
Passes `globals` dict of all current variables
"""
super().__init__(globals())
self.proc = model()


class model(BaseEstimator):
def __init__(self, optional=False):
self.optional = optional

def fit(self, X):
"""Defines the number of samples in the design, their placement
and their size.
Parameters
----------
X : {dict}
A dictionary containing images in each entry.
Returns
-------
self : object
Fitted model.
"""
self.n = len(X.keys())
h,w,self.rgb = next(iter(X.values())).shape
self.d = h*w
return self

def transform(self, X):
X_mat = np.dstack([X[el].reshape(self.d,self.rgb) for el in X.keys()])
X_mat = X_mat.reshape(self.n,self.d,self.rgb)
return pd.DataFrame(X_mat)
32 changes: 32 additions & 0 deletions src/vai_lab/DataProcessing/plugins/integral.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from scipy.integrate import simps as model
from vai_lab._plugin_templates import DataProcessingT
import pandas as pd

_PLUGIN_READABLE_NAMES = {"Integral": "default",
"integral": "alias"} # type:ignore
_PLUGIN_MODULE_OPTIONS = {"Type": "math operator"} # type:ignore
_PLUGIN_REQUIRED_SETTINGS = {"Data": "str"} # type:ignore
_PLUGIN_OPTIONAL_SETTINGS = {"dx": "float",
"axis": "int",
"even": "str"} # type:ignore
_PLUGIN_REQUIRED_DATA = {} # type:ignore
_PLUGIN_OPTIONAL_DATA = {"X","Y","X_tst","Y_tst"} # type:ignore

class Integral(DataProcessingT):
"""
Calculate integral of array using the composite trapezoidal rule
"""

def __init__(self):
"""Initialises parent class.
Passes `globals` dict of all current variables
"""
super().__init__(globals())
self.proc = model()

def fit(self):
return

def transform(self,data):
data.append_data_column("X", pd.DataFrame(self.proc(self.X)))
return data
2 changes: 1 addition & 1 deletion src/vai_lab/UserInteraction/plugins/OptimisationInput.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
_PLUGIN_MODULE_OPTIONS = {"layer_priority": 2,
"required_children": None} # type:ignore
_PLUGIN_REQUIRED_SETTINGS = {} # type:ignore
_PLUGIN_OPTIONAL_SETTINGS = {"Bounds": "list"} # type:ignore
_PLUGIN_OPTIONAL_SETTINGS = {"Bounds": "list"} # type:ignore
_PLUGIN_REQUIRED_DATA = {"X"} # type:ignore


Expand Down
32 changes: 32 additions & 0 deletions src/vai_lab/examples/xml_files/BO_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<pipeline>
<Initialiser name="Initialiser">
<inputdata>
<X file="./examples/optimisation/X.csv" />
</inputdata>
<relationships>
<child name="User Interaction" />
</relationships>
<coordinates>
[(350.0,50),0,{}]
</coordinates>
</Initialiser>
<UserInteraction name="User Interaction">
<relationships>
<parent name="Initialiser" />
<child name="Output" />
</relationships>
<coordinates>
[(350.0,350.0),2,{0:'d0-u2'}]
</coordinates>
<plugin type="optimisationUI" />
</UserInteraction>
<Output name="Output">
<plugin type="" />
<relationships>
<parent name="User Interaction" />
</relationships>
<coordinates>
[(350.0,650),1,{2:'d2-u1'}]
</coordinates>
</Output>
</pipeline>
29 changes: 29 additions & 0 deletions src/vai_lab/examples/xml_files/DecisionMaking_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<pipeline>
<Initialiser name="Initialiser">
<relationships>
<child name="Decision Making" />
</relationships>
<coordinates>
[(350.0,50),0,{}]
</coordinates>
</Initialiser>
<DecisionMaking name="Decision Making">
<plugin type="" />
<relationships>
<parent name="Initialiser" />
<child name="Output" />
</relationships>
<coordinates>
[(350.0, 350.0), 2, {0: 'd0-u2'}]
</coordinates>
</DecisionMaking>
<Output name="Output">
<plugin type="" />
<relationships>
<parent name="Decision Making" />
</relationships>
<coordinates>
[(350.0, 650), 1, {2: 'd2-u1'}]
</coordinates>
</Output>
</pipeline>
Loading

0 comments on commit f7ac460

Please sign in to comment.