r/PythonLearning • • 2d ago

Help Request Beginner-friendly Python resources for someone transitioning from Psychology to Computational Neuroscience?

Hi everyone!

I'm a Psychology graduate currently considering pursuing a Master's degree in Computational Neuroscience. Coming from a non-computer science background, I realize that I need to build a solid foundation in programming, particularly Python, before diving deeper into the field.

I've been trying to find introductory resources and came across Neuromatch Academy's Computational Neuroscience course, including their introductory materials.

However, I've found that even though the course is intended for beginners, I still struggle quite a bit with the Python explanations and code structure. I often find myself having difficulty understanding what's happening in the code itself, rather than focusing on the computational neuroscience concepts being taught.

I'm wondering if there are other resources that might be more suitable for someone in my position.

Ideally, I'm looking for resources that:

1.Start from the absolute basics, assuming little to no prior programming experience.

2.Explain programming concepts and code structure slowly and intuitively, rather than jumping straight into complex examples.

3.Gradually introduce concepts that are relevant to computational neuroscience (e.g., NumPy, SciPy, data analysis, mathematical modeling, simulations, etc.).

Help me understand not just how to write code, but also why certain approaches are used.

I'm open to pretty much any format: YouTube tutorials, online courses, textbooks, interactive platforms, or even a recommended learning sequence.

For additional context, my academic background is in Psychology, and I have very little programming experience. I'm not necessarily looking to jump straight into computational neuroscience yet; I'd rather develop a comfortable understanding of Python first, while keeping my long-term goal in mind.

For those who came from a non-CS background, how did you approach learning programming before getting into computational neuroscience? Are there any resources or learning paths you'd particularly recommend?

I'd really appreciate any suggestions. Thanks!

14 Upvotes

12 comments sorted by

4

u/Naive_Programmer_232 2d ago edited 2d ago

i'd probably start off with some basics of core python then go through the numpy / pandas / polars / matplotlib / seaborn landscape more so. these libraries tend to be integrated into other ones in the data analysis / data science space. learning them can give a good foundation of the objects / data structures you'll come across frequently.

while learning core python can be really helpful, you will definitely use it in the next steps to glue everything together, you don't want to go too deep at least initially. you'll see in these extended libraries there is a lot of syntax/semantics magic that extends regular python; that require more work in the core language to do. in addition because those libraries are built for data-heavy tasks more so, they are much more efficient than what python offers out of the box, they're very R-like.

ex core (matrix multiplication + solving matrix equation for q with regular python + standard library) :

  =========CORE PYTHON+STD-LIB=================

  # MULTIPLYING TWO NxN SQUARE MATRICES (regular python)

  import random 

  def make_square_matrix(nums: list[int], N: int) -> list[list[int]]:
      matrix=[]
      for i,n in enumerate(nums):
          if i%N==0:
             matrix.append([])
          matrix[-1].append(n)
      return matrix

  # get 9 random numbers (sampling w/o replacement) -> make two 3x3 matrices
  matrix1=make_square_matrix(random.sample(range(1,13),k=9),N=3)
  matrix2=make_square_matrix(random.sample(range(1,13),k=9),N=3)
 
  # show matrices
  print("matrix 1", *matrix1, sep="\n")
  matrix 1
  [10, 5, 8]
  [6, 3, 1]
  [11, 2, 4]
  print("matrix 2", *matrix2, sep="\n")
  matrix 2
  [5, 2, 10]
  [3, 12, 7]
  [6, 8, 4]

  # multiply matrices element-wise into product matrix (list comprehension)
  prod_matrix=make_square_matrix([
         matrix1[r][c]*matrix2[r][c]
         for r in range(3)
         for c in range(3)
  ], N=3)

  # show product matrix
  print("element-wise product matrix", *prod_matrix, sep="\n")
  element-wise product matrix
  [50, 10, 80]
  [18, 36, 7]
  [66, 16, 16]

  # multiply matrices linalg dot product into product matrix (regular python)

  def dot_product_sqr_matrices(M1: list[list[int]], 
                             M2: list[list[int]],
                            ) -> list[list[int]]:
      
       # transpose of 2nd matrix
       T_M2=list(zip(*M2))

       # N_ ~ NxN dimension
       N_=len(M2)
       
       # find dot product linear alg matrix multiplication 
       return make_square_matrix([
             sum(M1[r][c]*T_M2[k][c] for c in range(N_))
             for r in range(N_)
             for k in range(N_)
        ],N=N_)



  
  # show product matrix
  prod_matrix=dot_product_sqr_matrices(matrix1,matrix2)
  print("dot linalg product matrix", *prod_matrix, sep="\n")
  dot linalg product matrix
  [113,144,167]
  [45,56,85]
  [85,78,140]

  # suppose prod_matrix is A in the equation Aq=b
  # we want to solve for column vector q [x,y,z]
  # where b=[20,30,40]

  # to solve for q [x,y,z] we need to:
  # 1. augment A|b 
  # 2. gaussian elimination
  #      2a. forward elimination
  #      2b. back substitution
  
  def augment(A: list[list[int]], b: list[int]) -> list[list[int]]:
      return [
          A[i][:]+[b[i]]
          for i in range(len(A))
      ]

  def forward_elimination(M: list[list[int]]) -> None:
      n=len(M)
      for i in range(n):

          # get pivot row
          pivot=max(range(i,n),key=lambda r: abs(M[r][i]))

          # swap rows 
          M[i],M[pivot]=M[pivot],M[i]
          
          # eliminate entries below pivot
          for j in range(i+1,n):
              factor=M[j][i]/M[i][i]
              for k in range(i,n+1):
                  M[j][k]-=factor*M[i][k]

  def backsub(M: list[list[int|float]]) -> list[float]:
      n=len(M)
      q=[0.0]*n
      for i in range(n-1,-1,-1):
          q[i]=(
             M[i][n]-sum(
                M[i][j]*q[j]
                for j in range(i+1,n)
             )) / M[i][i]
      return q

  # Aq=b
  # A=[113,144,167]
  #   [45, 56, 85]
  #   [85, 78, 140]
  # b=[20,30,40]
  
  import copy

  # creating copy of prod_matrix (all nested levels) to A 
  A=copy.deepcopy(prod_matrix)
  b=[20,30,40]

  # 1. M=A|b
  M=augment(A,b)
  # gaussian 
  # 2a. forward eliminate 
  forward_elimination(M)
  # 2b. back substitution 
  q=backsub(M)
  
  # show system of equations solution for q 
  print("Aq=b",
        "q=",
        *[f"{k}: {v}" for k,v in zip("xyz",q)], 
        sep="\n"
  )
  Aq=b
  q =
  x: -1.2144491663942474
  y: -0.2672441974501466
  z: 1.1719516181758747

  def dot_product_matrix_vector(A: list[list[int]],
                                q: list[float],
                                ) -> list[int|float]:
      return [
         sum(A[r][c]*q[c] for c in range(len(q)))
         for r in range(len(A))
      ]


  # verify Aq=b
  print(dot_product_matrix_vector(A,q))
  [20.0, 30.0, 40.0]

ex (matrix multiplication + solving matrix equation with numpy and scipy):

  #===========Numpy+Scipy================

  # MULTIPLYING TWO NxN SQUARE MATRICES (numpy)

  import numpy as np 

  # get 9 random numbers (sampling w/o replacement) -> make two 3x3 matrices
  matrix1=np.random.choice(range(1,101),size=9,replace=False).reshape((3,3))
  matrix2=np.random.choice(range(1,101),size=9,replace=False).reshape((3,3))
  
  # show matrices
  print("matrix 1", matrix1, sep="\n")
  matrix 1
  [[10  8 12]
  [ 2  3  6]
  [ 1  5 11]]
  print("matrix 2", matrix2, sep="\n")
  matrix 2
  [[ 1  6 12]
   [ 7  9  5]
   [ 8 10  4]]

  # element-wise multiplication -> product matrix (vectorized element-wise)
  prod_matrix=matrix1*matrix2

  # show product matrix
  print("element-wise product matrix", prod_matrix, sep="\n")
  element-wise product matrix
  [[ 10  48 144]
  [ 14  27  30]
  [  8  50  44]]

  # dot product linalg multiplication -> product matrix (numpy way)
  prod_matrix=matrix1@matrix2
  
  # show product matrix
  print("dot linalg product matrix", prod_matrix, sep="\n")
  dot linalg product matrix
  [[162 252 208]
  [ 71  99  63]
  [124 161  81]]


  # suppose prod_matrix is A in the equation Aq=b
  # we want to solve for column vector q [x,y,z]
  # where b=[20,30,40]

  # deep-copy feature builtin to numpy
  A=prod_matrix.copy()
  b=np.array([20,30,40])

  # using solver for q in Aq=b from scipy
  from scipy.linalg import solve

  # show system of equations solution for q 
  q=solve(A,b)
  print("q=",
        *[f"{k}: {v}" for k,v in zip("xyz",q)],
        sep="\n",
  )
  q=
   x: -338.5714285713362
   y: 328.5714285713391
   z: -134.28571428567778
  
  # verify Aq=b 
  print(A@q)
  [20. 30. 40.]

