Skip to content

Instantly share code, notes, and snippets.

View fa-ahmad's full-sized avatar
👨‍💻

Farhan Ahmad fa-ahmad

👨‍💻
View GitHub Profile
@fa-ahmad
fa-ahmad / primary-key-postgres.md
Created February 11, 2022 12:38
Test if a table has a primary key in Postgres

This returns the names and data types of all columns of the primary key for the tablename table:

FROM   pg_index i
JOIN   pg_attribute a ON a.attrelid = i.indrelid
                     AND a.attnum = ANY(i.indkey)
WHERE  i.indrelid = 'tablename'::regclass
AND    i.indisprimary;
@fa-ahmad
fa-ahmad / ipython-sql.md
Last active February 11, 2022 10:25
Capture output of SQL command with Ipython/Jupyter Notebook

The indirection operator captures the output of any SQL query into a Python variable. In the example below the variable output will contain the result of the query. The query used here is irrelevant and only used for illustrating the usage.

%%sql output << 
SELECT a.attname, format_type(a.atttypid, a.atttypmod) AS data_type, a.attnotnull, i.indisprimary
FROM   pg_index i
JOIN   pg_attribute a ON a.attrelid = i.indrelid
 AND a.attnum = ANY(i.indkey)
@fa-ahmad
fa-ahmad / auto-nbconvert.md
Last active January 19, 2022 10:26
Trigger Nbconvert on Jupyter Notebook

Use the entr command to watch the Notebook for changes.

ls myfile.ipynb| entr -r jupyter nbconvert myfile.ipynb --to slides --post serve --ServePostProcessor.open_in_browser=False

The URL of the slides gets printed in the terminal:

Serving your slides at http://127.0.0.1:8000/myfile.slides.html

Sources:

class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
self.conv1 = Conv2D(32, 3, activation='relu')
self.flatten = Flatten()
self.d1 = Dense(128, activation='relu')
self.d2 = Dense(10)
def call(self, x):
x = self.conv1(x)
@fa-ahmad
fa-ahmad / pomegranate_example.py
Created October 11, 2019 08:20
Pomegranante library sample HMM code.
from pomegranate import *
d1 = DiscreteDistribution({'A' : 0.35, 'C' : 0.20, 'G' : 0.05, 'T' : 0.40})
d2 = DiscreteDistribution({'A' : 0.25, 'C' : 0.25, 'G' : 0.25, 'T' : 0.25})
d3 = DiscreteDistribution({'A' : 0.10, 'C' : 0.40, 'G' : 0.40, 'T' : 0.10})
s1 = State(d1, name="s1")
s2 = State(d2, name="s2")
s3 = State(d3, name="s3")
model = HiddenMarkovModel('example')
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# 1 input image channel, 6 output channels, 3x3 square convolution
@fa-ahmad
fa-ahmad / crash.py
Created June 6, 2019 04:47
Stack Trace
TypeError Traceback (most recent call last)
<ipython-input-44-a553646a889c> in <module>
22 for i, samples in enumerate([samples_1, samples_10, samples_100]):
23 results[clf_name][i] = \
---> 24 train_predict(clf, samples, X_train, y_train, X_test, y_test)
25
26 # Run metrics visualization for the three supervised learning models chosen
<ipython-input-36-2fd67492e721> in train_predict(learner, sample_size, X_train, y_train, X_test, y_test)
19 # TODO: Fit the learner to the training data using slicing with ‘sample_size’ using .fit(training_features[:], training_labels[:])