text:
import os
import cv2
import numpy as np
from enum import Enum
import pickle
from tfci import compress, decompress
from functools import partial
from statistics import mean
from skimage.metrics import structural_similarity as ssim
from skimage.metrics import mean_squared_error as mse
from skimage.metrics import peak_signal_noise_ratio as psnr
from skimage.metrics import normalized_root_mse as nrmse

def get_metrics(imageA, imageB):
    val_ssim = ssim(imageA, imageB, multichannel=True)
    val_mse = mse(imageA, imageB)
    val_psnr = psnr(imageA, imageB)
    val_nrmse = nrmse(imageA, imageB)
    return {
        "ssim": round(val_ssim, 3),
        "mse": round(val_mse, 3),
        "psnr": round(val_psnr, 3),
        "nrmse": round(val_nrmse, 3),
    }

class normalize(Enum):
    OPENCV = 1
    NUMPY = 2

class soften(Enum):
    BLUR = 1
    MEDIAN = 2
    NONE = 3

class encoding(Enum):
    NO_COMP = "NO_COMP"
    COMP_TFCI_HI = "COMP_TFCI_HI"
    COMP_TFCI_MI = "COMP_TFCI_MI"
    COMP_TFCI_LO = "COMP_TFCI_LO"
    COMP_JPEG_90 = "COMP_JPEG_90"
    COMP_JPEG_70 = "COMP_JPEG_70"
    COMP_JPEG_50 = "COMP_JPEG_50"
    COMP_BMSHJ_8 = "COMP_BMSHJ_8"
    COMP_BMSHJ_4 = "COMP_BMSHJ_4"
    COMP_BMSHJ_1 = "COMP_BMSHJ_1"

def img_normalize(img, choice=normalize.OPENCV):
    if choice == normalize.OPENCV:
        return cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
    return np.interp(img, (np.min(img), np.max(img)), (0.0, 255.0)).astype(np.uint8)

def img_denormalize(img, choice=normalize.OPENCV, to_range=None):
    if to_range is None:
        rng_min = 0.0
        rng_max = 1.0
    else:
        rng_min = to_range[0]
        rng_max = to_range[1]
    if choice == normalize.OPENCV:
        return cv2.normalize(np.float32(img), None, rng_min, rng_max, cv2.NORM_MINMAX)
    return np.interp(np.float32(img), (np.min(img), np.max(img)), (rng_min, rng_max))

def img_soften(img, choice=soften.BLUR):
    if choice == soften.BLUR:  # DWT_HH
        return cv2.blur(np.float32(img), (3, 3))
    elif choice == soften.MEDIAN:  # DWT_HL, DWT_LH, DWT_LL
        return cv2.medianBlur(np.float32(img), 5)
    return img

def getImgStat(img):
    return {"mean": np.mean(img), "min": np.min(img), "max": np.max(img)}

def generate_paths(uid, method, inFld, outFld, inFname_imgSrc, inFname_imgWtr):
    dct_paths = dict()
    dct_paths["inPath_imgSrc"] = os.path.join(inFld, inFname_imgSrc)
    dct_paths["inPath_imgWtr"] = os.path.join(inFld, inFname_imgWtr)
    dct_paths["outPath_imgSrc"] = os.path.join(
        outFld, "_".join([uid, method, "_imgSrc_", inFname_imgSrc])
    )
    dct_paths["outPath_imgWtr"] = os.path.join(
        outFld, "_".join([uid, method, "_imgWtr_", inFname_imgWtr])
    )
    dct_paths["outPath_imgWtrFilt"] = os.path.join(
        outFld, "_".join([uid, method, "_imgWtrFilt_", inFname_imgWtr])
    )
    dct_paths["outPath_imgEmb"] = os.path.join(
        outFld, "_".join([uid, method, "_imgEmb_", inFname_imgSrc])
    )
    dct_paths["outPath_imgEmbComp"] = os.path.join(
        outFld, "_".join([uid, method, "_imgEmbComp_", inFname_imgSrc])
    )
    dct_paths["outPath_imgExt"] = os.path.join(
        outFld, "_".join([uid, method, "_imgExt_", inFname_imgWtr])
    )
    dct_paths["outPath_imgExtFilt"] = os.path.join(
        outFld, "_".join([uid, method, "_imgExtFilt_", inFname_imgWtr])
    )
    dct_paths["outPath_imgDiffSrc"] = os.path.join(
        outFld, "_".join([uid, method, "_imgDiffSrc_", inFname_imgSrc])
    )
    dct_paths["outPath_imgDiffWtr"] = os.path.join(
        outFld, "_".join([uid, method, "_imgDiffWtr_", inFname_imgWtr])
    )
    return dct_paths

def getDiffImgs(choice=soften.BLUR, **kwargs):
    imgSrc = pickle.load(open(kwargs.get("outPath_imgSrc") + ".pkl", "rb"))
    # imgEmb = pickle.load(open(kwargs.get("outPath_imgEmb") + ".pkl", "rb"))
    imgEmbPkl = pickle.load(open(kwargs.get("outPath_imgEmb") + ".pkl", "rb"))
    imgEmb = cv2.imread(kwargs.get("outPath_imgEmb"))
    imgEmb = img_denormalize(imgEmb, to_range=(np.min(imgEmbPkl), np.max(imgEmbPkl)))
    imgDiffSrc = np.abs(imgEmb - imgSrc)
    imgDiffSrc = img_normalize(imgDiffSrc)
    cv2.imwrite(kwargs.get("outPath_imgDiffSrc"), imgDiffSrc)
    imgSrc = img_normalize(imgSrc)
    imgEmb = img_normalize(imgEmb)
    dct_metrics_emb_src = get_metrics(imgEmb, imgSrc)
    print("Emb vs Src : {}".format(dct_metrics_emb_src))

    imgWtrRz = pickle.load(open(kwargs.get("outPath_imgWtr") + ".pkl", "rb"))
    imgExt = pickle.load(open(kwargs.get("outPath_imgExt") + ".pkl", "rb"))
    imgExtFilt = img_soften(imgExt, choice)
    imgWtrRzFilt = img_soften(imgWtrRz, choice)
    imgDiffwtr = np.abs(imgExtFilt - imgWtrRzFilt)
    imgDiffwtr = img_normalize(imgDiffwtr)
    cv2.imwrite(kwargs.get("outPath_imgDiffWtr"), imgDiffwtr)

    pickle.dump(
        imgWtrRzFilt,
        open(kwargs.get("outPath_imgWtrFilt") + ".pkl", "wb"),
        pickle.HIGHEST_PROTOCOL,
    )
    # imgWtrRzFilt = cv2.normalize(imgWtrRzFilt, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
    imgWtrRzFilt = img_normalize(imgWtrRzFilt)
    cv2.imwrite(kwargs.get("outPath_imgWtrFilt"), imgWtrRzFilt)
    pickle.dump(
        imgExtFilt,
        open(kwargs.get("outPath_imgExtFilt") + ".pkl", "wb"),
        pickle.HIGHEST_PROTOCOL,
    )
    # imgExtFilt = cv2.normalize(imgExtFilt, None, 0, 255, cv2.NORM_MINMAX, cv2.CV_8U)
    imgExtFilt = img_normalize(imgExtFilt)
    cv2.imwrite(kwargs.get("outPath_imgExtFilt"), imgExtFilt)
    dct_metrics_ext_wtr = get_metrics(imgExtFilt, imgWtrRzFilt)
    print("Ext vs Wtr : {}".format(dct_metrics_ext_wtr))

    return dct_metrics_emb_src, dct_metrics_ext_wtr
    # return {
    #     "metrics_ext_wtr": dct_metrics_ext_wtr,
    #     "metrics_emb_src": dct_metrics_emb_src,
    # }

def compress_deepEncoding(imgPath, mode="hific-hi"):
    compress(mode, imgPath, imgPath + ".tfci")
    decompress(imgPath + ".tfci", imgPath)

def compress_JpegEnconding(imgPath, quality=90):
    imgemb = cv2.imread(imgPath)
    cv2.imwrite(
        imgPath + ".jpg",
        imgemb,
        [int(cv2.IMWRITE_JPEG_QUALITY), quality],
    )
    imgemb = cv2.imread(imgPath + ".jpg")
    cv2.imwrite(imgPath, imgemb)

def compress_nothing():
    pass

def do_compress(imgPath, choice=encoding.NO_COMP):
    switcher = {
        encoding.COMP_JPEG_50: partial(compress_JpegEnconding, imgPath, quality=50),
        encoding.COMP_JPEG_70: partial(compress_JpegEnconding, imgPath, quality=70),
        encoding.COMP_JPEG_90: partial(compress_JpegEnconding, imgPath, quality=90),
        encoding.COMP_TFCI_HI: partial(compress_deepEncoding, imgPath, mode="hific-hi"),
        encoding.COMP_TFCI_MI: partial(compress_deepEncoding, imgPath, mode="hific-mi"),
        encoding.COMP_TFCI_LO: partial(compress_deepEncoding, imgPath, mode="hific-lo"),
        encoding.COMP_BMSHJ_8: partial(
            compress_deepEncoding, imgPath, mode="bmshj2018-factorized-mse-8"
        ),
        encoding.COMP_BMSHJ_4: partial(
            compress_deepEncoding, imgPath, mode="bmshj2018-factorized-mse-4"
        ),
        encoding.COMP_BMSHJ_1: partial(
            compress_deepEncoding, imgPath, mode="bmshj2018-factorized-mse-1"
        ),
        encoding.NO_COMP: partial(compress_nothing),
    }

    return switcher.get(choice, compress_nothing)()

def get_summary(dct_results):
    lst_ssim_src = list()
    lst_ssim_wtr = list()
    lst_psnr_src = list()
    lst_psnr_wtr = list()
    for key, value in dct_results.items():
        if "metrics_emb_src" in value:
            lst_ssim_src.append(value["metrics_emb_src"]["ssim"])
            lst_psnr_src.append(value["metrics_emb_src"]["psnr"])
            lst_ssim_wtr.append(value["metrics_ext_wtr"]["ssim"])
            lst_psnr_wtr.append(value["metrics_ext_wtr"]["psnr"])
    dct_summary = {
        "summary": {
            "ssim_emb_src": round(mean(lst_ssim_src), 3),
            "psnr_emb_src": round(mean(lst_psnr_src), 3),
            "ssim_ext_wtr": round(mean(lst_ssim_wtr), 3),
            "psnr_ext_wtr": round(mean(lst_psnr_wtr), 3),
        }
    }
    print(dct_summary)
    return dct_summary


text:
import numpy as np
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torch.optim as optim
from torchvision import transforms
from tqdm import *
from skimage import transform, data
import pickle
from torch.utils.data import DataLoader, Dataset
from PIL import Image
import os

device = torch.device('cuda:1')

class FastDataSet(Dataset):
    def __init__(self, data_path, label_path, transform4img):
        [train_img, train_label] = loadTinyImageNet(data_path, label_path,
                                                    transform4img)
        self.img = train_img
        self.label = train_label
        self.len = len(self.label)

    def __getitem__(self, item):
        return self.img[item], self.label[item]

    def __len__(self):
        return self.len

def loadTinyImageNet(data_path, label_file, transform4img):
    label_list = []
    train_img = []
    train_label = []
    with open(label_file, 'r') as winds:
        for line in winds:
            label_list.append(line.strip('\n'))
    for index, label in enumerate(label_list):
        train_label_imgs_path = os.path.join(data_path, label)
        train_img_file_list = os.listdir(train_label_imgs_path)
        for path in train_img_file_list:
            path = os.path.join(train_label_imgs_path, path)
            pic = Image.open(path)
            pic = transform4img(pic)
            train_img.append(pic)
            train_label.append(index)
    train_img = [img.numpy() for img in train_img]  # torch.from_numpy(np.array(train_img))
    train_img = torch.Tensor(train_img)
    train_label = torch.Tensor(np.array(train_label)).long()
    return train_img, train_label