As you can probably tell above haha, without scipy or numpy in the mix, you have to define quite a bit in order to do the same with regular python, lots of looping, using standard library features. So that in some sense, is what I was mentioning prior when it comes to the 'syntax/semantic magic' these libraries provide, same is true with pandas and others in the same realm. They all work with each other and they do more than python out of the box, more efficient (vectorization R-like operations) and better for handling bigger / more complex data.

For the basics mention with python, I'd go:

  1. variables / data types

  2. conditionals (if/elif/else)

  3. builtin data structures (str/tuple vs list/set/dict)

  4. loops (for loops / while loops)

  5. comprehensions (list/dict comprehension vs generator expressions)

  6. pip/imports & working with libraries (start with standard library)

  7. file IO (read/write/append .txt and .csv + path libraries)

  8. functions (regular using 'def' as well as lambda functions)

  9. type handling & verification (try/except/else/finally + verification)

  10. basic OOP: classes/methods/attributes -> single inheritance + encapsulation

That should be enough to give you a decent foundation to jump into the data-world libraries. You don't have to do all of the above, this is just my suggestion, you could get away with less as well though.

Anyway, good luck with everything. Hopefully this helps!

4

u/stepback269 2d ago

"3.Gradually introduce concepts that are relevant to computational neuroscience (e.g., NumPy, SciPy, data analysis, mathematical modeling, simulations, etc.)."

Perhaps you are going too fast, too far at this stage?

As a beginner, you need to consider yourself a baby and learn to crawl (learn the "fundamentals") before you can stand up and walk or sprint. So search for the Python "fundamentals" on Google and/or YouTube.

As for a roadmap tailored to your goals, that's going to be difficult. Python is a vast and expansive universe. There is never going to be a "one" soulmate tutorial that works "best" for you and with regard to every aspect of Python. With each step up that learning curve, you're going to have to shop around until you find a teacher/resource that "clicks and sticks" especially for you.

With that said:

(1) There are tons and tons of tutorial materials out there on the net including many good YouTube ones that are free. You should shop around rather than putting all your eggs in one basket.

(2) As a relative noob myself, I've been logging my personal learning journey and adding to it on an almost-daily basis at a blog page called "Links for Python Noobs" (--HERE--) Any of the top listed ones on that page should be good for you. And there are many add-ons at the tail end of the page. Personally, I cut my first Python teeth with Nana's Zero to Hero (==HERE==). Since then, I've moved on to watching short lessons with Indently and Tech with Tim. You should sample at least a few until you find a lecturer that suits your style.

(3) The main piece of advice is the 80/20 rule. Spend 80% of your time writing your own code (using your own fingers and your own creativity) as opposed to copying recipes and only 20% watching the lectures. Good luck.

3

u/nullptr_0x 2d ago

Seems like you're probably looking for a data science in Python course.

I've reviewed those courses before. My wife took one and I often helped her. I have generally observed that they sort of slack on the actual programming side. (No surprise they're basically only trying to teach you enough programming to perform particular functions and tasks that a researcher needs to do.)

If you're wanting a good introduction to programming in Python, I have a free course I made for some friends.

https://understandit.dev Programming 101 Python

2

u/Fabulous_Panic_7263 2d ago

I'd say just start with the basics and try building small projects along the way

2

u/seanv507 2d ago

Op, can you paste an example of neuromatches introductory materials to give an example of what you are struggling with?

One option might be using an ai to explain the introductory materials.. ie to use it as a teaching assistant, whenever you are stuck. I suspect that will actually be the most effective way of getting answers to the specific questions you have.

2

u/Arjun68 2d ago

Hey, what do you aim in computational neuroscience

2

u/Personal-March-4340 2d ago

W3schools is free. I used their tutorial in my Java class. I imagine python is about the same. Your list of things to learn sounds pretty ambitious. I wonder if you realize the amount of work you are biting off with your agenda.