from re import I
from django.shortcuts import render
from rest_framework.decorators import action
from .models import Listing, Transaction, UserProfile, Review
from .serializers import ListingSerializer, TransactionSerializer, TutorSerializer, UserProfileSerializer, ReviewSerializer
from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnly
from rest_framework import serializers, viewsets, mixins, generics, status, views
from rest_framework.response import Response
from api.permissions import ProfilePermission, ListingPermission, TransactionPermission
from rest_framework.permissions import AllowAny
from django.contrib.auth import get_user_model
from rest_framework.generics import get_object_or_404
from allauth.account.admin import EmailAddress
from allauth.account.utils import send_email_confirmation
from rest_framework.exceptions import APIException
# Create your views here.

User = get_user_model()

class UserProfileAPIView(generics.RetrieveUpdateAPIView):
	serializer_class = UserProfileSerializer
	permission_classes = [ProfilePermission]

	def get_queryset(self):
			return UserProfile.objects.all()

class NewEmailConfirmation(views.APIView):
	permission_classes = [AllowAny]

	def post(self, request):
		user = get_object_or_404(User, email=request.data['email'])
		emailAddress = EmailAddress.objects.filter(user=user, verified=True).exists()

		if emailAddress:
			return Response({'message': 'This email is already verified'}, status=status.HTTP_400_BAD_REQUEST)
		else:
			try:
				send_email_confirmation(request, user=user)
				return Response({'message': 'Email confirmation sent'}, status=status.HTTP_201_CREATED)
			except APIException:
				return Response({'message': 'This email does not exist, please create a new account'}, status=status.HTTP_403_FORBIDDEN)

class ListingViewSet(viewsets.ModelViewSet):
	serializer_class = ListingSerializer
	permission_classes = [ListingPermission, IsAuthenticatedOrReadOnly]

	def get_queryset(self):
		user = self.request.GET.get('user')
		mod_code = self.request.GET.get('mod_code')
		if user:
			return Listing.objects.filter(owner=user).order_by("date_created")
		elif mod_code:
			return Listing.objects.filter(mod_code__icontains=mod_code).order_by("-owner__xp")
		else:
			return Listing.objects.all()

class TransactionViewSet(viewsets.ModelViewSet):
	serializer_class = TransactionSerializer
	permission_classes = [TransactionPermission, IsAuthenticatedOrReadOnly]

	def get_queryset(self):
			return Transaction.objects.all()

	@action(detail=False)
	def accept(self, request):
		# pk here is the student id
		listing_id = self.request.GET.get('listing')
		student = self.request.GET.get('student')
		if not student or not listing_id:
			return Response({"message": "please provide valid listing and student id"}, status=status.HTTP_400_BAD_REQUEST)
		listing = get_object_or_404(Listing, id=listing_id)
		if listing.owner.pk != request.user.id:
			return Response({"message": "you are not the owner of this listing!"}, status=status.HTTP_401_UNAUTHORIZED)
		try:
			transaction = listing.transactions.get(student=student)
		except Transaction.DoesNotExist:
			return Response({"message": "transaction does not exist"}, status=status.HTTP_400_BAD_REQUEST)

		transaction.is_accepted = True
		transaction.save(update_fields=['is_accepted'])
		return Response({"message": "accepted student (" + str(transaction.student) + ")"}, status=status.HTTP_200_OK)

	@action(detail=True)
	def students(self, request, pk=None):
		user = request.user.id
		listing = get_object_or_404(Listing, id=pk)
		if user != listing.owner.pk:
			return Response(status=status.HTTP_401_UNAUTHORIZED)
		transactions = listing.transactions.all()
		# requested = transactions.filter(is_accepted=False)
		# accepted = transactions.filter(is_accepted=True)
		# requested_serializer = TransactionSerializer(requested, many=True)
		# accepted_serializer = TransactionSerializer(accepted, many=True)
		tx_serializer = TransactionSerializer(transactions, many=True)
		return Response( tx_serializer.data
		# 	{
		# # 	"requested": requested_serializer.data,
		# # 	"accepted": accepted_serializer.data
		# }
		)

	@action(detail=False)
	def tutors(self, request):
		pk = request.user.id
		if not pk:
			return Response(status=status.HTTP_401_UNAUTHORIZED)
		print(pk)
		transactions = Transaction.objects.filter(student=pk)
		print(transactions)
		tutor_serializer = TutorSerializer(transactions, many=True)
		# requested = transactions.filter(is_accepted=False)
		# accepted = transactions.filter(is_accepted=True)
		# requested_serializer = TutorSerializer(requested,many=True)
		# accepted_serializer = TutorSerializer(accepted,many=True)
		return Response(
			tutor_serializer.data
		# 	{
		# 	"requested": requested_serializer.data,
		# 	"accepted": accepted_serializer.data
		# }
		)

	@action(detail=True)
	def status(self, request, pk=None):
		# pk here is listing id
		listing = get_object_or_404(Listing, id=pk)
		user = request.user.id
		if listing.owner.pk == user:
			return Response ({
				"user_type": "owner"
			})

		try:
			transaction = listing.transactions.get(student=user)
		except Transaction.DoesNotExist:
			return Response({"user_type": ""})

		is_accepted = transaction.is_accepted
		gave_review = transaction.gave_review
		return Response({
			"user_type": "student",
			"is_accepted": is_accepted,
			"gave_review": gave_review
		})

class ReviewVewSet(viewsets.ViewSet):
	permission_classes = [IsAuthenticatedOrReadOnly]

	def retrieve(self, request, pk=None):
		queryset = Review.objects.filter(tutor=pk)
		serializer = ReviewSerializer(queryset, many=True)
		return Response(serializer.data)

	def create(self, request):
		if not request.user.id:
			return Response(status=status.HTTP_401_UNAUTHORIZED)
		request.data["student"] = request.user.id
		serializer = ReviewSerializer(data=request.data)
		if serializer.is_valid():
			serializer.save()
			return Response(serializer.data, status=status.HTTP_201_CREATED)
		return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


import pytest
from flask import Flask
from api import app

@pytest.fixture
def client():
    app.config['TESTING'] = True
    with app.test_client() as client:
        yield client

def test_get_domains(client):
    response = client.get('/domains')
    assert response.status_code == 200
    # Assert other conditions based on the expected behavior of the API

# Write more test cases for other API endpoints


import os
from operator import attrgetter
from pathlib import Path
from typing import Dict, List, Tuple

import av2.geometry.interpolate as interp_utils
import numpy as np

from av2.datasets.motion_forecasting import scenario_serialization
from av2.map.map_api import ArgoverseStaticMap
from numpy.typing import NDArray
from rtree import index
from shapely.geometry import LineString, Point
from tqdm.contrib.concurrent import process_map

from metric.lane_assignment import LaneAssignment
from viz.scenario_visualization import visualize_scenario

FIXED_PLOT_SIZE = (120, 120)

DATASET_SAMPLING_FREQUENCY = 10  # Hertz

VELOCITY_TRHESHOLD_FACTOR = 0.2  # Meter / (Meter / Second) = Second
VELOCITY_TRHESHOLD_OFFSET = 0.7  # Meter

DISTANCE_THRESHOLD = 5.0  # Meter
ORIENTATION_ESTIMATION_STEP_SIZE = 0.001  # Meter
ORIENTATION_THRESHOLD = np.pi  # Rad
ORIENTATION_WEIGHTING = 0.5  # -

ASSIGNMENT_CONFIDENCE_THRESHOLD = 0.1  # -

