Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Sunday, May 21, 2023

Python Basics with ChatGPT

 

### Summary of the Python Coding Session


1. **Introduction to Recording Setup**:

   - Clarified the recording process and privacy measures.

   - Example given on Python syntax and explaining key elements like lists and functions.


2. **Basic Python Examples**:

   - Demonstrated a simple Python script to calculate the average of a list of numbers.

   - Discussed the importance of syntax elements such as square brackets for lists and the sum function.


3. **Explaining Code in Detail**:

   - Importance of indentation in Python was highlighted.

   - Function definition and arithmetic operations were explained.

   - Addressed the need for better explanations of commas and other syntax elements.


4. **Loop and Indentation**:

   - Provided an example of a loop and discussed the concept of indentation in Python.

   - Introduced the concept of defining functions and their use.


5. **Object-Oriented Programming (OOP)**:

   - Explained OOP concepts using classes like Rectangle, Square, and Circle.

   - Demonstrated inheritance and method overriding with practical examples.


6. **Coding Exercise with Google Colab**:

   - Encouraged using Google Colab for running Python code.

   - Showcased copying code from ChatGPT into Colab and executing it.

   - Addressed potential logical errors in code and the importance of verifying outputs.


7. **Interactive Q&A with ChatGPT**:

   - Examples included sorting algorithms (bubble sort, selection sort, insertion sort).

   - Discussed the importance of comments for code explanation.

   - Covered variable data types and list operations in Python.


8. **Weather and Air Quality Example**:

   - Attempted to use an API to get weather and air quality data.

   - Faced issues with API keys and registration requirements.

   - Showcased alternative methods like web scraping using BeautifulSoup.


9. **Challenges with Drawing and Visualization**:

   - Asked ChatGPT to draw a bunny using Python, encountered issues with libraries and runtime errors.

   - Highlighted the iterative process of debugging and refining code.


10. **Reflections and Future Directions**:

    - Discussed the implications of using AI tools for learning and completing homework assignments.

    - Emphasized the importance of creativity and critical thinking in coding.


11. **Wrap-up and Next Steps**:

    - Encouraged participants to explore basic Python concepts using provided GitHub resources.

    - Suggested taking a break and reconvening to continue the session.


### Suggested Next Steps


**a.** If you need a deeper dive into any specific concept covered during the session, let me know, and I can provide more detailed explanations and examples.


**b.** If you have specific code you want to run or debug, share it here, and I'll help you refine and troubleshoot it.


What would you like to explore or discuss next?

Wednesday, June 22, 2022

numpy reshape(-1, 1)

 -1 means rows are unknown. 

1 means column is 1. 

https://stackoverflow.com/questions/18691084/what-does-1-mean-in-numpy-reshape


Monday, April 18, 2022

parse yeast PPI from biogrid 4.4.208

code at  https://github.com/QinLab/Biogrid-Qin2022 


download BIOGRID-ALL-4.4.208.tab3.zip 

write a python code to parse out yeast entries

myfile ='data-large-unsynced/BIOGRID-ALL-4.4.208.tab3.txt'
df = pd.read_csv(myfile,sep='\t', header=(0))
df = df[df['Organism Name Interactor A'].str.contains('Saccharomyces cerevisiae') ]
df = df[df['Organism Name Interactor B'].str.contains('Saccharomyces cerevisiae') ]

Remove duplicated  interactions

def alphabetic_ordered_tag(in_tag1, in_tag2):
    tmp = [str(in_tag1), str(in_tag2)]
    tmp.sort()
    return( str(in_tag1) + "_" + str(in_tag2))
df['alphabetic_ordered_tag'] = df.apply(lambda x: alphabetic_ordered_tag(x['Systematic Name Interactor A'], x['Systematic Name Interactor B']), axis=1)
df2 = df.drop_duplicates(subset=['alphabetic_ordered_tag'])

Output a lean version form small file size

df3 = df2[['Systematic Name Interactor A', 'Systematic Name Interactor B', 'Official Symbol Interactor A', 'Official Symbol Interactor B', 'alphabetic_ordered_tag' ]]
df3.to_csv("biogrid_s288c_4.4.208.lean.csv")

Output a dictionary from systematic names to symbols. 

dicA = df3[['Systematic Name Interactor A', 'Official Symbol Interactor A']]
dicB = df3[['Systematic Name Interactor B', 'Official Symbol Interactor B']]
dicA.columns = ['Name', 'Symbol']
dicB.columns = ['Name', 'Symbol']
dic = pd.concat([dicA, dicB])
dic2 = dic.drop_duplicates(subset=['Name', 'Symbol'])
dic2.to_csv("Sce_Name2Symbol.csv")

A total 627732 interactions and 6155 unique names/symbols were found for s288c biogrid data set. 

Note: Self-interactions were included. 









