#!/usr/bin/env python

import os.path

import vtk

def get_program_parameters():
    import argparse
    description = 'Decimate polydata.'
    epilogue = '''
    This is an example using vtkDecimatePro to decimate input polydata, if provided, or a sphere otherwise.
    '''
    parser = argparse.ArgumentParser(description=description, epilog=epilogue)
    parser.add_argument('filename', nargs='?', default=None, help='Optional input filename e.g Torso.vtp.')
    parser.add_argument('reduction', nargs='?', type = float, default=.9, help='Sets the decimation target reduction, (default is 0.9).')
    args = parser.parse_args()
    return args.filename, args.reduction

def main():
    filePath, reduction = get_program_parameters()

    # Define colors
    colors = vtk.vtkNamedColors()
    backFaceColor = colors.GetColor3d("gold")
    inputActorColor = colors.GetColor3d("flesh")
    decimatedActorColor = colors.GetColor3d("flesh")
    colors.SetColor('leftBkg', [0.6, 0.5, 0.4, 1.0])
    colors.SetColor('rightBkg', [0.4, 0.5, 0.6, 1.0])

    if filePath and os.path.isfile(filePath):
        inputPolyData = ReadPolyData(filePath)
        if not inputPolyData:
            inputPolyData = GetSpherePD()
    else:
        inputPolyData = GetSpherePD()

    print("Before decimation")
    print(f"There are {inputPolyData.GetNumberOfPoints()} points.")
    print(f"There are {inputPolyData.GetNumberOfPolys()} polygons.")

    decimate = vtk.vtkDecimatePro()
    decimate.SetInputData(inputPolyData)
    decimate.SetTargetReduction(reduction)
    decimate.PreserveTopologyOn()
    decimate.Update()

    decimated = vtk.vtkPolyData()
    decimated.ShallowCopy(decimate.GetOutput())

    print("After decimation")
    print(f"There are {decimated.GetNumberOfPoints()} points.")
    print(f"There are {decimated.GetNumberOfPolys()} polygons.")
    print(f"Reduction: {(inputPolyData.GetNumberOfPolys() - decimated.GetNumberOfPolys()) / inputPolyData.GetNumberOfPolys()}")

    inputMapper = vtk.vtkPolyDataMapper()
    inputMapper.SetInputData(inputPolyData)

    backFace = vtk.vtkProperty()
    backFace.SetColor(backFaceColor)

    inputActor = vtk.vtkActor()
    inputActor.SetMapper(inputMapper)
    inputActor.GetProperty().SetInterpolationToFlat()
    inputActor.GetProperty().SetColor(inputActorColor)
    inputActor.SetBackfaceProperty(backFace)

    decimatedMapper = vtk.vtkPolyDataMapper()
    decimatedMapper.SetInputData(decimated)

    decimatedActor = vtk.vtkActor()
    decimatedActor.SetMapper(decimatedMapper)
    decimatedActor.GetProperty().SetColor(decimatedActorColor)
    decimatedActor.GetProperty().SetInterpolationToFlat()
    decimatedActor.SetBackfaceProperty(backFace)

    # There will be one render window
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.SetSize(600, 300)

    # And one interactor
    interactor = vtk.vtkRenderWindowInteractor()
    interactor.SetRenderWindow(renderWindow)

    # Define viewport ranges
    # (xmin, ymin, xmax, ymax)
    leftViewport = [0.0, 0.0, 0.5, 1.0]
    rightViewport = [0.5, 0.0, 1.0, 1.0]

    # Setup both renderers
    leftRenderer = vtk.vtkRenderer()
    renderWindow.AddRenderer(leftRenderer)
    leftRenderer.SetViewport(leftViewport)
    leftRenderer.SetBackground((colors.GetColor3d('leftBkg')))

    rightRenderer = vtk.vtkRenderer()
    renderWindow.AddRenderer(rightRenderer)
    rightRenderer.SetViewport(rightViewport)
    rightRenderer.SetBackground((colors.GetColor3d('rightBkg')))

    # Add the sphere to the left and the cube to the right
    leftRenderer.AddActor(inputActor)
    rightRenderer.AddActor(decimatedActor)

    # Shared camera
    # Shared camera looking down the -y axis
    camera = vtk.vtkCamera()
    camera.SetPosition (0, -1, 0)
    camera.SetFocalPoint (0, 0, 0)
    camera.SetViewUp (0, 0, 1)
    camera.Elevation(30)
    camera.Azimuth(30)

    leftRenderer.SetActiveCamera(camera)
    rightRenderer.SetActiveCamera(camera)

    leftRenderer.ResetCamera()
    leftRenderer.ResetCameraClippingRange()

    renderWindow.Render()
    renderWindow.SetWindowName('Decimation')

    interactor.Start()

def ReadPolyData(file_name):
    import os
    path, extension = os.path.splitext(file_name)
    extension = extension.lower()
    if extension == ".ply":
        reader = vtk.vtkPLYReader()
        reader.SetFileName(file_name)
        reader.Update()
        poly_data = reader.GetOutput()
    elif extension == ".vtp":
        reader = vtk.vtkXMLpoly_dataReader()
        reader.SetFileName(file_name)
        reader.Update()
        poly_data = reader.GetOutput()
    elif extension == ".obj":
        reader = vtk.vtkOBJReader()
        reader.SetFileName(file_name)
        reader.Update()
        poly_data = reader.GetOutput()
    elif extension == ".stl":
        reader = vtk.vtkSTLReader()
        reader.SetFileName(file_name)
        reader.Update()
        poly_data = reader.GetOutput()
    elif extension == ".vtk":
        reader = vtk.vtkpoly_dataReader()
        reader.SetFileName(file_name)
        reader.Update()
        poly_data = reader.GetOutput()
    elif extension == ".g":
        reader = vtk.vtkBYUReader()
        reader.SetGeometryFileName(file_name)
        reader.Update()
        poly_data = reader.GetOutput()
    else:
        # Return a None if the extension is unknown.
        poly_data = None
    return poly_data

def GetSpherePD():
    """
    :return: The PolyData representation of a sphere.
    """
    source = vtk.vtkSphereSource()
    source.SetThetaResolution(30)
    source.SetPhiResolution(15)
    source.Update()
    return source.GetOutput()

if __name__ == '__main__':
    main()


import datetime
import os
import time

import xarray as xr
from app.dataprocessing.benchmark import Timer
from app.dataprocessing.datasource_interface import IDatasource
from app.dataprocessing.local.local_reader import LocalReader
from app.dataprocessing.remote.opendap_access_cas import OpendapAccessCAS
from app.datastructures.datastructure_interface import INode, IStructure, get_bounds
from app.datastructures.n_dimensional.kd_tree import KDTree
from app.datastructures.three_dimensional.octree import Octree
from app.datastructures.two_dimensional.quad_tree import QuadTree
from dotenv import load_dotenv
from sympy import im

class CachedDS:
    def __init__(self, node: INode):
        self.ds = node.ds
        self.bounds = get_bounds(node.ds)
        self.time_stamp = datetime.datetime.now()
        self.resolution = node.resolution

    def __str__(self) -> str:
        return f"\tBounds:{self.bounds}\n\tCreated:{self.time_stamp}\n\tResolution:{self.resolution:.2f}"

class DataHandler:
    """
    Defines the data source and selects proper data structure
    """

    def __init__(self) -> None:
        self.ds = None  # xarray.Dataset

        self.data_source: IDatasource = None

        self.data_structure: IStructure = None

        self.max_chunk_size = 50

        self.on_demand_data = False

        self.custom_rules = None

        self.cache: list[CachedDS] = []

    def set_max_chunk_size(self, chunk_size):
        self.max_chunk_size = chunk_size

    def get_cache(self):
        return self.cache

    def set_custom_rules(self, custom_rules):
        self.custom_rules = custom_rules

    def set_opendap_cas(
        self,
        cas_url,
        ds_url,
        username,
        password,
        file_size=None,
        constraints=None,
        struct=None,
    ):
        self.on_demand_data = True

        if username == None or password == None:
            print("please save credentials to .env")

        self.data_source = OpendapAccessCAS(
            username,
            password,
            ds_url,
            cas_url,
            file_size_MB=file_size,
            constraints=constraints,
        )

        self.ds = self.data_source.get_dataset()
        with Timer("Creating data structure"):
            self.data_structure = self.__set_data_structure(struct)

    def set_local_netcdf_reader(self, file_path, constraints=None, struct=None):
        self.data_source = LocalReader(file_path, constraints)

        with Timer("Loading dataset"):
            self.ds = self.data_source.get_dataset()
        with Timer("Creating data structure"):
            self.data_structure = self.__set_data_structure(struct)

    def get_inital_netcdf(self):
        ds, bounds, node = self.data_structure.get_initial_dataset()

        file_name = "tmp/nc/data_" + str(time.time()) + ".nc"  # TODO: revisit.

        ds.to_netcdf(file_name)

        if self.on_demand_data:
            self.__node_stream_to_local_src(node, file_name)

        return file_name

    def get_initial_ds(self):
        ds, bounds, node = self.data_structure.get_initial_dataset()

        return ds, bounds, node

    def request_data_netcdf(self, bounds, return_xr_chunk=False, fit_bounds=False):
        ds, bounds, node = self.data_structure.request_data_single_chunk(
            bounds, fit_bounds=fit_bounds
        )

        file_name = "tmp/nc/data_" + str(time.time())[-5:] + ".nc"  # TODO: revisit.

        ds.to_netcdf(file_name)

        if self.on_demand_data and fit_bounds == False:
            self.__node_stream_to_local_src(node, file_name)

        if return_xr_chunk:
            return file_name, bounds, node
        else:
            return file_name

    def get_file_size_MB(self, file_path):
        return os.path.getsize(file_path) / (1024 * 1024)

    def get_node_resolution(self, node):
        return self.data_structure.get_node_resolution(node) * 100

    def get_node_spatial_resolution(self, node) -> dict:
        return self.data_structure.get_node_spatial_resolution(node)

    def get_full_xr_ds(self) -> xr.Dataset:
        return self.data_structure.ds

    def __node_stream_to_local_src(self, node, file_path):
        # store cache in list

        node.ds = xr.open_dataset(file_path)
        self.cache.append(CachedDS(node))

    def __set_data_structure(self, custom):
        if custom:
            if custom == "KDTree":
                return KDTree(
                    self.ds,
                    full_file_size=self.data_source.get_file_size_MB(),
                    max_chunk_size=self.max_chunk_size,
                    custom_rules=self.custom_rules,
                )

        ds_dims = self.__get_num_dimensions()
        if ds_dims == 2:
            return QuadTree(
                self.ds, self.data_source.get_file_size_MB(), self.max_chunk_size
            )
        elif ds_dims == 3:
            return Octree(
                self.ds, self.data_source.get_file_size_MB(), self.max_chunk_size
            )
        elif ds_dims > 3:
            return KDTree(
                self.ds,
                full_file_size=self.data_source.get_file_size_MB(),
                max_chunk_size=self.max_chunk_size,
                custom_rules=self.custom_rules,
            )
        else:
            raise Exception("DataHandler: unsupported number of dimensions")

    def __get_num_dimensions(self):
        return len(self.ds.dims)


"""
Count the number of occurrences of each character and return it as a (list of tuples) in order of appearance.
For empty output return (an empty list).

Consult the solution set-up for the exact data structure implementation depending on your language.

Example:

ordered_count("abracadabra") == [('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d', 1)]
"""

# def ordered_count(inp):
#     def count(inp, i):
#         return (i, inp.count(i))
#     [count(inp, n) for n in inp]

# [l.append((n, inp.count(n))) for n in inp if ]

def ordered_count(inp):
    l = []
    for n in inp:
        if (n, inp.count(n)) not in l:
            l.append((n, inp.count(n)))
    return l

inp = "nlkugiluhpouywo8eyrp hv wg vcyewqgirdyqb"

print(ordered_count(inp))

tasks_from_codewars/7kyu/Ordered Count of Characters.py


from setuptools import setup, find_packages
from os import path
from io import open

here = path.abspath(path.dirname(__file__))
reqs = []

with open(path.join(here, "README.md"), encoding="utf-8") as f:
    long_description = f.read()

with open(path.join(here, "requirements.txt"), encoding="utf-8") as f:
    read_lines = f.readlines()
    reqs = [each.strip() for each in read_lines]

setup(
    name="decimaljs",
    version="1.0.4",
    description="An arbitrary-precision Decimal type for JavaScript to Python",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/kirankotari/decimaljs",
    author="Kiran Kumar Kotari",
    author_email="kirankotari@live.com",
    install_requires=reqs,
    classifiers=[
        "Development Status :: 5 - Production/Stable",
        "Intended Audience :: Developers",
        "Topic :: Software Development :: Build Tools",
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python :: 2.7",
        "Programming Language :: Python :: 3.5",
        "Programming Language :: Python :: 3.6",
        "Programming Language :: Python :: 3.7",
    ],
    keywords="decimal.js decimal decimaljs",
    packages=find_packages(where=".", exclude=["tests"]),
    include_package_data=True,
)


#!/usr/bin/python3

from .config_utils import get_base_config
from .crypto_utils import hash_file
from .file_utils import profile_url_file, clean_up
from .filter_utils import filter_url_list
from .log_utils import get_module_logger
from .plugin_utils import load_plugins
from .viper_utils import upload_to_viper
from .virus_total import get_urls_for_ip, get_class_for_hash
from threading import Thread

import numpy
import os
import sys

CDIR = os.path.dirname(os.path.realpath(__file__))
ROOTDIR = os.path.abspath(os.path.join(CDIR, os.pardir))
BASECONFIG = get_base_config(ROOTDIR)
LOGGING = get_module_logger(__name__)

def get_malware_urls():
    """Produce a list of malware URLs from daily updated feeds.

    Returns:
    - result: (type: MalwareUrl list) filtered malware URL list.
    """
    url_list = []
    return_list = []

    malware_host_list = get_plugin_malware_hosts()

    if len(malware_host_list) > 0:
        for host_object in malware_host_list:
            host_url_list = get_urls_for_ip(host_object.address, host_object.source)

            if len(host_url_list) > 0:
                return_list.extend(host_url_list)

    malware_url_list = get_plugin_malware_urls()

    if len(malware_url_list) > 0:
        return_list.extend(malware_url_list)

    return filter_url_list(return_list)

def get_plugin_malware_hosts():
    """Produce a list of malware hosts.

    Returns:
    - host_list: (type: MalwareHost list) list of malware hosts.
    """
    host_list = []

    LOGGING.info('Loading malware host plugins...')

    for plugin in load_plugins():
        try:
            if 'malware-host' in plugin.TYPES and plugin.DISABLED == False:
                LOGGING.info('Running plugin: {0}'.format(plugin.NAME))
                plugin_list = plugin.get_malwarehost_list()

                if len(plugin_list) > 0:
                    host_list.extend(plugin_list)

        except Exception as e:
            LOGGING.error('Problem running plugin. Aborting task.')
            LOGGING.exception(sys.exc_info())
            LOGGING.exception(type(e))
            LOGGING.exception(e.args)
            LOGGING.exception(e)

    return host_list

def get_plugin_malware_urls():
    """Produce a list of malware URLs from regularly updated feeds.

    Returns:
    - result: (type: MalwareUrl list) filtered malware URL list.
    """
    url_list = []
    return_list = []

    LOGGING.info('Loading malware URL plugins...')

    for plugin in load_plugins():
        try:
            if 'malware-url' in plugin.TYPES and plugin.DISABLED == False:
                LOGGING.info('Running plugin: {0}'.format(plugin.NAME))
                plugin_list = plugin.get_malwareurl_list()

                if len(plugin_list) > 0:
                    for url_object in plugin_list:
                        if url_object.url not in url_list:
                            return_list.append(url_object)
                            url_list.append(url_object.url)

        except Exception as e:
            LOGGING.error('Problem running plugin. Aborting task.')
            LOGGING.exception(sys.exc_info())
            LOGGING.exception(type(e))
            LOGGING.exception(e.args)
            LOGGING.exception(e)

    return return_list

def process_malware_list(url_list):
    """Fetch and store all malware URLs in a list.

    Params:
    - url_list: (type: MalwareUrl list) malware URL list.
    """
    for mal_url in url_list:
        tmp_file_path = profile_url_file(mal_url.url)

        if not tmp_file_path:
            LOGGING.warning('URL: {0} did not successfully download. Continuing...'.format(mal_url.url))
            continue

        try:
            if BASECONFIG.tag_samples:
                file_hash = hash_file(tmp_file_path)
                mal_class = get_class_for_hash(file_hash)

                if mal_class:
                    upload_to_viper(mal_url, tmp_file_path, mal_class)

                else:
                    LOGGING.warning('Failed to classify file. Tag will be generic.')
                    upload_to_viper(mal_url, tmp_file_path)

            else:
                upload_to_viper(mal_url, tmp_file_path)

        except Exception as e:
            LOGGING.error('Problem preparing submission to Viper. Aborting task.')
            LOGGING.exception(sys.exc_info())
            LOGGING.exception(type(e))
            LOGGING.exception(e.args)
            LOGGING.exception(e)

        clean_up(tmp_file_path)

def queue_malware_list(full_list):
    """Partition and process a malware URL list.

    Params:
    - full_list: (type: MalwareUrl list) filtered malware URL list.
    """
    url_lists = numpy.array_split(full_list, BASECONFIG.malware_workers)

    worker_threads = []

    for url_list in url_lists:
        LOGGING.info('Queueing {0} items...'.format(len(url_list)))

        list_thread = Thread(target=process_malware_list, args=[url_list])
        worker_threads.append(list_thread)

    for worker_thread in worker_threads:
        worker_thread.start()

    try:
        LOGGING.info('Spawning {0} ph0neutria spiders. Press CTRL+C to terminate.'.format(BASECONFIG.malware_workers))

        for worker_thread in worker_threads:
            worker_thread.join()

    except KeyboardInterrupt:
        LOGGING.warning('Mother spider received Ctrl+C. Killing babies.')

        for worker_thread in worker_threads:
            worker_thread.terminate()
            worker_thread.join()


import math
from re import L
import cv2
import mediapipe as mp
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from moviepy.editor import VideoFileClip

mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_pose = mp.solutions.pose

subjectpath = '/Volumes/Transcend/data/2020醒吾華橋 科技部/DT006-f/'
videopath = subjectpath + 'DT006 t1 前測 側.MP4'
folderpath = 'DT006_t1_S_1_1/'
txtpath = subjectpath + folderpath + 'log.txt'

cap = cv2.VideoCapture(videopath)
clip = VideoFileClip(videopath)

width =cap.get(3)
height =cap.get(4)
TotalFrame = cap.get(cv2.CAP_PROP_FRAME_COUNT)

framelist = []
RAnklePosX = []
LAnklePosX = []

with mp_pose.Pose(
    min_detection_confidence=0.5,
    min_tracking_confidence=0.5) as pose:
    while(True):
        success, image = cap.read()
        if not success:
            continue
        image.flags.writeable = False
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        results = pose.process(image)
        # Draw the pose annotation on the image.
        image.flags.writeable = False
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        frame = cap.get(cv2.CAP_PROP_POS_FRAMES)
        framelist.append(frame)
        if int(frame) == 1 and results.pose_landmarks:
            firstX = width - results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_ANKLE].x * width #以右邊界為0
        else:
            firstX = 5000 #使默認左邊為0
        if firstX < 2500 and results.pose_landmarks:
            LankleX = width - results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_ANKLE].x * width #以右邊界為0
            RankleX = width - results.pose_landmarks.landmark[mp_pose.PoseLandmark.RIGHT_ANKLE].x * width
        elif results.pose_landmarks:
            LankleX = results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_ANKLE].x * width #以左邊界為0
            RankleX = results.pose_landmarks.landmark[mp_pose.PoseLandmark.RIGHT_ANKLE].x * width
        else:
            LankleX = 0
            RankleX = 0
        LAnklePosX.append(LankleX)
        RAnklePosX.append(RankleX)

        if cv2.waitKey(5) & int(frame) == int(TotalFrame):
            break

Amp = 30 #Arithmetic mean parameter
LAm_X = []
RAm_X = []
Lcal_X = 0
Rcal_X = 0
for i in range(Amp):
    Lcal_X = Lcal_X + LAnklePosX[i]
    Rcal_X = Rcal_X + RAnklePosX[i]
for i in range(Amp, len(LAnklePosX)):
    LAm_X.append(Lcal_X / Amp)
    RAm_X.append(Rcal_X / Amp)
    Lcal_X = Lcal_X - LAnklePosX[i-Amp]
    Lcal_X = Lcal_X + LAnklePosX[i]
    Rcal_X = Rcal_X - RAnklePosX[i-Amp]
    Rcal_X = Rcal_X + RAnklePosX[i]

Am_framelist = framelist[Amp:]

plt.figure()
plt.plot(Am_framelist, RAm_X)
plt.plot(Am_framelist, LAm_X)
plt.savefig(subjectpath + folderpath + 'ankle_trajectory.png')

Ldx_list = [0]
Rdx_list = [0]
for i in range(1,len(Am_framelist)):
    Rdx = RAm_X[i]-RAm_X[i-1]
    Ldx = LAm_X[i]-LAm_X[i-1]
    Rdx_list.append(Rdx)
    Ldx_list.append(Ldx)
plt.figure()
plt.plot(Am_framelist, Rdx_list)
plt.plot(Am_framelist, Ldx_list)
plt.savefig(subjectpath + folderpath + 'delta_ankle.png')

#腳踝
Rslice_frame = []
Lslice_frame = []
label = []

# Rmaxframe = 1
# Rmax = 0
# for i in range(1, 85):
#     if Rdx_list[i] > Rmax:
#         Rmax = Rdx_list[i]
#         Rmaxframe = i
# Rslice_frame.append(Am_framelist[Rmaxframe])
# Lmaxframe = 1
# Lmax = 0
# for i in range(1, 85):
#     if Ldx_list[i] > Lmax:
#         Lmax = Ldx_list[i]
#         Lmaxframe = i
# Lslice_frame.append(Am_framelist[Lmaxframe])

for i in range(85,len(Am_framelist)-85):
    Rhighest = 1
    Lhighest = 1
    for j in range(1,85):
        if RAm_X[i] <= 1000 or Rdx_list[i] <= Rdx_list[i+j] or Rdx_list[i] <= Rdx_list[i-j]:
            Rhighest = 0
            break
    if Rhighest == 1:
        Rslice_frame.append(Am_framelist[i])

    for k in range(1,85):
        if LAm_X[i] <= 1000 or Ldx_list[i] <= Ldx_list[i+k] or Ldx_list[i] <= Ldx_list[i-k]:
            Lhighest = 0
            break
    if Lhighest == 1:
        Lslice_frame.append(Am_framelist[i])
Rmaxframe = len(Am_framelist)-85
Rmax = 0
for i in range(len(Am_framelist)-85, len(Am_framelist)):
    if Rdx_list[i] > Rmax:
        Rmax = Rdx_list[i]
        Rmaxframe = i
Rslice_frame.append(Am_framelist[Rmaxframe])

Lmaxframe = len(Am_framelist)-85
Lmax = 0
for i in range(len(Am_framelist)-85, len(Am_framelist)):
    if Ldx_list[i] > Lmax:
        Lmax = Ldx_list[i]
        Lmaxframe = i
Lslice_frame.append(Am_framelist[Lmaxframe])
print("Slice frame calculated by the right ankle:", Rslice_frame)
print("Slice frame calculated by the left ankle::", Lslice_frame)

#若左右frame差正負100內，取先發生的
Kslice_frame = []
i=0
j=0

while i < len(Lslice_frame) and j < len(Rslice_frame):
    if abs(Lslice_frame[i] - Rslice_frame[j]) <= 100:
        if Lslice_frame[i] <= Rslice_frame[j]:
            Kslice_frame.append(Lslice_frame[i])
            label.append("L")
        else:
            Kslice_frame.append(Rslice_frame[j])
            label.append("R")
        i += 1
        j += 1
    elif Lslice_frame[i] < Rslice_frame[j]:
        Kslice_frame.append(Lslice_frame[i])
        label.append("L")
        i += 1
    else:
        Kslice_frame.append(Rslice_frame[j])
        label.append("R")
        j += 1

print("total slice frame:", Kslice_frame)

with open(txtpath, 'w') as f:
    f.write(f"Slice frame calculated by the right ankle: {Rslice_frame}\n")
    f.write(f"Slice frame calculated by the left ankle: {Lslice_frame}\n")
    f.write(f"Total sliced frame: {Kslice_frame}\n")

fps=clip.fps
n=1
b=1
l=1
r=1
for i in range(len(Kslice_frame)):
    start_frame = Kslice_frame[i]- 40
    if start_frame + 85 <= TotalFrame:
        end_frame = start_frame + 85
    else:
        end_frame = TotalFrame
    start_time = start_frame / fps
    print("start",start_time)
    end_time = end_frame / fps
    print("end",end_time)
    clip.subclip(start_time, end_time).write_videofile(subjectpath + folderpath + f'{n}.mp4')
    n+=1
    # if i <= 3:
    #     clip.subclip(start_time, end_time).write_videofile(subjectpath + folderpath + f'B{b}.mp4')
    #     b+=1
    # elif label[i] == "L":
    #     clip.subclip(start_time, end_time).write_videofile(subjectpath + folderpath + f'L{l}.mp4')
    #     l+=1
    # elif label[i] == "R":
    #     clip.subclip(start_time, end_time).write_videofile(subjectpath + folderpath + f'R{r}.mp4')
    #     r+=1


import unittest
import parlai.utils.testing as testing_utils

class TestAlice(unittest.TestCase):
    def test_alice_runs(self):
        """
        Test that the ALICE agent is stable over time.
        """
        valid, test = testing_utils.eval_model(dict(task='convai2', model='alice'))
        self.assertEqual(valid['f1'], 0.01397)

if __name__ == '__main__':
    unittest.main()


import fresh_tomatoes
import media
import requests
import json
import config

youtube_suffix = config.youtube_key
youtube_prefix = 'https://www.youtube.com/watch?v='

# Movie list -- Here you can add and subtract movies as your tastes change

movie_list = ["There Will Be Blood", "The Life Aquatic", "Unforgiven",
              "Gladiator", "About Time", "The 'Burbs"]

movies = []

def get_info(video):
    """Fetches movie info from Open Movie Database"""

    # Request data from Youtube

    youtube = requests.get('https://www.googleapis.com/youtube/v3/search?part=s'
                           'nippet&q=' + video + 'trailer&maxResults=1&key=' +
                           youtube_suffix, timeout=20)
    youtube_str = youtube.text
    youtube_dict = json.loads(youtube_str)
    video_id = youtube_dict['items'][0]['id']['videoId']
    video_url = youtube_prefix + video_id

    # Request data from OMDB

    result = requests.get('http://www.omdbapi.com/?t=' + video + '&y=&plot='
                          'short&r=json', timeout=20)
    resp_str = result.text

    # Convert data into a python dictionary
    # http://stackoverflow.com/questions/12788217/extract-single-value-from-json-response-python

    resp_dict = json.loads(resp_str)

    trailer = video_url
    title = resp_dict["Title"]
    poster = resp_dict["Poster"]
    release = resp_dict["Released"]
    rating = resp_dict["Rated"]
    runtime = resp_dict["Runtime"]
    genre = resp_dict["Genre"]
    director = resp_dict["Director"]
    plot = resp_dict["Plot"]
    actors = resp_dict["Actors"]

    movies.append(media.Movie(trailer, title, poster, release, rating, runtime,
                              genre, director, plot, actors))

# Create movie instances and add them to the movies list

for movie in movie_list:
    get_info(movie)

fresh_tomatoes.open_movies_page(movies)


# -*- coding: utf-8 -*-

from odoo import api, fields, models

class reader(models.Model):
    _name = 'rfid.reader'
    _description = 'RFID Reader'

    name = fields.Char(string="Reader Name")
    alias = fields.Char(string="Reader Alias")
    mac_address = fields.Char(string="MAC Address")

    tag_reads = fields.One2many(
        comodel_name="rfid.reads.tag",
        inverse_name="reader"
    )

    heartbeat_reads = fields.One2many(
        comodel_name="rfid.reads.heartbeat",
        inverse_name="reader"
    )

class tag_read(models.Model):
    _name = 'rfid.reads.tag'
    _description = "RFID Tag Reads"

    name = fields.Char()

    reader = fields.Many2one(
        string="Reader",
        comodel_name="rfid.reader",
        ondelete="set null"
    )

    reader_alias = fields.Char(related='reader.alias', string="Reader Alias")

    epc = fields.Text(
        string="Electronic Product Code",
        help="""The Tag Electronic Product Code (EPC) Data field"""
        )

    pc = fields.Text(
        string="Protocol Control",
        help="""Protocol Control (PC) Bits"""
    )

    antennaPort = fields.Integer(
        string="Antenna",
        help="""Antenna ID on which Tag is read."""
    )

    peakRssi = fields.Integer(
        string="Peak RSSI",
        help="""Peak Received Signal Strength Indicator (RSSI)"""
    )

    seenCount = fields.Integer(
        string="Seen Count",
        help="""The number of times the Tag is read (in-case of periodic reporting)."""
    )

    timeStamp = fields.Datetime(
        string="Time Stamp",
        help="""Timestamp when Tag is seen."""
    )

    phase = fields.Float(
        string="Phase",
        help="""Phase information reported by the reader when the tag is seen."""
    )

    channelIndex = fields.Integer(
        string="Channel Index",
        help="""Index of the first channel when the tag is seen."""
    )

class heartbeat_read(models.Model):
    _name = 'rfid.reads.heartbeat'
    _description = "RFID Heartbeat"

    reader = fields.Many2one(
        string="Reader",
        comodel_name="rfid.reader",
        ondelete="set null"
    )

    timeStamp = fields.Datetime(
        string="Time Stamp",
        help="""Timestamp when Tag is seen."""
    )


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('ventas', '0075_auto_20161016_1522'),
    ]

    operations = [
        migrations.RenameField(
            model_name='comanda',
            old_name='tiempo_estimado_elaboracion',
            new_name='tiempo_estimado_procesamiento',
        ),
        migrations.AlterField(
            model_name='comanda',
            name='area_solicitante',
            field=models.ForeignKey(related_name='area_solicitante_comanda', verbose_name=b'Area Solicitante', to='bar.Sector'),
        ),
        migrations.AlterField(
            model_name='comanda',
            name='fecha_hora_pedido_comanda',
            field=models.DateTimeField(verbose_name=b'Fecha/hora Comanda'),
        ),
        migrations.AlterField(
            model_name='comanda',
            name='numero_pedido',
            field=models.ForeignKey(verbose_name=b'Numero de Pedido', to='ventas.Pedido'),
        ),
        migrations.AlterField(
            model_name='comanda',
            name='producto_a_entregar',
            field=models.ForeignKey(verbose_name=b'Producto Solicitado', to='stock.ProductoVenta'),
        ),
    ]


import numpy as np

# Things that were changed from the original:
# - Reformatted code and variable names to conform with PEP8
# - Added legal header

# This file contains routines from Lisbon Machine Learning summer school.
# The code is freely distributed under a MIT license. https://github.com/LxMLS/lxmls-toolkit/

def parse_proj(scores, gold=None):
    # pylint: disable=too-many-locals
    """
    Parse using Eisner's algorithm.
    """
    nr, nc = np.shape(scores)
    if nr != nc:
        raise ValueError("scores must be a squared matrix with nw+1 rows")

    N = nr - 1  # Number of words (excluding root).

    # Initialize CKY table.
    complete = np.zeros([N + 1, N + 1, 2])  # s, t, direction (right=1).
    incomplete = np.zeros([N + 1, N + 1, 2])  # s, t, direction (right=1).
    complete_backtrack = -np.ones([N + 1, N + 1, 2], dtype=int)  # s, t, direction (right=1).
    incomplete_backtrack = -np.ones([N + 1, N + 1, 2], dtype=int)  # s, t, direction (right=1).

    incomplete[0, :, 0] -= np.inf

    # Loop from smaller items to larger items.
    for k in range(1, N + 1):
        for s in range(N - k + 1):
            t = s + k

            # First, create incomplete items.
            # left tree
            incomplete_vals0 = (
                complete[s, s:t, 1]
                + complete[(s + 1) : (t + 1), t, 0]
                + scores[t, s]
                + (0.0 if gold is not None and gold[s] == t else 1.0)
            )
            incomplete[s, t, 0] = np.max(incomplete_vals0)
            incomplete_backtrack[s, t, 0] = s + np.argmax(incomplete_vals0)
            # right tree
            incomplete_vals1 = (
                complete[s, s:t, 1]
                + complete[(s + 1) : (t + 1), t, 0]
                + scores[s, t]
                + (0.0 if gold is not None and gold[t] == s else 1.0)
            )
            incomplete[s, t, 1] = np.max(incomplete_vals1)
            incomplete_backtrack[s, t, 1] = s + np.argmax(incomplete_vals1)

            # Second, create complete items.
            # left tree
            complete_vals0 = complete[s, s:t, 0] + incomplete[s:t, t, 0]
            complete[s, t, 0] = np.max(complete_vals0)
            complete_backtrack[s, t, 0] = s + np.argmax(complete_vals0)
            # right tree
            complete_vals1 = incomplete[s, (s + 1) : (t + 1), 1] + complete[(s + 1) : (t + 1), t, 1]
            complete[s, t, 1] = np.max(complete_vals1)
            complete_backtrack[s, t, 1] = s + 1 + np.argmax(complete_vals1)

    # value = complete[0][N][1]
    heads = [-1 for _ in range(N + 1)]  # -np.ones(N+1, dtype=int)
    _backtrack_eisner(incomplete_backtrack, complete_backtrack, 0, N, 1, 1, heads)
    value_proj = 0.0
    for m in range(1, N + 1):
        h = heads[m]
        value_proj += scores[h, m]
    return heads

# pylint: disable=too-many-arguments
def _backtrack_eisner(incomplete_backtrack, complete_backtrack, s, t, direction, complete, heads):
    """
    Backtracking step in Eisner's algorithm.
    - incomplete_backtrack is a (NW+1)-by-(NW+1) numpy array indexed by a start
     position, an end position, and a direction flag (0 means left, 1 means
     right). This array contains the arg-maxes of each step in the Eisner
     algorithm when building *incomplete* spans.
    - complete_backtrack is a (NW+1)-by-(NW+1) numpy array indexed by a start
     position, an end position, and a direction flag (0 means left, 1 means
     right). This array contains the arg-maxes of each step in the Eisner
     algorithm when building *complete* spans.
    - s is the current start of the span
    - t is the current end of the span
    - direction is 0 (left attachment) or 1 (right attachment)
    - complete is 1 if the current span is complete, and 0 otherwise
    - heads is a (NW+1)-sized numpy array of integers which is a placeholder
      for storing the head of each word.
    """
    if s == t:
        return
    if complete:
        r = complete_backtrack[s][t][direction]
        if direction == 0:
            _backtrack_eisner(incomplete_backtrack, complete_backtrack, s, r, 0, 1, heads)
            _backtrack_eisner(incomplete_backtrack, complete_backtrack, r, t, 0, 0, heads)
            return
        _backtrack_eisner(incomplete_backtrack, complete_backtrack, s, r, 1, 0, heads)
        _backtrack_eisner(incomplete_backtrack, complete_backtrack, r, t, 1, 1, heads)
        return

    r = incomplete_backtrack[s][t][direction]
    if direction == 0:
        heads[s] = t
        _backtrack_eisner(incomplete_backtrack, complete_backtrack, s, r, 1, 1, heads)
        _backtrack_eisner(incomplete_backtrack, complete_backtrack, r + 1, t, 0, 1, heads)
        return
    heads[t] = s
    _backtrack_eisner(incomplete_backtrack, complete_backtrack, s, r, 1, 1, heads)
    _backtrack_eisner(incomplete_backtrack, complete_backtrack, r + 1, t, 0, 1, heads)
    return


import pandas as pd
import numpy as np

from sklearn import preprocessing

i = 5

train_mice = '../data/five_imps/train_mice_%d.csv' % i
all_mice = '../data/five_imps/all_mice_%d.csv' % i

train_hot = '../data/five_imps/train_mice_hot_%d.csv' % i
test_hot = '../data/five_imps/test_mice_hot_%d.csv' % i

# Read dataframe
X = pd.read_csv(train_mice)
X_all = pd.read_csv(all_mice)

y = np.ravel(X[['y']])
X.drop('y', axis=1, inplace=True)

# Specify categorical columns
is_cat  = ['workclass', 'edu',  'married', 'occupation', 'relationship',
           'race', 'sex', 'country']

# Onehot hot encoding
#X_enc =  pd.get_dummies(X, columns=is_cat)
X_all_enc = pd.get_dummies(X_all, columns=is_cat)

X_train_enc = X_all_enc.ix[0:len(X)-1, :]
X_train_enc['y'] = y
X_train_enc.to_csv(train_hot, index=False)

X_test_enc = X_all_enc.ix[(len(X)):len(X_all), :]
X_test_enc.to_csv(test_hot, index=False)


import celery
import logging
import requests

from django.conf import settings

from ..models import Job
from ..helper import data_job_for_applicant

JOB_URL_CREATE = 'api/admin/'
JOB_URL_DETAIL = 'api/admin/{}/'

logger = logging.getLogger('celery-task')

class ApplicantJobMixin:
    host = settings.EXOLEVER_HOST + settings.SERVICE_JOBS_HOST
    headers = {'USERNAME': settings.AUTH_SECRET_KEY}

    def get_url(self, uuid=None):
        if uuid is None:
            url = self.host + JOB_URL_CREATE
        else:
            url = self.host + JOB_URL_DETAIL.format(uuid.__str__())

        return url

    def get_job(self, *args, **kwargs):
        try:
            return Job.objects.get(id=kwargs.get('job_id'))
        except Job.DoesNotExist:
            logger.error('Job does not exist')
            raise Exception()

class ApplicantJobCreate(ApplicantJobMixin, celery.Task):
    name = 'ApplicantJobCreate'

    def run(self, *args, **kwargs):
        if settings.POPULATOR_MODE:
            return

        job = self.get_job(*args, **kwargs)
        url = self.get_url()
        data = data_job_for_applicant(job.applicant)

        try:
            response = requests.post(url, json=data, headers=self.headers)
            assert response.status_code == requests.codes.created
            uuid = response.json().get('uuid')
            job.uuid = uuid
            job.save()
        except AssertionError:
            message = 'Exception: {}-{}'.format(response.content, url)
            logger.error(message)
            self.retry(countdown=120, max_retries=20)

class ApplicantJobUpdate(ApplicantJobMixin, celery.Task):
    name = 'ApplicantJobUpdate'

    def run(self, *args, **kwargs):
        if settings.POPULATOR_MODE:
            return

        job = self.get_job(*args, **kwargs)
        url = self.get_url(uuid=job.uuid.__str__())
        data = data_job_for_applicant(job.applicant)

        try:
            response = requests.put(url, json=data, headers=self.headers)
            assert response.status_code == requests.codes.ok
        except AssertionError:
            message = 'Exception: {}-{}'.format(response.content, url)
            logger.error(message)
            self.retry(countdown=120, max_retries=20)

