Created
April 5, 2024 12:37
-
-
Save jelofsson/d66a9b0337b17f4b59fac1b916838e5e to your computer and use it in GitHub Desktop.
find-traits - nft-generator-py helper
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
import json | |
import sys | |
from collections import Counter | |
import csv | |
# | |
# example usage: | |
# python find-traits.py "trait1,trait2,trait3" | |
# | |
# Parse the input argument | |
attributes_to_filter = set(sys.argv[1].split(',')) | |
# Load the JSON data | |
with open('output/metadata/all-objects.json', 'r') as f: | |
data = json.load(f) | |
# Filter the objects | |
filtered_objects = [obj for obj in data if attributes_to_filter.issubset({attr['value'] for attr in obj['attributes']})] | |
# Find the ranking score of the filtered objects from rarity_scores.csv | |
with open('rarity_scores.csv', 'r') as f: | |
reader = csv.reader(f) | |
next(reader) # Skip the header | |
ranking_scores = {int(token_id): float(rarity_score) for token_id, _, rarity_score in reader} | |
rarity_scores = {token_id: i for i, (token_id, _) in enumerate(sorted(ranking_scores.items(), key=lambda x: x[1], reverse=True), start=1)} | |
# Sort the filtered objects by ranking score | |
filtered_objects.sort(key=lambda obj: ranking_scores[obj['token_id']]) | |
# Print the match count and each matching token_id | |
print(f'Token ID | Ranking Score | Rarity Score') | |
for obj in filtered_objects: | |
print(f'{str(obj["token_id"]).ljust(8)} | {str(ranking_scores[obj["token_id"]]).ljust(14)} | {str(format(rarity_scores[obj["token_id"]], ".2f")).ljust(10)}') | |
print(f'Match count: {len(filtered_objects)}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment