import os
import json
import random
import numpy as np
import pandas as pd
import logging
import torch.utils.data

from PIL import Image, ImageOps
from typing import Dict, Tuple, List
from omegaconf import DictConfig
from classifier.preprocess import get_crop_from_bbox, Compose

FORMATS = (".jpeg", ".jpg", ".jp2", ".png", ".tiff", ".jfif", ".bmp", ".webp", ".heic")

class GestureDataset(torch.utils.data.Dataset):
    """
    Custom Dataset for gesture classification pipeline
    """

    def __init__(
            self,
            is_train: bool,
            conf: DictConfig,
            transform: Compose = None,
            is_test: bool = False
    ) -> None:

        """
        Custom Dataset for gesture classification pipeline

        Parameters
        ----------
        is_train : bool
            True if collect train dataset else False
        is_test: Bool
            For metrics calculation on test set
        conf : DictConfig
            Config with training params
        transform : Compose
            Compose of transforms
        """
        self.conf = conf
        self.transform = transform
        self.is_train = is_train

        self.labels = {label: num for (label, num) in
                       zip(self.conf.dataset.targets, range(len(self.conf.dataset.targets)))}

        self.leading_hand = {"right": 0, "left": 1}

        subset = self.conf.dataset.get("subset", None)

        self.annotations = self.__read_annotations(subset)

        users = self.annotations["user_id"].unique()
        users = sorted(users)
        random.Random(self.conf.random_state).shuffle(users)

        train_users = users[:int(len(users) * 0.8)]
        val_users = users[int(len(users) * 0.8):]

        self.annotations = self.annotations.copy()

        if not is_test:
            if is_train:
                self.annotations = self.annotations[self.annotations["user_id"].isin(train_users)]
            else:
                self.annotations = self.annotations[self.annotations["user_id"].isin(val_users)]

    @staticmethod
    def __get_files_from_dir(pth: str, extns: Tuple, subset: int = None) -> List:
        """
        Get list of files from dir according to extensions(extns)

        Parameters
        ----------
        pth : str
            Path ot dir
        extns: Tuple
            Set of file extensions
        subset : int
            Length of subset for each target
        """
        if not os.path.exists(pth):
            logging.warning(f"Dataset directory doesn't exist {pth}")
            return []
        files = [f for f in os.listdir(pth) if f.endswith(extns)]
        if subset is not None:
            files = files[:subset]
        return files

    def __read_annotations(self, subset: int = None) -> pd.DataFrame:
        """
        Read annotations json

        Parameters
        ----------
        subset : int
            Length of subset for each target
        """
        exists_images = []
        annotations_all = pd.DataFrame()
        path_to_json = os.path.expanduser(self.conf.dataset.annotations)
        for target in self.conf.dataset.targets:
            target_tsv = os.path.join(path_to_json, f"{target}.json")
            if os.path.exists(target_tsv):
                json_annotation = json.load(open(
                    os.path.join(path_to_json, f"{target}.json")
                ))

                json_annotation = [dict(annotation, **{"name": f"{name}.jpg"}) for name, annotation in zip(
                    json_annotation, json_annotation.values()
                )]

                annotation = pd.DataFrame(json_annotation)

                annotation["target"] = target
                annotations_all = pd.concat([annotations_all, annotation], ignore_index=True)
                exists_images.extend(
                    self.__get_files_from_dir(os.path.join(self.conf.dataset.dataset, target),
                                              FORMATS, subset))
            else:
                logging.info(f"Databse for {target} not found")

        annotations_all["exists"] = annotations_all["name"].isin(exists_images)

        return annotations_all[annotations_all["exists"]]

    def __prepare_image_target(
            self,
            target: str,
            name: str,
            bboxes: List,
            labels: List,
            leading_hand: str
    ) -> Tuple[Image.Image, str, str]:
        """
        Crop and padding image, prepare target

        Parameters
        ----------
        target : str
            Class name
        name : str
            Name of image
        bboxes : List
            List of bounding boxes [xywh]
        labels: List
            List of labels
        leading_hand : str
            Leading hand class name
        """
        image_pth = os.path.join(self.conf.dataset.dataset, target, name)

        image = Image.open(image_pth).convert("RGB")

        width, height = image.size

        choice = np.random.choice(["gesture", "no_gesture"], p=[0.7, 0.3])

        bboxes_by_class = {}

        for i, bbox in enumerate(bboxes):
            x1, y1, w, h = bbox
            bbox_abs = [x1 * width, y1 * height, (x1 + w) * width, (y1 + h) * height]
            if labels[i] == "no_gesture":
                bboxes_by_class["no_gesture"] = (bbox_abs, labels[i])
            else:
                bboxes_by_class["gesture"] = (bbox_abs, labels[i])

        if choice not in bboxes_by_class:
            choice = list(bboxes_by_class.keys())[0]

        if self.is_train:
            box_scale = np.random.uniform(low=1.0, high=2.0)
        else:
            box_scale = 1.0

        image_cropped, bbox_orig = get_crop_from_bbox(image, bboxes_by_class[choice][0], box_scale=box_scale)

        image_resized = ImageOps.pad(image_cropped, tuple(self.conf.dataset.image_size), color=(0, 0, 0))

        gesture = bboxes_by_class[choice][1]

        leading_hand_class = leading_hand
        if gesture == "no_gesture":
            leading_hand_class = "right" if leading_hand == "left" else "left"

        return image_resized, gesture, leading_hand_class

    def __len__(self) -> int:
        return self.annotations.shape[0]

    def __getitem__(self, index: int) -> Tuple[Image.Image, Dict]:
        """
        Get item from annotations

        Parameters
        ----------
        index : int
            Index of annotation item
        """
        row = self.annotations.iloc[[index]].to_dict('records')[0]

        image_resized, gesture, leading_hand = self.__prepare_image_target(
            row["target"],
            row["name"],
            row["bboxes"],
            row["labels"],
            row["leading_hand"]
        )

        label = {"gesture": self.labels[gesture],
                 "leading_hand": self.leading_hand[leading_hand]}

        if self.transform is not None:
            image_resized, label = self.transform(image_resized, label)

        return image_resized, label


"""Security methods for graphql"""

import logging

import time
from typing import Optional

from fastapi import HTTPException
from fastapi.security import HTTPBasic, HTTPAuthorizationCredentials, HTTPBearer
from jose import jwt
from starlette.requests import Request
from starlette.responses import Response
from starlette.types import Scope, Receive, Send
from starlette.websockets import WebSocket
from starlette_graphene3 import GraphQLApp

JWT_DECODE_KEY = "Change_me_please_please_please"
JWT_ALGORITHM = "HS256"

logger = logging.getLogger(__name__)

security = HTTPBasic()

def decode_jwt(token: str) -> Optional[dict]:
    try:
        decoded_token = jwt.decode(token, JWT_DECODE_KEY, algorithms=[JWT_ALGORITHM])
        return decoded_token if decoded_token["exp"] >= time.time() else None
    except Exception as exp:
        logger.error(exp)
        return None

class JWTBearer(HTTPBearer):
    def __init__(self, auto_error: bool = True):
        super(JWTBearer, self).__init__(auto_error=auto_error)

    async def __call__(self, request: Request):
        credentials: HTTPAuthorizationCredentials = await super(
            JWTBearer, self
        ).__call__(request)
        if credentials:
            if not credentials.scheme == "Bearer":
                raise HTTPException(
                    status_code=403, detail="Invalid authentication scheme."
                )
            if not JWTBearer.verify_jwt(credentials.credentials):
                raise HTTPException(
                    status_code=403, detail="Invalid token or expired token."
                )
            return credentials.credentials
        else:
            raise HTTPException(status_code=403, detail="Invalid authorization code.")

    @staticmethod
    async def validate_ws(headers):
        auth_header = headers.get("Authorization")
        if not auth_header or not isinstance(auth_header, str):
            raise HTTPException(status_code=403, detail="Invalid authorization code.")
        try:
            scheme, token = auth_header.split(" ")
        except ValueError:
            raise HTTPException(
                status_code=403, detail="Invalid authentication scheme."
            )
        if scheme != "Bearer":
            raise HTTPException(
                status_code=403, detail="Invalid authentication scheme."
            )
        if not JWTBearer.verify_jwt(token):
            raise HTTPException(
                status_code=403, detail="Invalid token or expired token."
            )
        return token

    @staticmethod
    def verify_jwt(jwtoken: str) -> bool:
        is_token_valid: bool = False

        payload = decode_jwt(jwtoken)
        if payload:
            is_token_valid = True
        return is_token_valid

class SecureGraphQLApp(GraphQLApp):
    """Class that inherits default starlette_graphene_3
    and adding security to all graphql requests
    """

    async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
        if scope["type"] == "http":
            request = Request(scope=scope, receive=receive)
            bearer = JWTBearer()
            await bearer(request=request)
            response: Optional[Response] = None
            if request.method == "POST":
                response = await self._handle_http_request(request)
            elif request.method == "GET":
                response = await self._get_on_get(request)
            if not response:
                response = Response(status_code=405)
            await response(scope, receive, send)

        elif scope["type"] == "websocket":
            headers = scope.get("headers")
            bearer = JWTBearer()
            await bearer.validate_ws(headers)
            websocket = WebSocket(scope=scope, receive=receive, send=send)
            await self._run_websocket_server(websocket)
        else:
            raise ValueError(f"Unsupported scope type: ${scope['type']}")


from flask import Flask ,render_template ,request
from werkzeug.utils import secure_filename

import Docker_Logs
import database_con

app = Flask(__name__)

@app.route('/upload')
def upoad_file():
    return render_template("upload.html")

@app.route('/uploader' , methods =['GET','POST'])
def upload_file():
    if request.method == 'POST':
        f = request.files['file']
        f.save(secure_filename(f.filename))

        return {"data":{
            "filename":f.filename
        }}

@app.route('/analiz',methods=['GET','POST'])
def Database(file_name):
    #results=database_con.SQL_request(database_con.hashing('Level.exe')) // bu çalışacak fakat ilk başta veri tabanı kurulmalı
    results=database_con.hashing('filename')
    return results

@app.route('/docker',methods=['GET','POST'])
def docker():
    #direkt çalıştır.
    logs=Docker_Logs.docker()
    return logs

if __name__ == '__main__':
    app.run()


import json

with open('../features.txt') as f:
    FEATURES = json.loads(f.read())

sites =  ['frontiersin.org','chicagotribune.com','link.springer.com',
          'aljazeera.com','instructables.com','npr.org',
          'dailymail.co.uk','csmonitor.com','baltimoresun.com','city-data.com']

for site in sites:

    feature_dict = {}
    for i in range(FEATURES['c4']['url']):
        feature_dict[i] = 0

    with open('files/'+site+'_data.json','r') as f:
        for line in f:
            data = json.loads(line)
            feature_dict[data['url_id']] += 1

    top_features = sorted(feature_dict, key=feature_dict.get, reverse=True)[:10]

    count = 0
    with open('files/'+site+'_data_filtered.json','w') as f_filtered:
        with open('files/'+site+'_data.json','r') as f:
            for line in f:
                data = json.loads(line)
                if data['url_id'] in top_features:
                    if count != 0:
                        f_filtered.write('\n'+line)
                    else:
                        f_filtered.write(line)
                    count += 1

    print(site)
    print(top_features)
    print(count)
    print()


import zipfile
from io import BytesIO
from unittest.mock import patch

import pytest
from mock_module.config import ServiceWithFilesConfig
from mock_module.resource import (
    CustomDisabledUploadFileResourceConfig,
    CustomFileResourceConfig,
    CustomRecordResourceConfig,
)
from zipstream import ZipStream

from invenio_records_resources.resources import FileResource, RecordResource
from invenio_records_resources.services import RecordService

@pytest.fixture(scope="module")
def service():
    return RecordService(ServiceWithFilesConfig)

@pytest.fixture(scope="module")
def record_resource(service):
    """Record Resource."""
    return RecordResource(CustomRecordResourceConfig, service)

@pytest.fixture(scope="module")
def file_resource(file_service):
    """File Resources."""
    return FileResource(CustomFileResourceConfig, file_service)

@pytest.fixture(scope="module")
def disabled_file_upload_resource(file_service):
    """Disabled Upload File Resource."""
    return FileResource(CustomDisabledUploadFileResourceConfig, file_service)

@pytest.fixture(scope="module")
def base_app(
    base_app, file_resource, disabled_file_upload_resource, service, file_service
):
    """Application factory fixture."""
    base_app.register_blueprint(file_resource.as_blueprint())
    base_app.register_blueprint(disabled_file_upload_resource.as_blueprint())
    registry = base_app.extensions["invenio-records-resources"].registry
    registry.register(service, service_id="mock-records")
    registry.register(file_service, service_id="mock-files")
    yield base_app

@pytest.fixture()
def input_data(input_data):
    input_data["files"] = {"enabled": True}
    return input_data

def test_files_api_flow(client, search_clear, headers, input_data, location):
    """Test record creation."""
    # Initialize a draft
    res = client.post("/mocks", headers=headers, json=input_data)
    assert res.status_code == 201
    id_ = res.json["id"]
    assert res.json["links"]["files"].endswith(f"/api/mocks/{id_}/files")

    # Initialize files upload
    res = client.post(
        f"/mocks/{id_}/files",
        headers=headers,
        json=[
            {"key": "test.pdf", "title": "Test file"},
        ],
    )
    assert res.status_code == 201
    res_file = res.json["entries"][0]
    assert res_file["key"] == "test.pdf"
    assert res_file["status"] == "pending"
    assert res_file["metadata"] == {"title": "Test file"}
    assert res_file["links"]["self"].endswith(f"/api/mocks/{id_}/files/test.pdf")
    assert res_file["links"]["content"].endswith(
        f"/api/mocks/{id_}/files/test.pdf/content"
    )
    assert res_file["links"]["commit"].endswith(
        f"/api/mocks/{id_}/files/test.pdf/commit"
    )

    # Get the file metadata
    res = client.get(f"/mocks/{id_}/files/test.pdf", headers=headers)
    assert res.status_code == 200
    assert res.json["key"] == "test.pdf"
    assert res.json["status"] == "pending"
    assert res.json["metadata"] == {"title": "Test file"}

    # Upload a file
    res = client.put(
        f"/mocks/{id_}/files/test.pdf/content",
        headers={
            "content-type": "application/octet-stream",
            "accept": "application/json",
        },
        data=BytesIO(b"testfile"),
    )
    assert res.status_code == 200
    assert res.json["status"] == "pending"

    # Commit the uploaded file
    res = client.post(f"/mocks/{id_}/files/test.pdf/commit", headers=headers)
    assert res.status_code == 200
    assert res.json["status"] == "completed"

    # Get the file metadata
    res = client.get(f"/mocks/{id_}/files/test.pdf", headers=headers)
    assert res.status_code == 200
    assert res.json["key"] == "test.pdf"
    assert res.json["status"] == "completed"
    assert res.json["metadata"] == {"title": "Test file"}
    file_size = str(res.json["size"])
    assert isinstance(res.json["size"], int), "File size not integer"

    # Read a file's content
    res = client.get(f"/mocks/{id_}/files/test.pdf/content", headers=headers)
    assert res.status_code == 200
    assert res.data == b"testfile"

    # Update file metadata
    res = client.put(
        f"/mocks/{id_}/files/test.pdf", headers=headers, json={"title": "New title"}
    )
    assert res.status_code == 200
    assert res.json["key"] == "test.pdf"
    assert res.json["status"] == "completed"
    assert res.json["metadata"] == {"title": "New title"}

    # Get all files
    res = client.get(f"/mocks/{id_}/files", headers=headers)
    assert res.status_code == 200
    assert len(res.json["entries"]) == 1
    assert res.json["entries"][0]["key"] == "test.pdf"
    assert res.json["entries"][0]["status"] == "completed"
    assert res.json["entries"][0]["metadata"] == {"title": "New title"}

    # Delete a file
    res = client.delete(f"/mocks/{id_}/files/test.pdf", headers=headers)
    assert res.status_code == 204

    # Get all files
    res = client.get(f"/mocks/{id_}/files", headers=headers)
    assert res.status_code == 200
    assert len(res.json["entries"]) == 0

def test_empty_file(client, search_clear, headers, input_data, location):
    """Test if an empty file works properly."""
    # Initialize a draft
    res = client.post("/mocks", headers=headers, json=input_data)
    assert res.status_code == 201
    id_ = res.json["id"]
    assert res.json["links"]["files"].endswith(f"/api/mocks/{id_}/files")

    # Initialize files upload
    res = client.post(
        f"/mocks/{id_}/files",
        headers=headers,
        json=[
            {"key": "empty", "title": "Zero-length test file"},
        ],
    )
    assert res.status_code == 201

    # Upload the empty file
    res = client.put(
        f"/mocks/{id_}/files/empty/content",
        headers={
            "content-type": "application/octet-stream",
            "accept": "application/json",
        },
        data=BytesIO(b""),
    )
    assert res.status_code == 200

    # Commit the uploaded file
    res = client.post(f"/mocks/{id_}/files/empty/commit", headers=headers)
    assert res.status_code == 200

    # Get the file metadata, check if everything (especially size) is present
    res = client.get(f"/mocks/{id_}/files/empty", headers=headers)
    assert res.status_code == 200
    assert res.json["key"] == "empty"
    assert res.json["checksum"] == "md5:d41d8cd98f00b204e9800998ecf8427e"
    assert res.json["size"] == 0

    # Read a file's content
    res = client.get(f"/mocks/{id_}/files/empty/content", headers=headers)
    assert res.status_code == 200
    assert res.data == b""

def test_default_preview_file(app, client, search_clear, headers, input_data, location):
    # Initialize a draft
    res = client.post("/mocks", headers=headers, json=input_data)
    assert res.status_code == 201
    id_ = res.json["id"]
    assert res.json["links"]["files"].endswith(f"/api/mocks/{id_}/files")

    # Initialize 3 file uploads
    res = client.post(
        f"/mocks/{id_}/files",
        headers=headers,
        json=[
            {"key": "f1.pdf"},
            {"key": "f2.pdf"},
            {"key": "f3.pdf"},
        ],
    )
    assert res.status_code == 201
    file_entries = res.json["entries"]
    assert len(file_entries) == 3
    assert {(f["key"], f["status"]) for f in file_entries} == {
        ("f1.pdf", "pending"),
        ("f2.pdf", "pending"),
        ("f3.pdf", "pending"),
    }
    assert res.json["default_preview"] is None

    # Upload and commit the 3 files
    for f in file_entries:
        res = client.put(
            f"/mocks/{id_}/files/{f['key']}/content",
            headers={
                "content-type": "application/octet-stream",
                "accept": "application/json",
            },
            data=BytesIO(b"testfile"),
        )
        assert res.status_code == 200
        assert res.json["status"] == "pending"

        res = client.post(f"/mocks/{id_}/files/{f['key']}/commit", headers=headers)
        assert res.status_code == 200
        assert res.json["status"] == "completed"

    # Set the default preview file
    input_data["files"]["default_preview"] = "f1.pdf"
    res = client.put(f"/mocks/{id_}", headers=headers, json=input_data)
    assert res.status_code == 200
    assert res.json["files"]["default_preview"] == "f1.pdf"

    # Change the default preview file
    input_data["files"]["default_preview"] = "f2.pdf"
    res = client.put(f"/mocks/{id_}", headers=headers, json=input_data)
    assert res.status_code == 200
    assert res.json["files"]["default_preview"] == "f2.pdf"

    # Unset the default preview file
    input_data["files"]["default_preview"] = None
    res = client.put(f"/mocks/{id_}", headers=headers, json=input_data)
    assert res.status_code == 200
    assert res.json["files"].get("default_preview") is None

    # Empty string the default preview file
    input_data["files"]["default_preview"] = ""
    res = client.put(f"/mocks/{id_}", headers=headers, json=input_data)
    assert res.status_code == 200
    assert res.json["files"].get("default_preview") is None

    # Set the default preview file
    input_data["files"]["default_preview"] = "f3.pdf"
    res = client.put(f"/mocks/{id_}", headers=headers, json=input_data)
    assert res.status_code == 200
    assert res.json["files"]["default_preview"] == "f3.pdf"

    # Delete the default preview file
    res = client.delete(f"/mocks/{id_}/files/f3.pdf", headers=headers)
    assert res.status_code == 204

    # Get all files and check default preview
    res = client.get(f"/mocks/{id_}/files", headers=headers)
    assert res.status_code == 200
    assert len(res.json["entries"]) == 2
    assert res.json["default_preview"] is None

def test_file_api_errors(client, search_clear, headers, input_data, location):
    """Test REST API errors for file management."""
    h = headers

    # Initialize a draft
    res = client.post("/mocks", headers=headers, json=input_data)
    assert res.status_code == 201
    id_ = res.json["id"]
    assert res.json["links"]["files"].endswith(f"/api/mocks/{id_}/files")

    # Initialize files upload
    # Pass an object instead of an array
    res = client.post(f"/mocks/{id_}/files", headers=headers, json={"key": "test.pdf"})
    assert res.status_code == 400

    res = client.post(
        f"/mocks/{id_}/files",
        headers=headers,
        json=[
            {"key": "test.pdf", "title": "Test file"},
        ],
    )
    assert res.status_code == 201

    # Upload a file
    res = client.put(
        f"/mocks/{id_}/files/test.pdf/content",
        headers={
            "content-type": "application/octet-stream",
            "accept": "application/json",
        },
        data=BytesIO(b"testfile"),
    )
    assert res.status_code == 200
    assert res.json["status"] == "pending"

    # Commit the uploaded file
    res = client.post(f"/mocks/{id_}/files/test.pdf/commit", headers=headers)
    assert res.status_code == 200
    assert res.json["status"] == "completed"

    # Initialize same file upload again
    res = client.post(
        f"/mocks/{id_}/files",
        headers=headers,
        json=[
            {"key": "test.pdf", "title": "Test file"},
        ],
    )
    assert res.status_code == 400

def test_disabled_upload_file_resource(
    client, search_clear, headers, input_data, location
):
    """Test file resources with disabled file upload"""

    # Initialize a draft
    res = client.post("/mocks", headers=headers, json=input_data)
    assert res.status_code == 201
    id_ = res.json["id"]

    # File manipulation api should not be exposed if allow_upload is disabled
    res = client.post(
        f"/mocks_disabled_files_upload/{id_}/files",
        headers=headers,
        json=[{"key": "test.pdf", "title": "Test file"}],
    )
    assert res.status_code == 405

    res = client.put(
        f"/mocks_disabled_files_upload/{id_}/files",
        headers=headers,
        json=[{"default_preview": "test.pdf"}],
    )
    assert res.status_code == 405

    res = client.delete(
        f"/mocks_disabled_files_upload/{id_}/files/test.pdf", headers=headers
    )
    assert res.status_code == 405

def test_disable_files_when_files_already_present_should_error(
    app, client, search_clear, headers, input_data, location
):
    # Initialize a record
    response = client.post("/mocks", headers=headers, json=input_data)
    id_ = response.json["id"]
    # Add file
    file_id = "f1.pdf"
    client.post(f"/mocks/{id_}/files", headers=headers, json=[{"key": file_id}])
    client.put(
        f"/mocks/{id_}/files/{file_id}/content",
        headers={
            "content-type": "application/octet-stream",
            "accept": "application/json",
        },
        data=BytesIO(b"testfile"),
    )
    client.post(f"/mocks/{id_}/files/{file_id}/commit", headers=headers)
    # Disable files
    input_data["files"] = {"enabled": False}

    response = client.put(f"/mocks/{id_}", headers=headers, json=input_data)

    assert response.status_code == 400
    assert response.json["errors"] == [
        {
            "field": "files.enabled",
            "messages": [
                "You must first delete all files to set the record to be "
                "metadata-only."
            ],
        }
    ]

def test_download_archive(
    app,
    client,
    search_clear,
    headers,
    input_data,
    location,
):
    # Initialize a draft
    res = client.post("/mocks", headers=headers, json=input_data)
    assert res.status_code == 201
    id_ = res.json["id"]
    assert res.json["links"]["files"].endswith(f"/api/mocks/{id_}/files")

    # Initialize 3 file uploads
    res = client.post(
        f"/mocks/{id_}/files",
        headers=headers,
        json=[
            {"key": "f1.pdf"},
            {"key": "f2.pdf"},
            {"key": "f3.pdf"},
        ],
    )
    assert res.status_code == 201
    file_entries = res.json["entries"]
    assert len(file_entries) == 3
    assert {(f["key"], f["status"]) for f in file_entries} == {
        ("f1.pdf", "pending"),
        ("f2.pdf", "pending"),
        ("f3.pdf", "pending"),
    }

    # Upload and commit the 3 files
    for f in file_entries:
        res = client.put(
            f"/mocks/{id_}/files/{f['key']}/content",
            headers={
                "content-type": "application/octet-stream",
                "accept": "application/json",
            },
            data=BytesIO(b"testfile"),
        )
        assert res.status_code == 200
        assert res.json["status"] == "pending"

        res = client.post(f"/mocks/{id_}/files/{f['key']}/commit", headers=headers)
        assert res.status_code == 200
        assert res.json["status"] == "completed"

    captured_fps = []

    class MockZipStream(ZipStream):
        def add(self, fp, *args, **kwargs):
            # Keep track of all passed file pointers
            captured_fps.append(fp)
            return super().add(fp, *args, **kwargs)

    # Get all files as zipped archive
    with patch(
        "invenio_records_resources.resources.files.resource.ZipStream",
        new=MockZipStream,
    ):
        res = client.get(
            f"/mocks/{id_}/files-archive",
        )
        assert res.status_code == 200
        res_bytes = BytesIO(res.data)
        with zipfile.ZipFile(res_bytes, "r") as zf:
            files = zf.namelist()
            files.sort()
            assert files == ["f1.pdf", "f2.pdf", "f3.pdf"]
    assert all(f.closed for f in captured_fps)


#import required libraries
import time
from MATICAPI import MaticAPI

matapi = MaticAPI()
i = 1

while True:
    #get information on your wombats
    mywombats = matapi.ownedWombats()

    #no wombats found, so nothing to do
    if len(mywombats) == 0:
        print('No Wombats found on supplied address.')
        exit()

    if mywombats[0][3] >= 1:
        #We found poop to scoop!
        print(str(i) + '. Found poop to clean!')
        tx = matapi.cleanShit()
        print(str(i) + '. txid: ' + str(tx))
        print(str(i) + '. Sleeping for 24 hours, goodnight.')
        time.sleep(86400)
    else:
        print(str(i) + '. No poop, waiting for an hour.')
        time.sleep(3600)

    i += 1


# Definition for singly-linked list.
from typing import Optional
from collections import deque

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
        carry, rd = 0, 0
        loopcond = True
        while loopcond:
            (carry, rd) = ((l1.val + l2.val) // 10, (l1.val + l2.val) % 10)
            print(carry, "*", rd)
            loopcond = l1.next and l2.next
            if loopcond:
                l1 = l1.next
                l2 = l2.next

        n = l1.next if l1.next else l2.next

        while n.next:
            print(n.val)
            n = n.next

def walk(l: ListNode):
    if (l):
        print(l.val)
        walk(l.next)

def makeFromList(lseed):
    l = None
    p = None
    for i in range(len(lseed)):
        c = ListNode(lseed.popleft(), None)
        if p:
            p.next = c
        p = c
        if not l:
            l = c

    return l

if __name__ == '__main__':
    # 9,9,9,9,9,9,9
    lseed = deque([9, 9, 9, 9, 9, 9, 9])
    l = makeFromList(lseed)
    l2seed = deque([9, 9, 9, 9])
    l2 = makeFromList(l2seed)
    walk(l)
    walk(l2)

    s = Solution()
    s.addTwoNumbers(l, l2)


import cv2

def main():

	imgpath = "/home/neha/Desktop/jungkook/jk1.jpg"
	img = cv2.imread(imgpath)

	cv2.imshow('First', img)
	cv2.waitKey(0) #to bind the keyboard event with cv2.imshow() event
	cv2.destroyAllWindows()

if __name__ == "__main__":
	main()


from typing import Dict

from spreadsheet.cellstorage import CellStorage
from spreadsheet.coord import Coord
from spreadsheet.coord import CoordLike
from spreadsheet.formula import const
from spreadsheet.formula import formula
from spreadsheet.spreadsheet_error import SpreadsheetError

class Spreadsheet(CellStorage):
    def __init__(self):
        self.data: Dict[Coord, formula] = {}

    def __getitem__(self, item):
        if not isinstance(item, CoordLike):
            raise TypeError
        try:
            item = Coord.make(item)
        except SpreadsheetError as e:
            raise KeyError(e)
        if item in self.data:
            return self.data[item].value(self)
        return 0

    def __setitem__(self, item, value):
        if not isinstance(item, CoordLike):
            raise TypeError
        try:
            item = Coord.make(item)
        except SpreadsheetError as e:
            raise KeyError(e)
        if item in self.data:
            self.data[item].clear(self)
        self.data[item] = formula.make(value)

    def get(self, coord: Coord):
        if coord not in self.data:
            self.data[coord] = const(0)
        return self.data[coord]


import time
start = time.time()
#----------------- work space ------------------

ls = [ i for i in range(2,10000)];summer = 0

def num_1(num):return sum(i for i in range(1,num) if num%i == 0)

for num in ls:
    sum_1 = num_1(num)
    sum_2 = num_1(sum_1)
    if sum_1 == num:continue
    else:
        if num == sum_2:
            ls.remove(sum_1)
            summer_1 =summer + num + sum_1
            summer = summer_1
print(summer_1)

#-----------------     end    ------------------
print('----------- time work ----------')
end = time.time()
print(end - start)


class Solution:
    def minimumDeviation(self, nums: List[int]) -> int:
        import heapq
        n = len(nums)
        mini = float('inf')
        res = float('inf')
        h = []
        for i in range(n):
            if nums[i]%2==1:
                nums[i]*=2
            heapq.heappush(h,-nums[i])
            mini = min(mini,nums[i])
        while True:
            maxi = -heapq.heappop(h)
            res = min(res,maxi-mini)
            # print(maxi,res)
            if maxi%2==1:
                break
            mini = min(mini,maxi//2)
            heapq.heappush(h,-maxi//2)
        return res

# 作者：仅存老实人
# 链接：https://www.acwing.com/solution/content/26183/
# 来源：AcWing
# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。

优先队列or堆/1675数组的最小偏移量.py

1675数组的最小偏移量.py


#=============================================================
# File Name: buffer_mmap.py
# Author: miclefeng
# mail: miclefengzss@163.com
# Created Time: Thu 14 Jun 2018 05:22:01 PM CST
#=============================================================
# coding:utf8
import os
import mmap

def read_into_buffer(filename):
    buf = bytearray(os.path.getsize(filename))
    with open(filename, 'rb') as f:
        # readinto() 方法能被用来为预先分配内存的数组填充数据
        # readinto() 填充已存在的缓冲区而不是为新对象重新分配内存再返回它们
        f.readinto(buf)
    return buf

with open('sample.bin', 'wb') as f:
    f.write(b'hello world')

buf = read_into_buffer('sample.bin')
print(buf)
buf[0:5] = b'HELLO'
print(buf)
# memoryview ， 它可以通过零复制的方式对已存在的缓冲区执行切片操作，甚至还能修改它的内容。
m1 = memoryview(buf)
m2 = m1[-5:]
print(bytes(m2))
m2[:] = b'WORLD'
print(buf)
print('\n--------------')

# memeory_map() 函数打开的文件同时支持读和写操作。 任何的修改内容都会复制回原来的文件中。
# 如果需要只读的访问模式，可以给参数 access 赋值为 mmap.ACCESS_READ
# 如果你想在本地修改数据，但是又不想将修改写回到原始文件中，可以使用 mmap.ACCESS_COPY
# 内存映射一个文件并不会导致整个文件被读取到内存中。
# 也就是说，文件并没有被复制到内存缓存或数组中。相反，操作系统仅仅为文件内容保留了一段虚拟内存。
# 当你访问文件的不同区域时，这些区域的内容才根据需要被读取并映射到内存区域中。 而那些从没被访问到的部分还是留在磁盘上
def memory_map(filename, access=mmap.ACCESS_WRITE):
    size = os.path.getsize(filename)
    fd = os.open(filename, os.O_RDWR)
    return mmap.mmap(fd, size, access=access)

size = 1000
with open('data', 'wb') as f:
    f.seek(size - 1)
    f.write(b'\x00')

m = memory_map('data')
print(len(m))
m[0:11] = b'Hello World'
print(m[0:10])
print(m[0])
m.close()

with open('data', 'rb') as f:
    print(f.read(11))
print('\n-----------')
with memory_map('data') as m:
    print(len(m))
    print(m[0:11])


#!/usr/bin/env python
# coding=utf-8
import logging
import math
import os
from pathlib import Path
import sys
from itertools import chain

import datasets
from datasets import load_dataset, load_metric

from huggingface_hub import Repository
import transformers
from transformers import (
    CONFIG_MAPPING,
    MODEL_FOR_CAUSAL_LM_MAPPING,
    AutoConfig,
    AutoModelForCausalLM,
    AutoTokenizer,
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    default_data_collator,
    is_torch_tpu_available,
    set_seed,
)
from transformers.testing_utils import CaptureLogger
from transformers.trainer_utils import get_last_checkpoint
from transformers.utils import check_min_version
from transformers.utils import get_full_repo_name
from transformers.utils.versions import require_version

logger = logging.getLogger(__name__)
# Will error if the minimal version of Transformers is not installed. Remove at your own risks.
check_min_version("4.20.0.dev0")
require_version("datasets>=1.8.0", "To fix: pip install -r examples/pytorch/language-modeling/requirements.txt")

MODEL_CONFIG_CLASSES = list(MODEL_FOR_CAUSAL_LM_MAPPING.keys())
MODEL_TYPES = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES)

def main(ModelArguments, DataTrainingArguments, TrainingArguments):
    parser = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments))
    if len(sys.argv) == 2 and sys.argv[1].endswith(".json"):
        model_args, data_args, training_args = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]))
    else:
        model_args, data_args, training_args = parser.parse_args_into_dataclasses()

    # Setup logging
    logging.basicConfig(
        format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
        datefmt="%m/%d/%Y %H:%M:%S",
        handlers=[logging.StreamHandler(sys.stdout)],
    )

    training_args.log_level = logging.DEBUG
    log_level = training_args.log_level
    # log_level = training_args.get_process_log_level()
    logger.setLevel(log_level)
    datasets.utils.logging.set_verbosity(log_level)
    transformers.utils.logging.set_verbosity(log_level)
    transformers.utils.logging.enable_default_handler()
    transformers.utils.logging.enable_explicit_format()

    '''
    # HF Hub
    if training_args.push_to_hub:
        if training_args.hub_model_id is None:
            repo_name = get_full_repo_name(
                Path(training_args.output_dir).absolute().name, token=training_args.hub_token)
        else:
            repo_name = training_args.hub_model_id
        repo = Repository(training_args.output_dir, clone_from=repo_name)
        repo.git_pull()
    '''

    # Log on each process the small summary:
    logger.warning(
        f"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}"
        + f"distributed training: {bool(training_args.local_rank != -1)}, 16-bits training: {training_args.fp16}"
    )
    logger.info(f"Training/evaluation parameters {training_args}")

    # Detecting last checkpoint.
    last_checkpoint = None
    if os.path.isdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir:
        last_checkpoint = get_last_checkpoint(training_args.output_dir)
        if last_checkpoint is None and len(os.listdir(training_args.output_dir)) > 0:
            raise ValueError(
                f"Output directory ({training_args.output_dir}) already exists and is not empty. "
                "Use --overwrite_output_dir to overcome."
            )
        elif last_checkpoint is not None and training_args.resume_from_checkpoint is None:
            logger.info(
                f"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change "
                "the `--output_dir` or add `--overwrite_output_dir` to train from scratch."
            )

    set_seed(training_args.seed)

    # datasets
    if data_args.dataset_name is not None:
        # Downloading and loading a dataset from the hub.
        raw_datasets = load_dataset(
            data_args.dataset_name,
            data_args.dataset_config_name,
            cache_dir=model_args.cache_dir,
            use_auth_token=True if model_args.use_auth_token else None,
        )
        if "validation" not in raw_datasets.keys():
            raw_datasets["validation"] = load_dataset(
                data_args.dataset_name,
                data_args.dataset_config_name,
                split=f"train[:{data_args.validation_split_percentage}%]",
                cache_dir=model_args.cache_dir,
                use_auth_token=True if model_args.use_auth_token else None,
            )
            raw_datasets["train"] = load_dataset(
                data_args.dataset_name,
                data_args.dataset_config_name,
                split=f"train[{data_args.validation_split_percentage}%:]",
                cache_dir=model_args.cache_dir,
                use_auth_token=True if model_args.use_auth_token else None,
            )
    else:
        data_files = {}
        dataset_args = {}
        if data_args.train_file is not None:
            data_files["train"] = data_args.train_file
        if data_args.validation_file is not None:
            data_files["validation"] = data_args.validation_file
        extension = (
            data_args.train_file.split(".")[-1]
            if data_args.train_file is not None
            else data_args.validation_file.split(".")[-1]
        )
        if extension == "txt":
            extension = "text"
            dataset_args["keep_linebreaks"] = data_args.keep_linebreaks
        raw_datasets = load_dataset(
            extension,
            data_files=data_files,
            cache_dir=model_args.cache_dir,
            use_auth_token=True if model_args.use_auth_token else None,
            **dataset_args,
        )
        # If no validation data is there, validation_split_percentage will be used to divide the dataset.
        if "validation" not in raw_datasets.keys():
            raw_datasets["validation"] = load_dataset(
                extension,
                data_files=data_files,
                split=f"train[:{data_args.validation_split_percentage}%]",
                cache_dir=model_args.cache_dir,
                use_auth_token=True if model_args.use_auth_token else None,
                **dataset_args,
            )
            raw_datasets["train"] = load_dataset(
                extension,
                data_files=data_files,
                split=f"train[{data_args.validation_split_percentage}%:]",
                cache_dir=model_args.cache_dir,
                use_auth_token=True if model_args.use_auth_token else None,
                **dataset_args,
            )

    # shuffle the datasets for sure
    # raw_datasets['train'] = raw_datasets['train'].shuffle(seed=training_args.seed)

    # configs
    config_kwargs = {
        "cache_dir": model_args.cache_dir,
        "revision": model_args.model_revision,
        "use_auth_token": True if model_args.use_auth_token else None,
    }
    if model_args.config_name:
        config = AutoConfig.from_pretrained(model_args.config_name, **config_kwargs)
    elif model_args.model_name_or_path:
        config = AutoConfig.from_pretrained(model_args.model_name_or_path, **config_kwargs)
    else:
        config = CONFIG_MAPPING[model_args.model_type]()
        logger.warning("You are instantiating a new config instance from scratch.")
        if model_args.config_overrides is not None:
            logger.info(f"Overriding config: {model_args.config_overrides}")
            config.update_from_string(model_args.config_overrides)
            logger.info(f"New config: {config}")

    tokenizer_kwargs = {
        "cache_dir": model_args.cache_dir,
        "use_fast": model_args.use_fast_tokenizer,
        "revision": model_args.model_revision,
        "use_auth_token": True if model_args.use_auth_token else None,
    }
    if model_args.tokenizer_name:
        tokenizer = AutoTokenizer.from_pretrained(model_args.tokenizer_name, **tokenizer_kwargs)
    elif model_args.model_name_or_path:
        tokenizer = AutoTokenizer.from_pretrained(model_args.model_name_or_path, **tokenizer_kwargs)
    else:
        raise ValueError(
            "You are instantiating a new tokenizer from scratch. This is not supported by this script."
            "You can do it from another script, save it, and load it from here, using --tokenizer_name."
        )

    if model_args.model_name_or_path:
        model = AutoModelForCausalLM.from_pretrained(
            model_args.model_name_or_path,
            from_tf=bool(".ckpt" in model_args.model_name_or_path),
            config=config,
            cache_dir=model_args.cache_dir,
            revision=model_args.model_revision,
            use_auth_token=True if model_args.use_auth_token else None,
        )
        n_params = sum(dict((p.data_ptr(), p.numel()) for p in model.parameters()).values())
        logger.info(
            f"Training from pretrained `{model_args.model_name_or_path}` - Total size={n_params/2**20:.2f}M params")
    else:
        model = AutoModelForCausalLM.from_config(config)
        n_params = sum(dict((p.data_ptr(), p.numel()) for p in model.parameters()).values())
        logger.info(f"Training new model from scratch - Total size={n_params/2**20:.2f}M params")

    model.resize_token_embeddings(len(tokenizer))

    # Preprocessing the datasets.
    # First we tokenize all the texts.
    if training_args.do_train:
        column_names = raw_datasets["train"].column_names
    else:
        if data_args.dataset_name == 'oscar':
            column_names = raw_datasets["train"].column_names
        else:
            column_names = raw_datasets["validation"].column_names
    text_column_name = "text" if "text" in column_names else column_names[0]

    # since this will be pickled to avoid _LazyModule error in Hasher force logger loading before tokenize_function
    tok_logger = transformers.utils.logging.get_logger("transformers.tokenization_utils_base")

    def tokenize_function(examples):
        with CaptureLogger(tok_logger) as cl:
            output = tokenizer(examples[text_column_name])
        # clm input could be much much longer than block_size
        if "Token indices sequence length is longer than the" in cl.out:
            tok_logger.warning(
                "^^^^^^^^^^^^^^^^ Please ignore the warning above - this long input will be chunked into smaller bits before being passed to the model."  # noqa
            )
        return output

    with training_args.main_process_first(desc="dataset map tokenization"):
        tokenized_datasets = raw_datasets.map(
            tokenize_function,
            batched=True,
            num_proc=data_args.preprocessing_num_workers,
            remove_columns=column_names,
            load_from_cache_file=not data_args.overwrite_cache,
            desc="Running tokenizer on dataset",
        )

    if data_args.block_size is None:
        block_size = tokenizer.model_max_length
        if block_size > 1024:
            logger.warning(
                f"The tokenizer picked seems to have a very large `model_max_length` ({tokenizer.model_max_length}). "
                "Picking 1024 instead. You can change that default value by passing --block_size xxx."
            )
            block_size = 1024
    else:
        if data_args.block_size > tokenizer.model_max_length:
            logger.warning(
                f"The block_size passed ({data_args.block_size}) is larger than the maximum length for the model"
                f"({tokenizer.model_max_length}). Using block_size={tokenizer.model_max_length}."
            )
        block_size = min(data_args.block_size, tokenizer.model_max_length)
    logger.info(f"block_size = {block_size}")

    # Main data processing function that will concatenate all texts from our dataset and generate chunks of block_size.
    def group_texts(examples):
        # Concatenate all texts.
        concatenated_examples = {k: list(chain(*examples[k])) for k in examples.keys()}
        total_length = len(concatenated_examples[list(examples.keys())[0]])
        # We drop the small remainder, we could add padding if the model supported it instead of this drop, you can
        # customize this part to your needs.
        if total_length >= block_size:
            total_length = (total_length // block_size) * block_size
        # Split by chunks of max_len.
        result = {
            k: [t[i:i + block_size] for i in range(0, total_length, block_size)]
            for k, t in concatenated_examples.items()
        }
        result["labels"] = result["input_ids"].copy()
        return result

    with training_args.main_process_first(desc="grouping texts together"):
        lm_datasets = tokenized_datasets.map(
            group_texts,
            batched=True,
            num_proc=data_args.preprocessing_num_workers,
            load_from_cache_file=not data_args.overwrite_cache,
            desc=f"Grouping texts in chunks of {block_size}",
        )

    if training_args.do_train:
        if "train" not in tokenized_datasets:
            raise ValueError("--do_train requires a train dataset")
        train_dataset = lm_datasets["train"]
        if data_args.max_train_samples is not None:
            train_dataset = train_dataset.select(range(data_args.max_train_samples))

    if training_args.do_eval:
        if "validation" not in tokenized_datasets:
            raise ValueError("--do_eval requires a validation dataset")
        eval_dataset = lm_datasets["validation"]
        if data_args.max_eval_samples is not None:
            eval_dataset = eval_dataset.select(range(data_args.max_eval_samples))

        def preprocess_logits_for_metrics(logits, labels):
            if isinstance(logits, tuple):
                logits = logits[0]
            return logits.argmax(dim=-1)

        metric = load_metric("accuracy")

        def compute_metrics(eval_preds):
            preds, labels = eval_preds
            labels = labels[:, 1:].reshape(-1)
            preds = preds[:, :-1].reshape(-1)
            return metric.compute(predictions=preds, references=labels)

    # Initialize our Trainer
    trainer = Trainer(
        model=model,
        args=training_args,
        train_dataset=train_dataset if training_args.do_train else None,
        eval_dataset=eval_dataset if training_args.do_eval else None,
        tokenizer=tokenizer,
        # Data collator will default to DataCollatorWithPadding, so we change it.
        data_collator=default_data_collator,
        compute_metrics=compute_metrics if training_args.do_eval and not is_torch_tpu_available() else None,
        preprocess_logits_for_metrics=preprocess_logits_for_metrics
        if training_args.do_eval and not is_torch_tpu_available()
        else None,
    )

    # Training
    if training_args.do_train:
        checkpoint = None
        if training_args.resume_from_checkpoint is not None:
            checkpoint = training_args.resume_from_checkpoint
        elif last_checkpoint is not None:
            checkpoint = last_checkpoint
        train_result = trainer.train(resume_from_checkpoint=checkpoint)
        trainer.save_model()  # Saves the tokenizer too for easy upload

        metrics = train_result.metrics

        max_train_samples = (
            data_args.max_train_samples if data_args.max_train_samples is not None else len(train_dataset)
        )
        metrics["train_samples"] = min(max_train_samples, len(train_dataset))

        trainer.log_metrics("train", metrics)
        trainer.save_metrics("train", metrics)
        trainer.save_state()

    # Evaluation
    if training_args.do_eval:
        logger.info("*** Evaluate ***")

        metrics = trainer.evaluate()

        max_eval_samples = data_args.max_eval_samples if data_args.max_eval_samples is not None else len(eval_dataset)
        metrics["eval_samples"] = min(max_eval_samples, len(eval_dataset))
        try:
            perplexity = math.exp(metrics["eval_loss"])
        except OverflowError:
            perplexity = float("inf")
        metrics["perplexity"] = perplexity

        trainer.log_metrics("eval", metrics)
        trainer.save_metrics("eval", metrics)

    kwargs = {"finetuned_from": model_args.model_name_or_path, "tasks": "text-generation"}
    if data_args.dataset_name is not None:
        kwargs["dataset_tags"] = data_args.dataset_name
        if data_args.dataset_config_name is not None:
            kwargs["dataset_args"] = data_args.dataset_config_name
            kwargs["dataset"] = f"{data_args.dataset_name} {data_args.dataset_config_name}"
        else:
            kwargs["dataset"] = data_args.dataset_name

    if training_args.push_to_hub:
        trainer.push_to_hub(**kwargs)
    else:
        trainer.create_model_card(**kwargs)

def _mp_fn(index):
    # For xla_spawn (TPUs)
    main()

if __name__ == "__main__":
    import importlib
    from utils import Timer

    with Timer(prefix="main"):
        # import argparse
        # parser = argparse.ArgumentParser()
        # parser.add_argument("--cfg", type=str, default="cfg0")
        # args, _ = parser.parse_known_args()
        # config = importlib.import_module(args.cfg)
        config = importlib.import_module(os.getenv("CONF"))
        # import cfg as config
        main(config.ModelArguments, config.DataArguments, TrainingArguments)


import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import pandas as pd
import re
import os
import utils as ut

runCppCode = 0
# Set to false if you have the results files and just want to plot the results.
if runCppCode == True:
    # Compile and run the C++ files (this is exactly what is in the makefile):
    os.system("echo compiling C++ codes...")
    #os.system("g++ -O3 -o main.out ../cpp_codes/main.cpp ../cpp_codes/utils.cpp ../cpp_codes/ising_solver.cpp -larmadillo")
    os.system("g++ -O3 -o main.out ../cpp_codes/main.cpp ../cpp_codes/utils.cpp -larmadillo -fopenmp")
    os.system("echo executing...")
    os.system("./main.out")

#data = np.loadtxt(directory + filename, delimiter=",")
#data = pd.DataFrame(data)

df_u_xt = pd.read_csv("../results/1D_diffusion/analytical_1D.csv", sep=',',header=None)
u_xt_array = np.array(df_u_xt.values)
df_v_xt = pd.read_csv("../results/1D_diffusion/v_xt.csv", sep=',',header=None)
v_xt_array = np.array(df_v_xt.values)

# Get the axis values (time t and space x):
df_xList = pd.read_csv("../results/1D_diffusion/analytical_1D_xList.csv", sep=',',header=None)
df_tList = pd.read_csv("../results/1D_diffusion/analytical_1D_tList.csv", sep=',',header=None)
xList = np.array(df_xList.values)
tList = np.array(df_tList.values)

X, T = np.meshgrid(xList, tList)
#plt.pcolormesh(dataFrame)
#plt.pcolor(xList, tList, u_xt_array)#, cmap=cm)

#plotCodeWord = 'colormesh'
#plotCodeWord = 'oneFrame' # Plots u for one specified time.
plotCodeWord = 'animate' # Animates the time steps using matplotlib.animate.
t_index = 10 # Which timestep should be plotted?

if plotCodeWord == 'colormesh':
    plt.pcolormesh(X, T, u_xt_array)#, cmap=cm)
    #plt.pcolormesh(u_xt_array)
    #plot1, = plt.plot(xList, v_t_list)
    #plt.legend(plot1, )
    plt.legend()
    plt.grid()
    plt.ylabel(r'$t$')

elif plotCodeWord == 'oneFrame':
    t = tList[t_index]
    u_xt_list = u_xt_array[t_index, :] # u(x,t) at the specific time t.
    v_xt_list = v_xt_array[t_index, :]

    minus_f_x_list = (-ut.f(xList,1))[:,0]

    u_xt_list_simple_sum = v_xt_list + minus_f_x_list

    #plt.plot(xList, u_xt_list, label=r'$u(x,t) = v(x,t)+x/L$')
    plt.plot(xList, v_xt_list, label=r'$v(x,t)$', color='b')
    plt.plot(xList, -ut.f(xList,1), label=r'$x/L = -f(x)$', color='y')
    plt.plot(xList, u_xt_list_simple_sum, label=r'$u(x,t) = v(x,t)+x/L$', color='r')
    plt.ylabel("u(x,t=" + format(float(t), '.7f') + ")")

elif plotCodeWord == 'animate':
    fig, ax = plt.subplots()
    xdata, ydata = [], []
    line, = plt.plot([], [], 'ro')

    def init():
        ax.set_xlim(-0.01, 1.01)
        #ax.set_ylim(-1, 40)
        ax.set_ylim(-1, v_xt_array.max()*1.3)
        return line,

    print('tList:'); print(tList)

    def update(frame): # frame = t_index basically
        t = tList[frame]

        u_xt_list = u_xt_array[frame, :] # u(x,t) at the specific time t.
        #u_xt_list = u_xt_array[frame+1, :]

        v_xt_list = v_xt_array[frame, :]
        minus_f_x_list = (-ut.f(xList,1))[:,0]
        u_xt_list_simple_sum = v_xt_list + minus_f_x_list

        xdata.append(t)
        #ydata.append(u_xt_list_simple_sum[frame])
        ydata.append(minus_f_x_list[frame])
        line.set_data(xdata, ydata)
        return line,

    def animate(i):
        x = xList

        v_xt_list = v_xt_array[i, :]
        minus_f_x_list = (-ut.f(xList,1))[:,0]
        u_xt_list_simple_sum = v_xt_list + minus_f_x_list
        y = u_xt_list_simple_sum

        line.set_data(x, y)
        return line,
    #ani = FuncAnimation(fig, update, frames=np.array(range(len(tList))),
                        #init_func=init, blit=True)
    ani = FuncAnimation(fig, animate, init_func=init, frames=len(tList), interval=50, blit=True)
    plt.ylabel(r'$u(x,t)$')

plt.xlabel(r'$x$')
plt.suptitle('Analytical 1D solution, diffusion equation')
plt.legend()
plt.show()

if plotCodeWord=='animate': # Save the animation as a gif:
    ani.save('../results/1D_diffusion/analytical_1D.gif',writer='imagemagick')


import networkx as nx
import matplotlib.pyplot as plt

def show_graph(G):
    G = nx.DiGraph(G)
    pos = nx.random_layout(G)
    nx.draw(G, pos=pos, with_labels=True)
    plt.show()
    return

def print_graph(name, graph: dict):
    print('{}:'.format(name))
    for v1, v2 in graph.items():
        print("{} -> {}".format(v1, v2))
    print()
    return

def print_slides(slides: dict):
    print('slides:')
    for control_leaf, slide in slides.items():
        print('slide({}) = {}'.format(control_leaf, slide))
    print()
    return

def print_buckets(marked, before, after):
    print("""BUCKETS
marked: {}
before: {}
after:  {}
    """.format(marked, before, after))
    return