Question-and-Answer Resource for the Building Energy Modeling Community
Get started with the Help page
Ask Your Question
1

Error: 'float' object has no attribute 'apply'

asked 2022-02-02 12:14:25 -0500

prp92's avatar

updated 2022-02-08 09:14:12 -0500

Hi...

I am trying to use ANN model to predict the window opening and closing. For this I have used sensor data to get indoor and outdoor temperature. I have also trained and imported by ANN model using pickle library. The ANN model is named as 'ANNclassify'. The value from the ANNclassify will be given to actuator, which will change my schedule.

For some reason, the code is giving me the following error.

Traceback (most recent call last):
  File "<ipython-input-73-8928f248afed>", line 23, in my_function
AttributeError: 'float' object has no attribute 'apply'
Exception ignored on calling ctypes callback function: <function my_function at 0x000001DC00155D30>

My code is as follows:

api = EnergyPlusAPI()

# Loading the ANN model using pickle function. pick

with open('ANNClassify.pickle', 'rb') as f:
    ANNclassify = pickle.load(f)        

def my_function(state):
    if api.exchange.api_data_fully_ready(state): #Necessary condition
        if api.exchange.warmup_flag(state) == 0:
            To_handle = api.exchange.get_variable_handle(state, "Site Outdoor Air Drybulb Temperature", "Environment")
            Ti_handle = api.exchange.get_variable_handle(state, "Zone Mean Air Temperature", "SecondFloor:Unit1XBR")
            # "Zone Thermostat Cooling Setpoint Temperature", zone_name)

            To = api.exchange.get_variable_value(state, To_handle)
            Ti = api.exchange.get_variable_value(state, Ti_handle)

            To = To.apply(stats.zscore)
            Ti = Ti.apply(stats.zscore)

            var_list = [Ti, To]
            var_list = torch.FloatTensor(var_list)
            var_list = var_list.reshape(1,2)

            Modified_Sch = ANNclassify(var_list)

            Modified_Sch_1 = Modified_Sch.item()

            if Modified_Sch_1 <= 0:       
                Modified_Sch_1 = 0       
            else:              
                Modified_Sch_1 = 1

            sch_handle = self.api.exchange.get_actuator_handle(state,
                                                           "SCHEDULE:COMPACT",
                                                           "SCHEDULE VALUE",
                                                           "LIV1XWIN_SCH")

            self.api.exchange.set_actuator_value(state, sch_handle, Modified_Sch_1)

state = api.state_manager.new_state() #Necessary condition
api.state_manager.reset_state(state)
api.runtime.callback_begin_zone_timestep_after_init_heat_balance(state, my_function)

api.runtime.run_energyplus(state,
    [
        '-d', '/Users/.../Desktop/ReOrg/EPlus_Files',
        '-w', '/Users/.../Desktop/ReOrg/EPlus_Files/Weather_File_SYR.epw',
        'C:/Users/.../Desktop/ReOrg/EPlus_Files/Heating_design_new.idf'
    ]
)

Is there any way I can solve this issue? Thanks..

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-02-03 10:01:20 -0500

It seems that your lines of code trying to reset To and Ti variables with .apply(stats.zscore) is the issue.

        To = To.apply(stats.zscore)
        Ti = Ti.apply(stats.zscore)

In the lines just above this, you've initialized these variables and set them to the temperature sensor values -- which are floats (numbers). From the error message, you can't use the .apply() method to floats.

Has this To.apply(stats.zscore) worked in other projects? From the Python statistics library documentation, it seems like the correct code would be stats.zscore(To) instead.

edit flag offensive delete link more

Comments

@ Aaron thanks Aaron. your solution worked. But I got into another problem. The last line of the code : api.exchange.set_actuator_value(state, sch_handle, Modified_Sch_1)

gives me following error.

* Severe * Data Exchange API: index error in setActuatorValue; received handle: -1 * ~~~ * The setActuatorValue function will return to allow the plugin to finish, then EnergyPlus will abort

Do you have any suggestions for this problem?

thank you so much for your time.

prp92's avatar prp92  ( 2022-02-03 11:12:33 -0500 )edit

The sch_handle variable used in the set_actuator_value method is -1, causing the severe error. This variable is set a few lines above with the .get_actuator_handle method, which returns -1 if the object can't be found or one of the parameters (state, component_type, control_type, actuator_key) are set incorrectly. Have you initialized state = api.state_manager.new_state() before this? Otherwise "LIV1XWIN_SCH" may not be the correct schedule name.

Aaron Boranian's avatar Aaron Boranian  ( 2022-02-03 14:00:05 -0500 )edit

@ Aaron Hi Aaron,

If I am using python EMS, then I don't have to define EnergyManagementSystem:Actuator in the IDF file manually. Is that correct?

Also, can we define Window opening and closing using the Component Type - Zone Ventilation Wind And Stack Open Area and the Control Type - Opening Area Fraction Schedule Name?

I tried to use these but it is still throwing -1 (severe error).

Thank you so much.

prp92's avatar prp92  ( 2022-02-04 06:25:34 -0500 )edit

Correct, the only EMS object you need in the IDF is the Output:EnergyManagementSystem object, along with the PythonPlugin:Instance object to use Python plugin features. The EDD output file generated by the Output EMS object can list all possible actuators for your IDF if you set the first input field to "Verbose". You may need to temporarily remove or comment out the PythonPlugin:Instance object to avoid your code issue and let EnergyPlus write the EDD file.

Aaron Boranian's avatar Aaron Boranian  ( 2022-02-04 09:15:08 -0500 )edit

I would recommend reviewing an example IDF and Python script using the Python plugin that comes with EnergyPlus. You can find these in the ExampleFiles folder where EnergyPlus is installed, and the files using Python plugin being with PythonPlugin... in their file name (both .idf and .py files).

Aaron Boranian's avatar Aaron Boranian  ( 2022-02-04 09:16:26 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Careers

Question Tools

1 follower

Stats

Asked: 2022-02-02 12:14:25 -0500

Seen: 1,439 times

Last updated: Feb 03 '22