Saturday, September 11, 2021

Tuesday, January 12, 2021

try to download PDF from google drive (unsuccessful attemp)

 

import sys

sys.path.append("/opt/anaconda3/lib/python3.7/site-packages")


import requests

from bs4 import BeautifulSoup as soup

from urllib import parse

import os

import re

import pandas as pd


tb = pd.read_excel('Submission of Report _ Lesson plan, online-R-coding bootcamp Dec 2020 (Responses).xlsx')

doc_urls = tb['Please upload your report (for students) or lesson plans (for teachers) in PDF format']

type( doc_urls )
doc_urls[1]
content = requests.get(doc_urls[1] )
with open(("test.pdf"), 'wb') as pdf:
    pdf.write(content.content)
dir(content)

#The generated pdf cannot be open. It is an html file. 


Saturday, December 19, 2020

Ayalew SIR model script

 

https://www.glowscript.org/#/user/mayalew/folder/MyPrograms/program/EpiModeling/edit



Saturday, July 11, 2020

python and ipython has different path, modules not found



(base) CS313BQin:~ hqin$ python
Python 3.7.4 (default, Aug 13 2019, 15:17:50) 
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/opt/anaconda3/lib/python37.zip', '/opt/anaconda3/lib/python3.7', '/opt/anaconda3/lib/python3.7/lib-dynload', '/opt/anaconda3/lib/python3.7/site-packages', '/opt/anaconda3/lib/python3.7/site-packages/aeosa']









import sys

sys.path.append("/opt/anaconda3/lib/python3.7/site-packages")

This solved the seaborn module loading problem in jupyter-notebook for big macbookpro

However, there is still a loading problem for libriso

Reference:
https://stackoverflow.com/questions/15514593/importerror-no-module-named-when-trying-to-run-python-script/15622021#15622021


Wednesday, June 10, 2020

pandas dataframe chunk


for large pandas dataframe, after slicing, it keeps its original indices. So, an iterator has to be used.

#metaTb = pd.read_csv("metaGecode_2020-6-6.csv")
metaTb = pd.read_csv(infilename)
metaTb = metaTb[ mystart : myend]

for i, row in metaTb.iterrows():


Sunday, June 7, 2020

ecCodes install; pygrib

Ref:
https://github.com/ecmwf/eccodes-python#system-dependencies

==== ecCodes
On large macpro laptop

#homebrew works
brew install eccodes

(base) CS313BQin:build hqin$  python -m eccodes selfcheck
Found: ecCodes v2.17.0.
Your system is ready.

pip, pip3, and conda did not work

=====pygrib

#conda install -c conda-forge pygrib

download github sources

Copy setup.cfg.template to setup.cfg #did not make anychanges
(base) CS313BQin:pygrib-master hqin$ python setup.py build

  • Run 'python setup.py build' #passed
  • Run 'sudo python setup.py install' #passed
  • Run 'python test.py' to test your pygrib installation. #passed

Monday, March 2, 2020

update Anaconda


download package, but it failed to install.

try this, worked;

cond update --all 

Sunday, February 24, 2019

python deep learning notes




========
stochastic gradient descent is so called because only batch of data are used each time.
========gradient descent
# Set the learning rate: learning_rate
learning_rate = 0.01

# Calculate the predictions: preds
preds = (weights * input_data).sum()

# Calculate the error: error
error = preds - target

# Calculate the slope: slope
slope = 2 * input_data * error

# Update the weights: weights_updated
weights_updated = weights - learning_rate * slope

# Get updated predictions: preds_updated
preds_updated = (weights_updated * input_data).sum()

# Calculate updated error: error_updated
error_updated = preds_updated - target

# Print the original error
print(error)

# Print the updated error
print(error_updated)

========

# Import necessary modules
import keras
from keras.layers import Dense
from keras.models import Sequential

# Specify the model
n_cols = predictors.shape[1]
model = Sequential()
model.add(Dense(50, activation='relu', input_shape = (n_cols,)))
model.add(Dense(32, activation='relu'))
model.add(Dense(1))


# Compile the model
model.compile(optimizer='adam', loss='mean_squared_error')

# Verify that model contains information from compiling
print("Loss function: " + model.loss)

# Fit the model
model.fit(predictors, target)


========================
# Import necessary modules
import keras
from keras.layers import Dense
from keras.models import Sequential
from keras.utils import to_categorical

# Convert the target to categorical: target
target = to_categorical(df.survived)

# Set up the model
model = Sequential()

# Add the first layer
model.add(Dense(32, activation='relu', input_shape=(n_cols,)))

# Add the output layer
model.add(Dense(2, activation='softmax'))

# Compile the model
model.compile(optimizer='sgd',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# Fit the model
model.fit(predictors, target)

====
dead neuron versus vanishing gradient

====