import fenics as fem
import numpy as np
from scipy import optimize

class Problem(object):
    def __init__(self, msh, model, solver):
        # TODO: link Solver to Model implicitly
        pass

    def solve(self, input):
        return self.solver(self.u, self.v, self.du, self.bcs, self.model)

def hyperelastic_uniaxial_compression(delta_z, msh, model, u_init=None, friction=True, q_deg=None):

    # --------------------
    # Function spaces (superseded by the mesh only)
    # --------------------
    V = fem.VectorFunctionSpace(msh, "Lagrange", 1)  # displacement function space
    u_tr = fem.TrialFunction(V)
    u_test = fem.TestFunction(V)

    # Define functions
    du = fem.TrialFunction(V)  # Incremental displacement
    v = fem.TestFunction(V)  # Test function
    u = fem.Function(V)  # Displacement from previous iteration

    if u_init is not None:
        u.vector().set_local(u_init)

    # --------------------
    # Boundary conditions (superseded by the mesh and loading)
    # --------------------

    # Mark boundary subdomians
    def bot(x, on_boundary):
        return (on_boundary and fem.near(x[2], 0.0))

    def top(x, on_boundary):
        return (on_boundary and fem.near(x[2], msh.coordinates()[:, 2].max()))

    if friction:
        bc_bot = fem.DirichletBC(V, fem.Constant((0.0, 0.0, 0.0)), bot)  # fixed bottom
        bc_top = fem.DirichletBC(V, fem.Constant((0.0, 0.0, delta_z)), top)  # fixed top
    else:
        bc_bot = fem.DirichletBC(V.sub(2), 0.0, bot)  # fixed bottom z only
        bc_top = fem.DirichletBC(V.sub(2), delta_z, top)  # fixed top z only

    bcs = [bc_bot, bc_top]

    # ------------------------
    # Hyperelastic solver part (superseded by mesh, bcs, and material model)
    # ------------------------

    # Total potential energy
    if q_deg is None:
        Pi = model.psi(u) * fem.dx  # - dot(B, u)*dx # - dot(T, u)*ds(1)
    else:
        Pi = model.psi(u) * fem.dx(metadata={'quadrature_degree': q_deg})

    # Compute first variation of Pi (directional derivative about u in the direction of v)
    F = fem.derivative(Pi, u, v)

    # Compute Jacobian of F
    J = fem.derivative(F, u, du)

    # Optimization options for the form compiler
    fem.parameters["form_compiler"]["cpp_optimize"] = True
    # additional options demonstrated here: https://fenicsproject.org/olddocs/dolfin/2016.2.0/python/demo/documented/hyperelasticity/python/demo_hyperelasticity.py.html

    problem = fem.NonlinearVariationalProblem(F, u, bcs, J)
    solver = fem.NonlinearVariationalSolver(problem)
    prm = solver.parameters
    prm['newton_solver']['maximum_iterations'] = 10

    solver.solve()

    return u

def get_reaction_force_area(u, mesh, model, dim=2, use_pk=False):

    mesh_d = mesh

    boundary_markers = fem.MeshFunction("size_t", mesh_d, mesh_d.topology().dim() - 1, 0)

    class TopBoundary(fem.SubDomain):
        def inside(self, x, on_boundary):
            return fem.near(x[2], mesh_d.coordinates()[:, 2].max()) and on_boundary

    # Mark the top boundary
    top_boundary = TopBoundary()
    boundary_markers.set_all(0)  # Initialize to 0
    top_boundary.mark(boundary_markers, 1)  # Mark the top boundary with 1

    if use_pk:
        #
        I = fem.Identity(3)  # Identity tensor
        F = I + fem.grad(u)  # Deformation gradient
        J = fem.det(F)
        sigma = model.sigma(u)
        P = J * sigma * fem.inv(F)
        # Compute the reaction force
        ds = fem.Measure("ds", domain=mesh_d, subdomain_data=boundary_markers)
        n = fem.FacetNormal(mesh_d)
        rz = [fem.assemble(fem.dot(P, n)[i] * ds(1)) for i in range(3)]
    else:
        # Compute the reaction force
        sigma = model.sigma(u)
        ds = fem.Measure("ds", domain=mesh_d, subdomain_data=boundary_markers)
        n = fem.FacetNormal(mesh_d)
        rz = [fem.assemble(fem.dot(sigma, n)[i] * ds(1)) for i in range(3)]

    surf = fem.assemble(fem.Constant(1.0) * ds(1))

    return rz, surf

def get_area(u, mesh, model, dim=2):
    mesh_d = fem.Mesh(mesh)

    boundary_markers = fem.MeshFunction("size_t", mesh_d, mesh_d.topology().dim() - 1, 0)

    class TopBoundary(fem.SubDomain):
        def inside(self, x, on_boundary):
            return fem.near(x[2], mesh_d.coordinates()[:, 2].max()) and on_boundary

    # Mark the top boundary
    top_boundary = TopBoundary()
    boundary_markers.set_all(0)  # Initialize to 0
    top_boundary.mark(boundary_markers, 1)  # Mark the top boundary with 1

    # Deform the mesh
    fem.ALE.move(mesh_d, u)

    # Measure the area of the top surface
    ds = fem.Measure("ds", domain=mesh_d, subdomain_data=boundary_markers)
    surf = fem.assemble(fem.Constant(1.0) * ds(1))

    return surf

def generate_force_curve(model, mesh, bv, target_strain, max_stress=10e6, interp_mode="continue", alpha=0.50):

    u_sols = []

    force = np.zeros_like(target_strain) * np.nan

    pbar = tqdm.tqdm(enumerate(target_strain), total=len(target_strain))
    for i, eps in pbar:

        u_init = None
        if interp_mode == "continue":
            if i > 1:
                u_init = u_sols[i-1]
        elif interp_mode == "taylor":
            # nonlinear warm start
            if i == 1:    # do order-zero taylor expansion
                u_init = u_sols[0]
            elif i == 2:  # do order-one taylor expansion
                step = (u_sols[i-1] - u_sols[i-2])
                u_init = u_sols[i-1] + alpha * step
            elif i == 3:  # do order-two taylor expansion
                step = (u_sols[i-1] - u_sols[i-2]) + (u_sols[i-1] - 2*u_sols[i-2] + u_sols[i-3])
                u_init = u_sols[i-1] + alpha * step
            elif i == 4:  # do order-three taylor expansion
                step = (u_sols[i-1] - u_sols[i-2]) + (u_sols[i-1] - 2*u_sols[i-2] + u_sols[i-3]) + (u_sols[i-1] - 3*u_sols[i-2] + 3*u_sols[i-3] - u_sols[i-4])
                u_init = u_sols[i-1] + alpha * step
            elif i >= 5:  # do order-four taylor expansion
                step = (u_sols[i-1] - u_sols[i-2]) + (u_sols[i-1] - 2*u_sols[i-2] + u_sols[i-3]) + (u_sols[i-1] - 3*u_sols[i-2] + 3*u_sols[i-3] - u_sols[i-4]) + (u_sols[i-1] - 4*u_sols[i-2] + 6*u_sols[i-3] - 4*u_sols[i-4] + u_sols[i-5])
                u_init = u_sols[i-1] + alpha * step
        elif interp_mode == "proportional":
            if i > 1:
                u_init = u_sols[i-1] * target_strain[i] / target_strain[i-1]
        elif interp_mode == "secant":
            if i > 1:
                u_init = u_sols[i-1] - (target_strain[i-1] * (u_sols[i-1] - u_sols[i-2])) / (target_strain[i-1] - target_strain[i-2])

        disp = -eps*bv.height
        try:
            u = sim.scenarios.hyperelastic_uniaxial_compression(disp, mesh, model, u_init=u_init)
            u_sols.append(np.array(u.vector()))
        except RuntimeError:
            break  # save progress so far

        f, area = get_reaction_force_area(u, mesh, model)
        force[i] = f[2]

        stress = f[2] / area
        if np.abs(stress) > max_stress:
            break

    return force / area

def analytical_uniaxial_stress(stress_model, trueStrainVec):
    stress = np.zeros(len(trueStrainVec))
    for i in range(len(trueStrainVec)):
        lam1 = 1. + trueStrainVec[i]
        calcS22Abs = lambda x : np.abs(stress_model([lam1, x.tolist()[0], x.tolist()[0]])[1, 1])
        lam2 = optimize.fmin(calcS22Abs, x0=1/np.sqrt(lam1), xtol=1e-9, ftol=1e-9, disp=False)
        stress[i] = stress_model([lam1, lam2.tolist()[0], lam2.tolist()[0]])[0, 0]
    return stress


from flask import Flask, request, jsonify, make_response, abort
from flask_sqlalchemy import SQLAlchemy
import os

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///lilekov.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app=app)

class Users(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(length=20), nullable=False)
    surname = db.Column(db.String(length=20), nullable=False)
    phone = db.Column(db.Integer, nullable=False)

    def __repr__(self):
        return '<Users %r>' % self.id

    def get_user_info(self):
        return {
            'id': self.id,
            'user_name': self.name,
            'user_surname': self.surname,
            'user_phone': self.phone
        }

@app.route('/users', methods=['POST'])
def add_user():
    if not request.json:
        abort(400)

    new_user = Users(name=request.json['name'],
                     surname=request.json['surname'], phone=request.json['phone'])
    try:
        db.session.add(new_user)
        db.session.commit()
        return jsonify({'result': True}), 201
    except:
        abort(400)

@app.route('/users', methods=['GET'])
def all_users():
    users = [user.get_user_info() for user in Users.query.all()]
    return jsonify({'users': users})

@app.route('/users/<int:user_id>', methods=['DELETE'])
def delete_user(user_id):
    find_id = Users.query.get_or_404(user_id)

    try:
        db.session.delete(find_id)
        db.session.commit()
        return jsonify({'result': True})
    except:
        abort(400)

@app.errorhandler(404)
def not_found(error):
    return make_response(jsonify({'error': 'Not Found'}), 404)

if __name__ == '__main__':
    if not os.path.exists('lilekov.db'):
        db.create_all()

    app.run(debug=True, host='0.0.0.0',  port='80')


