text:
import os
import shutil
import time
from flask_apscheduler import APScheduler
import numpy as np
import pandas as pd
from keras.preprocessing.image import ImageDataGenerator
from keras.models import load_model
from PIL import Image
import gdown
from flask import Flask, render_template, request, redirect, flash, send_from_directory
from werkzeug.utils import secure_filename

disease_map = {
    0: 'Apple: Apple Scab',
    1: 'Apple: Black Rot',
    2: 'Apple: Cedar Rust',
    3: 'Apple: Healthy',
    4: 'Blueberry: Healthy',
    5: 'Cherry: Powdery Mildew',
    6: 'Cherry: Healthy',
    7: 'Corn (Maize): Grey Leaf Spot',
    8: 'Corn (Maize): Common Rust of Maize',
    9: 'Corn (Maize): Northern Leaf Blight',
    10: 'Corn (Maize): Healthy',
    11: 'Grape: Black Rot',
    12: 'Grape: Black Measles (Esca)',
    13: 'Grape: Leaf Blight (Isariopsis Leaf Spot)',
    14: 'Grape: Healthy',
    15: 'Orange: Huanglongbing (Citrus Greening)',
    16: 'Peach: Bacterial spot',
    17: 'Peach: Healthy',
    18: 'Bell Pepper: Bacterial Spot',
    19: 'Bell Pepper: Healthy',
    20: 'Potato: Early Blight',
    21: 'Potato: Late Blight',
    22: 'Potato: Healthy',
    23: 'Raspberry: Healthy',
    24: 'Rice: Brown Spot',
    25: 'Rice: Hispa',
    26: 'Rice: Leaf Blast',
    27: 'Rice: Healthy',
    28: 'Soybean: Healthy',
    29: 'Squash: Powdery Mildew',
    30: 'Strawberry: Leaf Scorch',
    31: 'Strawberry: Healthy',
    32: 'Tomato: Bacterial Spot',
    33: 'Tomato: Early Blight',
    34: 'Tomato: Late Blight',
    35: 'Tomato: Leaf Mold',
    36: 'Tomato: Septoria Leaf Spot',
    37: 'Tomato: Spider Mites (Two-spotted Spider Mite)',
    38: 'Tomato: Target Spot',
    39: 'Tomato: Yellow Leaf Curl Virus',
    40: 'Tomato: Mosaic Virus',
    41: 'Tomato: Healthy'
    }

