File: my_work.py

package info (click to toggle)
datalab 1.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 36,260 kB
  • sloc: python: 29,592; makefile: 3
file content (114 lines) | stat: -rw-r--r-- 3,785 bytes parent folder | download
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -*- coding: utf-8 -*-

"""
Tutorial 'Working with Spyder'
==============================

Generate 1D data that can be used in the tutorials to represent some results.
"""

from __future__ import annotations

import numpy as np
from sigima.client import SimpleRemoteProxy


def generate_1d_data(
    amplitude: float,
    peak_position: float,
    peak_width: float,
    relaxation_oscillations: bool = True,
    noise: bool = True,
    noise_std: float = 0.1,
    x_min: float = -1,
    x_max: float = 1,
    num_points: int = 1000,
) -> tuple[np.ndarray, np.ndarray]:
    """Generate 1D data that can be used in the tutorials to represent some results.

    Args:
        amplitude: Amplitude of the peak.
        peak_position: Position of the peak.
        peak_width: Width of the peak.
        relaxation_oscillations: Whether to add relaxation oscillations to the data,
         by default True.
        noise: Whether to add noise to the data, by default True.
        noise_std: Standard deviation of the noise, by default 0.1.
        x_min: Minimum value of the x axis, by default -1.
        x_max: Maximum value of the x axis, by default 1.
        num_points: Number of points in the generated data, by default 100.

    Returns:
        Generated data (x, y; where y is a 1D array and x is a 1D array).
    """
    x = np.linspace(x_min, x_max, num_points)
    y = amplitude * np.exp(-(((x - peak_position) / peak_width) ** 2))
    if relaxation_oscillations:
        y += 0.1 * np.sin(10 * x)
    if noise:
        y += np.random.normal(scale=noise_std, size=num_points)
    return x, y


def generate_2d_data(
    num_lines: int = 10,
    noise_std: float = 0.1,
    x_min: float = -1,
    x_max: float = 1,
    num_points: int = 100,
    debug_with_datalab: bool = False,
) -> tuple[np.ndarray, np.ndarray]:
    """Generate 2D data that can be used in the tutorials to represent some results.

    Args:
        num_lines: Number of lines in the generated data, by default 10.
        noise_std: Standard deviation of the noise, by default 0.1.
        x_min: Minimum value of the x axis, by default -1.
        x_max: Maximum value of the x axis, by default 1.
        num_points: Number of points in the generated data, by default 100.
        debug_with_datalab: Whether to use the DataLab to debug the function,
         by default False.

    Returns:
        Generated data (x, y; where y is a 2D array and x is a 1D array).
    """
    proxy = None
    if debug_with_datalab:
        proxy = SimpleRemoteProxy()
        proxy.connect()
    z = np.zeros((num_lines, num_points))
    for i in range(num_lines):
        amplitude = 0.1 * i**2
        peak_position = 0.5 * i**2
        peak_width = 0.1 * i**2
        x, y = generate_1d_data(
            amplitude,
            peak_position,
            peak_width,
            relaxation_oscillations=True,
            noise=True,
            noise_std=noise_std,
            x_min=x_min,
            x_max=x_max,
            num_points=num_points,
        )
        z[i] = y
        if proxy is not None:
            proxy.add_signal(f"Line {i}", x, y)
    return x, z


def test_my_1d_algorithm() -> np.ndarray:
    """Generate 1D data that can be used in the tutorials to represent some results."""
    # Generate 1D data using the function generate_1d_data:
    data = generate_1d_data(1, 0, 0.1, relaxation_oscillations=True, noise=True)
    return data


def test_my_2d_algorithm(debug_with_datalab: bool = False) -> np.ndarray:
    """Generate 2D data that can be used in the tutorials to represent some results."""
    # Generate 2D data using the function generate_2d_data:
    data = generate_2d_data(
        num_lines=10, noise_std=0.1, debug_with_datalab=debug_with_datalab
    )
    return data