I am using rpy2 to use a R library in Python. The library has a function prebas() that returns an array in which the item with index [8] is the output. When I write this output to CSV within the R code snippet, everything works as expected (the output is a CSV over 200kB). However, when I return the same object (PREBASout[8]), it returns an empty object. So, obviously, when I write that object to CSV, the file is empty.
run_prebasso = robjects.r('''
weather <- read.csv("/home/example_inputs/weather.csv",header = T)
PAR = c(weather$PAR,weather$PAR,weather$PAR)
TAir = c(weather$TAir,weather$TAir,weather$TAir)
Precip = c(weather$Precip,weather$Precip,weather$Precip)
VPD = c(weather$VPD,weather$VPD,weather$VPD)
CO2 = c(weather$CO2,weather$CO2,weather$CO2)
DOY = c(weather$DOY,weather$DOY,weather$DOY)
library(Rprebasso)
PREBASout = prebas(nYears = 100, PAR=PAR,TAir=TAir,VPD=VPD,Precip=Precip,CO2=CO2)
write.csv(PREBASout[8],"/home/outputs/written_in_r.csv",row.names = F)
PREBASout[8]
''')
r_write_csv = robjects.r['write.csv']
r_write_csv(run_prebasso, "/home/outputs/written_in_py.csv")
This is what the code snippet returns:
(Pdb) run_prebasso
<rpy2.rinterface.NULLType object at 0x7fc1b31e6b48> [RTYPES.NILSXP]
Question: Why aren't written_in_py.csv and written_in_r.csv the same?
I have just found the bug. The problem was in the line
write.csv(PREBASout[8],"/home/outputs/written_in_r.csv",row.names = F)
This statement was returned instead of what I wanted (PREBASout[8]). When I removed it or assigned it to a variable, everything worked as expected.
Related
I'm trying to make a program able to print wafermaps and histograms for each value selected.
To achieve that, I've made one button to show the graphics of the next parameter selected from the list.
The histogram is shown as I want for every parameter, but it doesn't work for the wafermap graph and it shows this error.
# NEXT PARAMETER
def next_parameter_analyze(self, data_values):
widgets.cmbCurrentParameter.setCurrentText("")
FileName = widgets.txtDataFile.text()
result_file = ResultFile(FileName)
self.measurements = result_file.get_params(list(self.txtParameters.keys()))
self.actual_parameter=(self.actual_parameter+1)%len(self.txtParameters)
par=list(self.txtParameters.keys())[self.actual_parameter]
widgets.cmbCurrentParameter.setCurrentText(par)
self.data_value = self.txtParameters[par].replace(" ", "\t")
estadistica = StatisticsEstepa(self.actual_parameter,self.measurements[par]["measure"],self.config["estepa"])
self.generate_histogram() #GRAPH WORKING
self.generate_wafermap(data_values) #GRAPH NOT WORKING
data_values is necessary to get the values for every parameter, in the histogram graph is not necessary, and it's defined in another function as:
# Get data values from result_file
for fileName in parameters_file_list:
self.textoParametros[fileName]=""
data_values = result_file.get_data_values(fileName) #HERE
for chip in data_values:
self.textoParametros[fileName]+=str(chip)+"\t"+str(data_values[chip])+"\n"
And the get_data_values function is:
def get_data_values(self, name_param):
# get data values chip + measure for printing in QPlainText
get_values = dict()
for die in self.dies:
for module in self.modules:
for param in self.params_list:
if param == name_param:
measure = self.params[die][module][param] # get measure value
if not die in get_values:
get_values[die] = dict()
get_values[die] = measure
return get_values
Not sure where in your code this comes up but it sounds like you have some variable that is a boolean and you tried to access it like
somebool.items and Python is telling you that somebool has no attribute items
A mathematica program returns as an output a sum of cardinal sines. The first element of the vector is
-19.9959 Sinc[0.0418879 (0. + t)] Sinc[0.0897598 (-65. + u)]
The variable is saved in a text file; however, this has to be read in pyomo as a variable, so a StringReplace is used to adapt this variable to python's grammmar
savedXPython =
Import["savedWindX.txt"] //
StringReplace[#, {"[" -> "(", "]" -> ")",
"t" -> "m.lammda[i]*180/np.pi", "u" -> "m.phi[i]*180/np.pi"}] &
Then, savedXPython was saved to another text file. However, an error appeared while working with pyomo; I asked here and the answer was to save the result in a json file instead of a text.
Export["savedWindXPython.txt", savedXPython];
Export["savedWindXPythonJ.json", savedXPython, "ExpressionJSON"];
Now, in the pyomo part, the text file was originally read as
g = open("savedWindXPython.txt","r")
b=f.readline()
g.close
later on, following this thread, the json has been read as
f = open("savedWindXPythonJ.json","r")
a=f.readline()
f.close
And then, the variable inside the pyomo code is defined as
def Wind_lammda_definition(model, i):
return m.Wind_lammda[i] == a
m.Wind_lammda_const = Constraint(m.N, rule = Wind_lammda_definition)
in the case of the json file or
def Wind_lammda_definition(model, i):
return m.Wind_lammda[i] == b
m.Wind_lammda_const = Constraint(m.N, rule = Wind_lammda_definition
in the case of the original text file
The code however doesn't work. AttributeError: 'str' object has no attribute 'is_relational', the error which prevented me from just reading the variable from the text file also appears in the json case.
It seems that using the json format has not helped. Can someone tell me if the json implementation has been done wrong?
When reading a line from your file, Pyhton will always return a string. If 1 is the only content in your line, the returned value will be equal to "1" and not 1. That can be solved with a = float(a), since you want to use a numeric value in your constraint. This will simply convert your a string into a float.
I am trying to create a function in python 3 that builds a dataframe from a csv file. However, I keep getting a syntax error when I call
y = (data_df["Status"].replace("underperform",0).replace("outperform",1).values.tolist())
This line of code is not running, because I never actually call the function. Here is all of my code.
def Build_Data_Set(features = ["DE Ratio","Trailing P/E"]):
data_df = pd.read_csv("key_stats.csv") #created in other file
X = np.array(data_df[features].values#.tolist())
y = (data_df["Status"].replace("underperform",0).replace("outperform",1).values.tolist())
return X,y
How should I go about fixing this error?
You're missing a closing parenthesis in your X = np.array(data_df[features].values#.tolist()) - it's there, but it's commented out of the code with the # sign.
Your python interpreter does not know that you actually wanted to end that line there and continues to search for a closing parenthesis. Before it finds one, it stumbles over the assignment in the next line, which is illegal and causes your syntax error.
You can simply do:
def Build_Data_Set(features = ["DE Ratio","Trailing P/E"]):
data_df = pd.read_csv("key_stats.csv") #created in other file
X = data_df[features].values # already returns an array
y = data_df["Status"].replace({"underperform": 0, "outperform":1}).values
return X,y
df = Build_Data_Set()
I'm having some problem with avoiding my code to repeat itself, like the title says, when I import data from a txt-file. My question is, if there is a smarter way to loop the function. I'm still very new to python in general so I don't have good knowledge in this area.
The code that I'm using is the following
with open("fundamenta.txt") as fundamenta:
fundamenta_list = []
for row in fundamenta:
info_1 = row.strip()
fundamenta_list.append(info_1)
namerow_1 = fundamenta_list[1]
sol_1 = fundamenta_list[2]
pe_1 = fundamenta_list[3]
ps_1 = fundamenta_list[4]
namerow_2 = fundamenta_list[5]
sol_2 = fundamenta_list[6]
pe_2 = fundamenta_list[7]
ps_2 = fundamenta_list[8]
namerow_3 = fundamenta_list[9]
sol_3 = fundamenta_list[10]
pe_3 = fundamenta_list[11]
ps_3 = fundamenta_list[12]
So when the code is reading from "fundamenta_list" how do I change to prevent code repetition?
It looks to me that your input file has records each as a block of 4 rows, so in turn is namerow, sol, pe, ps, and you'll be creating objects that take these 4 fields. Assuming your object is called MyObject, you can do something like:
with open("test.data") as f:
objects = []
while f:
try:
(namerow, sol, pe, ps) = next(f).strip(), next(f).strip(), next(f).strip(), next(f).strip()
objects.append(MyObject(namerow, sol, pe, ps))
except:
break
then you can access your objects as objects[0] etc.
You could even make it into a function returning the list of objects like in Moyote's answer.
If I understood your question correctly, you may want to make a function out of your code, so you can avoid repeating the same code.
You can do this:
def read_file_and_save_to_list(file_name):
with open(file_name) as f:
list_to_return = []
for row in f:
list_to_return.append(row.strip())
return list_to_return
Then afterwards you can call the function like this:
fundamenta_list = read_file_and_save_to_list("fundamenta.txt")
I am running into a problem trying to pass a Python list to an R legend call for a plot using rpy2. I generate a list of text names and their corresponding symbols from an existing dictionary as:
dataDict = {'data':{...},
'symbols':{'xylem':14,'groundwater':17,'lagoon':16,'stream':15}}
nameListForLegend = [name for name in dataDict["symbols"]]
symbolListForLegend = [dataDict['symbols'][category] \
for category in dataDict['symbols']]
The nameListForLegend variable works in the call, but the symbolListForLegend does not:
r.legend(1.5, -42, nameListForLegend,
pch = symbolListForLegend,cex=3,col="black",border = 0)
I have tried passing it in as an R object with hacky solutions such as:
#after converting each list element to a string
symbolListString = ",".join(symbolListForLegend)
symbolListForLegend = robjects.r('list('+symbolListString+')')
All return:
RRuntimeError: Error in plot.xy(xy.coords(x, y), type = type, ...) :invalid plotting symbol
As is usually the case, I'm missing something simple here. Any help or direction to appropriate documentation would be greatly appreciated.
Thank you.
symbolListForLegend is a Python list, which has no direct conversion to an R object (although it could be an R list with default-named elements). The R function is expecting a vector.
Try:
from rpy2.robjects.vectors import IntVector
symbolListForLegend = [dataDict['symbols'][category] \
for category in dataDict['symbols']
symbolVectorForLegend = IntVector(symbolListForLegend)
r.legend(1.5, -42, nameListForLegend,
pch = symbolVectorForLegend, cex=3, col="black", border = 0)