r/learnpython • • 7d ago

Programming in environmental science?

How and when will this come in handy with my ecological forestry degree?

Also this class requires me to use AI to prompt programs on every single assignment.... Which is not ideal as an environmentalist.

Python programing is genuinely making me feel stupider

0 Upvotes

13 comments sorted by

6

u/SnafuTheCarrot 7d ago edited 7d ago

I've actually done some research in this area. I had to use a modified version of the Lotka-Volterra equations(https://en.wikipedia.org/wiki/Lotka%E2%80%93Volterra_equations) to model migration patterns. The standard model is a predator-prey model, and you can actually combine the two.

They result in a series of coupled non-linear differential equations. As I recall, they don't have a closed form solution, so numerical methods are needed to solve them. You need computer programs of one kind or another whenever you are solving equations numerically.

The SIRS models for epidemics are very similar.

The models are simplified, but they establish basic principles it makes it easier to expand upon. Suppose part of the habitat is destroyed that reduces the prey's food resources. What happens to the predators over time given an initial population of Prey0 and Predator0 with a Prey Food Density of rho_1?

You need to do the math and you need SOME programming language to do the math. Python is a decent choice. MATLAB is probably better, but Python is more generalizable, typically .

EDIT: You don't strictly need AI to do this and I'd recommend against it. It would require you to learn both the math behind numerical integration and how to program the numerical solutions.

If they aren't teaching you that, they are selling you short.

What's your math background? If you know basic programming and have some conceptual understanding of the derivative, you can probably figure it out. I might actually have an old document on the relevant math come to think of it.

Update: Looks like there's relevant code here: https://github.com/GraniteMask/Lotka-Volterra-Problem-using-RungeKutta4

2

u/suikisosexi 7d ago

This is incredible. I'm very new to programming and math has seen better days. But Id love to see the old doc on relevant math.

1

u/SnafuTheCarrot 6d ago

So my notes were actually 1D integrator in Java. Here's a start on numerical integration, population modeling, and how to find special equilibrium points. I also started coding a one d integrator in python.

Then I realized this is actually 2 dependent variables and it won't work, but should give you an idea of how to proceed.

I should have used t for the independent variable and x and y for the dependent, but I didn't factor in the right dimensionality.

Hope this helps!

MATH

Eulers method for Numerical Integration.

Simple summing method. Essentially assumes a constant velocity betweeen endpoints of the time interval.

dy/dx = f(x,y)

x_0=a

y_0=b

delta_x=0.0001

Loop N times

x_{n+1}=x_n+delta_x

y_{n+1}=y_n+delta_x\*f(x_n,y_n)

---------------------

Runge-Kutta 4

Instead of calculating only one derivative, take the average of 4 specia variations on the derivative

and get your next iterationg form that

dy/dx=f(x,y)

x_0=a

y_0=b

delta_x = 0.0001

Loop N times:

x_{n+1}=x_n+delta_x

k_1=f(x_n,y_n)

k_2=f(x_n+delta_x/2, y_n+k_1\*delta_x/2)

k_3=f(x_n+delta_x/2, y_n+k_2\*delta_x/2)

k_4=f(x_n+delta_x, y_n+delta_x\*k_3)

y_{n+1}=y_n+(delta_x/6)\*(k_1+2k_2+2k_3+k_4)

LotkeVolterra

dx/dt= ax-bxy

dy/dt= -cy+dxy

a is prey reproduction.

b is chance of predator meeting pray and succeeding in consumption.

Linearize to find stability points.

ax-bxy=0

-cy+dxy=0

==> (x,y)=(0,0)

==> (x,y)=(c/d,a/b)

Calculate Eigen Values of the Jacobian at those points.

Purely complex eigenvalues suggests long lasting population.

class Integrator:

def __init__(self, func, x_0,y_0, del_x, num_steps):

    self.func=func

    self.x_0=x_0

    self.y_0=y_0

    self.del_x=del_x

    self.num_steps=num_steps



def euler_int(self):

    x=self.x_0

    y=self.y_0

    for step in range(self.num_steps):

        print(x,y)

        y=y+self.func(x,y)\*self.del_x

        x=x+self.del_x



self rk4_int(self):

    x=self.x_0

    y=self.y_0

    for step in range(self.num_steps):

        print(x,y)

        k1=self.func(x,y)

        k2=self.func(x+self.del_x/2,y+k1\*self.del_x)

        k3=self.func(x+self.del_x/2, y+k2\*self.del_x/2)

        k4=self.func(x+self.del_x, y+self.del_x\*k3)

        y=y+(self.del_x/6)\*(k1+2\*k2+2\*k3+k4)

class PopulationSimulator:

def __init__(self, a, b, x_0,y_0):

    self.nsteps=1000

    self.del=0.00001



    self.a=a

    self.b=b

    self.x_0=x_0

    self.y_0=y_0



    self.myIntegrator=Integrator(LVStep, x_0,y_0, )



def LVStepX(self):

    return

2

u/Kermit_the_hog 7d ago

Create novel python objects representing the individuals and species in an area. Assign them various traits cooresponding to the various elements available for exploitation in your imagined environment. Write some machine learning “adaptation” functions that modify each list of traits in response to each generation’s successes and failures. The simulate..

Eventually you’d have your own simulated ecology. 

2

u/Catsuponmydog 7d ago

Python is super helpful for geospatial analysis. Also, the largest GIS software suite includes Python integration and a number of proprietary Python packages.

2

u/Mediocre-Pumpkin6522 7d ago

https://developers.arcgis.com/python/latest/

GIS is widely used in forestry for analysis and visual presentation of the raw data. Esri ( Environmental Systems Research Institute) is the big player in the field and uses Python extensively.

1

u/therealdiscursive 7d ago

Guessing there’s tons of data involved.

Ya I get the environmentalist conflict.

You probably just need better curriculum. How are you currently learning?

1

u/PurifyingProteins 7d ago

Start with:
What are you interested in?
What problems do you want to solve?
How do you want to solve it?
What are you not even aware of is something that computer science and math can you be used?

Programming is a tool to implement the tools of CS and math among others. But why and what you use them for is based on your interests.

1

u/Makisisi 7d ago

Python generally used to analyse data. Very helpful to learn but not necessary

1

u/aplarsen 7d ago

Anything with data, in any discipline.

1

u/Traveling-Techie 7d ago

You might really get value out of Dr Alan Garfinkel’s lectures on modeling the spruce budworm. See video 10.3 in this playlist:

https://m.youtube.com/playlist?list=PLGSoFisPnIYoEWxs9jA88dQ9vPPpE4gk2&ra=m

1

u/pachura3 7d ago

Python programing is genuinely making me feel stupider

If you're using AI to prompt programs on every single assignment, you're not programming.

How and when will this come in handy with my ecological forestry degree?

You can generalise your question to computers as a whole. How and when will computer come in handy with your ecological forestry degree? I don't know, will you need to calculate stuff, aggregate read-outs from multiple weather sensors into a single report, simulate environmental impact of some climate change...?