Skip to content

Commit

Permalink
Clean up kmeans folders
Browse files Browse the repository at this point in the history
  • Loading branch information
Gongsta committed Jul 2, 2024
1 parent ae2afec commit 612f862
Show file tree
Hide file tree
Showing 30 changed files with 293 additions and 150 deletions.
Binary file removed data/clusters/flop/1669086738_samples=10000_bins=5.npy
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed data/raw/flop/1669086738_samples=10000_bins=5
Binary file not shown.
Binary file removed data/raw/flop/1669086738_samples=10000_bins=5.npy
Binary file not shown.
Binary file removed data/raw/flop/1669110120_samples=100000_bins=5
Binary file not shown.
Binary file removed data/raw/flop/1669110120_samples=100000_bins=5.npy
Binary file not shown.
Binary file removed data/raw/flop/1719047678_samples=10000_bins=10
Binary file not shown.
Binary file removed data/raw/flop/1719047678_samples=10000_bins=10.npy
Binary file not shown.
Binary file removed data/raw/turn/1669084044_samples=10000_bins=5
Binary file not shown.
Binary file removed data/raw/turn/1669084044_samples=10000_bins=5.npy
Binary file not shown.
Binary file removed data/raw/turn/1669110257_samples=100000_bins=5
Binary file not shown.
Binary file removed data/raw/turn/1669110257_samples=100000_bins=5.npy
Binary file not shown.
Binary file removed data/raw/turn/1719048213_samples=10000_bins=10
Binary file not shown.
Binary file removed data/raw/turn/1719048213_samples=10000_bins=10.npy
Binary file not shown.
Binary file removed data/slumbot/steveng_call.joblib
Binary file not shown.
Binary file removed data/slumbot/steveng_equity.joblib
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
381 changes: 267 additions & 114 deletions notebooks/abstraction_visualization.ipynb

Large diffs are not rendered by default.

60 changes: 25 additions & 35 deletions src/abstraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import argparse
from sklearn.cluster import KMeans

USE_KMEANS = False # use kmeans if you want to cluster by equity distribution (more refined, but less accurate)
USE_KMEANS = True # use kmeans if you want to cluster by equity distribution (more refined, but less accurate)
NUM_FLOP_CLUSTERS = 10
NUM_TURN_CLUSTERS = 10
NUM_RIVER_CLUSTERS = 10
Expand All @@ -47,23 +47,14 @@

def load_kmeans_classifiers():
global kmeans_flop, kmeans_turn
raw_dataset_filenames = sorted(get_filenames(f"../data/clusters/flop"))
filename = raw_dataset_filenames[-1] # Take the most recently generated dataset
print("Loading KMeans Flop Classifier", filename)

centroids = joblib.load(f"../data/clusters/flop/{filename}")
kmeans_flop = KMeans(NUM_FLOP_CLUSTERS)
kmeans_flop.cluster_centers_ = centroids
kmeans_flop._n_threads = -1
filename = sorted(get_filenames(f"../kmeans_data/kmeans/flop"))[-1]
print("Loading KMeans Flop Classifier", filename)
kmeans_flop = joblib.load(f"../kmeans_data/kmeans/flop/{filename}")

raw_dataset_filenames = sorted(get_filenames(f"../data/clusters/turn"))
filename = raw_dataset_filenames[-1] # Take the most recently generated dataset
filename = sorted(get_filenames(f"../kmeans_data/kmeans/turn"))[-1]
print("Loading KMeans Turn Classifier", filename)

centroids = joblib.load(f"../data/clusters/turn/{filename}")
kmeans_turn = KMeans(NUM_TURN_CLUSTERS)
kmeans_turn.cluster_centers_ = centroids
kmeans_turn._n_threads = -1
kmeans_turn = joblib.load(f"../kmeans_data/kmeans/turn/{filename}")

