NameError: name 'self' is not defined

The engine.
Post Reply
bahman
Posts: 21
Joined: Sun Mar 13, 2022 12:24 pm

NameError: name 'self' is not defined

Post by bahman »

HI;

Code: Select all

import pysmile
import pysmile_license

net = pysmile.Network()

e = self.create_cpt_node(net,

    "Economy", "State of the economy",

    ["Up","Flat","Down"], 

    160, 40)

s = self.create_cpt_node(net,

    "Success", "Success of the venture",

    ["Success","Failure"], 

    60, 40)

f = self.create_cpt_node(net,

    "Forecast", "Expert forecast",

    ["Good","Moderate","Poor"], 

    110, 140)

def create_cpt_node(self, net, id, name, outcomes, x_pos, y_pos):

    handle = net.add_node(pysmile.NodeType.CPT, id)

    net.set_node_name(handle, name)

    net.set_node_position(handle, x_pos, y_pos, 85, 55)

    initial_outcome_count = net.get_outcome_count(handle)

    for i in range(0, initial_outcome_count):

        net.set_outcome_id(handle, i, outcomes[i])

    for i in range(initial_outcome_count, len(outcomes)):

        net.add_outcome(handle, outcomes[i])

    return handle

    net.add_arc(e, s)

net.add_arc(s, f)

net.add_arc("Economy", "Forecast")

successDef = [

    0.3, # P(Success=S|Economy=U)

    0.7, # P(Success=F|Economy=U)

    0.2, # P(Success=S|Economy=F)

    0.8, # P(Success=F|Economy=F)

    0.1, # P(Success=S|Economy=D)

    0.9  # P(Success=F|Economy=D)

]

net.set_node_definition(s, successDef)

print("Posteriors with no evidence set:")

net.update_beliefs()

self.print_all_posteriors(net)

i want to run this code but i get this error why ?

Code: Select all

      4 net = pysmile.Network()
      5 
----> 6 e = self.create_cpt_node(net,
      7 
      8     "Economy", "State of the economy",

NameError: name 'self' is not defined
is "self" have defenition like net ?

thank you
piotr [BayesFusion]
Site Admin
Posts: 60
Joined: Mon Nov 06, 2017 6:41 pm

Re: NameError: name 'self' is not defined

Post by piotr [BayesFusion] »

You are trying to run modified Tutorial1 code - in that code self refers to object of Tutorial1 class. Try to remove all self calls from your code and move create_cpt_node fuction definition before calling it.

You can learn more about self from here: https://www.w3schools.com/python/gloss_python_self.asp
bahman
Posts: 21
Joined: Sun Mar 13, 2022 12:24 pm

Re: NameError: name 'self' is not defined

Post by bahman »

HI piotr

whould you check this ? is it true ? i still have error

Code: Select all



def create_cpt_node(self, net, id, name, outcomes, x_pos, y_pos):

    handle = net.add_node(pysmile.NodeType.CPT, id)

    net.set_node_name(handle, name)

    net.set_node_position(handle)

    initial_outcome_count = net.get_outcome_count(handle)

    for i in range(0, initial_outcome_count):

        net.set_outcome_id(handle, i, outcomes[i])

    for i in range(initial_outcome_count, len(outcomes)):

        net.add_outcome(handle, outcomes[i])

    return handle


net = pysmile.Network()

e = create_cpt_node(net,

    "Economy", "State of the economy",

    ["Up","Flat","Down"], 

    160, 40)

s = create_cpt_node(net,

    "Success", "Success of the venture",

    ["Success","Failure"], 

    60, 40)

f = create_cpt_node(net,

    "Forecast", "Expert forecast",

    ["Good","Moderate","Poor"], 

    110, 140)



net.add_arc(e, s)

net.add_arc(s, f)

net.add_arc("Economy", "Forecast")

successDef = [

    0.3, # P(Success=S|Economy=U)

    0.7, # P(Success=F|Economy=U)

    0.2, # P(Success=S|Economy=F)

    0.8, # P(Success=F|Economy=F)

    0.1, # P(Success=S|Economy=D)

    0.9  # P(Success=F|Economy=D)

]

net.set_node_definition(s, successDef)

print("Posteriors with no evidence set:")

net.update_beliefs()

print_all_posteriors(net)

Code: Select all

    31     ["Up","Flat","Down"],
     32 
---> 33     160, 40)
     34 
     35 s = create_cpt_node(net,

TypeError: create_cpt_node() missing 1 required positional argument: 'y_pos'

thank you
piotr [BayesFusion]
Site Admin
Posts: 60
Joined: Mon Nov 06, 2017 6:41 pm

Re: NameError: name 'self' is not defined

Post by piotr [BayesFusion] »

Remove self from create_cpt_node arguments.

Code: Select all

...

def create_cpt_node(net, id, name, outcomes, x_pos, y_pos):

...
Also - I can't see print_all_posteriors function declaration. You have to copy that function from our tutorial.
Post Reply