<commit_before>import json
from PIL import Image
import scipy
from scipy.misc import fromimage
from sklearn.cluster import KMeans
from skimage.color import rgb2lab, deltaE_ciede2000
from .conversion import *


class ColorFinder:
    def __init__(self, mode="rgb"):
        colors_wide_file = open('colors_wide.json')
        colors_file = open('colors.json')
        self.colors_wide = json.load(colors_wide_file)
        self.colors = json.load(colors_file)
        if mode.lower() not in ('rgb', 'xyz', 'lab'):
            raise ValueError("'mode' parameter needs to be one of 'RGB', 'XYZ' or 'LAB'")
        else:
            self.mode = mode.lower()


    def find_closest_color(self, color, wide=True):
        if len(color) != 3:
            raise ValueError("'color' parameter needs to be 1D array of length 3")

        mode = self.mode
        if mode == 'xyz':
            color = xyz_to_lab(color)
            mode = 'lab'

        colors = self.colors_wide if wide else self.colors
        min_dist = float("inf")
        best_color = None
        for clr in colors:
            distance = deltaE_ciede2000(color, clr[mode])
            if distance < min_dist:
                min_dist = distance
                best_color = clr

        return best_color

    def find_colors_kmeans(self, image, num_colors):
        im = Image.open(image)
        w = 300 if im.size[0] > 300 else im.size[0]
        h = w * im.size[1] // im.size[0]
        im = im.resize((w, h), Image.BILINEAR)

        ar = fromimage(im)
        ar = rgb2lab(ar)
        ar = ar.reshape(ar.shape[0] * ar.shape[1], ar.shape[2])
        km = KMeans(n_clusters=num_colors, n_jobs=-1).fit(ar)

        colors = set()
        for center in km.cluster_centers_:
            colors.add(self.find_closest_color(center, wide=False))
            colors.add(self.find_closest_color(center))

        return list(colors)


def find_color(color, mode="RGB", wide=True):
    return ColorFinder(mode).find_closest_color(color, wide)

def find_image_colors(image, mode, num_colors):
    return ColorFinder(mode).find_colors_kmeans(image, num_colors)<commit_msg>Fixed mode conversions<commit_after>import json
from PIL import Image
from scipy.misc import fromimage
from sklearn.cluster import KMeans
from skimage.color import *
from .conversion import *


class ColorFinder:
    def __init__(self):
        colors_wide_file = open('colors_wide.json')
        colors_file = open('colors.json')
        self.colors_wide = json.load(colors_wide_file)
        self.colors = json.load(colors_file)

    def find_closest_color(self, color, mode, wide):
        if len(color) != 3:
            raise ValueError("'color' parameter needs to be 1D array of length 3")

        mode = mode.lower()
        if mode not in ('rgb', 'xyz', 'lab'):
            raise ValueError("'mode' parameter needs to be one of 'RGB', 'XYZ' or 'LAB'")

        if mode == 'xyz':
            color = xyz_to_lab(color)
            mode = 'lab'

        colors = self.colors_wide if wide else self.colors
        min_dist = float("inf")
        best_color = None
        for clr in colors:
            distance = deltaE_ciede2000(color, clr[mode])
            if distance < min_dist:
                min_dist = distance
                best_color = clr

        return best_color

    def find_colors_kmeans(self, image, num_colors, mode):
        mode = mode.lower()
        if mode not in ('rgb', 'xyz', 'lab'):
            raise ValueError("'mode' parameter needs to be one of 'RGB', 'XYZ' or 'LAB'")

        im = Image.open(image)
        w = 300 if im.size[0] > 300 else im.size[0]
        h = w * im.size[1] // im.size[0]
        im = im.resize((w, h), Image.BILINEAR)

        ar = fromimage(im)
        if mode == 'rgb':
            ar = rgb2lab(ar)
        elif mode == 'xyz':
            ar = xyz2lab(ar)

        ar = ar.reshape(ar.shape[0] * ar.shape[1], ar.shape[2])
        km = KMeans(n_clusters=num_colors, n_jobs=-1).fit(ar)

        colors = set()
        for center in km.cluster_centers_:
            colors.add(self.find_closest_color(center, mode='lab', wide=False))
            colors.add(self.find_closest_color(center, mode='lab', wide=True))

        return list(colors)


def find_color(color, mode="RGB", wide=True):
    return ColorFinder().find_closest_color(color, mode, wide)


def find_image_colors(image, num_colors, mode="RGB"):
    return ColorFinder().find_colors_kmeans(image, num_colors, mode)
{'fraction_non_alphanumeric': 0.11593554162936437, 'fraction_numerical': 0.011190689346463742, 'mean_word_length': 2.442989214175655, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3706887', 'n_tokens_mistral': 1481, 'n_tokens_neox': 1411, 'n_words': 367}