def TinyImageNetLoader(data_path, label_path, transform4img, batch_size, shuffle=True):
    dataset = FastDataSet(data_path, label_path, transform4img)
    return DataLoader(dataset, batch_size, shuffle)

# define nn
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        # my network is composed of only affine layers
        self.fc1 = nn.Linear(h * h, 500)
        self.fc2 = nn.Linear(500, 200)
        self.fc3 = nn.Linear(200, 10)

    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

# DATA LOADERS
def flat_trans(x):
    x.resize_(h * h)  # 图片缩放
    return x

if __name__ == '__main__':
    label_file = '../../data/IMagenet-master/tiny-imagenet-200/label.txt'
    train_path = '../../data/IMagenet-master/tiny-imagenet-200/train_LR'
    test_path = '../../data/IMagenet-master/tiny-imagenet-200/test_LR'
    h = 32
    batch_size = 64
    transform = transforms.Compose([
        transforms.Resize((h, h)),
        transforms.Grayscale(),
        transforms.ToTensor(),
    ])
    net = Net()
    net = net.to(device)
    # define the loss function
    SoftmaxWithXent = nn.CrossEntropyLoss()
    # define optimization algorithm
    optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9, weight_decay=1e-04)
    # load the data
    trainLoader = TinyImageNetLoader(train_path, label_file, transform, batch_size=batch_size)
    testLoader = TinyImageNetLoader(test_path, label_file, transform, batch_size=batch_size)

    # strat to train the nn
    for epoch in range(500):
        print("Epoch: {}".format(epoch))
        running_loss = 0.0
        n = 0
        N = 0
        # import ipdb; ipdb.set_trace()
        for data in tqdm(trainLoader):
            # get the inputs
            inputs, labels = data
            n = len(inputs)
            N += n
            inputs = inputs.to(device)
            labels = labels.to(device)
            inputs = inputs.reshape((-1, h * h))
            # wrap them in a variable
            # zero the gradients
            optimizer.zero_grad()
            # print(inputs.shape)

            # forward + loss + backward
            outputs = net(inputs)  # forward pass
            loss = SoftmaxWithXent(outputs, labels)  # compute softmax -> loss
            loss.backward()  # get gradients on params
            optimizer.step()  # SGD update

            # print statistics
            running_loss += loss.cpu().detach().numpy()
        running_loss = running_loss / N
        print('Epoch: {} | Loss: {}'.format(epoch, running_loss))
        # print("Finished Training")
        # TEST
        correct = 0.0
        total = 0
        for data in testLoader:
            images, labels = data
            images = images.to(device)
            labels = labels.to(device)
            images = images.reshape((-1, h * h))
            outputs = net(Variable(images.float()))
            _, predicted = torch.max(outputs.data, 1)
            total += labels.size(0)
            correct += (predicted == labels).sum()
        print("test set Accuracy: {}".format(float(correct) / total))

    print("Dumping weights to disk")
    weights_dict = {}
    # save the weights of nn with protocol=2
    for param in list(net.named_parameters()):
        print("Serializing Param", param[0])
        weights_dict[param[0]] = param[1].cpu().detach()
    with open("weights_size=48_protocol=2.pkl", "wb") as f:
        import pickle

        pickle.dump(weights_dict, f, protocol=2)

    weights_dict2 = {}
    # save the weights of nn with protocol=3
    for param in list(net.named_parameters()):
        print("Serializing Param", param[0])
        weights_dict2[param[0]] = param[1].cpu().detach()
    with open("weights_size=48_protocol=3.pkl", "wb") as f2:
        pickle.dump(weights_dict2, f2, protocol=3)
    print("Finished dumping to disk..")

ext: py

reason: import numpy


text:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
from . import data_info
import pickle
import numpy as np

# Load pickled data
def load_data(data_dir=None, one_hot=False):
    """
    Load and preprocess GSTRB dataset
    :return: tuple of four arrays containing training data, training labels,
             testing data and testing labels.
    """
    if data_dir is not None:
        with open(data_dir + '/train.p', mode='rb') as f:
            train = pickle.load(f)
        with open(data_dir + '/valid.p', mode='rb') as f:
            valid = pickle.load(f)
        with open(data_dir + '/test.p', mode='rb') as f:
            test = pickle.load(f)

        X = {}
        Y = {}
        # split into train - test - validation
        X['train'] = train['features']
        Y['train'] = train['labels']
        X['test'] = test['features']
        Y['test'] = test['labels']
        X['valid'] = valid['features']
        Y['valid'] = valid['labels']

        # print shapes
        data_info.get_shapes(X, Y)

        return X, Y

    else:
        print('data_dir is None.. data not loaded')
        return None


text:
"""multipy: Python library for multicomponent mass transfer"""

__author__ = "James C. Sutherland, Kamila Zdybal"
__copyright__ = "Copyright (c) 2022, James C. Sutherland, Kamila Zdybal"
__license__ = "MIT"
__version__ = "1.0.0"
__maintainer__ = ["Kamila Zdybal"]
__email__ = ["kamilazdybal@gmail.com"]
__status__ = "Production"

import numpy as np
import pandas as pd
import random
import copy
import scipy
import multipy
import warnings

gas_constant = 8.31446261815324

################################################################################
################################################################################
####
####    Class: MulticomponentEffects
####
################################################################################
################################################################################

class MulticomponentEffects:
    """
    Supports performing checks of the multicomponent effects.
    """

    # --------------------------------------------------------------------------

    def __init__(self):

        pass

    # --------------------------------------------------------------------------

max_stars_repo_name: kamilazdybal/multipy

max_issues_repo_name: kamilazdybal/multipy

max_forks_repo_name: kamilazdybal/multipy

reason: import numpy,import scipy


text:
import pandas as pd
from datetime import datetime, timedelta, timezone
import numpy as np

class ModelTransformer(object):
    def __init__(self,ticker,start,end,reporting_gap):
        self.ticker = ticker
        self.reporting_gap = reporting_gap
        self.start = start
        self.end = end
        ## might need to drop years from columns here
        self.drop_columns = [
                                    'open',
                                    'high',
                                    'low',
                                    'close',
                                    'volume',
                                    'dividend',
                                    'split',
                                    'adjopen',
                                    'adjhigh',
                                    'adjlow',
                                    "split",
                                    "dividend",
                                    'adjvolume',
                                    "divcash",
                                    "splitfactor"
                                    "filed","_id","cik"
                                    ,"ticker","ticker_x"
                                    ,"ticker_y"
                                    ,"year"
                                    ,"index","level_0"
                                    ,"_id_x","_id_y","adsh","score",
                                    "level_0"
                                    ,"index_x"
                                    ,"index_y"
                                    ,"reporting_gap"
                                    ,"_id"
                                    ,"id_x","id_y"
                                    ,"filed"
                                    ,"splitfactor"
                                    ]

    ##TODO reaggregate based on filed or some other date
    ##TODO double check indexing
    def fundamental_daily_merge(self,price,funds):
        funds.reset_index(inplace=True)
        price.reset_index(inplace=True)
        for column in price.columns:
            price.rename(columns={column:"".join(column.lower().replace("_","").split())},inplace=True)
        fund_drop_columns = []
        for column in funds.columns:
            if len([x for x in funds.columns if x == column]) > 1:
                fund_drop_columns.append(column)
        funds.drop(fund_drop_columns,axis=1,inplace=True)
        try:
            funds["filed"] = pd.to_datetime(funds["filed"],format="%Y%m%d",utc=True)
        except:
            funds["filed"] = funds["filed"]
        funds["quarter"] = [x.quarter for x in funds["filed"]]
        funds["year"] = [x.year for x in funds["filed"]]
        try:
            price["date"] = pd.to_datetime(price["date"],utc=True)
        except:
            price["date"] = price["date"]
        price["quarter"] = [(x - timedelta(days=self.reporting_gap)).quarter for x in price["date"]]
        price["year"] = [(x - timedelta(days=self.reporting_gap)).year for x in price["date"]]
        final = price.merge(funds,on=["year","quarter"],how="left")
        final["reporting_gap"] = final["date"] - final["filed"]
        final["reporting_gap"] = [x.days for x in final["reporting_gap"]]
        final = final[(final["date"] >= datetime.strptime(self.start,"%Y-%m-%d").replace(tzinfo=timezone.utc))
                    & (final["date"] <= datetime.strptime(self.end,"%Y-%m-%d").replace(tzinfo=timezone.utc))
                    & (final["reporting_gap"] <= self.reporting_gap)
                    & (final["adjclose"] > 0)]
        cleaned = final.drop(self.drop_columns,axis=1,errors="ignore")
        cleaned.drop_duplicates(inplace=True)
        cleaned["ticker"] = self.ticker
        return cleaned


text:
import cPickle as pickle
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
import json
from sklearn.naive_bayes import MultinomialNB
import numpy as np
from sklearn.linear_model import SGDClassifier
from nltk import word_tokenize
from nltk.stem import WordNetLemmatizer
from sklearn import cross_validation
from sklearn.naive_bayes import GaussianNB
from sklearn import svm
from sklearn.neighbors import NearestNeighbors

# class LemmaTokenizer(object):
#     def __init__(self):
#         self.wnl = WordNetLemmatizer()
#     def __call__(self, doc):
#         return [self.wnl.lemmatize(t) for t in word_tokenize(doc)]

with open ('data.json') as input:
	raw_data = json.load(input)

texts = []
labels = []
count = 1
for entry in raw_data:
	texts.append(entry['summary'])
	labels.append(entry['policy_area'])
	print 'processing entey #' + str(count)
	count +=1

# tokenization and stop words removal
count_vect = CountVectorizer(stop_words = 'english')
tokenized_texts = count_vect.fit_transform(texts)

# tfidf feature vector extraction
tfidf_transformer = TfidfTransformer()
texts_tfidf = tfidf_transformer.fit_transform(tokenized_texts)

# storing processed data locally with pickle
# pickle.dump(texts_tfidf,open('feature_vectors.p','wb'))
# pickle.dump(labels,open('labels.p','wb'))

# length = len(labels)
# border = int(length*0.9)
# training_data = texts_tfidf[:border]
# training_labels = labels[:border]
# testing_data = texts_tfidf[border:]
# testing_label = labels[border:]

NB = MultinomialNB()
NB_scores = cross_validation.cross_val_score(NB, texts_tfidf, labels, cv=5)
print NB_scores

# pickle.dump(NB_classifier,open('NB_classifier.p','wb'))

SVM = SGDClassifier(loss='hinge', penalty='l2',alpha=1e-3, n_iter=5, random_state=42)

SVM_scores = cross_validation.cross_val_score(SVM, texts_tfidf, labels, cv=5)
print SVM_scores

# GNB = GaussianNB()
# GNB_scores = cross_validation.cross_val_score(GNB, texts_tfidf.toarray(), labels, cv=5)
# print GNB_scores

# SVM_Linear = svm.LinearSVC()
# SVM_Linear_scores = cross_validation.cross_val_score(SVM_Linear, texts_tfidf.toarray(), labels, cv=5)
# print SVM_Linear_scores

# pickle.dump(SVM,open('SVM.p','wb'))

SVM_rbf = svm.SVC(decision_function_shape='ovr', kernel = 'rbf')
SVM_rbf_scores = cross_validation.cross_val_score(SVM_rbf, texts_tfidf, labels, cv=5)
print SVM_rbf_scores
# 10% accuracy

# predicted = NB_classifier.predict(testing_data)
# print "accuracy = " + str(np.mean(predicted == testing_label))

# predicted = SVM.predict(testing_data)
# print "accuracy = " + str(np.mean(predicted == testing_label))


text:
import math
import functools
import sys
import copy

import numpy as np

import src.anamator.basic_func as basic_func
import src.anamator.objects as objects