"""empty message

Revision ID: f293e999f5de
Revises:
Create Date: 2023-02-28 12:39:58.945500

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'f293e999f5de'
down_revision = None
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('purchases',
    sa.Column('id_purchase', sa.Integer(), nullable=False),
    sa.Column('date', sa.Date(), nullable=True),
    sa.Column('name', sa.String(length=120), nullable=True),
    sa.Column('brand', sa.String(length=120), nullable=True),
    sa.Column('season', sa.String(length=120), nullable=True),
    sa.Column('price', sa.Float(), nullable=True),
    sa.Column('amount_purchase', sa.Integer(), nullable=True),
    sa.PrimaryKeyConstraint('id_purchase')
    )
    op.create_index(op.f('ix_purchases_date'), 'purchases', ['date'], unique=False)
    op.create_table('user',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name_user', sa.String(), nullable=False),
    sa.Column('email', sa.String(), nullable=False),
    sa.Column('password', sa.String(), nullable=False),
    sa.Column('is_admin', sa.Boolean(), nullable=False),
    sa.Column('is_user', sa.Boolean(), nullable=False),
    sa.Column('not_active', sa.Boolean(), nullable=False),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('email')
    )
    op.create_table('sales',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('date', sa.Date(), nullable=True),
    sa.Column('purchase_id', sa.Integer(), nullable=True),
    sa.Column('amount_sale', sa.Integer(), nullable=True),
    sa.Column('total', sa.Float(), nullable=True),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['purchase_id'], ['purchases.id_purchase'], ),
    sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_sales_date'), 'sales', ['date'], unique=False)
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_sales_date'), table_name='sales')
    op.drop_table('sales')
    op.drop_table('user')
    op.drop_index(op.f('ix_purchases_date'), table_name='purchases')
    op.drop_table('purchases')
    # ### end Alembic commands ###


"""Module to query till request is complete."""

import time
from collections.abc import Callable
from typing import Any

from requests import exceptions

from lol_stats import logging

_LOGGER = logging.create_colored_logger(__name__)

def query(
    command: Callable,
    args: list[Any] = [],  # noqa: B006
    kwargs: dict[Any, Any] = {},  # noqa: B006
) -> dict:
    while True:
        try:
            return command(*args, **kwargs)
        except exceptions.HTTPError as error:
            _LOGGER.info(
                msg=(
                    "Waiting for 120 seconds for the next request.\n"
                    "This is likely required because because you exceeded your limit."
                ),
            )
            _LOGGER.debug(str(error))
            time.sleep(120)


import csv
import statistics

exclude_vets = ["15-01", "15-02", "15-03", "15-04", "15-05",
                "15-06", "15-07", "15-08", "15-09", "15-10", "15-11", "15-12"]

with open('Merged4.csv', mode='r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    line_count = 0

    haveGender = 0
    haveFlair = 0
    excluded = 0

    Women_timeTillFlairChange = []
    Men_timeTillFlairChange = []
    Overall_timeTillFlairChange = []

    durationWomen = {}
    durationMen = {}
    durationFlairs = {}
    # durationNonFlairs = {}

    for row in csv_reader:
        # if(line_count > 1000):
        #     break
        if(line_count % 300000 == 0):
            print(line_count)
        if line_count == 0:
            print(f'Column names are {", ".join(row)}')
        else:
            try:
                first_date = row['first-date']
                flairChangeDate = row['flair-change-date'][2:-3]
                month = int(float(row['first-date-month']))
                duration = int(float(row['duration'])) + 1
                consistency = int(float(row['consistency-months-commented']))
                this_gender = row['loseItGender']

                if len(flairChangeDate) < 1:
                    # print("NO FLAIR")
                    continue

                if first_date in exclude_vets:
                    excluded += 1
                    line_count += 1
                    continue

                helper_firstYear = int(first_date[:2])
                helper_firstMonth = int(first_date[-2:])
                helper_changeYear = int(flairChangeDate[:2])
                helper_changeMonth = int(flairChangeDate[-2:])

                months_till_flair_change = 1000

                if(helper_changeYear == helper_firstYear):
                    months_till_flair_change = helper_changeMonth - helper_firstMonth
                else:
                    months_till_flair_change = (
                        helper_changeYear - helper_firstYear)*12 + (helper_changeMonth - helper_firstMonth)

                # if first_date == flairChangeDate:
                # print(this_gender, first_date, flairChangeDate,
                #       months_till_flair_change, duration)

                if(len(this_gender) == 1 or len(row['gender']) == 1):
                    haveGender += 1
                    if('F' in this_gender.upper()):
                        Women_timeTillFlairChange.append(duration)
                        try:
                            durationWomen[months_till_flair_change].append(
                                duration)
                        except:
                            durationWomen[months_till_flair_change] = [
                                duration]
                    else:
                        Men_timeTillFlairChange.append(duration)
                        try:
                            durationMen[months_till_flair_change].append(
                                duration)
                        except:
                            durationMen[months_till_flair_change] = [
                                duration]

                if not(row['flair'] == 'None' or row['flair'] == 'New'):
                    Overall_timeTillFlairChange.append(duration)
                    try:
                        durationFlairs[months_till_flair_change].append(
                            duration)
                    except:
                        durationFlairs[months_till_flair_change] = [
                            duration]
                # else:

                #     try:
                #         durationNonFlairs[months_till_flair_change].append(
                #             duration)
                #     except:
                #         durationNonFlairs[months_till_flair_change] = [
                #             duration]

            except:
                continue

        line_count += 1

    # print(statistics.median(durationMen),
    #       statistics.median(durationWomen), statistics.median(durationFlairs), statistics.median(durationNonFlairs))
    print("**************************************************WOMEN")
    for key in sorted(durationWomen):
        print(key, len(durationWomen[key]))
    print("**************************************************MEN")
    for key in sorted(durationMen):
        print(key, len(durationMen[key]))
    print("**************************************************OVERALL")
    for key in sorted(durationFlairs):
        print(key, len(durationFlairs[key]))
    print(f'Processed {line_count} lines.')


import face_recognition
import cv2
import sys
import numpy as np
import configparser

# Jakub Podolak (jjp241) 2019, based on face_recognition module: https://github.com/ageitgey/face_recognition
# Totally OpenSource project, hope u all have fun!

# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.

#function to read paths to face images from config file
def get_faces(config):
    face_files = config.items("FACES")
    faces = [second for first,second in face_files]
    return faces

#function reads names of faces
def get_names(config):
    face_files = config.items("FACES")
    names = [first for first,second in face_files]
    return names

#function creates array of face encodings
def get_face_encodings(faces):
    known_face_encodings = []
    for face in faces:
        image = face_recognition.load_image_file(face)
        temp_encoding = face_recognition.face_encodings(image)[0]
        known_face_encodings.append(temp_encoding)
    return known_face_encodings

def run():
    #initialize config parser, read from config file passed as an argument
    config_file = sys.argv[1]
    config = configparser.ConfigParser()
    config.read(config_file)

    #read some settings from config file
    output_names_path = config['SETTINGS']['output']
    compression_factor = float(config['SETTINGS']['compression_factor'])
    webcam_port = int(config['SETTINGS']['webcam_port'])
    kill_key = config['SETTINGS']['kill_key']
    #create arrays for our face recognition
    face_paths = get_faces(config)
    known_face_names = get_names(config)
    known_face_encodings = get_face_encodings(face_paths)

    # Initialize some variables
    face_locations = []
    face_encodings = []
    face_names = []
    process_this_frame = True

    # Get a reference to webcam on specified port (the default one)
    video_capture = cv2.VideoCapture(webcam_port)

    while True:
        # Grab a single frame of video
        ret, frame = video_capture.read()

        # Resize frame of video for faster face recognition processing
        small_frame = cv2.resize(frame, (0, 0), fx= compression_factor, fy= compression_factor)

        # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
        rgb_small_frame = small_frame[:, :, ::-1]

        # Only process every other frame of video to save time
        if process_this_frame:
            # Find all the faces and face encodings in the current frame of video
            face_locations = face_recognition.face_locations(rgb_small_frame)
            face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
            #print(face_locations)
            face_names = []
            for face_encoding in face_encodings:
                # See if the face is a match for the known face(s)
                matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
                name = "Unknown"

                # Use the known face with the smallest distance to the new face
                face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
                best_match_index = np.argmin(face_distances)
                if matches[best_match_index]:
                    #get the name of best matching face
                    name = known_face_names[best_match_index]
                    #write it to the buffer
                    buffer = open(output_names_path, "a")
                    buffer.write('\n' + name)
                    buffer.close()

                face_names.append(name)

        process_this_frame = not process_this_frame

        # Display the results
        for (top, right, bottom, left), name in zip(face_locations, face_names):
            # Scale back up face locations since the frame we detected in was scaled
            top *= int(1/compression_factor)
            right *= int(1/compression_factor)
            bottom *= int(1/compression_factor)
            left *= int(1/compression_factor)

            # Draw a box around the face
            cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

            # Draw a label with a name below the face
            cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
            font = cv2.FONT_HERSHEY_DUPLEX
            cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

        # Display the resulting image
        cv2.imshow('Video', frame)

        # Hit kill_key specified in config file on the keyboard to quit!
        if cv2.waitKey(1) & 0xFF == ord(kill_key):
            break

    # Release handle to the webcam
    video_capture.release()
    cv2.destroyAllWindows()

run()


# -*- coding: utf-8 -*-
# __author__ = 'xuxi_20210911'

import os
from biocluster.workflow import Workflow
import datetime
import unittest
import types
import os
import glob
from Bio import SeqIO
from biocluster.core.exceptions import OptionError

class RocplotWorkflow(Workflow):
    """
    Rocplot base
    """
    def __init__(self, wsheet_object):
        self._sheet = wsheet_object
        super(RocplotWorkflow, self).__init__(wsheet_object)
        options = [
            {"name": "infile", "type": "infile", "format": "ref_rna_v2.common"},
            {"name": "main_id", "type": "string"},
            {'name': 'update_info', 'type': 'string'}
        ]
        self.add_option(options)
        self.revise_infiles()
        self.Rocplot = self.add_tool("tool_lab.rocplot")
        self.set_options(self._sheet.options())

    def run(self):
        self.run_Rocplot()
        super(RocplotWorkflow, self).run()

    def check_options(self):
        if not self.option("infile").is_set:
            raise OptionError("必须设置输入表达量文件")
        return True

    def run_Rocplot(self):
        opts = {
            "infile":self.option('infile').prop['path']
        }
        self.Rocplot.set_options(opts)
        self.Rocplot.on('end', self.set_db)
        self.Rocplot.run()

    def set_db(self):
        """
        保存结果表到mongo数据库中
        """
        if os.path.exists(os.path.join(self.Rocplot.output_dir, 'roc.pdf')):
            pdf_s3_path = os.path.join(self._sheet.output,"roc.pdf")
        else:
            pdf_s3_path = None
        mongo_api = self.api.api('tool_lab.rocplot')
        mongo_api.add_rocplot(
            s3_output=pdf_s3_path,
            main_id = self.option('main_id'),
        )
        self.end()

    def end(self):
        result_dir = self.add_upload_dir(self.Rocplot.output_dir)
        result_dir.add_relpath_rules([
            [".", "", "ROC",0],
            ['./roc.pdf', '', 'ROC', 0],
        ])
        super(RocplotWorkflow, self).end()

class TestFunction(unittest.TestCase):
    """
    This is test for the workflow. Just run this script to do test.
    """

    def test_this(self):
        cmd = 'python /mnt/ilustre/users/sanger-dev/biocluster/bin/webapitoollabtest.py '
        cmd += 'post toollabpipeline '
        cmd += '-c {} '.format("client03")
        cmd += "-b http://bcl.tsg.com "
        cmd += "-n \"params;basis\" -d \"{"
        args = dict(
            vcf_path='/mnt/ilustre/users/sanger-dev/sg-users/zhangyitong/test/tool_052021/bsa/pop.final.vcf',
            # wp='',
            # mp='HQS1',
            mb='XS11_1',
            wb='F44_mix',
        )
        config = dict(
            type="workflow",
            task_type="submit",
            name="tool_lab.bsa",
            main_table_name="sg_bsa",
            task_id="bsa",
            project_sn="bsa",
            submit_location="bsa"
        )
        for arg in args:
            cmd += "\\\""
            cmd += arg
            cmd += "\\\":\\\""
            cmd += args[arg]
            cmd += "\\\","
        cmd = cmd.rstrip(",")
        cmd += "};{"
        for arg in config:
            cmd += "\\\""
            cmd += arg
            cmd += "\\\":\\\""
            cmd += config[arg]
            cmd += "\\\","
        cmd = cmd.rstrip(",")
        cmd += "}\""

        print(cmd)
        os.system(cmd)

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


#Check if a number is Palindrome
s = input("enter the value")
reverse = s[::-1]
if(s==reverse):
    print("palindrome")
else:
    print("no palindrome")

#Program for Sum of the digits of a given number
n = int(input())
sum = 0
while n>0:
    reminder = n % 10
    sum = sum + reminder
    n = n//10
print(sum)

#Given a number n, find sum of first n natural numbers
n=int(input("enter a number"))
sum=0
for i in range(1,n+1):
    sum=sum+i
print("the sum is : ",sum)

#Given two number x and y, find product using
#recursion
def product(a,b):
    if(a<b):
        return product(b,a)
    elif(b!=0):
        return(a+product(a,b-1))
    else:
        return 0
a=int(input("enter the first number: "))
b=int(input("enter the sacond number: "))
print("product of the provided number is: ",product(a,b))


from collections import defaultdict
from functools import lru_cache
ddict =dict()
with open('input.txt') as f:
    for line in f:
        flines = line.split(' ')
        start = flines[1]
        f_rate = flines[4][5:-1]
        ends = [s.strip().strip(',').strip('\n') for s in flines[9:]]
        ddict[start] = [int(f_rate), ends]
@lru_cache(maxsize=None)

def maxflow(cur, opened, min_left):
    if min_left <= 0:
        return 0
    best = 0
    if cur not in opened:
        val = (min_left - 1) * ddict[cur][0]
        cur_opened = tuple(sorted(opened + (cur,)))
        for adj in ddict[cur][1]:
            if val > 0:
                best = max(best ,
                    val + maxflow(adj, cur_opened, min_left - 2))
            best = max(best,
                maxflow(adj , opened, min_left - 1))
    else:
        for(adj) in ddict[cur][1]:
            best = max(best,
                maxflow(adj, opened, min_left - 1))
    return best

print("Part 1: ",maxflow('AA', (), 30))

@lru_cache(maxsize=None)
def maxflow2(my_cur, e_curr, opened, min_left):
    if min_left <= 0:
        return 0
    best = 0
    val_my = (min_left - 1) * ddict[my_cur][0]
    val_e = (min_left - 1) * ddict[e_curr][0]
    for adj_my in ddict[my_cur][1]:
        for adj_e in ddict[e_curr][1]:
            if adj_my == adj_e:
                continue
            if adj_my not in opened and adj_e not in opened:
                best = max(best,
                    val_my + val_e + maxflow2(adj_my, adj_e, opened + (adj_my, adj_e), min_left - 2))
            elif adj_my not in opened:
                best = max(best,
                    val_my + maxflow2(adj_my, adj_e, opened + (adj_my,), min_left - 2))
            elif adj_e not in opened:
                best = max(best,
                    val_e + maxflow2(adj_my, adj_e, opened + (adj_e,), min_left - 2))
            else:
                best = max(best,
                    maxflow2(adj_my, adj_e, opened, min_left - 2))
    return best

print(maxflow2('AA','AA', (), 26))


class Solution:
    def backpack(self, nums, target):
        nums = sorted(nums)
        self.res = []
        self.dfs(nums, target, 0, [])
        return self.res

    def dfs(self, nums, target, idx, lt):
        if target == 0:
            self.res.append(lt[:])
            return
        if idx == len(nums) or target < 0:
            return

        #key = str(idx) + "-" + str(target)
        for j in range(idx, len(nums)):
            self.dfs(nums, target-nums[j], j, lt + [nums[j]])

        return

obj = Solution()
nums = [1, 2, 3]
target = 6
print(obj.backpack(nums, target))


import gensim
from gensim.models import Word2Vec
import numpy as np
from gensim.models import KeyedVectors

def featureVector(input,num_features):
    words = input.split()
    featureVec = np.zeros((num_features,), dtype='float32')
    num_words = 0
    for word in words:
        num_words += 1
        featureVec = np.add(featureVec, gensim.models.Word2Vec(word, min_count = 1,size = 100, window = 5))

    if (num_words > 0):
        featureVec = np.divide(featureVec, num_words)

    return featureVec

print("[INFO] Model loaded...")


import string
from nltk.translate.bleu_score import corpus_bleu, SmoothingFunction

token_path = "Flickr8K_Text/Flickr8k.token.txt"
trans_path = 'backtrans_output.txt'

table = str.maketrans('', '', string.punctuation)
bleu_w = {
        "bleu-1": [1.0],
        "bleu-2": [0.5, 0.5],
        "bleu-3": [0.333, 0.333, 0.333],
        "bleu-4": [0.25, 0.25, 0.25, 0.25]
    }

def avg(lst):
    return sum(lst) / len(lst)

with open(token_path, "r") as f:
    data = []
    for line in f.readlines() :
        tokens = line.split()
        img_name, caption_words = tokens[0].split("#")[0], tokens[1:]
        caption_words = [token.strip().lower().translate(table) for token in caption_words]
        data.append(caption_words)

with open(trans_path, "r") as f:
    trans_data = []
    for line in f.readlines() :
        tokens = line.split()
        img_name, caption_words = tokens[0].split("#")[0], tokens[1:]
        caption_words = [token.strip().lower().translate(table) for token in caption_words]
        trans_data.append(caption_words)

chencherry = SmoothingFunction()
bleu = {"b1": [],
        "b2": [],
        "b3": [],
        "b4": []
        }

for i in range(len(data)):
    bleu_1 = corpus_bleu([[data[i]]], [trans_data[i]], weights=bleu_w["bleu-1"], smoothing_function=chencherry.method3) * 100
    bleu_2 = corpus_bleu([[data[i]]], [trans_data[i]], weights=bleu_w["bleu-2"], smoothing_function=chencherry.method3) * 100
    bleu_3 = corpus_bleu([[data[i]]], [trans_data[i]], weights=bleu_w["bleu-3"], smoothing_function=chencherry.method3) * 100
    bleu_4 = corpus_bleu([[data[i]]], [trans_data[i]], weights=bleu_w["bleu-4"], smoothing_function=chencherry.method3) * 100
    bleu["b1"].append(bleu_1)
    bleu["b2"].append(bleu_2)
    bleu["b3"].append(bleu_3)
    bleu["b4"].append(bleu_4)

print(avg(bleu["b1"]), avg(bleu["b2"]), avg(bleu["b3"]), avg(bleu["b4"]))


grammatical_construct_list = [

    # KEYWORDS
    'as',
    'assert',
    'check-sat',
    'check-sat-assuming',
    'declare-const',
    'declare-datatype',
    'declare-datatypes',
    'declare-fun',
    'declare-sort',
    'define-fun',
    'define-fun-rec',
    'define-funs-rec',
    'define-sort',
    'echo',
    'exit',
    'get-assertions',
    'get-assignment',
    'get-assignment',
    'get-info',
    'get-info',
    'get-model',
    'get-option',
    'get-option',
    'get-proof',
    'get-unsat-assumptions',
    'get-unsat-core',
    'get-value',
    'pop',
    'push',
    'reset',
    'reset-assertions',
    'set-info',
    'set-logic',
    'set-option',

    # BINDERS
    'exists',
    'forall',
    'let',

    # CORE
    'true',
    'false',
    'not',
    '=>',
    'and',
    'or',
    'xor',
    '=',
    'distinct',
    'ite',
    'Bool',

    # ARRAYS
    'Array',
    'select',
    'store',

    # BV
    'BitVec',
    'concat',
    'extract',
    'bvnot',
    'bvand',
    'bvor',
    'bvneg',
    'bvadd',
    'bvmul',
    'bvudiv',
    'bvurem',
    'bvshl',
    'bvlshr',
    'bvult',
    'bvnand',
    'bvnor',
    'bvxor',
    'bvxnor',
    'bvcomp',
    'bvsub',
    'bvsdiv',
    'bvsrem',
    'bvsmod',
    'bvashr',
    'repeat',
    'zero_extend',
    'sign_extend',
    'rotate_left',
    'rotate_right',
    'bvule',
    'bvugt',
    'bvuge',
    'bvslt',
    'bvsle',
    'bvsgt',
    'bvsge',

    # FP
    'RoundingMode',
    'FloatingPoint',
    'Float16',
    'Float32',
    'Float64',
    'Float128',
    'fp',

    'roundNearestTiesToEven',
    'roundNearestTiesToAway',
    'roundTowardPositive',
    'roundTowardNegative',
    'roundTowardZero',

    'RNE',
    'RNA',
    'RTP',
    'RTN',
    'RTZ',

    'fp.abs',
    'fp.neg',
    'fp.add',
    'fp.sub',
    'fp.mul',
    'fp.div',
    'fp.fma',
    'fp.sqrt',
    'fp.rem',
    'fp.roundToIntegral',
    'fp.min',
    'fp.max',
    'fp.leq',
    'fp.lt',
    'fp.geq',
    'fp.gt',
    'fp.eq',
    'fp.isNormal',
    'fp.isSubnormal',
    'fp.isZero',
    'fp.isInfinite',
    'fp.isNaN',
    'fp.isNegative',
    'fp.isPositive',
    'to_fp',
    'to_fp_unsigned',
    'fp.to_ubv',
    'fp.to_sbv',
    'fp.to_real',

    # INTS+REAL
    'Int',
    '-',
    '+',
    '*',
    'div',
    'mod',
    'abs',
    '<=',
    '<',
    '>=',
    '>',
    'to_real',
    'to_int',
    'is_int',
    'Real',

    'String',
    'RegLan',
    "str.++",
    "str.len",
    "str.<",
    "str.to_re",
    "str.in_re",
    "re.none",
    "re.all",
    "re.allchar",
    "re.++",
    "re.union",
    "re.inter",
    "re.*",
    "str.<=",
    "str.at",
    "str.substr",
    "str.prefixof",
    "str.suffixof",
    "str.contains",
    "str.indexof",
    "str.replace",
    "str.replace_all",
    "str.replace_re",
    "str.replace_re_all",
    "re.comp",
    "re.diff",
    "re.comp",
    "re.diff",
    "re.opt",
    "re.range",
    "re.range",
    "re.loop",
    "re.^",
    "str.is_digit",
    "str.to_code",
    "str.from_code",
    "str.to_int",
    "str.from_int",
]


from rest_framework import serializers
from accounts.models import ShoppingListItem
from posts.serializers import IngredientQuantitySerializer

class ShoppingListItemSerializer(serializers.ModelSerializer):
    ingredients = serializers.SerializerMethodField('get_ingredients',
                                                    read_only=True)
    userid = serializers.PrimaryKeyRelatedField(read_only=True)

    class Meta:
        model = ShoppingListItem
        fields = ('id', 'item', 'userid', 'ingredients')

    def create(self, validated_data):
        userid = self.context['request'].user
        item = ShoppingListItem.objects.create(
            userid=userid,
            item=validated_data.get('item')
        )
        return item

    def get_ingredients(self, obj):
        return IngredientQuantitySerializer(obj.item).data


import contextvars
import re

from absl import flags
from google.cloud.logging import handlers
from pdf_sprinkles import iap_auth
import tornado.web

FLAGS = flags.FLAGS
flags.DEFINE_string('expected_audience', None, 'Expected audience for IAP.')

_TRACE_ID_LABEL = 'appengine.googleapis.com/trace_id'

http_request = contextvars.ContextVar('http_request', default={})
trace_id = contextvars.ContextVar('trace_id', default=None)
span_id = contextvars.ContextVar('span_id', default=None)

def get_request_data():
  return http_request.get(), trace_id.get(), span_id.get()

def parse_trace_span(header: str):
  """Parses a Cloud Tracing span from its header."""
  spans = re.match(r'^(?P<trace_id>\w+)(?:/(?P<span_id>\w+))?', header)
  if not spans:
    return None, None
  else:
    return (spans.groupdict().get('trace_id'),
            spans.groupdict().get('span_id'))

def set_request_data(request: tornado.httputil.HTTPServerRequest):
  """Records request data in context vars for async logging."""
  http_request.set({
      'requestMethod': request.method,
      'requestUrl': request.uri,
      'requestSize': len(request.body) if isinstance(request.body, str) else 0,
      'userAgent': request.headers.get('User-Agent', ''),
      'remoteIp': request.remote_ip,
      'referer': request.headers.get('Referer', ''),
      'protocol': request.protocol,
  })

  extracted = parse_trace_span(request.headers.get('X-Cloud-Trace-Context', ''))
  trace_id.set(extracted[0])
  span_id.set(extracted[1])

class RequestHandler(tornado.web.RequestHandler):
  """Request handler that records trace context."""

  async def prepare(self):
    set_request_data(self.request)

    if FLAGS.expected_audience and self.request.path != '/_ah/warmup':
      iap_jwt = self.request.headers['X-Goog-IAP-JWT-Assertion']
      _, user_email = await iap_auth.validate_iap_jwt(iap_jwt,
                                                      FLAGS.expected_audience)
      self.current_user = user_email

    return super().prepare()

class LoggingHandler(handlers.AppEngineHandler):
  """Sets user overrides for App Engine request logging."""

  def emit(self, record):
    inferred_http, inferred_trace, _ = get_request_data()
    if inferred_http:
      setattr(record, 'http_request', inferred_http)
    if inferred_trace:
      setattr(record, 'trace',
              f'projects/{self.project_id}/traces/{inferred_trace}')
      setattr(record, 'labels', {_TRACE_ID_LABEL: inferred_trace})
    super().emit(record)


'''
How to use:
1. Scrap the information into excel.
2. Save it into .csv format.
3. Put the .csv file and csv_mod.py in the same directory.
4. Modify 'num' into number of columns you want to sort.
5. Run 'python csv_mod.py' in terminal.
6. Open 'result.csv' file.

'''
import csv

def opencsv(filename):
    f = open(filename, 'r')
    reader = csv.reader(f)
    output = []
    for i in reader:
        output.append(i)
    return output

def writecsv(filename, the_list):
    with open(filename, 'w', newline = '') as f:
        a = csv.writer(f, delimiter = ',')
        a.writerows(the_list)

def sortcsv(input, num):
    output = []

    for i in range(0,len(input)-1,num):
        output.append([])
        for j in range(num):
            output[i//num].append(input[i+j][0])
    return output

def main():
    num = 3 #number of columns you want to sort
    input = opencsv('excel_test.csv')
    output = sortcsv(input, num)
    writecsv('result.csv', output)

if __name__ == "__main__":
    main()


import re
import string

CC_SOURCE_FILES=(r'^cc/.*\.(cc|h)$',)

def CheckChangeLintsClean(input_api, output_api):
  input_api.cpplint._cpplint_state.ResetErrorCounts()  # reset global state
  source_filter = lambda x: input_api.FilterSourceFile(
    x, white_list=CC_SOURCE_FILES, black_list=None)
  files = [f.AbsoluteLocalPath() for f in
           input_api.AffectedSourceFiles(source_filter)]
  level = 1  # strict, but just warn

  for file_name in files:
    input_api.cpplint.ProcessFile(file_name, level)

  if not input_api.cpplint._cpplint_state.error_count:
    return []

  return [output_api.PresubmitPromptWarning(
    'Changelist failed cpplint.py check.')]

def CheckAsserts(input_api, output_api, white_list=CC_SOURCE_FILES, black_list=None):
  black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST)
  source_file_filter = lambda x: input_api.FilterSourceFile(x, white_list, black_list)

  assert_files = []
  notreached_files = []

  for f in input_api.AffectedSourceFiles(source_file_filter):
    contents = input_api.ReadFile(f, 'rb')
    # WebKit ASSERT() is not allowed.
    if re.search(r"\bASSERT\(", contents):
      assert_files.append(f.LocalPath())
    # WebKit ASSERT_NOT_REACHED() is not allowed.
    if re.search(r"ASSERT_NOT_REACHED\(", contents):
      notreached_files.append(f.LocalPath())

  if assert_files:
    return [output_api.PresubmitError(
      'These files use ASSERT instead of using DCHECK:',
      items=assert_files)]
  if notreached_files:
    return [output_api.PresubmitError(
      'These files use ASSERT_NOT_REACHED instead of using NOTREACHED:',
      items=notreached_files)]
  return []

def CheckStdAbs(input_api, output_api,
                white_list=CC_SOURCE_FILES, black_list=None):
  black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST)
  source_file_filter = lambda x: input_api.FilterSourceFile(x,
                                                            white_list,
                                                            black_list)

  using_std_abs_files = []
  found_fabs_files = []
  missing_std_prefix_files = []

  for f in input_api.AffectedSourceFiles(source_file_filter):
    contents = input_api.ReadFile(f, 'rb')
    if re.search(r"using std::f?abs;", contents):
      using_std_abs_files.append(f.LocalPath())
    if re.search(r"\bfabsf?\(", contents):
      found_fabs_files.append(f.LocalPath());

    no_std_prefix = r"(?<!std::)"
    # Matches occurrences of abs/absf/fabs/fabsf without a "std::" prefix.
    abs_without_prefix = r"%s(\babsf?\()" % no_std_prefix
    fabs_without_prefix = r"%s(\bfabsf?\()" % no_std_prefix
    # Skips matching any lines that have "// NOLINT".
    no_nolint = r"(?![^\n]*//\s+NOLINT)"

    expression = re.compile("(%s|%s)%s" %
        (abs_without_prefix, fabs_without_prefix, no_nolint))
    if expression.search(contents):
      missing_std_prefix_files.append(f.LocalPath())

  result = []
  if using_std_abs_files:
    result.append(output_api.PresubmitError(
        'These files have "using std::abs" which is not permitted.',
        items=using_std_abs_files))
  if found_fabs_files:
    result.append(output_api.PresubmitError(
        'std::abs() should be used instead of std::fabs() for consistency.',
        items=found_fabs_files))
  if missing_std_prefix_files:
    result.append(output_api.PresubmitError(
        'These files use abs(), absf(), fabs(), or fabsf() without qualifying '
        'the std namespace. Please use std::abs() in all places.',
        items=missing_std_prefix_files))
  return result

def CheckPassByValue(input_api,
                     output_api,
                     white_list=CC_SOURCE_FILES,
                     black_list=None):
  black_list = tuple(black_list or input_api.DEFAULT_BLACK_LIST)
  source_file_filter = lambda x: input_api.FilterSourceFile(x,
                                                            white_list,
                                                            black_list)

  local_errors = []

  # Well-defined simple classes containing only <= 4 ints, or <= 2 floats.
  pass_by_value_types = ['base::Time',
                         'base::TimeTicks',
                         ]

  for f in input_api.AffectedSourceFiles(source_file_filter):
    contents = input_api.ReadFile(f, 'rb')
    match = re.search(
      r'\bconst +' + '(?P<type>(%s))&' %
        string.join(pass_by_value_types, '|'),
      contents)
    if match:
      local_errors.append(output_api.PresubmitError(
        '%s passes %s by const ref instead of by value.' %
        (f.LocalPath(), match.group('type'))))
  return local_errors

def CheckTodos(input_api, output_api):
  errors = []

  source_file_filter = lambda x: x
  for f in input_api.AffectedSourceFiles(source_file_filter):
    contents = input_api.ReadFile(f, 'rb')
    if ('FIX'+'ME') in contents:
      errors.append(f.LocalPath())

  if errors:
    return [output_api.PresubmitError(
      'All TODO comments should be of the form TODO(name).',
      items=errors)]
  return []

def FindUnquotedQuote(contents, pos):
  match = re.search(r"(?<!\\)(?P<quote>\")", contents[pos:])
  return -1 if not match else match.start("quote") + pos

def FindNamespaceInBlock(pos, namespace, contents, whitelist=[]):
  open_brace = -1
  close_brace = -1
  quote = -1
  name = -1
  brace_count = 1
  quote_count = 0
  while pos < len(contents) and brace_count > 0:
    if open_brace < pos: open_brace = contents.find("{", pos)
    if close_brace < pos: close_brace = contents.find("}", pos)
    if quote < pos: quote = FindUnquotedQuote(contents, pos)
    if name < pos: name = contents.find(("%s::" % namespace), pos)

    if name < 0:
      return False # The namespace is not used at all.
    if open_brace < 0:
      open_brace = len(contents)
    if close_brace < 0:
      close_brace = len(contents)
    if quote < 0:
      quote = len(contents)

    next = min(open_brace, min(close_brace, min(quote, name)))

    if next == open_brace:
      brace_count += 1
    elif next == close_brace:
      brace_count -= 1
    elif next == quote:
      quote_count = 0 if quote_count else 1
    elif next == name and not quote_count:
      in_whitelist = False
      for w in whitelist:
        if re.match(w, contents[next:]):
          in_whitelist = True
          break
      if not in_whitelist:
        return True
    pos = next + 1
  return False

# Checks for the use of cc:: within the cc namespace, which is usually
# redundant.
def CheckNamespace(input_api, output_api):
  errors = []

  source_file_filter = lambda x: x
  for f in input_api.AffectedSourceFiles(source_file_filter):
    contents = input_api.ReadFile(f, 'rb')
    match = re.search(r'namespace\s*cc\s*{', contents)
    if match:
      whitelist = [
        r"cc::remove_if\b",
        ]
      if FindNamespaceInBlock(match.end(), 'cc', contents, whitelist=whitelist):
        errors.append(f.LocalPath())

  if errors:
    return [output_api.PresubmitError(
      'Do not use cc:: inside of the cc namespace.',
      items=errors)]
  return []

def CheckChangeOnUpload(input_api, output_api):
  results = []
  results += CheckAsserts(input_api, output_api)
  results += CheckStdAbs(input_api, output_api)
  results += CheckPassByValue(input_api, output_api)
  results += CheckChangeLintsClean(input_api, output_api)
  results += CheckTodos(input_api, output_api)
  results += CheckNamespace(input_api, output_api)
  results += input_api.canned_checks.CheckPatchFormatted(input_api, output_api)
  return results

def GetPreferredTrySlaves(project, change):
  return [
    'linux_layout_rel',
    'linux_gpu',
    'mac_gpu',
    'mac_gpu_retina',
    'win_gpu',
  ]


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 19 22:43:47 2018

Movie Recommender System using Matrix Factorization
@author: apoorv
"""