details_map = {
    'Apple: Apple Scab': [
        'A serious disease of apples and ornamental crabapples, apple scab (Venturia inaequalis) attacks both leaves and fruit. The fungal disease forms pale yellow or olive-green spots on the upper surface of leaves. Dark, velvety spots may appear on the lower surface. Severely infected leaves become twisted and puckered and may drop early in the summer.',
        'Symptoms on fruit are similar to those found on leaves. Scabby spots are sunken and tan and may have velvety spores in the center. As these spots mature, they become larger and turn brown and corky. Infected fruit becomes distorted and may crack allowing entry of secondary organisms. Severely affected fruit may drop, especially when young.',
        'https://www.planetnatural.com/pest-problem-solver/plant-disease/apple-scab'],
    'Apple: Black Rot': [
        'Black rot is occasionally a problem on Minnesota apple trees. This fungal disease causes leaf spot, fruit rot and cankers on branches. Trees are more likely to be infected if they are: Not fully hardy in Minnesota, Infected with fire blight or Stressed by environmental factors like drought.',
        'Large brown rotten areas can form anywhere on the fruit but are most common on the blossom end. Brown to black concentric rings can often be seen on larger infections. The flesh of the apple is brown but remains firm. Infected leaves develop "frog-eye leaf spot". These are circular spots with purplish or reddish edges and light tan interiors.',
        'https://extension.umn.edu/plant-diseases/black-rot-apple'],
    'Apple: Cedar Rust': [
        'Cedar apple rust (Gymnosporangium juniperi-virginianae) is a fungal disease that requires juniper plants to complete its complicated two year life-cycle. Spores overwinter as a reddish-brown gall on young twigs of various juniper species. In early spring, during wet weather, these galls swell and bright orange masses of spores are blown by the wind where they infect susceptible apple and crab-apple trees. The spores that develop on these trees will only infect junipers the following year. From year to year, the disease must pass from junipers to apples to junipers again; it cannot spread between apple trees.',
        'On apple and crab-apple trees, look for pale yellow pinhead sized spots on the upper surface of the leaves shortly after bloom. These gradually enlarge to bright orange-yellow spots which make the disease easy to identify. Orange spots may develop on the fruit as well. Heavily infected leaves may drop prematurely.',
        'https://www.planetnatural.com/pest-problem-solver/plant-disease/cedar-apple-rust'],
    'Apple: Healthy': [
        'Your crops are healthy. You took good care of it.',
        'Healthy Crops',
        'Just take care of it as you usually do.'],
    'Blueberry: Healthy': [
        'Your crops are healthy. You took good care of it.',
        'Healthy Crops',
        'Just take care of it as you usually do.'],
    'Cherry: Powdery Mildew': [
        'Powdery mildew of sweet and sour cherry is caused by Podosphaera clandestina, an obligate biotrophic fungus. Mid- and late-season sweet cherry (Prunus avium) cultivars are commonly affected, rendering them unmarketable due to the covering of white fungal growth on the cherry surface. Season long disease control of both leaves and fruit is critical to minimize overall disease pressure in the orchard and consequently to protect developing fruit from accumulating spores on their surfaces.',
        'Initial symptoms, often occurring 7 to 10 days after the onset of the first irrigation, are light roughly-circular, powdery looking patches on young, susceptible leaves (newly unfolded, and light green expanding leaves). Older leaves develop an age-related (ontogenic) resistance to powdery mildew and are naturally more resistant to infection than younger leaves. Look for early leaf infections on root suckers, the interior of the canopy or the crotch of the tree where humidity is high.',
        'http://treefruit.wsu.edu/crop-protection/disease-management/cherry-powdery-mildew'],
    'Cherry: Healthy': [
        'Your crops are healthy. You took good care of it.',
        'Healthy Crops',
        'Just take care of it as you usually do.'],
    'Corn (Maize): Grey Leaf Spot': [
        'Gray leaf spot (GLS) is a common fungal disease in the United States caused by the pathogen Cercospora zeae-maydis in corn. Disease development is favored by warm temperatures, 80°F or 27 °C; and high humidity, relative humidity of 90% or higher for 12 hours or more. Cercospora zeae-maydis overwinters in corn residue, allowing inoculum to build up from year to year in fields. Cropping systems with reduced- or no-till and/or continuous corn are at higher risk for gray leaf spot outbreaks.',
        'Gray leaf spot lesions begin as small necrotic pinpoints with chlorotic halos, these are more visible when leaves are backlit. Coloration of initial lesions can range from tan to brown before sporulation begins. Because early lesions are ambiguous, they are easily confused with other foliar diseases such as anthracnose leaf blight, eyespot, or common rust. As infection progresses, lesions begin to take on a more distinct shape. Lesion expansion is limited by parallel leaf veins, resulting in the blocky shaped “spots”. As sporulation commences, the lesions take on a more gray coloration.',
        'https://www.pioneer.com/us/agronomy/gray_leaf_spot_cropfocus.html'],
    'Corn (Maize): Common Rust of Maize': [
        'Common rust is caused by the fungus Puccinia sorghi. Late occurring infections have limited impact on yield. The fungus overwinters on plants in southern states and airborne spores are wind-blown to northern states during the growing season. Disease development is favored by cool, moist weather (60 – 70◦ F).',
        'Symptoms of common rust often appear after silking. Small, round to elongate brown pustules form on both leaf surfaces and other above ground parts of the plant. As the pustules mature they become brown to black. If disease is severe, the leaves may yellow and die early.',
        'https://fieldcrops.cals.cornell.edu/corn/diseases-corn/common-rust'],
    'Corn (Maize): Northern Leaf Blight': [
        'Northern corn leaf blight caused by the fungus Exerohilum turcicum is a common leaf blight. If lesions begin early (before silking), crop loss can result. Late infections may have less of an impact on yield. Northern corn leaf blight is favored by wet humid cool weather typically found later in the growing season. Spores of the fungus that causes this disease can be transported by wind long distances from infected fields. Spread within and between fields locally also relies on wind blown spores.',
        'The tan lesions of northern corn leaf blight are slender and oblong tapering at the ends ranging in size between 1 to 6 inches. Lesions run parallel to the leaf margins beginning on the lower leaves and moving up the plant. They may coalesce and cover the enter leaf. Spores are produced on the underside of the leaf below the lesions giving the appearance of a dusty green fuzz.',
        'https://fieldcrops.cals.cornell.edu/corn/diseases-corn/northern-corn-leaf-blight'],
    'Corn (Maize): Healthy': [
        'Your crops are healthy. You took good care of it.',
        'Healthy Crops',
        'Just take care of it as you usually do.'],
    'Grape: Black Rot': [
        'Black rot is one of the most damaging diseases of grapes. The disease is caused by the fungus Guignardia bidwellii. The fungus can infect the leaves, shoots, berries, tendrils, rachises and cluster stems (peduncles) of grapes. If the disease is not managed early in the season, the impact on grape clusters can be devastating, resulting in complete crop losses.',
        'Disease development is favored by warm and humid weather. Symptoms of black rot first appear as small yellow spots on leaves. Enlarged spots (lesions) have a dark brownish-red border with tan to dark brown centers. As the infection develops, tiny black dots appear in the lesion, usually in a ring pattern near the border of the lesion. These dots are fungal structures (pycnidia), which contain thousands of spores (conidia) that can infect new tissue. New infections can occur in less than 10 hours at temperatures between 60 to 85 degrees Fahrenheit.',
        'https://ohioline.osu.edu/factsheet/plpath-fru-24'],
    'Grape: Black Measles (Esca)': [
        'Grapevine measles, also called esca, black measles or Spanish measles, has long plagued grape growers with its cryptic expression of symptoms and, for a long time, a lack of identifiable causal organism(s). The name "measles" refers to the superficial spots found on the fruit. During the season, the spots may coalesce over the skin surface, making berries black in appearance. Spotting can develop anytime between fruit set and a few days prior to harvest.',
        'Leaf symptoms are characterized by a "tiger stripe" pattern when infections are severe from year to year. Mild infections can produce leaf symptoms that can be confused with other diseases or nutritional deficiencies. White cultivars will display areas of chlorosis followed by necrosis, while red cultivars are characterized by red areas followed by necrosis. Early spring symptoms include shoot tip dieback, leaf discoloration and complete defoliation in severe cases.',
        'https://grapes.extension.org/grapevine-measles'],
    'Grape: Leaf Blight (Isariopsis Leaf Spot)': [
        'Common in tropical and subtropical grapes. The disease appear late in the season. Cynthiana and Cabernet Sauvignon are susceptible to this pathogen.',
        'On leaf surface we will see lesions which are irregularly shaped (2 to 25 mm in diameter). Initially lesions are dull red to brown in color turn black later. If disease is severe this lesions may coalesce. On berries we can see symptom similar to black rot but the entire clusters will collapse.',
        'https://plantvillage.psu.edu/topics/grape/infos'],
    'Grape: Healthy': [
        'Your crops are healthy. You took good care of it.',
        'Healthy Crops',
        'Just take care of it as you usually do.'],
    'Orange: Huanglongbing (Citrus Greening)': [
        'Huanglongbing (HLB) or citrus greening is the most severe citrus disease, currently devastating the citrus industry worldwide. The presumed causal bacterial agent Candidatus Liberibacter spp. affects tree health as well as fruit development, ripening and quality of citrus fruits and juice. Fruit from infected orange trees can be either symptomatic or asymptomatic. Symptomatic oranges are small, asymmetrical and greener than healthy fruit. Furthermore, symptomatic oranges show higher titratable acidity and lower soluble solids, solids/acids ratio, total sugars, and malic acid levels.',
        'In the early stages of the disease, it is difficult to make a clear diagnosis. McCollum and Baldwin (2017) noted that HLB symptoms are more apparent during cooler seasons, more so than in warmer months. It is uncertain how long a tree can be infected before showing the symptoms of the disease but, when it eventually becomes symptomatic, symptoms appear on different parts of the tree. Infected trees generally develop some canopy thinning, with twig dieback and discolored leaves, which appear in contrast to the other healthy or symptomless parts of the tree.',
        'https://www.frontiersin.org/articles/10.3389/fpls.2018.01976/full'],
    'Peach: Bacterial spot': [
        'Bacterial spot affects peaches, nectarines, apricots, plums, prunes and cherries. The disease is widespread throughout all fruit growing states east of the Rocky Mountains. Bacterial spot can affect leaves, twigs, and fruit. Severe infection results in reduced fruit quality and yield. Fruit infection is most serious on late-maturing varieties. If proper environmental conditions occur, up to 50 percent or more of the fruit of susceptible varieties may have to be discarded.',
        'Small (1/25 to 1/5 inch) spots form in the leaves. Spots are irregular to angular and have a deep purple to rusty-brown or black color. In time, the centers dry and tear away leaving ragged "shot-holes". When several spots merge, the leaf may appear scorched, blighted or ragged. Badly infected leaves may turn yellow and drop early. Early defoliation is most common on trees deficient in nitrogen or where the disease is further complicated by pesticide injury.',
        'https://ohioline.osu.edu/factsheet/plpath-fru-38'],
    'Peach: Healthy': [
        'Your crops are healthy. You took good care of it.',
        'Healthy Crops',
        'Just take care of it as you usually do.'],
    'Bell Pepper: Bacterial Spot': [
        'Bacterial leaf spot, caused by Xanthomonas campestris pv. vesicatoria, is the most common and destructive disease for peppers in the eastern United States. It is a  gram-negative, rod-shaped bacterium that can survive in seeds and plant debris from one season to another. Different strains or races of the bacterium are cultivar-specific, causing disease symptoms in certain varieties due to stringent host specificity. Bacterial leaf spot can devastate a pepper crop by early defoliation of infected leaves and disfiguring fruit.',
        'Disease symptoms can appear throughout the above-ground portion of the plant, which may include leaf spot, fruit spot and stem canker. However, early symptoms show up as water-soaked lesions on leaves that can quickly change from green to dark brown and enlarge into spots that are up to 1/4 inch in diameter with slightly raised margins. Over time, these spots can dry up in less humid weather, which allows the damaged tissues to fall off, resulting in a tattered appearance on the affected leaves.',
        'https://extension.wvu.edu/lawn-gardening-pests/plant-disease/fruit-vegetable-diseases/bacterial-leaf-spot-of-pepper'],
    'Bell Pepper: Healthy': [
        'Your crops are healthy. You took good care of it.',
        'Healthy Crops',
        'Just take care of it as you usually do.'],
    'Potato: Early Blight': [
        'Common on tomato and potato plants, early blight is caused by the fungus Alternaria solani. Symptoms first appear on the lower, older leaves as small brown spots with concentric rings that form a "bull’s eye" pattern. As the disease matures, it spreads outward on the leaf surface causing it to turn yellow, wither and die. Eventually the stem, fruit and upper portion of the plant will become infected. Crops can be severely damaged.',
        'Early blight overwinters on infected plant tissue and is spread by splashing rain, irrigation, insects and garden tools. The disease is also carried on tomato seeds and in potato tubers. In spite of its name, early blight can occur any time throughout the growing season. High temperatures (80-85˚F.) and wet, humid conditions promote its rapid spread. In many cases, poorly nourished or stressed plants are attacked.',
        'https://www.planetnatural.com/pest-problem-solver/plant-disease/early-blight'],
    'Potato: Late Blight': [
        'Late blight (Phytophthora infestans) fungus is in the same genus as the fungus causing pink rot (P. erythroseptica). Late blight was responsible for the Irish potato famine in the mid-nineteenth century (Daly, 1996). In the late twentieth century, there have been major re-occurrences and concern around the world over this pathogen and its disease due to recent mutations (Fry and Goodwin, 1997). These mutations, most notably strain US-8, have made the pathogen resistant to control by metalaxyl, the stand-by fungicide for many years.',
        'Late blight will first appear as water-soaked spots, usually at the tips or edges of lower leaves where water or dew tends to collect. Under moist, cool conditions, water-soaked spots rapidly enlarge and a broad yellow halo may be seen surrounding the lesion (Mohan et al., 1996). On the leaf underside, a spore-producing zone of white moldy growth approximately 0.1 - 0.2 inches wide may appear at the border of the lesion. Under continuously wet conditions, the disease progresses rapidly and warm, dry weather will slow or stop disease development.',
        'https://cropwatch.unl.edu/potato/late_blights'],
    'Potato: Healthy': [
        'Your crops are healthy. You took good care of it.',
        'Healthy Crops',
        'Just take care of it as you usually do.'],
    'Raspberry: Healthy': [
        'Your crops are healthy. You took good care of it.',
        'Healthy Crops',
        'Just take care of it as you usually do.'],
    'Rice: Brown Spot': [
        'Brown Spot is called as sesame leaf spot or Helminthosporiose or fungal blight. The fungus attacks the crop from seedling in nursery to milk stage in main field.',
        'The disease appears first as minute brown dots, later becoming cylindrical or oval to circular (resemble sesame seed). Spots measures 0.5 to 2.0mm in breadth - coalesce to form large patches. Then several spots coalesce and the leaf dries up. Infection also occurs on panicle, neck with brown colour appearance. Seeds also infected (black or brown spots on glumes spots are covered by olivaceous velvety growth)',
        'http://www.agritech.tnau.ac.in/expert_system/paddy/cpdisbrownspot.html'],
    'Rice: Hispa': [
        'The mining of the grubs will be clearly seen on the leaves. Scraping of the upper surface of the leaf blade leaving only the lower epidermis as white streaks parallel to the midrib. Tunneling of larvae through leaf tissue causes irregular translucent white patches that are parallel to the leaf veins. Damaged leaves wither off. Rice field appears burnt when severely infested.',
        'The grub mines into the leaf blade and feed on the green tissue between the veins. Adults also feed in the green tissue; they scrape green matter of the tender leaves. Generally the plants are affected in the young stage.',
        'http://www.agritech.tnau.ac.in/expert_system/paddy/cppests_ricehispa.html'],
    'Rice: Leaf Blast': [
        'Blast, also called rotten neck, is one of the most destructive diseases of Missouri rice. Losses due to this disease have been on the increase since 2000. Blast does not develop every year but is very destructive when it occurs. Rice blast can be controlled by a combination of preventive measures and foliar fungicides applied when rice is in the late boot stage and again when it is 80 to 90 percent headed.',
        'Blast symptoms can occur on leaves, leaf collars, nodes and panicles. Leaf spots are typically elliptical (football shaped), with gray-white centers and brown to red-brown margins. Fully developed leaf lesions are approximately 0.4 to 0.7 inch long and 0.1 to 0.2 inch wide. Both the shape and the color vary depending on the environment, age of the lesion and rice variety. Lesions on leaf sheaths, which rarely develop, resemble those on leaves.',
        'https://extension.missouri.edu/publications/mp645'],
    'Rice: Healthy': [
        'Your crops are healthy. You took good care of it.',
        'Healthy Crops',
        'Just take care of it as you usually do.'],
    'Soybean: Healthy': [
        'Your crops are healthy. You took good care of it.',
        'Healthy Crops',
        'Just take care of it as you usually do.'],
    'Squash: Powdery Mildew': [
        'Powdery mildew, mainly caused by the fungus Podosphaera xanthii, infects all cucurbits, including muskmelons, squash, cucumbers, gourds, watermelons and pumpkins. In severe cases, powdery mildew can cause premature death of leaves, and reduce yield and fruit quality.',
        'The first sign of powdery mildew is pale yellow leaf spots. White powdery spots can form on both upper and lower leaf surfaces, and quickly expand into large blotches. The large blotches can cover entire leaf, petiole and stem surfaces. When powdery mildew infects the majority of the foliage, the plant weakens and the fruit ripens prematurely.',
        'https://extension.umn.edu/diseases/powdery-mildew-cucurbits'],
    'Strawberry: Leaf Scorch': [
        'In addition to leaves, leaf scorch (Diplocarpon earlianum) can infect petioles, runners, fruit stalks and berry caps. If unchecked, plants can be significantly weakened reducing the growth of all plant parts. Severely infected plants are weakened and can die from other stresses such as drought or extreme temperatures.',
        'Dark purple, angular to round spots appear on the upper surface of the leaf. As the disease progresses the tissues around these spots turn reddish or purple. In severe cases, the infected area dries to a tan color and the leaf curls upward looking scorched. Lesions remain reddish purple and do not turn tan or gray in the center.',
        'https://extension.umn.edu/fruit/growing-strawberries-home-garden#gray-mold%2C-leaf-blight%2C-leaf-scorch-and-leaf-spot--1008160'],
    'Strawberry: Healthy': [
        'Your crops are healthy. You took good care of it.',
        'Healthy Crops',
        'Just take care of it as you usually do.'],
    'Tomato: Bacterial Spot': [
        'Bacterial spot can be a devastating disease when the weather is warm and humid. The disease can affect all above-ground parts of tomato and pepper plants: stems, petioles, leaves, and fruits. Fruit spots commonly result in unmarketable fruit, not only for fresh market but also for processing because the spots make the fruit difficult to peel.',
        'Tomato leaves have small (<1/8 inch), brown, circular spots surrounded by a yellow halo. The center of the leaf spots often falls out resulting in small holes. Small, brown, circular spots may also occur on stems and the fruit calyx. Fruit spots are ¼ inch, slightly raised, brown and scabby. Tomato fruit often have a waxy white halo surrounding the fruit spot.',
        'https://extension.umn.edu/diseases/bacterial-spot-tomato-and-pepper'],
    'Tomato: Early Blight': [
        'Early blight is one of the most common tomato diseases, occurring nearly every season wherever tomatoes are grown. It affects leaves, fruits and stems and can be severely yield limiting when susceptible cultivars are used and weather is favorable. Severe defoliation can occur and result in sunscald on the fruit. Early blight is common in both field and high tunnel tomato production in Minnesota.',
        'Initially, small dark spots form on older foliage near the ground. Leaf spots are round, brown and can grow up to half inch in diameter. Larger spots have target-like concentric rings. The tissue around spots often turns yellow. Severely infected leaves turn brown and fall off, or dead, dried leaves may cling to the stem.',
        'https://extension.umn.edu/diseases/early-blight-tomato'],
    'Tomato: Late Blight': [
        'Late blight is a potentially devastating disease of tomato and potato, infecting leaves, stems and fruits of tomato plants. The disease spreads quickly in fields and can result in total crop failure if untreated. Late blight of potato was responsible for the Irish potato famine of the late 1840s.',
        'Leaves have large, dark brown blotches with a green gray edge; not confined by major leaf veins. Infections progress through leaflets and petioles, resulting in large sections of dry brown foliage. Stem infections are firm and dark brown with a rounded edge.',
        'https://extension.umn.edu/diseases/late-blight'],
    'Tomato: Leaf Mold': [
        'Leaf mold is not normally a problem in field-grown tomatoes in northern climates. It can cause losses in tomatoes grown in greenhouses or high tunnels due to the higher humidity found in these environments. Foliage is often the only part of the plant infected and will cause infected leaves to wither and die, indirectly affecting yield. In severe cases, blossoms and fruit can also be infected, directly reducing yield.',
        'The oldest leaves are infected first. Pale greenish-yellow spots, usually less than 1/4 inch, with no definite margins, form on upper sides of leaves. Olive-green to brown velvety mold forms on the lower leaf surface below leaf spots. Leaf spots grow together and turn brown. Leaves wither and die but often remain attached to the plant.',
        'https://extension.umn.edu/diseases/leaf-mold-tomato'],
    'Tomato: Septoria Leaf Spot': [
        'Septoria leaf spot is a very common disease of tomatoes. It is caused by a fungus (Septoria lycopersici) and can affect tomatoes and other plants in the Solanaceae family, especially potatoes and eggplant, just about anywhere in the world. Although Septoria leaf spot is not necessarily fatal for your tomato plants, it spreads rapidly and can quickly defoliate and weaken the plants, rendering them unable to bear fruit to maturity.',
        'Septoria leaf spots start off somewhat circular and first appear on the undersides of older leaves, at the bottom of the plant. They are small, 1/16 to 1/8 inches (1.6 to 3.2 millimeters) in diameter, with a dark brown margin and lighter gray or tan centers. A yellow halo may surround the spot.',
        'https://www.thespruce.com/identifying-and-controlling-septoria-leaf-spot-of-tomato-1402974'],
    'Tomato: Spider Mites (Two-spotted Spider Mite)': [
        'Many species of the spider mite (family: Tetranychidae), so common in North America, attack both indoor and outdoor plants. They can be especially destructive in greenhouses. Spider mites are not true insects, but are classed as a type of arachnid, relatives of spiders, ticks and scorpions.',
        'Spider mites, almost too small to be seen, pass into our gardens without notice. No matter how few, each survives by sucking material from plant cells. Large infestations cause visible damage. Leaves first show patterns of tiny spots or stipplings. They may change color, curl and fall off. The mites activity is visible in the tight webs that are formed under leaves and along stems.',
        'https://www.planetnatural.com/pest-problem-solver/houseplant-pests/spider-mite-control'],
    'Tomato: Target Spot': [
        'Also known as early blight, target spot of tomato is a fungal disease that attacks a diverse assortment of plants, including papaya, peppers, snap beans, potatoes, cantaloupe, and squash as well as passion flower and certain ornamentals. Target spot on tomato fruit is difficult to control because the spores, which survive on plant refuse in the soil, are carried over from season to season.',
        'Target spot on tomato fruit is difficult to recognize in the early stages, as the disease resembles several other fungal diseases of tomatoes. However, as diseased tomatoes ripen and turn from green to red, the fruit displays circular spots with concentric, target-like rings and a velvety black, fungal lesions in the center. The "targets" become pitted and larger as the tomato matures.',
        'https://www.gardeningknowhow.com/edible/vegetables/tomato/target-spot-on-tomatoes.htm'],
    'Tomato: Yellow Leaf Curl Virus': [
        'Tomato yellow leaf curl virus is undoubtedly one of the most damaging pathogens of tomato, and it limits production of tomato in many tropical and subtropical areas of the world. It is also a problem in many countries that have a Mediterranean climate such as California. Thus, the spread of the virus throughout California must be considered as a serious potential threat to the tomato industry.',
        'Infected tomato plants initially show stunted and erect or upright plant growth; plants infected at an early stage of growth will show severe stunting. However, the most diagnostic symptoms are those in leaves.',
        'https://www2.ipm.ucanr.edu/agriculture/tomato/tomato-yellow-leaf-curl'],
    'Tomato: Mosaic Virus': [
        'Tomato mosaic virus (ToMV) and  Tobacco mosaic virus (TMV) are hard to distinguish. Tomato mosaic virus (ToMV) can cause yellowing and stunting of tomato plants resulting in loss of stand and reduced yield. ToMV may cause uneven ripening of fruit, further reducing yield.',
        'Mottled light and dark green on leaves. If plants are infected early, they may appear yellow and stunted overall. Leaves may be curled, malformed, or reduced in size. Spots of dead leaf tissue may become apparent with certain cultivars at warm temperatures. Fruits may ripen unevenly. Reduced fruit number and size.',
        'https://extension.umn.edu/diseases/tomato-mosaic-virus-and-tobacco-mosaic-virus'],
    'Tomato: Healthy': [
        'Your crops are healthy. You took good care of it.',
        'Healthy Crops',
        'Just take care of it as you usually do.']
    }

