Error when calling Chapel from Python using PyChapel - chapel

I am trying to get Chapel to return an integer to Python. I'd like to call it with python call.py.
call.py
import os
from pych.extern import Chapel
currentloc = os.path.dirname(os.path.realpath(__file__))
#Chapel(sfile=os.path.join(currentloc + '/response.chpl'))
def flip_bit(v=int):
return int
if __name__=="__main__":
u = 71
w = flip_bit(u)
print(w)
And response.chpl
export
proc flip_bit(v: int) :int {
v = -v;
return v;
}
This returns the error
/tmp/temp-7afvL9.chpl:2: In function 'flip_bit':
/tmp/temp-7afvL9.chpl:3: error: illegal lvalue in assignment
g++: error: /tmp/tmpvmKeSi.a: No such file or directory
Traceback (most recent call last):
File "call.py", line 15, in <module>
w = flip_bit(u)
File "/home/buddha314/.virtualenvs/pychapel/local/lib/python2.7/site-packages/pych/extern.py", line 212, in wrapped_f
raise MaterializationError(self)
pych.exceptions.MaterializationError: Failed materializing ({'anames': ['v'],
'atypes': [<type 'int'>],
'bfile': None,
'dec_fp': '/home/buddha314/pychapel/tmp/response.chpl',
'dec_hs': '7ecfac2d168f3423f7104eeb38057ac3',
'dec_ts': 1502208246,
'doc': None,
'efunc': None,
'ename': 'flip_bit',
'lib': 'sfile-chapel-7ecfac2d168f3423f7104eeb38057ac3-1502208246.so',
'module_dirs': [],
'pfunc': <function flip_bit at 0x7fa4d72bd938>,
'pname': 'flip_bit',
'rtype': <type 'int'>,
'sfile': '/home/buddha314/pychapel/tmp/response.chpl',
'slang': 'chapel',
'source': None}).
UPDATE
Based on Lydia's response, I did
export
proc flip_bit(v: int) :int {
var w: int;
w = -v;
return w;
}
And that worked! WOO-HOOO!!!!
UPDATE 2
Based on Brad's comments, this also works
export
proc flip_bit(in v: int) :int {
return -v;
}
Perhaps he can add a comment on benefits of each approach.