import pandas as pd
import numpy as np
#import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import pickle
import random

# String Literals
userId = 'userId'
movieId = 'movieId'
rating = 'rating'

# Function to transform given data into dictionary
def getTrainAndTestData(ratings):

    ratings_dict_train = dict()
    ratings_dict_test = dict()
    movie_id_dict = dict()
    movie_rating_list = list()

    movie_count = 0
    for movie in sorted(ratings[movieId].unique()):
        movie_id_dict[movie] = movie_count
        movie_count += 1

    userID = 0
    # Iterate over all users and create a dictionary - {userID : {movieID : rating}}
    for index, row in ratings.iterrows():
        if(int(row[userId]) == userID):
            movie_rating_list.append({movie_id_dict[row[movieId]]:row[rating]})
        else:
            data = np.array(movie_rating_list)
            x_train_50, x_test_50 = train_test_split(data,test_size=0.5)
            ratings_dict_train[userID] = x_train_50
            ratings_dict_test[userID] = x_test_50
            # Empty cuurent list - add next user's records
            movie_rating_list = list()
            movie_rating_list.append({movie_id_dict[row[movieId]]: row[rating]})
            userID += 1

    # Handles the data for last user
    data = np.array(movie_rating_list)
    x_train_50, x_test_50 = train_test_split(data,test_size=0.5)
    ratings_dict_train[userID] = x_train_50
    ratings_dict_test[userID] = x_test_50

    # Write data to file to avoid recomputation later
    """
    output = open('train_data_50_new.pkl', 'wb')
    pickle.dump(ratings_dict_train, output, protocol=pickle.HIGHEST_PROTOCOL)
    output = open('test_data_50_new.pkl', 'wb')
    pickle.dump(ratings_dict_test, output, protocol=pickle.HIGHEST_PROTOCOL)
    """

    return ratings_dict_train, ratings_dict_test, movie_count