if not os.path.exists('AgentCropKeras_v1.h5'):
    url='https://drive.google.com/uc?id=1JNggWQ9OJFYnQpbsFXMrVu-E-sR3VnCu'
    output = 'AgentCropKeras_v1.h5'
    gdown.download(url, output, quiet=False)

model = load_model('AgentCropKeras_v1.h5')
if not os.path.exists('./static/test'):
        os.makedirs('./static/test')

def predict(test_dir):
    test_img = [f for f in os.listdir(os.path.join(test_dir)) if not f.startswith(".")]
    test_df = pd.DataFrame({'Image': test_img})

    test_gen = ImageDataGenerator(rescale=1./255)

    test_generator = test_gen.flow_from_dataframe(
        test_df,
        test_dir,
        x_col = 'Image',
        y_col = None,
        class_mode = None,
        target_size = (256, 256),
        batch_size = 20,
        shuffle = False
    )
    predict = model.predict(test_generator, steps = np.ceil(test_generator.samples/20))
    test_df['Label'] = np.argmax(predict, axis = -1) # axis = -1 --> To compute the max element index within list of lists
    test_df['Label'] = test_df['Label'].replace(disease_map)

    prediction_dict = {}
    for value in test_df.to_dict('index').values():
        image_name = value['Image']
        image_prediction = value['Label']
        prediction_dict[image_name] = {}
        prediction_dict[image_name]['prediction'] = image_prediction
        prediction_dict[image_name]['description'] = details_map[image_prediction][0]
        prediction_dict[image_name]['symptoms'] = details_map[image_prediction][1]
        prediction_dict[image_name]['source'] = details_map[image_prediction][2]
    return prediction_dict

# Create an app
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 50 * 1024 * 1024 # maximum upload size is 50 MB
app.secret_key = "agentcrop"
ALLOWED_EXTENSIONS = {'png', 'jpeg', 'jpg'}
folder_num = 0
folders_list = []

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

# initialize scheduler
scheduler = APScheduler()
scheduler.api_enabled = True
scheduler.init_app(app)

# Adding Interval Job to delete folder
@scheduler.task('interval', id='clean', seconds=1800, misfire_grace_time=900)
def clean():
    global folders_list
    try:
        for folder in folders_list:
            if (time.time() - os.stat(folder).st_ctime) / 3600 > 1:
                shutil.rmtree(folder)
                folders_list.remove(folder)
                print("\n***************Removed Folder '{}'***************\n".format(folder))
    except:
        flash("Something Went Wrong! couldn't delete data!")

scheduler.start()

@app.route('/', methods=['GET', 'POST'])

def get_disease():
    global folder_num
    global folders_list
    if request.method == 'POST':
        if folder_num >= 1000000:
            folder_num = 0
        # check if the post request has the file part
        if 'hiddenfiles' not in request.files:
            flash('No files part!')
            return redirect(request.url)
        # Create a new folder for every new file uploaded,
        # so that concurrency can be maintained
        files = request.files.getlist('hiddenfiles')
        app.config['UPLOAD_FOLDER'] = "./static/test"
        app.config['UPLOAD_FOLDER'] = app.config['UPLOAD_FOLDER'] + '/predict_' + str(folder_num).rjust(6, "0")
        if not os.path.exists(app.config['UPLOAD_FOLDER']):
            os.makedirs(app.config['UPLOAD_FOLDER'])
            folders_list.append(app.config['UPLOAD_FOLDER'])
            folder_num += 1
        for file in files:
            if file.filename == '':
                flash('No Files are Selected!')
                return redirect(request.url)
            if file and allowed_file(file.filename):
                filename = secure_filename(file.filename)
                file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            else:
                flash("Invalid file type! Only PNG, JPEG/JPG files are supported.")
                return redirect('/')
        try:
            if len(os.listdir(app.config['UPLOAD_FOLDER'])) > 0:
                diseases = predict(app.config['UPLOAD_FOLDER'])
                return render_template('show_prediction.html',
                folder = app.config['UPLOAD_FOLDER'],
                predictions = diseases)
        except:
            return redirect('/')

    return render_template('index.html')

@app.route('/favicon.ico')

def favicon():
    return send_from_directory(os.path.join(app.root_path, 'static'), 'Agent-Crop-Icon.png')

#API requests are handled here
@app.route('/api/predict', methods=['POST'])

def api_predict():
    global folder_num
    global folders_list
    if folder_num >= 1000000:
            folder_num = 0
    # check if the post request has the file part
    if 'files' not in request.files:
        return {"Error": "No files part found."}
    # Create a new folder for every new file uploaded,
    # so that concurrency can be maintained
    files = request.files.getlist('files')
    app.config['UPLOAD_FOLDER'] = "./static/test"
    app.config['UPLOAD_FOLDER'] = app.config['UPLOAD_FOLDER'] + '/predict_' + str(folder_num).rjust(6, "0")
    if not os.path.exists(app.config['UPLOAD_FOLDER']):
        os.makedirs(app.config['UPLOAD_FOLDER'])
        folders_list.append(app.config['UPLOAD_FOLDER'])
        folder_num += 1
    for file in files:
        if file.filename == '':
            return {"Error": "No Files are Selected!"}
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        else:
            return {"Error": "Invalid file type! Only PNG, JPEG/JPG files are supported."}
    try:
        if len(os.listdir(app.config['UPLOAD_FOLDER'])) > 0:
            diseases = predict(app.config['UPLOAD_FOLDER'])
            return diseases
    except:
        return {"Error": "Something Went Wrong!"}

max_stars_repo_name: Aashrut/Agent-Crop

max_issues_repo_name: Aashrut/Agent-Crop

max_forks_repo_name: Aashrut/Agent-Crop


text:
#!/usr/bin/env python
"""
Stacks boss spectra.
"""

import argparse
import numpy as np
import h5py

import qusp

