77@app .cell (hide_code = True )
88def _ ():
99 import marimo as mo
10+
1011 return (mo ,)
1112
1213
@@ -21,11 +22,6 @@ def _(mo):
2122 - `i_point`: Index konkrétního bodu v nepravidelné síti.
2223 - `i_sample`: Index vzorku (realizace) náhodného pole.
2324 - `i_dim`: Označení prostorové osy (např. 'x', 'y').
24-
25- **Datové proměnné (data_vars):**
26- - `X`: Matice prostorových souřadnic bodů. Má tvar `[i_dim, i_point]`.
27- - `QA`: První vygenerované náhodné pole. Má tvar `[i_point, i_sample]`.
28- - `QB`: Druhé vygenerované náhodné pole. Má tvar `[i_point, i_sample]`.
2925 """ )
3026 return
3127
@@ -34,132 +30,93 @@ def _(mo):
3430def _ ():
3531 import xarray as xr
3632 import numpy as np
37- import matplotlib .pyplot as plt
38- from scipy .stats import binned_statistic_2d
33+ import analyza
3934
4035 n_points = 1000
41- n_samples = 100
36+ n_samples_A = 10
37+ n_samples_B = 20
4238 n_dim = 2
4339
4440 X_data = np .random .rand (n_dim , n_points )
45- QA_data = np .random .uniform (0.01 , 1.0 , size = (n_points , n_samples ))
46- QB_data = np .random .normal (loc = 0.5 , scale = 0.15 , size = (n_points , n_samples ))
47- QB_data = np .clip (QB_data , 0.01 , None )
41+
42+ # Testovací pole = 10**( np.random.normal(N, loc = -10, scale=3))
43+ QA_data = 10 ** (np .random .normal (loc = - 10 , scale = 3 , size = (n_points , n_samples_A )))
44+
45+ # případně zvětšit scale pro větší rozdíl průměrů polí
46+ QB_data = 10 ** (np .random .normal (loc = - 10 , scale = 5 , size = (n_points , n_samples_B )))
4847
4948 ds = xr .Dataset (
5049 data_vars = {
5150 "X" : (("i_dim" , "i_point" ), X_data ),
52- "QA" : (("i_point" , "i_sample " ), QA_data ),
53- "QB" : (("i_point" , "i_sample " ), QB_data ),
51+ "QA" : (("i_point" , "i_sample_A " ), QA_data ),
52+ "QB" : (("i_point" , "i_sample_B " ), QB_data ),
5453 },
5554 coords = {
5655 "i_point" : np .arange (n_points ),
57- "i_sample" : np .arange (n_samples ),
56+ "i_sample_A" : np .arange (n_samples_A ),
57+ "i_sample_B" : np .arange (n_samples_B ),
5858 "i_dim" : ["x" , "y" ]
5959 }
6060 )
61+ return analyza , ds
62+
63+
64+ @app .cell (hide_code = True )
65+ def _ (ds ):
66+ # Průměry pro každé ze dvou polí. Průměry do samostatné buňky.
67+ mean_QA = ds ["QA" ].mean (dim = "i_sample_A" )
68+ mean_QB = ds ["QB" ].mean (dim = "i_sample_B" )
69+ return mean_QA , mean_QB
70+
71+
72+ @app .cell (hide_code = True )
73+ def _ (analyza , ds , mean_QA , mean_QB ):
74+ grid_mean_A , x_edges , y_edges = analyza .bin_single_field (ds ["X" ], mean_QA )
75+ grid_mean_B , _ , _ = analyza .bin_single_field (ds ["X" ], mean_QB )
76+
77+ fig_means = analyza .plot_means_two_columns (x_edges , y_edges , grid_mean_A , grid_mean_B )
78+ fig_means
79+ return x_edges , y_edges
80+
81+
82+ @app .cell (hide_code = True )
83+ def _ (analyza , ds ):
84+ grid_A , _ , _ = analyza .bin_all_samples (ds ["X" ], ds ["QA" ])
85+ grid_B , _ , _ = analyza .bin_all_samples (ds ["X" ], ds ["QB" ])
86+
87+ t_stat , p_value = analyza .perform_ttest (grid_A , grid_B )
88+ return p_value , t_stat
6189
62- ds ["QA" ].attrs ["long_name" ] = "Porovnávaná veličina A"
63- ds ["QA" ].attrs ["units" ] = "-"
64- ds ["QB" ].attrs ["long_name" ] = "Porovnávaná veličina B"
65- ds ["QB" ].attrs ["units" ] = "-"
66-
67- stats = xr .Dataset ({
68- "mean_QA" : ds ["QA" ].mean (dim = "i_sample" ),
69- "var_QA" : ds ["QA" ].var (dim = "i_sample" ),
70- "mean_QB" : ds ["QB" ].mean (dim = "i_sample" ),
71- "var_QB" : ds ["QB" ].var (dim = "i_sample" )
72- })
73-
74- x = ds ["X" ].sel (i_dim = "x" ).values
75- y = ds ["X" ].sel (i_dim = "y" ).values
76-
77- # Pro grafy průměru a rozptylu použít imgshow nebo podobnou funkci
78- num_bins = 20
79- bins = (num_bins , num_bins )
80-
81- binned_mean_QA = binned_statistic_2d (x , y , stats ["mean_QA" ].values , statistic = 'mean' , bins = bins )
82- binned_var_QA = binned_statistic_2d (x , y , stats ["var_QA" ].values , statistic = 'mean' , bins = bins )
83- binned_mean_QB = binned_statistic_2d (x , y , stats ["mean_QB" ].values , statistic = 'mean' , bins = bins )
84- binned_var_QB = binned_statistic_2d (x , y , stats ["var_QB" ].values , statistic = 'mean' , bins = bins )
85-
86- x_edges = binned_mean_QA .x_edge
87- y_edges = binned_mean_QA .y_edge
88-
89- fig , axes = plt .subplots (2 , 3 , figsize = (15 , 10 ))
90-
91- im1 = axes [0 , 0 ].pcolormesh (x_edges , y_edges , binned_mean_QA .statistic .T , cmap = 'viridis' , shading = 'flat' )
92- axes [0 , 0 ].set_title ("QA: Mapa průměru" )
93- axes [0 , 0 ].set_aspect ('equal' )
94- plt .colorbar (im1 , ax = axes [0 , 0 ])
95-
96- im2 = axes [0 , 1 ].pcolormesh (x_edges , y_edges , binned_var_QA .statistic .T , cmap = 'magma' , shading = 'flat' )
97- axes [0 , 1 ].set_title ("QA: Mapa rozptylu" )
98- axes [0 , 1 ].set_aspect ('equal' )
99- plt .colorbar (im2 , ax = axes [0 , 1 ])
100-
101- axes [0 , 2 ].hist (ds ["QA" ].values .flatten (), bins = 30 , color = 'skyblue' , edgecolor = 'black' )
102- axes [0 , 2 ].set_title ("QA: Histogram" )
103-
104- im3 = axes [1 , 0 ].pcolormesh (x_edges , y_edges , binned_mean_QB .statistic .T , cmap = 'viridis' , shading = 'flat' )
105- axes [1 , 0 ].set_title ("QB: Mapa průměru" )
106- axes [1 , 0 ].set_aspect ('equal' )
107- plt .colorbar (im3 , ax = axes [1 , 0 ])
108-
109- im4 = axes [1 , 1 ].pcolormesh (x_edges , y_edges , binned_var_QB .statistic .T , cmap = 'magma' , shading = 'flat' )
110- axes [1 , 1 ].set_title ("QB: Mapa rozptylu" )
111- axes [1 , 1 ].set_aspect ('equal' )
112- plt .colorbar (im4 , ax = axes [1 , 1 ])
113-
114- axes [1 , 2 ].hist (ds ["QB" ].values .flatten (), bins = 30 , color = 'salmon' , edgecolor = 'black' )
115- axes [1 , 2 ].set_title ("QB: Histogram" )
116-
117- plt .tight_layout ()
118- plt .show ()
119-
120- # Vypočtěte různé druhy průměrů (aritmetický, geometrický, harmonický) na podčtvercích (multi-scale) pro různé velikosti oken. Zatím pro jednu velikost okna.
121- q_values = stats ["mean_QA" ].values
122- grid_size = 10
123- A_arith = np .zeros ((grid_size , grid_size ))
124- A_geom = np .zeros ((grid_size , grid_size ))
125- A_harm = np .zeros ((grid_size , grid_size ))
126-
127- dx = 1.0 / grid_size
128- dy = 1.0 / grid_size
129-
130- for i in range (grid_size ):
131- for j in range (grid_size ):
132- mask = (x >= i * dx ) & (x < (i + 1 )* dx ) & (y >= j * dy ) & (y < (j + 1 )* dy )
133- q_in_window = q_values [mask ]
134-
135- if len (q_in_window ) > 0 :
136- A_arith [j , i ] = np .mean (q_in_window )
137- A_geom [j , i ] = np .exp (np .mean (np .log (q_in_window )))
138- A_harm [j , i ] = 1.0 / np .mean (1.0 / q_in_window )
139- else :
140- A_arith [j , i ] = np .nan
141- A_geom [j , i ] = np .nan
142- A_harm [j , i ] = np .nan
143-
144- fig2 , axes2 = plt .subplots (1 , 3 , figsize = (15 , 5 ))
145-
146- im5 = axes2 [0 ].imshow (A_arith , origin = 'lower' , extent = [0 , 1 , 0 , 1 ], cmap = 'viridis' )
147- axes2 [0 ].set_title ("Aritmetický průměr" )
148- plt .colorbar (im5 , ax = axes2 [0 ])
149-
150- im6 = axes2 [1 ].imshow (A_geom , origin = 'lower' , extent = [0 , 1 , 0 , 1 ], cmap = 'viridis' )
151- axes2 [1 ].set_title ("Geometrický průměr" )
152- plt .colorbar (im6 , ax = axes2 [1 ])
153-
154- im7 = axes2 [2 ].imshow (A_harm , origin = 'lower' , extent = [0 , 1 , 0 , 1 ], cmap = 'viridis' )
155- axes2 [2 ].set_title ("Harmonický průměr" )
156- plt .colorbar (im7 , ax = axes2 [2 ])
157-
158- plt .tight_layout ()
159- plt .show ()
160-
161- return ds , fig , fig2
90+
91+ @app .cell (hide_code = True )
92+ def _ (analyza , p_value , t_stat , x_edges , y_edges ):
93+ fig_test = analyza .plot_ttest_results (x_edges , y_edges , t_stat , p_value )
94+ fig_test
95+ return
96+
97+
98+ @app .cell (hide_code = True )
99+ def _ (analyza , ds ):
100+ x_vals = ds ["X" ].sel (i_dim = "x" ).values
101+ y_vals = ds ["X" ].sel (i_dim = "y" ).values
102+ q_a_vals = ds ["QA" ].values
103+ q_b_vals = ds ["QB" ].values
104+
105+ fig_multiscale_maps , fig_multiscale_line = analyza .multiscale_analysis (x_vals , y_vals , q_a_vals , q_b_vals )
106+ return fig_multiscale_line , fig_multiscale_maps
107+
108+
109+ @app .cell (hide_code = True )
110+ def _ (fig_multiscale_maps ):
111+ fig_multiscale_maps
112+ return
113+
114+
115+ @app .cell (hide_code = True )
116+ def _ (fig_multiscale_line ):
117+ fig_multiscale_line
118+ return
162119
163120
164121if __name__ == "__main__" :
165- app .run ()
122+ app .run ()
0 commit comments