# Function to compute Matrix Factorization
def calcMatrixFactorization(training_matrix, movie_count, mean_rating, rank, alpha, beta, iters):

    user_count = len(ratings_dict_train)

    # Randomly initialize the thin matrices
    V = np.random.rand(user_count, rank)
    W = np.random.rand(movie_count, rank)

    # Initialize the biases
    b_user = np.random.uniform(-1,1,user_count)
    b_movie = np.random.uniform(-1,1,movie_count)

    #b_user = np.random.uniform(-0.05,0.05,user_count)
    #b_movie = np.random.uniform(-0.05,0.05,movie_count)

    # Global Bias
    b_global = mean_rating

    # The loops below assures iterations to be - "iters * No. of entries"
    for i in range(0,iters):   # Number of epochs
        print(i)
        # Since dictionary is unordered, our data here is effectively shuffled again here
        for user, value in sorted(training_matrix.items(), key = lambda x:random.random()): # Iterate for all users
            for data in value:
                for movie, rate in data.items():    # Iterate for all movies rated by this user

                    # Prediction with current weights
                    prediction = V[user, :].dot(W[movie, :].T) + b_global + b_user[user] + b_movie[movie]

                    # Compute the error for current prediction
                    error = rate - prediction
                    #print(error)
                    # Update bias parameters
                    b_user[user] += alpha * (error - beta * b_user[user])
                    b_movie[movie] += alpha * (error - beta * b_movie[movie])

                    # Update feature matrix
                    V[user, :] += alpha * (error * W[movie, :] - beta * V[user,:])
                    W[movie, :] += alpha * (error * V[user, :] - beta * W[movie,:])
                    #print(userId, movieId, ratings)
            if(user%10000 == 0):
                print(user)

    return V, W, b_user, b_movie

