Skip to content

Commit

Permalink
2024-08-30 17:12:40.941245 new snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardocerqueira committed Aug 30, 2024
1 parent 6507d4a commit be37b8c
Show file tree
Hide file tree
Showing 12 changed files with 888 additions and 196 deletions.
26 changes: 26 additions & 0 deletions seeker/report.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
--------------------------------------------------------------------------------
2024-08-30 17:12:40.941245
--------------------------------------------------------------------------------
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: snippet/recept_analysis.py

Untracked files:
(use "git add <file>..." to include in what will be committed)
snippet/bench.py
snippet/collect_data_openai_sample_imports.py
snippet/csv2star.py
snippet/disable.bash
snippet/docker_iptables
snippet/forty.java
snippet/mat_demo_v1.py
snippet/radar_chart_example.py
snippet/test.py
snippet/tts_backup.py

no changes added to commit (use "git add" and/or "git commit -a")

--------------------------------------------------------------------------------
2024-08-29 17:11:54.148933
--------------------------------------------------------------------------------
Expand Down
139 changes: 139 additions & 0 deletions seeker/snippet/bench.py
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)
23 changes: 23 additions & 0 deletions seeker/snippet/collect_data_openai_sample_imports.py
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()
55 changes: 55 additions & 0 deletions seeker/snippet/csv2star.py
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.")
13 changes: 13 additions & 0 deletions seeker/snippet/disable.bash
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
Loading

0 comments on commit be37b8c

Please sign in to comment.