Example #1
0
def main(data_sizes: List, approximation_ranks: List, increments: List,
         singular_values: RowVector, used_data_factory: Callable,
         results_path: str, experiment_type: str, power_method_iterations,
         _seed) -> None:
    """ The main function of this project

    This functions performs the desired experiment according to the given configuration.
    The function runs the random_svd and random_id for every combination of data_size, approximation rank and increment
    given in the config and saves all the results to a csv file in the results folder (given in the configuration).
    """
    seed(_seed)  # Seed the interpolative package of Scipy for later usage.
    results_log = DataLog(LogFields)  # Initializing an empty results log.
    random_svd_with_run_time: Callable = measure_time(random_svd)
    random_id_with_run_time: Callable = measure_time(random_id)

    for data_size in data_sizes:
        data_matrix: Matrix = used_data_factory(data_size, singular_values)

        for approximation_rank in approximation_ranks:
            next_singular_value: Scalar = singular_values[approximation_rank + 1] if \
                approximation_rank < len(singular_values) else singular_values[-1]

            for increment in increments:
                # Executing all the tested methods.
                print(
                    f'n={data_size}, k={approximation_rank}, l={approximation_rank + increment}'
                )
                random_svd_approximation, svd_duration = random_svd_with_run_time(
                    data_matrix, approximation_rank, increment)
                random_svd_accuracy: Scalar = estimate_spectral_norm_diff(
                    data_matrix, random_svd_approximation,
                    power_method_iterations)
                print(
                    f'runtime={svd_duration}, accuracy={random_svd_accuracy}')
                random_id_approximation, id_duration = random_id_with_run_time(
                    data_matrix, approximation_rank, increment)
                random_id_accuracy: Scalar = estimate_spectral_norm_diff(
                    data_matrix, random_id_approximation,
                    power_method_iterations)
                print(f'runtime={id_duration}, accuracy={random_id_accuracy}')

                # Appending all the experiment results to the log.
                results_log.append(LogFields.DataSize, data_size)
                results_log.append(LogFields.ApproximationRank,
                                   approximation_rank)
                results_log.append(LogFields.Increment,
                                   increment + approximation_rank)
                results_log.append(LogFields.NextSingularValue,
                                   next_singular_value)
                results_log.append(LogFields.RandomSVDAccuracy,
                                   random_svd_accuracy)
                results_log.append(LogFields.RandomIDAccuracy,
                                   random_id_accuracy)
                results_log.append(LogFields.RandomSVDDuration, svd_duration)
                results_log.append(LogFields.RandomIDDuration, id_duration)

    results_log.save_log(experiment_type + " results",
                         results_folder_path=results_path)
Example #2
0
    def test_rand(self):
        pymatrixid.seed('default')
        assert_(np.allclose(pymatrixid.rand(2), [0.8932059, 0.64500803], 1e-4))

        pymatrixid.seed(1234)
        x1 = pymatrixid.rand(2)
        assert_(np.allclose(x1, [0.7513823, 0.06861718], 1e-4))

        np.random.seed(1234)
        pymatrixid.seed()
        x2 = pymatrixid.rand(2)

        np.random.seed(1234)
        pymatrixid.seed(np.random.rand(55))
        x3 = pymatrixid.rand(2)

        assert_allclose(x1, x2)
        assert_allclose(x1, x3)
Example #3
0
    def test_rand(self):
        pymatrixid.seed('default')
        assert_(np.allclose(pymatrixid.rand(2), [0.8932059, 0.64500803], 1e-4))

        pymatrixid.seed(1234)
        x1 = pymatrixid.rand(2)
        assert_(np.allclose(x1, [0.7513823, 0.06861718], 1e-4))

        np.random.seed(1234)
        pymatrixid.seed()
        x2 = pymatrixid.rand(2)

        np.random.seed(1234)
        pymatrixid.seed(np.random.rand(55))
        x3 = pymatrixid.rand(2)

        assert_allclose(x1, x2)
        assert_allclose(x1, x3)