# Function to calculate the root mean square error
def calcRMSE(test_matrix, V, W, test_entry_count, b_user, b_movie, b_global):

    rmse = 0
    for user, value in test_matrix.items(): # Iterate for all users
        for data in value:
            for movie, rate in data.items():    # Iterate for all movies rated by this user

                rmse += pow(rate - (V[user].dot(W[movie].T)) - b_global - b_user[user] - b_movie[movie], 2)

    rmse = rmse/test_entry_count

    return np.sqrt(rmse)

# Function to calculate the Mean Recipocal Rank
def calcMRR(test_matrix, V, W, b_global, b_user, b_movie):

     curr_user = 1
     mrr = []
     list_rating = list()
     list_predicition = list()

     # Iterate over all users and movies
     for user, value in test_matrix.items():
         for data in value:
             for movie, rate in data.items():
                 if user == curr_user and rate >= 3:
                     # Store all movies with rating more than 3 for current user
                     list_rating.append([movie, rate])
                 if user == curr_user:
                     # Store predictions for all movies for current user
                     prediction  = prediction = V[user-1, :].dot(W[movie, :].T) + b_global + b_user[user-1] + b_movie[movie]
                     list_predicition.append([movie, prediction])
                 else:
                    # Sort the movie predictions in decreasing order
                    list_predicition = sorted(list_predicition, key = lambda l:l[1], reverse = True)
                    rank = 0
                    movie_list = list()
                    # Get movie ids
                    for pred in list_predicition:
                        movie_list.append(pred[0])
                    # Get index and rank
                    for rating in range(len(list_rating)):
                        index, = np.where(np.array(movie_list) == np.array(list_rating[rating][0]))
                        rank += 1/(index+1)
                    # For all ranked movies, store the average rating for the user
                    if rank > 0:
                        mrr.append(rank/len(list_rating))
                    else:
                        # Since the movie is not rated
                        mrr.append(0)

                    # Compute for the last user, as curr_user will not handle that case
                    list_rating = list()
                    list_predicition = list()
                    list_rating.append([movie, rate])
                    prediction  = prediction = V[user-1, :].dot(W[movie, :].T) + b_global + b_user[user-1] + b_movie[movie]
                    list_predicition.append([movie, prediction])
                    curr_user = user

     list_predicition = sorted(list_predicition, key = lambda l:l[1], reverse = True)
     rank  = 0
     movie_list = list()
     for pred in list_predicition:
         movie_list.append(pred[0])
     # Get index and rank
     for rating in range(len(list_rating)):
         index, = np.where(np.array(movie_list) == np.array(list_rating[rating][0]))
         rank += 1/(index+1)
     # For all ranked movies, store the average rating for the user
     if rank > 0:
         mrr.append(rank/len(list_rating))
     else:
         # Since the movie is not rated
         mrr.append(0)

     return np.mean(mrr)

