-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader.py
More file actions
96 lines (73 loc) · 3.74 KB
/
data_loader.py
File metadata and controls
96 lines (73 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-
"""
data_loader.py - The data management module
===========================================
This module handles the fetching of the data from the local resources path, given in the configuration and arranging it
for our purposes of estimations. For instance, the data for Example no. 1 can be fetched by:
::
get_data(ExperimentType.ExampleNo1) - Creating the data for Example no. 1 of the paper.
"""
from scipy.linalg import qr
import numpy as np
from Infrastructure.enums import ExperimentType
from Infrastructure.utils import Dict, RowVector, Matrix, create_factory, Callable
from matrices_classes import MatInSVDForm, ExperimentNo5Form
def _random_orthonormal_cols(data_size: int, columns: int) -> Matrix:
return np.ascontiguousarray(qr(np.random.randn(data_size, columns), mode="economic", overwrite_a=True,
check_finite=False)[0])
def _get_first_3_examples_data(data_size: int, singular_values: RowVector) -> Matrix:
"""
A method which creates a random matrix of size data_size x data_size with given singular values.
Args:
data_size(int): The input data size n.
singular_values(RowVector): The singular values to be set for the matrix to create.
Returns:
A random size data_size x data_size Matrix awith the given singular values.
"""
rank: int = len(singular_values)
U: Matrix = _random_orthonormal_cols(data_size, rank)
V: Matrix = _random_orthonormal_cols(data_size, rank)
return MatInSVDForm(U, singular_values, V)
def _get_example_4_data(data_size: int, singular_values: RowVector) -> Matrix:
"""
A method which creates a data_size x data_size matrix whose singular values are the input values.
Args:
data_size(int): The input data size n.
singular_values(RowVector): The singular values to be set for the matrix to create.
Returns:
A data_size x data_size Matrix with the given singular values.
"""
U: Matrix = np.ascontiguousarray(
np.stack([
np.ones(data_size),
np.tile([1, -1], data_size // 2),
np.tile([1, 1, -1, -1], data_size // 4),
np.tile([1, 1, 1, 1, -1, -1, -1, -1], data_size // 8)]).T) / np.sqrt(data_size)
V: Matrix = np.ascontiguousarray(
np.stack([
np.concatenate([np.ones(data_size - 1), [0]]) / np.sqrt(data_size - 1),
np.concatenate([np.zeros(data_size - 1), [1]]),
np.concatenate([np.tile([1, -1], (data_size - 2) // 2) / np.sqrt(data_size - 2), [0, 0]]),
np.concatenate([[1, 0, -1], np.zeros(data_size - 3)]) / np.sqrt(2)]).T)
return MatInSVDForm(U, np.array(singular_values), V)
def _get_example_5_data(data_size: int, singular_values: RowVector) -> Matrix:
"""
A method which creates a ``data_size x data_size`` matrix :math:`A` with the form: :math:`A=uv^{T}+\sigma I_n`
where :math:`u=(1,0,...,0)` and :math:`v^{T}=\frac{1}{sqrt{n}}(1,1,...,1)`
Args:
data_size(int): The input data size n.
singular_values(RowVector): A vector of singular values for the created matrix.
Returns:
A ``data_size x data_size`` Matrix in this form.
"""
return ExperimentNo5Form((data_size, data_size), singular_values[0])
# A private dictionary used to create the method "get_data"
_data_type_to_function: Dict[str, Callable] = {
ExperimentType.ExampleNo1: _get_first_3_examples_data,
ExperimentType.ExampleNo2: _get_first_3_examples_data,
ExperimentType.ExampleNo3: _get_first_3_examples_data,
ExperimentType.ExampleNo4: _get_example_4_data,
ExperimentType.ExampleNo5: _get_example_5_data
}
# The public method which fetches the data loading methods.
get_data: Callable = create_factory(_data_type_to_function, are_methods=True)