Skip to content
Snippets Groups Projects
test_data_utils.py 13 KiB
Newer Older
  • Learn to ignore specific revisions
  • Pedro Magalhães's avatar
    Pedro Magalhães committed
    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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    # imports
    
    # standard
        
    import random
    
    import math
    
    from statistics import mean
    
    # local, internal
    
    from src.topupopt.data.misc import utils
    
    #******************************************************************************
    #******************************************************************************
    
    class TestDataUtils:
        
        # *************************************************************************
        # *************************************************************************
    
        def test_profile_synching2(self):
            
            integration_result = 10446
            ratio_min_avg = 0.2
            
            avg_state = [
                2.66,
                2.34,
                3.54,
                7.42,
                11.72,
                16.94,
                17.94,
                17.98,
                14.1,
                10.48,
                6.74,
                3.16]
            
            time_interval_durations = [
                31, # jan
                28, # fev
                31, # mar
                30, # apr
                31, # may
                30, # june
                31, # july
                31, # august
                30, # september
                31, # october
                30, # november
                31  # december
                ] 
            
            # *********************************************************************
            # *********************************************************************
            
            # state correlates with output
            
            new_profile = utils.create_profile_using_time_weighted_state(
                integration_result=integration_result, 
                avg_state=avg_state, 
                time_interval_durations=time_interval_durations, 
                ratio_min_avg=ratio_min_avg,
                state_correlates_with_output=False
                )
            
            expected_result = [
                1500.0513102636057, 
                1436.2189684321309, 
                1500.0513102636044, 
                1206.6051909345115, 
                896.2493366213225, 
                525.7218723351705, 
                283.3475134825171, 
                185.83058429361876, 
                270.8127439165165, 
                538.2566419011698, 
                861.4985340132666, 
                1241.355993542566
                ]
            
            abs_tol = 1e-3
            
            assert math.isclose(
                sum(new_profile),
                integration_result,
                abs_tol=abs_tol
                )   
            
            for sample, expected_sample in zip(new_profile, expected_result):
                
                assert math.isclose(
                    sample,
                    expected_sample,
                    abs_tol=abs_tol
                    )
                
            # *********************************************************************
            # *********************************************************************
            
            # state does not correlate with output
            
            # state correlates with output
            
            new_profile = utils.create_profile_using_time_weighted_state(
                integration_result=integration_result, 
                avg_state=avg_state, 
                time_interval_durations=time_interval_durations, 
                ratio_min_avg=ratio_min_avg,
                state_correlates_with_output=True
                )
            
            expected_result = [
                274.3377308322865, 
                166.45500417060902, 
                274.33773083228533, 
                510.54549399699533, 
                878.1397044745678, 
                1191.4288125963367, 
                1491.041527613374, 
                1588.5584568022714, 
                1446.3379410149894, 
                1236.132399194721, 
                855.6521509182398, 
                533.0330475533233
                ]
            
            abs_tol = 1e-3
            
            assert math.isclose(
                sum(new_profile),
                integration_result,
                abs_tol=abs_tol
                )   
            
            for sample, expected_sample in zip(new_profile, expected_result):
                
                assert math.isclose(
                    sample,
                    expected_sample,
                    abs_tol=abs_tol
                    )
                
            # *********************************************************************
            # *********************************************************************
            
            # find out the peaks of the sinusoidal profile
                
            pmax, pmin = utils.max_min_sinusoidal_profile(
                integration_result=integration_result,
                period=sum(time_interval_durations),
                time_interval_duration=mean(time_interval_durations),
                ratio_min_avg=ratio_min_avg
                )
            
            expected_pmax, expected_pmin = 1558.972133279683, 182.02786672031687
                
            assert math.isclose(
                pmax,
                expected_pmax,
                abs_tol=1e-3
                )
                
            assert math.isclose(
                pmin,
                expected_pmin,
                abs_tol=1e-3
                )
                
            # *********************************************************************
            # *********************************************************************
            
            # raise exception
            
            error_triggered = False
            time_interval_durations.pop(0)
            try:
                new_profile = utils.create_profile_using_time_weighted_state(
                    integration_result=integration_result, 
                    avg_state=avg_state, 
                    time_interval_durations=time_interval_durations, 
                    ratio_min_avg=ratio_min_avg,
                    state_correlates_with_output=True
                    )
            except ValueError:
                error_triggered = True
            assert error_triggered
            
            # *********************************************************************
            # *********************************************************************
            
        # *************************************************************************
        # *************************************************************************
        
        def test_profile_synching(self):
            
            # synch, normal, ex1
            
            profile = [1,2,3,4]
            reference_profile = [2,3,4,1]
            synched_profile = utils.synch_profile(profile, reference_profile)
            true_synched_profile = [2,3,4,1]
            assert repr(synched_profile) == repr(true_synched_profile)
            
            # synch, normal, ex2
            
            profile = [-2,-1,1,2,0]
            reference_profile = [2,3,4,1,5]
            synched_profile = utils.synch_profile(profile, reference_profile)
            true_synched_profile = [-1,0,1,-2,2]
            assert repr(synched_profile) == repr(true_synched_profile)
            
            # synch, alternative, ex1
            
            profile = [1,2,3,4]
            reference_profile = [2,3,4,1]
            synched_profile = utils.synch_profile(profile, reference_profile, synch=False)
            true_synched_profile = [3,2,1,4]
            assert repr(synched_profile) == repr(true_synched_profile)
            
            
        # *************************************************************************
        # *************************************************************************
        
        def test_profile_generation(self):
        
            # *********************************************************************
            
            # fixed time interval durations
            
            number_tests = 10
            
            for test_index in range(number_tests):
            
                integration_period = 365*24*3600
                
                number_intervals = random.randint(1,8760)
                
                phase_shift_radians = 2*math.pi*random.random()
                
                time_interval_durations = [
                    round(integration_period/number_intervals)
                    for i in range(number_intervals)
                    ]
                
                integration_result = 100
                
                ratio_min_avg = 0.2
                
                profile = utils.discrete_sinusoid_matching_integral(
                    integration_result, 
                    time_interval_durations, 
                    ratio_min_avg, 
                    phase_shift_radians=phase_shift_radians)
                
                assert math.isclose(
                    sum(profile),
                    integration_result,
                    abs_tol=0.01
                    )
            
            # *********************************************************************
            
            # import matplotlib.pyplot as plt
            
            # # Data for plotting
            # x = [i for i in range(number_intervals)]
            # y = profile
        
            # fig, ax = plt.subplots()
            # ax.plot(x, y)
        
            # ax.set(xlabel='time (s)', ylabel='voltage (mV)',
            #         title='About as simple as it gets, folks')
            # ax.grid()
        
            # #fig.savefig("test.png")
            # plt.show()
                
            # *********************************************************************
        
            # variable time step durations
            
            number_tests = 10
            
            for test_index in range(number_tests):
            
                number_intervals = random.randint(10,8760)
            
                time_interval_durations = [
                    random.random()*3.6e3
                    for i in range(number_intervals)
                    ]
            
                integration_period = sum(time_interval_durations)
                
                phase_shift_radians = 2*math.pi*random.random()
                
                integration_result = 100
                
                ratio_min_avg = 0.2
                
                profile = utils.discrete_sinusoid_matching_integral(
                    integration_result, 
                    time_interval_durations, 
                    ratio_min_avg, 
                    phase_shift_radians=phase_shift_radians)
                
                assert math.isclose(
                    sum(profile),
                    integration_result,
                    abs_tol=0.01
                    )
            
            # *********************************************************************
            
            # # import matplotlib.pyplot as plt
            
            # t = [sum(time_interval_durations[0:i])
            #      for i in range(len(time_interval_durations)+1)]
            
            # # Data for plotting
            # x = [(t[i+1]+t[i])*0.5
            #      for i in range(number_intervals)] # time interval's center point
            # y = profile
        
            # fig, ax = plt.subplots()
            # ax.plot(x, y)
        
            # ax.set(xlabel='time (s)', ylabel='voltage (mV)',
            #         title='About as simple as it gets, folks')
            # ax.grid()
        
            # #fig.savefig("test.png")
            # plt.show()
        
            # *********************************************************************
            
            # use the default phase shift
            
            integration_period = 365*24*3600
            
            number_intervals = random.randint(1,8760)
            
            time_interval_durations = [
                round(integration_period/number_intervals)
                for i in range(number_intervals)
                ]
            
            integration_result = 100
            
            ratio_min_avg = 0.2
            
            profile = utils.discrete_sinusoid_matching_integral(
                integration_result, 
                time_interval_durations, 
                ratio_min_avg)
            
            assert math.isclose(
                sum(profile),
                integration_result,
                abs_tol=0.01
                )
            
        # *************************************************************************
        # *************************************************************************
        
        def test_key_generation(self):
            
            # generate_pseudo_unique_key
            
            key_list = (str(random.random()) for i in range(10))
            
            new_key = utils.generate_pseudo_unique_key(key_list=key_list)
            
            assert new_key not in key_list
            
            # use an empty key list
            
            new_key = utils.generate_pseudo_unique_key(key_list=[])
            
            assert new_key not in key_list
            
            # use zero iterations to force an error
            
            error_triggered = False
            try:
                new_key = utils.generate_pseudo_unique_key(key_list=key_list,
                                                           max_iterations=0)
            except Exception:
                error_triggered = True
            assert error_triggered
            
            # use a seed number to trigger more iterations
            
            import uuid
            rand = random.Random()
            rand.seed(360)
            uuid.uuid4 = lambda: uuid.UUID(int=rand.getrandbits(128), version=4)
            
            key_list = ['3e225573-4e78-48c8-bb08-efbeeb795c22',
                        'f6d30428-15d1-41e9-a952-0742eaaa5a31',
                        '8c29b906-2518-41c5-ada8-07b83508b5b8',
                        'f9a72a39-1422-4a02-af97-906ce79c32a3',
                        'b6941a48-10cc-465d-bf53-178bd2939bd1']
            
            new_key = utils.generate_pseudo_unique_key(key_list=key_list)
            
            assert new_key not in key_list
        
        #**************************************************************************
        #**************************************************************************
    
    #******************************************************************************
    #******************************************************************************