def main():
    # parse command-line arguments
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("--verbose", action="store_true",
        help="more verbose output")
    parser.add_argument("--norm-min", type=float, default=1275,
        help="min norm wavelength")
    parser.add_argument("--norm-max", type=float, default=1285,
        help="max norm wavelength")
    parser.add_argument("--z-col", type=int, default=1,
        help="redshift column index")
    parser.add_argument("--tpcorr", type=str, default=None,
        help="throughput correction filename")
    parser.add_argument("-o", "--output", type=str, default=None,
        help="output file name")
    parser.add_argument("--use-lite", action="store_true",
        help="use lite spectra files")
    parser.add_argument("--keep", type=str, default=None,
        help="only keep these targets")
    parser.add_argument("--observed", action="store_true",
        help="stack in observed frame")
    qusp.target.add_args(parser)
    qusp.Paths.add_args(parser)
    args = parser.parse_args()

    # setup boss data directory path
    paths = qusp.Paths(**qusp.Paths.from_args(args))
    # read target list
    target_list = qusp.target.load_target_list_from_args(args, fields=[('z', float, args.z_col)])

    if args.keep:
        keep_list = set(np.loadtxt(args.keep, dtype='S15').tolist())
        print len(keep_list)
        keep_target_list = []
        for target in target_list:
            if args.use_lite:
                tstring = '%d-%d-%d' %(target['boss_plate'], target['boss_mjd'], target['boss_fiber'])
            else:
                tstring = target.to_string()
            if tstring in keep_list:
                keep_target_list.append(target)
        target_list = keep_target_list

    print len(target_list)

    if args.tpcorr:
        tpcorr = h5py.File(args.tpcorr)
        tpcorr_wave = tpcorr['wave'].value
    else:
        tpcorr = None

    norm_min = qusp.wavelength.Wavelength(args.norm_min)
    norm_max = qusp.wavelength.Wavelength(args.norm_max)

    # initialize stack arrays
    ntargets = 0

    if args.observed:
        min_fid_index = 0
        max_fid_index = 4800
    else:
        min_fid_index = -6150
        max_fid_index = -670
    fid_npixels = max_fid_index - min_fid_index

    continuum_wave_centers = qusp.wavelength.get_fiducial_wavelength(np.arange(min_fid_index, max_fid_index))

    print qusp.wavelength.get_fiducial_wavelength(min_fid_index), qusp.wavelength.get_fiducial_wavelength(max_fid_index-1)
    print fid_npixels

    norm_min_index = np.round(qusp.wavelength.get_fiducial_pixel_index_offset(np.log10(norm_min))).astype(int)-min_fid_index
    norm_max_index = np.round(qusp.wavelength.get_fiducial_pixel_index_offset(np.log10(norm_max))).astype(int)-min_fid_index

    redshifted_fluxes = np.ma.empty((len(target_list), fid_npixels))
    redshifted_fluxes[:] = np.ma.masked

    def get_lite_spectra(target_list):
        for target in target_list:
            try:
                spec = qusp.target.get_lite_spectrum(target, paths=paths)
                if tpcorr:
                    from scipy.interpolate import interp1d
                    wave = tpcorr['wave'].value
                    try:
                        value = tpcorr['%s/%s/%s' % (target['plate'], target['mjd'], target['fiber'])].value
                    except KeyError:
                        print 'No tpcorr entry for: %s' % target.to_string()
                        continue
                    correction = interp1d(wave, value, kind='linear', copy=False)
                    spec = spec.create_corrected(correction)
                yield target, spec
            except IOError:
                continue

    if args.use_lite:
        spectrum_gen = get_lite_spectra(target_list)
    else:
        spectrum_gen = qusp.target.get_corrected_spectrum(target_list, tpcorr=tpcorr, paths=paths)

    targets_used = []

    # loop over targets
    for target, combined in spectrum_gen:

        if args.observed:
            continuum_wave = combined.wavelength
        else:
            continuum_wave = combined.wavelength/(1+target['z'])

        fid_offsets = qusp.wavelength.get_fiducial_pixel_index_offset(np.log10(continuum_wave))
        fid_offset_indices = np.round(fid_offsets).astype(int)
        continuum_indices = fid_offset_indices - min_fid_index

        valid_pixels = (continuum_indices < fid_npixels) & (continuum_indices >= 0)
        norm_pixels = (continuum_indices < norm_max_index) & (continuum_indices >=  norm_min_index)

        normfluxes = np.ma.masked_array(combined.flux.values[norm_pixels], mask=(combined.ivar.values[norm_pixels] == 0))
        norm = np.ma.average(normfluxes, weights=combined.ivar.values[norm_pixels])

        if np.sum(normfluxes.mask) == np.sum(norm_pixels):
            print 'no unmasked pixels in norm region', target.to_string(), target['z']
            continue

        if norm <= 0:
            print 'norm less than 0', target.to_string(), target['z']
            continue

        redshifted_fluxes[ntargets, continuum_indices[valid_pixels]] = combined.flux.values[valid_pixels]/norm

        ntargets += 1
        targets_used.append(target.to_string())

    print redshifted_fluxes.shape
    print ntargets

    with open(args.output+'.txt', 'w') as outfile:
        for target in sorted(targets_used):
            outfile.write('%s\n'%target)

    median_flux = np.ma.median(redshifted_fluxes, axis=0)
    mean_flux = np.ma.mean(redshifted_fluxes, axis=0)

    if args.output:
        outfilename = args.output+'.hdf5'
        if args.verbose:
            print 'Saving stack to file: %s' % outfilename
        # save target list with sn column
        outfile = h5py.File(outfilename, 'w')

        outfile.create_dataset('median_flux', data=median_flux)
        outfile.create_dataset('mean_flux', data=mean_flux)
        outfile.create_dataset('wavelength', data=continuum_wave_centers)
        outfile.attrs['ntargets'] = ntargets

if __name__ == '__main__':
    main()

ext: py

max_stars_repo_name: dmargala/qusp

max_issues_repo_name: dmargala/qusp

max_forks_repo_name: dmargala/qusp

reason: import numpy,from scipy


text:
import numpy as np

class Path(list):
    def __init__(self, *args):
        super(Path, self).__init__(*args)
        self.__sorted_str = None

    @property
    def confidence(self):
        """
        Cumulative product of edges' confidence
        :return:
        """
        return np.prod([edge.confidence for edge in self])

    def addable(self, candidate_edge):
        """
        Whether candidate edge would be connected to the current path
        :param candidate_edge:
        :return:
        """
        if len(self) == 0:
            return True
        for edge in self:
            if edge.uri == candidate_edge.uri or \
                    edge.source_node == candidate_edge.source_node or \
                    edge.dest_node == candidate_edge.dest_node or \
                    edge.source_node == candidate_edge.dest_node or \
                    edge.dest_node == candidate_edge.source_node:
                return True
        return False

    def replace_edge(self, old_edge, new_edge):
        """
        Create a new path in which an existing edge is replaced with the new edge
        :param old_edge:
        :param new_edge:
        :return: None if the old_edge is not in the current path
        """
        try:
            new_path = Path(self)
            new_path[new_path.index(old_edge)] = new_edge
            return new_path
        except ValueError:
            return None

    def __eq__(self, other):
        if len(self) == len(other):
            same_flag = True
            for edge in self:
                if edge not in other:
                    same_flag = False
                    break
            if same_flag:
                return True
        return False

    def generic_equal(self, other):
        return self.__generic_equal(other)[0]

    def generic_equal_with_substitutable_id(self, other):
        output = self.__generic_equal(other)
        if not output[0]:
            return False
        return len(set(output[1])) == 1

    def __generic_equal(self, other):
        """
        Check whether the other path in the same as the current one, perhaps with different generic node id
        :param other:
        :return:
        """
        output = []
        if len(self) == len(other):
            for edge1 in self:
                edge_found = False
                for edge2 in other:
                    if edge1.generic_equal(edge2):
                        edge_found = True
                        if edge1.source_node.generic_equal(
                                edge2.source_node) and edge1.source_node.are_all_uris_generic() \
                                and edge2.source_node.are_all_uris_generic():
                            output.append(
                                frozenset(
                                    [edge1.source_node.first_uri_if_only(), edge2.source_node.first_uri_if_only()]))
                        if edge1.dest_node.generic_equal(
                                edge2.dest_node) and edge1.dest_node.are_all_uris_generic() \
                                and edge2.dest_node.are_all_uris_generic():
                            output.append(
                                frozenset([edge1.dest_node.first_uri_if_only(), edge2.dest_node.first_uri_if_only()]))
                        if edge1.uri.is_generic() and edge1.uri.generic_equal(
                                edge2.uri):
                            output.append(
                                frozenset([edge1.uri, edge2.uri]))
                        break
                if not edge_found:
                    return False, []
            return True, output
        else:
            return False, []

    def __str__(self):
        if self.__sorted_str is None:
            self.__sorted_str = '-'.join(sorted([item.full_path() for item in self]))
        return self.__sorted_str

max_stars_repo_name: tamlhp/kbqa

max_issues_repo_name: tamlhp/kbqa

max_forks_repo_name: tamlhp/kbqa

reason: import numpy


text:
# By aplying Y = W * x + b. It get a line that fit to the points.

# random dots
import numpy as np
num_puntos = 100
conjunto_puntos = []
for i in range(num_puntos):
    x1 = np.random.normal(0.0, 0.55)
    y1 = x1 * 0.1 + 0.3 + np.random.normal(0.0, 0.03)
    conjunto_puntos.append([x1, y1])
x_data = [v[0] for v in conjunto_puntos]
y_data = [v[1] for v in conjunto_puntos]

# show dots
import matplotlib.pyplot as plt
plt.plot(x_data, y_data, 'ro', label='Dots')
plt.legend()
plt.show()

# TensorFlow
#   data
import tensorflow as tf
w = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = w * x_data + b

#   cost function
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

#   train
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for step in range(8):
    sess.run(train)
    plt.plot(x_data, y_data, 'ro', label='Dots')
    plt.plot(x_data, sess.run(w) * x_data + sess.run(b))
    plt.show()

max_stars_repo_path: 02_Linear regression.py

max_issues_repo_path: 02_Linear regression.py

max_forks_repo_path: 02_Linear regression.py


text:
from symbol import parameters
import tkinter as tk
from collections import namedtuple
from numpy import nan

class InterfaceState:
    """Seperation layer between interface and logic, keeps information of currently """

    def __init__(self, master) -> None:
        self.parameters = self.ParameterState(master)
        self.transform = self.TransformState(master)
        self.display = self.DisplayState(master)

    class ParameterState:

        parameter_names = ["constant", "linear", "annual (sin)", "annual (cos)", "bi-annual (sin)", "bi-annual (cos)"]

        def __init__(self, master) -> None:
            self.values = {name : tk.DoubleVar(master, value = 0) for name in self.parameter_names}
            self.sigmas = {name : tk.DoubleVar(master, value = nan) for name in self.parameter_names}
            self.is_custom = {name : tk.BooleanVar(master, value = True) for name in self.parameter_names}

            self.is_custom["constant"].set(False)
            self.is_custom["linear"].set(False)

        def get_parameter_dict(self):
            """Return all vars as Parameter tuples"""
            Parameter = namedtuple("Parameter", ["value", "sigma", "is_custom"])
            result = {}

            for name, value, sigma, is_custom in zip(self.parameter_names,
                                                    self.values.values(),
                                                    self.sigmas.values(),
                                                    self.is_custom.values()):
                result[name] = Parameter(value, sigma, is_custom)

            return result

    class TransformState:

        def __init__(self, master) -> None:
            self.fpath_a = tk.StringVar(master, value = "")
            self.fpath_b = tk.StringVar(master, value = "")
            self.interpolation_type = tk.StringVar(master, value= "Linear")

            self.weighted = tk.BooleanVar(master, value = False)

            self.chi_squared = tk.DoubleVar()
            self.weighted_root_mean_squared = tk.DoubleVar()

            self.t0 = tk.DoubleVar(master, value = 2000)
            self.t_min = tk.DoubleVar(master, value = 0)
            self.t_max = tk.DoubleVar(master, value = 0)
            self.max_sigma = tk.DoubleVar(master, value = 0)

    class DisplayState:

        def __init__(self, master) -> None:
            self.t_min_bound = tk.DoubleVar(master, value = 0)
            self.t_max_bound = tk.DoubleVar(master, value = 0)
            self.max_sigma_bound = tk.DoubleVar(master, value = 0)

            self.selected_serie = tk.StringVar(master, value = "X")
            self.selected_sigma = tk.StringVar(master, value = "X_SIGMA")
            self.master = tk.StringVar(master, value = "a")

            self.plot_type = tk.StringVar(master, "Residuals")

max_stars_repo_name: AdrianLundell/adrians-geotools

max_issues_repo_name: AdrianLundell/adrians-geotools

max_forks_repo_name: AdrianLundell/adrians-geotools

reason: from numpy


text:
"""
    Tests for VF2 isomorphism algorithm.
"""

import os
import struct
import random

from nose.tools import assert_true, assert_equal
from nose import SkipTest
import networkx as nx
from networkx.algorithms import isomorphism as iso

class TestWikipediaExample(object):
    # Source: http://en.wikipedia.org/wiki/Graph_isomorphism

    # Nodes 'a', 'b', 'c' and 'd' form a column.
    # Nodes 'g', 'h', 'i' and 'j' form a column.
    g1edges = [['a','g'], ['a','h'], ['a','i'],
               ['b','g'], ['b','h'], ['b','j'],
               ['c','g'], ['c','i'], ['c','j'],
               ['d','h'], ['d','i'], ['d','j']]

    # Nodes 1,2,3,4 form the clockwise corners of a large square.
    # Nodes 5,6,7,8 form the clockwise corners of a small square
    g2edges = [[1,2], [2,3], [3,4], [4,1],
               [5,6], [6,7], [7,8], [8,5],
               [1,5], [2,6], [3,7], [4,8]]

    def test_graph(self):
        g1 = nx.Graph()
        g2 = nx.Graph()
        g1.add_edges_from(self.g1edges)
        g2.add_edges_from(self.g2edges)
        gm = iso.GraphMatcher(g1,g2)
        assert_true(gm.is_isomorphic())

        mapping = sorted(gm.mapping.items())
# this mapping is only one of the possibilies
# so this test needs to be reconsidered
#        isomap = [('a', 1), ('b', 6), ('c', 3), ('d', 8),
#                  ('g', 2), ('h', 5), ('i', 4), ('j', 7)]
#        assert_equal(mapping, isomap)

    def test_subgraph(self):
        g1 = nx.Graph()
        g2 = nx.Graph()
        g1.add_edges_from(self.g1edges)
        g2.add_edges_from(self.g2edges)
        g3 = g2.subgraph([1,2,3,4])
        gm = iso.GraphMatcher(g1,g3)
        assert_true(gm.subgraph_is_isomorphic())

class TestVF2GraphDB(object):
    # http://amalfi.dis.unina.it/graph/db/

    @staticmethod
    def create_graph(filename):
        """Creates a Graph instance from the filename."""

        # The file is assumed to be in the format from the VF2 graph database.
        # Each file is composed of 16-bit numbers (unsigned short int).
        # So we will want to read 2 bytes at a time.

        # We can read the number as follows:
        #   number = struct.unpack('<H', file.read(2))
        # This says, expect the data in little-endian encoding
        # as an unsigned short int and unpack 2 bytes from the file.

        fh = open(filename, mode='rb')

        # Grab the number of nodes.
        # Node numeration is 0-based, so the first node has index 0.
        nodes = struct.unpack('<H', fh.read(2))[0]

        graph = nx.Graph()
        for from_node in range(nodes):
            # Get the number of edges.
            edges = struct.unpack('<H', fh.read(2))[0]
            for edge in range(edges):
                # Get the terminal node.
                to_node = struct.unpack('<H', fh.read(2))[0]
                graph.add_edge(from_node, to_node)

        fh.close()
        return graph

    def test_graph(self):
        head,tail = os.path.split(__file__)
        g1 = self.create_graph(os.path.join(head,'iso_r01_s80.A99'))
        g2 = self.create_graph(os.path.join(head,'iso_r01_s80.B99'))
        gm = iso.GraphMatcher(g1,g2)
        assert_true(gm.is_isomorphic())

    def test_subgraph(self):
        # A is the subgraph
        # B is the full graph
        head,tail = os.path.split(__file__)
        subgraph = self.create_graph(os.path.join(head,'si2_b06_m200.A99'))
        graph = self.create_graph(os.path.join(head,'si2_b06_m200.B99'))
        gm = iso.GraphMatcher(graph, subgraph)
        assert_true(gm.subgraph_is_isomorphic())

class TestAtlas(object):
    @classmethod
    def setupClass(cls):
        global atlas
        import platform
        if platform.python_implementation()=='Jython':
            raise SkipTest('graph atlas not available under Jython.')
        import networkx.generators.atlas as atlas

    def setUp(self):
        self.GAG=atlas.graph_atlas_g()

    def test_graph_atlas(self):
        #Atlas = nx.graph_atlas_g()[0:208] # 208, 6 nodes or less
        Atlas = self.GAG[0:100]
        alphabet = list(range(26))
        for graph in Atlas:
            nlist = graph.nodes()
            labels = alphabet[:len(nlist)]
            for s in range(10):
                random.shuffle(labels)
                d = dict(zip(nlist,labels))
                relabel = nx.relabel_nodes(graph, d)
                gm = iso.GraphMatcher(graph, relabel)
                assert_true(gm.is_isomorphic())

def test_multiedge():
    # Simple test for multigraphs
    # Need something much more rigorous
    edges = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5),
             (5, 6), (6, 7), (7, 8), (8, 9), (9, 10),
             (10, 11), (10, 11), (11, 12), (11, 12),
             (12, 13), (12, 13), (13, 14), (13, 14),
             (14, 15), (14, 15), (15, 16), (15, 16),
             (16, 17), (16, 17), (17, 18), (17, 18),
             (18, 19), (18, 19), (19, 0), (19, 0)]
    nodes = list(range(20))

    for g1 in [nx.MultiGraph(), nx.MultiDiGraph()]:
        g1.add_edges_from(edges)
        for _ in range(10):
            new_nodes = list(nodes)
            random.shuffle(new_nodes)
            d = dict(zip(nodes, new_nodes))
            g2 = nx.relabel_nodes(g1, d)
            if not g1.is_directed():
                gm = iso.GraphMatcher(g1,g2)
            else:
                gm = iso.DiGraphMatcher(g1,g2)
            assert_true(gm.is_isomorphic())

def test_selfloop():
    # Simple test for graphs with selfloops
    edges = [(0, 1), (0, 2), (1, 2), (1, 3), (2, 2),
             (2, 4), (3, 1), (3, 2), (4, 2), (4, 5), (5, 4)]
    nodes = list(range(6))

    for g1 in [nx.Graph(), nx.DiGraph()]:
        g1.add_edges_from(edges)
        for _ in range(100):
            new_nodes = list(nodes)
            random.shuffle(new_nodes)
            d = dict(zip(nodes, new_nodes))
            g2 = nx.relabel_nodes(g1, d)
            if not g1.is_directed():
                gm = iso.GraphMatcher(g1,g2)
            else:
                gm = iso.DiGraphMatcher(g1,g2)
            assert_true(gm.is_isomorphic())

def test_isomorphism_iter1():
    # As described in:
    # http://groups.google.com/group/networkx-discuss/browse_thread/thread/2ff65c67f5e3b99f/d674544ebea359bb?fwc=1
    g1 = nx.DiGraph()
    g2 = nx.DiGraph()
    g3 = nx.DiGraph()
    g1.add_edge('A','B')
    g1.add_edge('B','C')
    g2.add_edge('Y','Z')
    g3.add_edge('Z','Y')
    gm12 = iso.DiGraphMatcher(g1,g2)
    gm13 = iso.DiGraphMatcher(g1,g3)
    x = list(gm12.subgraph_isomorphisms_iter())
    y = list(gm13.subgraph_isomorphisms_iter())
    assert_true({'A':'Y','B':'Z'} in x)
    assert_true({'B':'Y','C':'Z'} in x)
    assert_true({'A':'Z','B':'Y'} in y)
    assert_true({'B':'Z','C':'Y'} in y)
    assert_equal(len(x),len(y))
    assert_equal(len(x),2)

def test_isomorphism_iter2():
    # Path
    for L in range(2,10):
        g1 = nx.path_graph(L)
        gm = iso.GraphMatcher(g1,g1)
        s = len(list(gm.isomorphisms_iter()))
        assert_equal(s,2)
    # Cycle
    for L in range(3,10):
        g1 = nx.cycle_graph(L)
        gm = iso.GraphMatcher(g1,g1)
        s = len(list(gm.isomorphisms_iter()))
        assert_equal(s, 2*L)

def test_multiple():
    # Verify that we can use the graph matcher multiple times
    edges = [('A','B'),('B','A'),('B','C')]
    for g1,g2 in [(nx.Graph(),nx.Graph()), (nx.DiGraph(),nx.DiGraph())]:
        g1.add_edges_from(edges)
        g2.add_edges_from(edges)
        g3 = nx.subgraph(g2, ['A','B'])
        if not g1.is_directed():
            gmA = iso.GraphMatcher(g1,g2)
            gmB = iso.GraphMatcher(g1,g3)
        else:
            gmA = iso.DiGraphMatcher(g1,g2)
            gmB = iso.DiGraphMatcher(g1,g3)
        assert_true(gmA.is_isomorphic())
        g2.remove_node('C')
        assert_true(gmA.subgraph_is_isomorphic())
        assert_true(gmB.subgraph_is_isomorphic())
#        for m in [gmB.mapping, gmB.mapping]:
#            assert_true(m['A'] == 'A')
#            assert_true(m['B'] == 'B')
#            assert_true('C' not in m)

max_stars_repo_name: nishaero/wifi-userseg-ryu

max_issues_repo_name: nishaero/wifi-userseg-ryu

max_forks_repo_name: nishaero/wifi-userseg-ryu

reason: import networkx,from networkx


text:
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F

def hidden_init(layer):
    fan_in = layer.weight.data.size()[0]
    lim = 1. / np.sqrt(fan_in)
    return (-lim, lim)