class LaneMissRate:
    def __init__(
        self,
        dataset_root: str,
        plot_dir: str = None,
    ):
        """Lane Miss Rate (LMR) metric class.

        Args:
            dataset_root (str): Dataset root path.
            plot_dir (str, optional): Output directory of scenario plots.
                Defaults to None, meaning to plots are generated.
        """
        self.dataset_root = dataset_root
        self.plot_dir = plot_dir

    def compute_lane_miss_rate(
        self,
        forecasted_trajectories: Dict[int, NDArray[np.float64]],
        gt_trajectories: Dict[int, NDArray[np.float64]],
        velocity_treshold_factor: float = VELOCITY_TRHESHOLD_FACTOR,
        velocity_threshold_offset: float = VELOCITY_TRHESHOLD_OFFSET,
        n_cpus: int = 1,
    ) -> Dict[str, float]:
        """Calculate the Lane Miss Rate (LMR) for k=1 and multi-modal predictions.
            The results are accumulated over all given scenarios.

        Args:
            forecasted_trajectories (Dict[int, NDArray[np.float64]]): Sequence ids
                and predicted trajectories in the format
                [Number of Modes, Number of Timesteps, 2 (x and y coordinates)].
            gt_trajectories (Dict[int, NDArray[np.float64]]): Sequence ids and
                ground-truth trajectory in the format
                [Number of Timesteps, 2 (x and y coordinates)].
            velocity_treshold_factor (float, optional): Factor to determine
                hit threshold. Defaults to VELOCITY_TRHESHOLD_FACTOR.
            velocity_threshold_offset (float, optional): Constant offset to determine
                hit threshold. Defaults to VELOCITY_TRHESHOLD_OFFSET.
            n_cpus (int, optional): Number of CPUs for parallel processing.
                Defaults to 1.

        Returns:
            Dict[str, float]: Lane Miss Rate (LMR).
        """
        input_values = [
            (
                forecasted_trajectories[key],
                gt_trajectories[key],
                key,
                velocity_treshold_factor,
                velocity_threshold_offset,
            )
            for key in forecasted_trajectories
        ]

        # Parallel computation of hits and misses, returning values
        # for the top 1 prediction (k=1) and all predictions
        is_miss = process_map(
            self._compute_is_missed_lane_wrapper, input_values, max_workers=n_cpus
        )
        is_miss = np.stack(is_miss)

        # Average misses over the whole dataset
        metrics = dict()
        metrics["lmr_k1"] = is_miss[:, 0].mean()
        metrics["lmr"] = is_miss.all(axis=-1).mean()

        return metrics

    def _compute_is_missed_lane_wrapper(self, args: tuple) -> NDArray[np.bool_]:
        """Wrapper method to enable parallel processing of scenarios.

        Args:
            args (tuple): Input values.

        Returns:
            NDArray[np.bool_]: Boolean flag that indicates for each predicted
                trajectory whether it is a hit (False) or a miss (True).
        """
        return self.compute_is_missed_lane(*args)

    def compute_is_missed_lane(
        self,
        forecasted_trajectories: NDArray[np.float64],
        gt_trajectory: NDArray[np.float64],
        argo_id: str,
        velocity_treshold_factor: float = VELOCITY_TRHESHOLD_FACTOR,
        velocity_threshold_offset: float = VELOCITY_TRHESHOLD_OFFSET,
    ) -> NDArray[np.bool_]:
        """Caluclate whether predictions are a hit or a
            miss for a single scenario.

        Args:
            forecasted_trajectories (NDArray[np.float64]): Predicted trajectories
                in the format
                [Number of Modes, Number of Timesteps, 2 (x and y coordinates)].
            gt_trajectory (NDArray[np.float64]): Ground-truth trajectory
                in the format [Number of Timesteps, 2 (x and y coordinates)].
            argo_id (str): Argoverse 2 id of the current scenario.
            velocity_treshold_factor (float, optional): Factor to determine
                hit threshold. Defaults to VELOCITY_TRHESHOLD_FACTOR.
            velocity_threshold_offset (float, optional): Constant offset to
                determine hit threshold. Defaults to VELOCITY_TRHESHOLD_OFFSET.

        Returns:
            NDArray[np.bool_]: Boolean flag that indicates for each predicted
                trajectory whether it is a hit (False) or a miss (True).
        """
        # Load the map of the current scenario
        scenario_root = os.path.join(self.dataset_root, argo_id)
        static_map_path = os.path.join(scenario_root, f"log_map_archive_{argo_id}.json")
        scenario_map = ArgoverseStaticMap.from_json(Path(static_map_path))

        # Get centerlines
        lane_segments = scenario_map.vector_lane_segments
        scenario_lane_centerlines = {
            lane_seg_id: interp_utils.compute_midpoint_line(
                left_ln_boundary=lane_segment.left_lane_boundary.xyz,
                right_ln_boundary=lane_segment.right_lane_boundary.xyz,
                num_interp_pts=interp_utils.NUM_CENTERLINE_INTERP_PTS,
            )
            for lane_seg_id, lane_segment in lane_segments.items()
        }

        # Initialize R-tree
        rtree = self._get_rtree(scenario_lane_centerlines)

        # Calculate average velocity of ground-truth trajectory
        gt_deltas = gt_trajectory[1:] - gt_trajectory[:-1]
        gt_velocity_per_ts = (
            np.sqrt(gt_deltas[:, 0] ** 2 + gt_deltas[:, 1] ** 2)
            * DATASET_SAMPLING_FREQUENCY
        )
        gt_velocity = np.mean(gt_velocity_per_ts)

        # Calculate velocity-dependent lane hit threshold s_hit
        s_val_threshold = (
            velocity_treshold_factor * gt_velocity
        ) + velocity_threshold_offset

        # Get assignment for the ground-truth
        gt_orientation = np.arctan2(
            gt_trajectory[-1, 1] - gt_trajectory[-2, 1],
            gt_trajectory[-1, 0] - gt_trajectory[-2, 0],
        )
        gt_assignment = self._get_lane_assignments(
            scenario_lane_centerlines, rtree, gt_trajectory[-1], gt_orientation
        )

        # Check if there is no ground-truth assignment
        # We then fall back to original Miss Rate but use
        # the dynamic threshold we defined
        if len(gt_assignment) == 0:
            distance_to_gt = np.sqrt(
                (
                    (
                        forecasted_trajectories[:, -1]
                        - np.expand_dims(gt_trajectory[-1], 0)
                    )
                    ** 2
                ).sum(axis=-1)
            )
            return distance_to_gt > s_val_threshold

        # Extract assignment with highest confidence value
        max_gt_assignment = max(gt_assignment, key=attrgetter("confidence"))

        forecasted_assignments = []
        for forecasted_trajectory in forecasted_trajectories:
            # Get assignments for the prediction
            forecasted_orientation = np.arctan2(
                forecasted_trajectory[-1, 1] - forecasted_trajectory[-2, 1],
                forecasted_trajectory[-1, 0] - forecasted_trajectory[-2, 0],
            )
            curr_forecasted_assignments = self._get_lane_assignments(
                scenario_lane_centerlines,
                rtree,
                forecasted_trajectory[-1],
                forecasted_orientation,
            )

            if len(curr_forecasted_assignments) >= 1:
                # Extract assignment with highest confidence value
                max_forecasted_assignment = max(
                    curr_forecasted_assignments, key=attrgetter("confidence")
                )

                # Extract all assignments within threshold of assignment confidence
                max_forecasted_assignment_confidence = (
                    max_forecasted_assignment.confidence
                )
                curr_forecasted_assignments = [
                    x
                    for x in curr_forecasted_assignments
                    if x.confidence
                    >= max_forecasted_assignment_confidence
                    - ASSIGNMENT_CONFIDENCE_THRESHOLD
                ]

                # Sort remaining assignments by confidence (highest first)
                curr_forecasted_assignments = sorted(
                    curr_forecasted_assignments,
                    key=attrgetter("confidence"),
                    reverse=True,
                )

                # Take the highest confidence and remove all
                # others that assign to succ or prec lane segments
                curr_forecasted_assignments_clean = curr_forecasted_assignments
                for curr_forecasted_assignment in curr_forecasted_assignments:
                    # Check if curr_forecasted_assignmend is in
                    # curr_forecasted_assignments_clean
                    # If this is the case, remove all with lower confidence
                    if curr_forecasted_assignment in curr_forecasted_assignments_clean:
                        # Get index in curr_forecasted_assignments_clean
                        idx = curr_forecasted_assignments_clean.index(
                            curr_forecasted_assignment
                        )

                        successing_ids = scenario_map.vector_lane_segments[
                            curr_forecasted_assignment.lane_seg_id
                        ].successors
                        preceding_ids = scenario_map.vector_lane_segments[
                            curr_forecasted_assignment.lane_seg_id
                        ].predecessors
                        combined_ids = successing_ids + preceding_ids

                        idx_of_ids_to_delete = set()
                        # Check all remaining ones (with higher numbers than the current index)
                        # if they are they are directly successing or preceding wrt
                        # to the current lane segment
                        for i in range(idx + 1, len(curr_forecasted_assignments_clean)):
                            id_to_check = curr_forecasted_assignments_clean[
                                i
                            ].lane_seg_id
                            if id_to_check in combined_ids:
                                idx_of_ids_to_delete.add(i)

                        # Remove the assignments
                        curr_forecasted_assignments_clean = [
                            i
                            for j, i in enumerate(curr_forecasted_assignments_clean)
                            if j not in list(idx_of_ids_to_delete)
                        ]
            else:
                curr_forecasted_assignments_clean = [None]
            forecasted_assignments.append(curr_forecasted_assignments_clean)

        # Get the lanes and the s values on these lanes that are reachable
        # from the ground-truth assignment via the s_val_treshold
        reachable_lanes = self._get_reachable_lanes(
            max_gt_assignment.lane_seg_id,
            max_gt_assignment.s_val,
            s_val_threshold,
            scenario_map,
        )

        is_missed = []
        # Check if predictions are reachable from the ground-truth assignment
        # and lie within the s_val_treshold
        for curr_forecasted_assignments in forecasted_assignments:
            curr_is_missed = []
            for forecasted_assignment in curr_forecasted_assignments:
                if forecasted_assignment is None:
                    curr_is_missed.append(True)
                elif forecasted_assignment.lane_seg_id not in reachable_lanes:
                    curr_is_missed.append(True)
                else:
                    # Prediction and ground-truth are on the same lane segment
                    if (
                        forecasted_assignment.lane_seg_id
                        == max_gt_assignment.lane_seg_id
                    ):
                        # Prediction is in front of ground-truth
                        if forecasted_assignment.s_val > max_gt_assignment.s_val:
                            if (
                                reachable_lanes[max_gt_assignment.lane_seg_id][
                                    "forward"
                                ]
                                == -1
                                or reachable_lanes[max_gt_assignment.lane_seg_id][
                                    "forward"
                                ]
                                >= forecasted_assignment.s_val
                            ):
                                curr_is_missed.append(False)
                            else:
                                curr_is_missed.append(True)
                        else:
                            if (
                                reachable_lanes[max_gt_assignment.lane_seg_id][
                                    "backward"
                                ]
                                == -1
                                or reachable_lanes[max_gt_assignment.lane_seg_id][
                                    "backward"
                                ]
                                <= forecasted_assignment.s_val
                            ):
                                curr_is_missed.append(False)
                            else:
                                curr_is_missed.append(True)
                    else:
                        # Whole segment is reachable in forward direction
                        if (
                            "forward"
                            in reachable_lanes[forecasted_assignment.lane_seg_id]
                            and reachable_lanes[forecasted_assignment.lane_seg_id][
                                "forward"
                            ]
                            == -1
                        ):
                            curr_is_missed.append(False)
                        # Whole segment is reachable in backward direction
                        elif (
                            "backward"
                            in reachable_lanes[forecasted_assignment.lane_seg_id]
                            and reachable_lanes[forecasted_assignment.lane_seg_id][
                                "backward"
                            ]
                            == -1
                        ):
                            curr_is_missed.append(False)
                        # Prediction assignment is reachable in forward direction
                        elif (
                            "forward"
                            in reachable_lanes[forecasted_assignment.lane_seg_id]
                            and forecasted_assignment.s_val
                            <= reachable_lanes[forecasted_assignment.lane_seg_id][
                                "forward"
                            ]
                        ):
                            curr_is_missed.append(False)
                        # Prediction assignment is reachable in backward direction
                        elif (
                            "backward"
                            in reachable_lanes[forecasted_assignment.lane_seg_id]
                            and forecasted_assignment.s_val
                            >= reachable_lanes[forecasted_assignment.lane_seg_id][
                                "backward"
                            ]
                        ):
                            curr_is_missed.append(False)
                        # Not reachable
                        else:
                            curr_is_missed.append(True)
            # Current prediction only is a miss, if all assignments result in a miss
            is_missed.append(all(curr_is_missed))

        is_missed = np.array(is_missed)

        if self.plot_dir is not None:
            scenario = scenario_serialization.load_argoverse_scenario_parquet(
                os.path.join(scenario_root, f"scenario_{argo_id}.parquet")
            )
            save_path = os.path.join(self.plot_dir, f"{argo_id}.pdf")

            visualize_scenario(
                scenario,
                scenario_map,
                gt_trajectory=gt_trajectory,
                gt_assignment=max_gt_assignment,
                reachable_lanes=reachable_lanes,
                forecasted_trajectories=forecasted_trajectories,
                forecasted_assignments=forecasted_assignments,
                is_missed=is_missed,
                save_path=save_path,
                fixed_plot_size=FIXED_PLOT_SIZE,
            )

        return is_missed

    def _get_lane_assignments(
        self,
        scenario_lane_centerlines: Dict[int, Tuple[NDArray[np.float64], float]],
        rtree: index.Index,
        query_xy: NDArray[np.float64],
        query_orientation: float = None,
        query_radius: float = 3.0,  # width of a US freeway is 12 feet ~= 4m
    ) -> List[LaneAssignment]:
        """Assign a trajectory to one ore multiple lane segment centerlines.

        Args:
            scenario_lane_centerlines (Dict[int, Tuple[NDArray[np.float64], float]]):
                Lane segment ids and tuples containing centerline
                coordinates and lane widths.
            rtree (index.Index): R-tree of lane segments.
            query_xy (NDArray[np.float64]): Query location, in this
                case a trajectory endpoint.
            query_orientation (float, optional): Query orientation, in this case the
                orientation of the trajectory at the endpoint. Defaults to None.
            query_radius (float, optional): Query radius around the endpoint.
                Defaults to 3.0.

        Returns:
            List[LaneAssignment]: List of lane assignments.
        """
        # Use R-tree to get nearby lane segments
        query_box = (
            query_xy[0] - query_radius,
            query_xy[1] - query_radius,
            query_xy[0] + query_radius,
            query_xy[1] + query_radius,
        )
        box_hits = rtree.intersection(query_box, objects="raw")

        query_point = Point(query_xy)

        lane_assignements = []
        unique_lane_ids = []
        for box_hit in box_hits:
            lane_seg_id = box_hit[0]

            # Only check each lane segment once (the R-tree can result in
            # multiple matches per segment)
            if lane_seg_id in unique_lane_ids:
                continue
            else:
                unique_lane_ids.append(lane_seg_id)

            line = LineString(scenario_lane_centerlines[lane_seg_id][0][:, :2])
            line_len = line.length
            s_val = line.project(query_point)
            matched_point = line.interpolate(s_val)
            d_val = np.sqrt(
                (query_point.x - matched_point.x) ** 2
                + (query_point.y - matched_point.y) ** 2
            )

            # Check if the d_val is bigger than lane width / 2.0 (assignment is
            # outside of lane boundaries)
            if d_val > (scenario_lane_centerlines[lane_seg_id][1] / 2.0):
                continue

            # Calculate distance-based assignment confidence
            assignment_confidence = max(0.0, 1.0 - (d_val / DISTANCE_THRESHOLD))

            if query_orientation is not None:
                # Calculate angle in both directions
                orientation_forward = None
                orientation_backward = None

                # Approximate orientation of lane segment by going forward and
                # backward from the assigned point
                # If the assigned point is the start or end of a lane segment,
                # only one direction is possible
                if s_val >= ORIENTATION_ESTIMATION_STEP_SIZE:
                    point_backward = line.interpolate(
                        s_val - ORIENTATION_ESTIMATION_STEP_SIZE
                    )
                    orientation_backward = np.arctan2(
                        matched_point.y - point_backward.y,
                        matched_point.x - point_backward.x,
                    )
                if s_val <= (line_len - ORIENTATION_ESTIMATION_STEP_SIZE):
                    point_forward = line.interpolate(
                        s_val + ORIENTATION_ESTIMATION_STEP_SIZE
                    )
                    orientation_forward = np.arctan2(
                        point_forward.y - matched_point.y,
                        point_forward.x - matched_point.x,
                    )
                if orientation_forward is None and orientation_backward is None:
                    print(
                        "Matched a lane segment that is too short to calculate an orientation"
                    )
                    continue

                orientations = [
                    x for x in [orientation_backward, orientation_forward] if x != None
                ]
                average_orientation = sum(orientations) / len(orientations)

                # Get orientation difference between lane and trajectory
                orientation_difference = abs(
                    np.arctan2(
                        np.sin(query_orientation - average_orientation),
                        np.cos(query_orientation - average_orientation),
                    )
                )

                # Get combined assignment confidence
                orientation_confidence = max(
                    0.0, 1.0 - (orientation_difference / ORIENTATION_THRESHOLD)
                )
                assignment_confidence = (
                    (1.0 - ORIENTATION_WEIGHTING) * assignment_confidence
                    + ORIENTATION_WEIGHTING * orientation_confidence
                )

            lane_assignements.append(
                LaneAssignment(
                    lane_seg_id,
                    assignment_confidence,
                    s_val,
                    d_val,
                    line_len,
                    [matched_point.x, matched_point.y],
                )
            )

        return lane_assignements

    def _get_rtree(
        self,
        scenario_lane_centerlines: Dict[int, Tuple[NDArray[np.float64], float]],
    ) -> index.Index:
        """Build an R-tree of lane centerline segments.

        Args:
            scenario_lane_centerlines (Dict[int, Tuple[NDArray[np.float64], float]]):
                Lane segment ids and tuples containing centerline
                coordinates and lane widths.

        Returns:
            index.Index: R-tree.
        """

        def generate_items():
            subseg_id = 0
            for lane_seg_id, lane_segment in scenario_lane_centerlines.items():
                # Only extract x and y, throw z away
                lane_seg_coords = lane_segment[0][:, :2]
                for i in range(len(lane_seg_coords) - 1):
                    x_1, y_1 = lane_seg_coords[i]
                    x_2, y_2 = lane_seg_coords[i + 1]
                    subseg = ((x_1, y_1), (x_2, y_2))
                    subseg_box = (
                        min(x_1, x_2),
                        min(y_1, y_2),
                        max(x_1, x_2),
                        max(y_1, y_2),
                    )
                    yield (subseg_id, subseg_box, (lane_seg_id, subseg))
                    subseg_id += 1

        return index.Index(generate_items())

    def _get_reachable_lanes(
        self,
        start_id: int,
        s_val_on_start: float,
        s_val_threshold: float,
        scenario_map: ArgoverseStaticMap,
    ) -> Dict[int, Dict[str, float]]:
        """Get each lane segment and also the point on each corresponding
            lane segment that is reachable from a starting point on an
            initial lane segment. Only reachable lane segments that are
            within a threshold distance along the lane graph are returned.
            Lane segments can be reachable up to a Frenet s-value in
            "forward" and "backward" direction.

        Args:
            start_id (int): Id of the initial lane segment.
            s_val_on_start (float): Frenet s-value of starting point on
                the initial lane segment.
            s_val_threshold (float): Threshold distance along the lane graph.
            scenario_map (ArgoverseStaticMap): Scenario map api of Argoverse 2.

        Returns:
            Dict[int, Dict[str, float]]: Dictionary consisting of the lane
                segment id and dictionary pairs.
                The second-order dictionary holds the reachability of the
                corresponding lane segment:
                A float value assigned to the "forward" key indicatates that
                the lane segment is reachable up to the Frenet s-value
                in forward direction.
                A float value assigned to the "backward" key indicatates that
                the lane segment is reachable up to the Frenet s-value
                in backward direction.
                In both cases the value -1 indicates that the whole lane
                segment is reachable.
        """
        centerline = scenario_map.get_lane_segment_centerline(start_id)
        centerline_length = LineString(centerline).length

        # Perform dfs in forward and backward direction
        # starting from the assigned lane segment
        visited = {}
        dfs_result_forward = self._dfs(
            visited=visited,
            direction="forward",
            start_id=start_id,
            s_val_on_start=s_val_on_start,
            start_centerline_length=centerline_length,
            s_val_traveled=centerline_length - s_val_on_start,
            s_val_threshold=s_val_threshold,
            scenario_map=scenario_map,
        )

        visited = {}
        dfs_result_backward = self._dfs(
            visited=visited,
            direction="backward",
            start_id=start_id,
            s_val_on_start=s_val_on_start,
            start_centerline_length=centerline_length,
            s_val_traveled=s_val_on_start,
            s_val_threshold=s_val_threshold,
            scenario_map=scenario_map,
        )

        dfs_result = dfs_result_forward + dfs_result_backward

        # It might be possible to visit some lane segments multiple times (loops)
        # We then only keep the one segment and the reachable position that is further
        # away from the starting point
        reachable_lanes = dict()
        for dfs_sequence in dfs_result:
            for dfs_sequence_entry in dfs_sequence:
                if dfs_sequence_entry[0] not in reachable_lanes:
                    reachable_lanes[dfs_sequence_entry[0]] = dict()

                if dfs_sequence_entry[1] == "forward":
                    if "forward" not in reachable_lanes[dfs_sequence_entry[0]]:
                        reachable_lanes[dfs_sequence_entry[0]][
                            "forward"
                        ] = dfs_sequence_entry[2]
                    elif dfs_sequence_entry[2] == -1:
                        reachable_lanes[dfs_sequence_entry[0]]["forward"] = -1
                    else:
                        reachable_lanes[dfs_sequence_entry[0]]["forward"] = max(
                            reachable_lanes[dfs_sequence_entry[0]]["forward"],
                            dfs_sequence_entry[2],
                        )

                if dfs_sequence_entry[1] == "backward":
                    if "backward" not in reachable_lanes[dfs_sequence_entry[0]]:
                        reachable_lanes[dfs_sequence_entry[0]][
                            "backward"
                        ] = dfs_sequence_entry[2]
                    elif dfs_sequence_entry[2] == -1:
                        reachable_lanes[dfs_sequence_entry[0]]["backward"] = -1
                    else:
                        reachable_lanes[dfs_sequence_entry[0]]["backward"] = min(
                            reachable_lanes[dfs_sequence_entry[0]]["backward"],
                            dfs_sequence_entry[2],
                        )

        return reachable_lanes

    def _dfs(
        self,
        visited: Dict[int, Dict[str, bool]],
        direction: str,
        start_id: int,
        s_val_on_start: float,
        start_centerline_length: float,
        s_val_traveled: float,
        s_val_threshold: float,
        scenario_map: ArgoverseStaticMap,
    ) -> List[List[Tuple[int, str, float]]]:
        """Directed depth-first search that is able to go into
            forward or backward direction.

        Args:
            visited (Dict[int, Dict[str, bool]]): Dictionary consisting of lane
                segment id and dictionary pairs. The second-order dictionary
                holds the direction ("forward" or "backward") of visited
                lane segments.
            direction (str): Search direction ("forward" or "backward").
            start_id (int): Id of the initial lane segment.
            s_val_on_start (float): Frenet s-value of starting point on the
                initial lane segment.
            start_centerline_length (float): Centerline length of the initial
                lane segment.
            s_val_traveled (float): Already travelend distance along the lane
                graph.
            s_val_threshold (float): Threshold distance along the lane graph.
            scenario_map (ArgoverseStaticMap): Scenario map api of Argoverse 2.

        Returns:
            List[List[Tuple[int, str, float]]]: List containing the results of
                the depth-first search. Each list entry contains one possible
                path. Each path is defined by edges. These edges consist of:
                    int: Id of the lane segment.
                    str: Traversed direction during the search ("forward" or
                        "backward").
                    float: Reachable Frenet s-value in search direction,
                        with -1 indicating full reachability.
        """
        # Check if node already got visited from both directions
        if start_id in visited:
            if "forward" in visited[start_id] and "backward" in visited[start_id]:
                if visited[start_id]["forward"] and visited[start_id]["backward"]:
                    return [[]]
        else:
            visited[start_id] = dict()

        # Set the visited flags
        if direction == "forward":
            visited[start_id]["forward"] = True

        if direction == "backward":
            visited[start_id]["backward"] = True

        # Check if the threshold got exceeded and return if so
        if s_val_traveled > s_val_threshold:
            if direction == "forward":
                traveled_on_current_lane_segment = (
                    start_centerline_length - s_val_on_start
                )
                traveled_without_current_lane_segment = (
                    s_val_traveled - traveled_on_current_lane_segment
                )
                left_distance_on_current_lane_segment = (
                    s_val_threshold - traveled_without_current_lane_segment
                )
                s_on_lane = s_val_on_start + left_distance_on_current_lane_segment

            if direction == "backward":
                traveled_on_current_lane_segment = s_val_on_start
                traveled_without_current_lane_segment = (
                    s_val_traveled - traveled_on_current_lane_segment
                )
                left_distance_on_current_lane_segment = (
                    s_val_threshold - traveled_without_current_lane_segment
                )
                s_on_lane = s_val_on_start - left_distance_on_current_lane_segment

            return [[(start_id, direction, s_on_lane)]]

        curr_path = []

        # If direction is forward, another dfs for all successors is
        # triggered in forward direction
        # Also, for each predecessor of each successor, a dfs in backward
        # direction is triggered
        if direction == "forward":
            successing_ids = scenario_map.vector_lane_segments[start_id].successors
            for successing_id in successing_ids:
                if successing_id in scenario_map.vector_lane_segments:

                    successing_centerline = scenario_map.get_lane_segment_centerline(
                        successing_id
                    )
                    successing_centerline_length = LineString(
                        successing_centerline
                    ).length

                    dfs_result = self._dfs(
                        visited=visited,
                        direction="forward",
                        start_id=successing_id,
                        s_val_on_start=0,
                        start_centerline_length=successing_centerline_length,
                        s_val_traveled=s_val_traveled
                        + successing_centerline_length
                        - 0,
                        s_val_threshold=s_val_threshold,
                        scenario_map=scenario_map,
                    )

                    curr_path.extend(dfs_result)

                    predecessor_ids_of_successor = scenario_map.vector_lane_segments[
                        successing_id
                    ].predecessors
                    for predecessor_id_of_successor in predecessor_ids_of_successor:
                        if (
                            predecessor_id_of_successor
                            in scenario_map.vector_lane_segments
                            and predecessor_id_of_successor != start_id
                        ):

                            centerline = scenario_map.get_lane_segment_centerline(
                                predecessor_id_of_successor
                            )
                            centerline_length = LineString(centerline).length

                            dfs_result = self._dfs(
                                visited=visited,
                                direction="backward",
                                start_id=predecessor_id_of_successor,
                                s_val_on_start=centerline_length,
                                start_centerline_length=centerline_length,
                                s_val_traveled=s_val_traveled + centerline_length,
                                s_val_threshold=s_val_threshold,
                                scenario_map=scenario_map,
                            )

                            curr_path.extend(dfs_result)

        # If direction is backward, another dfs for all predecessors is
        # triggered in backward direction
        # Also, for each successor of each predecessor, a dfs in forward
        # direction is triggered
        if direction == "backward":
            preceding_ids = scenario_map.vector_lane_segments[start_id].predecessors
            for preceding_id in preceding_ids:
                if preceding_id in scenario_map.vector_lane_segments:

                    preceding_centerline = scenario_map.get_lane_segment_centerline(
                        preceding_id
                    )
                    preceding_centerline_length = LineString(
                        preceding_centerline
                    ).length

                    dfs_result = self._dfs(
                        visited=visited,
                        direction="backward",
                        start_id=preceding_id,
                        s_val_on_start=preceding_centerline_length,
                        start_centerline_length=preceding_centerline_length,
                        s_val_traveled=s_val_traveled + preceding_centerline_length,
                        s_val_threshold=s_val_threshold,
                        scenario_map=scenario_map,
                    )

                    curr_path.extend(dfs_result)

                    successor_ids_of_predecessor = scenario_map.vector_lane_segments[
                        preceding_id
                    ].successors
                    for successor_id_of_predecessor in successor_ids_of_predecessor:
                        if (
                            successor_id_of_predecessor
                            in scenario_map.vector_lane_segments
                            and successor_id_of_predecessor != start_id
                        ):

                            centerline = scenario_map.get_lane_segment_centerline(
                                successor_id_of_predecessor
                            )
                            centerline_length = LineString(centerline).length

                            dfs_result = self._dfs(
                                visited=visited,
                                direction="forward",
                                start_id=successor_id_of_predecessor,
                                s_val_on_start=0,
                                start_centerline_length=centerline_length,
                                s_val_traveled=s_val_traveled + centerline_length,
                                s_val_threshold=s_val_threshold,
                                scenario_map=scenario_map,
                            )

                            curr_path.extend(dfs_result)

        if len(curr_path) == 0:
            return [[(start_id, direction, -1)]]

        return_value = []
        for curr_subpath in curr_path:
            return_value.append(curr_subpath + [(start_id, direction, -1)])

        return return_value


