-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2024-08-30 17:12:40.941245 new snippets
- Loading branch information
1 parent
6507d4a
commit be37b8c
Showing
12 changed files
with
888 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
#date: 2024-08-30T16:36:13Z | ||
#url: https://api.github.com/gists/6832dd94f8bb34a1a6a5a20de6af6132 | ||
#owner: https://api.github.com/users/samwho | ||
|
||
import random | ||
import string | ||
import time | ||
from typing import Tuple | ||
|
||
import psycopg2 | ||
from psycopg2.extensions import cursor | ||
from rich.console import Console | ||
from rich.table import Table | ||
|
||
LARGE_STRING = "a" * 64 * 1024 | ||
|
||
|
||
def random_string(length: int = 10) -> str: | ||
return "".join(random.choices(string.ascii_lowercase, k=length)) | ||
|
||
|
||
def create_tables(cur: cursor) -> None: | ||
cur.execute("""CREATE TABLE IF NOT EXISTS int_table | ||
(id INTEGER PRIMARY KEY, value TEXT)""") | ||
cur.execute("""CREATE TABLE IF NOT EXISTS string_table | ||
(id TEXT PRIMARY KEY, value TEXT)""") | ||
|
||
|
||
def truncate_table(cur: cursor, table_name: str) -> None: | ||
cur.execute(f"TRUNCATE TABLE {table_name}") | ||
cur.connection.commit() | ||
|
||
|
||
def insert_data( | ||
cur: cursor, table_name: str, data: list[Tuple[int | str, str]] | ||
) -> float: | ||
total = 0 | ||
truncate_table(cur, table_name) | ||
for record in data: | ||
start_time = time.perf_counter() | ||
cur.execute(f"INSERT INTO {table_name} (id, value) VALUES (%s, %s)", record) | ||
cur.connection.commit() | ||
end_time = time.perf_counter() | ||
total += end_time - start_time | ||
return total | ||
|
||
|
||
def read_data(cur: cursor, table_name: str, ids: list[int | str]) -> float: | ||
total = 0 | ||
for id in ids: | ||
start_time = time.perf_counter() | ||
cur.execute(f"SELECT * FROM {table_name} WHERE id = %s", (id,)) | ||
cur.fetchone() | ||
end_time = time.perf_counter() | ||
total += end_time - start_time | ||
return total | ||
|
||
|
||
def benchmark(num_records: int = 10000, num_reads: int = 1000) -> dict[str, float]: | ||
# PostgreSQL connection parameters | ||
conn_params = { | ||
"dbname": "postgres", | ||
"user": "postgres", | ||
"password": "**********" | ||
"host": "localhost", | ||
"port": "5432", | ||
} | ||
|
||
conn = psycopg2.connect(**conn_params) | ||
cur = conn.cursor() | ||
|
||
# Ensure tables don't exist | ||
cur.execute("DROP TABLE IF EXISTS int_table") | ||
cur.execute("DROP TABLE IF EXISTS string_table") | ||
conn.commit() | ||
|
||
create_tables(cur) | ||
conn.commit() | ||
|
||
ints = list(range(num_records)) | ||
random_ints = ints | ||
random.shuffle(random_ints) | ||
|
||
# Prepare data | ||
int_seq_data = [(i, LARGE_STRING) for i in ints] | ||
int_random_data = [(i, LARGE_STRING) for i in random_ints] | ||
str_seq_data = [(f"{i:010d}", LARGE_STRING) for i in ints] | ||
str_random_data = [(random_string(), LARGE_STRING) for i in ints] | ||
|
||
# Benchmark insertions | ||
int_seq_insert = insert_data(cur, "int_table", int_seq_data) | ||
int_random_insert = insert_data(cur, "int_table", int_random_data) | ||
str_seq_insert = insert_data(cur, "string_table", str_seq_data) | ||
str_random_insert = insert_data(cur, "string_table", str_random_data) | ||
|
||
# Prepare read data | ||
int_seq_ids = [i for i, _ in int_seq_data[:num_reads]] | ||
int_random_ids = [i for i, _ in int_random_data[:num_reads]] | ||
str_seq_ids = [i for i, _ in str_seq_data[:num_reads]] | ||
str_random_ids = [i for i, _ in str_random_data[:num_reads]] | ||
|
||
# Benchmark reads | ||
int_seq_read = read_data(cur, "int_table", int_seq_ids) | ||
int_random_read = read_data(cur, "int_table", int_random_ids) | ||
str_seq_read = read_data(cur, "string_table", str_seq_ids) | ||
str_random_read = read_data(cur, "string_table", str_random_ids) | ||
|
||
cur.close() | ||
conn.close() | ||
|
||
return { | ||
("int", "sequential", "insert"): int_seq_insert, | ||
("int", "random", "insert"): int_random_insert, | ||
("str", "sequential", "insert"): str_seq_insert, | ||
("str", "random", "insert"): str_random_insert, | ||
("int", "sequential", "read"): int_seq_read, | ||
("int", "random", "read"): int_random_read, | ||
("str", "sequential", "read"): str_seq_read, | ||
("str", "random", "read"): str_random_read, | ||
} | ||
|
||
|
||
if __name__ == "__main__": | ||
n = 10000 | ||
results = benchmark(num_records=n, num_reads=n) | ||
|
||
table = Table() | ||
|
||
table.add_column("Type", style="cyan", no_wrap=True) | ||
table.add_column("Mode", style="cyan", no_wrap=True) | ||
table.add_column("Operation", style="cyan", no_wrap=True) | ||
table.add_column("Time (seconds)", style="magenta") | ||
|
||
for (type, mode, op), time in results.items(): | ||
table.add_row(type, mode, op, f"{time:.3f}") | ||
|
||
console = Console() | ||
console.print(table) | ||
table) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#date: 2024-08-30T17:12:04Z | ||
#url: https://api.github.com/gists/22d5da6fb48207927a0dc4b8ec62df0d | ||
#owner: https://api.github.com/users/zsasko | ||
|
||
import json | ||
from typing import AsyncGenerator, NoReturn | ||
|
||
import uvicorn | ||
from dotenv import load_dotenv | ||
from fastapi import FastAPI, WebSocket | ||
from fastapi.responses import HTMLResponse | ||
from openai import AsyncOpenAI | ||
|
||
load_dotenv() | ||
|
||
model = "gpt-3.5-turbo" | ||
conversation_history = [] | ||
|
||
app = FastAPI() | ||
client = AsyncOpenAI() | ||
|
||
with open("index.html") as f: | ||
html = f.read() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#date: 2024-08-30T16:56:05Z | ||
#url: https://api.github.com/gists/7781ef8063e0102d7121d799efcb700f | ||
#owner: https://api.github.com/users/shahpnmlab | ||
|
||
import os | ||
import glob | ||
import pandas as pd | ||
import starfile | ||
|
||
# Function to read CSV files | ||
def read_csv_files(directory): | ||
all_data = [] | ||
for filename in glob.glob(os.path.join(directory, '*.csv')): | ||
df = pd.read_csv(filename, header=None, names=['X', 'Y', 'Z']) | ||
tomogram_name = os.path.basename(filename).split('_')[1] | ||
df['MicrographName'] = f'TS_{tomogram_name}' | ||
all_data.append(df) | ||
return pd.concat(all_data, ignore_index=True) | ||
|
||
# Read all CSV files | ||
data = read_csv_files('particle_lists') | ||
|
||
# Create the particles data block | ||
particles_data = pd.DataFrame({ | ||
'rlnMicrographName': data['MicrographName'], | ||
'rlnCoordinateX': data['X'], | ||
'rlnCoordinateY': data['Y'], | ||
'rlnCoordinateZ': data['Z'], | ||
'rlnOriginXAngst': [0] * len(data), | ||
'rlnOriginYAngst': [0] * len(data), | ||
'rlnOriginZAngst': [0] * len(data) | ||
}) | ||
|
||
# Create the optics data block | ||
optics_data = pd.DataFrame({ | ||
'rlnOpticsGroup': [1,""], | ||
'rlnOpticsGroupName': ['opticsGroup1',""], | ||
'rlnSphericalAberration': [2.700000,""], | ||
'rlnVoltage': [300.000000,""], | ||
'rlnImagePixelSize': [13.48,""], | ||
'rlnImageSize': [64,""], | ||
'rlnImageDimensionality': [3,""], | ||
'rlnPickingImagePixelSize': [13.48,""] | ||
}) | ||
|
||
# Create the STAR file structure | ||
star_data = { | ||
'optics': optics_data, | ||
'particles': particles_data | ||
} | ||
|
||
# Write the STAR file | ||
starfile.write(star_data, 'particles.star', overwrite=True) | ||
|
||
print("particles.star file has been created successfully.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#date: 2024-08-30T17:10:15Z | ||
#url: https://api.github.com/gists/f1825a7e2c54c2c6e46095f95c697285 | ||
#owner: https://api.github.com/users/genzj | ||
|
||
#!/bin/bash | ||
|
||
# ref: | ||
# https://askubuntu.com/a/1167767 | ||
# https://manpages.ubuntu.com/manpages/bionic/man5/NetworkManager.conf.5.html#connectivity%20section | ||
|
||
sudo cp --backup=t /etc/NetworkManager/NetworkManager.conf /etc/NetworkManager/NetworkManager.conf.backup | ||
echo -e "\n[connectivity]\nuri=\n" | sudo tee -a /etc/NetworkManager/NetworkManager.conf | ||
sudo systemctl restart NetworkManager.service |
Oops, something went wrong.