class CNNNetwork(nn.Module):
    """Actor (Policy) Model."""

    last_conv_layer_size = 16
    conv_output_size = last_conv_layer_size * 7 * 7

    def __init__(self, state_size, action_size, seed):
        """Initialize parameters and build model.
        Params
        ======
            state_size (int): Dimension of each state
            action_size (int): Dimension of each action
            seed (int): Random seed
        """
        super(CNNNetwork, self).__init__()
        self.seed = torch.manual_seed(seed)

        self.conv1 = nn.Conv2d(1, 8, kernel_size=(2, 4), stride=(1, 2))                 # 42x84 --> 40x40
        self.conv2 = nn.Conv2d(8, 16, kernel_size=4, stride=2)                          # 40x40 --> 18x18
        self.conv3 = nn.Conv2d(16, self.last_conv_layer_size, kernel_size=4, stride=2)  # 18x18 --> 7x7

        self.bn1=nn.BatchNorm2d(8)
        self.bn2=nn.BatchNorm2d(16)
        self.bn3=nn.BatchNorm2d(self.last_conv_layer_size)

        self.maxpool = nn.MaxPool2d(kernel_size=2, stride=1, padding=0)

        self.fc1 = nn.Linear(self.conv_output_size, 512)
        self.fc2 = nn.Linear(512, action_size)

        self.reset_parameters()

        self.dropout = nn.Dropout(p=0.4)

    def reset_parameters(self):
        self.fc1.weight.data.uniform_(*hidden_init(self.fc1))
        self.fc2.weight.data.uniform_(*hidden_init(self.fc2))

    def forward(self, x):
        """Build a network that maps state -> action values."""
        x = x.permute(0, 3, 1, 2)

        do_print = False

        if do_print:
            print(x.shape)

        x = F.relu(self.conv1(x))
        x = self.bn1(x)
        x = self.maxpool(x)

        if do_print:
            print(x.shape)

        x = F.relu(self.conv2(x))
        x = self.bn2(x)
        x = self.maxpool(x)

        if do_print:
            print(x.shape)

        x = F.relu(self.conv3(x))
        x = self.bn3(x)
        x = self.maxpool(x)

        if do_print:
            print(x.shape)

        x = x.view(-1, self.conv_output_size)

        if do_print:
            print(x.shape)

        #x = self.dropout(x)
        x = F.relu(self.fc1(x))

        if do_print:
            print(x.shape)

        #x = self.dropout(x)
        x = self.fc2(x)

        if do_print:
            print(x.shape)
            print()

        return x

max_stars_repo_name: AnKra/deep-reinforcement-learning

max_issues_repo_name: AnKra/deep-reinforcement-learning

max_forks_repo_name: AnKra/deep-reinforcement-learning


text:
import acme
import numpy as np
import pandas as pd
from random import randint

def products(number_of_products=30):
    """products function provides a total of 30 products with random values for the name, price, weight, and        flammability fields. For the name attribute, I needed a list of possible name to choose from:
       Will print a list of the product names, the total price of the products, the
       total weight of the products, and the average flammability of the products.
    """

    # List of names needed for random choice
    adj_list = ['Awesome', 'Shiny', 'Impressive', 'Portable', 'Improved']
    noun_list = ['Anvil', 'Catapult' 'Disguise' 'Mousetrap', '???']
    return [acme.Product("{} {}".format(random.choice(adj_list),
                                        random.choice(noun_list)),
                         random.randint(5, 100), random.randint(5, 100),
                         random.uniform(0.0, 2.5)) for i in range(amount)]

# Report function
def report(list_of_products):
    product_name = []
    product_price = []
    product_weight = []
    product_flammability = []
    for i in list_of_products:
        product_name.append(i.product_names)
        product_price.append(i.product_prices)
        product_weight.append(i.product_weights)
        product_flammability.append(i.product_flammabilities)
    print('ACME CORPORATION OFFICIAL INVENTORY REPORT')
    print('Unique product names:', len(list(set(product_name))))
    print('Average price:', sum(product_price) / len(product_price))
    print('Average weight:', sum(product_weight) / len(product_weight))
    print('Average flammability:', sum(product_flammability) / len(product_flammability))

if __name__ == '__main__':
    report(list_of_products())


text:
import numpy as np
import pandas as pd
import os.path
import random
import collections
from bisect import bisect_right
from bisect import bisect_left
from .. import multimatch_gaze as mp

dtype = [
    ("onset", "<f8"),
    ("duration", "<f8"),
    ("label", "<U10"),
    ("start_x", "<f8"),
    ("start_y", "<f8"),
    ("end_x", "<f8"),
    ("end_y", "<f8"),
    ("amp", "<f8"),
    ("peak_vel", "<f8"),
    ("med_vel", "<f8"),
    ("avg_vel", "<f8"),
]

def same_sample(run=1, subj=1):
    """duplicate dataset to force exactly similar scanpaths. Choose the run
    (integer between 1-8) and whether you want a lab (1) or mri (2) subject"""
    if subj == 1:
        sub = "sub-30"
    else:
        sub = "sub-10"
    path = os.path.join(
        "multimatch_gaze/tests/testdata",
        "{}_task-movie_run-{}_events.tsv".format(sub, run),
    )
    loc = os.path.join(
        "multimatch_gaze/tests/testdata", "locations_run-{}_events.tsv".format(run)
    )
    data = np.recfromcsv(
        path,
        delimiter="\t",
        dtype={
            "names": (
                "onset",
                "duration",
                "label",
                "start_x",
                "start_y",
                "end_x",
                "end_y",
                "amp",
                "peak_vel",
                "med_vel",
                "avg_vel",
            ),
            "formats": (
                "f8",
                "f8",
                "U10",
                "f8",
                "f8",
                "f8",
                "f8",
                "f8",
                "f8",
                "f8",
                "f8",
            ),
        },
    )
    data2 = data
    shots = pd.read_csv(loc, sep="\t")
    return data, data2, shots

def short_shots(run=3):
    """create a shortened shots location annotation to test longshots()"""
    loc = os.path.join(
        "multimatch_gaze/tests/testdata", "locations_run-{}_events.tsv".format(run)
    )
    shots = pd.read_csv(loc, sep="\t")
    shortshots = shots[0:20]
    return shortshots

def mk_fix_vector(length=5):
    """creates a random length x 3 fixation vector in form of a record array"""
    fix = np.recarray(
        (0,), dtype=[("start_x", "<f8"), ("start_y", "<f8"), ("duration", "<f8")]
    )
    for i in range(0, length):
        fixation = np.array(
            (
                np.random.uniform(1, 720),
                np.random.uniform(1, 720),
                np.random.uniform(0.01, 5),
            ),
            dtype=[("start_x", float), ("start_y", float), ("duration", float)],
        )
        fix = np.append(fix, fixation)
    return fix

def mk_strucarray(length=5):
    """create a random scanpath in the data format generateScanpathStructureArray
    would output"""
    fixation_x = random.sample(range(700), length)
    fixation_y = random.sample(range(700), length)
    fixation_dur = random.sample(range(5), length)
    saccade_x = random.sample(range(700), length - 1)
    saccade_y = random.sample(range(700), length - 1)
    saccade_lenx = random.sample(range(700), length - 1)
    saccade_leny = random.sample(range(700), length - 1)
    saccade_rho = random.sample(range(700), length - 1)
    saccade_theta = random.sample(range(4), length - 1)
    eyedata = dict(
        fix=dict(x=fixation_x, y=fixation_y, dur=fixation_dur,),
        sac=dict(
            x=saccade_x,
            y=saccade_y,
            lenx=saccade_lenx,
            leny=saccade_leny,
            theta=saccade_theta,
            rho=saccade_rho,
        ),
    )
    eyedata2 = dict(
        fix=dict(
            x=fixation_x[::-1] * 2, y=fixation_y[::-1] * 2, dur=fixation_dur[::-1] * 2,
        ),
        sac=dict(
            x=saccade_x[::-1] * 2,
            y=saccade_y[::-1] * 2,
            lenx=saccade_lenx[::-1] * 2,
            leny=saccade_leny[::-1] * 2,
            theta=saccade_theta[::-1] * 2,
            rho=saccade_rho[::-1] * 2,
        ),
    )
    return eyedata, eyedata2

def mk_angles():
    """creates vectors with predefined angular relations. angles1 and angles2
    contain the following properties: 1. same 0, 2. 60 diff, 3. 90 diff,
    4.120 diff,4. 180 diff (max. dissimilar). They are in sectors (0,1) and
    (0, -1).
    Angles3 and angles4 contain the same properties reversed and lie in sectors
    (-1, 0) and (-1, -1)"""
    angles1 = dict(sac=dict(theta=[0, 0.523, 0.785, 1.04, 1.57]))
    angles2 = dict(sac=dict(theta=[0, -0.523, -0.785, -1.04, -1.57]))
    angles3 = dict(sac=dict(theta=[1.57, 2.093, 2.356, 2.617, 3.14]))
    angles4 = dict(sac=dict(theta=[-1.57, -2.093, -2.356, -2.617, -3.14]))
    path = [0, 6, 12, 18, 24]
    M_assignment = np.arange(5 * 5).reshape(5, 5)
    return M_assignment, path, angles1, angles2, angles3, angles4

def mk_durs():
    """create some example duration for test_durationsim()"""
    durations1 = collections.OrderedDict()
    durations2 = collections.OrderedDict()
    durations1 = dict(fix=dict(dur=[0.001, 20.0, 7, -18, -2.0]))
    durations2 = dict(fix=dict(dur=[0.008, 18.0, 7, -11, 3.0]))
    path = [0, 6, 12, 18, 24]
    M_assignment = np.arange(5 * 5).reshape(5, 5)
    return M_assignment, path, durations1, durations2

def mk_supershort_shots():
    data = {
        "onset": np.arange(0, 20),
        "duration": np.repeat(1, 20),
        "locale": np.repeat("somewhere", 20),
    }
    shots = pd.DataFrame(data)
    return shots

def mk_longershots():
    data = {
        "onset": np.arange(0, 20),
        "duration": np.repeat(5, 20),
        "locale": np.repeat("somewhere", 20),
    }
    shots = pd.DataFrame(data)
    return shots

# some functions to work specifically with studyforrest eye tracking data

# Functions specifically for the data at hand

def takeclosestright(mylist, mynumber):
    """Return integer closest right to 'myNumber' in an ordered list.

    :param: mylist: int
    :param: mynumber: array

    :return: after: float, number within mylist closest to right of my number

    """

    pos = bisect_right(mylist, mynumber)
    if pos == 0:
        return mylist[0]
    if pos == len(mylist):
        return mylist[-1]
    after = mylist[pos]
    return after

def takeclosestleft(mylist, mynumber):
    """Return integer closest left to 'myNumber' in an ordered list.

    :param: mylist: int
    :param: mynumber: array

    :return: after: float, number within mylist closest to the left of mynumber
    """

    pos = bisect_left(mylist, mynumber)
    if pos == 0:
        return mylist[0]
    if pos == len(mylist):
        return mylist[-1]
    before = mylist[pos - 1]
    return before

def create_onsets(data, dur):
    """Create shot onsets from studyforrests location annotation.

    Create onset times of all shots of at least 'dur' seconds of length.

    :param: data: dataframe
        location annotation from studyforrest
    :param: dur: float
        time in seconds a shot should at least be long

    :return: onsets: array-like, list of shot onset times
    """
    onsets = []
    for index, row in data.iterrows():
        if row["duration"] >= dur:
            onsets.append(row["onset"])
    return onsets

def create_offsets(data, dur):
    """Create shot offsets from studyforrests location annotation.

    Create offset times of all shots of at least 'dur' seconds of length

    :param: data: dataframe, location annotation from studyforrest
    :param: dur: float, time in seconds a shot should at least be long

    :return: onsets: array-like, list of shot offset times
    """

    offsets = []
    for index, row in data.iterrows():
        if row["duration"] >= dur:
            # calculate end of shot by adding onset + duration, subtract an
            # epsilon to be really sure not to get into a cut
            offsets.append(row["onset"] + row["duration"] - 0.03)
    return offsets

