klaundal / lompe

MIT License
15 stars 8 forks source link

index error with 3 regularization parameters #51

Open hayleyclev opened 3 weeks ago

hayleyclev commented 3 weeks ago

I am receiving an error message related to the reg_E and run_inversion functions within lompe/lompe/model/model.py with the addition of the third regularization parameter. The existing version of the code I run which calls this funcrion has worked previously with just l1 and l2. Any variation of FAC_reg with any variation of tuples or floats for l1, l2, an l3 produce this same error. I initially followed the documentation/descriptions for these (i.e. following: ```Parameters

    l1 : float or tuple
        Damping parameter for model norm. If FAC_reg=True l1 can be a tuple 
        where the first and second entry target the FAC and E norm, 
        respectively. If it is just a float the E norm will be ignored.
    l2 : float
        Damping parameter for variation in the magnetic eastward direction.
        Functionality similar to l1 in regards to FAC_reg.
    l3 : float
        Damping parameter for variation in the magnetic northward direction
        Functionality similar to l1 in regards to FAC_reg.
    FAC_reg : boolean
        Activates FAC based regularization if True (default is False). Read
        l1 description for details on mixed FAC and E 2-norm regularization.```)

Error message:

Traceback (most recent call last):
  File "/Users/clevenger/Projects/isr_3d_vefs/isr_3d_vefs/pfrr_runscript.py", line 59, in <module>
    run_lompe_pfisr(start_time, end_time, time_step, Kp, x_resolution,
  File "/Users/clevenger/Projects/isr_3d_vefs/isr_3d_vefs/pfrr_run_lompe.py", line 120, in run_lompe_pfisr
    gtg, ltl = model.run_inversion(l1 = 1, l2 = 0.1, l3 = 0.1, FAC_reg=False)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/clevenger/miniconda3/envs/lompe/lib/python3.11/site-packages/lompe/model/model.py", line 434, in run_inversion
    LTL += reg_E(self, l1, l2, l3)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/clevenger/miniconda3/envs/lompe/lib/python3.11/site-packages/lompe/model/model.py", line 399, in reg_E
    LTL_l1 = np.eye(self.GTG.shape[0])
                    ~~~~~~~~~~~~~~^^^
IndexError: tuple index out of range

MWE (just including where run_inversion function is called, at the end of this block):

# Loop through times and save
    for i, (stime, etime) in enumerate(time_intervals):
        t = stime
        print("t: ",t)

        SH = lambda lon = grid.lon, lat = grid.lat: hardy_EUV(lon, lat, Kp, t, 'hall'    )
        SP = lambda lon = grid.lon, lat = grid.lat: hardy_EUV(lon, lat, Kp, t, 'pedersen')

        model.clear_model(Hall_Pedersen_conductance = (SH, SP)) # reset

        # Add datasets for this time
        if pfisrfn:
            model.add_data(pfisr_data[i])
        if pokermagfn:
            model.add_data(mag_data[i])
        if superdarn_direc:
            model.add_data(superdarn_kod_data[i])
            #model.add_data(superdarn_ksr_data[i])
        if swarm_a_prime:
            model.add_data(swarm_a_mag_data[i])
        if swarm_b_prime:
            model.add_data(swarm_b_mag_data[i])
        if swarm_c_prime:
            model.add_data(swarm_c_mag_data[i])

        # Run model
        #gtg, ltl = model.run_inversion(l1 = 1, l2 = 0.1)
        gtg, ltl = model.run_inversion(l1 = 1, l2 = 0.1, l3 = 0.1, FAC_reg=False)

Any insight to properly calling run_inversion with this new parameter will be greatly appreciated. Thanks!

BingMM commented 3 weeks ago

Hi Hayley,

I just pulled the latest version of Lompe and ran the following tests. They executed successfully:

gtg, ltl = model.run_inversion()
gtg, ltl = model.run_inversion(l1=1)
gtg, ltl = model.run_inversion(l1=1, l2=1)
gtg, ltl = model.run_inversion(l1=1, l2=1, l3=1)

gtg, ltl = model.run_inversion(FAC_reg=True)
gtg, ltl = model.run_inversion(l1=1, FAC_reg=True)
gtg, ltl = model.run_inversion(l1=1, l2=1, FAC_reg=True)
gtg, ltl = model.run_inversion(l1=1, l2=1, l3=1, FAC_reg=True)

try:
    gtg, ltl = model.run_inversion(l1=(1, 1))
except:
    print('Not supposed to work')

gtg, ltl = model.run_inversion(l1=(1,1), FAC_reg=True)
gtg, ltl = model.run_inversion(l1=(1,1), l2=(1,1), FAC_reg=True)
gtg, ltl = model.run_inversion(l1=(1,1), l2=(1,1), l3=(1,1), FAC_reg=True)
gtg, ltl = model.run_inversion(l1=0, l2=(1,1), l3=1, FAC_reg=True)

Unless you have made changes to model.py, your error code suggests that you are running an old version of Lompe. The lines numbers highlighted are not consistent with the current version.

Would you mind trying to pull the latest version of Lompe and see if the issue persists?