Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
Troubled Cell Detection
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Laura Christine Kühle
Troubled Cell Detection
Commits
88873b0c
Commit
88873b0c
authored
Nov 24, 2021
by
Laura Christine Kühle
Browse files
Options
Downloads
Patches
Plain Diff
Added documentation to 'ANN_Data_Generator'.
parent
70dd7b5e
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
ANN_Data_Generator.py
+119
-0
119 additions, 0 deletions
ANN_Data_Generator.py
with
119 additions
and
0 deletions
ANN_Data_Generator.py
+
119
−
0
View file @
88873b0c
...
@@ -9,6 +9,7 @@ TODO: Improve verbose output -> Done
...
@@ -9,6 +9,7 @@ TODO: Improve verbose output -> Done
TODO: Change order of methods -> Done
TODO: Change order of methods -> Done
TODO: Fix bug in initialization of input matrix -> Done
TODO: Fix bug in initialization of input matrix -> Done
TODO: Improve function selection (more even distribution) -> Done
TODO: Improve function selection (more even distribution) -> Done
TODO: Add documentation -> Done
"""
"""
...
@@ -21,8 +22,45 @@ import timeit
...
@@ -21,8 +22,45 @@ import timeit
class
TrainingDataGenerator
(
object
):
class
TrainingDataGenerator
(
object
):
"""
Class for training data generator.
Generate random training data for given initial conditions.
Attributes
----------
smooth_functions : list
List of smooth initial/continuous conditions.
troubled_functions : list
List of discontinuous initial conditions.
data_dir : str
Path to directory in which training data is saved.
Methods
-------
build_training_data()
Builds random training data.
"""
def
__init__
(
self
,
initial_conditions
,
left_bound
=-
1
,
right_bound
=
1
,
balance
=
0.5
,
def
__init__
(
self
,
initial_conditions
,
left_bound
=-
1
,
right_bound
=
1
,
balance
=
0.5
,
stencil_length
=
3
,
directory
=
None
):
stencil_length
=
3
,
directory
=
None
):
"""
Initializes TrainingDataGenerator.
Parameters
----------
initial_conditions : list
List of names of initial conditions for training.
left_bound : float, optional
Left boundary of interval. Default: -1.
right_bound : float, optional
Right boundary of interval. Default: 1.
balance: float, optional
Ratio between smooth and discontinuous training data. Default: 0.5.
stencil_length : int, optional
Size of training data array. Default: 3.
directory : str, optional
Path to directory in which training data is saved. Default:
'
test_data
'
.
"""
self
.
_balance
=
balance
self
.
_balance
=
balance
self
.
_left_bound
=
left_bound
self
.
_left_bound
=
left_bound
self
.
_right_bound
=
right_bound
self
.
_right_bound
=
right_bound
...
@@ -49,6 +87,21 @@ class TrainingDataGenerator(object):
...
@@ -49,6 +87,21 @@ class TrainingDataGenerator(object):
os
.
makedirs
(
self
.
_data_dir
)
os
.
makedirs
(
self
.
_data_dir
)
def
build_training_data
(
self
,
num_samples
):
def
build_training_data
(
self
,
num_samples
):
"""
Builds random training data.
Creates training data consisting of random ANN input and saves it.
Parameters
----------
num_samples : int
Number of training data samples to generate.
Returns
-------
data_dict : dict
Dictionary containing input (normalized and non-normalized) and output data.
"""
tic
=
timeit
.
default_timer
()
tic
=
timeit
.
default_timer
()
print
(
'
Calculating training data...
\n
'
)
print
(
'
Calculating training data...
\n
'
)
data_dict
=
self
.
_calculate_data_set
(
num_samples
)
data_dict
=
self
.
_calculate_data_set
(
num_samples
)
...
@@ -60,6 +113,22 @@ class TrainingDataGenerator(object):
...
@@ -60,6 +113,22 @@ class TrainingDataGenerator(object):
return
data_dict
return
data_dict
def
_calculate_data_set
(
self
,
num_samples
):
def
_calculate_data_set
(
self
,
num_samples
):
"""
Calculates random training data of given stencil length.
Creates training data with a given ratio between smooth and discontinuous samples and
fixed stencil length.
Parameters
----------
num_samples : int
Number of training data samples to generate.
Returns
-------
dict
Dictionary containing input (normalized and non-normalized) and output data.
"""
num_smooth_samples
=
round
(
num_samples
*
self
.
_balance
)
num_smooth_samples
=
round
(
num_samples
*
self
.
_balance
)
smooth_input
,
smooth_output
=
self
.
_generate_cell_data
(
num_smooth_samples
,
smooth_input
,
smooth_output
=
self
.
_generate_cell_data
(
num_smooth_samples
,
self
.
_smooth_functions
,
True
)
self
.
_smooth_functions
,
True
)
...
@@ -84,6 +153,28 @@ class TrainingDataGenerator(object):
...
@@ -84,6 +153,28 @@ class TrainingDataGenerator(object):
'
normalized_input
'
:
norm_input_matrix
}
'
normalized_input
'
:
norm_input_matrix
}
def
_generate_cell_data
(
self
,
num_samples
,
initial_conditions
,
is_smooth
):
def
_generate_cell_data
(
self
,
num_samples
,
initial_conditions
,
is_smooth
):
"""
Generates random training input and output.
Generates random training input and output for either smooth or discontinuous
initial_conditions.
Parameters
----------
num_samples : int
Number of training data samples to generate.
initial_conditions : list
List of names of initial conditions for training.
is_smooth : boolean
Flag whether initial conditions are smooth.
Returns
-------
input_data : np.array
Array containing input data.
output_data : np.array
Array containing output data.
"""
troubled_indicator
=
'
without
'
if
is_smooth
else
'
with
'
troubled_indicator
=
'
without
'
if
is_smooth
else
'
with
'
print
(
'
Calculating data
'
+
troubled_indicator
+
'
troubled cells...
'
)
print
(
'
Calculating data
'
+
troubled_indicator
+
'
troubled cells...
'
)
print
(
'
Samples to complete:
'
,
num_samples
)
print
(
'
Samples to complete:
'
,
num_samples
)
...
@@ -131,6 +222,20 @@ class TrainingDataGenerator(object):
...
@@ -131,6 +222,20 @@ class TrainingDataGenerator(object):
return
input_data
,
output_data
return
input_data
,
output_data
def
_build_stencil
(
self
):
def
_build_stencil
(
self
):
"""
Builds random stencil.
Calculates fixed number of cell centers around a random point in a given 1D domain.
Returns
-------
interval : np.array
List containing left and right bound of interval.
stencil : np.array
List of cell centers in stencil.
grid_spacing: float
Length of cell in grid.
"""
# Calculating Cell centers for a given 1D domain with n elements, and
# Calculating Cell centers for a given 1D domain with n elements, and
# Calculating Corresponding Legendre Basis Coefficients for given polynomial_degree
# Calculating Corresponding Legendre Basis Coefficients for given polynomial_degree
# Create stencil and basis_coefficients for smooth_function mapped onto stencil
# Create stencil and basis_coefficients for smooth_function mapped onto stencil
...
@@ -156,6 +261,19 @@ class TrainingDataGenerator(object):
...
@@ -156,6 +261,19 @@ class TrainingDataGenerator(object):
@staticmethod
@staticmethod
def
_normalize_data
(
input_data
):
def
_normalize_data
(
input_data
):
"""
Normalize data.
Parameters
----------
input_data : np.array
Array containing input data.
Returns
-------
np.array
Array containing normalized input data.
"""
normalized_input_data
=
input_data
normalized_input_data
=
input_data
for
i
in
range
(
len
(
input_data
)):
for
i
in
range
(
len
(
input_data
)):
max_function_value
=
max
(
max
(
np
.
absolute
(
input_data
[
i
])),
1
)
max_function_value
=
max
(
max
(
np
.
absolute
(
input_data
[
i
])),
1
)
...
@@ -163,6 +281,7 @@ class TrainingDataGenerator(object):
...
@@ -163,6 +281,7 @@ class TrainingDataGenerator(object):
return
normalized_input_data
return
normalized_input_data
def
_save_data
(
self
,
data
):
def
_save_data
(
self
,
data
):
"""
Saves data.
"""
print
(
'
Saving training data.
'
)
print
(
'
Saving training data.
'
)
for
key
in
data
.
keys
():
for
key
in
data
.
keys
():
name
=
self
.
_data_dir
+
'
/
'
+
key
+
'
_data.npy
'
name
=
self
.
_data_dir
+
'
/
'
+
key
+
'
_data.npy
'
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment