I am new in Python and I am trying to carry out a code working in a module of obspy package. From a .txt file with a row with five values separate by comas (example: 40,47.698,146.9212, etc....) I need to use these values as variables in a function of the obspy module. I Will show you the code and you understand better.
from obspy.taup import TauPyModel
model = TauPyModel(model="iasp91")
archivo=open('Dato.txt', 'r')
for linea in archivo.readlines():
columna = str(linea).split(',')
print(columna[0])
print(columna[1])
print(columna[2])
print(columna[3])
print(columna[4])
archivo.close()
a=columna[0]
b=columna[1]
c=columna[2]
d=columna[3]
e=columna[4]
arrivals=model.get_pierce_points_geo(a, b, c, d, e, phase_list=('SKS',), resample=False)
arrival = arrivals[0]
print(arrival.pierce)
If I define the variables as a numeric value (example:a=408; b=47.6981; c=146.9212; etc ….) code works perfectly and it shows me that I want:
408
47.6981
146.9212
36.882277
-3.068689
C:\Users\peopl\Desktop\BO\env\lib\site-packages\obspy\taup\tau_branch.py:496: UserWarning: Resizing a TauP array inplace failed due to the existence of other references to the array, creating a new array. See Obspy #2280.
warnings.warn(msg)
[ ( 323.37738085, 0.00000000e+00, 0.00000000e+00, 408. , 47.6981 , 146.9212 )
( 323.37738085, 4.25942791e-01, 9.18383444e-05, 410. , 47.70292225, 146.9180712 )
( 323.37738085, 4.95211705e+01, 1.33680904e-02, 660. , 48.39912219, 146.45957792)
( 323.37738085, 4.30994629e+02, 3.09568047e-01, 2889. , 63.17117462, 131.25054174)
( 323.37738085, 6.19102877e+02, 7.88455257e-01, 3482.54497821, 73.50766588, 55.65029149)
( 323.37738085, 8.07211124e+02, 1.26734247e+00, 2889. , 54.05973754, 7.50927585)
( 323.37738085, 1.18868458e+03, 1.56354242e+00, 660. , 38.47340944, -2.34102958)
( 323.37738085, 1.23777981e+03, 1.57681868e+00, 410. , 37.75869395, -2.67200616)
( 323.37738085, 1.23820575e+03, 1.57691051e+00, 408. , 37.75374671, -2.67427329)
( 323.37738085, 1.28179336e+03, 1.58536568e+00, 210. , 37.29809076, -2.88171143)
( 323.37738085, 1.32180477e+03, 1.59207012e+00, 35. , 36.93652754, -3.04441779)
( 323.37738085, 1.32587993e+03, 1.59253065e+00, 20. , 36.91168346, -3.05553737)
( 323.37738085, 1.33192110e+03, 1.59307573e+00, 0. , 36.882277 , -3.068689 )]
Nevertheless, when I use the variables from .txt file the code shows this:
408
47.6981
146.9212
36.882277
-3.068689
Traceback (most recent call last):
File "pierce.py", line 20, in <module>
arrivals=model.get_pierce_points_geo(a, b, c, d, e, phase_list=('SKS',), resample=False)
File "C:\Users\peopl\Desktop\BO\env\lib\site-packages\obspy\taup\tau.py", line 784, in get_pierce_points_geo
distance_in_deg = calc_dist(source_latitude_in_deg,
File "C:\Users\peopl\Desktop\BO\env\lib\site-packages\obspy\taup\taup_geo.py", line 53, in calc_dist
return calc_dist_azi(source_latitude_in_deg, source_longitude_in_deg,
File "C:\Users\peopl\Desktop\BO\env\lib\site-packages\obspy\taup\taup_geo.py", line 86, in calc_dist_azi
g = ellipsoid.Inverse(source_latitude_in_deg,
File "C:\Users\peopl\Desktop\BO\env\lib\site-packages\geographiclib\geodesic.py", line 1035, in Inverse
a12, s12, salp1,calp1, salp2,calp2, m12, M12, M21, S12 = self._GenInverse(
File "C:\Users\peopl\Desktop\BO\env\lib\site-packages\geographiclib\geodesic.py", line 712, in _GenInverse
lon12, lon12s = Math.AngDiff(lon1, lon2)
File "C:\Users\peopl\Desktop\BO\env\lib\site-packages\geographiclib\geomath.py", line 156, in AngDiff
d, t = Math.sum(Math.AngNormalize(-x), Math.AngNormalize(y))
TypeError: bad operand type for unary -: 'str'
Numeric values of the first five rows are the same from .txt file but it seems to show a problem with 'str'. I will be pleased to you if you can help me to solve the problem. Sorry my Arcaic English and my novel status in Python.
Thank you very much and greetings to all of you.
I solved the problem thanks to another boy in the Spanish Community. I have to convert the variables to numeric values because the code read it as strings. I solved with this change:
a = float(columna[0])
b = float(columna[1])
c = float(columna[2])
d = float(columna[3])
e = float(columna[4])
Thanks for all and I hope to go on my learning (Programming with Python and writing in English).
Related
I am trying to understand why I get invalid number of argument in the below code and if there is a way to fix it.
here is the code:
import numpy as np
acc_reading = []
a = np.array([0.11e+00, 1.11e-08, 1.11e-02])
b = np.array([0.12e+00, 1.22e-08, 2.22e-02])
c = np.array([3.11e+00, 3.18e-08, 3.33e-02])
d = np.array([3.41e+00, 4.18e-08, 4.31e-02])
e = np.array([0.55e+00, 1.55e-08, 5.31e-02])
f = np.array([0.66e+00, 1.66e-08, 3.66e-02])
g = np.array([0.66e+00, 1.66e-08, 3.66e-02])
h = np.array([0.66e+00, 1.66e-08, 3.66e-02])
ab = np.add(a,b)
cd = np.add(c, d)
ef = np.add(e, f)
i = np.add(g, h)
acc_reading.append(ab)
acc_reading.append(cd)
acc_reading.append(ef)
acc_reading.append(i)
kk = np.add(acc_reading[0], acc_reading[1], acc_reading[2], acc_reading[3])
The output of the above code is:
ValueError: invalid number of arguments
Read the docs for np.add and np.sum
Your lists - 4 terms of size 3
In [213]: acc_reading
Out[213]:
[array([2.30e-01, 2.33e-08, 3.33e-02]),
array([6.52e+00, 7.36e-08, 7.64e-02]),
array([1.21e+00, 3.21e-08, 8.97e-02]),
array([1.32e+00, 3.32e-08, 7.32e-02])]
sum all values to one:
In [214]: np.sum(acc_reading)
Out[214]: 9.5526001622
sum rows and columns - treating acc_reading as (3,4) array:
In [215]: np.sum(acc_reading, axis=0)
Out[215]: array([9.280e+00, 1.622e-07, 2.726e-01])
In [216]: np.sum(acc_reading, axis=1)
Out[216]: array([0.26330002, 6.59640007, 1.29970003, 1.39320003])
Your attempt to use np.add (which you used correctly earlier)
In [217]: np.add(acc_reading[0], acc_reading[1], acc_reading[2], acc_reading[3])
...:
Traceback (most recent call last):
File "<ipython-input-217-52b8f942b588>", line 1, in <module>
np.add(acc_reading[0], acc_reading[1], acc_reading[2], acc_reading[3])
TypeError: add() takes from 2 to 3 positional arguments but 4 were given
giving it just 2 arrays:
In [218]: np.add(acc_reading[0], acc_reading[1])
Out[218]: array([6.750e+00, 9.690e-08, 1.097e-01])
a more direct way:
In [220]: arr = np.array([a+b, c+d, e+f, g+h])
In [221]: arr
Out[221]:
array([[2.30e-01, 2.33e-08, 3.33e-02],
[6.52e+00, 7.36e-08, 7.64e-02],
[1.21e+00, 3.21e-08, 8.97e-02],
[1.32e+00, 3.32e-08, 7.32e-02]])
In [222]: arr.sum()
Out[222]: 9.5526001622
q.head()
Outputs
Weekly_Sales
Date
2010-02-28 131963.08
2010-03-31 91237.14
2010-04-30 150516.76
2010-05-31 66694.15
2010-06-30 66740.70
Now the problem i'm facing is that i want to plot 'Date' Column vs 'Weekly_Sales' Column. I've already used the command
q=y.resample('M',on='Date').sum()
to convert weekly data to monthly which results in the upper Dataframe.
type(q)
outputs "class 'pandas.core.frame.DataFrame'" showing that q is a data frame. Now since q doesn't have two different columns as shown here,
q.Weekly_Sales
outputs
Date
2010-02-28 131963.08
2010-03-31 91237.14
2010-04-30 150516.76
2010-05-31 66694.15
2010-06-30 66740.70
2010-07-31 81915.01
2010-08-31 64578.81
2010-09-30 71913.27
2010-10-31 134644.53
2010-11-30 92161.40
2010-12-31 173983.88
2011-01-31 69146.59
2011-02-28 125762.63
2011-03-31 82823.34
2011-04-30 165056.95
2011-05-31 68251.72
2011-06-30 62978.57
2011-07-31 78856.23
2011-08-31 59061.95
2011-09-30 87756.41
2011-10-31 98806.83
2011-11-30 98537.51
2011-12-31 174512.07
2012-01-31 70205.35
2012-02-29 134683.30
2012-03-31 114680.54
2012-04-30 125600.12
2012-05-31 70792.98
2012-06-30 83646.54
2012-07-31 66468.79
2012-08-31 83045.57
2012-09-30 76137.90
2012-10-31 96244.56
Freq: M, Name: Weekly_Sales, dtype: float64
whereas
q.Date
outputs
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
q.Date
File "C:\Program Files (x86)\Python36-32\lib\site-packages\pandas\core\generic.py", line 3614, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'Date'
since both the columns come under q.Weekly_Sales , how do i seperate them to get 2 columns and finally plot them?
double [[]] will query the single columns as dataframe rather than Series, then we using reset_index
new_s=q[['Weekly_Sales']].reset_index()
Please can someone help with a solution for running Discrete wavelet transform in R.
I have tried with the following data format; Year, Rain.
Year is in form of 1970,1972,1973.... and Rain in form of 200, 85, 34, 56 23, 0.5... etc. I don't know if my data frame is correct. or if i need to do something to the data before I run it.
Haven't saved the data.frame as wave, I ran as:
rain.dwt.01 <- wavDWT(wave)
Here is my code:
getwd()
setwd("C:\\Users\\dell\\Desktop\\ANN")
wave<-read.csv(file.choose(),header = T)
library(wmtsa)
library(wavelets)
library(waveslim)
library(MASS)
library(wavethresh) ### loaded auxillary functions from Internet
con <- url("faculty.washington.edu/dbp/R-CODE/workshop.Rdata")
print(load(con))
close(con)
lplot(wave)
abline(h=mean(wave),lty="dotted",col="red")
rain.dwt.01 <- wavDWT(wave)
I got this error:
Error in itCall("RS_wavelets_transform_discrete_wavelet_convolution", :
(list) object cannot be coerced to type 'double
Please help with example so I can understand why this error appears
I use Python3/pywinauto/and tested app - all are 64.
I got a error when I trying to expend a tree
tree_item = systreeview.GetItem([current_menu_item, u'xxxxxx'])
everything worked with 32 app.
*log:
File "C:\Python36\lib\site-packages\pywinauto\controls\common_controls.py", line 1523, in get_item
texts = [r.text() for r in roots]
File "C:\Python36\lib\site-packages\pywinauto\controls\common_controls.py", line 1523, in <listcomp>
texts = [r.text() for r in roots]
File "C:\Python36\lib\site-packages\pywinauto\controls\common_controls.py", line 960, in text
return self._readitem()[1]
File "C:\Python36\lib\site-packages\pywinauto\controls\common_controls.py", line 1383, in _readitem
remote_mem)
ctypes.ArgumentError: argument 4: <class 'OverflowError'>: int too long to convert*
It was a bug. Fixed now. Thank you everyone.
Fixed another way in pull request #373. pywinauto 0.6.3 is out with the fix.
Just replaced 2 remaining win32functions.SendMessage calls with self.send_message everywhere.
I want to get all the dihedral angles of a protein in Pymol (phi, psi, chi1, chi2, chi3, chi4) but I only manage to find a function that can shows me the phi and psi.
For instance:
PyMOL>phi_psi 1a11
SER-2: ( 67.5, 172.8 )
GLU-3: ( -59.6, -19.4 )
LYS-4: ( -66.4, -61.7 )
MET-5: ( -64.1, -17.9 )
SER-6: ( -78.3, -33.7 )
THR-7: ( -84.0, -18.1 )
ALA-8: ( -85.7, -40.8 )
ILE-9: ( -75.1, -30.8 )
SER-10: ( -77.6, -47.0 )
VAL-11: ( -61.3, -27.4 )
LEU-12: ( -60.7, -47.5 )
LEU-13: ( -71.1, -38.6 )
ALA-14: ( -46.2, -50.7 )
GLN-15: ( -69.1, -47.4 )
ALA-16: ( -41.9, -52.6 )
VAL-17: ( -82.6, -23.7 )
PHE-18: ( -53.4, -63.4 )
LEU-19: ( -61.2, -30.4 )
LEU-20: ( -61.1, -32.3 )
LEU-21: ( -80.6, -60.1 )
THR-22: ( -45.9, -34.4 )
SER-23: ( -74.5, -47.8 )
GLN-24: ( -83.5, 11.0 )
It's missing the chiral angles. Does anyone know how to get all the dihedral angles?
Many thanks!
You can get arbitrary dihedral angles with get_dihedral. Create four selections, each with a single atom and then use it like this:
get_dihedral s1, s2, s3, s4
It's exposed to the Python API as cmd.get_dihedral(). I suggest writing a Python script that uses this function along with cmd.iterate() to loop over residues. Create a dict so that on each residue you can look up a list of atom quadruples that define the chi-angles.
You can easily do it in R. This is the link containing information on how to calculate the main chain and side chain Torsion/Dihedral Angles:
http://thegrantlab.org/bio3d/html/torsion.pdb.html
But first you have to install the Bio3D package for R: http://thegrantlab.org/bio3d/download
After installing the package, load it by typing library(bio3d) at the R console prompt.
>library(bio3d)
This R script answers your question:
#returns the file path of the current working directory.
getwd()
#sets the working directory to where you want.
setwd("home/R/Rscripts")
#fetches the pdb file from the protein data bank and saves to dataframe 'pb'
pb <- read.pdb("insert PDB ID")
#trim to protein only
pb.prot <- trim.pdb(pb, "protein")
#calculates the torsion angles of the protein and save to dataframe 'tor'
tor <- torsion.pdb(pb.prot)
#to get the number of rows and columns of 'tor'
dim(tor$tbl)
#identify each row by their chain, residue ID and residue Number obtained from your PDB entry
res_label <- paste(pb.prot$atom$chain[pb.prot$calpha], pb.prot$atom$resid[pb.prot$calpha], pb.prot$atom$resno[pb.prot$calpha], sep="-")
rownames(tor$tbl) <- res_label
#creates a table of the torsion angle
torsion <- tor$tbl
#For example, to look at the angles for VAL, residue 223 from chain A
tor$tbl["A-VAL-223",]
#writes out the table to a file
write.table(torsion, file = "torsion_angles.txt", quote = F, sep = "\t")
Your output file which is saved in your working directory will contain a table of the chain-resID-resNo and their corresponding phi, psi, chi1, chi2, chi3, chi4, and chi5 values. Goodluck!
#install bio3d library and call
library(bio3d)
#returns the file path of the current working directory.
getwd()
#sets the working directory to where you want.
setwd("home/R/Rscripts")
#fetches the pdb file from the protein data bank and saves to dataframe 'pb'
pb <- read.pdb("insert PDB ID")
#trim to protein only
pb.prot <- trim.pdb(pb, "protein")
#calculates the torsion angles of the protein and save to dataframe 'tor'
tor <- torsion.pdb(pb.prot)
#to get the number of rows and columns of 'tor'
dim(tor$tbl)
#identify each row by their chain, residue ID and residue Number obtained from your PDB entry
res_label <- paste(pb.prot$atom$chain[pb.prot$calpha], pb.prot$atom$resid[pb.prot$calpha], pb.prot$atom$resno[pb.prot$calpha], sep="-")
rownames(tor$tbl) <- res_label
#creates a table of the torsion angle
torsion <- tor$tbl
#For example, to look at the angles for VAL, residue 223 from chain A
tor$tbl["A-GLY-65",]
#convert "double" into a datatype
dataframe_df=as.data.frame.matrix(torsion)
#write dataframe to a .csv file
write.csv(dataframe_df, file="name.csv", row.names=TRUE,col.names=TRUE)