class ApplicantJobDelete(ApplicantJobMixin, celery.Task):
    name = 'ApplicantJobDelete'

    def run(self, *args, **kwargs):
        uuid = kwargs.get('job_uuid', None)

        if settings.POPULATOR_MODE or uuid is None:
            return

        url = self.get_url(uuid=kwargs.get('job_uuid'))

        try:
            response = requests.delete(url, headers=self.headers)
            assert response.status_code == requests.codes.no_content
        except AssertionError:
            message = 'Exception: {}-{}'.format(response.content, url)
            logger.error(message)
            self.retry(countdown=120, max_retries=20)


# Demander à l'utilisateur de saisir une phrase
phrase_utilisateur = input("Veuillez saisir une phrase : ")

# Initialiser le compteur de mots
nombre_mots = 0
mot = False

# Parcourir chaque caractère de la phrase
for caractere in phrase_utilisateur:
    if caractere != ' ':
        # Si le caractère n'est pas un espace, c'est un mot
        if not mot:
            mot = True
            nombre_mots += 1
    else:
        # Si le caractère est un espace, marquer la fin du mot
        mot = False

# Gérer le cas où la phrase se termine par un mot
if phrase_utilisateur and phrase_utilisateur[-1] != ' ':
    nombre_mots += 1

# Convertir la phrase en majuscules
phrase_majuscules = ""
for caractere in phrase_utilisateur:
    if 'a' <= caractere <= 'z':
        lettre_majuscule = chr(ord(caractere) - 32)
        phrase_majuscules += lettre_majuscule
    else:
        phrase_majuscules += caractere

# Convertir la phrase en minuscules
phrase_minuscules = ""
for caractere in phrase_utilisateur:
    if 'A' <= caractere <= 'Z':
        lettre_minuscule = chr(ord(caractere) + 32)
        phrase_minuscules += lettre_minuscule
    else:
        phrase_minuscules += caractere

# Afficher les résultats
print(f"La phrase en majuscules : {phrase_majuscules}")
print(f"La phrase en minuscules : {phrase_minuscules}")
print(f"Le nombre de mots dans la phrase : {nombre_mots}")


from tkinter import *
from tkinter import ttk
from tkinter.filedialog import askdirectory
from tkinter.filedialog import askopenfilename
from PIL import Image

# =================================================================================

def convertion():
    u_path = str(t1.get())
    d_path = str(t2.get())
    name = str(t3.get())
    p_type = str(types.get())

    im = Image.open(rf"{t1.get()}")
    im.save(rf"{t2.get()}\{t3.get()}{types.get()}")

def clear():
    t1.delete(0,'end')
    t2.delete(0,'end')
    t3.delete(0,'end')
    types.delete(0,'end')

def brsfnc():
    location = askdirectory()
    if t2.get() != "":
        t2.delete(0,'end')
        t2.insert(0,location)
    else:
        t2.insert(0,location)

def brspic():
    f_loc = askopenfilename()
    if t1.get() !="":
        t1.delete(0,'end')
        t1.insert(0,f_loc)
    else:
        t1.insert(0,f_loc)

# =================================================================================

win = Tk()
win.title("Image Converter")
win.iconbitmap(r"C:\Users\Public\Pictures\Sample Pictures\Treetog-Junior-Monitor-desktop.ico")
win.geometry("900x500")
win.maxsize(900,500)
win.minsize(900,500)
win['bg']="#83a2f2"

heading = Label(win,text="Image Converter",font=("verdana",35,"bold"),bg="#83a2f2",fg="gold")
heading.place(x=150,y=10)

l1 = Label(win,text="Enter The Image Path",font=("verdana",15,"bold")).grid(row=0,column=0,padx=20,pady=120)
t1 = Entry(win,width=25,borderwidth=5,font=(("verdana",15,"bold")))
t1.grid(row=0,column=1,pady=120)

brs = Button(win,text="Browse File",font=("verdan",8,"bold"),borderwidth=5,width=10,command=brspic)
brs.place(x=660,y=120)

l2 = Label(win,text="Enter Saving Path",font=("verdana",15,"bold")).place(x=20,y=200)
t2 = Entry(win,width=25,borderwidth=5,font=(("verdana",15,"bold")))
t2.place(x=293,y=200)

brsbtn = Button(win,text="Browse Folder",font=("verdan",8,"bold"),borderwidth=5,width=14,command=brsfnc)
brsbtn.place(x=660,y=200)

l3 = Label(win,text="Enter Saving Name",font=("verdana",15,"bold")).place(x=20,y=280)
t3 = Entry(win,width=20,borderwidth=5,font=(("verdana",10,"bold")))
t3.place(x=293,y=280)

combo = ttk.Label(win, text="Enter image type",font=("verdana",14,"bold"))
combo.place(x=20,y=360)

types = ttk.Combobox(win,width = 27,font=("verdana",12,"bold"))
types['values']=('.jgp','.png','.ico','.jpeg','.gif')
types.place(x=293,y=360)
types.current()

b1 = Button(win,text="Convert",font=("verdan",12,"bold"),borderwidth=5,width=12,command=convertion)
b1.place(x=730,y=370)

b1 = Button(win,text="Clear",font=("verdan",12,"bold"),borderwidth=5,width=12,command=clear)
b1.place(x=730,y=440)

win.mainloop()


from data_preparation import Preprocessing
import pickle

import tensorflow as tf
import os
from tensorflow.python.framework import ops
from sklearn.metrics.classification import accuracy_score
from sklearn.metrics import precision_recall_fscore_support
import warnings

warnings.filterwarnings("ignore")
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
ops.reset_default_graph()

checkpoints_dir = 'checkpoints/1519576475' # Change this path based on the output from '$ python3 train.py' script "Model saved in: checkpoints/1517781236"

data_dir = 'data/' # Data directory containing 'data.csv' file with 'SentimentText' and 'Sentiment\'. Intermediate files will automatically be stored here'
stopwords_file = 'data/stopwords.txt' # Path to stopwords file. If stopwords_file is None, no stopwords will be used'
sequence_len = None # Maximum sequence length
n_samples= None # Set n_samples=None to use the whole dataset
test_size = 0.2
batch_size = 100 #Batch size
random_state = 0 # Random state used for data splitting. Default is 0

if checkpoints_dir is None:
    raise ValueError('Please, a valid checkpoints directory is required (--checkpoints_dir <file name>)')

# Load data
data_lstm = Preprocessing(data_dir=data_dir,
                 stopwords_file=stopwords_file,
                 sequence_len=sequence_len,
                 n_samples=n_samples,
                 test_size=test_size,
                 val_samples=batch_size,
                 random_state=random_state,
                 ensure_preprocessed=True)

# Import graph and evaluate the model using test data
original_text, x_test, y_test, test_seq_len = data_lstm.get_test_data(original_text=True)
graph = tf.Graph()
with graph.as_default():
    sess = tf.Session()

    # Import graph and restore its weights
    print('Restoring graph ...')
    saver = tf.train.import_meta_graph("{}/model.ckpt.meta".format(checkpoints_dir))
    saver.restore(sess, ("{}/model.ckpt".format(checkpoints_dir)))

    # Recover input/output tensors
    input = graph.get_operation_by_name('input').outputs[0]
    target = graph.get_operation_by_name('target').outputs[0]
    seq_len = graph.get_operation_by_name('lengths').outputs[0]
    dropout_keep_prob = graph.get_operation_by_name('dropout_keep_prob').outputs[0]
    predict = graph.get_operation_by_name('final_layer/softmax/predictions').outputs[0]
    accuracy = graph.get_operation_by_name('accuracy/accuracy').outputs[0]

    # Perform prediction
    pred, acc = sess.run([predict, accuracy],
                         feed_dict={input: x_test,
                                    target: y_test,
                                    seq_len: test_seq_len,
                                    dropout_keep_prob: 1})
    print("Evaluation done.")

# Print results
print('\nAccuracy: {0:.4f}\n'.format(acc))
for i in range(100):
    print('Sample: {0}'.format(original_text[i]))
    print('Predicted sentiment: [{0:.4f}, {1:.4f}]'.format(pred[i, 0], pred[i, 1]))
    print('Real sentiment: {0}\n'.format(y_test[i]))


"""
test cli module
"""
import subprocess
from typing import List, Tuple

