I am trying to create a tree with branches in root through python. I have a .root file and I am trying to create branches that are the variables (or data pts) of my .root file. Here is my attempt:
f = ROOT.TFile('event.root', 'read') #opening the file and creating a file object f
T = ROOT.TTree("T", "simple tree")
#ntuple = ROOT.TNtuple("ntuple","Demo ntuple","px:py:pz:m")
T.Scan("px:py:pz:m")
This just gives me:
Error in TTreeFormula::Compile: Bad numerical expression : “px”
Error in TTreeFormula::Compile: Bad numerical expression : “py”
Error in TTreeFormula::Compile: Bad numerical expression : “pz”
Error in TTreeFormula::Compile: Bad numerical expression : “m”
Row * px * py * pz * m *
which I understand why since I did not define my variables. So I am looking through an example, https://www.niser.ac.in/sercehep2017/notes/RootTutorial_TTree.pdf, (slide 3) and I attempt to define my variable that should be contained in my .root file as:
f = ROOT.TFile('event.root', 'read') #opening the file and creating a file object f
T = ROOT.TTree("T", "simple tree")
px_as_floats = float(px)
py_as_float = float(py)
pz_as_float = float(pz)
m_as_float = float(m)
T.Branch("px",&px,"px/F")
T.Branch("py",&py,"py/F")
T.Branch("pz,&pz,"pz/F")
T.Branch("m",&m,"m/F")
But, I end up with this error:
Traceback (most recent call last):
File “”, line 1, in
File “/mnt/c/1/writeroot.py”, line 17
T.Branch(“px”,&px,“px/F”)
^
SyntaxError: invalid syntax
Is there a way to write this in python? Writing:
T.ROOT.Branch(“px”,&px,“px/F”)
did not work either.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/mnt/c/1/writeroot.py", line 17
T.ROOT.Branch("pt1",&pt1,"pt/F")
^
SyntaxError: invalid syntax
How can I fix the syntax.
Ultimately I am trying to load the dictionary used from the .root file to my tree and then do some calculations on the items in the dictionary.
In other words, how do you extract the dictionary from the .root file?
When I type:
When I type gFile->ls(), I get
TFile** rdata.root
TFile* rdata.root
KEY: TH1F mass;1 masses
KEY: TNtuple tnt;1 tnt
Unless you are trying to make bitwise AND operation, the symobl & is not valid. I assume that you want to send pointer to original variable.
We don't do this in python. If that's the case look up on google for local and global variable.
Tl;dr in python all mutable types are passed by reference
Personally I would have wrote this like:
T.Branch("px",px,"px/F")
T.Branch("py",py,"py/F")
T.Branch("pz", pz,"pz/F")
T.Branch("m",m,"m/F")
Related
I have a following problem. I am following this example about spatial regression in Python:
import numpy
import libpysal
import spreg
import pickle
# Read spatial data
ww = libpysal.io.open(libpysal.examples.get_path("baltim_q.gal"))
w = ww.read()
ww.close()
w_name = "baltim_q.gal"
w.transform = "r"
Example above works. But I would like to read my own spatial matrix which I have now as a list of lists. See my approach:
ww = libpysal.io.open(matrix)
But I got this error message:
Traceback (most recent call last):
File "/usr/lib/python3.8/code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "/home/vojta/Desktop/INTERNET_HANDEL/ZASILKOVNA/optimal-delivery-branches/venv/lib/python3.8/site-packages/libpysal/io/fileio.py", line 90, in __new__
cls.__registry[cls.getType(dataPath, mode, dataFormat)][mode][0]
File "/home/vojta/Desktop/INTERNET_HANDEL/ZASILKOVNA/optimal-delivery-branches/venv/lib/python3.8/site-packages/libpysal/io/fileio.py", line 105, in getType
ext = os.path.splitext(dataPath)[1]
File "/usr/lib/python3.8/posixpath.py", line 118, in splitext
p = os.fspath(p)
TypeError: expected str, bytes or os.PathLike object, not list
this is how matrix looks like:
[[0, 2, 1], [2, 0, 4], [1, 4, 0]]
EDIT:
If I try to insert my matrix into the GM_Lag like this:
model = spreg.GM_Lag(
y,
X,
w=matrix,
)
I got following error:
warn("w must be API-compatible pysal weights object")
Traceback (most recent call last):
File "/usr/lib/python3.8/code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 2, in <module>
File "/home/vojta/Desktop/INTERNET_HANDEL/ZASILKOVNA/optimal-delivery-branches/venv/lib/python3.8/site-packages/spreg/twosls_sp.py", line 469, in __init__
USER.check_weights(w, y, w_required=True)
File "/home/vojta/Desktop/INTERNET_HANDEL/ZASILKOVNA/optimal-delivery-branches/venv/lib/python3.8/site-packages/spreg/user_output.py", line 444, in check_weights
if w.n != y.shape[0] and time == False:
AttributeError: 'list' object has no attribute 'n'
EDIT 2:
This is how I read the list of lists:
import pickle
with open("weighted_matrix.pkl", "rb") as f:
matrix = pickle.load(f)
How can I insert list of lists into spreg.GM_Lag ? Thanks
Why do you want to pass it to the libpysal.io.open method? If I understand correctly this code, you first open a file, then read it (and the read method seems to be returning a List). So in your case, where you already have the matrix, you don't need to neither open nor read any file.
What will be needed though is what w is supposed to look like here: w = ww.read(). If it is a simple matrix, then you can initialize w = matrix. If the read method also format the data a certain way, you'll need to do it another way. If you could describe the expected behavior of the read method (e.g. what does the input file contain, and what is returned), it would be useful.
As mentioned, as the data is formatted into a libpysal.weights object, you must build one yourself. This can supposedly be done with this method libpysal.weights.W. (Read the doc too fast).
my goal is to have a string turned into a symbolic expression using sympify and then make substitutions.
import sympy as sp
Eq_Str = 'a*x+b'
Eq_Sym = sp.sympify(Eq_Str)
Then, for instance, substitute a for something else:
Eq_Sym.subs(a,2)
But I get the error:
Traceback (most recent call last):
File "<ipython-input-5-e9892d6ffa06>", line 1, in <module>
Eq_Sym.subs(a,2)
NameError: name 'a' is not defined
I understand that there is no symbol a in the workspace. Am I right?
Is there a way to get the symbols from the set I get from Eq_Sym.free_symbols into the workspace so I can substitute them in Eq_Sym.
Thank you very much for taking the time to read this.
you can use globals() for that:
import sympy as sp
Eq_Str = 'a*x+b'
Eq_Sym = sp.sympify(Eq_Str)
for s in Eq_Sym.free_symbols :
globals()[s.name] = s;
print (Eq_Sym.subs(a,2)); #b + 2*x
I have a sample json:
I want to use the json module of python and recurse through to find the "MaxSize" in "pevcwebasg". Have the following code:
import json
param_file_handle = json.load(open("sample.json"))
print param_file_handle['Resources']['pevcwebasg']['Type']
resources = param_file_handle['Resources']
for asg in resources:
print asg["Type"]
The out put of which is :
> AWS::AutoScaling::AutoScalingGroup Traceback (most recent call last):
> File "test.py", line 8, in <module>
> print asg['Type'] TypeError: string indices must be integers
What I dont get is this line "print param_file_handle['Resources']['pevcwebasg']['Type']" works fine and gets me the output but when i recurse through and try and find asg["Type"], it fails. Any better way to do this ? I need to recurse through the tree and find the value.
Edit 1:
as I do recurse through values, I get stuck with error.
param_file_handle = json.load(open("sample.json"))
resources = param_file_handle['Resources']
for asg in resources.values():
if asg["Type"] == "AWS::AutoScaling::AutoScalingGroup":
for values in asg['Properties']:
print values["MaxSize"]
error :
Traceback (most recent call last):
File "test.py", line 9, in <module>
print values["MaxSize"]
TypeError: string indices must be integers
for asg in resources:
This iterates through the keys of resources, rather than the values. Try:
for asg in resources.values():
param_file_handle = json.load(open("sample.json"))
resources = param_file_handle['Resources']
for asg in resources.values():
if asg["Type"] == "AWS::AutoScaling::AutoScalingGroup":
print asg["Properties"]["MaxSize"]
try this.
you broke hirerchy, missed "pevcwebasg"
resources = param_file_handle['Resources']['pevcwebasg']
for asg in resources:
print asg["Type"]
I am using netcdf4 in python 2.7 on a windows7 machine. I have loaded numpy recarrays into a netcdf file I created and have subsequently retrieved the data several times. Then, for some unknown reason when I try to retrieve the data I get a ValueError could not convert string to float:
The code that is being used to retrieve the data is:
def getNetCDFGroupVarData(NCfilename, GroupPath, Variable):
""" ==============================================================
TITLE: getNetCDFGroupVarData
DESCR: for a valid variable on the specified path in a NetCDF file
returns a data vector
ARGS: NCfilename : netcdf4 file path and name
GroupPath : group path
Variable : variable name
RETURN: VarData: vector of variable data
DEPEND: netCDF4.Dataset
=======================================================================
"""
# get rootgroup and group from which to return attributes
if os.path.isfile(NCfilename):
RG = Dataset(NCfilename, 'a')
G = giveListEndGroup(RG,GroupPath)
# retrieve variable data from group
keyVar = G.variables.keys()
print(keyVar)
kvlen = len(keyVar)
var = unicode(Variable)
if kvlen > 0 :
print('variable name: ',var)
V = G.variables[var]
print V.dtype
print V.shape
print V.dimensions
VarData = V[:] #====== Error raised here ==============
else:
print('no keys found')
VarData = None
RG.close()
return VarData
The print outputs and error stack I get when calling this function are:
[u'time', u'SECONDS', u'NANOSECONDS', u'Rg', u'Ts1', u'Ts2', u'Ts3', u'V_log', u'T_log']
('variable name: ', u'time')
float64
(88872,)
(u'time',)
variable: time does not exist
Unexpected error: <type 'exceptions.ValueError'>
Traceback (most recent call last):
File "C:\Users\rclement\Documents\My Dropbox\Code\python\NCTSutil\Panel_NCTS_structure.py", line 69, in tree_path_changed
pub.sendMessage('NetcdfTS.group.specified', arg1=pathlist )
File "C:\Python27\lib\site-packages\pubsub\core\kwargs\publisher.py", line 27, in sendMessage
topicObj.publish(**kwargs)
File "C:\Python27\lib\site-packages\pubsub\core\kwargs\publishermixin.py", line 24, in publish
self._publish(msgKwargs)
File "C:\Python27\lib\site-packages\pubsub\core\topicobj.py", line 376, in _publish
self.__sendMessage(data, self, iterState)
File "C:\Python27\lib\site-packages\pubsub\core\topicobj.py", line 397, in __sendMessage
self._mix_callListener(listener, data, iterState)
File "C:\Python27\lib\site-packages\pubsub\core\kwargs\publishermixin.py", line 64, in _mix_callListener
listener(iterState.filteredArgs, self, msgKwargs)
File "C:\Python27\lib\site-packages\pubsub\core\kwargs\listenerimpl.py", line 43, in __call__
cb(**kwargs)
File "C:\Users\rclement\Documents\My Dropbox\Code\python\NCTSutil\NetcdfTimeSeries.py", line 70, in listner_group
atime = self.GetSelectedVariableData(pathlist, u'time')
File "C:\Users\rclement\Documents\My Dropbox\Code\python\NCTSutil\NetcdfTimeSeries.py", line 307, in GetSelectedVariableData
VarData = MNU.getNetCDFGroupVarData(self.filename, GroupPathList, variable )
File "C:\Users\rclement\Documents\My Dropbox\Code\python\NCTSutil\MyNetcdfUtil.py", line 304, in getNetCDFGroupVarData
VarData = V[:]
File "netCDF4.pyx", line 2949, in netCDF4.Variable.__getitem__ (netCDF4.c:36472)
File "netCDF4.pyx", line 2969, in netCDF4.Variable._toma (netCDF4.c:36814)
ValueError: could not convert string to float:
When I use other netcdf utilities (i.e. panolpy) I can access the data.
Does anyone have a clue why netcdf4 would be throwing this excpetion - or worse - how it could have inserted a string in my float32 field in the netcdf file?
From the traceback the problem was occurring in the "_toma" Netcdf4 function which converts the data to a masked array. When reading the file with other utilities (eg. NCDUMP) I had no problem accessing the data.
At the moment I believe the problem occurred because I had an unassigned 'missing_value' attribute for the variable. Apparently, if there is no 'missing_value' attribute Netcdf4 defaults to a missing value appropriate for the dtype. In my implementation the 'missing_value' attribute was being exposed for editing via a wxpyhton grid control. When the edited attributes in the grid were written back to the netcdf file the empty grid cell was returning either a None object or wx.emptyString, which Netcdf4 attempted to insert into the float type in the netcdf file.
I am trying to take the inverse Fourier transform of a list, and for some reason I keep getting the following error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "simulating_coherent_data.py", line 238, in <module>
exec('ift%s = np.fft.ifft(nd.array(FTxSQRT_PS%s))'(x,x))
TypeError: 'str' object is not callable
And I can't figure out where I have a string. The part of my code it relates to is as follows
def FTxSQRT_PS(FT,PS):
# Import: The Fourier Transform and the Power Spectrum, both as lists
# Export: The result of FTxsqrt(PS), as a list
# Function:
# Takes each element in the FT and PS and finds FTxsqrt(PS) for each
# appends each results to a list called signal
signal = []
print type(PS)
for x in range(len(FT)):
indiv_signal = np.abs(FT[x])*math.sqrt(PS[x])
signal.append(indiv_signal)
return signal
for x in range(1,number_timesteps+1):
exec('FTxSQRT_PS%s = FTxSQRT_PS(fshift%s,power_spectrum%s)'%(x,x,x))
exec('ift%s = np.fft.ifft(FTxSQRT_PS%s)'(x,x))
Where FTxSQRT_PS%s are all lists. fshift%s is a np.array and power_spectrum%s is a list. I've also tried setting the type for FTxSQRT_PS%s as a np.array but that did not help.
I have very similar code a few lines up that works fine;
for x in range(1,number_timesteps+1):
exec('fft%s = np.fft.fft(source%s)'%(x,x))
where source%s are all type np.array
The only thing I can think of is that maybe np.fft.ifft is not how I should be taking the inverse Fourier transform for Python 2.7.6 but I also cannot find an alternative.
Let me know if you'd like to see the whole code, there is about 240 lines up to where I'm having trouble, though a lot of that is commenting.
Thanks for any help,
Teresa
You are missing a %
exec('ift%s = np.fft.ifft(FTxSQRT_PS%s)'(x,x))
Should be:
exec('ift%s = np.fft.ifft(FTxSQRT_PS%s)'%(x,x))