HD = (1280, 720)
FHD = (1920, 1080)
SHIT = (640, 480)

def linear(a=1):
    return lambda x: a*x

def find_sup(foo, interval, precision=50):
    return max([foo(x) for x in np.linspace(*interval, precision)])

def find_inf(foo, interval, precision=50):
    return -find_sup(lambda x: -foo(x), interval, precision)

def generate_lower_sum_frame(function, division, resolution):
    frame = basic_func.OneAxisFrame(resolution, 'black', 50, 50)
    func = objects.Function(function)

    x_bounds = (-5, 5)
    y_bounds = (-1, 2.5)
    frame.add_axis_surface(x_bounds=x_bounds, y_bounds=y_bounds)

    settings_function = {
        'sampling rate': 3,
        'thickness': 6,
        'blur': 2,
        'color': 'white'
    }
    settings_axes = {
        'sampling rate': 3,
        'thickness': 3,
        'blur': 0,
        'color': 'white'
    }
    settings_rec_1 = {
        'sampling rate': 3,
        'thickness': 0,
        'blur': 0,
        'color': 'green 1'
    }
    settings_rec_2 = {
        'sampling rate': 3,
        'thickness': 0,
        'blur': 0,
        'color': 'green 2'
    }

    def make_const_foo(c):
        return lambda x: c

    intervals = list(zip(division[:-1], division[1:]))
    division1 = intervals[::2]
    division2 = intervals[1:][::2]
    for interval in division1:
        rectangle = objects.FilledObject(objects.Function(lambda x: 0),
                                         objects.Function(make_const_foo(find_inf(function, interval))), interval)
        frame.axis_surface.blit_filled_object(rectangle, settings_rec_1, interval, queue=True)
    frame.axis_surface.blit_filled_queue()
    for interval in division2:
        rectangle = objects.FilledObject(objects.Function(lambda x: 0),
                                         objects.Function(make_const_foo(find_inf(function, interval))), interval)
        frame.axis_surface.blit_filled_object(rectangle, settings_rec_2, interval, queue=True)
    frame.axis_surface.blit_filled_queue()

    # frame.blit_parametric_object(func2, settings_function2)
    # frame.blit_x_grid(settings_grid, interval=.5, length=.02)
    # frame.blit_y_grid(settings_grid, interval=.05, length=.03)
    frame.blit_parametric_object(func, settings_function)
    frame.blit_axes(settings_axes, x_only=True)

    # first_dash = objects.ParametricObject(lambda x: x, math.sin)
    # second_dash = objects.ParametricObject(lambda x: 5, lambda x: x, y_bounds)
    #
    # frame.axis_surface.blit_dashed_curve(first_dash, number_of_dashes=90, precision=100, settings=settings_axes,
    #                                      interval_of_param=x_bounds, queue=False)
    # frame.axis_surface.blit_dashed_curve(second_dash, 50, 50, settings_axes, y_bounds, queue=False)

    # for x in np.linspace(-5, 5, 10):
    #     frame.axis_surface.blit_closed_pixel_point(coords=func.get_point(x), radius=10, opacity=1,
    #                                                settings=settings_point_interior)
    #     frame.axis_surface.blit_open_pixel_point(coords=func.get_point(x), radius=10, opacity=1,
    #                                              settings=settings_point)
    # frame.axis_surface.blit_closed_pixel_point(coords=(1, 1), radius=4, opacity=1, settings=settings_point)

    frame.blit_axis_surface()
    frame.generate_png('test_riemann_regular_nlogn_square.png')
    return frame

def make_lower_sum_film(resolution):
    film = basic_func.Film(5, resolution, id='test_riemann_regular')
    # division = sorted(list(np.random.uniform(-5.1, 5.1, 5)) + [-5.1, 5.1])
    for n in range(3, 70):
        basic_func.debug(f'[{n+1}/70]', False)
        division = [-5+10*k/int(n*math.log(n)) for k in range(int(n*math.log(n))+1)]
        f = generate_lower_sum_frame(lambda x: math.exp(x / 10) * math.sin(x+1) ** 2 + 0.2,
                                     division, resolution)
        # division = sorted(division + list(np.random.uniform(-5.1, 5.1, 5)))
        film.add_frame(f, save_ram=True)
    film.render('riemann_regular_nlogn_square.mp4', save_ram=True)

def smooth_animate(division_seq, foo):
    def frame_gen(t):
        return generate_lower_sum_frame(foo, list((1-t+math.floor(t))*np.array(division_seq[math.floor(t)])
                                        + (t-math.floor(t))*np.array(division_seq[min(math.floor(t)+1,
                                                                                len(division_seq)-1)])))
    differential = lambda x: (x-3/8)**2*(5/8-x)**2 if abs(x-1/2) < 1/8 else 0

    animation = basic_func.SingleAnimation(frame_gen,
                                          basic_func.normalize_function(basic_func.make_periodic(differential)))
    settings = {
        'fps': 30,
        'resolution': (640, 480),
        'duration': 5
    }
    animation.render('riemann_seq.mp4', settings, save_ram=True, id_='riem1')

def generate_integral(function, interval, x_bounds, y_bounds, function_settings, int_settings, axes_settings, res):
    def make_const(c):
        return lambda x: c

    frame = basic_func.OneAxisFrame(res, 'black', 100, 100)
    frame.add_axis_surface(x_bounds, y_bounds)
    area = objects.FilledObject(objects.Function(lambda x: 0), objects.Function(function), interval)
    frame.axis_surface.blit_filled_object(area, int_settings)
    function_obj = objects.Function(function)
    frame.blit_parametric_object(function_obj, function_settings)
    first_dash = objects.ParametricObject(make_const(interval[0]), lambda t: t)
    second_dash = objects.ParametricObject(make_const(interval[1]), lambda t: t)
    frame.axis_surface.blit_dashed_curve(first_dash, 40, 50, axes_settings, y_bounds, queue=True)
    frame.axis_surface.blit_dashed_curve(second_dash, 40, 50, axes_settings, y_bounds, queue=False)
    frame.blit_axes(axes_settings)
    frame.blit_axis_surface()
    return frame

def smooth_animate_changing_integral(function_seq, int_seq, res):
    settings_axes = {
        'sampling rate': 3,
        'thickness': 3,
        'blur': 0,
        'color': 'white'
    }
    settings_rec_2 = {
        'sampling rate': 3,
        'thickness': 0,
        'blur': 0,
        'color': 'thistle'
    }
    settings_function = {
        'sampling rate': 3,
        'thickness': 6,
        'blur': 2,
        'color': 'white'
    }
    x_bounds = (-5, 5)
    y_bounds = (-2, 2)

    def frame_gen(t):
        return generate_integral(lambda x: (1 - t + math.floor(t)) * function_seq[math.floor(t)](x) +
                                           (t-math.floor(t)) * function_seq[min(math.floor(t)+1, len(function_seq)-1)](x),
                                 list((1 - t + math.floor(t)) * np.array(int_seq[math.floor(t)])
                                 + (t - math.floor(t)) * np.array(int_seq[min(math.floor(t) + 1, len(int_seq) - 1)])),
                                 x_bounds, y_bounds, settings_function, settings_rec_2, settings_axes, res)

    differential = lambda x: (x - 3 / 8) ** 2 * (5 / 8 - x) ** 2 if abs(x - 1 / 2) < 1 / 8 else 0

    animation = basic_func.SingleAnimation(frame_gen,
                                           basic_func.normalize_function(basic_func.make_periodic(differential)))
    settings = {
        'fps': 24,
        'resolution': res,
        'duration': len(int_seq) - 1
    }

    animation.render('smooth_int.mp4', settings, save_ram=True, id_='riem1', speed=.25)

def make_const(c):
    return lambda x: c

def smooth_animate_divisions(divisions, resolution, speed, bigger_ab_at=2, filename='seq.mp4', radius_foo=1):
    filename = f'foo{radius_foo}' + filename
    init_x_length = .025
    settings_axes = {
        'sampling rate': 1,
        'thickness': 3,
        'blur': 0,
        'color': 'white'
    }
    settings_bounds = {
        'sampling rate': 1,
        'thickness': 2,
        'blur': 0,
        'color': 'gray'
    }
    inverted_divisions = divisions[::-1]

    def radius(x):
        if x <= 3/4:
            return int(12*x)
        if x <= 1:
            return int(-16*x+21)
        else:
            return 5

    def big_radius2(x):
        if x <= 1/4:
            return int(60 * x + 5)
        if x <= 1/2:
            return int(-40 * x + 30)
        if x <= 3/4:
            return int(20*x)
        return int(-24*x + 35)

    def big_radius(x):
        if x <= 1/4:
            return int(40 * x + 5)
        if x <= 1/2:
            return int(-20 * x + 20)
        if x <= 3/4:
            return int(40*x - 20)
        return int(-44*x + 53)

    def big_radius3(x):
        if x <= 1/2:
            return int(30 * x + 5)
        return int(-22 * x + 31)

    def generate_division_frame(division, grid_settings, axis_settings, bounds_settings,
                                x_length=init_x_length, r=5, points=False, bigger_ab=False,
                                ab_radius=9, ab_color='white'):
        x_bounds = (division[0] - .1, division[-1] + .1)
        y_bounds = (-2, 2)
        frame = basic_func.OneAxisFrame(resolution, 'black', 100, 100)
        frame.add_axis_surface(x_bounds, y_bounds)
        first_dash = objects.ParametricObject(make_const(divisions[0][0]), lambda t: t)
        second_dash = objects.ParametricObject(make_const(divisions[0][-1]), lambda t: t)
        frame.axis_surface.blit_dashed_curve(first_dash, 40, 200, bounds_settings, y_bounds, queue=True)
        frame.axis_surface.blit_dashed_curve(second_dash, 40, 200, bounds_settings, y_bounds, queue=False)
        if not points:
            lines = [objects.ParametricObject(make_const(point), lambda t: t, [-x_length, x_length])
                     for point in division]
            grid = functools.reduce(lambda x, y: x.stack_parametric_objects(y), lines)
            frame.axis_surface.blit_parametric_object(grid, grid_settings, interval_of_param=(-x_length, (2 * len(lines) - 1) * x_length))
        elif not bigger_ab:
            points = [objects.BitmapDisk(r, 'white', 1) for point in division]
            centers = list(map(lambda x: (x, 0), division))
            frame.axis_surface.blit_distinct_bitmap_objects(centers, points, settings_bounds)
        else:
            points = [objects.BitmapDisk(r, 'white', 1) for point in division[1:-1]]
            points = [objects.BitmapDisk(ab_radius, ab_color, 1)] + points + [objects.BitmapDisk(ab_radius, ab_color, 1)]
            centers = list(map(lambda x: (x, 0), division))
            frame.axis_surface.blit_distinct_bitmap_objects(centers, points, settings_bounds)
        frame.blit_axes(axis_settings, x_only=True)
        frame.blit_axis_surface()
        return frame

    def generate_frame(t):
        if t <= len(divisions) - 1:

            return generate_division_frame(basic_func.SingleAnimation.blend_lists(divisions, t), settings_axes,
                                           settings_axes, settings_bounds)
        if t <= len(divisions) - .5:
            t2 = t - len(divisions) + 1
            return generate_division_frame(divisions[-1], settings_axes, settings_axes, settings_bounds,
                                           x_length=(.5-t2)*init_x_length)
        if t <= len(divisions):
            t3 = 2*(t - len(divisions) + .5)
            return generate_division_frame(divisions[-1], settings_axes, settings_axes, settings_bounds, x_length=0,
                                           r=radius(t3), points=True)
        if t <= len(divisions) + bigger_ab_at:
            t4 = t - len(divisions)
            return generate_division_frame(basic_func.SingleAnimation.blend_lists(inverted_divisions, t4),
                                           settings_axes, settings_axes, settings_bounds, x_length=0,
                                           r=5, points=True)
        if t <= len(divisions) + bigger_ab_at + 1:
            t4 = t - len(divisions)
            t5 = t - len(divisions) - bigger_ab_at
            if radius_foo == 1:
                return generate_division_frame(basic_func.SingleAnimation.blend_lists(inverted_divisions, t4),
                                               settings_axes, settings_axes, settings_bounds, x_length=0,
                                               r=5, points=True, bigger_ab=True, ab_radius=big_radius(t5))
            if radius_foo == 2:
                return generate_division_frame(basic_func.SingleAnimation.blend_lists(inverted_divisions, t4),
                                               settings_axes, settings_axes, settings_bounds, x_length=0,
                                               r=5, points=True, bigger_ab=True, ab_radius=big_radius2(t5))
            return generate_division_frame(basic_func.SingleAnimation.blend_lists(inverted_divisions, t4),
                                           settings_axes, settings_axes, settings_bounds, x_length=0,
                                           r=5, points=True, bigger_ab=True, ab_radius=big_radius3(t5))

        t6 = t - len(divisions)
        return generate_division_frame(basic_func.SingleAnimation.blend_lists(inverted_divisions, t6),
                                       settings_axes, settings_axes, settings_bounds, x_length=0,
                                       r=5, points=True, bigger_ab=True, ab_radius=11)

    differential = objects.PredefinedSettings.exp_differential
    animation = basic_func.SingleAnimation(generate_frame,
                                           basic_func.normalize_function(basic_func.make_periodic(differential)))
    settings = {
        'fps': 24,
        'resolution': resolution,
        'duration': 2*len(divisions) - 1
    }
    animation.render(filename, settings, save_ram=True, id_='div', speed=speed)