import tushare as ts
import pandas as pd
import matplotlib.pyplot as plt
plt.figure(figsize=(40,30))
def get_stock_price(code0,start0,end0,ktype0):
    data = ts.get_hist_data(code0,start=start0,end=end0,ktype=ktype0)
    data.loc[data['p_change']<-4,'p_change']=1
    y=data['p_change']
    fig=plt.plot(y,label='p_change')
    y1=data['close']
    fig=plt.plot(y1,label='close_price')
    plt.legend()
    plt.xlabel('TIME')
    plt.xticks(rotation=45)
    plt.show()
start = '2016-01-01'
end = '2018-03-30'
code = '600315'
ktype = 'W'

get_stock_price(code,start,end,ktype)


from credentials import *
import tweepy
from time import sleep
import pprint

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
leftHand = {"q","w","e","r","t","a","s","d","f","g","z","x","c","v","b"," ","1","2","3","4","5","!","@","#","$","%"}
for tweet in tweepy.Cursor(api.search,q='#Rockets').items():
    try:
        onlyLeft = True
        for letters in tweet.text:
            print(letters)
            if letters.lower() not in leftHand:
                print("not in set")
                onlyLeft = False
                break
        print(onlyLeft)
        if onlyLeft == True:
            tweet.retweet()
            print(tweet.user.screen_name + ' only used their left hand to type : ' + tweet.text)
        sleep(5)

    except tweepy.TweepError as e:
        print(e.reason)

    except StopIteration:
        break