def create_chunks(onsets, fixations, dur):
    """Chunk eyetracking data into scanpaths.

    Use onset data to obtain indices of full eyetracking data
    for chunking.

    :param: onsets:  array-like, onset times of movie shots
    :param: fixations: record array, nx4 fixation vector
        (onset, x, y, duration),
        output of preprocess() function
    :param: dur: float, desired duration of segment length

    :return: startidx, endix: array, start and end ids of eyemovement data
            to chunk into segments
    """
    # initialize empty lists
    startidx, endidx = [], []
    for shotonset in onsets:
        start = takeclosestright(fixations["onset"], shotonset)
        startidx.append(np.where(fixations["onset"] == start)[0].tolist())
        end = takeclosestright(fixations["onset"], shotonset + dur)
        endidx.append(np.where(fixations["onset"] == end)[0].tolist())
    # flatten the nested lists
    startidx = [element for sublist in startidx for element in sublist]
    endidx = [element for sublist in endidx for element in sublist]
    return startidx, endidx

def create_offsetchunks(offsets, fixations, dur):
    """Chunk eyetracking data into scanpaths.

    Use offset data to obtain indices of full eyetracking data
    for chunking.

    :param: offsets:  array-like, offset times of movie shots
    :param: fixations: record array, nx4 fixation vector
        (onset, x, y, duration), output of preprocess()
    :param: dur: float, desired duration of segment length

    :return: startidx, endix: array start and end ids of eyemovement data
            to chunk into segments
    """
    startidx, endidx = [], []
    for shotoffset in offsets:
        start = takeclosestright(fixations["onset"], shotoffset - dur)
        startidx.append(np.where(fixations["onset"] == start)[0].tolist())
        end = takeclosestleft(fixations["onset"], shotoffset)
        endidx.append(np.where(fixations["onset"] == end)[0].tolist())
    # flatten the nested lists
    startidx = [element for sublist in startidx for element in sublist]
    endidx = [element for sublist in endidx for element in sublist]
    return startidx, endidx

def fixations_chunks(fixations, startid, endid):
    """Chunk eyemovement data into scanpaths.

    :param: fixations: record array, nx4 fixation vector
        (onset, x, y, duration), output of preprocess()
    :param: startid, endid: array, start- and end-ids of the
        scanpaths, output from either create_chunks()
        or create_offsetchunks()

    :return: fixation_vector: array-like, a nx3 fixation vector
        (x, y, duration)
    """

    fixation_vector = []
    # slice fixation data according to indices, take columns
    # start_x, start_y and duration
    for idx in range(0, len(startid)):
        ind = fixations[startid[idx] : endid[idx]][["start_x", "start_y", "duration"]]
        fixation_vector.append(ind)
    return fixation_vector

def pursuits_to_fixations(remodnav_data):
    """Transform start and endpoints of pursuits to fixations.

    Uses the output of a record array created by the remodnav algorithm for
    eye-movement classification to transform pursuit data into fixations.
    The start and end point of a pursuit are relabeled as a fixation.
    This is useful for example if the underlying stimulus material is a
    moving image - visual intake of a moving object would then resemble
    a pursuit.

    :param: npdata: recordarray, remodnav output of eyemovement data

    :return: newdata: recordarray
    """
    # initialize empty rec array of the same shape
    newdata = np.recarray((0,), dtype=dtype)
    # reassemble rec array.
    # split pursuits to use end and start as fixations later
    from copy import deepcopy

    data = deepcopy(remodnav_data)
    for i, d in enumerate(data):
        if data[i]["label"] == "PURS":
            # start and end point of pursuit get
            #  half the total duration
            d["duration"] = d["duration"] / 2
            d["label"] = "FIXA"
            d2 = deepcopy(d)
            # end point of the pursuit is start
            # of new fixation
            d2["onset"] += d2["duration"]
            d2["start_x"] = d2["end_x"]
            d2["start_y"] = d2["end_y"]
            newdata = np.append(newdata, np.array(d, dtype=dtype))
            newdata = np.append(newdata, np.array(d2, dtype=dtype))
        else:
            newdata = np.append(newdata, np.array(d, dtype=dtype))
    return newdata

def preprocess_remodnav(data, screensize):
    """Preprocess record array of eye-events.

    A record array from REMoDNaV data is preprocessed
    in the following way: Subset to only get fixation data,
    disregard out-of-frame gazes, subset to only keep x, y coordinates,
    duration.

    :param: data: recordarray, REMoDNaV output of eye events from movie
        data
    :param: screensize: list of float, screen measurements in px

    :return: fixations: array-like nx3 fixation vectors (onset, x, y,
        duration)

    """
    # only fixation labels
    filterevents = data[(data["label"] == "FIXA")]
    # within x coordinates?
    filterxbounds = filterevents[
        np.logical_and(
            filterevents["start_x"] >= 0, filterevents["start_x"] <= screensize[0]
        )
    ]
    # within y coordinates?
    filterybounds = filterxbounds[
        np.logical_and(
            filterxbounds["start_y"] >= 0, filterxbounds["end_y"] <= screensize[1]
        )
    ]
    # give me onset times, start_x, start_y and duration
    fixations = filterybounds[["onset", "start_x", "start_y", "duration"]]
    return fixations

def read_remodnav(data):
    """ Helper to read input data produced by the REMoDNaV algorithm.
    Further information on the REMoDNaV algorithm can be found here:
    https://github.com/psychoinformatics-de/remodnav
    """
    d = np.recfromcsv(data, delimiter="\t", dtype=dtype)

    return d

def longshot(shots, group_shots, ldur=4.92):
    """Group movie shots without a cut together to obtain longer segments.

    Note: This way, fewer but longer scanpaths are obtained. Example: use
    median shotlength of 4.92s.

    :param: shots: dataframe, contains movie location annotation
    :param: group_shots: boolean, if True, grouping of movie shots is performed
    :param: dur: float, length in seconds for movie shot. An attempt is made to
        group short shots without a cut together to form longer shots of ldur
        length
    :return: aggregated, dataframe of aggregated movie shots
    """
    # turn pandas dataframe shots into record array
    structshots = shots.to_records()
    if group_shots:
        i = 0
        while i < len(structshots):
            # break before running into index error
            if structshots[i] == structshots[-1]:
                break
            else:
                if (
                    (structshots[i]["duration"] < ldur)
                    & (structshots[i + 1]["duration"] < ldur)
                    & (structshots[i]["locale"] == structshots[i + 1]["locale"])
                ):
                    # add durations together and delete second row
                    structshots[i]["duration"] += structshots[i + 1]["duration"]
                    structshots = np.delete(structshots, i + 1, 0)
                else:
                    i += 1
    aggregated = pd.DataFrame(
        {
            "onset": structshots["onset"].tolist(),
            "duration": structshots["duration"].tolist(),
        },
        columns=["onset", "duration"],
    )
    return aggregated

def docomparison_forrest(
    shots,
    data1,
    data2,
    screensize=[1280, 720],
    dur=4.92,
    ldur=0,
    offset=False,
    TDur=0,
    TDir=0,
    TAmp=0,
    grouping=False,
):
    """Compare two scanpaths on five similarity dimensions.

    :param: data1, data2: recarray, eyemovement information of forrest gump studyforrest dataset
    :param: screensize: list, screen dimensions in px.
    :param: ldur: float, duration in seconds. An attempt is made to group short shots
        together to form shots of ldur length
    :param: grouping: boolean, if True, simplification is performed based on thresholds TAmp,
        TDir, and TDur
    :param: TDir: float, Direction threshold, angle in degrees.
    :param: TDur: float, Duration threshold, duration in seconds.
    :param: TAmp: float, Amplitude threshold, length in px.

    :return: scanpathcomparisons: array
        array of 5 scanpath similarity measures
    :return: durations: array-like
        durations of extracted scanpaths. Vector (Shape), Direction
        (Angle), Length, Position, and Duration. 1 = absolute
        similarity, 0 = lowest similarity possible.
    :return: onsets: array-like
        onset times of the scanpaths
    """
    # determine whether short shots should be grouped together
    if ldur != 0:
        group_shots = True
    else:
        group_shots = False

    scanpathcomparisons = []
    # transform pursuits into fixations
    newdata1 = pursuits_to_fixations(data1)
    newdata2 = pursuits_to_fixations(data2)
    print("Loaded data.")
    # preprocess input files
    fixations1 = preprocess_remodnav(newdata1, screensize)
    fixations2 = preprocess_remodnav(newdata2, screensize)
    shots = longshot(shots, group_shots, ldur)
    # get shots and scanpath on- and offsets
    if offset:
        onset = create_offsets(shots, dur)
        startid1, endid1 = create_offsetchunks(onset, fixations1, dur)
        startid2, endid2 = create_offsetchunks(onset, fixations2, dur)
    else:
        onset = create_onsets(shots, dur)
        startid1, endid1 = create_chunks(onset, fixations1, dur)
        startid2, endid2 = create_chunks(onset, fixations2, dur)
    fixation_vectors1 = fixations_chunks(fixations1, startid1, endid1)
    fixation_vectors2 = fixations_chunks(fixations2, startid2, endid2)
    print("Split fixation data into {} scanpaths.".format(len(startid1)))
    # save onset and duration times, if valid ones can be calculated
    onset_times = []
    exact_durations = []
    for i in range(0, len(startid1)):
        onset_time = fixations1[startid1[i]]["onset"]
        onset_times.append(onset_time)
        exact_duration = (
            fixations1[endid1[i]]["onset"] - fixations1[startid1[i]]["onset"]
        )
        # capture negative durations for invalid scanpaths
        if exact_duration > 0:
            exact_durations.append(exact_duration)
        else:
            exact_durations.append(np.nan)
        if i == len(startid1):
            print("Captured onsets and duration" " times of all scanpath pairs.")
    # loop over all fixation vectors/scanpaths and calculate similarity
    for i in range(0, len(onset)):
        # check if fixation vectors/scanpaths are long enough
        if (len(fixation_vectors1[i]) >= 3) & (len(fixation_vectors2[i]) >= 3):
            subj1 = mp.gen_scanpath_structure(fixation_vectors1[i])
            subj2 = mp.gen_scanpath_structure(fixation_vectors2[i])
            if grouping:
                subj1 = mp.simplify_scanpath(subj1, TAmp, TDir, TDur)
                subj2 = mp.simplify_scanpath(subj2, TAmp, TDir, TDur)
            M = mp.cal_vectordifferences(subj1, subj2)
            scanpath_dim = np.shape(M)
            M_assignment = np.arange(scanpath_dim[0] * scanpath_dim[1]).reshape(
                scanpath_dim[0], scanpath_dim[1]
            )
            numVert, rows, cols, weight = mp.createdirectedgraph(
                scanpath_dim, M, M_assignment
            )
            path, dist = mp.dijkstra(
                numVert, rows, cols, weight, 0, scanpath_dim[0] * scanpath_dim[1] - 1
            )
            unnormalised = mp.getunnormalised(subj1, subj2, path, M_assignment)
            normal = mp.normaliseresults(unnormalised, screensize)
            scanpathcomparisons.append(normal)
        # return nan as result if at least one scanpath it too short
        else:
            scanpathcomparisons.append(np.repeat(np.nan, 5))
            print(
                "Scanpath {} had a length of {}, however, a minimal "
                "length of 3 is required. Appending nan.".format(
                    i, min(len(fixation_vectors1), len(fixation_vectors2))
                )
            )
    return scanpathcomparisons, onset_times, exact_durations

max_stars_repo_name: AdinaWagner/MultiMatch

max_forks_repo_name: AdinaWagner/MultiMatch


text:
import numpy as np

