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

@ Aaron Hi Aaron,

Is it possible to execute EnergyPlus from library "from pyenergyplus.api import EnergyPlusAPI" without using API?

Also, the difference between using python plugin and python API lies in the method of executing the RunTime function. For plugin, I have to create an instance of callback class directly inside an IDF file where as using API I do not need to do that. Is that correct? Is there any significant difference between plugin and API?

Thanks.

prp92's avatar prp92  ( 2022-02-04 10:47:05 -0500 )edit

No, you need to import the EnergyPlusAPI in order to use it in your Python plugin scripts.

No, the RunTime API class allows you to access a running EnergyPlus simulation to see results and more changes to the model (use actuators, e.g.). API documentation.

Aaron Boranian's avatar Aaron Boranian  ( 2022-02-04 12:33:37 -0500 )edit

I would recommend looking at an example Python plugin project on Github. This was posted by @mechyai, and you can search for "pyenergyplus.api" or whatever code portion you're interested in. You can read more about this Github project in another Unmet Hours post.

Aaron Boranian's avatar Aaron Boranian  ( 2022-02-04 12:35:40 -0500 )edit

Library mode uses the EnergyPlus API to register callback functions and start an EnergyPlus run from a function call (call script first to run IDF). This is also known as using EnergyPlus as a library.

Plugin mode requires starting the IDF run first so that external scripts can be called to access results or override model settings, the same way EMS objects would.

Aaron Boranian's avatar Aaron Boranian  ( 2022-02-04 12:39:53 -0500 )edit

@Aaron Boranian my EmsPy repo is in active development and sufficiently working, but I have 5-month backlog of commits because of a bug when I try to push to GitHub. I will need to figure out how to get this fixed so others can make use of this.

Update: For now, I would direct people to my new repo that is up to date https://github.com/mechyai/RL-EmsPy. I am currently running PyTorch RL agents with this RL API. This will likely become the new home for EmsPy

mechyai's avatar mechyai  ( 2022-02-04 12:44:30 -0500 )edit

@mechyai I had an issue with pushing to GitHub a while back when they changed the login credentials to no longer use Username and Password. I think I got it to work by using the personal token as the password.

rraustad's avatar rraustad  ( 2022-02-04 13:12:06 -0500 )edit

@rraustad, thanks for the tip! I will look into this change

mechyai's avatar mechyai  ( 2022-02-06 21:21:05 -0500 )edit

Your Answer

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

Add Answer

Training Workshops

Careers

Question Tools

1 follower

Stats

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

Seen: 1,457 times

Last updated: Feb 03 '22