if __name__ == "__main__":

    # Open the data file
    ratings = pd.read_csv('ratings.csv')

    entry_count = len(ratings['userId'])

    # Mean rating of all the movies
    mean_rating = sum(ratings['rating'])/len(ratings['rating'])

    # Call to the function to get the data in the form of dictionary
    #ratings_dict_train, ratings_dict_test, movie_count = getTrainAndTestData(ratings)

    # Code to read from pickle file

    print("Loading Training Data..")
    pkl_file = open('train_data_50_new.pkl', 'rb')
    ratings_dict_train = pickle.load(pkl_file)

    print("Loading Test Data..")
    pkl_file = open('test_data_50_new.pkl', 'rb')
    ratings_dict_test = pickle.load(pkl_file)

    pkl_file.close()

    #Comment following when processing data
    movie_count = 26744
    # Beta values for which we will test our function
    beta_values = [0.2, 0.02, 0.002, 0.0002]

    # Get results for multiple values of rank and beta
    for rank in range(4, 16, 4):
        for beta in beta_values:
            # Call to matrix factorization function
            V, W, b_user, b_movie = calcMatrixFactorization(ratings_dict_train, movie_count, mean_rating, rank, 0.01, beta, 4)

            # Since it is a 50-50 split
            test_entry_count = int(entry_count*0.5)

            # Calculate TRAIN RMSE
            rmse = calcRMSE(ratings_dict_train, V, W, test_entry_count, b_user, b_movie, mean_rating)
            rmse_train_result = []
            rmse_train_result.append([rank, beta, rmse])

            print("Train RMSE -")
            print(rank, beta, rmse)

            # Calculate TEST RMSE
            rmse = calcRMSE(ratings_dict_test, V, W, test_entry_count, b_user, b_movie, mean_rating)
            rmse_test_result = []
            rmse_test_result.append([rank, beta, rmse])

            print("Test RMSE -")
            print(rank, beta, rmse)

            # Training MRR
            mrr = calcMRR(ratings_dict_train, V, W, mean_rating, b_user, b_movie)
            print("Train MRR - ")
            print(rank, beta, mrr)
            mrr_train_result = []
            mrr_train_result.append([rank, beta, rmse])

            # Test MRR
            mrr = calcMRR(ratings_dict_test, V, W, mean_rating, b_user, b_movie)
            print("Test MRR - ")
            print(rank, beta, mrr)
            mrr_test_result = []
            mrr_test_result.append([rank, beta, rmse])

    rmse_train_result = np.array(rmse_train_result)
    np.save("rmse_train_result.npz", rmse_train_result)
    rmse_test_result = np.array(rmse_test_result)
    np.save("rmse_test_result.npz", rmse_test_result)
    mrr_train_result = np.array(mrr_train_result)
    np.save("mrr_train_result.npz", mrr_train_result)
    mrr_test_result = np.array(mrr_test_result)
    np.save("mrr_test_result.npz", mrr_test_result)


import math
import tensorflow as tf
from models.ops import get_shape_list, mask_logits, trilinear_attention, create_attention_mask, \
    transpose_for_scores, gumbel_sample

def layer_norm(inputs, reuse=None, name='layer_norm'):
    """Layer normalize the tensor x, averaging over the last dimension."""
    with tf.compat.v1.variable_scope(name, default_name="layer_norm", values=[inputs], reuse=reuse):
        dim = get_shape_list(inputs)[-1]
        scale = tf.compat.v1.get_variable("layer_norm_scale", [dim], initializer=tf.compat.v1.ones_initializer())
        bias = tf.compat.v1.get_variable("layer_norm_bias", [dim], initializer=tf.compat.v1.zeros_initializer())
        mean = tf.reduce_mean(input_tensor=inputs, axis=[-1], keepdims=True)
        variance = tf.reduce_mean(input_tensor=tf.square(inputs - mean), axis=[-1], keepdims=True)
        norm_inputs = (inputs - mean) * tf.math.rsqrt(variance + 1e-6)
        result = norm_inputs * scale + bias
    return result

def conv1d(inputs, dim, kernel_size=1, use_bias=False, activation=None, padding='VALID', reuse=None, name='conv1d'):
    """The conv1d here act as a dense layer in default"""
    with tf.compat.v1.variable_scope(name, reuse=reuse):
        shapes = get_shape_list(inputs)
        kernel = tf.compat.v1.get_variable(name='kernel', shape=[kernel_size, shapes[-1], dim], dtype=tf.float32)
        outputs = tf.nn.conv1d(input=inputs, filters=kernel, stride=1, padding=padding)
        if use_bias:
            bias = tf.compat.v1.get_variable(name='bias', shape=[1, 1, dim], dtype=tf.float32, initializer=tf.compat.v1.zeros_initializer())
            outputs += bias
        return outputs if activation is None else activation(outputs)

def depthwise_separable_conv(inputs, kernel_size, dim, use_bias=True, reuse=None, activation=tf.nn.relu,
                             name='depthwise_separable_conv'):
    with tf.compat.v1.variable_scope(name, reuse=reuse):
        shapes = get_shape_list(inputs)
        depthwise_filter = tf.compat.v1.get_variable(name='depthwise_filter', dtype=tf.float32,
                                           shape=[kernel_size[0], kernel_size[1], shapes[-1], 1])
        pointwise_filter = tf.compat.v1.get_variable(name='pointwise_filter', shape=[1, 1, shapes[-1], dim], dtype=tf.float32)
        outputs = tf.nn.separable_conv2d(input=inputs, depthwise_filter=depthwise_filter, pointwise_filter=pointwise_filter, strides=[1, 1, 1, 1],
                                         padding='SAME')
        if use_bias:
            b = tf.compat.v1.get_variable('bias', outputs.shape[-1], initializer=tf.compat.v1.zeros_initializer())
            outputs += b
        outputs = activation(outputs)
        return outputs

def bilinear(input1, input2, dim, use_bias=True, reuse=None, name='bilinear_dense'):
    with tf.compat.v1.variable_scope(name, reuse=reuse):
        input1 = conv1d(input1, dim=dim, use_bias=False, reuse=reuse, name='dense_1')
        input2 = conv1d(input2, dim=dim, use_bias=False, reuse=reuse, name='dense_2')
        output = input1 + input2
        if use_bias:
            bias = tf.compat.v1.get_variable(name='bias', shape=[dim], dtype=tf.float32, initializer=tf.compat.v1.zeros_initializer())
            output += bias
        return output

def dual_multihead_attention(from_tensor, to_tensor, dim, num_heads, from_mask, to_mask, drop_rate, reuse=None,
                             name='dual_multihead_attention'):
    with tf.compat.v1.variable_scope(name, reuse=reuse):
        if dim % num_heads != 0:
            raise ValueError('The hidden size (%d) is not a multiple of the attention heads (%d)' % (dim, num_heads))
        batch_size, from_seq_len, _ = get_shape_list(from_tensor)
        _, to_seq_len, _ = get_shape_list(to_tensor)
        head_size = dim // num_heads
        # self-attn projection (batch_size, num_heads, from_seq_len, head_size)
        query = transpose_for_scores(conv1d(from_tensor, dim=dim, use_bias=True, reuse=reuse, name='query'),
                                     batch_size, from_seq_len, num_heads, head_size)
        f_key = transpose_for_scores(conv1d(from_tensor, dim=dim, use_bias=True, reuse=reuse, name='f_key'),
                                     batch_size, from_seq_len, num_heads, head_size)
        f_value = transpose_for_scores(conv1d(from_tensor, dim=dim, use_bias=True, reuse=reuse, name='f_value'),
                                       batch_size, from_seq_len, num_heads, head_size)
        # cross-attn projection (batch_size, num_heads, to_seq_len, head_size)
        t_key = transpose_for_scores(conv1d(to_tensor, dim=dim, use_bias=True, reuse=reuse, name='t_key'),
                                     batch_size, to_seq_len, num_heads, head_size)
        t_value = transpose_for_scores(conv1d(to_tensor, dim=dim, use_bias=True, reuse=reuse, name='t_value'),
                                       batch_size, to_seq_len, num_heads, head_size)
        # create attention mask
        s_attn_mask = tf.expand_dims(create_attention_mask(from_mask, from_mask, broadcast_ones=False), axis=1)
        x_attn_mask = tf.expand_dims(create_attention_mask(from_mask, to_mask, broadcast_ones=False), axis=1)
        # compute self-attn score
        s_attn_value = tf.multiply(tf.matmul(query, f_key, transpose_b=True), 1.0 / math.sqrt(float(head_size)))
        s_attn_value += (1.0 - s_attn_mask) * -1e30
        s_attn_score = tf.nn.softmax(s_attn_value, axis=-1)
        s_attn_score = tf.nn.dropout(s_attn_score, rate=drop_rate)
        # compute cross-attn score
        x_attn_value = tf.multiply(tf.matmul(query, t_key, transpose_b=True), 1.0 / math.sqrt(float(head_size)))
        x_attn_value += (1.0 - x_attn_mask) * -1e30
        x_attn_score = tf.nn.softmax(x_attn_value, axis=-1)
        x_attn_score = tf.nn.dropout(x_attn_score, rate=drop_rate)

        # compute self-attn value
        s_value = tf.transpose(a=tf.matmul(s_attn_score, f_value), perm=[0, 2, 1, 3])
        s_value = tf.reshape(s_value, shape=[batch_size, from_seq_len, num_heads * head_size])
        s_value = conv1d(s_value, dim=dim, use_bias=True, activation=None, reuse=reuse, name='s_dense')

        # compute cross-attn value
        x_value = tf.transpose(a=tf.matmul(x_attn_score, t_value), perm=[0, 2, 1, 3])
        x_value = tf.reshape(x_value, shape=[batch_size, from_seq_len, num_heads * head_size])
        x_value = conv1d(x_value, dim=dim, use_bias=True, activation=None, reuse=reuse, name='x_dense')

        # cross gating
        s_score = conv1d(s_value, dim=dim, use_bias=True, activation=tf.sigmoid, reuse=reuse, name='s_gate')
        x_score = conv1d(x_value, dim=dim, use_bias=True, activation=tf.sigmoid, reuse=reuse, name='x_gate')
        outputs = tf.multiply(s_score, x_value) + tf.multiply(x_score, s_value)
        outputs = conv1d(outputs, dim=dim, use_bias=True, reuse=reuse, name='guided_dense')
        scores = bilinear(from_tensor, outputs, dim=dim, use_bias=True, reuse=reuse, name='bilinear_1')
        values = bilinear(from_tensor, outputs, dim=dim, use_bias=True, reuse=reuse, name='bilinear_2')
        outputs = tf.sigmoid(mask_logits(scores, tf.expand_dims(from_mask, axis=2))) * values
        return outputs