def smooth_lower_sums_op(divisions, foos, resolution=FHD, speed=1, filename='op_lower_sums.mp4'):
    foo_settings = objects.PredefinedSettings.t2b0white
    axis_settings = objects.PredefinedSettings.t2b0white
    blended_rec_settings1 = {
        'sampling rate': 3,
        'thickness': 0,
        'blur': 0,
        'color': 'baby blue eyes'
    }
    blended_rec_settings2 = {
        'sampling rate': 3,
        'thickness': 0,
        'blur': 0,
        'color': 'uranian blue'
    }

    divs_and_foo = []
    for divs, foo in zip(divisions, foos):
        divs_and_foo += [(div, foo) for div in divs]
    divs = [x[0] for x in divs_and_foo]
    foos = [x[1] for x in divs_and_foo]

    def generate_frame(t):
        # t0 = min(math.floor(t) + 2*(t - math.floor(t)), math.floor(t) + 1)
        t0 = min(2*(t - math.floor(t)), 1)
        t1 = max(math.floor(t), math.floor(t) + 2*(t - math.floor(t))-1)

        division = basic_func.SingleAnimation.blend_lists(divs, t1)
        func = basic_func.SingleAnimation.blend_functions(foos, t1)
        x_bounds = (division[0] - 1, division[-1] + 1)
        y_bounds = (-1, 3)
        frame = basic_func.OneAxisFrame(resolution, 'black', 100, 100)
        frame.add_axis_surface(x_bounds, y_bounds)

        for interval in list(zip(division[:-1], division[1:]))[::2]:
            level = objects.Function(const=t0*find_inf(func, interval) + (1-t0)*find_sup(func, interval)) \
                if math.floor(t) % 2 == 0 \
                else objects.Function(const=(1-t0)*find_inf(func, interval) + t0*find_sup(func, interval))
            level.add_bounds(interval)
            blended_rec = objects.FilledObject(level, objects.Function(const=0), interval)
            frame.axis_surface.blit_filled_object(blended_rec, blended_rec_settings1, interval, queue=True)
        frame.axis_surface.blit_filled_queue()

        for interval in list(zip(division[:-1], division[1:]))[1::2]:
            level = objects.Function(const=t0*find_inf(func, interval) + (1-t0)*find_sup(func, interval)) \
                if math.floor(t) % 2 == 0 \
                else objects.Function(const=(1-t0)*find_inf(func, interval) + t0*find_sup(func, interval))
            level.add_bounds(interval)
            blended_rec = objects.FilledObject(level, objects.Function(const=0), interval)
            frame.axis_surface.blit_filled_object(blended_rec, blended_rec_settings2, interval, queue=True)
        frame.axis_surface.blit_filled_queue()

        frame.axis_surface.blit_parametric_queue()
        frame.blit_axes(axis_settings, x_only=True)
        func_obj = objects.Function(func)
        frame.blit_parametric_object(func_obj, foo_settings)
        # for point in division:
        #     line = objects.ParametricObject(make_const(point), lambda x: x, y_bounds)
        #     frame.axis_surface.blit_dashed_curve(line, 50, 200, dash_settings, y_bounds, True)
        frame.blit_axis_surface()
        frame.generate_png('op_lower_test_frame.png')
        return frame

    differential = objects.PredefinedSettings.slow_differential
    animation = basic_func.SingleAnimation(generate_frame, differential)
    settings = {
        'fps': 24,
        'resolution': resolution,
        'duration': len(divs) - 1
    }
    animation.render(filename, settings, save_ram=True, id_='op', speed=speed, read_only=True)

def smooth_animate_change_to_recs(division, foo, resolution, speed, filename='change_to_rec.mp4'):
    foo_settings = {
            'sampling rate': 3,
            'thickness': 7,
            'blur': 3,
            'color': 'white'
        }
    axis_settings = objects.PredefinedSettings.t5b2white
    blended_settings = {
            'sampling rate': 3,
            'thickness': 7,
            'blur': 3,
            'color': 'vivid tangerine'
        }
    blended_rec_settings = {
            'sampling rate': 3,
            'thickness': 0,
            'blur': 0,
            'color': 'vivid tangerine'
        }
    dash_settings = {
            'sampling rate': 10,
            'thickness': 2,
            'blur': 0,
            'color': 'gray'
        }

    def generate_frame(t):
        x_bounds = (division[0] - 1, division[-1] + 1)
        y_bounds = (-.5, 2)
        frame = basic_func.OneAxisFrame(resolution, 'black', 100, 100)
        frame.add_axis_surface(x_bounds, y_bounds)
        for interval in zip(division[:-1], division[1:]):
            # blended_foo_sup = objects.Function(basic_func.SingleAnimation.blend_functions([foo, make_const(find_sup(foo, interval))], t))
            # blended_foo_sup.add_bounds(interval)
            blended_foo_inf = objects.Function(basic_func.SingleAnimation.blend_functions([foo, make_const(find_inf(foo, interval))], t))
            blended_foo_inf.add_bounds(interval)
            blended_rec = objects.FilledObject(blended_foo_inf, objects.Function(lambda x: 0), interval)
            # frame.axis_surface.blit_parametric_object(blended_foo_sup, blended_settings, interval, queue=True)
            frame.axis_surface.blit_parametric_object(blended_foo_inf, blended_settings, interval, queue=True)
            frame.axis_surface.blit_filled_object(blended_rec, blended_rec_settings, interval, queue=True)
        frame.axis_surface.blit_parametric_queue()
        frame.axis_surface.blit_filled_queue()
        foo_obj = objects.Function(foo)
        frame.blit_parametric_object(foo_obj, foo_settings)
        frame.blit_axes(axis_settings)
        for point in division:
            line = objects.ParametricObject(make_const(point), lambda x: x, y_bounds)
            frame.axis_surface.blit_dashed_curve(line, 50, 200, dash_settings, y_bounds, True)
        frame.blit_axis_surface()
        return frame

    differential = lambda x: (x - 3 / 8) ** 2 * (5 / 8 - x) ** 2 if abs(x - 1 / 2) < 1 / 8 else 0
    animation = basic_func.SingleAnimation(generate_frame,
                                           basic_func.normalize_function(basic_func.make_periodic(differential)))
    settings = {
        'fps': 24,
        'resolution': resolution,
        'duration': 1
    }
    animation.render(filename, settings, save_ram=True, id_='div', speed=speed)

def decompose_shape(resolution=FHD, speed=.1, filename='decompose.mp4'):
    cube_root = lambda x: x**(1/3) if x >= 0 else -(-x)**(1/3)
    foo1_ = lambda x: (x*(5-x)*math.sin(cube_root(x)) + math.exp(x/5))**.5 - 7*math.exp(-(x+15)**2/30)
    foo2_ = lambda x: -10*(1-((x-5.556)/18.28 + 1)**2)**.5 + 4*math.exp(-(x+15)**2/20)

    foo1 = lambda x: foo1_(x) if not (math.isnan(foo1_(x)) or np.iscomplex(foo1_(x))) else 0
    foo2 = lambda x: foo2_(x) if not (math.isnan(foo2_(x)) or np.iscomplex(foo2_(x))) else 0

    after1_foo1 = lambda t: lambda x: foo1(x+10*t) + 10*t
    after2_foo1 = lambda t: lambda x: foo1(x) + 10*t
    after3_foo1 = lambda t: lambda x: foo1(x-10*t) + 10*t
    after1_foo2 = lambda t: lambda x: foo2(x + 10*t) - 10*t
    after2_foo2 = lambda t: lambda x: foo2(x) - 10*t
    after3_foo2 = lambda t: lambda x: foo2(x - 10*t) - 10*t

    interval1 = lambda t: [-31 - 10*t, -18 - 10*t]
    interval2 = [-18, -6]
    interval3 = lambda t: [-6 + 10*t, 5.55 + 10*t]

    axis_settings = objects.PredefinedSettings.t2b0white
    blended_rec_settings = {
        'sampling rate': 10,
        'thickness': 0,
        'blur': 0,
        'color': 'red pastel 1'
    }

    def generate_frame(t):
        if t <= 1:
            t2 = 0
            t3 = 0
            y_bounds = (-25, 30)
            x_bounds = (-45, 20)
        elif t <= 2:
            t2 = t - 1
            t = 1
            t3 = 0
            x_bounds = (-45*(1-t2) + 1*t2, 20*(1-t2)+18.55*t2)
            y_bounds = (-25*(1-t2) + 5*t2, 30*(1-t2)+20*t2)
        else:
            t3 = t - 2
            t = 1
            t2 = 1
            x_bounds = (1, 18.55)
            y_bounds = (5, 20)

        frame = basic_func.OneAxisFrame(resolution, 'black', 100, 100)
        frame.add_axis_surface(x_bounds, y_bounds)
        # frame.blit_axes(axis_settings, x_only=False)

        s1 = objects.FilledObject(objects.Function(after1_foo1(t)), objects.Function(make_const(10*t)), interval1(t))
        s2 = objects.FilledObject(objects.Function(after2_foo1(t)), objects.Function(make_const(10*t)), interval2)
        s3 = objects.FilledObject(objects.Function(after3_foo1(t)), objects.Function(make_const(10*t)), interval3(t))
        s4 = objects.FilledObject(objects.Function(after1_foo2(t)), objects.Function(make_const(-10*t)), interval1(t))
        s5 = objects.FilledObject(objects.Function(after2_foo2(t)), objects.Function(make_const(-10*t)), interval2)
        s6 = objects.FilledObject(objects.Function(after3_foo2(t)), objects.Function(make_const(-10*t)), interval3(t))

        frame.axis_surface.blit_filled_object(s1, blended_rec_settings, queue=True)
        frame.axis_surface.blit_filled_object(s2, blended_rec_settings, queue=True)
        frame.axis_surface.blit_filled_object(s3, blended_rec_settings, queue=True)
        frame.axis_surface.blit_filled_object(s4, blended_rec_settings, queue=True)
        frame.axis_surface.blit_filled_object(s5, blended_rec_settings, queue=True)
        frame.axis_surface.blit_filled_object(s6, blended_rec_settings, queue=True)
        frame.axis_surface.blit_filled_queue()

        # if t2 > .0001:
        #     ceil = objects.FilledObject(objects.Function(const=20), objects.Function(const=30), x_bounds)
        #     right_wall = objects.FilledObject(objects.Function(const=5), objects.Function(const=20), (-45, 1))
        #     left_wall = objects.FilledObject(objects.Function(const=5), objects.Function(const=20), (18.55, 20))
        #     floor = objects.FilledObject(objects.Function(const=-25), objects.Function(const=5), x_bounds)
        #     settings_walls = {
        #         'sampling rate': 1,
        #         'thickness': 0,
        #         'blur': 0,
        #         'color': (0, 0, 0, t2)
        #     }
        #     frame.axis_surface.blit_filled_object(ceil, settings_walls, queue=True)
        #     frame.axis_surface.blit_filled_object(right_wall, settings_walls, queue=True)
        #     frame.axis_surface.blit_filled_object(left_wall, settings_walls, queue=True)
        #     frame.axis_surface.blit_filled_object(floor, settings_walls, queue=True)
        #     frame.axis_surface.blit_filled_queue()

        if t3 > .0001:
            x_axis = objects.Function(const=10)
            y_axis = objects.ParametricObject(lambda x: 9.775, lambda y: y)
            frame.axis_surface.blit_parametric_object(y_axis, axis_settings, interval_of_param=(5, 5+t3*15), queue=True)
            frame.axis_surface.blit_parametric_object(x_axis, axis_settings, interval_of_param=(1, 1+t3*17.55), queue=True)
            frame.axis_surface.blit_parametric_queue()

        if t3 > .5:
            t4 = 2*(t3-.5)

            def thickness(x):
                if x <= .5:
                    return int(40*x)
                return int(-26*x + 28)

            settings_dash = {
                'sampling rate': 1,
                'thickness': thickness(t4),
                'blur': 0,
                'color': 'gray'
            }

            dash1 = objects.ParametricObject(lambda x: 4, lambda y: y)
            dash2 = objects.ParametricObject(lambda x: 15.55, lambda y: y)
            frame.axis_surface.blit_dashed_curve(dash1, 40, 50, settings_dash, queue=True)
            frame.axis_surface.blit_dashed_curve(dash2, 40, 50, settings_dash, queue=False)

        frame.blit_axis_surface()
        return frame

    differential = objects.PredefinedSettings.exp_differential
    animation = basic_func.SingleAnimation(generate_frame,
                                           basic_func.normalize_function(basic_func.make_periodic(differential)))
    settings = {
        'fps': 24,
        'resolution': resolution,
        'duration': 3
    }
    animation.render(filename, settings, save_ram=True, id_='op', speed=speed, precision=2000)