from fragmenter import adjacency
from fragmenter import clusterings
from fragmenter import colormaps

from nibabel import freesurfer

# define clustering options
METHODS = ['gmm', 'k_means', 'spectral', 'ward']

class Fragment(object):
    """
    Class to fragment the cortical surface into equal sized parcels.

    Parameters:
    - - - - -
    n_clusters : int
        number of parcels to generate
    use_pretty_colors : bool
        use gradient color scheme for viewing map
    """

    def __init__(self, n_clusters, use_pretty_colors=True):

        self.n_clusters = n_clusters
        self.use_pretty_colors = use_pretty_colors

    def fit(
            self, vertices, faces, parcels=None, rois=None, size=False,
            method='k_means'):

        """
        Main surface fragmentation wrapper.

        Parameters:
        - - - - -
        vertices : array
            vertex coordinates
        faces : array
            list of faces
        parcels : dictionary
            mapping between region names and region indices
        rois : list of strings
            specific regions to fragment.  If None, fragment all regions.
        size : int
            desired size of generated framents.  If specified, overrides
            n_clusters.
        method : string
            algorithm to use for generating parcels
        """

        # make sure method exists in allowed algorithms
        assert method in METHODS
        assert isinstance(size, int)

        # define function dictionary
        clust_funcs = {
            'gmm': clusterings.gmm,
            'k_means': clusterings.k_means,
            'spectral': clusterings.spectral_clustering,
            'ward': clusterings.ward}

        self.vertices = vertices
        n_clusters = self.n_clusters

        # if provided method is spectral,
        # generate adjacency matrix
        if method == 'spectral':
            surf_adj = adjacency.SurfaceAdjacency(vertices, faces)
            surf_adj.generate()

        # if parcels and rois are None, just parcellate the whole cortex
        if not parcels or not rois:
            # if method is spectral, convert whole adjacency list to
            # adjacency matrix
            if method == 'spectral':
                samples = surf_adj.filtration(
                    filter_indices=None, toArray=True)
            else:
                samples = vertices

            if size:
                n_clusters = np.int32(np.floor(
                    samples.shape[0]/size))

            label = clust_funcs[method](n_clusters, samples)

        # otherwise, if parcels AND rois are provided
        # fragment on a region-by-region basis
        else:
            label = np.zeros((vertices.shape[0]))

            # loop over regions
            lmax = 0
            for region in rois:
                print(region)
                # make sure the region has vertices
                if np.any(parcels[region]):
                    # get region indices
                    parcel_idx = parcels[region]

                    # if method == spectral, regional adjacency matrix
                    if method == 'spectral':
                        parcel_samples = surf_adj.filtration(
                            filter_indices=parcel_idx, toArray=True)
                    # otherwise, extract region-specific
                    # vertex coordaintes
                    else:
                        parcel_samples = vertices[parcel_idx, :]

                    # make sure that the desired number of clusters does not
                    # exceed the number of samples to cluster
                    if size:
                        n_clusters = np.int32(np.ceil(
                            parcel_samples.shape[0]/size))

                    if n_clusters > parcel_samples.shape[0]:
                        n_clusters = 1

                    # apply clustering
                    clusters = clust_funcs[method](
                        n_clusters, parcel_samples)

                    # ensure that cluster ID is += by current cluster count
                    clusters += lmax

                    lmax += len(np.unique(clusters))
                    label[parcel_idx] = clusters

        self.label_ = np.int32(label)

    def write(self, output_name, to_file=False):
        """
        Write the fragmented label file to FreeSurfer annotation file.

        Parameters:
        - - - - -
        output_name: string
            name of save file. Must contain desired file extension (e.g. '.annot', '.csv')
        to_file: bool
            indicate whether to create a txt or csv file instead
        """

        # If labels are to be exported to csv or txt
        if to_file:
            if output_name.endswith(('.txt','.csv')):
                np.savetxt(output_name, self.label_, fmt='%5.0f')
            else:
                print('Warning: neither csv or txt extensions provided...')
        # Otherwise write an annotation file with freesurfer
        else:
            if output_name.endswith('.annot'):
                [keys,ctab,names,remapped] = colormaps.get_ctab_and_names(
                    self.vertices, self.label_, use_pretty_colors=self.use_pretty_colors)
                freesurfer.io.write_annot(output_name, remapped, ctab, names)
            else:
                print('Warning: .annot file extension not provided...')


text:
#-*- coding: utf8
from __future__ import print_function, division

from pyksc import ksc

import myio
import numpy as np

def cluster(T, num_clust=5):
    '''
    Runs the KSC algorithm on time series matrix T.

    Parameters
    ----------
    T : ndarray of shape (row, time series length)
        The time series to cluster
    num_clust : int
        Number of clusters to create
    '''
    T = np.asarray(T + 1e-20, order='C').copy()
    cents, assign, _, _ = ksc.inc_ksc(T, num_clust)
    return cents, assign

if __name__ == '__main__':
    T_train_visits = myio.read_48h_timeseries('visits').values[:, :12]

    for num_clust in [10, 30, 50, 70, 90, 110]:
        cents_visits, assign_visits = cluster(T_train_visits, num_clust)
        np.savetxt('cents_visits_%d.dat' % num_clust, cents_visits)
        np.savetxt('assign_visits_%d.dat' % num_clust, assign_visits)


text:
"""
FIR approximation of Hilbert Transform

MIT license
Copyright (c) 2021 Davi C. M. de Almeida

@author: Davi Carvalho Moreno de Almeida (davicmorenoa@gmail.com)
Tue Jun 29 20:52:55 2021

Versions of the packages used:
    numpy : 1.17.0
    scipy : 1.2.1
"""

import numpy as np
from scipy.signal.windows import get_window

def hilb(L, window='boxcar'):
# Compute the coefficients of length L FIR aproximation of Hilbert Transform
#
# Parameters:
#        L : int
#            Length of filter (odd number).
#   window : string, float, or tuple
#            Type of window used (see 'window' parameter of
#            scipy.signal.windows.get_window for details); rectangular window
#            ('boxcar') is the default window.
# Return:
#        h : numpy.ndarray
#            1D array with filter coefficients
  assert L % 2, "L should be integer and odd"
  window = get_window(window, L, fftbins=False)
  M = L-1
  alpha = M / 2
  n = np.arange(-alpha, 0, 1)
  h =  2/(n*np.pi)*np.sin(n*np.pi/2)**2
  return np.concatenate((h, [0], -h[::-1])) * window


text:
####
import numpy as np

from typing import  List

def get_array_shape(a)->int:
    """return the sum of the shape of an array

    Args:
        a (ndarray): input array
    Returns:
        tuple: shape of the array

    >>> get_array_shape(np.random.randn(2,3,4))
    9
    """
    return sum(a.shape)

def is_it_number(in_value):
    """Checks if a value is a valid number.

    Parameters
    ----------
    in_value
        A variable of any type that we want to check is a number.

    Returns
    -------
    bool
        True/False depending on whether it was a number.

    Examples
    --------
    >>> is_it_number(1)
    True
    >>> is_it_number(1.0)
    True
    >>> is_it_number("1")
    True
    >>> is_it_number("1.0")
    True
    >>> is_it_number("Hello")
    False

    You can also pass more complex objects, these will all be `False`.

    >>> is_it_number({"hello": "world"})
    False
    >>> from datetime import datetime
    >>> is_it_number(datetime.now())
    False

    Even something which contains all numbers will be ``False``, because it is not itself a number.

    >>> is_it_number([1, 2, 3, 4])
    False

    """
    try:
        float(in_value)
        return True
    except (ValueError, TypeError):
        return False

def add_numbers(a, b):
    """Sums the given numbers.

    :param int a: The first number.
    :param int b: The second number.

    :return: The sum of the given numbers.

    >>> add_numbers(1, 2)
    3
    >>> add_numbers(50, -8)
    42
    """
    return a + b

def is_float(in_value):
    """Checks if a value is a valid float.

    Parameters
    ----------
    in_value
        A variable of any type that we want to check is a float.

    Returns
    -------
    bool
        True/False depending on whether it was a float.

    Examples
    --------
    >>> is_float(1.5)
    True
    >>> is_float(1)
    False

    """

    try:
        return not float(in_value).is_integer()
    except (ValueError, TypeError):
        return False

max_stars_repo_name: dolun/toy-package

max_issues_repo_name: dolun/toy-package

max_forks_repo_name: dolun/toy-package


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

# Copyright 2018 IBM.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================
"""
Quantum Fourier Transform Circuit.
"""

import numpy as np

from qiskit import QuantumRegister, QuantumCircuit

from qiskit.aqua import AquaError

class FourierTransformCircuits:

    def __init__(self, num_qubits, approximation_degree=0, inverse=False):
        """Constructor.

        Args:
            num_qubits (int): number of qubits for the fourier transform circuit
            approximation_degree (int): degree of approximation for the desired circuit
            inverse (bool): Boolean flag to indicate Inverse Quantum Fourier Transform
        """
        self._num_qubits = num_qubits
        self._approximation_degree = approximation_degree
        self._inverse = inverse

    @staticmethod
    def _set_up(circ, qubits, num_qubits):
        if circ:
            if not qubits:
                raise AquaError(
                    'A QuantumRegister or a list of qubits need to be specified with the input QuantumCircuit.'
                )
        else:
            circ = QuantumCircuit()
            if not qubits:
                qubits = QuantumRegister(num_qubits, name='q')

        if len(qubits) < num_qubits:
            raise AquaError('Insufficient input qubits: {} provided but {} needed.'.format(
                len(qubits), num_qubits
            ))

        if isinstance(qubits, QuantumRegister):
            _ = qubits
        elif isinstance(qubits, list) and isinstance(qubits[0], tuple) and isinstance(qubits[0][0], QuantumRegister):
            _ = qubits[0][0]
        else:
            raise AquaError('Unrecognized input. Register or qubits expected.')
        if not circ.has_register(_):
            circ.add_register(_)
        return circ, qubits

    def construct_circuit(self, qubits=None, circuit=None):
        """
        Construct the circuit representing the desired state vector.

        Args:
            qubits (QuantumRegister | list of qubits): The optional qubits to construct the circuit with.
            circuit (QuantumCircuit): The optional circuit to extend from.

        Returns:
            QuantumCircuit.
        """
        circuit, qubits = FourierTransformCircuits._set_up(circuit, qubits, self._num_qubits)

        qubit_range = range(self._num_qubits)
        if self._inverse:
            qubit_range = reversed(qubit_range)
        for j in qubit_range:
            neighbor_range = range(np.max([0, j - self._num_qubits + self._approximation_degree + 1]), j)
            if self._inverse:
                neighbor_range = reversed(neighbor_range)
                circuit.u2(0, np.pi, qubits[j])
            for k in neighbor_range:
                lam = 1.0 * np.pi / float(2 ** (j - k))
                if self._inverse:
                    lam *= -1
                circuit.u1(lam / 2, qubits[j])
                circuit.cx(qubits[j], qubits[k])
                circuit.u1(-lam / 2, qubits[k])
                circuit.cx(qubits[j], qubits[k])
                circuit.u1(lam / 2, qubits[k])
            if not self._inverse:
                circuit.u2(0, np.pi, qubits[j])
        return circuit

max_stars_repo_name: dominik-steenken/qiskit-aqua

max_issues_repo_name: dominik-steenken/qiskit-aqua

max_forks_repo_name: dominik-steenken/qiskit-aqua