def cq_attention(inputs1, inputs2, mask1, mask2, drop_rate=0.0, reuse=None, name='cqa'):
    with tf.compat.v1.variable_scope(name, reuse=reuse):
        # We regard the inputs1 as the context, while inputs2 as the query, the output has the same shape with inputs1
        dim = get_shape_list(inputs1)[-1]
        maxlen1 = tf.reduce_max(input_tensor=tf.reduce_sum(input_tensor=mask1, axis=1))
        maxlen2 = tf.reduce_max(input_tensor=tf.reduce_sum(input_tensor=mask2, axis=1))
        score = trilinear_attention([inputs1, inputs2], maxlen1=maxlen1, maxlen2=maxlen2, drop_rate=drop_rate,
                                    reuse=reuse, name='efficient_trilinear')
        mask_q = tf.expand_dims(mask2, 1)
        score_ = tf.nn.softmax(mask_logits(score, mask=mask_q))
        mask_v = tf.expand_dims(mask1, 2)
        score_t = tf.transpose(a=tf.nn.softmax(mask_logits(score, mask=mask_v), axis=1), perm=[0, 2, 1])
        c2q = tf.matmul(score_, inputs2)
        q2c = tf.matmul(tf.matmul(score_, score_t), inputs1)
        attention_outputs = tf.concat([inputs1, c2q, inputs1 * c2q, inputs1 * q2c], axis=-1)
        outputs = conv1d(attention_outputs, dim=dim, use_bias=False, activation=None, reuse=reuse, name='dense')
        return outputs, score

def weighted_pooling(inputs, mask, reuse=None, name='weighted_pooling'):
    with tf.compat.v1.variable_scope(name, reuse=reuse):
        dim = get_shape_list(inputs)[-1]
        weight = tf.compat.v1.get_variable(name='weight', shape=[dim, 1], dtype=tf.float32)
        x = tf.tensordot(inputs, weight, axes=1)  # shape = (batch_size, seq_length, 1)
        mask = tf.expand_dims(mask, axis=-1)  # shape = (batch_size, seq_length, 1)
        x = mask_logits(x, mask=mask)
        alphas = tf.nn.softmax(x, axis=1)
        output = tf.matmul(tf.transpose(a=inputs, perm=[0, 2, 1]), alphas)
        return tf.squeeze(output, axis=-1)

def cq_concat(inputs, pool_inputs, pool_mask, reuse=None, name='cq_concat'):
    with tf.compat.v1.variable_scope(name, reuse=reuse):
        dim = get_shape_list(inputs)[-1]
        # weighted pooling
        pool_output = weighted_pooling(pool_inputs, mask=pool_mask, reuse=reuse, name='weighted_pooling')
        # concatenation
        pool_output = tf.tile(tf.expand_dims(pool_output, axis=1), multiples=[1, tf.shape(input=inputs)[1], 1])
        outputs = tf.concat([inputs, pool_output], axis=-1)
        outputs = conv1d(outputs, dim=dim, use_bias=True, reuse=False, name='dense')
        return outputs

def matching_loss(inputs, labels, label_size, mask, tau=0.3, gumbel=True, reuse=None, name='matching_loss'):
    with tf.compat.v1.variable_scope(name, reuse=reuse):
        # (batch_size, seq_length, label_size)
        logits = conv1d(inputs, dim=label_size, use_bias=True, reuse=reuse, name='dense')
        # prepare labels
        # labels = tf.one_hot(labels, depth=label_size, axis=-1, dtype=logits.dtype)
        if gumbel:
            # sample gumbel noise
            noise = gumbel_sample(tf.shape(input=logits))
            logits = (logits + noise) / tau
        # compute log_probs and probs
        log_probs = tf.nn.log_softmax(logits, axis=-1)
        probs = tf.nn.softmax(logits)
        # compute loss
        loss_per_sample = -tf.reduce_sum(input_tensor=labels * log_probs, axis=-1)
        mask = tf.cast(mask, dtype=logits.dtype)
        loss = tf.reduce_sum(input_tensor=loss_per_sample * mask) / (tf.reduce_sum(input_tensor=mask) + 1e-12)
        return loss, probs

def localizing_loss(start_logits, end_logits, y1, y2, mask):
    start_logits = mask_logits(start_logits, mask=mask)
    end_logits = mask_logits(end_logits, mask=mask)
    start_losses = tf.nn.softmax_cross_entropy_with_logits(logits=start_logits, labels=y1)
    end_losses = tf.nn.softmax_cross_entropy_with_logits(logits=end_logits, labels=y2)
    loss = tf.reduce_mean(input_tensor=start_losses + end_losses)
    return loss

def ans_predictor(start_logits, end_logits, mask, match_scores):

    start_logits = mask_logits(start_logits, mask=mask)
    end_logits = mask_logits(end_logits, mask=mask)

    # start_logits = start_logits * match_scores[:, :, 1]
    # end_logits = end_logits *  match_scores[:, :, 3]

    # start_prob = tf.nn.softmax(start_logits, axis=1)
    # end_prob = tf.nn.softmax(end_logits, axis=1)
    start_prob = tf.nn.sigmoid(start_logits)
    end_prob = tf.nn.sigmoid(end_logits)

    outer = tf.matmul(tf.expand_dims(start_prob, axis=2), tf.expand_dims(end_prob, axis=1))
    outer = tf.linalg.band_part(outer, num_lower=0, num_upper=-1)
    start_index = tf.argmax(input=tf.reduce_max(input_tensor=outer, axis=2), axis=1)
    end_index = tf.argmax(input=tf.reduce_max(input_tensor=outer, axis=1), axis=1)
    return start_index, end_index

def frameCLoss(video, query, pos_label, v_mask):
    # FrameCL
    # query = tf.sigmoid(query)
    # video = tf.sigmoid(video)
    query = tf.math.l2_normalize(query, axis=1, epsilon=1e-12)
    video = tf.math.l2_normalize(video, axis=1, epsilon=1e-12)

    pos_mask = tf.cast(pos_label, dtype=tf.float32)
    neg_mask = (tf.ones_like(pos_mask) - pos_mask ) *  tf.cast(v_mask, dtype=tf.float32)

    query = tf.reduce_max(input_tensor=query, axis=1)
    query = tf.expand_dims(query, axis=2)

    res = tf.matmul(video, query)
    res = tf.squeeze(res)

    log_2 = math.log(2.)
    E_pos = log_2 - tf.nn.softplus(- (res * pos_mask))
    E_pos = tf.reduce_sum(input_tensor=E_pos * pos_mask, axis=1) / (tf.reduce_sum(input_tensor=pos_mask, axis=1) + 1e-12)

    E_neg = log_2 - tf.nn.softplus(- (res * neg_mask))
    E_neg = tf.reduce_sum(input_tensor=E_neg * neg_mask, axis=1) / (tf.reduce_sum(input_tensor=neg_mask, axis=1) + 1e-12)

    closs = E_neg - E_pos
    closs = tf.reduce_mean(input_tensor=closs)

    return closs


from tsundoku.utils.re import PUNCTUATION_RE
from tsundoku.utils.files import read_list

def remove_stopwords(df, stopwords_file, token_column="token"):
    stopwords = set(read_list(stopwords_file))
    df = df[(~df[token_column].isin(stopwords))]
    return df

def remove_punctuation(df, token_column="token"):
    return df[(~df[token_column].str.contains(pat=PUNCTUATION_RE))]

def filter_vocabulary(
    vocabulary,
    min_freq=50,
    stopwords_file=None,
    remove_punctuation=True,
    token_column="token",
    frequency_column="frequency",
    remove_twitter_urls=True,
):
    if remove_punctuation:
        vocabulary = vocabulary[
            (~vocabulary[token_column].str.contains(pat=PUNCTUATION_RE))
        ]

    if stopwords_file is not None:
        stopwords = set(read_list(stopwords_file))
        vocabulary = vocabulary[(~vocabulary[token_column].isin(stopwords))]

    if remove_twitter_urls:
        vocabulary = vocabulary[
            ~vocabulary[token_column].str.startswith("https://t.co")
        ]

    return (
        vocabulary.groupby(token_column)
        .sum()
        .sort_values(frequency_column, ascending=False)
        .pipe(lambda x: x[x[frequency_column] >= min_freq])
        .assign(token_id=lambda x: range(len(x)))
    )

def build_elem_to_id(dask_df, key, keep="last"):
    return (
        dask_df.drop_duplicates(subset=key, keep=keep)[[key]]
        .compute()
        .assign(row_id=lambda x: range(len(x)))
        .set_index(key)
    )


from functions import *
import argparse

import time
time0 = time.time()

# arguments
parser = argparse.ArgumentParser()
# parser.add_argument('-masses', action='store_true', help="calculate only masses")
# parser.add_argument('-isos', action='store_true', help="calculate only isos")
parser.add_argument("resolution", help="resolution of maps at which to make measurements",
                    choices=['quick', 'highres'])

parser.add_argument("-sats", action='store_true', help="include satellites in addition to centrals")
parser.add_argument("-icl", action='store_true', help="include ICL (fuzz) in addition to centrals")

parser.add_argument("-TNG", action='store_true', help="include TNG only")
parser.add_argument("-TNG300", action='store_true', help="include TNG300 only")
parser.add_argument("-Illustris", action='store_true', help="include Illustris only")
args = parser.parse_args()

# resolution argument
resolution = args.resolution
if resolution == 'quick':
    Illustris_file = Illustris_file_quick
    TNG_file = TNG_file_quick
elif resolution == 'highres':
    Illustris_file = Illustris_file_highres
    TNG_file = TNG_file_highres

# components arguments
components = 'cen'
if args.sats:
    if args.icl:
        components = 'all'
    else:
        components += '+sats'
elif args.icl:
    components += '+icl'

if not args.TNG and not args.Illustris and not args.TNG300:
    args.TNG = True
    args.Illustris = True

