Sourcing a python script in Rstudio - python

So I can run my python script eq_odds.py (which takes a file as input) in my terminal as follows: eq_odds.py data/health.csv
I need to use this output in my R code, therefore I want to source this python code directly in Rstudio.
The sourcing went well, I just don't know how to get the python code running with the input file in RStudio.
Has anyone got a clue?
The python script and data can be found at https://github.com/gpleiss/equalized_odds_and_calibration

Related

my code is not showing is the terminal and there is no error

I was trying to run a code in vs code but the code didn't run and there is no error showing in the terminal. I made a new file and then tried to run a "Hello World!" but the code didn't run too. I uninstalled and re-installed vs code (three times) but that didn't work.
And when I say that there is no error showing I mean it. whenever I run the code nothing happens and vs code doesn't even give a hint or a sign that the code got executed or not.
First, you should understand that vscode is a lightweight editing
tool. If you want to run python files, first make sure you have
installed python correctly. You can enter command python --version
in the terminal to confirm it.
Then, please install python extension in the vscode extension store.
Then use shortcuts "Ctrl+Shift+P" and type "Python: Select
Interpreter" to choose the python interpreter.
Finally, creat *.py file and run it.
Read this docs for more details which can help you get started faster.

How do I tell VS Code to run my python file in the correct terminal?

In VS Code, I have two terminals open. In one of them, I have python activated (as I like to tinker with my python code). The problem is, when I attempt to run my python file, VS Code tries to execute it in that terminal (the one with python activated), so the file obviously does not run.
Is there a way to fix this annoying behavior?

Executing command line program (Abaqus-python script) from python script

I am trying to run an Abaqus-python script file from another python script executed in Spyder (python 3.7, windows 10).
If I run the following command in CMD
abaqus cae -noGUI model.py
the code executes as expected. However, if I try to run cp = subprocess.run('abaqus cae -noGUI model.py',shell=True)
I don't get any output. Below is what is returned in cp.
Notes:
The Abaqus-python file I am trying to run is in the same folder as the python script I have.
I have tried various sp.call, sp.run, os.systems in different string and list formats.
"C:\Abaqus\Commands" and "C:\SIMULIA\Commands" are both in my PATH environment variable.
I think the -noGUI call is important to the problem. I can't seem to find others on SO with this issue.
After a fair amount of digging, I think the issue is that the PATH for my python install is done thought Anaconda so I have a different path this vs CMD. I fixed this by adding the full Abaqus location to the command. See below.
subprocess.call(r'C:\SIMULIA\Commands\abaqus job=Job-1 ask_delete=OFF interactive', shell=True)
I am not sure what your problem is due to, but note that Abaqus scripting is based on python 2.7, therefore you might need to downgrade it

Why will my python script only run in Jupyter Notebook, but not in VS Code if I am running the same interpreter for each?

I would just like to say off the bat, that I am very new to programming and this could be a real quick fix.
I am having trouble running a python script out of a standard VS Code from my 'base' conda env: python 3.7.6, but for some reason the same code when transferred into a jupyter notebook or even ran from the makeshift jup notebook on VS CODE it works fine.
When I run the code in the standard VS Code with the same python interpreter, I continue to have the following errors:
I would really like to learn how to avoid this in the future, because I prefer to write in VS Code modules instead of out of Jup. Notebooks.
Thanks for the help!
data= pickle.load(open('myData.pickle','rb'))
_pickle.UnpicklingError: invalid load key, '\xef'.
Here I also attached the image of the jup notebook output to show that the code runs fine on its own when not in a standard script .py file. Since I am new, the best I can do is leave the link to the image.

Logging input and output in Spyder's console

I learnt data manipulation and analysis through Stata and I used the log command to record all the commands written and the output generated. Doing so I could reproduce my findings, check previous results and share it with others in pdf or txt. What to use with Python? Is there a difference if I use a Python Jupyter Notebook or Spyder?
The way to do what you want is by using the %logstart command, as described here:
Log IPython output?
Stop using the python interpreter and start storing your commands in a text file before running them. This way you will be able to share, reuse, and revise your python code.
By executing the following command:
python code.txt
Python will read all of the lines in your file sequentially.
This is the main way python programs are executed, as a convention, python code files end with .py , so if you see a file with a .py extension, you can try executing it yourself using this method!

Categories