It looks like your issue is that you're trying to modify the argument before returning it. Chapel's default argument intent for integers is const in ( see the Chapel spec http://chapel.cray.com/docs/latest/language/spec.html, section 13.5 ), which means it can't be modified within the body of the function and is a copy of the value passed to it. If you store the result in a local variable and return that instead, that should solve your compilation failure and give you the behavior you desire.

Related

Dependent component can't be instantiated in AbstractModel with Pyomo >= 5.7.3

The following code does work on Pyomo 5.7.0, but does not work on Pyomo 5.7.3 and above anymore. The Error is "ValueError: Error retrieving immutable Param value (battery.n_time_steps)" (full traceback at the end) when trying to build the time_steps set.
import pyomo.environ as pyo
def test_param_pyomo5_7():
def create_battery():
block = pyo.Block()
block.n_time_steps = pyo.Param(within=pyo.NonNegativeIntegers, doc='Number of time steps')
block.time_steps = pyo.RangeSet(1, block.n_time_steps, doc='Time steps')
return block
def create_model() -> pyo.AbstractModel:
model = pyo.AbstractModel()
model.battery = create_battery()
return model
data = {
None: {
'battery': {
'n_time_steps': {None: 24},
},
}
}
model = create_model()
instance = model.create_instance(data=data)
The full traceback is:
..\..\opt\Miniconda3\envs\tesca_optimizer\lib\site-packages\pyomo\core\base\PyomoModel.py:697: in create_instance
instance.load( data,
..\..\opt\Miniconda3\envs\tesca_optimizer\lib\site-packages\pyomo\core\base\PyomoModel.py:734: in load
self._load_model_data(dp,
..\..\opt\Miniconda3\envs\tesca_optimizer\lib\site-packages\pyomo\core\base\PyomoModel.py:787: in _load_model_data
self._initialize_component(modeldata, namespaces, component_name, profile_memory)
..\..\opt\Miniconda3\envs\tesca_optimizer\lib\site-packages\pyomo\core\base\PyomoModel.py:825: in _initialize_component
declaration.construct(data)
..\..\opt\Miniconda3\envs\tesca_optimizer\lib\site-packages\pyomo\core\base\block.py:2207: in construct
obj.construct(data.get(name, None))
..\..\opt\Miniconda3\envs\tesca_optimizer\lib\site-packages\pyomo\core\base\disable_methods.py:116: in construct
return base.construct(self, data)
..\..\opt\Miniconda3\envs\tesca_optimizer\lib\site-packages\pyomo\core\base\set.py:2792: in construct
args = tuple(value(arg) for arg in args)
..\..\opt\Miniconda3\envs\tesca_optimizer\lib\site-packages\pyomo\core\base\set.py:2792: in <genexpr>
args = tuple(value(arg) for arg in args)
pyomo\core\expr\numvalue.pyx:153: in pyomo.core.expr.numvalue.value
???
pyomo\core\expr\numvalue.pyx:138: in pyomo.core.expr.numvalue.value
???
..\..\opt\Miniconda3\envs\tesca_optimizer\lib\site-packages\pyomo\core\base\param.py:853: in __call__
return self[None]
..\..\opt\Miniconda3\envs\tesca_optimizer\lib\site-packages\pyomo\core\base\indexed_component.py:577: in __getitem__
return self._getitem_when_not_present(index)
There was an inconsistency in how blocks were initialized from dictionary data in Pyomo<5.7.2, which was fixed in 5.7.2 (PR #1703). The correct data dictionary requires "indices" for all components (even scalar components). Your data dictionary is missing the None key (implicit index) for the data associated with the scalar Block battery. The following data dictionary will work in recent versions of Pyomo:
data = {
None: {
'battery': {
None: {
'n_time_steps': {None: 24},
},
},
}
}

python when there is no input for the subscriber it starts publishing error?/! how to solve it?

I have provided the following python code,but the problem is that when it doesnot receive any input, it start showing error. how can i modify the code in a way that this error dosnot appear:
#!/usr/bin/env python from roslib import message import rospy import sensor_msgs.point_cloud2 as pc2 from sensor_msgs.msg import PointCloud2, PointField import numpy as np import ros_numpy from geometry_msgs.msg import Pose
#listener def listen():
rospy.init_node('listen', anonymous=True)
rospy.Subscriber("/Filtered_points_x", PointCloud2, callback_kinect)
def callback_kinect(data):
pub = rospy.Publisher('lidar_distance',Pose, queue_size=10)
data_lidar = Pose()
xyz_array = ros_numpy.point_cloud2.pointcloud2_to_xyz_array(data)
print(xyz_array)
mini_data = min(xyz_array[:,0])
print("mini_data", mini_data)
data_lidar.position.x = mini_data
pub.publish(data_lidar)
print("data_points", data_lidar.position.x)
height = int (data.height / 2)
middle_x = int (data.width / 2)
middle = read_depth (middle_x, height, data) # do stuff with middle
def read_depth(width, height, data) :
if (height >= data.height) or (width >= data.width) :
return -1
data_out = pc2.read_points(data, field_names= ('x','y','z'), skip_nans=True, uvs=[[width, height]])
int_data = next(data_out)
rospy.loginfo("int_data " + str(int_data))
return int_data
if __name__ == '__main__':
try:
listen()
rospy.spin()
except rospy.ROSInterruptException:
pass
the following is the error that i mentioned:
[[ 7.99410915 1.36072445 -0.99567264]]
('mini_data', 7.994109153747559)
('data_points', 7.994109153747559)
[INFO] [1662109961.035894]: int_data (7.994109153747559, 1.3607244491577148, -0.9956726431846619)
[]
[ERROR] [1662109961.135572]: bad callback: <function callback_kinect at 0x7f9346d44230>
Traceback (most recent call last):
File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
cb(msg)
File "/home/masoumeh/catkin_ws/src/yocs_velocity_smoother/test4/distance_from_pointcloud.py", line 27, in callback_kinect
mini_data = min(xyz_array[:,0])
ValueError: min() arg is an empty sequence
The code is still receiving input, but specifically it’s receiving an empty array. You’re then trying to splice the empty array, causing the error. Instead you should check that the array has elements before the line min(xyz_array[:,0]). It can be as simple as:
if xyz_array == []:
return
As another note, you’re creating a publisher in the callback. You shouldn’t do this as a new publisher will be created every time it gets called. Instead create it as a global variable.

Python global variables not available in function

I am using the following code to read sensor data, process it and publish some feedback to a different topic, /scans_freespace.
#!/usr/bin/env python
import rospy
from rospy.core import is_shutdown
from sensor_msgs.msg import LaserScan
from sweep_bot.msg import Space
from sweep_bot.msg import SpaceArray
class FreeSpace :
def __init__(self) :
rospy.loginfo('starting')
self.scans = ()
self.regions = {}
self.publisher = self.subscriber = ''
def callbackScans(self, data) :
self.scans = data.ranges
self.regions = {
'starboard_aft' : self.scans[0:135],
'starboard_abeam_aft' : self.scans[136:271],
'starboard_abeam_bow' : self.scans[272:407],
'starboard_bow' : self.scans[408:543],
'port_aft' : self.scans[949:1084],
'port_abeam_aft' : self.scans[814:949],
'port_abeam_bow' : self.scans[679:814],
'port_bow' : self.scans[544:679],
}
def publish(self) :
self.getSomeData('port_bow')
def getSomeData(self, region) :
rospy.loginfo("Checking region: %s", region)
rospy.loginfo("Checking scans: %s", self.scans)
return self.scans[region]
if __name__ == '__main__' :
rospy.init_node('space_identifier')
rate = rospy.Rate(10)
spacer = FreeSpace()
subscriber = rospy.Subscriber("scans", LaserScan, spacer.callbackScans)
publisher = rospy.Publisher("scans_freespace", SpaceArray, queue_size=10)
while not rospy.is_shutdown() :
spacer.publish()
rate.sleep()
As the code shows, I am calling the getSomeData() function from my publish() function passing a single string argument. getSomeData() receives the argument as expected but the global variables which are populated from the callbackScan() function are empty.
The output from the script is as follow:
[INFO] [1629830370.246796, 0.000000]: starting
[INFO] [1629830370.253719, 0.000000]: Checking region: port_bow
[INFO] [1629830370.256151, 0.000000]: Checking scans: ()
Traceback (most recent call last):
File "/home/sisko/catkin_ws/src/sweepbot/Sweeper/sweep_bot/src/freespace.py", line 49, in <module>
spacer.publish()
File "/home/sisko/catkin_ws/src/sweepbot/Sweeper/sweep_bot/src/freespace.py", line 31, in publish
self.getSomeData('port_bow')
File "/home/sisko/catkin_ws/src/sweepbot/Sweeper/sweep_bot/src/freespace.py", line 37, in getSomeData
return self.scans[region]
TypeError: tuple indices must be integers, not str
Ironically, the code did work until I attempted to update my output.
What am I missing?
There are a few things wrong here. However, your error is because you do not properly initialize your message fields. This in turn causes issue when publish() is called before a LaserScan message is received. As well, ranges is a std_msg/Float32[] so even if it was assigned correctly in the callback you'd have another error when trying to index a float list with a string. It is a little unclear as to what you want to do, but looking at the key names getSomeData() should look something more like this:
def getSomeData(self, region) :
rospy.loginfo("Checking region: %s", region)
rospy.loginfo("Checking scans: %s", self.scans)
return self.regions[region] if region in self.regions else None
Another point to make is if you're hoping to publish the results of this function out via the publish() function, you need to actually define a publisher inside your class with the correct type.

type matching error in theano [ Cannot convert Type Generic (of Variable <Generic>) into Type TensorType]

thisfile.py
import cPickle
import gzip
import os
import numpy
import theano
import theano.tensor as T
def load_data(dataset):
f = gzip.open(dataset, 'rb')
train_set, valid_set, test_set = cPickle.load(f)
f.close()
def shared_dataset(data_xy, borrow=True):
data_x, data_y = data_xy
shared_x = theano.shared(numpy.asarray(data_x,
dtype=theano.config.floatX),
borrow=borrow)
shared_y = theano.shared(numpy.asarray(data_y,
dtype=theano.config.floatX),
borrow=borrow)
return shared_x, T.cast(shared_y, 'int32')
test_set_x, test_set_y = shared_dataset(test_set)
valid_set_x, valid_set_y = shared_dataset(valid_set)
train_set_x, train_set_y = shared_dataset(train_set)
rval = [(train_set_x, train_set_y), (valid_set_x, valid_set_y),
(test_set_x, test_set_y)]
return rval
class PCA(object):
def __init__(self):
self.param = 0
def dimemsion_transform(self, X):
m_mean = T.mean(X, axis=0)
X = X - m_mean ##################### this line makes error
return X
if __name__ == '__main__':
dataset = 'mnist.pkl.gz'
# load the MNIST data
data = load_data(dataset)
X = T.matrix('X')
m_pca = PCA()
transform = theano.function(
inputs=[],
outputs=m_pca.dimemsion_transform(X),
givens={
X: data
}
)
error showing like below
Traceback (most recent call last):
File ".../thisfile.py", line 101, in <module>
X: data
File ".../Theano/theano/compile/function.py", line 322, in function
output_keys=output_keys)
File ".../Theano/theano/compile/pfunc.py", line 443, in pfunc
no_default_updates=no_default_updates)
File ".../Theano/theano/compile/pfunc.py", line 219, in rebuild_collect_shared
cloned_v = clone_v_get_shared_updates(v, copy_inputs_over)
File ".../Theano/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
clone_v_get_shared_updates(i, copy_inputs_over)
File ".../Theano/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
clone_v_get_shared_updates(i, copy_inputs_over)
File ".../Theano/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
clone_v_get_shared_updates(i, copy_inputs_over)
File ".../Theano/theano/compile/pfunc.py", line 96, in clone_v_get_shared_updates
[clone_d[i] for i in owner.inputs], strict=rebuild_strict)
File ".../Theano/theano/gof/graph.py", line 242, in clone_with_new_inputs
new_inputs[i] = curr.type.filter_variable(new)
File ".../Theano/theano/tensor/type.py", line 234, in filter_variable
self=self))
TypeError: Cannot convert Type Generic (of Variable <Generic>) into Type TensorType(float64, matrix). You can try to manually convert <Generic> into a TensorType(float64, matrix).
I am making PCA function with theano but have a problem.
mean value is subtracted from MNIST data in dimension_transform in PCA class
I do not get why it gives type matching error and how do I fix it
Your problem comes from these lines:
data = load_data(dataset)
Here data is a list (as this is what load_data() returns).
transform = theano.function(
inputs=[],
outputs=m_pca.dimemsion_transform(X),
givens={
X: data
}
)
And here you pass it as a value. You have to extract the item you want from the return value of load_data() like so:
[(train_set_x, train_set_y), (valid_set_x, valid_set_y),
(test_set_x, test_set_y)] = load_data(dataset)
and then use
givens={
X: train_set_x
}
or one of the other values.

