Debian Codegraph @

doc/intro/tutorials/my_work.py

raw
1 # -*- coding: utf-8 -*-
2
3 """
4 Tutorial 'Working with Spyder'
5 ==============================
6
7 Generate 1D data that can be used in the tutorials to represent some results.
8 """
9
10 from __future__ import annotations
11
12 import numpy as np
13 from sigima.client import SimpleRemoteProxy
14
15
16 def generate_1d_data(
17 amplitude: float,
18 peak_position: float,
19 peak_width: float,
20 relaxation_oscillations: bool = True,
21 noise: bool = True,
22 noise_std: float = 0.1,
23 x_min: float = -1,
24 x_max: float = 1,
25 num_points: int = 1000,
26 ) -> tuple[np.ndarray, np.ndarray]:
27 """Generate 1D data that can be used in the tutorials to represent some results.
28
29 Args:
30 amplitude: Amplitude of the peak.
31 peak_position: Position of the peak.
32 peak_width: Width of the peak.
33 relaxation_oscillations: Whether to add relaxation oscillations to the data,
34 by default True.
35 noise: Whether to add noise to the data, by default True.
36 noise_std: Standard deviation of the noise, by default 0.1.
37 x_min: Minimum value of the x axis, by default -1.
38 x_max: Maximum value of the x axis, by default 1.
39 num_points: Number of points in the generated data, by default 100.
40
41 Returns:
42 Generated data (x, y; where y is a 1D array and x is a 1D array).
43 """
44 x = np.linspace(x_min, x_max, num_points)
45 y = amplitude * np.exp(-(((x - peak_position) / peak_width) ** 2))
46 if relaxation_oscillations:
47 y += 0.1 * np.sin(10 * x)
48 if noise:
49 y += np.random.normal(scale=noise_std, size=num_points)
50 return x, y
51
52
53 def generate_2d_data(
54 num_lines: int = 10,
55 noise_std: float = 0.1,
56 x_min: float = -1,
57 x_max: float = 1,
58 num_points: int = 100,
59 debug_with_datalab: bool = False,
60 ) -> tuple[np.ndarray, np.ndarray]:
61 """Generate 2D data that can be used in the tutorials to represent some results.
62
63 Args:
64 num_lines: Number of lines in the generated data, by default 10.
65 noise_std: Standard deviation of the noise, by default 0.1.
66 x_min: Minimum value of the x axis, by default -1.
67 x_max: Maximum value of the x axis, by default 1.
68 num_points: Number of points in the generated data, by default 100.
69 debug_with_datalab: Whether to use the DataLab to debug the function,
70 by default False.
71
72 Returns:
73 Generated data (x, y; where y is a 2D array and x is a 1D array).
74 """
75 proxy = None
76 if debug_with_datalab:
77 proxy = SimpleRemoteProxy()
78 proxy.connect()
79 z = np.zeros((num_lines, num_points))
80 for i in range(num_lines):
81 amplitude = 0.1 * i**2
82 peak_position = 0.5 * i**2
83 peak_width = 0.1 * i**2
84 x, y = generate_1d_data(
85 amplitude,
86 peak_position,
87 peak_width,
88 relaxation_oscillations=True,
89 noise=True,
90 noise_std=noise_std,
91 x_min=x_min,
92 x_max=x_max,
93 num_points=num_points,
94 )
95 z[i] = y
96 if proxy is not None:
97 proxy.add_signal(f"Line {i}", x, y)
98 return x, z
99
100
101 def test_my_1d_algorithm() -> np.ndarray:
102 """Generate 1D data that can be used in the tutorials to represent some results."""
103 # Generate 1D data using the function generate_1d_data:
104 data = generate_1d_data(1, 0, 0.1, relaxation_oscillations=True, noise=True)
105 return data
106
107
108 def test_my_2d_algorithm(debug_with_datalab: bool = False) -> np.ndarray:
109 """Generate 2D data that can be used in the tutorials to represent some results."""
110 # Generate 2D data using the function generate_2d_data:
111 data = generate_2d_data(
112 num_lines=10, noise_std=0.1, debug_with_datalab=debug_with_datalab
113 )
114 return data