How to use formatted strings in numba.jit function?

Viewed 2236

Hello fellow programmers!

I want to get into numba to speed up my python program, so I am trying to get my code to work with the jit function. Unfortunately the print() function does not seem to work with formatted strings inside. This part of the function throws the error (the last line of code shows the print() function):

@jit()
def calculate_total_node_THD_func_real_data_with_topo_enhanced(self):

    smallest_harmonic = len(self.list_of_machines[0].harmonic_list_VH_v)
    list_for_longest_measurement = []
    for counter, machine in enumerate(self.list_of_machines):
        list_for_longest_measurement.append(len(machine.harmonic_list_VH_v[0][self.next_state[counter][0]]))  # for checking only needs to check one harmonic, we take the first [0]
    longest_measurement = np.max(list_for_longest_measurement)
    THD_phases = []
    THD_phases_with_topo = []
    harmonics_state_all_phases = []

    for phase in range(0, 3):
        total_THD_for_all_timesteps = []
        total_THD_for_all_timesteps_with_topo = []
        harmonics_state_phase = []

        for time_ in range(0, longest_measurement):
            time1 = time.perf_counter()
            if self.consider_topology == True:
                # power für alle maschinen für den Zeitpunkt [p_m1, ...., p_m5]
                # harmonischen 2...40 für den zeitpunkt time_ [h_m1_2-40, ..., h_m5_2-40]
                p_soll = np.zeros(shape=len(self.list_of_machines) + 1)
                i_harm_amp = np.zeros(shape=(len(self.list_of_machines) + 1, self.list_of_machines[0].smallest_harmonic - 1))
                i_harm_angle = np.zeros(shape=(len(self.list_of_machines) + 1, self.list_of_machines[0].smallest_harmonic - 1))
                # fill the matrices
                for counter, machine in enumerate(self.list_of_machines):
                    if len(machine.harmonic_list_VH_v[0][self.next_state[counter][0]]) <= time_:
                        p_soll[machine.position_in_grid] = machine.power[self.next_state[counter][0]] * 10 ** -3  # power already sampled down, do not use standardized power, power in W -> kW
                        i_harm_amp[machine.position_in_grid, :] = [machine.harmonic_list_CH_v[x][self.next_state[counter][0]][-1][phase] * math.sqrt(2) for x in range(0, machine.smallest_harmonic - 1)]
                        i_harm_angle[machine.position_in_grid, :] = [machine.harmonic_list_CH_p[x][self.next_state[counter][0]][-1][phase] for x in range(0, machine.smallest_harmonic - 1)]
                    else:
                        p_soll[machine.position_in_grid] = machine.power[self.next_state[counter][0]] * 10 ** -3  # power already sampled down, do not use standardized power, power in W -> kW
                        i_harm_amp[machine.position_in_grid, :] = [machine.harmonic_list_CH_v[x][self.next_state[counter][0]][time_][phase] * math.sqrt(2) for x in range(0, machine.smallest_harmonic - 1)]  # *sqrt(2) for converting RMS to amplitude
                        i_harm_angle[machine.position_in_grid, :] = [machine.harmonic_list_CH_p[x][self.next_state[counter][0]][time_][phase] for x in range(0, machine.smallest_harmonic - 1)]
                THD_fake_slack_node = self.grid_topo.harmonic_load_flow_calc(p_soll, i_harm_amp, i_harm_angle)

                total_THD_for_all_timesteps_with_topo.append(THD_fake_slack_node)

                print(f'{5}')

Output:

Traceback (most recent call last):
  File "C:/Users/Artur/Desktop/RL_framework/train.py", line 87, in <module>
    main()
  File "C:/Users/Artur/Desktop/RL_framework/train.py", line 77, in main
    duration = cf.training(episodes, env, agent, filename, topology=topology, enhanced_processing=enhanced_processing, CPUs_used=CPUs_used)
  File "C:\Users\Artur\Desktop\RL_framework\help_functions\custom_functions.py", line 163, in training
    save_interval = enhanced_training(episodes=range(episodes), env=env, agent=agent, log_data_qvalues=log_data_qvalues, log_data=log_data, filename=filename, CPUs_used=CPUs_used)
  File "C:\Users\Artur\Desktop\RL_framework\help_functions\custom_functions.py", line 111, in enhanced_training
    next_state, reward = env.step_enhanced(action, state)
  File "C:\Users\Artur\Desktop\RL_framework\help_functions\environment_machines.py", line 152, in step_enhanced
    self.calculate_total_node_THD_func_real_data_with_topo_enhanced()                                                # THD_plant calculation with considering grid topo
  File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\dispatcher.py", line 404, in _compile_for_args
    error_rewrite(e, 'unsupported_error')
  File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\dispatcher.py", line 344, in error_rewrite
    reraise(type(e), e, None)
  File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\utils.py", line 80, in reraise
    raise value.with_traceback(tb)
numba.core.errors.UnsupportedError: Failed in object mode pipeline (step: analyzing bytecode)
Use of unsupported opcode (FORMAT_VALUE) found

File "help_functions\environment_machines.py", line 572:
    def calculate_total_node_THD_func_real_data_with_topo_enhanced(self):
        <source elided>

                    print(f'{5}')
                    ^


Process finished with exit code 1

When I use normal strings -> print(5) it works but not with formatted strings. Is there a way to use string formatting?

Thank you in advance.

1 Answers

It seems that f-strings don't work but formatted strings do:

print('{val}'.format(val))

Related