Error 'bool' object has no attribute 'any'

I wrote a script to do interpolation
import scipy.interpolate
import csv
inputfile1 = 'test.csv'
outputfile = 'Day1_out.csv'
distance_list = []
EC_list = []
new_dist_list=[]
outfile = open(outputfile,'w')
outfile.write('Distance,EC\n')
with open (inputfile1,'rb') as csvfile:
f1 = csv.reader(csvfile,delimiter=',')
next(f1) #skip header line
for row in f1:
dist = row[12]
EC=row[13]
distance_list.append(dist)
EC_list.append(EC)
y_interp = scipy.interpolate.interp1d(distance_list,EC_list)
new_dist = 561.7
end = 560.2
while new_dist>end:
new_dist_list.append(dist)
new_dist=new_dist-0.2
for distance in new_dist_list:
EC=y_interp(distance)
outfile.write(str(distance)+','+str(EC)+'\n')
outfile.close()
When I ran the script it gave me the error message
Traceback (most recent call last):
File "D:\14046\Scripts\interpolation_RoR.py", line 41, in <module>
EC=y_interp(distance)
File "C:\Python27\lib\site-packages\scipy\interpolate\polyint.py", line 54, in __call__
y = self._evaluate(x)
File "C:\Python27\lib\site-packages\scipy\interpolate\interpolate.py", line 448, in _evaluate
out_of_bounds = self._check_bounds(x_new)
File "C:\Python27\lib\site-packages\scipy\interpolate\interpolate.py", line 474, in _check_bounds
if self.bounds_error and below_bounds.any():
AttributeError: 'bool' object has no attribute 'any'
Anyone has any idea where I have errors?
BTW, the input file have these values for distance and EC
Distance,EC
561.8,450
561.78,446
561.7,444
561.2,440
561.02,438
560.5,437
560.1,435
Thanks,
We are getting the same error message here. I think this does not necessarily need to be a problem with your code.
In our case switching to SciPy version 0.15.0 instead of 0.13.x solves the problem.
So it looks like the current version of SciPy accepts a wider range of input values.