assert len(kmeans_flop.cluster_centers_) == NUM_FLOP_CLUSTERS
assert len(kmeans_turn.cluster_centers_) == NUM_TURN_CLUSTERS
Expand All @@ -79,8 +70,8 @@ def load_kmeans_classifiers():
NUM_RIVER_CLUSTERS = 10
try:
load_kmeans_classifiers()
except:
print("Couldn't load KMeans Classifiers. Generating new ones.")
except Exception as e:
print(e, "Couldn't load KMeans Classifiers. Generating new ones.")
clustering = True


Expand Down Expand Up @@ -410,10 +401,10 @@ def plot_equity_hist(equity_hist, player_cards=None, community_cards=None):

# Post-Flop Abstraction using Equity Distributions
def create_abstraction_folders():
if not os.path.exists("../data"):
for split in ["clusters", "raw"]:
for stage in ["flop", "turn", "river"]:
os.makedirs(f"../data/{split}/{stage}")
if not os.path.exists("../kmeans_data"):
for split in ["centroids", "cards", "distributions", "kmeans"]:
for stage in ["flop", "turn"]:
os.makedirs(f"../kmeans_data/{split}/{stage}")


def generate_postflop_equity_distributions(
Expand Down Expand Up @@ -455,12 +446,14 @@ def process_sample(num_community_cards, bins):
if save:
create_abstraction_folders()
file_id = int(time.time()) # Use the time as the file_id
# TODO: Change to joblib saving for consistency everywhere
with open(f"../data/raw/{stage}/{file_id}_samples={n_samples}_bins={bins}.npy", "wb") as f:
np.save(f, equity_distributions)
joblib.dump(
hands, f"../data/raw/{stage}/{file_id}_samples={n_samples}_bins={bins}"
) # Store the list of hands, so you can associate a particular distribution with a particular hand
np.save(
f"../kmeans_data/distributions/{stage}/{file_id}_samples={n_samples}_bins={bins}.npy",
equity_distributions,
)
np.save(
f"../kmeans_data/cards/{stage}/{file_id}_samples={n_samples}_bins={bins}.npy", hands
)
# Store the list of hands, so you can associate a particular distribution with a particular hand


def predict_cluster_kmeans(kmeans_classifier, cards, n=200):
Expand Down Expand Up @@ -554,20 +547,17 @@ def predict_cluster_fast(cards, n=2000, total_clusters=10):
if clustering:
stages = ["flop", "turn"]
for stage in stages:
raw_dataset_filenames = sorted(get_filenames(f"../data/raw/{stage}"))
# Take the most recently generated dataset to run our clustering on
filename = raw_dataset_filenames[-1]
print(filename)

equity_distributions = np.load(f"../data/raw/{stage}/{filename}")
if not os.path.exists(f"../data/clusters/{stage}/{filename}"):
filename = sorted(get_filenames(f"../kmeans_data/distributions/{stage}"))[-1]
equity_distributions = np.load(f"../kmeans_data/distributions/{stage}/{filename}")
if not os.path.exists(f"../kmeans_data/centroids/{stage}/centroids_{filename}"):
if stage == "flop":
kmeans = KMeans(NUM_FLOP_CLUSTERS)
elif stage == "turn":
kmeans = KMeans(NUM_TURN_CLUSTERS)

kmeans.fit(equity_distributions) # Perform Clustering
centroids = kmeans.cluster_centers_
joblib.dump(centroids, f"../data/clusters/{stage}/{filename}")
np.save(f"../kmeans_data/centroids/{stage}/centroids_{filename}", centroids)
joblib.dump(kmeans, f"../kmeans_data/kmeans/{stage}/kmeans_{filename.split('.')[0]}.joblib")
else: # Centroids have already been generated, just load them, which are tensors
load_kmeans_classifiers()
2 changes: 1 addition & 1 deletion src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os


def get_filenames(folder, extension=".npy"):
def get_filenames(folder, extension=""):
filenames = []

for path in glob.glob(os.path.join(folder, "*" + extension)):
Expand Down

0 comments on commit 612f862

Please sign in to comment.