def capture(command: List[str]) -> Tuple[bytes, bytes, int]:
    proc = subprocess.Popen(
        command,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    out, err = proc.communicate()
    return out, err, proc.returncode

def test_cli() -> None:
    """Test cli module"""
    command = ["sdwaddle"]
    out, err, exitcode = capture(command)
    assert exitcode == 0


class Solution(object):
    def removeComments(self, source):
        """
        :type source: List[str]
        :rtype: List[str]
        """
        inblock = False
        result = []

        remain = []
        for line in source:
        	blockStart = -2
        	blockEnd = -2
        	for i,c in enumerate(line):
        		if not inblock:
        			remain.append(c)
        			if i > 0 and i-2 != blockEnd and line[i-1] == "/" and c == "/":  # //
        				remain.pop()
        				remain.pop()
        				break

        			if i > 0 and i-2 != blockEnd and line[i-1] == "/" and c == "*":   # /*
        				inblock = True
        				remain.pop()
        				remain.pop()
        				blockStart = i-1

        		elif i > 0 and i-2 != blockStart and line[i-1] == "*" and c == "/":  # */
        			inblock = False
        			blockEnd = i -1

        	if not inblock:
        		if len(remain):
        			result.append("".join(remain))
        			remain = []
        return result

#Remove Comments
#https://leetcode.com/problems/remove-comments/description/


import re
import pandas as pd
pd.set_option('display.max_rows', None)
from pathlib import Path

file_path = Path('texts/wf_anthology.txt')

# Read in the text file as a string
with open(file_path, 'r') as f:
    text = f.read()

# define the regular expression pattern to match the section title, author's name, and introductory paragraph
pattern = r'^##(.+?)\n\n(.+?)\n\n((?:.|\n)*?)\n\n'

# process each section
new_text = ''
for match in re.finditer(pattern, text, re.MULTILINE | re.DOTALL):
    # extract the relevant information
    title = match.group(1)
    author = match.group(2)
    intro = match.group(3)

    # remove the unwanted text
    new_intro = re.sub(r'^' + author + r'(?:.|\n)*?\n\n', '', intro)

    # concatenate the modified section to the new text
    new_text += '##' + title + '\n\n' + author + '\n\n' + new_intro

with open('wf_anthology_manually_tidied_v1.txt', 'w') as outfile:
    outfile.write(''.join(new_text))

'''
# Split the text by the '##' delimiter and extract the text content
blocks = []
for block in re.split(r'(?m)^##', text)[1:]:
    # Extract the story name and author name
    match = re.match(r'(.*?)\n\n(.*?)\n\n', block, re.DOTALL)
    if match:
        story_name = match.group(1)
        author_name = match.group(2).replace('\n', '')
    else:
        story_name = ''
        author_name = ''

    # Extract only the text content
    text_content = re.sub(r'(.*?)\n\n(.*?)\n', '', block, re.DOTALL).strip().replace('\n', '')

    # Add the block to the list of dictionaries
    blocks.append({'story_name': story_name, 'author_name': author_name, 'text': text_content})

# Create a pandas DataFrame from the list of dictionaries
df = pd.DataFrame(blocks)
print(df)
'''


# def sum_of_intervals(intervals):

#     sum_of_length = []

#     for char in intervals:
#         length_of_intervals = char[1]-char[0]

#     print(sum_of_length)

# def sum_of_intervals(intervals):
#     # Sort the intervals
#     sorted_intervals = sorted(intervals)

#     # Initialize variables to keep track of the current interval and the total sum
#     total_sum = 0

#     for interval in sorted_intervals:

#         length_of_intervals = interval[1]-interval[0]
#         total_sum += length_of_intervals

#     return total_sum

# result = sum_of_intervals([(6, 10), (1, 9)])
# print(result)

def merge_overlapping_intervals(intervals):
    # Sort the intervals
    sorted_intervals = sorted(intervals)

    # Initialize variables to keep track of the merged intervals
    merged_intervals = [sorted_intervals[0]]

    # Iterate through the sorted intervals and merge overlapping ones
    for interval in sorted_intervals[1:]: # this semantic helps to loop on the list from index 1
        if merged_intervals[-1][1] >= interval[0]:
            # print(merged_intervals[-1][1])  # last value in the interval
            # print(merged_intervals[-1])     # interval
            # print(merged_intervals[-1][0])  # first value in the interval

            # Merge
            merged_intervals[-1] = (merged_intervals[-1][0], max(merged_intervals[-1][1], interval[1]))
        else:
            # Add non-overlapping interval
            merged_intervals.append(interval)

    return merged_intervals

def sum_of_intervals(intervals):
    # Merge overlapping intervals
    merged_intervals = merge_overlapping_intervals(intervals)
    print(merged_intervals)

    total_sum = 0

    # Calculate the total sum of the merged intervals (first solution)
    # total_sum = sum(end - start for start, end in merged_intervals)

    # second solution
    for interval in merged_intervals:

        length_of_intervals = interval[1]-interval[0]
        total_sum += length_of_intervals

    return total_sum

result = sum_of_intervals([(1, 4), (1, 6)])
print(result)


# Задача 26:  Напишите программу, которая на вход принимает два числа A и B,
# и возводит число А в целую степень B с помощью рекурсии.

import my_functions

my_functions.show_header("Программа возводит введенное число в введеную степень")

num = my_functions.get_number("Введите число:")

degree = my_functions.get_number("Введите степень:")

print(f'\nЧисло {num} в степени {degree} = {my_functions.Degree_Recursion(num, degree)}')

my_functions.end()


import pandas as pd
import numpy as np
import joblib
import pickle
import warnings
import os
from data.make_dataset import preprocess_train_df

warnings.filterwarnings("ignore")

def make_categorical_dataset(processed_dfs, proteins_df):
    """
    Turns the train_updrs.csv into a categorical dataset
    based on the ratings:
    updrs 1 categorical ratings: 10 and below is mild, 11 to 21 is moderate, 22 and above is severe
    updrs 2 categorical ratings: 12 and below is mild, 13 to 29 is moderate, 30 and above is severe
    updrs 3 categorical ratings: 32 and below is mild, 33 to 58 is moderate, 59 and above is severe
    updrs 4 categorical ratings: 4 and below is mild, 5 to 12 is moderate, 13 and above is severe

    Args:
        processed_df: dataframe with one row per visit_month containing all of the protein and peptide columns
        proteins_df: dataframe with the UniProt column and the peptide columns

    Returns:
        categorical_df: dataframe with the updrs values as categorical values based on the ratings as well as the proteins and peptides values
    """

    # read the data
    updrs1_df = processed_dfs["updrs_1"]
    updrs2_df = processed_dfs["updrs_2"]
    updrs3_df = processed_dfs["updrs_3"]
    updrs4_df = processed_dfs["updrs_4"]

    protein_list = list(proteins_df["UniProt"].unique())

    # list of columns for information
    info_cols = [
        "visit_id",
        "patient_id",
        "visit_month",
        "updrs_1",
        "updrs_2",
        "updrs_3",
        "updrs_4",
        "kfold",
    ]

    # protein and peptide columns
    peptide_list = [
        col
        for col in updrs1_df.columns
        if col not in protein_list and col not in info_cols
    ]
    prot_pep_cols = protein_list + peptide_list

    # add a column for the number of proteins and peptides present
    updrs1_df["num_prot_pep"] = updrs1_df[prot_pep_cols].sum(axis=1)
    updrs2_df["num_prot_pep"] = updrs2_df[prot_pep_cols].sum(axis=1)
    updrs3_df["num_prot_pep"] = updrs3_df[prot_pep_cols].sum(axis=1)
    updrs4_df["num_prot_pep"] = updrs4_df[prot_pep_cols].sum(axis=1)
    # number of proteins
    updrs1_df["num_prot"] = updrs1_df[protein_list].sum(axis=1)
    updrs2_df["num_prot"] = updrs2_df[protein_list].sum(axis=1)
    updrs3_df["num_prot"] = updrs3_df[protein_list].sum(axis=1)
    updrs4_df["num_prot"] = updrs4_df[protein_list].sum(axis=1)
    # number of peptides
    updrs1_df["num_pept"] = updrs1_df[peptide_list].sum(axis=1)
    updrs2_df["num_pept"] = updrs2_df[peptide_list].sum(axis=1)
    updrs3_df["num_pept"] = updrs3_df[peptide_list].sum(axis=1)
    updrs4_df["num_pept"] = updrs4_df[peptide_list].sum(axis=1)

    # apply the categorical ratings
    updrs1_df["updrs_1_cat"] = np.where(
        updrs1_df["updrs_1"] <= 10,
        "mild",
        np.where(updrs1_df["updrs_1"] <= 21, "moderate", "severe"),
    )
    updrs2_df["updrs_2_cat"] = np.where(
        updrs2_df["updrs_2"] <= 12,
        "mild",
        np.where(updrs2_df["updrs_2"] <= 29, "moderate", "severe"),
    )
    updrs3_df["updrs_3_cat"] = np.where(
        updrs3_df["updrs_3"] <= 32,
        "mild",
        np.where(updrs3_df["updrs_3"] <= 58, "moderate", "severe"),
    )
    updrs4_df["updrs_4_cat"] = np.where(
        updrs4_df["updrs_4"] <= 4,
        "mild",
        np.where(updrs4_df["updrs_4"] <= 12, "moderate", "severe"),
    )

    categorical_dfs = {
        "updrs_1": updrs1_df,
        "updrs_2": updrs2_df,
        "updrs_3": updrs3_df,
        "updrs_4": updrs4_df,
    }

    return categorical_dfs

def add_med_data(clin_df, updrs_df):
    """
    Takes in the separate upd23b_clinical_state_on_medication data.
    Creates dummy columns and adds them to the updrs dataset for the clinical medication data

    Args:
        clin_df: dataframe with the upd23b_clinical_state_on_medication column and visit_id column
        updrs_df: dataframe with the all of the protein, peptide, visit_id, visit_month, and patient_id columns

    Returns:
        updrs_df: the dataframe with the updrs_1_cat_preds column added
    """

    clin_df["upd23b_clinical_state_on_medication"] = clin_df[
        "upd23b_clinical_state_on_medication"
    ].fillna("Unknown")

    # get dummies for on_medication column
    clin_df_dummies = pd.get_dummies(
        clin_df, columns=["upd23b_clinical_state_on_medication"], drop_first=True
    )

    clin_df_dummies = clin_df_dummies[
        [
            "visit_id",
            "upd23b_clinical_state_on_medication_On",
            "upd23b_clinical_state_on_medication_Unknown",
        ]
    ]

    # merge the updrs data with the clinical data for dummy columns
    updrs_df = pd.merge(updrs_df, clin_df_dummies, on="visit_id")

    return updrs_df

def predict_updrs1(df):
    """Predict the updrs_1_cat column for the provided dataframe using saved CatBoost Classifier model.

    Args:
        df: the dataframe with the updrs_1_cat column to be predicted

    Returns:
        df: the dataframe with the updrs_1_cat_preds column added
    """
    # Load the saved model
    model_path = os.path.join(
        "..", "models", "catboost_updrs_1_model_hyperopt_smote.sav"
    )
    model = joblib.load(model_path)

    # Make predictions on the test data
    X = df.drop(columns=["updrs_1_cat", "kfold", "visit_id", "patient_id", "updrs_1"])

    try:
        preds = model.predict_proba(X)[:, 1]
    except AttributeError as e:
        print(f"Error: {e}")

    # use threshold of 0.46 to get the predicted updrs_1_cat
    updrs_1_cat_preds = np.where(preds >= 0.46, 1, 0)

    # add the column to the dataframe
    df["updrs_1_cat_preds"] = updrs_1_cat_preds

    return df

def predict_updrs2(df):
    """Predict the updrs_2_cat column for the provided dataframe using saved CatBoost Classifier model.

    Args:
        df: the dataframe with the updrs_2_cat column to be predicted

    Returns:
        df: the dataframe with the updrs_2_cat_preds column added
    """
    model_path = os.path.join(
        "..", "models", "catboost_updrs_2_model_hyperopt_smote_meds.sav"
    )
    model = joblib.load(model_path)

    # Make predictions on the test data
    X = df.drop(columns=["updrs_2_cat", "kfold", "visit_id", "patient_id", "updrs_2"])

    try:
        preds = model.predict_proba(X)[:, 1]
    except AttributeError as e:
        print(f"Error: {e}")

    # use threshold of 0.22 to get the predicted updrs_2_cat
    updrs_2_cat_preds = np.where(preds >= 0.22, 1, 0)

    # add the column to the dataframe
    df["updrs_2_cat_preds"] = updrs_2_cat_preds

    return df

def predict_updrs3(df):
    """Predict the updrs_3_cat column for the provided dataframe using saved LightGBM Classifier model.

    Args:
        df: the dataframe with the updrs_3_cat column to be predicted

    Returns:
        df: the dataframe with the updrs_3_cat_preds column added
    """
    # Load the saved model
    filename = os.path.join(
        "..", "models", "lgboost_updrs_3_model_hyperopt_smote_meds.sav"
    )

    # model = pickle.load(open(filename, "rb"))
    model = joblib.load(filename)

    # Make predictions on the test data
    X = df.drop(columns=["updrs_3_cat", "kfold", "visit_id", "patient_id", "updrs_3"])

    try:
        preds = model.predict_proba(X, verbose=-100)[:, 1]
    except AttributeError as e:
        print(f"Error: {e}")

    # use threshold of 0.28 to get the predicted updrs_3_cat
    updrs_3_cat_preds = np.where(preds >= 0.28, 1, 0)

    # add the column to the dataframe
    df["updrs_3_cat_preds"] = updrs_3_cat_preds

    return df

if __name__ == "__main__":
    # read in the data
    train_clin_path = os.path.join("..", "data", "raw", "train_clinical_data.csv")
    train_prot_path = os.path.join("..", "data", "raw", "train_proteins.csv")
    train_pep_path = os.path.join("..", "data", "raw", "train_peptides.csv")

    train_clin_df = pd.read_csv(train_clin_path)
    train_prot_df = pd.read_csv(train_prot_path)
    train_pep_df = pd.read_csv(train_pep_path)

    proc_dfs = preprocess_train_df(
        train_clin_df, train_prot_df, train_pep_df, save_data=False
    )

    # convert to only 12 month data since that was what was used for training
    for updrs in ["updrs_1", "updrs_2", "updrs_3", "updrs_4"]:
        temp_df = proc_dfs[updrs]
        proc_dfs[updrs] = temp_df[temp_df["visit_month"] <= 12]

    cat_dfs = make_categorical_dataset(proc_dfs, train_prot_df)

    cat_dfs["updrs_2"] = add_med_data(train_clin_df, cat_dfs["updrs_2"])
    cat_dfs["updrs_3"] = add_med_data(train_clin_df, cat_dfs["updrs_3"])

    pred_updrs1_df = predict_updrs1(cat_dfs["updrs_1"])
    pred_updrs2_df = predict_updrs2(cat_dfs["updrs_2"])
    pred_updrs3_df = predict_updrs3(cat_dfs["updrs_3"])

    # combine prediction columns into one dataframe
    updrs_preds = pd.merge(
        pred_updrs1_df,
        pred_updrs2_df[["visit_id", "updrs_2_cat", "updrs_2_cat_preds"]],
        on="visit_id",
    )

    updrs_preds = pd.merge(
        updrs_preds,
        pred_updrs3_df[["visit_id", "updrs_3_cat", "updrs_3_cat_preds"]],
        on="visit_id",
        how="left",
    )

    # save the dataframe as a csv
    file_path = os.path.join("..", "data", "predictions", "full_updrs_preds.csv")
    updrs_preds.to_csv(file_path, index=False)


import requests

from starwhale import Link, Image, Point, dataset, Polygon, MIMEType  # noqa: F401
from starwhale.utils.retry import http_retry

PATH_ROOT = "https://starwhale-examples.oss-cn-beijing.aliyuncs.com/dataset/cityscapes"
ANNO_PATH = "disparity/train"
DATA_PATH_LEFT = "leftImg8bit/train"
DATA_PATH_RIGHT = "rightImg8bit/train"
SUFFIX_MASK = "_disparity.png"
SUFFIX_DATA_LEFT = "_leftImg8bit.png"
SUFFIX_DATA_RIGHT = "_rightImg8bit.png"

@http_retry
def request_link_json(anno_link):
    return requests.get(anno_link, timeout=10).json()

def mask_image(_name, dir_name):
    return Image(
        display_name=_name,
        mime_type=MIMEType.PNG,
        as_mask=True,
        link=Link(uri=f"{PATH_ROOT}/{ANNO_PATH}/{dir_name}/{_name}"),
    )

def build_ds():
    ds = dataset("cityscapes_disparity")
    ds.info["baseline"] = 22
    ds.info["homepage"] = "https://www.cityscapes-dataset.com"
    tree = request_link_json(f"{PATH_ROOT}/{ANNO_PATH}/tree.json")
    for d in tree:
        if d["type"] != "directory":
            continue
        dir_name = d["name"]
        for f in d["contents"]:
            if f["type"] != "file":
                continue
            _name = str(f["name"])
            if not _name.endswith(SUFFIX_MASK):
                continue
            disparity_mask = mask_image(_name, dir_name)
            name = _name.replace(SUFFIX_MASK, "")
            right_image = Image(
                display_name=name,
                link=Link(
                    uri=f"{PATH_ROOT}/{DATA_PATH_RIGHT}/{dir_name}/{name}{SUFFIX_DATA_RIGHT}"
                ),
                mime_type=MIMEType.JPEG,
            )
            left_image = Image(
                display_name=name,
                link=Link(
                    uri=f"{PATH_ROOT}/{DATA_PATH_LEFT}/{dir_name}/{name}{SUFFIX_DATA_LEFT}"
                ),
                mime_type=MIMEType.JPEG,
            )
            ds.append(
                {
                    "left_image_8bit": left_image,
                    "right_image_8bit": right_image,
                    "disparity_mask": disparity_mask,
                }
            )

    ds.commit()
    load_ds = dataset(ds.uri)
    print(load_ds.info)
    ds.close()

if __name__ == "__main__":
    build_ds()


import time

t_start_script = time.time()
print(__name__)

import matplotlib.pyplot as plt
import numpy as np

a = np.random.randn(1000 * 100* 100)
print('start_time and prepare data:', time.time() - t_start_script)
print(a.shape)
print(sys.argv)
#   >>>
#print('11123',a)
if __name__ == '__main__':
    t0 = time.time()
    # plt.plot([1, 2, 3, 4, 5, 1])
    plt.hist(a, bins=20,color='red')
    plt.xlabel('haha')
    plt.ylabel('y is lalala')
    print(time.time() - t0)
    print('full time', time.time() - t_start_script)
    plt.show()


import argparse

from . import completion_helpers

class ArgumentParser(argparse.ArgumentParser):

    def enable_print_header(self):
        self.add_argument(
            '-q', action='store_true',
            help="Suppresses printing of headers when multiple tasks are " +
                 "being examined"
        )

    def task_argument(self, optional=False):
        kwargs = {
            "default": "",
            "type": str,
            "help": "ID of the task. May match multiple tasks (or all)"
        }
        if optional:
            kwargs["nargs"] = "?"

        self.add_argument('task', **kwargs).completer = completion_helpers.task

    def file_argument(self):
        self.add_argument(
            'file', nargs="*", default=["stdout"],
            help="Path to the file inside the task's sandbox."
        ).completer = completion_helpers.file

    def path_argument(self):
        self.add_argument(
            'path', type=str, nargs="?", default="",
            help="""Path to view."""
        ).completer = completion_helpers.file


import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder

def get_data_splits(X, Y, train_size=0.8):
    """This function splits the whole dataset into train, test and validation sets.

    Args:
        X (pd.DataFrame): DataFrame containing feature values of data points of shape (num_samples,num_features)
        Y (pd.DataFrame): DataFrame containing labels for samples of shape (num_samples,)
        train_size (float, optional): size of the training set in (0,1), Defaults to 0.8.

    Returns:
        tuple: tuple containing X_train, Y_train, X_val, Y_val, X_test, Y_test
    """
    X_train, X_, Y_train, Y_ = train_test_split(X, Y, train_size=train_size, stratify=Y)
    X_val, X_test, Y_val, Y_test = train_test_split(X_, Y_, train_size=0.5, stratify=Y_)
    return X_train, Y_train, X_val, Y_val, X_test, Y_test

def rename_columns(df):
    """This function renames the columns of features DataFrame into cat_ and num_ styles.

    Args:
        df (pd.DataFrame): A DataFrame containing features as columns and samples as rows

    Returns:
        pd.DataFrame: A DataFrame with renamed columns.
    """
    categorical_cols = ["0", "1", "2", "3", "13"]
    numerical_cols = ["4", "5", "6", "7", "8", "9", "10", "11", "12"]
    new_names = []
    for col_name in df.columns.astype(str).values:
        if col_name in numerical_cols:
            df[col_name] = pd.to_numeric(df[col_name])
            new_names.append((col_name, "num_" + col_name))
        elif col_name in categorical_cols:
            new_names.append((col_name, "cat_" + col_name))
        else:  # pragma: no cover, other data
            new_names.append((col_name, col_name))
    df.rename(columns=dict(new_names), inplace=True)
    return df

def preprocess(X_df, Y_df=None, label_col="18", enc=None):
    """This function preprocess the features and labels DataFrames by encoding categorical features and relabeling. If Y_df is not None, normal samples get label 1 and anomalies get label -1.

    Args:
        X_df (pd.DataFrame): DataFrame containing features with renamed columns.
        Y_df (pd.DataFrame, optional): DataFrame containing the labels for samples in X_df. Defaults to None.
        label_col (str, optional): Name of the label column in Y_df. Defaults to "18".
        enc (sklearn.preprocessing.OneHotEncoder, optional): Fitted OneHotEncoder, a new encoder will be fit to the data if none is given. Defaults to None.

    Returns:
        tuple: df_new, Y_df, enc, the encoded features, the labels, and the OneHotEncoder
    """
    X_df = X_df.reset_index(drop=True)
    if Y_df is not None:
        Y_df = Y_df.reset_index(drop=True)
    if not enc:
        enc = OneHotEncoder(handle_unknown="ignore")
        enc.fit(X_df.loc[:, ["cat_" in i for i in X_df.columns]])
    num_cat_features = enc.transform(X_df.loc[:, ["cat_" in i for i in X_df.columns]]).toarray()
    df_catnum = pd.DataFrame(num_cat_features)
    df_catnum = df_catnum.add_prefix("catnum_")
    df_new = pd.concat([X_df, df_catnum], axis=1)
    if Y_df is not None:
        filter_clear = Y_df[label_col] == 1
        filter_infected = Y_df[label_col] < 0
        Y_df[label_col][filter_clear] = 1
        Y_df[label_col][filter_infected] = -1
    return df_new, Y_df, enc

def get_preprocessed_train(X_df, Y_df, label_col="18"):
    """This function prepares the dataset for training. Returns only normal samples.

    Args:
        X_df (pd.DataFrame): A DataFrame containing features for samples of shape (num_samples, num_features)
        Y_df (pd.DataFrame): A DataFrame containing labels for data samples of shape (n_samples,)
        label_col (str, optional): Name of the label column in Y_df. Defaults to "18"

    Returns:
        tuple: X_train_num, Y_train_clear, numerical_cols, ohe_enc: features and labels of normal samples together with names of the columns for training and the OneHotEncoder used in preprocessing
    """
    count_norm = X_df[Y_df[label_col] == 1].shape[0]
    count_anomaly = X_df[Y_df[label_col] != 1].shape[0]
    print("normal:", count_norm, "anomalies:", count_anomaly)
    X_df = rename_columns(X_df)
    X_df, Y_df, ohe_enc = preprocess(X_df, Y_df)
    # select numerical features
    numerical_cols = X_df.columns.to_numpy()[["num_" in i for i in X_df.columns]]
    X_train_clear = X_df[Y_df[label_col] == 1]
    Y_train_clear = Y_df[Y_df[label_col] == 1]
    X_train_num = X_train_clear[numerical_cols]
    return X_train_num, Y_train_clear, numerical_cols, ohe_enc

def get_test(X_df, enc, Y_df=None):
    """This function works similar to get_preprocessed_train, but prepares the test dataset for evaluation or inference purposes.

    Args:
        (pd.DataFrame): A DataFrame containing features for samples of shape (num_samples, num_features)
        enc (sklearn.preprocessing.OneHotEncoder): A fitted OneHotEncoder for transforming features
        Y_df (pd.DataFrame, optional): A DataFrame containing labels for data samples of shape (n_samples,), is none for inference purposes. Defaults to None.

    Returns:
        tuple: X_df, Y_df, preprocessed features and labels
    """
    X_df = rename_columns(X_df)
    X_df, Y_df, _ = preprocess(X_df, Y_df, enc=enc)
    numerical_cols = X_df.columns.to_numpy()[["num_" in i for i in X_df.columns]]
    X_df = X_df[numerical_cols]
    return X_df, Y_df


import pathlib
from typing import Optional

import essentia.standard as es
import numpy as np
import pyrubberband as pyrb
from madmom.features.downbeats import DBNDownBeatTrackingProcessor, RNNDownBeatProcessor

from mixer.logger import logger

SAMPLE_RATE = 44100  # Sample rate fixed for essentia

class TrackProcessor:
    SAMPLE_RATE = SAMPLE_RATE

    def __init__(self, file_path: str, name: Optional[str] = None) -> None:
        """
        Parameters
        ----------
        file_path : str
            absolute or relative location of track audio file
        name : Optional[str]
            name to give to track if not present in file path
        """
        self._file_path = pathlib.Path(file_path)
        if name is None:
            self._name = self._file_path.stem
        else:
            self._name = name

        self._audio = np.array([])
        self._bpm = None
        self._downbeats = np.array([])

    def __str__(self):
        return self._name

    @property
    def audio(self) -> np.ndarray:
        return self._audio

    @property
    def downbeats(self) -> np.ndarray:
        return self._downbeats

    @property
    def bpm(self) -> Optional[float]:
        return self._bpm

    @bpm.setter
    def bpm(self, bpm: float) -> np.ndarray:
        """
        Time stretch audio file to increase BPM to target

        Parameters
        ----------
        bpm : float
            intended BPM of audio

        Returns
        -------
        np.ndarray
            time-stretched audio
        """
        if self._bpm is None:
            self.calculate_bpm()

        assert self._bpm is not None

        stretch_factor = bpm / self._bpm
        self._audio = pyrb.time_stretch(
            self._audio, SAMPLE_RATE, stretch_factor
        ).astype(np.float32)
        self.calculate_bpm()

        logger.info(f"Tempo for {self} set to {round(self._bpm, 2)}")

        return self._audio

    def load(self, path: Optional[str] = None) -> np.ndarray:
        """
        Load an audio file from a given path.

        Parameters
        ----------
        path : Optional[str]
            local path to audio file
            if None, file_path attribute value used

        Returns
        -------
        np.ndarray
            mono representation of audio file
        """
        if path is None:
            path = str(self._file_path.resolve())

        loader = es.MonoLoader(filename=path, sampleRate=SAMPLE_RATE)
        self._audio = loader()

        logger.info(f"Loaded audio for {self}")

        return self._audio

    def crop(self, offset: int, length: int) -> None:
        """
        Crop track using number of downbeats.

        Parameters
        ----------
        offset : int
            number of downbeats into original audio to crop from
        length : int
            number of downbeats that new audio will contain
        """
        if self.downbeats.size == 0:
            self.calculate_downbeats()

        start_sample = int(self._downbeats[offset] * SAMPLE_RATE)
        end_sample = int(self._downbeats[offset + length] * SAMPLE_RATE)

        self._audio = self._audio[start_sample : end_sample + 1]

        logger.info(
            f"Cropped {self} audio between downbeats {offset} and {offset + length}"
        )

    def calculate_bpm(self) -> float:
        """
        Determine BPM for audio using essentia

        Returns
        -------
        bpm : float
            tempo of audio file
        """
        rhythm_extractor = es.RhythmExtractor2013(method="degara")
        self._bpm, _, _, _, _ = rhythm_extractor(self._audio)

        assert self._bpm is not None

        logger.info(f"Calculated tempo for {self} at {round(self._bpm, 2)}")

        return self._bpm

    def calculate_downbeats(self) -> None:
        """
        Use madmom downbeat tracking to estimate downbeat time points for audio file.
        """
        proc = DBNDownBeatTrackingProcessor(beats_per_bar=[3, 4], fps=100)
        act = RNNDownBeatProcessor()(self._audio)
        proc_res = proc(act)

        self._downbeats = proc_res[proc_res[:, 1] == 1, 0].astype(np.float32)

        logger.info(f"Calculated downbeats for {self}")

class TrackGroupProcessor:
    def __init__(self) -> None:
        self._tracks: list[TrackProcessor] = []
        self._bpm: Optional[float] = None

    @property
    def bpm(self) -> Optional[float]:
        return self._bpm

    @property
    def tracks(self) -> list[TrackProcessor]:
        return self._tracks

    def add_track(self, track: TrackProcessor) -> None:
        """
        Add a track to the track group.

        Parameters
        ----------
        track : TrackProcessor
            track to be added
        """
        self._tracks.append(track)
        self.calculate_bpm()

    def calculate_bpm(self):
        """
        Calculate average bpm of current tracks in group.
        """
        track_bpms = [track.bpm for track in self._tracks]
        self._bpm = sum(track_bpms) / len(track_bpms)


#!/usr/bin/env python

import csv
import gzip
import os
import re
import sys

from pyproj import Transformer

csv.field_size_limit(sys.maxsize)

AMOUNT_REGEX = re.compile('Both Installment[\s\S]+?\$([\d,\.]+)')

# California Zone 3
# https://epsg.io/2227
transformer = Transformer.from_crs(2227, 4326)

def get_val(row, key):
    val = row[key].strip()
    if not val:
        return 0
    return float(val)

with open('/home/ian/Downloads/Santa_Cruz_Assessor_Parcels.csv') as f_in, \
     open('./parse_output.csv', 'w') as f_out:
    reader = csv.DictReader(f_in)
    fieldnames = ['address', 'apn', 'longitude', 'latitude', 'tax', 'county']
    writer = csv.DictWriter(f_out, fieldnames=fieldnames)

    count = 0
    for row in reader:
        count += 1
        if count % 1000 == 0:
            print(count, '...')

        apn = row['APN']
        address = row['SITEADD']

        try:
            x_coord = float(row['XCOORD'])
            y_coord = float(row['YCOORD'])
        except:
            print('-> bad coords')
            continue

        centroid = transformer.transform(x_coord, y_coord)

        print(count, apn, address, centroid)

        output_path = '/home/ian/code/prop13/scrapers/santa_cruz/scrape_output/%s.html' % (apn)
        if not os.path.exists(output_path):
            print('-> no scraped file')
            continue

        try:
            with gzip.open(output_path, 'rt') as f_in:
                html = f_in.read()
        except:
            print('--> bad file')
            continue

        amount = -1
        try:
            amount_str = AMOUNT_REGEX.search(html).group(1).replace(',', '')
            amount = float(amount_str)
        except:
            print('--> Could not parse float', amount_str)
            continue

        print('--> Paid', amount)

        writer.writerow({
            'address': address,
            'apn': apn,
            'latitude': centroid[0],
            'longitude': centroid[1],
            'tax': amount,
            'county': 'SCZ',
        })