PythonHowTo : Différence entre versions

De Transport
(Page créée avec « = Where to start = Python tutorials == Install Python and Modules == * I recommend a scientific distribution of Python, ie Python with t... »)
 
(Where to start)
Ligne 44 : Ligne 44 :
 
* Better interpreter: ipython https://ipython.org/
 
* Better interpreter: ipython https://ipython.org/
 
* Running and presenting code with Jupyter Notebooks https://jupyter.org/
 
* Running and presenting code with Jupyter Notebooks https://jupyter.org/
 +
 +
= Links =
 +
* Finding the shortest cycling path in the shade https://medium.com/@tanyamarleytsui/shady-streets-6dad0979c13a
  
 
= Random Topics=
 
= Random Topics=

Version du 15 août 2022 à 13:21

Where to start

Python tutorials

Install Python and Modules

  • I recommend a scientific distribution of Python, ie Python with the improved interpreter iPython and the core scientific libraries (Numpy, Scipy, Matplotlib, Pandas, etc.) that are used for example in the project Traffic Intelligence:
    • There are three main choices: anaconda, PythonXY and Enthought Python Distribution.
    • In addition, if you want to install other Python packages not provided by default, such as OpenCV that is needed to display video data and replay extracted trajectories over the video (used in the cvutils module). If using anaconda, OpenCV (it it similar for other packages) can be installed in the following way (typing the following in a command line):
$ conda install opencv3

or (you can search your preferred version using)

$ anaconda search -t conda opencv
$ anaconda show <user/package>
$ conda install --channel https://conda.anaconda.org/menpo opencv3 # for example

I recommend a good text editor with indispensable functionalities such as text coloring and parenthesis highlighting such as notepad++ on Windows, or atom and gedit (for Windows, Mac and Linux).

Environment Variables for your Code and Other Downloaded Libraries

To use Python modules from your code or downloaded code, the Python interpreter needs to know where to look for them. The first and preferred way is to set (or add to) the environment variable PYTHONPATH to refer to the location of the Python modules to load:

  • on Windows, add or modify the PYTHONPATH environment variable. Given the example installation path C:\Code\my_python_modules\, it should be set to C:\Code\my_python_modules\, e.g. C:\Users\username\traffic-intelligence\ for the Traffic Intelligence modules (in the trafficintelligence package);
  • on Linux, add or modify the PYTHONPATH environment variable by typing $export PYTHONPATH=/home/username/Code/my_python_modules/ in a terminal. However, you have to do that for each terminal before running a Python interpreter. The better way is to set the environment variable when your shell starts. If using bash, simply add the previous command at the end of the user .bashrc file (in the home directory);
  • on both platforms, other paths can be added, separated by :.

If that did not work, you can add the path to the sys.path system variable in the Python interpreter or at the beginning of your code, before your import statements:

$ import sys
$ sys.append('path_to_python_modules')
$ import your_module

To test your installation, start a Python interpreter (or even better, ipython) in a new terminal (the variable will not be added to the ones that were started before the PYTHONPATH variable was added) and try to import one of your modules in the directory referred to by PYTHONPATH:

$ import your_module

Recommended Tools

Links

Random Topics

Shapely

- I left functions in moving (in Point) that returns whether a point is in a polygon or not. inPolygonNoShapely(polygon) where polygon is a Nx2 numpy array representing the polygon

- with Shapely, use their polygon and point class, eg

from shapely.geometry import Polygon, Point poly = Polygon(array([[0,0],[0,1],[1,1],[1,0]])) p = Point(0.5,0.5) poly.contains(p) -> returns True poly.contains(Point(-1,-1)) -> returns False

You can convert a moving.Point to a shapely point: p = moving.Point(1,2) p.asShapely() returns the equivalent shapely point

If you have several points to test, use moving.pointsInPolygon(points, polygon) where points are moving.Point and polygon is a shapely polygon.

You should dig in shapely for functions that compute intersections (there is one in our library, but it is slow): http://toblerity.github.io/shapely/manual.html#object.crosses from shapely.geometry import Polygon, Point, LineString coords = [(0, 0), (1, 1), (1, -1), (0, 1)] LineString(coords).crosses(LineString([(0, 1), (1, 0)]))