-
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-09-10 17:12:04.049356 new snippets
- Loading branch information
1 parent
15d0e61
commit 31f6422
Showing
7 changed files
with
565 additions
and
0 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-09-10T16:45:57Z | ||
#url: https://api.github.com/gists/0150766320516a0cfcfda5d94c2d18fe | ||
#owner: https://api.github.com/users/MattieOF | ||
|
||
from telethon import * | ||
import asyncio | ||
import json | ||
import os | ||
import time | ||
import dataclasses | ||
import sys | ||
import datetime | ||
from dataclasses import dataclass | ||
|
||
# Thanks to https://stackoverflow.com/a/54769644 | ||
def dataclass_from_dict(klass, d): | ||
try: | ||
fieldtypes = {f.name:f.type for f in dataclasses.fields(klass)} | ||
return klass(**{f:dataclass_from_dict(fieldtypes[f],d[f]) for f in d}) | ||
except: | ||
return d # Not a dataclass field | ||
|
||
api_id = 69420 # get your own at: https://my.telegram.org/apps | ||
api_hash = "nuh uh, you're not getting this one :3" | ||
|
||
client = TelegramClient('CuteBot', api_id, api_hash) | ||
|
||
@dataclass | ||
class ScheduledMessage: | ||
username: str | ||
message: str | ||
interval: int | ||
last_sent: float | ||
|
||
scheduledMessages = [] | ||
if (os.path.exists("scheduledMessages.json")): | ||
with open("scheduledMessages.json", "r") as file: | ||
loadedMessages = json.load(file) | ||
scheduledMessages = [dataclass_from_dict(ScheduledMessage, msg) for msg in loadedMessages] | ||
|
||
def save_messages(): | ||
with open("scheduledMessages.json", "w") as file: | ||
scheduledMessagesAsDict = list(map(lambda msg: dataclasses.asdict(msg), scheduledMessages)) | ||
json.dump(scheduledMessagesAsDict, file) | ||
|
||
async def loop(): | ||
logOut = False | ||
|
||
async def check_messages(): | ||
didEdit = False | ||
for msg in scheduledMessages: | ||
if time.time() - msg.last_sent >= msg.interval: | ||
try: | ||
await client.send_message(msg.username, msg.message) | ||
msg.last_sent = time.time() | ||
print(f"Sent message to {msg.username}! Next message in {msg.interval} seconds (at {datetime.datetime.fromtimestamp(time.time() + msg.interval).strftime('%Y-%m-%d %H:%M:%S')})") | ||
didEdit = True | ||
except Exception as error: | ||
print(f"Failed to send message to {msg.username}! Due to {error}") | ||
if didEdit: | ||
save_messages() | ||
|
||
if "autorun" in sys.argv: | ||
print("Running!") | ||
while True: | ||
await check_messages() | ||
await asyncio.sleep(1) | ||
|
||
while True: | ||
cmd = input("Enter command: ") | ||
if cmd == "exit": | ||
break | ||
elif cmd == "logout": | ||
logOut = True | ||
break | ||
|
||
cmd = cmd.split(" ") | ||
if cmd[0] == "list": | ||
# List all scheduled messages | ||
# Format: list | ||
if len(scheduledMessages) == 0: | ||
print("No scheduled messages yet :( Use the 'add' command to add one!") | ||
continue | ||
|
||
for i, msg in enumerate(scheduledMessages): | ||
print(f"{i}: To {msg.username}, \"{msg.message}\" (every {msg.interval} seconds)") | ||
elif cmd[0] == "add": | ||
# Add a scheduled message | ||
# Format: add <username> <interval in seconds> <message> | ||
if len(cmd) < 4: | ||
print("Not enough parameters! Format: add <username> <interval in seconds> <message>") | ||
continue | ||
|
||
try: | ||
interval = float(cmd[2]) | ||
if interval < 1: | ||
raise ValueError | ||
except ValueError: | ||
print("Invalid interval! Must be an integer above 1.") | ||
continue | ||
|
||
try: | ||
await client.send_message(cmd[1], "new scheduled message added :3") | ||
except: | ||
print("Invalid username! Make sure you have the correct username and that you've sent a message to the user at least once.") | ||
continue | ||
|
||
scheduledMessages.append(ScheduledMessage(username=cmd[1], interval=interval, message=" ".join(cmd[3:]), last_sent=time.time())) | ||
save_messages() | ||
elif cmd[0] == "remove": | ||
# Remove a scheduled message by index from the list | ||
# Format: remove <index> | ||
if len(cmd) < 2: | ||
print("Not enough parameters! Format: remove <index>") | ||
continue | ||
|
||
try: | ||
index = int(cmd[1]) | ||
if index < 0 or index >= len(scheduledMessages): | ||
raise ValueError | ||
except ValueError: | ||
print("Invalid index! Must be an integer within the range of the list.") | ||
continue | ||
|
||
scheduledMessages.pop(index) | ||
save_messages() | ||
elif cmd[0] == "run": | ||
while True: | ||
await check_messages() | ||
await asyncio.sleep(1) | ||
|
||
if logOut: | ||
await client.log_out() | ||
print("Logged out!") | ||
|
||
with client: | ||
client.loop.run_until_complete(loop()) | ||
|
||
print("Bye!") |
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,127 @@ | ||
#date: 2024-09-10T16:44:44Z | ||
#url: https://api.github.com/gists/01e1f2f2fd5a413db7dbd68df088489a | ||
#owner: https://api.github.com/users/srkim | ||
|
||
import pandas as pd | ||
import statsmodels.api as sm | ||
import matplotlib.pyplot as plt | ||
import seaborn as sns | ||
from sklearn.metrics import mutual_info_score | ||
import scipy.stats as stats | ||
from scipy.stats import linregress | ||
from sklearn.linear_model import LinearRegression | ||
from sklearn.model_selection import train_test_split | ||
import numpy as np | ||
|
||
# Set the aesthetic style of the plots | ||
sns.set(style="whitegrid") | ||
|
||
# Read data from an Excel file and store it in a Pandas DataFrame | ||
df = pd.read_excel('CorrelationsHedge2.xlsx') | ||
|
||
# Define the response and explanatory variables | ||
y = df['Y'] | ||
x = df['X'] | ||
|
||
# Calculate medians and IQRs for x and y | ||
median_x = np.median(x) | ||
median_y = np.median(y) | ||
q1_x, q3_x = np.percentile(x, [25, 75]) | ||
q1_y, q3_y = np.percentile(y, [25, 75]) | ||
iqr_x = q3_x - q1_x | ||
iqr_y = q3_y - q1_y | ||
|
||
# Identify outliers | ||
outliers = (x < q1_x - 1.5 * iqr_x) | (x > q3_x + 1.5 * iqr_x) | (y < q1_y - 1.5 * iqr_y) | (y > q3_y + 1.5 * iqr_y) | ||
|
||
# Calculate the slope, intercept, and R-squared value of the regression line | ||
slope, intercept, r_value, _, _ = linregress(x, y) | ||
r_squared = r_value**2 | ||
|
||
# Generate the regression line | ||
regression_line = slope * x + intercept | ||
|
||
# Divide data into quadrants counterclockwise from the top right | ||
q1 = (x >= 0) & (y >= 0) # Top right | ||
q2 = (x <= 0) & (y >= 0) # Top left | ||
q3 = (x <= 0) & (y <= 0) # Bottom left | ||
q4 = (x >= 0) & (y <= 0) # Bottom right | ||
|
||
# Calculate correlations for each quadrant | ||
corr_q1 = np.corrcoef(x[q1], y[q1])[0, 1] | ||
corr_q2 = np.corrcoef(x[q2], y[q2])[0, 1] | ||
corr_q3 = np.corrcoef(x[q3], y[q3])[0, 1] | ||
corr_q4 = np.corrcoef(x[q4], y[q4])[0, 1] | ||
|
||
# Calculate overall correlation | ||
overall_corr = np.corrcoef(x, y)[0, 1] | ||
|
||
# Calculate the number of points in each quadrant and the percentage of total points | ||
total_points = len(x) | ||
q1_count = np.sum(q1) | ||
q2_count = np.sum(q2) | ||
q3_count = np.sum(q3) | ||
q4_count = np.sum(q4) | ||
|
||
q1_percentage = (q1_count / total_points) * 100 | ||
q2_percentage = (q2_count / total_points) * 100 | ||
q3_percentage = (q3_count / total_points) * 100 | ||
q4_percentage = (q4_count / total_points) * 100 | ||
|
||
# Calculate the number of points where x is greater than y in each quadrant | ||
q1_x_greater_y = np.sum((q1) & (x > y)) | ||
q2_x_greater_y = np.sum((q2) & (x > y)) | ||
q3_x_greater_y = np.sum((q3) & (x > y)) | ||
q4_x_greater_y = np.sum((q4) & (x > y)) | ||
|
||
# Calculate the percentage of points where x is greater than y in each quadrant | ||
q1_x_greater_y_percentage = (q1_x_greater_y / q1_count) * 100 | ||
q2_x_greater_y_percentage = (q2_x_greater_y / q2_count) * 100 | ||
q3_x_greater_y_percentage = (q3_x_greater_y / q3_count) * 100 | ||
q4_x_greater_y_percentage = (q4_x_greater_y / q4_count) * 100 | ||
|
||
# Print summary statistics | ||
print(f"Sum of correlations in quadrants: {corr_q1 + corr_q2 + corr_q3 + corr_q4:.4f}") | ||
print(f"Overall correlation: {overall_corr:.4f}") | ||
print('________________________________________________') | ||
|
||
# Print the number of points in each quadrant and the percentage of points from the total points | ||
print(f"Number of points in Q1: {q1_count} ({q1_percentage:.2f}%)") | ||
print(f"Number of points in Q2: {q2_count} ({q2_percentage:.2f}%)") | ||
print(f"Number of points in Q3: {q3_count} ({q3_percentage:.2f}%)") | ||
print(f"Number of points in Q4: {q4_count} ({q4_percentage:.2f}%)") | ||
print('________________________________________________') | ||
|
||
# Print the percentage of points where x is greater than y in each quadrant | ||
print(f"Percentage of points where x > y in Q1: {q1_x_greater_y_percentage:.2f}%") | ||
print(f"Percentage of points where x > y in Q2: {q2_x_greater_y_percentage:.2f}%") | ||
print(f"Percentage of points where x > y in Q3: {q3_x_greater_y_percentage:.2f}%") | ||
print(f"Percentage of points where x > y in Q4: {q4_x_greater_y_percentage:.2f}%") | ||
|
||
# Create scatter plot with colored quadrants and 'x' marker for outliers | ||
plt.figure(figsize=(12, 8)) | ||
plt.scatter(x[q1 & ~outliers], y[q1 & ~outliers], alpha=.6, s=50, label=f'Q1: {corr_q1:.4f}') | ||
plt.scatter(x[q2 & ~outliers], y[q2 & ~outliers], alpha=.6, s=50, label=f'Q2: {corr_q2:.4f}') | ||
plt.scatter(x[q3 & ~outliers], y[q3 & ~outliers], alpha=.6, s=50, label=f'Q3: {corr_q3:.4f}') | ||
plt.scatter(x[q4 & ~outliers], y[q4 & ~outliers], alpha=.6, s=50, label=f'Q4: {corr_q4:.4f}') | ||
|
||
# Plot outliers with 'x' marker with the same color as their respective quadrants | ||
plt.scatter(x[q1 & outliers], y[q1 & outliers], s=40, color='C0', marker='x') | ||
plt.scatter(x[q2 & outliers], y[q2 & outliers], s=40, color='C1', marker='x') | ||
plt.scatter(x[q3 & outliers], y[q3 & outliers], s=40, color='C2', marker='x') | ||
plt.scatter(x[q4 & outliers], y[q4 & outliers], s=40, color='C3', marker='x') | ||
|
||
# Plot regression line and axis lines | ||
plt.plot(x, regression_line, color="gray", label="Regression Line") | ||
plt.axvline(x=0, color='gray', linestyle='--') | ||
plt.axhline(y=0, color='gray', linestyle='--') | ||
|
||
plt.title(f'Y vs. X\nOverall Correlation: {overall_corr:.4f}\nR-squared: {r_squared:.4f}\nSlope: {slope:.4f}: x are Outliers', fontsize=14) | ||
plt.xlabel('X') | ||
plt.ylabel('Y') | ||
plt.legend() | ||
|
||
# Save the plot as an SVG file | ||
plt.savefig('Correlation.svg') | ||
|
||
plt.show() |
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,32 @@ | ||
#date: 2024-09-10T16:56:58Z | ||
#url: https://api.github.com/gists/b5462dcec2d47ef4e4bdcdf05d3e4303 | ||
#owner: https://api.github.com/users/justin2004 | ||
|
||
# first download https://dlcdn.apache.org/jena/binaries/apache-jena-5.1.0.zip | ||
# then unzip it | ||
|
||
% mkdir /tmp/db1 | ||
% ls ~/Downloads/labels.ttl | ||
/Users/justin/Downloads/labels.ttl | ||
% ~/Downloads/apache-jena-5.1.0/bin/tdb2.tdbloader --loader=parallel --loc /tmp/db1 ~/Downloads/labels.ttl | ||
11:52:38 INFO loader :: Loader = LoaderParallel | ||
11:52:38 INFO loader :: Start: /Users/justin/Downloads/labels.ttl | ||
11:52:38 INFO loader :: Finished: /Users/justin/Downloads/labels.ttl: 5 tuples in 0.11s (Avg: 46) | ||
11:52:38 INFO loader :: Finish - index OSP | ||
11:52:38 INFO loader :: Finish - index POS | ||
11:52:38 INFO loader :: Finish - index SPO | ||
% cat /tmp/some.rq | ||
select * where { | ||
?s ?p ?o | ||
} limit 2 | ||
% ~/Downloads/apache-jena-5.1.0/bin/tdb2.tdbquery --loc /tmp/db1 --query /tmp/some.rq | ||
---------------------------------------------------------------------------------------------------------------------- | ||
| s | p | o | | ||
====================================================================================================================== | ||
| <http://www.wikidata.org/entity/Q6553274> | <http://www.w3.org/2000/01/rdf-schema#label> | "line number"@en | | ||
| <http://www.wikidata.org/entity/Q113515824> | <http://www.w3.org/2000/01/rdf-schema#label> | "contiguous lines"@en | | ||
---------------------------------------------------------------------------------------------------------------------- | ||
% ~/Downloads/apache-jena-5.1.0/bin/tdb2.tdbquery --results=csv --loc /tmp/db1 --query /tmp/some.rq | ||
s,p,o | ||
http://www.wikidata.org/entity/Q6553274,http://www.w3.org/2000/01/rdf-schema#label,line number | ||
http://www.wikidata.org/entity/Q113515824,http://www.w3.org/2000/01/rdf-schema#label,contiguous lines |
Oops, something went wrong.