###############################################################################
# run on Illustris
if args.Illustris:
    isos_illustris = [get_iso(Illustris_file, 'Illustris', resolution,
                              intMode='mean', components=components, gal_n=i) for i in range(339)]
    masses_illustris = [get_masses(isos_illustris[i], Illustris_file, 'Illustris', resolution, rs=[
                                   300, 500, 800], gal_n=i) for i in range(339)]

    # save as pickles
    outfile_loc = '/Users/fardila/Documents/GitHub/HSC_vs_hydro/Data/Illustris/'
    save_pkl(
        outfile_loc+'Illustris_isos_{0}_{1}.pkl'.format(components, resolution), isos_illustris)
    save_pkl(
        outfile_loc+'Illustris_masses_{0}_{1}.pkl'.format(components, resolution), vstack(masses_illustris))

###############################################################################
# run on TNG
if args.TNG:
    isos_tng = [get_iso(TNG_file, 'TNG', resolution, intMode='mean',
                        components=components, gal_n=i) for i in range(235)]
    masses_tng = [get_masses(isos_tng[i], TNG_file, 'TNG', resolution,
                             rs=[300, 500, 800], gal_n=i) for i in range(235)]

    # save as pickles
    outfile_loc = '/Users/fardila/Documents/GitHub/HSC_vs_hydro/Data/TNG/'
    save_pkl(outfile_loc+'TNG_isos_{0}_{1}.pkl'.format(components, resolution), isos_tng)
    save_pkl(outfile_loc+'TNG_masses_{0}_{1}.pkl'.format(components,
                                                         resolution), vstack(masses_tng))

###############################################################################
# run on TNG
if args.TNG300:
    components = 'cen'
    resolution = 'quick'
    sim_name = 'TNG300'
    TNG_file = TNG300_file_quick
    f = h5py.File(TNG_file, 'r')
    n = len(f['catgrp_Group_M_Crit200'])
    centrals = np.array(f['catgrp_is_primary'])
    f.close()
    # n = 1
    print(len(np.arange(n)[centrals]))
    # isos_tng = [get_iso(TNG_file, sim_name, resolution, intMode='mean',
    #                     components=components, gal_n=i) for i in [390,391]]
    isos_tng = [get_iso(TNG_file, sim_name, resolution, intMode='mean',
                        components=components, gal_n=i) for i in np.arange(n)[centrals]]
    # masses_tng = [get_masses(isos_tng[i], TNG_file, sim_name, resolution,
    #                          rs=[300, 500, 800], gal_n=i) for i in range(len())]

    # save as pickles
    outfile_loc = '/Users/fardila/Documents/GitHub/HSC_vs_hydro/Data/TNG/'
    save_pkl(outfile_loc+'{0}_isos_{1}_{2}.pkl'.format(sim_name, components, resolution), isos_tng)
    # save_pkl(outfile_loc+'TNG_masses_{0}_{1}.pkl'.format(components,
    #                                                      resolution), vstack(masses_tng))

print(time.time()-time0, ' seconds')


import logging
import isceobj

from iscesys.ImageUtil.ImageUtil import ImageUtil as IU
from mroipac.filter.Filter import Filter
from mroipac.icu.Icu import Icu
import os

logger = logging.getLogger('isce.insar.runFilter')

def runFilter(self, filterStrength, igramSpectrum = "full"):
    logger.info("Applying power-spectral filter")

    if igramSpectrum == "full":
        logger.info("Filtering the full-band interferogram")
        ifgDirname = self.insar.ifgDirname

    elif igramSpectrum == "low":
        if not self.doDispersive:
            print('Estimating dispersive phase not requested ... skipping sub-band interferograms')
            return
        logger.info("Filtering the low-band interferogram")
        ifgDirname = os.path.join(self.insar.ifgDirname, self.insar.lowBandSlcDirname)

    elif igramSpectrum == "high":
        if not self.doDispersive:
            print('Estimating dispersive phase not requested ... skipping sub-band interferograms')
            return
        logger.info("Filtering the high-band interferogram")
        ifgDirname = os.path.join(self.insar.ifgDirname, self.insar.highBandSlcDirname)

    topoflatIntFilename = os.path.join(ifgDirname , self.insar.ifgFilename)

    img1 = isceobj.createImage()
    img1.load(topoflatIntFilename + '.xml')
    widthInt = img1.getWidth()

    intImage = isceobj.createIntImage()
    intImage.setFilename(topoflatIntFilename)
    intImage.setWidth(widthInt)
    intImage.setAccessMode('read')
    intImage.createImage()

    # Create the filtered interferogram
    filtIntFilename = os.path.join(ifgDirname , 'filt_' + self.insar.ifgFilename)
    filtImage = isceobj.createIntImage()
    filtImage.setFilename(filtIntFilename)
    filtImage.setWidth(widthInt)
    filtImage.setAccessMode('write')
    filtImage.createImage()

    objFilter = Filter()
    objFilter.wireInputPort(name='interferogram',object=intImage)
    objFilter.wireOutputPort(name='filtered interferogram',object=filtImage)
    if filterStrength is not None:
        self.insar.filterStrength = filterStrength

    objFilter.goldsteinWerner(alpha=self.insar.filterStrength)

    intImage.finalizeImage()
    filtImage.finalizeImage()
    del filtImage

    #Create phase sigma correlation file here
    filtImage = isceobj.createIntImage()
    filtImage.setFilename(filtIntFilename)
    filtImage.setWidth(widthInt)
    filtImage.setAccessMode('read')
    filtImage.createImage()

    phsigImage = isceobj.createImage()
    phsigImage.dataType='FLOAT'
    phsigImage.bands = 1
    phsigImage.setWidth(widthInt)
    phsigImage.setFilename(os.path.join(ifgDirname , self.insar.coherenceFilename))
    phsigImage.setAccessMode('write')
    phsigImage.setImageType('cor')#the type in this case is not for mdx.py displaying but for geocoding method
    phsigImage.createImage()

    resampAmpImage = os.path.join(ifgDirname , self.insar.ifgFilename)

    if '.flat' in resampAmpImage:
        resampAmpImage = resampAmpImage.replace('.flat', '.amp')
    elif '.int' in resampAmpImage:
        resampAmpImage = resampAmpImage.replace('.int', '.amp')
    else:
        resampAmpImage += '.amp'

    ampImage = isceobj.createAmpImage()
    ampImage.setWidth(widthInt)
    ampImage.setFilename(resampAmpImage)
    #IU.copyAttributes(self.insar.resampAmpImage, ampImage)
    #IU.copyAttributes(resampAmpImage, ampImage)
    ampImage.setAccessMode('read')
    ampImage.createImage()

    icuObj = Icu(name='stripmapapp_filter_icu')
    icuObj.configure()
    icuObj.unwrappingFlag = False

    icuObj.icu(intImage = filtImage, ampImage=ampImage, phsigImage=phsigImage)

    filtImage.finalizeImage()
    phsigImage.finalizeImage()
    phsigImage.renderHdr()
    ampImage.finalizeImage()

    # Set the filtered image to be the one geocoded
    # self.insar.topophaseFlatFilename = filtIntFilename


import tkinter
from tkinter import *
from tkinter import messagebox

window = tkinter.Tk()
window.title("Choose your beverages")
window.geometry("300x200")

def submit_action():
    message_text = ""
    if c_var.get():
        message_text += "You have chosen coffee\n"
    if t_var.get():
        message_text += "You have chosen tea\n"
    if w_var.get():
        message_text += "You have chosen water\n"
    messagebox.showinfo("Test","This is a test message")

submit_button = Button(window, text="submit",
                       command=submit_action)

c_var = BooleanVar()
t_var = BooleanVar()
w_var = BooleanVar()

coffee_cb = Checkbutton (window, text= "Coffee",
                         variable =c_var, onvalue=True,
                         offvalue=False,
                         width=20, height=2)

tea_cb = Checkbutton (window, text="Tea",
                      variable=t_var, onvalue=True,
                      offvalue=False,
                      width=20, height=2)

water_cb= Checkbutton (window, text="Water",
                       variable=w_var, onvalue=True,
                       offvalue=False,
                       width=20, height=2)
water_cb.pack()
tea_cb.pack()
coffee_cb.pack()
submit_button.pack()
window.mainloop()


import cv2 as cv
import numpy as np

# Lê a imagem img_cells.jpg
img = cv.imread("img_cells.jpg")
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

# Binariza a imagem diretamente
ret,thresh1 = cv.threshold(img_gray, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)

# Mostra a imagem original e sua versão binarizada
cv.imshow('original', img)
cv.imshow('binarizada', thresh1)
cv.waitKey(0)
cv.destroyAllWindows()

# Preenche os buracos
thresh1_neg = cv.bitwise_not(thresh1)
contour = cv.findContours(thresh1_neg, cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE)[0]
for i in contour:
    cv.drawContours(thresh1_neg, [i], 0, 255, -1)

# Remoção de ruídos na imagem
kernel = np.ones((4,4), np.uint8)
opening = cv.morphologyEx(thresh1_neg, cv.MORPH_OPEN, kernel)

# Mostra a imagem com os buracos preenchidos e tratamento de ruídos
cv.imshow("preenchimento", thresh1_neg)
cv.imshow("tratamento de ruidos", opening)
cv.waitKey(0)
cv.destroyAllWindows()

# Encontrando a área desconhecida
backgound = cv.dilate(opening, kernel,iterations=3)
dist_transform = cv.distanceTransform(opening, cv.DIST_L2, 5)
foreground = cv.threshold(dist_transform, 0.4*dist_transform.max(), 255, 0)[1]
foreground = np.uint8(foreground)
unknown = cv.subtract(backgound, foreground)

# Mostra a imagem da área desconhecida
cv.imshow("area desconhecida", unknown)
cv.waitKey(0)
cv.destroyAllWindows()

# Rotulagem de marcador
markers = cv.connectedComponents(foreground)[1]

# Fazemos isso para que o fundo seja 1
markers = markers+1

# Marcando a região desconhecida com zero
markers[unknown==255] = 0

#Segmentação Watersheed
markers = cv.watershed(img, markers)
img[markers == -1] = [255,0,0]

# Mostra a o resultado final da segmentação de Watersheed
cv.imshow("img", img)
cv.waitKey(0)
cv.destroyAllWindows()

TRABALHO 2/Códigos - Projeto 2/questao3.py