import numpy as np

# error message when image is not colored while it should be
NOT_COLOR_IMAGE = 'NOT_COLOR_IMAGE'

def ensure_color(image):
    """
    Ensure that an image is colored
    Args:
        image: image to be checked for

    Returns:
        nothing

    Raises:
        ValueError with message code if image is not colored
    """
    if len(image.shape) < 3:
        raise ValueError(NOT_COLOR_IMAGE)

def div0(a, b):
    """ ignore / 0, div0( [-1, 0, 1], 0 ) -> [0, 0, 0] """
    with np.errstate(divide='ignore', invalid='ignore'):
        q = np.true_divide(a, b)
        q[~ np.isfinite(q)] = 0  # -inf inf NaN

    return q

def index_diff(image, green_scale=2.0, red_scale=1.4):

    ensure_color(image)

    bgr_sum = np.sum(image, axis=2)

    blues = div0(image[:, :, 0], bgr_sum)
    greens = div0(image[:, :, 1], bgr_sum)
    reds = div0(image[:, :, 2], bgr_sum)

    green_index = green_scale * greens - (reds + blues)
    red_index = red_scale * reds - (greens)

    return green_index - red_index


import json
import os
import shutil

from collections import Counter
from future.utils import iteritems
from tempfile import NamedTemporaryFile
from unittest import TestCase

from hypothesis import (
    given,
    HealthCheck,
    settings,
)
from hypothesis.strategies import sampled_from, text, dictionaries, booleans, integers
from mock import Mock, ANY, patch
from backports.tempfile import mkdtemp, TemporaryDirectory
from pathlib2 import Path

from oasislmf.api_client.client import OasisAPIClient
from oasislmf.cli import RootCmd
from oasislmf.cli.test import TestModelApiCmd
from oasislmf.utils.exceptions import OasisException

class TestModelApiCmdLoadAnalysisSettingsJson(TestCase):
    def test_do_il_is_true___result_has_input_dict_and_do_il_is_true(self):
        conf = {
            'analysis_settings': {
                'il_output': True,
                'foo': 'bar',
            }
        }

        with NamedTemporaryFile('w') as f:
            json.dump(conf, f)
            f.flush()

            res_conf, do_il, do_ri = TestModelApiCmd().load_analysis_settings_json(f.name)

            self.assertEqual(conf, res_conf)
            self.assertTrue(do_il)
            self.assertFalse(do_ri)

    def test_do_il_is_true_do_ri_is_true___result_has_input_dict_and_do_il_is_true_and_do_ri_is_true(self):
        conf = {
            'analysis_settings': {
                'il_output': True,
                'ri_output': True,
                'foo': 'bar',
            }
        }

        with NamedTemporaryFile('w') as f:
            json.dump(conf, f)
            f.flush()

            res_conf, do_il, do_ri = TestModelApiCmd().load_analysis_settings_json(f.name)

            self.assertEqual(conf, res_conf)
            self.assertTrue(do_il)
            self.assertTrue(do_ri)

    def test_do_il_is_false___result_has_input_dict_and_do_il_is_false(self):
        conf = {
            'analysis_settings': {
                'il_output': False,
                'foo': 'bar',
            }
        }

        with NamedTemporaryFile('w') as f:
            json.dump(conf, f)
            f.flush()

            res_conf, do_il, do_ri = TestModelApiCmd().load_analysis_settings_json(f.name)

            self.assertEqual(conf, res_conf)
            self.assertFalse(do_il)
            self.assertFalse(do_ri)

    @given(sampled_from(['true', 'TRUE', 'True']))
    def test_do_il_is_string_true___result_has_input_dict_and_do_il_is_true(self, do_il_in):
        conf = {
            'analysis_settings': {
                'il_output': do_il_in,
                'foo': 'bar',
            }
        }

        with NamedTemporaryFile('w') as f:
            json.dump(conf, f)
            f.flush()

            res_conf, do_il, do_ri = TestModelApiCmd().load_analysis_settings_json(f.name)

            self.assertEqual(conf, res_conf)
            self.assertTrue(do_il)
            self.assertFalse(do_ri)

    @given(sampled_from(['false', 'FALSE', 'False']))
    def test_do_il_is_string_false___result_has_input_dict_and_do_il_is_false(self, do_il_in):
        conf = {
            'analysis_settings': {
                'il_output': do_il_in,
                'foo': 'bar',
            }
        }

        with NamedTemporaryFile('w') as f:
            json.dump(conf, f)
            f.flush()

            res_conf, do_il, do_ri = TestModelApiCmd().load_analysis_settings_json(f.name)

            self.assertEqual(conf, res_conf)
            self.assertFalse(do_il)
            self.assertFalse(do_ri)

class TestModelApiCmdRunAnalysis(TestCase):
    @settings(suppress_health_check=[HealthCheck.too_slow], deadline=None, max_examples=1)
    @given(
        text(), text(), dictionaries(text(), text()), booleans(), booleans(), integers(min_value=0, max_value=5), integers(min_value=0, max_value=5)
    )
    def test_no_errors_are_raised___completed_is_incremented(self,
        input_dir, output_dir, settings, do_il, do_ri, initial_complete, initial_failed):
        client = OasisAPIClient('http://localhost:8001')
        client.upload_inputs_from_directory = Mock(return_value='input_location')
        client.run_analysis_and_poll = Mock()

        counter = Counter({
            'completed': initial_complete,
            'failed': initial_failed,
        })

        TestModelApiCmd().run_analysis((client, input_dir, output_dir, settings, do_il, do_ri, counter, [],1))

        self.assertEqual(initial_complete + 1, counter['completed'])
        self.assertEqual(initial_failed, counter['failed'])
        client.upload_inputs_from_directory.assert_called_once_with(
            input_dir, bin_directory=ANY, do_il=do_il, do_ri=do_ri, do_build=True)
        client.run_analysis_and_poll.assert_called_once_with(settings, 'input_location', output_dir)

    @settings(suppress_health_check=[HealthCheck.too_slow], deadline=None, max_examples=1)
    @given(
        text(), text(), dictionaries(text(), text()), booleans(), booleans(), integers(min_value=0, max_value=5), integers(min_value=0, max_value=5))
    def test_uploading_raises_an_error___failed_counter_is_incremented(self,
        input_dir, output_dir, settings, do_il, do_ri, initial_complete, initial_failed):
        client = OasisAPIClient('http://localhost:8001')
        client.upload_inputs_from_directory = Mock(side_effect=OasisException())
        client.run_analysis_and_poll = Mock()

        counter = Counter({
            'completed': initial_complete,
            'failed': initial_failed,
        })

        TestModelApiCmd().run_analysis((client, input_dir, output_dir, settings, do_il, do_ri, counter, [], 1))

        self.assertEqual(initial_complete, counter['completed'])
        self.assertEqual(initial_failed + 1, counter['failed'])

    @settings(suppress_health_check=[HealthCheck.too_slow], deadline=None, max_examples=1)
    @given(
        text(), text(), dictionaries(text(), text()), booleans(), booleans(), integers(min_value=0, max_value=5), integers(min_value=0, max_value=5)
    )
    def test_run_and_poll_raises_an_error___failed_counter_is_incremented(self,
        input_dir, output_dir, settings, do_il, do_ri, initial_complete, initial_failed):

        client = OasisAPIClient('http://localhost:8001')
        client.upload_inputs_from_directory = Mock()
        client.run_analysis_and_poll = Mock(side_effect=OasisException())

        counter = Counter({
            'completed': initial_complete,
            'failed': initial_failed,
        })

        TestModelApiCmd().run_analysis((client, input_dir, output_dir, settings, do_il, do_ri, counter, [], 1))

        self.assertEqual(initial_complete, counter['completed'])
        self.assertEqual(initial_failed + 1, counter['failed'])

class TestModelApiCmdRun(TestCase):
    def setUp(self):
        self.directory = mkdtemp()
        self._orig_cwd = os.getcwd()
        self.create_working_directory(self.directory)

    def tearDown(self):
        os.chdir(self._orig_cwd)
        shutil.rmtree(self.directory)

    def create_working_directory(self, d):
        os.mkdir(os.path.join(d, 'input'))
        Path(os.path.join(d, 'analysis_settings.json')).touch()

        os.chdir(d)

    def get_command(self, api_server_url='http://localhost:8001', analysis_directory=None, extras=None):
        kwargs = {}

        if analysis_directory:
            kwargs['input-directory'] = os.path.join(analysis_directory, 'input')
            kwargs['output-directory'] = os.path.join(analysis_directory, 'output')
            kwargs['analysis-settings-file'] = os.path.join(analysis_directory, 'analysis_settings.json')

        kwargs.update(extras or {})
        kwargs_str = ' '.join('--{} {}'.format(k, v) for k, v in iteritems(kwargs))

        return RootCmd(argv='test model-api {} {}'.format(kwargs_str, api_server_url).split())

    def test_api_server_is_not_supplied___parsing_args_raises_an_error(self):
        cmd = self.get_command(api_server_url='')

        with self.assertRaises(SystemExit):
            cmd.parse_args()

    def test_input_directory_is_not_supplied___default_is_abs_path_to_input_in_cwd(self):
        cmd = self.get_command()

        args = cmd.parse_args()

        self.assertEqual(args.input_directory, os.path.abspath('input'))

    def test_input_directory_is_supplied___path_is_abs_path_to_supplied_dir(self):
        with TemporaryDirectory() as input_dir:
            cmd = self.get_command(extras={'input-directory': input_dir})

            args = cmd.parse_args()

            self.assertEqual(args.input_directory, os.path.abspath(input_dir))

    def test_output_directory_is_not_supplied___default_is_abs_path_to_output_in_cwd(self):
        cmd = self.get_command()

        args = cmd.parse_args()

        self.assertEqual(args.output_directory, os.path.abspath('output'))

    def test_output_directory_is_supplied___path_is_abs_path_to_supplied_dir(self):
        with TemporaryDirectory() as output_dir:
            cmd = self.get_command(extras={'output-directory': output_dir})

            args = cmd.parse_args()

            self.assertEqual(args.output_directory, os.path.abspath(output_dir))

    def test_analysis_settings_is_not_supplied___default_is_abs_path_to_analysis_settings_in_cwd(self):
        cmd = self.get_command()

        args = cmd.parse_args()

        self.assertEqual(args.analysis_settings_file, os.path.abspath('analysis_settings.json'))

    def test_analysis_settings_is_supplied___path_is_abs_path_to_supplied_file(self):
        with TemporaryDirectory() as settings_path:
            settings_path = os.path.join(settings_path, 'analysis_settings.json')
            Path(settings_path).touch()

            cmd = self.get_command(extras={'analysis-settings-file': settings_path})

            args = cmd.parse_args()

            self.assertEqual(args.analysis_settings_file, os.path.abspath(settings_path))

    def test_num_analyses_is_not_supplied___default_1(self):
        cmd = self.get_command()

        args = cmd.parse_args()

        self.assertEqual(1, args.num_analyses)

    def test_num_analyses_is_supplied___num_analyses_is_correct(self):
        cmd = self.get_command(extras={'num-analyses': 6})

        args = cmd.parse_args()

        self.assertEqual(6, args.num_analyses)

    def test_health_check_attempts_is_not_supplied___default_1(self):
        cmd = self.get_command()

        args = cmd.parse_args()

        self.assertEqual(1, args.health_check_attempts)

    def test_health_check_attempts_is_supplied___health_check_attempts_is_correct(self):
        cmd = self.get_command(extras={'health-check-attempts': 6})

        args = cmd.parse_args()

        self.assertEqual(6, args.health_check_attempts)

    def test_input_directory_does_not_exist___error_is_raised(self):
        shutil.rmtree(os.path.join(self.directory, 'input'))

        cmd = self.get_command(analysis_directory=self.directory)
        cmd._logger = Mock()

        res = cmd.run()

        self.assertEqual(1, res)
        cmd.logger.error.assert_called_once_with('Input directory does not exist: {}'.format(os.path.join(self.directory, 'input')))

    def test_analysis_settings_file_does_not_exist___error_is_raised(self):
        os.remove(os.path.join(self.directory, 'analysis_settings.json'))

        cmd = self.get_command(analysis_directory=self.directory)
        cmd._logger = Mock()

        res = cmd.run()

        self.assertEqual(1, res)
        cmd.logger.error.assert_called_once_with('Analysis settings does not exist: {}'.format(os.path.join(self.directory, 'analysis_settings.json')))

    @given(integers(min_value=1, max_value=5))
    def test_client_health_check_fails___error_is_written_to_the_log(self, health_check_attempts):
        with patch('oasislmf.api_client.client.OasisAPIClient.health_check', Mock(return_value=False)) as health_check_mock:
            cmd = self.get_command(analysis_directory=self.directory, extras={'health-check-attempts': health_check_attempts})
            cmd._logger = Mock()

            res = cmd.run()

            self.assertEqual(1, res)
            cmd.logger.error.assert_called_once_with('Health check failed for http://localhost:8001.')
            health_check_mock.assert_called_once_with(health_check_attempts)

    @given(
        integers(min_value=1, max_value=5), booleans(), booleans(), dictionaries(text(), text()))
    def test_validation_is_successful___threadpool_is_started_for_each_analysis(self,
        num_analyses, do_il, do_ri, settings):

        pool_mock_object = Mock()

        with patch('oasislmf.api_client.client.OasisAPIClient.health_check', Mock(return_value=True)), \
                patch('oasislmf.cli.test.TestModelApiCmd.load_analysis_settings_json', Mock(return_value=(settings, do_il, do_ri))), \
                patch('oasislmf.cli.test.ThreadPool', Mock(return_value=pool_mock_object)) as pool_mock:
            cmd = self.get_command(analysis_directory=self.directory, extras={'num-analyses': num_analyses})
            cmd._logger = Mock()

            res = cmd.run()

            pool_mock.assert_called_once_with(processes=num_analyses)

            self.assertEqual(0, res)
            self.assertEqual(1, pool_mock_object.map.call_count)
            fn = pool_mock_object.map.call_args[0][0]
            args = list(pool_mock_object.map.call_args[0][1])

            self.assertEqual(fn.__name__, 'run_analysis')
            self.assertIsInstance(fn.__self__, TestModelApiCmd)
            self.assertEqual(args, [(ANY, cmd.args.input_directory, cmd.args.output_directory, settings, do_il, do_ri, ANY, ANY, ANY)] * num_analyses)


import pandas as pd
import numpy as np

def read_tsp(filename):
    """
    读取read_tsp数据 转化为 pandas DataFrame
    """
    with open(filename) as file:
        node_coord_start  = None
        city_number = None
        lines = file.readlines()
        i = 0
        # 获取tsp数据
        while True:
            line = lines[i]
            if line.startswith('DIMENSION :'):
                #获取城市数量
                city_number = int(line.split()[-1])
            if line.startswith('NODE_COORD_SECTION'):
                node_coord_start = i
                break
            i = i + 1
        print(' {} 个城市数据被读取.'.format(city_number))
        file.seek(0)

        # 读取各个城市的坐标信息转化为Dataframe
        cities = pd.read_csv(
            file,
            skiprows = node_coord_start +1,
            sep = ' ',
            names = ['city', 'y', 'x'],
            dtype = {'city' : str, 'x' : np.float64, 'y' : np.float64},
            header = None,
            nrows = city_number
        )

        return cities

def  normalize(points):
    """
    对向量进行归一化处理，[0,1]在y上，将原始比例维持在x上来标准化数据
    """
    ratio = (points.x.max() - points.x.min()) / (points.y.max() - points.y.min()),1

    ratio = np.array(ratio) / max(ratio)

    norm = points.apply(lambda c: (c - c.min()) / (c.max() - c.min()))

    return norm.apply(lambda p: ratio * p, axis = 1)


import pandas as pd
import speech_recognition as sr
import moviepy.editor as mp
from pydub import AudioSegment
from pydub.silence import split_on_silence
import shutil
import os
import subprocess

# create a directory to store the audio chunks
def create_folder(folder_name):
    if not os.path.isdir(folder_name):
        os.mkdir(folder_name)
    return folder_name

def rename_file(start, files, step2):
    for file in files:
        name_date = file#.split('_')[1:4]
        # new_name =  "_".join(name_date).replace(' ', '-')
        new_name =  name_date.replace(' ', '-')
        # print(name_date)
        # print(new_name)
        ## move files to next step
        shutil.copy(f"{start}/{file}", f"{step2}/{new_name}.mp4")

# a function that splits the audio file into chunks
# and applies speech recognition
def get_large_audio_transcription(path):
    """
    Splitting the large audio file into chunks
    and apply speech recognition on each of these chunks
    """
    # create a speech recognition object
    r = sr.Recognizer()
    # open the audio file using pydub
    sound = AudioSegment.from_wav(path)
    # split audio sound where silence is 700 miliseconds or more and get chunks
    chunks = split_on_silence(sound,
        # experiment with this value for your target audio file
        min_silence_len = 500,
        # adjust this per requirement
        silence_thresh = sound.dBFS-14,
        # keep the silence for 1 second, adjustable as well
        keep_silence=500,
    )
    folder_name = "audio-chunks"
    # create a directory to store the audio chunks
    if not os.path.isdir(folder_name):
        os.mkdir(folder_name)
    whole_text = ""
    # process each chunk
    for i, audio_chunk in enumerate(chunks, start=1):
        # export audio chunk and save it in
        # the `folder_name` directory.
        chunk_filename = os.path.join(folder_name, f"chunk{i}.wav")
        audio_chunk.export(chunk_filename, format="wav")
        # recognize the chunk
        with sr.AudioFile(chunk_filename) as source:
            audio_listened = r.record(source)
            # try converting it to text
            try:
                text = r.recognize_google(audio_listened)
            except sr.UnknownValueError as e:
                error = {}
                error['count'] += 1
            else:
                text = f"{text.capitalize()}. "
                # print(chunk_filename, ":", text)
                whole_text += text
    # return the text for all chunks detected
    shutil.rmtree('audio-chunks')
    print(path, error.items())
    return whole_text

def speech_to_text(src, files, batch_name):
    pd.DataFrame(columns=['name','text']).to_csv(batch_name,index=False)
    for file in files:
        name_date = file#.split('_')[:2]
        whole_text = get_large_audio_transcription(f'{src}/{file}')
        name_date.append(whole_text)

        df = pd.read_csv(batch_name)
        try:
            s = pd.Series(name_date, index=['name','date','text'])
            df = df.append(s, ignore_index=True)

            df.to_csv(batch_name,index=False)
        except:
            print('error')

def pipeline(start, rename, convert, load):
    start = create_folder(start)
    files = os.listdir(start)

    step2 = create_folder(rename)
    rename_file(start, files, step2)

    step3 = create_folder(convert)

    subprocess.run(f"FOR %i IN (test_data/named/*.mp4) DO ffmpeg -i test_data/named/%i -acodec pcm_s16le -ar 16000 test_data/audio/%i.wav", shell=True)

    step4 = os.listdir(step3)
    speech_to_text(step3, step4, load)

pipeline('test_data/raw', 'test_data/named', 'test_data/audio', "test_data/load/final.csv")


""" Views para usuarios """

# Django rest framework
from rest_framework import status, viewsets, mixins
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.generics import get_object_or_404

# Permisos
from rest_framework.permissions import IsAuthenticated
from facebook.permissions import IsAccountOwner

# Modelos
from facebook.models import User

# Serializers
from facebook.serializers import (CreateUserSerializer, UserModelSerializer,
                                  LoginSerializer, UpdateUserSerializer)

class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()

    def get_permissions(self):
        permissions= []
        if (self.action not in ['create', 'login']):
            permissions.append(IsAuthenticated)
        if (self.action in ['update', 'partial_update', 'destroy']):
            permissions.append(IsAccountOwner)
        return [p() for p in permissions]

    def get_serializer_class(self):
        if self.action == 'create':
            return CreateUserSerializer
        if self.action in ['update', 'partial_update']:
            return UpdateUserSerializer
        if self.action == 'login':
            return LoginSerializer
        return UserModelSerializer

    def create(self, request, *args, **kwargs):
        serializer_class = self.get_serializer_class()
        serializer = serializer_class(
            data = request.data,
            context = self.get_serializer_context()
        )
        serializer.is_valid(raise_exception=True)
        user = serializer.save()
        data = UserModelSerializer(user).data
        return Response(data, status=status.HTTP_201_CREATED)

    @action(detail=False, methods=['post'])
    def login(self, request):
        """ Vista para el login, esta vista obtiene o crea el token """
        serializer_class = self.get_serializer_class()
        serializer = serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)
        user, token = serializer.save()
        data = {
            'user': UserModelSerializer(user).data,
            'access_token': token
        }
        return Response(data, status=status.HTTP_201_CREATED)

    @action(detail=False, methods=['post'])
    def logout(self, request):
        """ Vista para logout del usuario, esta vista destruye el token """
        try:
            request.user.auth_token.delete()
        except (ObjectDoesNotExist):
            return Response(status=status.HTTP_404_NOT_FOUND)

        return Response({"success": "Successfully logged out."},
                    status=status.HTTP_200_OK)


def spin_lock(steps, length):
    lock = [0]
    reps = 0
    pos = 0
    while reps < length:
        reps += 1
        pos = (pos + steps) % len(lock)
        pos += 1
        lock.insert(pos, reps)
    return lock[pos + 1]

# print(spin_lock(359, 2017))

def angry_spin_lock(steps, length):
    val = 0
    reps = 0
    pos = 0
    while reps < length:
        reps += 1
        pos = (pos + steps) % (reps)
        pos += 1
        if pos == 1:
            val = reps
    return val

print(angry_spin_lock(359, 50000000))


"""
.. module:: Error
    :synopsis: Module to deliberately cause an error for testing handling.

.. moduleauthor:: Dave Pedu <dave@davepedu.com>

"""

from pyircbot.modulebase import ModuleBase, hook

class Error(ModuleBase):

    @hook("PRIVMSG")
    def error(self, message, command):
        """
        If the message recieved from IRC has the string "error" in it, cause a ZeroDivisionError
        """
        if "error" in message.trailing:
            print(10 / 0)


#!/usr/bin/env python3

import rospy
import rosbag
import cv2
import time
import numpy as np
import os
import sensor_msgs.point_cloud2 as pc2
import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation
from sensor_msgs.msg import PointCloud2
from sensor_msgs.msg import Image
from cv_bridge import CvBridge

##### USE Settings #####
subscribeLidar = True
subscribeWebcam = True

savePCL = True
showPlot = True
saveImage = True
showImage = True

folderDataLidarSave = os.path.join("catkin_ws", "src", "lidar_test", "scripts", "lidar_data")
folderDataCameraSave = os.path.join("catkin_ws", "src", "lidar_test", "scripts", "camera_data")

##### General Settings #####

# Creates a 3D figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Create a empty list to save the points
points = []

## Counter used t
contSaveLidar = 0
contSaveImage = 0
##############################

##### Callbacks #####
def callback_lidar(msg):
    global points, contSaveLidar, savePCL, showPlot, folderDataLidarSave

    ## If the save is defined
    if savePCL == True:
        pathSave = os.path.join(folderDataLidarSave, f'point_cloud{contSaveLidar}.bag')
        bag = rosbag.Bag(pathSave, 'w')
        bag.write('/rslidar_points', msg)
        bag.close()
        contSaveLidar += 1

    if showPlot == True:
        # Extect the relevant points of the PointCloud2 message
        point_cloud = pc2.read_points(msg, field_names=("x", "y", "z"), skip_nans=True)

        # Convet the points to a numpy array
        points = np.array(list(point_cloud))

def callback_webcam(msg):
    global contSaveImage, folderDataCameraSave
    bridge = CvBridge()
    frame = bridge.imgmsg_to_cv2(msg, desired_encoding='rgb8')
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    if saveImage == True:
        pathSave = os.path.join(folderDataCameraSave, f"image{contSaveImage}.jpg")
        print("Salvando")
        cv2.imwrite(pathSave, frame)
        contSaveImage += 1

    if showImage == True:
        cv2.imshow('Webcam', frame)
        cv2.waitKey(1)
##############################

#### Auxiliar functions #####

# Function to update the plot
def update_plot(frame):
    global points

    # Clean the grapth
    ax.cla()

    # Show the clound point updated
    if len(points) > 0:
        ax.scatter(points[:, 0], points[:, 1], points[:, 2])

    # Additional graph settings
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    ax.set_title('3D Cloud Point')

def delete_files_folder(path):
    if os.path.isdir(path):
        for filename in os.listdir(path):
            file_path = os.path.join(path, filename)
            if os.path.isfile(file_path):
                os.remove(file_path)

if __name__ == '__main__':

    delete_files_folder(folderDataLidarSave)
    delete_files_folder(folderDataCameraSave)

    rospy.init_node('main', anonymous=True)

    if subscribeLidar == True:
        rospy.Subscriber('/rslidar_points', PointCloud2, callback_lidar)

    if subscribeWebcam == True:
        rospy.Subscriber('webcam_image', Image, callback_webcam)

    if showPlot:
        # Update continualy the graph
        ani = FuncAnimation(fig, update_plot, interval=100)
        # Show the graph
        plt.show()

    if showImage == True:
        # Wait for a moment before to create the window
        time.sleep(1)

        cv2.namedWindow('Webcam', cv2.WINDOW_NORMAL)
        cv2.resizeWindow('Webcam', 640, 480)

    ## Keep the node alive
    rospy.spin()

    if showImage == True:
        cv2.destroyAllWindows()


from typing import Any, Dict, List
from numbers import Number
import torch
import torch.distributed as dist

__all__ = [
    "all_reduce_scalar",
    "all_reduce_tensor",
    "all_reduce_dict",
    "all_gather_tensor",
]

def all_reduce_scalar(value: Number, op: str = "sum") -> Number:
    """All-reduce single scalar value. NOT torch tensor."""
    # https://github.com/NVIDIA/DeepLearningExamples/blob/master/PyTorch/LanguageModeling/Transformer-XL/pytorch/utils/distributed.py
    if dist.is_initialized() and dist.is_available():
        op = op.lower()
        if (op == "sum") or (op == "mean"):
            dist_op = dist.ReduceOp.SUM
        elif op == "min":
            dist_op = dist.ReduceOp.MIN
        elif op == "max":
            dist_op = dist.ReduceOp.MAX
        elif op == "product":
            dist_op = dist.ReduceOp.PRODUCT
        else:
            raise RuntimeError(f"Invalid all_reduce op: {op}")

        backend = dist.get_backend()
        if backend == torch.distributed.Backend.NCCL:
            device = torch.device("cuda")
        elif backend == torch.distributed.Backend.GLOO:
            device = torch.device("cpu")
        else:
            raise RuntimeError(f"Unsupported distributed backend: {backend}")

        tensor = torch.tensor(value, device=device, requires_grad=False)
        dist.all_reduce(tensor, op=dist_op)
        if op == "mean":
            tensor /= dist.get_world_size()
        ret = tensor.item()
    else:
        ret = value
    return ret

def all_reduce_tensor(tensor: torch.Tensor, op="sum", detach: bool = True) -> torch.Tensor:
    if dist.is_initialized() and dist.is_available():
        ret = tensor.clone()
        if detach:
            ret = ret.detach()
        if (op == "sum") or (op == "mean"):
            dist_op = dist.ReduceOp.SUM
        else:
            raise RuntimeError(f"Invalid all_reduce op: {op}")

        dist.all_reduce(ret, op=dist_op)
        if op == "mean":
            ret /= dist.get_world_size()
    else:
        ret = tensor
    return ret

def all_reduce_dict(result: Dict[str, Any], op="sum") -> Dict[str, Any]:
    new_result = {}
    for k, v in result.items():
        if isinstance(v, torch.Tensor):
            new_result[k] = all_reduce_tensor(v, op)
        elif isinstance(v, Number):
            new_result[k] = all_reduce_scalar(v, op)
        else:
            raise RuntimeError(f"Dictionary all_reduce should only have either tensor or scalar, got: {type(v)}")
    return new_result

def all_gather_tensor(tensor: torch.Tensor) -> List[torch.Tensor]:
    if dist.is_initialized() and dist.is_available():
        world_size = dist.get_world_size()
        local_rank = dist.get_rank()
        output = [
            tensor if (i == local_rank) else torch.empty_like(tensor) for i in range(world_size)
        ]
        dist.all_gather(output, tensor, async_op=False)
        return output
    else:
        return [tensor]


# This file is named <stata_exec.py>, so the command is <stata_exec>
# and the key class is <StataExecCommand>, with a <run> method

# See also:
# - http://docs.sublimetext.info/en/latest/reference/build_systems/configuration.html
# - https://www.sublimetext.com/docs/3/api_reference.html
# - https://github.com/STealthy-and-haSTy/SublimeScraps/tree/master/build_enhancements

# Some excerpts based on:
# - https://github.com/TiesdeKok/ipystata/

# ---------------------------
# Imports
# ---------------------------

import os
import random
import winreg
import tempfile

import sublime
import sublime_plugin

from . import stata

#try:
#    import Pywin32.setup, win32com.client, win32con, win32api
#except:
#    sublime.message_dialog("StataEditor not loaded - Need Pywin32 package")
#    raise Exception

# ---------------------------
# Constants
# ---------------------------

settings_file = "Stata.sublime-settings"
stata_debug = False
sublime.stata = None  # Changed later

# ---------------------------
# Classes
# ---------------------------

class StataExecCommand(sublime_plugin.WindowCommand):

    def run(self, action='', mode='', **kwargs):

        if sublime.stata is None:
            sublime.stata = stata.Stata()

        view = self.window.active_view()
        current_file = view.file_name() # str or None
        selection_length = sum(len(s) for s in view.sel())

        # Test stata packages by running a "demo.do" file
        if mode == "build":
            # contents will be 'None' in case of failure (file not exists, etc.)
            contents = get_build_contents(info=self.window.extract_variables())
        else:
            contents = None

        # Read data from active screen (unless we are just running another file)
        if contents is None:
            print('Running current file...')
            # Extract contents
            if not selection_length:
                contents = view.substr(sublime.Region(0, view.size()))
            else:
                # Expand lines
                last_char = view.substr(view.sel()[-1])[-1]
                if last_char != '\n':
                    view.run_command("expand_selection", {"to": "line"})

                contents = ''.join(view.substr(sel) for sel in view.sel())

            if contents and contents[-1] != "\n":
                contents = contents + "\n"
        else:
            print('Running build do-file...')

        # Change current folder
        cwd = get_cwd(view)
        if cwd is not None:
            # Better to modify contents instead of polluting the Stata command history
            #sublime.stata.run("cd " + cwd)
            contents = 'cd "' + cwd + '"\n' + contents

        # Run requested command
        sublime.stata.run_script(contents)

        # Done!
        pass

class StataRegisterAutomationCommand(sublime_plugin.ApplicationCommand):
    def run(self, **kwargs):
        stata_fn = settings.get("stata_path")
        os.popen('powershell -command start-process "{}" "/Register" -verb RunAs'.format(stata_fn))

class StataUpdateExecutablePathCommand(sublime_plugin.ApplicationCommand):
    def run(self, **kwargs):

        def update_settings(fn):
            settings_fn = 'Stata.sublime-settings'
            settings = sublime.load_settings(settings_fn)

            old_fn = settings.get('stata_path', '')

            if check_correct_executable(fn):
                if old_fn!=fn:
                    settings.set('stata_path_old', old_fn)
                settings.set('stata_path', fn)
                sublime.save_settings(settings_fn)
                sublime.status_message("Stata path updated")
                launch_stata()
            else:
                sublime.error_message("Cannot run Stata; the path does not exist: {}".format(fn))

        def cancel_update():
            sublime.status_message("Stata path not updated")

        def check_correct(fn):
            is_correct = check_correct_executable(fn)
            if is_correct:
                sublime.status_message("Path is valid")
            else:
                sublime.status_message("Path is currently NOT valid")

        fn = get_exe_path()
        msg ="Enter the path of the Stata executable"
        sublime.active_window().show_input_panel(msg, fn, update_settings, check_correct, cancel_update)

# ---------------------------
# Functions
# ---------------------------

def plugin_loaded():
    global settings
    settings = sublime.load_settings(settings_file)

def get_cwd(view):
    fn = view.file_name()
    if not fn: return
    cwd = os.path.split(fn)[0]
    return cwd

def get_build_contents(info):
    extension = info.get('file_extension')
    package = info.get('project_base_name')
    file_name = info.get('file')
    file_path = info.get('file_path')

    if package is None:
        print('stata mode=build: aborted as attribute "project_base_name" is missing')
        return

    if extension not in ('mata', 'ado'):
        print('stata mode=build: aborted as extension is not .ado or .mata')
        return

    pointer_fn = os.path.join(file_path, 'build.txt')

    if not os.path.isfile(pointer_fn):
        print('stata mode=build: aborted as there is no build.txt file')
        return

    build_fn = open(pointer_fn).read()
    build_fn = os.path.join(file_path, build_fn)

    if not os.path.isfile(build_fn):
        print('stata mode=build: aborted as there is no %s file' % build_fn)
        return

    with open(build_fn) as fh:
        content = ''.join(fh.readlines())

    return content


""" Module for implementing exceptions thrown by disclosure avoidance systems"""
# 4/9/2019
# Pavel Zhuravlev

import logging

class Error(Exception):
    """Base class for DAS exceptions."""

    def __init__(self, msg=''):
        self.message = msg
        # Log it. One of the main reasons to have this class
        logging.error(msg)
        Exception.__init__(self, msg)

    def __repr__(self):
        return self.message

    __str__ = __repr__

class DASConfigError(Error):
    """ Errors resulting from parsing the config file"""

    def __init__(self, msg, option, section):
        if option is not None:
            message = f"{msg}: No option '{option}' in config section: [{section}]"
        else:
            message = f"{msg}: No section '{section}' in config file"
        Error.__init__(self, message)
        self.option = option
        self.section = section
        self.args = (option, section)

class IncompatibleAddendsError(Error):
    """ Errors when two objects of different type or with unaddable attributes are added"""

    def __init__(self, msg, attrname, addend1attr, addend2attr):
        Error.__init__(self, f"{msg} cannot be added: addends have different {attrname}: {addend1attr} != {addend2attr}")
        self.attrname = attrname
        self.addend1attr = addend1attr
        self.addend2attr = addend2attr
        self.args = (attrname, addend1attr, addend2attr)

class DASValueError(Error):
    def __init__(self, msg, value):
        Error.__init__(self, f"{msg}: value '{value}' is invalid")
        self.value = value
        self.args = (value, )


import cv2
import matplotlib.pyplot as plt
import numpy as np
from tqdm import tqdm
from Fuzzy.readMHA_something import BRATS2015 as Brats
brats = Brats(8)

from itertools import chain

def seg_kmeans_color():
    for i in range(15):
        image, label = brats.next_train_batch(5)
    # img = cv2.imread('000129.jpg', cv2.IMREAD_COLOR)
    # # 变换一下图像通道bgr->rgb，否则很别扭啊
    # b, g, r = cv2.split(img)
    # img = cv2.merge([r, g, b])
    img = image[0,:,:,:]
    label_min = label[0, :, :]

    # 3个通道展平
    img_flat = img.reshape((img.shape[0] * img.shape[1], 4))
    img_flat = np.float32(img_flat)

    # 迭代参数
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TermCriteria_MAX_ITER, 20, 1.0)
    flags = cv2.KMEANS_RANDOM_CENTERS

    # 聚类
    compactness, labels, centers = cv2.kmeans(img_flat, 5, None, criteria, 10, flags)

    # 显示结果
    img_output = labels.reshape((img.shape[0], img.shape[1]))
    plt.subplot(131), plt.imshow(img), plt.title('input')
    plt.subplot(132), plt.imshow(img_output, 'gray'), plt.title('kmeans')
    plt.subplot(133), plt.imshow(label_min, 'gray'), plt.title('LABEL')
    plt.show()

#均值和方差
def mean_var():
    image_flair_1 = []
    image_t1_1 = []
    image_t1c_1 = []
    image_t2_1 = []

    image_flair_2 = []
    image_t1_2 = []
    image_t1c_2 = []
    image_t2_2 = []

    image_flair_3 = []
    image_t1_3 = []
    image_t1c_3 = []
    image_t2_3 = []

    image_flair_4 = []
    image_t1_4 = []
    image_t1c_4 = []
    image_t2_4 = []

    for i in tqdm(range((brats.train_batch * 155)),
                  total=((brats.train_batch * 155)),
                  ncols=70, leave=False, unit='b'):
        image_temp, label_temp = brats.next_train_batch(1)
        i_f = image_temp[0, :, :, 0]
        i_t1 = image_temp[0, :, :, 1]
        i_t1c = image_temp[0, :, :, 2]
        i_t2 = image_temp[0, :, :, 3]

        label = label_temp[0, :, :]
        class_num = 1
        image_flair_1.append(i_f[label == class_num])
        image_t1_1.append(i_t1[label == class_num])
        image_t1c_1.append(i_t1c[label == class_num])
        image_t2_1.append(i_t2[label == class_num])

        class_num = 2
        image_flair_2.append(i_f[label == class_num])
        image_t1_2.append(i_t1[label == class_num])
        image_t1c_2.append(i_t1c[label == class_num])
        image_t2_2.append(i_t2[label == class_num])

        class_num = 3
        image_flair_3.append(i_f[label == class_num])
        image_t1_3.append(i_t1[label == class_num])
        image_t1c_3.append(i_t1c[label == class_num])
        image_t2_3.append(i_t2[label == class_num])

        class_num = 4
        image_flair_4.append(i_f[label == class_num])
        image_t1_4.append(i_t1[label == class_num])
        image_t1c_4.append(i_t1c[label == class_num])
        image_t2_4.append(i_t2[label == class_num])
        # if i == 75:
        #     break
        pass
    # image_flair_total = reduce(operator.add, image_flair)
    image_flair_total_1 = list(chain(*image_flair_1))
    image_t1_total_1 = list(chain(*image_t1_1))
    image_t1c_total_1 = list(chain(*image_t1c_1))
    image_t2_total_1 = list(chain(*image_t2_1))

    image_flair_total_2 = list(chain(*image_flair_2))
    image_t1_total_2 = list(chain(*image_t1_2))
    image_t1c_total_2 = list(chain(*image_t1c_2))
    image_t2_total_2 = list(chain(*image_t2_2))

    image_flair_total_3 = list(chain(*image_flair_3))
    image_t1_total_3 = list(chain(*image_t1_3))
    image_t1c_total_3 = list(chain(*image_t1c_3))
    image_t2_total_3 = list(chain(*image_t2_3))

    image_flair_total_4 = list(chain(*image_flair_4))
    image_t1_total_4 = list(chain(*image_t1_4))
    image_t1c_total_4 = list(chain(*image_t1c_4))
    image_t2_total_4 = list(chain(*image_t2_4))

    print("class", 1)
    print("flair_mean_var", np.mean(image_flair_total_1), np.var(image_flair_total_1))
    print("t1_mean_var", np.mean(image_t1_total_1), np.var(image_t1_total_1))
    print("t1c_mean_var", np.mean(image_t1c_total_1), np.var(image_t1c_total_1))
    print("t2_mean_var", np.mean(image_t2_total_1), np.var(image_t2_total_1))

    print("class", 2)
    print("flair_mean_var", np.mean(image_flair_total_2), np.var(image_flair_total_2))
    print("t1_mean_var", np.mean(image_t1_total_2), np.var(image_t1_total_2))
    print("t1c_mean_var", np.mean(image_t1c_total_2), np.var(image_t1c_total_2))
    print("t2_mean_var", np.mean(image_t2_total_2), np.var(image_t2_total_2))

    print("class", 3)
    print("flair_mean_var", np.mean(image_flair_total_3), np.var(image_flair_total_3))
    print("t1_mean_var", np.mean(image_t1_total_3), np.var(image_t1_total_3))
    print("t1c_mean_var", np.mean(image_t1c_total_3), np.var(image_t1c_total_3))
    print("t2_mean_var", np.mean(image_t2_total_3), np.var(image_t2_total_3))

    print("class", 4)
    print("flair_mean_var", np.mean(image_flair_total_4), np.var(image_flair_total_4))
    print("t1_mean_var", np.mean(image_t1_total_4), np.var(image_t1_total_4))
    print("t1c_mean_var", np.mean(image_t1c_total_4), np.var(image_t1c_total_4))
    print("t2_mean_var", np.mean(image_t2_total_4), np.var(image_t2_total_4))

if __name__ == '__main__':
    #seg_kmeans_color()
    mean_var()


import sys
import csv
import json
from glob import glob
import random
import re

def get_urls(train, getRealSet):
  fake_folder_names = glob('/mnt/misinformation-real-time/FakeNewsNet/code/fakenewsnet_dataset/politifact/fake/*/')
  real_folder_names = glob('/mnt/misinformation-real-time/FakeNewsNet/code/fakenewsnet_dataset/politifact/real/*/')

  fake = []
  real = []

  random.seed(10)
  random.shuffle(fake_folder_names)
  random.shuffle(real_folder_names)

  if train == True:
    words = {}
    fake = fake_folder_names[:int(0.8 * len(fake_folder_names))]
    real = real_folder_names[:int(0.8 * len(real_folder_names))]
  else:
    fake = fake_folder_names[int(0.8 * len(fake_folder_names)):]
    real = real_folder_names[int(0.8 * len(real_folder_names)):]

  final_folder_names = real if getRealSet else fake
  for folder_name in final_folder_names:
    files = glob(folder_name +  '*json')
    if len(files) == 0:
       continue
    file = files[0]
    line = ''
    with open(file) as jsonfile:
       try:
         data = json.load(jsonfile)
         line = data['text']
       except:
          continue
    if line == '':
       continue
    line = data['url']
    if line.startswith('www'):
         line = 'http://' + line
    elif not line.startswith('http'):
         line = 'https://www.' + line
    print(line)

def main(args):
  get_urls(args[0].lower()[0] == 't', args[1].lower()[0] == 'r')

if __name__ == "__main__":
  main(sys.argv[1:])


import pluggy

hookspec = pluggy.HookspecMarker("example_for_pycon")
hookimpl = pluggy.HookimplMarker("example_for_pycon")

class ExampleSpec:
    """A hook specification namespace."""

    @hookspec
    def myhook(self, arg1, arg2):
        """My special little hook that you can customize."""

class MyPlugin:
    """A hook implementation namespace."""

    @hookimpl
    def myhook(self, arg1, arg2):
        print("inside Plugin_1.myhook()")
        return arg1 + arg2

# create a manager and add the spec
pm = pluggy.PluginManager("example_for_pycon")
pm.add_hookspecs(ExampleSpec)

# register plugins
pm.register(MyPlugin())

# call our `myhook` hook
results = pm.hook.myhook(arg1=1, arg2=2)
print(results)


from selenium import webdriver
import os
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv())

driver = webdriver.Chrome(executable_path=os.getenv('CHROME'))
driver.get("https://rahulshettyacademy.com/AutomationPractice/")
checkboxes = driver.find_elements_by_xpath("//input[@type='checkbox']")
print(len(checkboxes))
option = "option2"
for checkbox in checkboxes:
    if checkbox.get_attribute("value") == option:
        checkbox.click()
        assert checkbox.is_selected()
radio_buttons = driver.find_elements_by_name("radioButton")
radio_buttons[2].click()
radio_buttons[2].is_selected()


# 자료구조를 계속 새로만드니까 시간초과 -> 자료구조를 수정하는 방식
from collections import Counter as c

def solution(topping):
    cnt=0
    bro1=c(topping)
    bro2=set()
    for i in topping:
        bro1[i] -=1
        if bro1[i] ==0:
            bro1.pop(i)

        bro2.add(i)

        if len(bro1.keys())==len(bro2):
            cnt+=1

    answer = cnt
    return answer

구현/프로그래머스_롤케이크 자르기.py

프로그래머스_롤케이크 자르기.py


from opentest.core.process_objects import Spec
from opentest.core.process_step import ProcessStep

class RecordGsysVersionStep(ProcessStep):
    def __init__(self,
                 process_plan,
                 name='Record Gsys Version'):
        super(RecordGsysVersionStep, self).__init__(process_plan, name)

    def run(self):
        ixion = self.variables['IXION']
        gsys_version = ixion.ssh.query('gsys --version').stdout

        gsys_version_label = gsys_version.split('Build label: ')[1].split('\nBuild tool')[0]

        self.add_data(
            'Gsys Version',
            gsys_version_label,
            Spec('==', '6.12-1'))

        return self.set_status()


import json, os
from flask import request, _request_ctx_stack
from functools import wraps
from jose import jwt
from urllib.request import urlopen

# Read AUTH0_DOMAIN and API_AUDIENCE from environment variables
AUTH0_DOMAIN = os.environ['AUTH0_DOMAIN']
ALGORITHMS = ['RS256']
API_AUDIENCE = os.environ['API_AUDIENCE']

## AuthError Exception
class AuthError(Exception):
    def __init__(self, error, status_code):
        self.error = error
        self.status_code = status_code

## Auth Header
def get_token_auth_header():
    auth = request.headers.get("Authorization", None)
    if not auth:
        raise AuthError(
            {
                "code": "authorization_header_missing",
                "description": "Authorisation header is missing"
            }, 401)
    auth_section = auth.split()
    if auth_section[0] != "Bearer": # auth header has no Bearer prefix
        raise AuthError(
            {
                "code": "invalid_header",
                "description": "Authorisation header needs to include Bearer prefix"
            }, 401)
    elif len(auth_section) == 1:   # auth header has only one section
        raise AuthError(
            {
                "code": "invalid_header",
                "description": "Token missing in authorisation header"
            }, 401)
    elif len(auth_section) > 2:   # auth header has too many sections
        raise AuthError(
            {
                "code": "invalid_header",
                "description": "Authorisation header has too many sections"
            }, 401)

    token = auth_section[1]
    return token

def check_permissions(permission, payload):
    try:
        permissions = payload.get('permissions')
        if (permission in permissions):
            print (str(permission) + " in " + str(permissions))
            return True
        else:
            raise AuthError(
                    {
                        'code': 'invalid_permissions',
                        'description': 'User doesn\'t have permission.'
                    }, 403)
    except:
        raise AuthError(
                {
                    'code': 'invalid_permissions',
                    'description': 'User doesn\'t have permission.'
                }, 401)

def verify_decode_jwt(token):
    jsonURL = urlopen(f'https://{AUTH0_DOMAIN}/.well-known/jwks.json')
    jsonWebKeySet = json.loads(jsonURL.read())
    unverified_token_header = jwt.get_unverified_header(token)
    rsa_key = {}
    if 'kid' not in unverified_token_header:
        raise AuthError(
            {
                'code': 'invalid_token',
                'description': 'Token does not contain a Key ID (kid) and so cannot be verified.'
            }, 401)
    for key in jsonWebKeySet['keys']:
        if key['kid'] == unverified_token_header['kid']:
            rsa_key = {
                'kty': key['kty'],
                'kid': key['kid'],
                'use': key['use'],
                'n':   key['n'],
                'e':   key['e']
            }
    if rsa_key:
        try:
            payload = jwt.decode(token,
                                 rsa_key,
                                 algorithms=ALGORITHMS,
                                 audience=API_AUDIENCE,
                                 issuer='https://' + AUTH0_DOMAIN + '/')

            return payload

        except jwt.ExpiredSignatureError:
            raise AuthError(
                {
                    'code': 'token_expired',
                    'description': 'The token has expired and can no longer be used.'
                }, 401)

        except jwt.JWTClaimsError:
            raise AuthError(
                {
                    'code': 'invalid_token',
                    'description': 'There is a problem with the claims.'
                }, 401)
        except Exception:
            raise AuthError(
                {
                    'code': 'invalid_token',
                    'description': 'Unable to parse authentication token.'
                }, 400)

def RequiresAuth(permission=''):
    def requires_auth_decorator(f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            token = get_token_auth_header()
            payload = verify_decode_jwt(token)
            check_permissions(permission, payload)
            return f(payload, *args, **kwargs)
        return wrapper
    return requires_auth_decorator


import rospy
from py_trees import Status

import giskardpy.identifier as identifier
from giskardpy.tree.behaviors.plugin import GiskardBehavior

class CommandPublisher(GiskardBehavior):
    joint_names = []

    @profile
    def __init__(self, name, hz=100):
        super().__init__(name)
        self.hz = hz
        if not hasattr(self, 'joint_names'):
            raise NotImplementedError('you need to set joint names')
        self.world.register_controlled_joints(self.joint_names)
        self.stamp = None

    @profile
    def initialise(self):
        self.sample_period = self.god_map.get_data(identifier.sample_period)
        self.stamp = rospy.get_rostime()
        self.timer = rospy.Timer(period=rospy.Duration(1/self.hz), callback=self.publish_joint_state)
        super().initialise()

    def update(self):
        self.stamp = rospy.get_rostime()
        return Status.RUNNING

    def terminate(self, new_status):
        self.timer.shutdown()

    def publish_joint_state(self, time):
        raise NotImplementedError()


from flask import Flask, request
from flask_restful import Api, Resource
from flask_cors import CORS
import json
import os
from models import db, User, Article, Comment, Like

basedir = os.path.dirname(os.path.abspath(__file__))
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.db')
app = Flask(__name__)
app.config.update({
    'SQLALCHEMY_TRACK_MODIFICATIONS': True,
    "SQLALCHEMY_DATABASE_URI": SQLALCHEMY_DATABASE_URI,
})
cors = CORS(app)
api = Api(app)
db.init_app(app)

def serializer(l):
    ret = []
    for row in l:
        ret.append(json.loads(row.serialize()))
    return json.dumps(ret)

class ArticleList(Resource):
    def get_articles(self):
        articles = Article.query.all()
        return articles

    def get(self):
        articles = self.get_articles()
        return serializer(articles)

    def post(self):
        r_json = request.get_json()
        user_id = r_json['user_id']
        title = r_json['title']
        content = r_json['content']
        email = r_json['email']

        new_article = Article(user_id, title, content)
        db.session.add(new_article)
        db.session.commit()
        return "write successfully"

api.add_resource(ArticleList, '/api/articles')

if __name__ == '__main__':
    with app.app_context():
        db.create_all()
    app.run(host='0.0.0.0', port=5000, debug=True)