def get_triple_frame(division, resolution=HD, second_color='baby blue eyes'):
    # function = lambda x: (abs(x) - abs(x - 1) + x ** 2 / 15) / 2 + 1.2
    function = lambda x: 2*math.exp(x/7)*math.sin(x)**2 + .8
    # division = np.linspace(-4, 4, 10)
    if resolution == FHD:
        frame = basic_func.ThreeAxisFrame(resolution, 'black', 100, 350, 200, 200)
    else:
        frame = basic_func.ThreeAxisFrame(resolution, 'black', 44, 155, 88, 88)
    func = objects.Function(function)

    x_bounds = (-5, 5)
    area_bounds = (-4, 4)
    y_bounds = (-1, 6)
    bounds = (x_bounds, y_bounds)
    frame.add_equal_axis_surfaces(bounds, bounds, bounds)

    settings_function = {
        'sampling rate': 3,
        'thickness': 2,
        'blur': 1,
        'color': 'white'
    }
    settings_axes = {
        'sampling rate': 3,
        'thickness': 2,
        'blur': 0,
        'color': 'white'
    }
    # settings_frames = {
    #     'sampling rate': 3,
    #     'thickness': 3,
    #     'blur': 0,
    #     'color': 'gray'
    # }

    settings_rec_1 = {
        'sampling rate': 3,
        'thickness': 0,
        'blur': 0,
        'color': 'uranian blue'
    }
    settings_rec_2 = {
        'sampling rate': 3,
        'thickness': 0,
        'blur': 0,
        'color': second_color
    }

    intervals = list(zip(division[:-1], division[1:]))
    division1 = intervals[::2]
    division2 = intervals[1:][::2]

    for settings, div in zip((settings_rec_1, settings_rec_2), (division1, division2)):
        for interval in div:
            rectangle_lower = objects.FilledObject(objects.Function(const=0),
                                                   objects.Function(const=func.inf(interval)), interval)
            frame.axis_surface_left.blit_filled_object(rectangle_lower, settings, interval, queue=True)
            rectangle_upper = objects.FilledObject(objects.Function(const=0),
                                                   objects.Function(const=func.sup(interval)), interval)
            frame.axis_surface_right.blit_filled_object(rectangle_upper, settings, interval, queue=True)

        frame.axis_surface_left.blit_filled_queue()
        frame.axis_surface_right.blit_filled_queue()

    integral = objects.FilledObject(objects.Function(const=0), func, area_bounds)
    frame.axis_surface_middle.blit_filled_object(integral, settings_rec_1, x_bounds, queue=False)

    frame.axis_surface_left.blit_parametric_object(func, settings_function)
    frame.axis_surface_right.blit_parametric_object(func, settings_function)
    frame.axis_surface_middle.blit_parametric_object(func, settings_function)

    frame.axis_surface_left.blit_axes(settings_axes, x_only=True)
    frame.axis_surface_right.blit_axes(settings_axes, x_only=True)
    frame.axis_surface_middle.blit_axes(settings_axes, x_only=True)

    frame.blit_axis_surfaces()
    # frame.blit_frames_around_axis_surfaces(settings_frames, x_inner_bounds=20, y_inner_bounds=20)
    return frame

def triple_densing(densing_function, no_frames, fps, resolution=FHD):
    area_bounds = (-4, 4)
    film = basic_func.Film(fps, resolution)
    for i in range(no_frames):
        division = np.linspace(*area_bounds, densing_function(i))
        film.add_frame(get_triple_frame(division, resolution), save_ram=True)
        print(f'{i+1}/{no_frames} ({int(100*(i+1)/no_frames)}%)')
    film.render('triple_densing2.mp4', save_ram=True)

def triple_moving(divisions, speed, resolution=FHD, filename='triple_moving.mp4'):
    animator = basic_func.IntervalSequenceAnimation(divisions, basic_func.normalize_function(basic_func.make_periodic(objects.PredefinedSettings.slow_differential)),
                                                    lambda d: get_triple_frame(d, resolution))
    settings = {
        'fps': 24,
        'resolution': resolution,
        'duration': len(divisions) - 1
    }
    animator.render(filename, settings, save_ram=True, id_='triple', speed=speed)

def no_frame():
    resolution = (1920, 1080)
    function = lambda x: (abs(x) - abs(x - 1) + x ** 2 / 15) / 2 + 1.2
    division = np.linspace(-4, 4, 10)
    frame = basic_func.ThreeAxisFrame(resolution, 'black', 100, 350, 200, 200)
    func = objects.Function(function)

    x_bounds = (-5, 5)
    area_bounds = (-4, 4)
    y_bounds = (-.5, 3)
    bounds = (x_bounds, y_bounds)
    frame.add_equal_axis_surfaces(bounds, bounds, bounds)

    settings_function = {
        'sampling rate': 3,
        'thickness': 6,
        'blur': 2,
        'color': 'white'
    }
    settings_axes = {
        'sampling rate': 3,
        'thickness': 3,
        'blur': 0,
        'color': 'white'
    }
    settings_frames = {
        'sampling rate': 3,
        'thickness': 3,
        'blur': 0,
        'color': 'gray'
    }
    settings_rec_1 = {
        'sampling rate': 3,
        'thickness': 0,
        'blur': 0,
        'color': 'green 1'
    }
    settings_rec_2 = {
        'sampling rate': 3,
        'thickness': 0,
        'blur': 0,
        'color': 'green 2'
    }

    intervals = list(zip(division[:-1], division[1:]))
    division1 = intervals[::2]
    division2 = intervals[1:][::2]

    for settings, div in zip((settings_rec_1, settings_rec_2), (division1, division2)):
        for interval in div:
            rectangle_lower = objects.FilledObject(objects.Function(const=0),
                                                   objects.Function(const=func.inf(interval)), interval)
            frame.axis_surface_left.blit_filled_object(rectangle_lower, settings, interval, queue=True)
            rectangle_upper = objects.FilledObject(objects.Function(const=0),
                                                   objects.Function(const=func.sup(interval)), interval)
            frame.axis_surface_right.blit_filled_object(rectangle_upper, settings, interval, queue=True)

        frame.axis_surface_left.blit_filled_queue()
        frame.axis_surface_right.blit_filled_queue()

    integral = objects.FilledObject(objects.Function(const=0), func, area_bounds)
    frame.axis_surface_middle.blit_filled_object(integral, settings_rec_1, x_bounds, queue=False)

    frame.axis_surface_left.blit_parametric_object(func, settings_function)
    frame.axis_surface_right.blit_parametric_object(func, settings_function)
    frame.axis_surface_middle.blit_parametric_object(func, settings_function)

    frame.axis_surface_left.blit_axes(settings_axes, x_only=True)
    frame.axis_surface_right.blit_axes(settings_axes, x_only=True)
    frame.axis_surface_middle.blit_axes(settings_axes, x_only=True)

    frame.blit_axis_surfaces()
    # frame.blit_frames_around_axis_surfaces(settings_frames, x_inner_bounds=20, y_inner_bounds=20)
    frame.generate_png('no_frame.png')

def gradient4d(resolution=FHD, filename='gradient.mp4', no_frames=50, fps=5):

    function = lambda x: (abs(x) - abs(x - 1) + x ** 2 / 15) / 2 + 1.2
    # division = np.linspace(-4, 4, 10)
    if resolution == FHD:
        frame = basic_func.ThreeAxisFrame(resolution, 'black', 100, 350, 200, 200)
    else:
        frame = basic_func.ThreeAxisFrame(resolution, 'black', 44, 155, 88, 88)
    func = objects.Function(function)

    x_bounds = (-5, 5)
    area_bounds = (-4, 4)
    y_bounds = (-.5, 3)
    bounds = (x_bounds, y_bounds)
    frame.add_equal_axis_surfaces(bounds, bounds, bounds)

    settings_function = {
        'sampling rate': 3,
        'thickness': 2,
        'blur': 1,
        'color': 'white'
    }
    settings_axes = {
        'sampling rate': 3,
        'thickness': 2,
        'blur': 0,
        'color': 'white'
    }
    settings_rec_1 = {
        'sampling rate': 3,
        'thickness': 0,
        'blur': 0,
        'color': 'red pastel 1'
    }

    integral = objects.FilledObject(objects.Function(const=0), func, area_bounds)
    frame.axis_surface_middle.blit_filled_object(integral, settings_rec_1, x_bounds, queue=False)
    frame.axis_surface_right.blit_filled_object(integral, settings_rec_1, x_bounds, queue=False)
    frame.axis_surface_left.blit_filled_object(integral, settings_rec_1, x_bounds, queue=False)

    frame.axis_surface_left.blit_parametric_object(func, settings_function)
    frame.axis_surface_right.blit_parametric_object(func, settings_function)
    frame.axis_surface_middle.blit_parametric_object(func, settings_function)

    frame.axis_surface_left.blit_axes(settings_axes, x_only=True)
    frame.axis_surface_right.blit_axes(settings_axes, x_only=True)
    frame.axis_surface_middle.blit_axes(settings_axes, x_only=True)

    frame.blit_axis_surfaces()
    # frame.blit_frames_around_axis_surfaces(settings_frames, x_inner_bounds=20, y_inner_bounds=20)

    densing_foo = lambda x: int(math.log(3*x+1)*x + 2)
    area_bounds = (-4, 4)
    film = basic_func.Film(fps, resolution)
    for i in range(no_frames):
        division = np.linspace(*area_bounds, densing_foo(i))
        film.add_frame(get_triple_frame(division, resolution,
                                        second_color=objects.ColorParser.blend('red pastel 2',
                                                                               'red pastel 1', i/no_frames)),
                       save_ram=True)
        print(f'{i + 1}/{no_frames} ({int(100 * (i + 1) / no_frames)}%)')
    for i in range(3*fps):
        film.add_frame(frame, save_ram=True)
    film.render(filename, save_ram=True)

