diff --git a/ANN_Data_Generator.py b/ANN_Data_Generator.py index d821b03ae3f436376547019226cfa576f4d40192..81461f99e435d03ce1a2b653027d2e9820f89d8f 100644 --- a/ANN_Data_Generator.py +++ b/ANN_Data_Generator.py @@ -13,7 +13,7 @@ import DG_Approximation class TrainingDataGenerator(object): """Class for training data generator. - Generate random training data for given initial conditions. + Generates random training data for given initial conditions. Attributes ---------- @@ -26,7 +26,7 @@ class TrainingDataGenerator(object): Methods ------- - build_training_data() + build_training_data(num_samples) Builds random training data. """ @@ -151,14 +151,14 @@ class TrainingDataGenerator(object): Number of training data samples to generate. initial_conditions : list List of names of initial conditions for training. - is_smooth : boolean + is_smooth : bool Flag whether initial conditions are smooth. Returns ------- - input_data : np.array + input_data : ndarray Array containing input data. - output_data : np.array + output_data : ndarray Array containing output data. """ @@ -215,11 +215,11 @@ class TrainingDataGenerator(object): Returns ------- - interval : np.array + interval : ndarray List containing left and right bound of interval. - stencil : np.array + stencil : ndarray List of cell centers in stencil. - grid_spacing: float + grid_spacing : float Length of cell in grid. """ @@ -244,16 +244,16 @@ class TrainingDataGenerator(object): @staticmethod def _normalize_data(input_data): - """Normalize data. + """Normalizes data. Parameters ---------- - input_data : np.array + input_data : ndarray Array containing input data. Returns ------- - np.array + ndarray Array containing normalized input data. """ diff --git a/ANN_Model.py b/ANN_Model.py index 350eab2d1e1047c6138b74a8055040bea9221cd7..e0140b520eee4fd365b7a61798ce16ccfe1845ba 100644 --- a/ANN_Model.py +++ b/ANN_Model.py @@ -17,17 +17,24 @@ class ThreeLayerReLu(torch.nn.Module): Attributes ---------- - name: str + name : str String containing name of model. - input_linear: torch.nn.Module + input_linear : torch.nn.Module Linear input layer. - middle_linear: torch.nn.Module + middle_linear : torch.nn.Module Linear middle layer. - output_linear: torch.nn.Module + output_linear : torch.nn.Module Linear output layer. - output_layer: torch.nn.Module + output_layer : torch.nn.Module Activation layer for output calculation. + Methods + ------- + forward(input_data) + Executes forward propagation. + get_name() + Returns string of model name. + """ def __init__(self, config): """Initializes ThreeLayerReLu. @@ -64,12 +71,12 @@ class ThreeLayerReLu(torch.nn.Module): Parameters ---------- - input_data: ndarray + input_data : ndarray 2D array containing input data. Returns ------- - prediction: ndarray + prediction : ndarray Matrix containing predicted output data. """ diff --git a/ANN_Training.py b/ANN_Training.py index 67f11cbbd4ba8c61806c442a1ed38b2274d6941a..e7628a204253885df68c743e6a19eb52431227ec 100644 --- a/ANN_Training.py +++ b/ANN_Training.py @@ -12,7 +12,8 @@ TODO: Write-protect all data and models TODO: Put legend outside plot (bbox_to_anchor) TODO: Put plotting into separate function TODO: Reduce number of testing epochs to 50 -TODO: Adapt docstring to uniform standard +TODO: Adapt docstring to uniform standard -> Done +TODO: Change maximal line length to 79 (as advised by PEP8) """ import numpy as np @@ -50,11 +51,11 @@ class ModelTrainer(object): Methods ------- - epoch_training() + epoch_training(dataset, num_epochs, verbose) Trains model for a given number of epochs. - test_model() + test_model(training_set, test_set) Evaluates predictions of a model. - save_model() + save_model(directory, model_name) Saves state and validation loss of a model. """ @@ -121,7 +122,7 @@ class ModelTrainer(object): dataset : torch.utils.data.dataset.TensorDataset Training dataset. num_epochs : int, optional - Number of epochs for training. If None, set to instance value. Default: None. + Number of epochs for training. Default: None (i.e. instance variable). verbose : bool, optional Flag whether commentary in console is wanted. Default: False. @@ -222,9 +223,9 @@ class ModelTrainer(object): Parameters ---------- - directory: str + directory : str Path to directory in which model is saved. - model_name: str, optional + model_name : str, optional Name of model for saving. Default: 'test_model'. """ @@ -244,9 +245,9 @@ def read_training_data(directory: str, Parameters ---------- - directory: str + directory : str Path to directory in which training data is saved. - normalized: bool, optional + normalized : bool, optional Flag whether normalized data should be used. Default: True. Returns @@ -267,15 +268,15 @@ def evaluate_models(models: dict, directory: str, num_iterations: int = 100, col Parameters ---------- - models: dict + models : dict Dictionary of models to evaluate. - directory: str + directory : str Path to directory for saving resulting plots. - num_iterations: int, optional + num_iterations : int, optional Number of iterations for evaluation. Default: 100. - colors: dict, optional + colors : dict, optional Dictionary containing plotting colors. If None, set to default colors. Default: None. - compare_normalization: bool, optional + compare_normalization : bool, optional Flag whether both normalized and raw data should be evaluated. Default: False. """ diff --git a/Basis_Function.py b/Basis_Function.py index 0630198c0d29f1a61d124f31d5c6439fc6340f19..197be81bc29ea0ff6561635cd5770718f92537eb 100644 --- a/Basis_Function.py +++ b/Basis_Function.py @@ -17,9 +17,9 @@ class Vector(object): Attributes ---------- - basis : np.array + basis : ndarray Array of basis. - wavelet : np.array + wavelet : ndarray Array of wavelet. Methods @@ -61,7 +61,7 @@ class Vector(object): Returns ------- - np.array + ndarray Vector containing basis evaluated at evaluation point. """ @@ -81,7 +81,7 @@ class Vector(object): Returns ------- - np.array + ndarray Vector containing wavelet evaluated at evaluation point. """ @@ -108,7 +108,7 @@ class Legendre(Vector): Returns ------- - np.array + ndarray Vector containing basis evaluated at evaluation point. """ @@ -124,7 +124,7 @@ class Legendre(Vector): Returns ------- - np.array + ndarray Vector containing Legendre polynomial evaluated at evaluation point. """ @@ -163,7 +163,7 @@ class OrthonormalLegendre(Legendre): Returns ------- - np.array + ndarray Vector containing basis evaluated at evaluation point. """ @@ -181,7 +181,7 @@ class OrthonormalLegendre(Legendre): Returns ------- - np.array + ndarray Vector containing wavelet evaluated at evaluation point. Notes @@ -227,7 +227,7 @@ class OrthonormalLegendre(Legendre): Returns ------- - np.array + ndarray Array containing the basis projection based on the integrals of the product of two basis vectors for each degree combination. @@ -248,7 +248,7 @@ class OrthonormalLegendre(Legendre): Returns ------- - np.array + ndarray Matrix containing the integral of basis products. """ @@ -268,7 +268,7 @@ class OrthonormalLegendre(Legendre): Returns ------- - np.array + ndarray Array containing the multiwavelet projection based on the integrals of the product of a basis vector and a wavelet vector for each degree combination. @@ -286,12 +286,12 @@ class OrthonormalLegendre(Legendre): First parameter. second_param : float Second parameter. - is_left_matrix : boolean + is_left_matrix : bool Flag whether the left matrix is calculated. Returns ------- - np.array + ndarray Matrix containing the integral of products of a basis and a wavelet vector. """ diff --git a/DG_Approximation.py b/DG_Approximation.py index fef6c673d36a453f360ccb95585bed972eda0f48..3d24bd0caec0733849d3c9780c92a3c9b4a69d99 100644 --- a/DG_Approximation.py +++ b/DG_Approximation.py @@ -25,6 +25,7 @@ TODO: Add way of saving data (np.savez('data/' + name, TODO: Outsource scripts into separate directory TODO: Allow comparison between ANN training datasets TODO: Add a default model state +TODO: Add type annotations to function heads """ import os @@ -59,9 +60,9 @@ class DGScheme(object): Length of a cell in mesh. basis : Basis object Basis for calculation. - mesh : array + mesh : ndarray List of mesh valuation points. - inv_mass : np.array + inv_mass : ndarray Inverse mass matrix. Methods @@ -98,8 +99,8 @@ class DGScheme(object): Left boundary of interval. Default: -1. right_bound : float, optional Right boundary of interval. Default: 1. - verbose : boolean, optional - Flag whether commentary in console is wanted. Default: False + verbose : bool, optional + Flag whether commentary in console is wanted. Default: False. plot_dir : str, optional Path to directory in which plots are saved. Default: 'test'. history_threshold : float, optional @@ -218,6 +219,11 @@ class DGScheme(object): Sets plot directory, if not already existing, and saves plots generated during the last approximation. + Parameters + ---------- + plot_name : str + Name of plot. + """ # Set paths for plot files if not existing already if not os.path.exists(self._plot_dir): @@ -273,7 +279,7 @@ class DGScheme(object): Returns ------- - np.array + ndarray Matrix containing projection of size (N+2, p+1) with N being the number of grid cells and p being the polynomial degree. @@ -323,7 +329,7 @@ class DGScheme(object): Returns ------- - np.array + ndarray Matrix containing cell averages and reconstructions for initial projection. """ diff --git a/Initial_Condition.py b/Initial_Condition.py index 4015d2af17c32cf66ba2e149364a7fc97749b4d3..625164fd265c8f41d556fbbcc4812a804a940a00 100644 --- a/Initial_Condition.py +++ b/Initial_Condition.py @@ -2,8 +2,6 @@ """ @author: Laura C. Kühle -TODO: Add is_smooth() to DiscontinuousConstant class -> Done - """ import numpy as np diff --git a/Limiter.py b/Limiter.py index 2734df71d9ff9c5559a0fe8d6ebcd5fc68c46c27..567b491491b4c972dc03907eb2997d994622d54c 100644 --- a/Limiter.py +++ b/Limiter.py @@ -47,14 +47,14 @@ class Limiter(object): Parameters ---------- - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. - cell + cell: int Index of cell. Returns ------- - np.array + ndarray Matrix of updated projection for each polynomial degree. """ @@ -117,14 +117,14 @@ class MinMod(Limiter): Parameters ---------- - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. cell : int Index of cell. Returns ------- - adapted_projection : np.array + adapted_projection : ndarray Matrix of updated projection for each polynomial degree. """ @@ -145,7 +145,7 @@ class MinMod(Limiter): Parameters ---------- - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. cell : int Index of cell. @@ -154,7 +154,7 @@ class MinMod(Limiter): Returns ------- - boolean + bool Flag whether cell should be adjusted. """ @@ -170,7 +170,7 @@ class MinMod(Limiter): Parameters ---------- - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. cell : int Index of cell. @@ -237,7 +237,7 @@ class ModifiedMinMod(MinMod): Parameters ---------- - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. cell : int Index of cell. @@ -246,7 +246,7 @@ class ModifiedMinMod(MinMod): Returns ------- - boolean + bool Flag whether cell should be adjusted. """ diff --git a/Plotting.py b/Plotting.py index 2ce4ec9c53c829c196f78a98a655fc69839e3fad..0406f13f081a3d1fe22eabba5dd45614c2be8f0a 100644 --- a/Plotting.py +++ b/Plotting.py @@ -102,7 +102,7 @@ def plot_shock_tube(num_grid_cells: int, troubled_cell_history: list, time_histo Number of cells in the mesh. Usually exponential of 2. troubled_cell_history : list List of detected troubled cells for each time step. - time_history: list + time_history : list List of value of each time step. """ @@ -223,9 +223,9 @@ def calculate_exact_solution(mesh: ndarray, cell_len: float, wave_speed: float, Returns ------- - grid: ndarray + grid : ndarray Array containing evaluation grid for a function. - exact: ndarray + exact : ndarray Array containing exact evaluation of a function. """ @@ -258,9 +258,9 @@ def plot_classification_accuracy(evaluation_dict: dict, colors: dict) -> None: Parameters ---------- - evaluation_dict: dict + evaluation_dict : dict Dictionary containing classification evaluation data. - colors: dict + colors : dict Dictionary containing plotting colors. """ @@ -292,9 +292,9 @@ def plot_boxplot(evaluation_dict: dict, colors: dict) -> None: Parameters ---------- - evaluation_dict: dict + evaluation_dict : dict Dictionary containing classification evaluation data. - colors: dict + colors : dict Dictionary containing plotting colors. """ diff --git a/Quadrature.py b/Quadrature.py index 9b4cc8922717d2f6b62d852c327f72bc575cbed7..c9920a461c2e9c43535d2d76acf653e0102267b1 100644 --- a/Quadrature.py +++ b/Quadrature.py @@ -15,9 +15,9 @@ class Quadrature(object): ---------- num_eval_points : int Number of evaluation points per cell used for approximation. - eval_points : np.array + eval_points : ndarray Evaluation points per cell used for approximation. - weights : np.array + weights : ndarray Weights used for approximation calculation. Methods @@ -80,9 +80,9 @@ class Gauss(Quadrature): ---------- num_eval_points : int Number of evaluation points per cell used for approximation. - eval_points : np.array + eval_points : ndarray Evaluation points per cell used for approximation. - weights : np.array + weights : ndarray Weights used for approximation calculation. Methods diff --git a/Troubled_Cell_Detector.py b/Troubled_Cell_Detector.py index 0dd15ec5ab2eb6c17869fc9363814f1aa13837f2..e73d6d5d9629002dce37c7a357618bfea3bb88bd 100644 --- a/Troubled_Cell_Detector.py +++ b/Troubled_Cell_Detector.py @@ -53,7 +53,7 @@ class TroubledCellDetector(object): Parameters ---------- - mesh : array + mesh : ndarray List of mesh valuation points. wave_speed : float Speed of wave in rightward direction. @@ -123,7 +123,7 @@ class TroubledCellDetector(object): Parameters ---------- - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. """ @@ -137,14 +137,14 @@ class TroubledCellDetector(object): Parameters ---------- - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. stencil_length : int Size of data array. Returns ------- - np.array + ndarray Matrix containing cell averages and reconstructions for initial projection. """ @@ -164,11 +164,11 @@ class TroubledCellDetector(object): Parameters ---------- - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. troubled_cell_history : list List of detected troubled cells for each time step. - time_history: + time_history : list List of value of each time step. """ @@ -184,12 +184,12 @@ class TroubledCellDetector(object): Parameters ---------- - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. Returns ------- - max_error + max_error : float Maximum error between exact and approximate solution. """ @@ -225,9 +225,14 @@ class NoDetection(TroubledCellDetector): Parameters ---------- - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. + Returns + ------- + list + List of indices for all detected troubled cells. + """ return [] @@ -239,7 +244,7 @@ class ArtificialNeuralNetwork(TroubledCellDetector): ---------- stencil_length : int Size of input data array. - model : ANNModel object + model : torch.nn.Model ANN model instance for evaluation. Methods @@ -280,7 +285,7 @@ class ArtificialNeuralNetwork(TroubledCellDetector): Parameters ---------- - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. Returns @@ -345,7 +350,7 @@ class WaveletDetector(TroubledCellDetector): Parameters ---------- - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. Returns @@ -362,12 +367,12 @@ class WaveletDetector(TroubledCellDetector): Parameters ---------- - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. Returns ------- - np.array + ndarray Matrix of wavelet coefficients. """ @@ -383,9 +388,9 @@ class WaveletDetector(TroubledCellDetector): Parameters ---------- - multiwavelet_coeffs : np.array + multiwavelet_coeffs : ndarray Matrix of multiwavelet coefficients. - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. Returns @@ -403,11 +408,11 @@ class WaveletDetector(TroubledCellDetector): Parameters ---------- - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. troubled_cell_history : list List of detected troubled cells for each time step. - time_history: + time_history : list List of value of each time step. """ @@ -424,12 +429,12 @@ class WaveletDetector(TroubledCellDetector): Parameters ---------- - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. Returns ------- - np.array + ndarray Matrix of projection on coarse grid for each polynomial degree. """ @@ -453,12 +458,12 @@ class WaveletDetector(TroubledCellDetector): Parameters ---------- - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. Returns ------- - max_error + max_error : float Maximum error between exact and approximate solution. """ @@ -487,7 +492,7 @@ class WaveletDetector(TroubledCellDetector): Parameters ---------- - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. """ @@ -540,9 +545,9 @@ class Boxplot(WaveletDetector): Parameters ---------- - multiwavelet_coeffs : np.array + multiwavelet_coeffs : ndarray Matrix of multiwavelet coefficients. - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. Returns @@ -619,9 +624,9 @@ class Theoretical(WaveletDetector): Parameters ---------- - multiwavelet_coeffs : np.array + multiwavelet_coeffs : ndarray Matrix of multiwavelet coefficients. - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. Returns @@ -645,16 +650,16 @@ class Theoretical(WaveletDetector): Parameters ---------- - multiwavelet_coeffs : np.array + multiwavelet_coeffs : ndarray Matrix of multiwavelet coefficients. cell : int Index of cell. - max_avg + max_avg : float Maximum average of projection. Returns ------- - boolean + bool Flag whether cell is troubled. """ diff --git a/Update_Scheme.py b/Update_Scheme.py index 5d91109a0eb175634b76ebabb3f07c813dc077ef..8200949a898d702d668abba2a9cce4e1e8b2608b 100644 --- a/Update_Scheme.py +++ b/Update_Scheme.py @@ -16,9 +16,9 @@ class UpdateScheme(object): Attributes ---------- - stiffness_matrix : np.array + stiffness_matrix : ndarray Matrix - boundary_matrix : np.array + boundary_matrix : ndarray Matrix Methods @@ -85,14 +85,14 @@ class UpdateScheme(object): Parameters ---------- - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. cfl_number : float CFL number to ensure stability. Returns ------- - current_projection : np.array + current_projection : ndarray Matrix of projection of current update step for each polynomial degree. troubled_cells : list List of indices for all detected troubled cells. @@ -107,14 +107,14 @@ class UpdateScheme(object): Parameters ---------- - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. cfl_number : float CFL number to ensure stability. Returns ------- - current_projection : np.array + current_projection : ndarray Matrix of projection of current update step for each polynomial degree. troubled_cells : list List of indices for all detected troubled cells. @@ -127,12 +127,12 @@ class UpdateScheme(object): Parameters ---------- - current_projection : np.array + current_projection : ndarray Matrix of projection of current update step for each polynomial degree. Returns ------- - new_projection : np.array + new_projection : ndarray Matrix of updated projection for each polynomial degree. troubled_cells : list List of indices for all detected troubled cells. @@ -153,12 +153,12 @@ class UpdateScheme(object): Parameters ---------- - current_projection : np.array + current_projection : ndarray Matrix of projection of current update step for each polynomial degree. Returns ------- - current_projection : np.array + current_projection : ndarray Matrix of projection of current update step for each polynomial degree. """ @@ -181,14 +181,14 @@ class SSPRK3(UpdateScheme): Parameters ---------- - projection : np.array + projection : ndarray Matrix of projection for each polynomial degree. cfl_number : float CFL number to ensure stability. Returns ------- - current_projection : np.array + current_projection : ndarray Matrix of projection of current update step for each polynomial degree. troubled_cells : list List of indices for all detected troubled cells. @@ -213,18 +213,18 @@ class SSPRK3(UpdateScheme): return current_projection, troubled_cells def _apply_first_step(self, original_projection, cfl_number): - """Apply first step of SSPRK3. + """Applies first step of SSPRK3. Parameters ---------- - original_projection : np.array + original_projection : ndarray Matrix of original projection for each polynomial degree. cfl_number : float CFL number to ensure stability. Returns ------- - np.array + ndarray Matrix of updated projection for each polynomial degree. """ @@ -232,20 +232,20 @@ class SSPRK3(UpdateScheme): return original_projection + (cfl_number*right_hand_side) def _apply_second_step(self, original_projection, current_projection, cfl_number): - """Apply second step of SSPRK3. + """Applies second step of SSPRK3. Parameters ---------- - original_projection : np.array + original_projection : ndarray Matrix of original projection for each polynomial degree. - current_projection : np.array + current_projection : ndarray Matrix of projection of current update step for each polynomial degree. cfl_number : float CFL number to ensure stability. Returns ------- - np.array + ndarray Matrix of updated projection for each polynomial degree. """ @@ -253,20 +253,20 @@ class SSPRK3(UpdateScheme): return 1/4 * (3*original_projection + (current_projection + cfl_number*right_hand_side)) def _apply_third_step(self, original_projection, current_projection, cfl_number): - """Apply third step of SSPRK3. + """Applies third step of SSPRK3. Parameter --------- - original_projection : np.array + original_projection : ndarray Matrix of original projection for each polynomial degree. - current_projection : np.array + current_projection : ndarray Matrix of projection of current update step for each polynomial degree. cfl_number : float CFL number to ensure stability. Returns ------- - np.array + ndarray Matrix of updated projection for each polynomial degree. """ @@ -274,16 +274,16 @@ class SSPRK3(UpdateScheme): return 1/3 * (original_projection + 2*(current_projection + cfl_number*right_hand_side)) def _update_right_hand_side(self, current_projection): - """Update right-hand side. + """Updates right-hand side. Parameter --------- - current_projection : np.array + current_projection : ndarray Matrix of projection of current update step for each polynomial degree. Returns ------- - np.array + ndarray Matrix of right-hand side. """