def setpot(pot, evvariables, evidstates): #FIXME: data format needed to be unified vars = pot.variables #vars = np.array(pot.variables) # convert to ndarray format #evariables = np.array(evvariables) # convert to ndarray format #evidstates = np.array(evidstates) # convert to ndarray format #print "variables:", vars table = pot.table nstates = pot.card #print "number of states:", nstates #print "vars:", vars #print "evvariables:", evvariables intersection, iv, iev = intersect(vars, evvariables) #iv = np.array(iv) #iev = np.array(iev) #print "intersection:", intersection #print "iv:", iv #print "iev:", iev #print "iv type:", type(iv) #print "number of intersection:", intersection.size if intersection.size == 0: newpot = copy.copy(pot) else: newvar = setminus(vars, intersection) dummy, idx = ismember(newvar, vars) newns = nstates[idx] newpot = potential() newpot.variables = newvar newpot.card = newns newpot.table = np.zeros(newns) #print "idx:", idx #print "iv:", iv for i in range(np.prod(newns)): newassign = IndexToAssignment(i, newns) oldassign = np.zeros(nstates.size, 'int8') oldassign[idx] = newassign oldassign[iv] = evidstates #print "newpot.table.shape:", newpot.table.shape #print "newassign:", newassign #print "newassign type:", type(newassign) newpot.table[tuple(newassign)] = pot.table[tuple(oldassign)] return newpot
def condpot(pot, varargin): #FIXME: only 1 varargin supported , use *arg in further development newpot = potential() y = [] x = varargin #print "pot.variables:", pot.variables #print "pot.table: \n", pot.table #print "x:", x #print "pot.variables:", pot.variables # convert variable to idx (not consistent in Python other than MATLAB) intersection, ix, ipot = intersect(x, pot.variables) #print "intersection=", intersection newpot.variables = intersection #print "pot.variables:", pot.variables FULL_axis = np.arange(pot.variables.size) #print pot.variables #print intersection axis_intersection = ipot #.index(intersection) other_axis = setminus(FULL_axis, axis_intersection) #print "axis_intersection=", axis_intersection #print "other_axis=", other_axis #print "Full_axis:", FULL_axis #print "other_axis:", other_axis newpot.table = np.apply_over_axes(np.sum, pot.table, other_axis) #print "newpot.variables:", newpot.variables #print "newpot.table: \n", newpot.table SUM = potential() SUM.variables = np.array([]) SUM.table = np.sum(newpot.table) #print "newpot.table:", newpot.table #print "SUM.table:", SUM.table newpot = newpot / SUM return newpot
def condpot(pot,varargin): #FIXME: only 1 varargin supported , use *arg in further development newpot = potential() y = [] x = varargin #print "pot.variables:", pot.variables #print "pot.table: \n", pot.table #print "x:", x #print "pot.variables:", pot.variables # convert variable to idx (not consistent in Python other than MATLAB) intersection, ix, ipot = intersect(x, pot.variables) #print "intersection=", intersection newpot.variables = intersection #print "pot.variables:", pot.variables FULL_axis = np.arange(pot.variables.size) #print pot.variables #print intersection axis_intersection = ipot #.index(intersection) other_axis = setminus(FULL_axis,axis_intersection) #print "axis_intersection=", axis_intersection #print "other_axis=", other_axis #print "Full_axis:", FULL_axis #print "other_axis:", other_axis newpot.table = np.apply_over_axes(np.sum, pot.table, other_axis) #print "newpot.variables:", newpot.variables #print "newpot.table: \n", newpot.table SUM = potential() SUM.variables = np.array([]) SUM.table = np.sum(newpot.table) #print "newpot.table:", newpot.table #print "SUM.table:", SUM.table newpot = newpot/SUM return newpot