def absolute_value(func, speed, resolution, filename='abs.mp4', id_='abs'):
    x_bounds = (-5, 5)
    area_bounds = (-5, 5)
    function = objects.Function(func)
    abs_function = objects.Function(lambda x: abs(func(x)))
    y_bounds = (max(function.sup(x_bounds), abs_function.sup(x_bounds)) + 1,
                min(function.inf(x_bounds), 0) - 1)
    settings_rec_positive = {
        'sampling rate': 2,
        'thickness': 0,
        'blur': 0,
        'color': 'tea green'
    }
    settings_rec_negative = {
        'sampling rate': 2,
        'thickness': 0,
        'blur': 0,
        'color': 'light red'
    }

    def gen_frame(foo):
        frame = basic_func.OneAxisFrame(resolution, 'black', 100, 100)
        frame.add_axis_surface(x_bounds, y_bounds)
        blended_function = objects.Function(foo)
        one_sign_intervals = blended_function.one_sign_intervals(area_bounds, precision=2000)
        for interval in one_sign_intervals:
            fill = objects.FilledObject(objects.Function(const=0), objects.Function(blended_function), interval)
            if blended_function(np.mean(np.asarray(interval))) >= 0:
                continue
            frame.axis_surface.blit_filled_object(fill, settings_rec_positive, queue=True)
        frame.axis_surface.blit_filled_queue()
        for interval in one_sign_intervals:
            fill = objects.FilledObject(objects.Function(const=0), objects.Function(blended_function), interval)
            if blended_function(np.mean(np.asarray(interval))) < 0:
                continue
            frame.axis_surface.blit_filled_object(fill, settings_rec_negative, queue=True)
        frame.axis_surface.blit_filled_queue()
        frame.axis_surface.blit_axes(objects.PredefinedSettings.fhd_axis)
        frame.axis_surface.blit_parametric_object(blended_function, objects.PredefinedSettings.fhd_foo)
        frame.blit_axis_surface()
        return frame

    animator = basic_func.FunctionSequenceAnimation((func, lambda t: -abs(func(t))),
                                                    objects.PredefinedSettings.fast_differential, gen_frame)
    settings = {
        'fps': 24,
        'resolution': resolution,
        'duration': 1
    }
    animator.render(filename, settings, True, id_, speed=speed)

def intervals_into_divisions(division, speed, resolution, filename='intervals_into_points.mp4',
                             id_='intervals', slow=False):
    settings_point_interior = {
        'sampling rate': 1,
        'thickness': 1,
        'blur': 0,
        'color': 'white',
        'blur kernel': 'none'
    }

    def radius(x):
        if x <= 3/4:
            return int(16*x)
        if x <= 1:
            return int(-16*x+24)
        else:
            return 8

    def frame_gen(t):
        frame = basic_func.OneAxisFrame(resolution, 'black', 100, 100)
        frame.add_axis_surface((0, 1), (-1, 1))
        points = [objects.BitmapDisk(radius(t), 'white', 1) for _ in division]
        frame.axis_surface.blit_distinct_bitmap_objects(list(map(lambda tt: (tt, 0), division)), points,
                                                        settings_point_interior)

        if t > 1:
            for interval in zip(division[:-1], division[1:]):
                a, b = interval
                mean = (a+b)/2
                t_ = t - 1
                if not math.isclose(t_, 0, abs_tol=.01):
                    frame.axis_surface.blit_parametric_object(objects.Function(const=0),
                                                              objects.PredefinedSettings.fhd_foo,
                                                              (t_*a + (1-t_)*mean, t_*b + (1-t_)*mean), queue=True)
            frame.axis_surface.blit_parametric_queue()
        frame.blit_axis_surface()
        return frame

    animator = basic_func.SingleAnimation(frame_gen, objects.PredefinedSettings.slow_differential) \
        if slow else basic_func.SingleAnimation(frame_gen, objects.PredefinedSettings.fast_differential)
    settings = {
        'fps': 24,
        'resolution': resolution,
        'duration': 2
    }
    animator.render(filename, settings, True, id_, speed=speed)

def bold(interval, bolded_interval, foo, x_bounds=(-1, 1), resolution=FHD, filename='bold.mp4', id_='bold', speed=1,
         dark_color='blue bell', light_color='thistle'):
    filename = dark_color + '_' + light_color + '_' + filename
    downing_settings = {
        'sampling rate': 1,
        'thickness': 5,
        'blur': 0,
        'color': dark_color
    }
    dark_rec_settings = {
        'sampling rate': 1,
        'thickness': 0,
        'blur': 0,
        'color': dark_color
    }
    foo_settings = {
        'sampling rate': 3,
        'thickness': 5,
        'blur': 3,
        'color': 'white'
    }
    axis_settings = {
        'sampling rate': 1,
        'thickness': 3,
        'blur': 0,
        'color': 'white'
    }
    dash_settings = {
        'sampling rate': 3,
        'thickness': 3,
        'blur': 0,
        'color': 'gray'
    }
    fill_downing_settings = {
        'sampling rate': 1,
        'thickness': 0,
        'blur': 0,
        'color': light_color
    }
    lines_settings = {
        'sampling rate': 1,
        'thickness': 5,
        'blur': 0,
        'color': 'gray'
    }
    settings_point_interior = {
        'sampling rate': 1,
        'thickness': 0,
        'blur': 0,
        'color': 'white',
        'blur kernel': 'box'
    }

    def radius(x):
        if x <= 3 / 4:
            return int(16 * x)
        if x <= 1:
            return int(-16 * x + 24)
        else:
            return 8

    def thickness(x):
        if x <= 3 / 4:
            return int(25 * x)
        if x <= 1:
            return int(-40 * x + 50)
        else:
            return 10

    def generate_frame(t):
        t1 = min(t, 1)
        t2 = min(t - 1, 1)
        t3 = min(t - 2, 1)
        t4 = min(t - 3, 1)
        t5 = min(t - 4, 1)

        frame = basic_func.OneAxisFrame(resolution, 'black', 100, 100)
        func = objects.Function(foo)
        argmin = func.argmin(bolded_interval)
        argmax = func.argmax(bolded_interval)
        y_bounds = (-1, func.sup(x_bounds)+1)
        frame.add_axis_surface(x_bounds, y_bounds)
        frame.blit_axes(axis_settings)

        for part in zip(interval[:-1], interval[1:]):
            downing = objects.Function(basic_func.SingleAnimation.blend_functions((foo, make_const(func.inf(part))),
                                                                                t1))
            frame.axis_surface.blit_parametric_object(downing, settings=downing_settings,
                                                      queue=True, interval_of_param=part)
            filling = objects.FilledObject(downing, objects.Function(basic_func.SingleAnimation.blend_functions((copy.copy(downing), lambda h: 0), t1)), part)
            frame.axis_surface.blit_filled_object(filling, fill_downing_settings, interval_of_param=part, queue=True)

        frame.axis_surface.blit_filled_queue()
        frame.axis_surface.blit_parametric_queue()

        if t > 3 and not math.isclose(t, 3):
            rec = objects.FilledObject(objects.Function(const=func(argmin)),
                                       objects.Function(const=(1-t4)*func(argmin) + t4*func(argmax)), bolded_interval)
            frame.axis_surface.blit_filled_object(rec, dark_rec_settings)

        for x in interval:
            line = objects.ParametricObject(make_const(x), lambda y: y, y_bounds)
            frame.axis_surface.blit_dashed_curve(line, 40, settings=dash_settings,
                                                 interval_of_param=y_bounds, queue=True)
        frame.axis_surface.blit_parametric_queue()

        if t > 2 and not math.isclose(t, 2):

            const_settings = {
                'sampling rate': 3,
                'thickness': thickness(t3),
                'blur': 0,
                'color': dark_color
            }
            bolded_const = objects.Function(const=func.inf(bolded_interval))
            bolded_const_int = [t3*bolded_interval[0] + (1-t3)*argmin, t3*bolded_interval[1] + (1-t3)*argmin]
            frame.axis_surface.blit_parametric_object(bolded_const, const_settings, bolded_const_int)

        if t > 4 and not math.isclose(t, 4):
            const_settings = {
                'sampling rate': 3,
                'thickness': thickness(t5),
                'blur': 0,
                'color': light_color
            }
            bolded_const = objects.Function(const=func.sup(bolded_interval))
            bolded_const_int = [t5*bolded_interval[0] + (1-t5)*argmax, t5*bolded_interval[1] + (1-t5)*argmax]
            frame.axis_surface.blit_parametric_object(bolded_const, const_settings, bolded_const_int)

        frame.blit_parametric_object(func, settings=foo_settings)

        if t > 1 and not math.isclose(t, 1):
            color = (0, 0, 0, 3*t2/4)
            gray_wall1 = objects.FilledObject(objects.Function(const=y_bounds[0]), objects.Function(const=y_bounds[1]),
                                              (x_bounds[0], bolded_interval[0]))
            gray_wall2 = objects.FilledObject(objects.Function(const=y_bounds[0]), objects.Function(const=y_bounds[1]),
                                              (bolded_interval[1], x_bounds[1]))
            wall_settings = {
                'sampling rate': 2,
                'thickness': 0,
                'blur': 0,
                'color': color
            }
            frame.axis_surface.blit_filled_object(gray_wall1, wall_settings, queue=True)
            frame.axis_surface.blit_filled_object(gray_wall2, wall_settings, queue=True)
            frame.axis_surface.blit_filled_queue()

            middle = np.mean(y_bounds)
            line_bounds = t2 * np.array(y_bounds) + (1 - t2) * np.array([middle, middle])
            line1 = objects.ParametricObject(make_const(bolded_interval[0]), lambda y: y, line_bounds)
            line2 = objects.ParametricObject(make_const(bolded_interval[1]), lambda y: y, line_bounds)
            frame.axis_surface.blit_parametric_object(line1, lines_settings, queue=True)
            frame.axis_surface.blit_parametric_object(line2, lines_settings, queue=True)
            frame.axis_surface.blit_parametric_queue()

        if t > 2 and not math.isclose(t, 2):
            circle = objects.BitmapDisk(int(.66*radius(t3))+2, dark_color, 1)
            dot = objects.BitmapDisk(int(.66*radius(t3)), light_color, 1)
            frame.axis_surface.blit_bitmap_object((argmin, func(argmin)), circle,
                                                  settings_point_interior, surface_coordinates=True)
            frame.axis_surface.blit_bitmap_object((argmin, func(argmin)), dot,
                                                  settings_point_interior, surface_coordinates=True)

        if t > 4 and not math.isclose(t, 4):
            circle = objects.BitmapDisk(int(.66*radius(t5))+2, light_color, 1)
            dot = objects.BitmapDisk(int(.66*radius(t5)), dark_color, 1)
            frame.axis_surface.blit_bitmap_object((argmax, func(argmax)), circle,
                                                  settings_point_interior, surface_coordinates=True)
            frame.axis_surface.blit_bitmap_object((argmax, func(argmax)), dot,
                                                  settings_point_interior, surface_coordinates=True)
        frame.blit_axis_surface()
        return frame

    animator = basic_func.SingleAnimation(generate_frame, objects.PredefinedSettings.exp_differential)
    settings = {
        'fps': 24,
        'resolution': resolution,
        'duration': 5
    }
    animator.render(filename, settings, save_ram=True, id_=id_, speed=speed, read_only=False)

def hill(speed, resolution, filename='hill.mp4'):
    seq = [lambda x: 0,
           lambda x: 8*math.exp(-(x-3)**2),
           lambda x: 8*math.exp(-(x-3)**2) - 3*math.exp(-(x-5.3)**4)]

    settings_rec_positive = {
        'sampling rate': 2,
        'thickness': 0,
        'blur': 3,
        'color': 'tea green'
    }

    def fame_generator(foo):
        frame = basic_func.OneAxisFrame(resolution, 'black', 0, 0)
        frame.add_axis_surface((0, 8), (-5, 10))
        ground = objects.FilledObject(objects.Function(const=-5), objects.Function(foo), (0, 8))
        frame.axis_surface.blit_filled_object(ground, settings_rec_positive)
        frame.blit_axis_surface()
        return frame

    animator = basic_func.FunctionSequenceAnimation(seq, objects.PredefinedSettings.fast_differential, fame_generator)
    settings = {
        'fps': 24,
        'resolution': resolution,
        'duration': 2
    }
    animator.render(filename, settings, save_ram=True, id_='koparaexp', speed=speed)

NO_RANDOM = 50
NO_NICE = 30

def plum_dirichlet(filename='plum_dirichlet_6px.mp4', speed=1, resolution=FHD, save=False):
    def make_exp_diff(x0):
        return basic_func.normalize_function(lambda x: math.exp(-70*(x-x0)**2), interval=(0, 2))

    differentials = [make_exp_diff(.5)]\
                    + [make_exp_diff(k) for k in np.linspace(1, 1.75, NO_NICE)]*2

    def radius(x):
        if x <= 3/4:
            return int(6+6*x)
        if x <= 1:
            return int(-18*x+24)
        else:
            return 6

    settings_dots = {
        'blur': 3,
        'blur kernel': 'box'
    }

    x_min = -.25
    x_max = 1.25

    random_1_points = [(min(max(np.random.normal(k, .1), x_min), x_max), .3) for k in np.linspace(x_min, x_max, NO_RANDOM)]
    random_0_points = [(min(max(np.random.normal(k, .1), x_min), x_max), 0) for k in np.linspace(x_min, x_max, NO_RANDOM)]
    random_0_points += [(min(max(np.random.normal(k, .1), x_min), 0), 0) for k in np.linspace(x_min, 0, NO_NICE//2)]
    random_0_points += [(min(max(np.random.normal(k, .1), 1), x_max), 0) for k in np.linspace(1, x_max, NO_NICE//2)]
    random_1_points += [(min(max(np.random.normal(k, .1), x_min), 0), .3) for k in np.linspace(x_min, 0, NO_NICE//2)]
    random_1_points += [(min(max(np.random.normal(k, .1), 1), x_max), .3) for k in np.linspace(1, x_max, NO_NICE//2)]

    nice_1_points = [(k, .3) for k in np.linspace(0, 1, NO_NICE)]
    nice_0_points = [(k, 0) for k in np.linspace(0, 1, NO_NICE)]

    def generate_frame(*t_list):
        t0 = t_list[0]
        color = (0, 0, 0, 3 * min(t0, 1) / 4)

        frame = basic_func.OneAxisFrame(resolution)
        frame.add_axis_surface((x_min, x_max), (-.3, .8))

        nice_dots = [objects.BitmapDisk(radius(t), 'white', 1) for t in t_list[1:]]
        random_dot = objects.BitmapDisk(6, 'white', 1)

        frame.blit_axes(objects.PredefinedSettings.fhd_axis, x_only=True)

        for dot, center in zip(nice_dots, nice_1_points):
            frame.axis_surface.blit_bitmap_object(center, dot, settings_dots)
        for dot, center in zip(nice_dots, nice_0_points):
            frame.axis_surface.blit_bitmap_object(center, dot, settings_dots)
        for center in random_1_points + random_0_points:
            frame.axis_surface.blit_bitmap_object(center, random_dot, settings_dots)

        gray_wall1 = objects.FilledObject(objects.Function(const=-.3), objects.Function(const=.8), (x_min, 0))
        gray_wall2 = objects.FilledObject(objects.Function(const=-.3), objects.Function(const=.8), (1, x_max))

        wall_settings = {
            'sampling rate': 2,
            'thickness': 0,
            'blur': 0,
            'color': color
        }
        frame.axis_surface.blit_filled_object(gray_wall1, wall_settings, queue=True, interval_of_param=(x_min, 0))
        frame.axis_surface.blit_filled_object(gray_wall2, wall_settings, queue=True, interval_of_param=(1, x_max))
        frame.axis_surface.blit_filled_queue()

        line_0 = objects.ParametricObject(lambda x: 0, lambda y: y)
        line_1 = objects.ParametricObject(lambda x: 1, lambda y: y)
        bounds = (-.55 * min(t0, 1) + .25, .25 + .55 * min(t0, 1))
        if bounds[1] - bounds[0] > .01:
            frame.axis_surface.blit_dashed_curve(line_0, 40, 50, objects.PredefinedSettings.t2b0gray,
                                                 interval_of_param=bounds, queue=True)
            frame.axis_surface.blit_dashed_curve(line_1, 40, 50, objects.PredefinedSettings.t2b0gray,
                                             interval_of_param=bounds, queue=False)

        frame.blit_axis_surface()
        if save:
            frame.generate_png('test_speedrun.png')
        return frame

    settings = {
        'fps': 24,
        'duration': 2,
        'resolution': resolution
    }

    animator = basic_func.MultiDifferentialAnimation(generate_frame, *differentials)
    animator.render(filename, settings, save_ram=True, speed=speed, id_='plum_dirichlet')

def flying_recs(filename='flying_recs.mp4', resolution=FHD, speed=1):
    init_x_length = .025
    settings_axes = {
        'sampling rate': 1,
        'thickness': 3,
        'blur': 0,
        'color': 'white'
    }
    settings_bounds = {
        'sampling rate': 1,
        'thickness': 2,
        'blur': 0,
        'color': 'gray'
    }
    settings_white = {
        'sampling rate': 1,
        'thickness': 2,
        'blur': 0,
        'color': 'white'
    }
    settings_recs1 = {
        'sampling rate': 3,
        'thickness': 0,
        'blur': 0,
        'color': 'baby powder'
    }
    settings_recs2 = {
        'sampling rate': 3,
        'thickness': 0,
        'blur': 0,
        'color': 'shimmering blush'
    }
    division = [0, .08, .15, .24, .36, .45, .6, .75, .88, .95, 1]
    x_bounds = (division[0] - .1, division[-1] + .1)
    y_bounds = (-2, 2)

    rec_size = [1.5, .3, -.4, .7, -1.6, -1.8, .2, -.9, .1, -1.2]

    def gen_frame(t):
        frame = basic_func.OneAxisFrame(resolution, 'black', 100, 100)
        frame.add_axis_surface(x_bounds, y_bounds)
        recs = [objects.FilledObject(objects.Function(const=t*h), objects.Function(const=0), interval)
                for h, interval in zip(rec_size, zip(division[:-1], division[1:]))]

        for obj in recs[::2]:
            frame.axis_surface.blit_filled_object(obj, settings_recs1, queue=True)
        frame.axis_surface.blit_filled_queue()

        for obj in recs[1::2]:
            frame.axis_surface.blit_filled_object(obj, settings_recs2, queue=True)
        frame.axis_surface.blit_filled_queue()

        first_dash = objects.ParametricObject(make_const(division[0]), linear())
        second_dash = objects.ParametricObject(make_const(division[-1]), linear())
        frame.axis_surface.blit_dashed_curve(first_dash, 40, 200, settings_bounds, y_bounds, queue=True)
        frame.axis_surface.blit_dashed_curve(second_dash, 40, 200, settings_bounds, y_bounds, queue=False)
        frame.blit_axes(settings_axes, x_only=True)
        lines = [objects.ParametricObject(make_const(point), lambda x: x, [-init_x_length, init_x_length])
                 for point in division]
        grid = functools.reduce(lambda x, y: x.stack_parametric_objects(y), lines)
        frame.axis_surface.blit_parametric_object(grid, settings_white,
                                                  interval_of_param=(-init_x_length, (2*len(lines) - 1)*init_x_length))
        frame.blit_axis_surface()
        return frame

    def differential(t):
        return math.sin(1.5*math.pi*t)*(1 + int(math.sin(1.5*math.pi*t) < 0)) if t < 2 \
            else math.sin(3*math.pi*t)*(1 + int(math.sin(3*math.pi*t) < 0))/2

    differential = objects.normalize_function(differential, (0, .66))

    animator = basic_func.SingleAnimation(gen_frame, differential)
    # animator = basic_func.SingleAnimation(gen_frame, objects.normalize_function(differential, (0, .5)))

    settings = {
        'fps': 24,
        'duration': 2,
        'resolution': resolution
    }

    animator.render(filename, settings, speed=speed)

def logo(filename='logo.png'):
    integral_settings = {
        'sampling rate': 3,
        'thickness': 0,
        'blur': 0,
        'color': 'baby blue eyes'
    }
    frame = basic_func.OneAxisFrame((1000, 1000), 'black', 100, 100)
    frame.add_axis_surface((-1, 7), (-1.5, 6))
    foo = objects.Function(lambda x: 2*math.exp(x/7)*math.sin(x)**2 + .8)
    frame.blit_axes(objects.PredefinedSettings.fhd_foo, x_only=True)
    integral = objects.FilledObject(foo, objects.Function(const=0), (0, 6))
    frame.axis_surface.blit_filled_object(integral, integral_settings)
    frame.blit_parametric_object(foo, objects.PredefinedSettings.fhd_foo)
    frame.blit_axis_surface()
    frame.generate_png(filename)

if __name__ == '__main__':
    sys.setrecursionlimit(3000)
    # make_lower_sum_film()
    # seq = [(-5, -5, -5, -5, -3, -1.5, 0, 2, 3, 4, 5),
    #        (-5, -5, -5, -3, -2.5, 0.5, 1.1, 2.3, 3.2, 4.1, 5),
    #        (-5, -5, -4, -2.8, -2.5, -2, -1, 1.4, 2.2, 3.5, 5),
    #        (-5, -4, -3.5, -2.1, -.5, 0, 1.2, 2.5, 3.1, 4, 5)]
    # smooth_animate(seq, lambda x: math.exp(x/10)*math.sin(x)**2+0.2)
    # generate_lower_sum_frame(lambda x: math.exp(x/10)*math.sin(x)**2+0.2, sorted(list(np.random.uniform(-5.1, 5.1, 30)) + [-5.1, 5.1]))
    # func_seq = [math.sin, lambda x: math.exp(x/10)*math.sin(x)**2+0.2, math.cos, lambda x: math.exp(x/10)]
    # int_seq = [(-4, 3), (-4.5, 2), (-3, 4), (-1, 4.5)]
    # smooth_animate_changing_integral(func_seq, int_seq, FHD)
    # divisions = [(0, 1, 1, 3, 3, 3, 4),
    #              (0, 1.5, 2, 2.5, 3.5, 3.7, 4),
    #              (0, 1, 1, 3, 3.4, 3.4, 4),
    #              (0, 2, 2, 2, 3, 3.2, 4)]
    # smooth_animate_divisions(divisions, (1280, 720), 1)
    # division_ = [-4, -3.5, -3, -2, -1.7, -1.3, -1, -.3, .5, 1.5, 2, 2.2, 2.4, 2.6, 2.9, 3, 3.8, 4]
    # smooth_animate_change_to_recs(division_, lambda x: math.exp(x / 10) * math.sin(x) ** 2 + 0.2, (1280, 720), .15,
    #                               'change_to_rec.mp4')
    # for a, b in zip(division_[:-1], division_[1:]):
    #     division_.append((a + b) / 2)
    # division_.sort()
    # smooth_animate_change_to_recs(division_, lambda x: math.exp(x / 10) * math.sin(x) ** 2 + 0.2, (1280, 720), .15,
    # #                               'change_to_rec_dense.mp4')
    # # #
    # foos = [lambda x: math.exp(x / 10) * math.sin(x) ** 2 + 1.2, lambda x: x*(x-2)*(x+2)*(x-5)*(x+5)*math.exp(-abs(x))*math.sin(x)/12+1,
    #         lambda x: (abs(x) - abs(x-1) + x**2/15)/2 + 1.2]
    #
    # divs = [[(-5, -5, -5, -5, -3, -1.5, 0, 2, 3, 4, 5),
    #         (-5, -5, -5, -3, -2.5, 0.5, 1.1, 2.3, 3.2, 4.1, 5),
    #         (-5, -5, -4, -2.8, -2.5, -2, -1, 1.4, 2.2, 3.5, 5),
    #         (-5, -4, -3.5, -2.1, -.5, 0, 1.2, 2.5, 3.1, 4, 5)],
    #         [(-5, -5, -5, -5, -3, -1.5, 0, 2, 3, 4, 5),
    #          (-5, -5, -5, -3, -2.5, 0.5, 1.1, 2.3, 3.2, 4.1, 5),
    #          (-5, -5, -4, -2.8, -2.5, -2, -1, 1.4, 2.2, 3.5, 5),
    #          (-5, -4, -3.5, -2.1, -.5, 0, 1.2, 2.5, 3.1, 4, 5)][::-1],
    #         [(-5, -5, -5, -5, -3, -1.5, 0, 2, 3, 4, 5),
    #          (-5, -5, -5, -3, -2.5, 0.5, 1.1, 2.3, 3.2, 4.1, 5),
    #          (-5, -5, -4, -2.8, -2.5, -2, -1, 1.4, 2.2, 3.5, 5),
    #          (-5, -4, -3.5, -2.1, -.5, 0, 1.2, 2.5, 3.1, 4, 5)]]
    # #
    # divs = [[(-5, -4, -3, -2, -2.5, 0, .5, 2, 3, 4.5, 5),
    #         (-5, -4.5, -3.5, -1.5, -.5, 1, 1.5, 2.5, 3.5, 4, 5),
    #         (-5, -4.3, -3, -2.2, -1, -.5, 1.2, 2.3, 3, 3.5, 5),
    #         (-5, -4.7, -4, -3.3, -2.2, -1.1, 0.4, 1.2, 2, 3.1, 5)],
    #         [(-5, -4, -3, -2, -2.5, 0, .5, 2, 3, 4.5, 5),
    #          (-5, -4.5, -3.5, -1.5, -.5, 1, 1.5, 2.5, 3.5, 4, 5),
    #          (-5, -4.3, -3, -2.2, -1, -.5, 1.2, 2.3, 3, 3.5, 5),
    #          (-5, -4.7, -4, -3.3, -2.2, -1.1, 0.4, 1.2, 2, 3.1, 5)][::-1],
    #         [(-5, -4, -3, -2, -2.5, 0, .5, 2, 3, 4.5, 5),
    #          (-5, -4.5, -3.5, -1.5, -.5, 1, 1.5, 2.5, 3.5, 4, 5),
    #          (-5, -4.3, -3, -2.2, -1, -.5, 1.2, 2.3, 3, 3.5, 5),
    #          (-5, -4.7, -4, -3.3, -2.2, -1.1, 0.4, 1.2, 2, 3.1, 5)]]
    #
    # smooth_lower_sums_op(divs, foos, FHD, 1)

    # decompose_shape((1280, 720), .25)
    # triple_densing(lambda x: int(x*math.log(x + 3) + 1), 15, 3, FHD)
    # bold([-1, -.4, -.15, .1, .3, .45, .7, .9, 1], [.45, .7], lambda x: math.exp(x/2)*math.sin(5*x)**2+1.2,
    #      resolution=FHD, speed=.4)
    # bold([-1, -.4, -.15, .1, .3, .45, .7, .9, 1], [.45, .7], lambda x: math.exp(x / 2) * math.sin(5 * x) ** 2 + 1.2,
    #      resolution=FHD, speed=.4, light_color='nyanza', dark_color='pistachio')
    # bold([-1, -.4, -.15, .1, .3, .45, .7, .9, 1], [.45, .7], lambda x: math.exp(x / 2) * math.sin(5 * x) ** 2 + 1.2,
    #          resolution=FHD, speed=.4, light_color='columbia blue', dark_color='beau blue')
    # bold([-1, -.4, -.15, .1, .3, .45, .7, .9, 1], [.45, .7], lambda x: math.exp(x / 2) * math.sin(5 * x) ** 2 + 1.2,
    #      resolution=FHD, speed=.4, light_color='beau blue', dark_color='cerulean frost')
    # bold([-1, -.4, -.15, .1, .3, .45, .7, .9, 1], [.45, .7], lambda x: math.exp(x / 2) * math.sin(5 * x) ** 2 + 1.2,
    #      resolution=FHD, speed=.4, light_color='silver pink', dark_color='tuscany')
    # bold([-1, -.4, -.15, .1, .3, .45, .7, .9, 1], [.45, .7], lambda x: math.exp(x / 2) * math.sin(5 * x) ** 2 + 1.2,
    #      resolution=FHD, speed=.4, light_color='tuscany', dark_color='eggplant')
    # bold([-1, -.4, -.15, .1, .3, .45, .7, .9, 1], [.45, .7], lambda x: math.exp(x / 2) * math.sin(5 * x) ** 2 + 1.2,
    #      resolution=FHD, speed=.4, light_color='tea green', dark_color='eton blue')
    # bold([-1, -.4, -.15, .1, .3, .45, .7, .9, 1], [.45, .7], lambda x: math.exp(x / 2) * math.sin(5 * x) ** 2 + 1.2,
    #      resolution=FHD, speed=.4, light_color='eton blue', dark_color='cadet gray')
    # bold([-1, -.4, -.15, .1, .3, .45, .7, .9, 1], [.45, .7], lambda x: math.exp(x / 2) * math.sin(5 * x) ** 2 + 1.2,
    #      resolution=FHD, speed=.25, light_color='beige', dark_color='irresistible')
    # bold([-1, -.4, -.15, .1, .3, .45, .7, .9, 1], [.45, .7], lambda x: math.exp(x / 2) * math.sin(5 * x) ** 2 + 1.2,
    #      resolution=FHD, speed=.4, light_color='banana mania', dark_color='grullo')
    # bold([-1, -.4, -.15, .1, .3, .45, .7, .9, 1], [.45, .7], lambda x: math.exp(x / 2) * math.sin(5 * x) ** 2 + 1.2,
    #      resolution=FHD, speed=.4, light_color='banana mania', dark_color='coyote brown')
    # decompose_shape(FHD, .1)
    # plum_dirichlet(speed=.3, resolution=FHD)
    triple_densing(lambda x: int(x*math.log(x+2) + 1), 60, 3)

max_stars_repo_name: AlexanderGolys/anamator

max_issues_repo_name: AlexanderGolys/anamator

max_forks_repo_name: AlexanderGolys/anamator


text:
import random
import numpy as np
from genotype import Genotype

class Population():
    '''Abstract class'''
    def __init__ (self, world, species, initial_pop_size, birth_prob):
        self.world = world
        self.species = species
        self.initial_pop_size = initial_pop_size
        self.pop = self.create_first_gen()
        self.birth_prob = birth_prob
        self.update_death_prob()
        self.update_genes()

    def get_pop(self):
        return self.pop

    def get_initial_pop_size(self):
        return self.initial_pop_size

    def get_birth_prob(self):
        return self.birth_prob

    def get_death_prob(self):
        return self.death_prob

    def update_death_prob(self):
        self.death_prob = 1 - (self.world.abs_max - len(self.pop))/float(self.world.abs_max)

    def create_first_gen(self):
        pop = list()
        for each in range(self.get_initial_pop_size()):
            pop.append(self.species(each))
        return pop

    def kill_wave(self):
        new_pop = list(self.pop)
        for individual in self.pop:
            if random.random() < self.get_death_prob():
                new_pop.remove(individual)

        self.pop = new_pop
        self.update_death_prob()

class AsexualPopulation(Population):
    '''Concrete Class'''
    def update_genes(self):
        self.update_genes_matrix()

    def update_genes_matrix(self):
        genes = list()
        for i in range(self.get_initial_pop_size()):
            qty = 0
            for individual in self.get_pop():
                if i == individual.get_genotype():
                    qty += 1
            genes.append(qty)
        self.genes_matrix = genes

    def get_genes_matrix(self):
        return self.genes_matrix

    def get_original_genes(self):
        original_genes = list()
        for individual in self.get_pop():
            original_genes.append(individual.get_genotype())
        return list(set(original_genes))

    def birth_wave(self):
        new_pop = list(self.get_pop())
        for individual in self.get_pop():
            if random.random() < self.get_birth_prob():
                new_born = self.species(individual.get_genotype())
                new_pop.append(new_born)

        self.pop = new_pop
        self.update_death_prob()

class SexualPopulation(Population):
    def __init__ (self, world, species, initial_pop_size, birth_prob, long_term = False, monogamy = False):
        self.world = world
        self.species = species
        self.initial_pop_size = initial_pop_size
        self.pop = self.create_first_gen()
        self.birth_prob = birth_prob
        self.long_term = long_term #Till Death Do Us Part
        self.monogamy = monogamy

        self.update_death_prob()
        self.update_genes()

        self.couples = list()
        self.update_couples()

    def create_first_gen(self):
        pop = list()
        for each in range(self.get_initial_pop_size()):
            genotype = Genotype()
            genotype.create_initial_genotype(each)
            pop.append(self.species(genotype))
        return pop

    def update_genes(self):
        pass #TODO

    def update_couples(self):
        if self.long_term:
            self.update_long_term_couples()
        else: self.update_short_term_couples()

    def update_couples(self):
        if self.long_term:
            self.remove_widows()
            open_to_mating = self.get_open_to_mating()
        else:
            open_to_mating = self.get_pop()
            self.couples = list()

        self.couples += self.find_new_couples(open_to_mating)

    def get_open_to_mating(self):
        married = list()
        for couple in self.get_couples():
            for partner in couple:
                married.append(partner)
        open_to_mating = list(set(self.get_pop()) - set(married))
        return open_to_mating

    def find_new_couples(self, open_to_mating):
        new_couples = list()
        singe_males = self.get_males(open_to_mating)
        for female in self.get_females(open_to_mating):
            if len(singe_males) == 0:
                break
            male = random.choice(singe_males)
            new_couples.append((female, male))
            if self.monogamy:
                singe_males.remove(male)
        return new_couples

    def remove_widows(self):
        updated_couples = self.get_couples()
        for couple in self.get_couples():
            still_together = True
            for partner in couple:
                if partner not in self.get_pop():
                    still_together = False

            if not still_together:
                updated_couples.remove(couple)

        self.couples = updated_couples

    def birth_wave(self):
        new_pop = list(self.get_pop())
        for couple in self.get_couples():
            if random.random() < self.get_birth_prob():
                new_born = self.give_birth(couple)
                new_pop.append(new_born)
        self.pop = new_pop
        self.update_death_prob()

    def give_birth(self, couple):
        new_born_genotype = Genotype()
        new_born_genotype.mix_parents_dna(couple)
        new_born = self.species(new_born_genotype)
        return new_born

    def get_males(self, pop):
        males = list()
        for individual in pop:
            if individual.is_male():
                males.append(individual)
        return males

    def get_females(self, pop):
        females = list(set(pop) - set(self.get_males(pop)))
        return females

    def get_couples(self):
        return self.couples