prompt
stringlengths
19
1.03M
completion
stringlengths
4
2.12k
api
stringlengths
8
90
# coding: utf-8 # # Structural durability analyses for carbon/epoxy laminates # # ## §3: Experimental # In[39]: #Preamble to hide inputs so that massive code scripts are not cluttering the data visualization output from IPython.display import HTML HTML('''<script> code_show=true; function code_toggle() { if (code_show){ $('div.input').hide(); } else { $('div.input').show(); } code_show = !code_show } $( document ).ready(code_toggle); </script> <form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''') # # DLN Contents # # # DLN Contents # # 0. [Materials Characterization Laboratory DLN | A Showcase for Convergent Manufacturing Group Ltd](DLN_0_About_Me.ipynb) - An 'Welcome' message to the Convergent Manufacturing - Materials Characterization Group, explaining the concept of these DLN entries, why I made them out of interest for the team's *Characterization Lab Technician/Scientist* opening, and presenting a brief 'About Me' StoryMap # <br> # # 1. [§1: Structural durability analyses of carbon fibre & epoxy-based composites - Introduction](DLN_1_Introduction.ipynb) - An introduction to the quasi-fatigue experiments performed on carbon fibre/epoxy composite specimens. # <br> # # 2. [§2: Structural durability analyses of carbon fibre & epoxy-based composites - Laminate mechanics theory](DLN_2_Theory.ipynb) - A discussion of composite laminate theory, as a basis for performing stress-strain-deformation calculations to characterize the structural durability of composite laminate layups. # <br> # # 3. [§3: Structural durability analyses of carbon fibre & epoxy-based composites - Experimental results](DLN_3_Experimental.ipynb) - Using Python scientific programming libraries to explore and visualize quasi-fatigue tensile & compressive loading experiments on carbon fibre/epoxy composite test coupons. # <br> # # 4. [§4: Structural durability analyses of carbon fibre & epoxy-based composites - Matrix calculations](DLN_4_Calculations.ipynb) - Using MATLAB to perform structural durability matrix calculations from carbon fibre/epoxy composite test coupon experimental data. # ## I. Experiment log # * **Date of experiment**: 10.14.2017 # * **Principle investigator**: <NAME>, EIT, BASc # * **Test operators**: <NAME>, <NAME>, <NAME> # * **Lead investigator**: Prof. <NAME> # * **Course**: LVA Nr. 378.029 - 480ADPTPPBV17 - Polymer Product Design and Engineering III - Graduate Seminar # * **Location**: Institute of Polymer Materials and Testing (IPMT), JKU Linz - Linz, Austria # * Compounding and specimen preparation Lab: *Composite coupon specimen preparation* # * Mechanical Lab: *Tensile & compression testing* # # ### i. Experiment test (lab) environment conditions # *Measurement taken: 10-14-2017 08:45:07* # <b> # # $T_{test} (°C) = 21.23$ (*within* $ 23 ± 3 °C$ *standard laboratory atmosphere range as per ASTM D5229*) # <br> # $RH (\%) = 55.7$ (*within* $ 50 ± 10 \%$ *standard laboratory atmosphere range as per ASTM D5229*) # ## 3.1 Composite specimens to be tested for structural durability analyses # ### 3.1.1 Properties applicable to all test coupons # * **Composite type**: CFRP - carbon/epoxy laminates # * Carbon fibre ply: # * Unidirectional 0°: # * Unidirectional 90°: # * Unidirectional 45°: # * Unidirectional ±45°: [HiMax™ FCIM151](https://www.hexcel.com/user_area/content_media/raw/FCIM151.pdf) # * Epoxy resin system: [HexPly® M35-4](https://www.hexcel.com/user_area/content_media/raw/HexPly_M354_DataSheet.pdf) # * **Void fraction ($v_{f}$, %)**: 55 # * **Test speed (mm/min)**: 1 # * **No. of samples per test**: 3 # # ### 3.1.2 Properties applicable to all specimens # The following table details the specimens to be tested for the investigation: # # **<center>Table 1. Set of carbon fibre/epoxy laminate coupons for quasi-static fatigue testing</center>** # # | Coupon tag | Direction | Orientation [°] | Loading | No. of Layers | Avg. Coupon Width [mm] | Avg. Coupon Thickness [mm] | # |:----------:|:---------:|:---------------:|:-----------:|:-------------:|:----------------------:|:--------------------------:| # | UD_0_4_T | UD | 0 | Tension | 4 | 9.98 | 1.02 | # | UD_90_8_T | UD | 90 | Tension | 8 | 20.02 | 1.98 | # | BD_±45_8_T | BD | ±45 | Tension | 8 | 20.1 | 1.95 | # | UD_45_8_T | UD | 45 | Tension | 8 | 20.06 | 2.01 | # | UD_0_4_C | UD | 0 | Compression | 4 | 9.98 | 1.01 | # | UD_90_8_C | UD | 90 | Compression | 8 | 19.98 | 2.02 | # # As a reference, ply (laminate) layers were layed-up according to the following convention: # # ![Image](0.Images/UD_laminate_orientation_ref.jpg) # # *<center> Fig. Y - UD lamina lay-up orientations (JKU Linz - IPPE, 2017)</center>* # # # ### 3.1.3 References # [1] *<NAME>., <NAME>., <NAME>., <NAME>., & <NAME>. (2014). Modelling and validation of fatigue life calculation method for short fiber reinforced injection molded parts. In 16th European conference of composite materials.* [Link](http://www.escm.eu.org/eccm16/assets/0329.pdf) # # [2] *<NAME>. (1999). Mechanics of composite materials. 2nd Ed. CRC press.* # ## 3.2 Carbon fibre/epoxy test coupon fabrication # 1. Carbon/epoxy specimens with 55% fibre volume fraction were produced with the following materials: # # * **Epoxy resin and system**: HexPly® M35-4 # * **Carbon fibres**: HiMax™ FCIM151 # * **Other**: Epoxy-compatible binder was used to make handling the layup of carbon fibre sheets easier and to prevent distortion during the layup manufacturing # <br> # # # 2. The specimen laminates were produced according to the cure cycle protocol indicated in the HexPly® M35-4 technical specification, with: # * the cure temperature set at $100 \pm 2.5°C$ # * cure processing time set at 4.5 hours # * heat-up and cool-down rates set at 1°C/minute, vacuum applied at -0.85 bar # * autoclave pressure set to 7.5 bar-g. # <br> # # 3. Unidirectional (UD) carbon/epoxy specimens were milled from plates with diamond blades at angles of 0°, 45°/±45° and 90° # <br> # # 4. Specimen geometries for mechanical tests with carbon/epoxy specimens were chosen according to the following specifications: # * Rectangular specimens (especially for UD) # * Tabs for load introduction # * Tab material had to possess a lower stiffness than tested materials; testing CF/epoxy composite coupons - aluminum tabs were used for fabrication # <br> # # The following figure shows the dimensions used to prepare the composite coupons for testing: # # ![Image](0.Images/CF-Epoxy_Coupon_Dim.jpg) # # *<center> Fig. 3.1 - Test coupon geometries for UD quasi-isotropic and 0° (JKU Linz - IPPE, 2017)</center>* # # 4-ply UD specimens had the following geometry: # * $200 \pm 0.1 mm \quad\quad x \quad\quad 10 \pm 0.025 mm \quad\quad x \quad\quad 1 \pm 0.025 mm$ for UD 0° specimens # # 8-ply UD specimens had the following geometry: # * $200 \pm 0.1 mm \quad\quad x \quad\quad 20 \pm 0.1 mm \quad\quad x \quad\quad 2 \pm 0.025 mm$ for 8-ply/off-axis specimens # # * Aluminium tabs with 1.5 mm thickness were adhered on both sides of all carbon/epoxy specimens( For tensile loads, usually 1 mm thickness is chosen for specimens tested in fibre direction) # ## 3.3 Experimental equipment and data reduction process # ### 3.3.1 Data analysis # For data evaluation, all moduli and strengths were calculated with the real cross-sections of the respective tested specimens. Moduli were evaluated between 0.001 and 0.003 absolute strain, as per: # * ASTM D3039/D3039M: Standard test method for tensile properties of polymer matrix composite materials # # * ASTM D3410 / D3410M: Standard Test Method for Compressive Properties of Polymer Matrix Composite Materials # # * ASTM_E111-04: Standard Test Method for Young’s Modulus, Tangent Modulus, and Chord Modulus # # ### 3.3.2 Equipment # * All mechanical tests were performed on a Zwick-Roell HA 100 kN servo-hydraulic fatigue test machine designed for loads up to 100 kN at room temperature # # ### 3.3.3 Quasi-static tension and compression tests # * In quasi-static tension and compression tests, specimens were loaded in a displacement controlled way with a test speed of 1 mm/min # * End-tabs were clamped completely between the Zwick-Roell HA system grips # * Strains in longitudinal direction were recorded by means of a proprietary digital sensor setup (JKU Linz IPMT) # * The experiment runs were designed to investigate the in-plane tensile and compressive properties of polymer matrix composite materials reinforced by high-modulus fibers (in this case, carbon fibre/epoxy laminate composites). The applicability of the ASTM test method are limited to continuous fiber or discontinuous fiber-reinforced composite material forms, in which the laminate is balanced and/or symmetric with respect to the test direction # ## 3.4 Experimental data analyses - Python Preamble # ### 3.4.1 Premable for python object-oriented programming # In[40]: ##===============================IMAGES============================================================ #Image import preamble import IPython from IPython.display import display, Image, SVG, Math, YouTubeVideo Image_PATH = "/Users/delroy_m/Desktop/(CMT) Materials Characterization ELN/0.Images/" # Use 'image drag & drop' IPython Notebook Extension #IPython.html.nbextensions.install_nbextension('https://raw.github.com/ipython-contrib/IPython-notebook-extensions/master/nbextensions/usability/dragdrop/main.js') #Load 'image drag & drop' extension #%javascript #IPython.load_extensions('usability/dragdrop/main'); #NOTE about 'image drag & drop' extension handling of images # The image will be uploaded to the server into the directory where your notebook resides. This means, the image is not copied into the notebook itself, it will only be linked to. ##===============================DATA ANALYSES===================================================== #import PANDAS - A library providing high-performance, easy-to-use data structures and data analysis tools import pandas as pd #print("Current Pandas version:", pd.__version__) # print("plotly version:", __version__) #import SciPy - A Python-based ecosystem of open-source software for mathematics, science, and engineering import scipy from scipy import * #Import Gaussian distribution STATS package to validate whether experimental data is randomly (normally) #distributed from scipy.stats import * #from scipy.stats import norm # if using a Jupyter notebook, include: #%matplotlib inline #import NumPy - A fundamental package for scientific computing with Python import numpy as np #import qgrid - Allows querying of DataFrames with intuitive scrolling, sorting, and filtering controls, #as well as editing features, for the DataFrames, by double clicking cells import qgrid ##===============================DATA VISUALIZATION================================================ #import matplotlib - A Python 2D plotting library #import matplotlib.pyplot as plt #import Pygal - A Python SVG Charts Creator import pygal #import Plotly for online or offline interactive plot rendering # #If using Plotly with online server: #import plotly.plotly as py # #If using Plotly offline and saving code/graphs/images locally: import plotly.graph_objs as go import plotly as py from plotly import __version__ #ensures that most up-to-date plotly pckg is being used from plotly.offline import init_notebook_mode, plot, download_plotlyjs, iplot import plotly.figure_factory as ff from plotly import tools #Improve Plotly figure render responsiveness import plotly.io as pio pio.renderers.default = 'iframe' # #import cufflinks as cf #import Seaborn - Statistical data visualization using Matplotlib import seaborn as sns #from matplotlylib import fig_to_plotly #import Plotly express - A terse, consistent, high-level wrapper around Plotly.py for rapid data exploration and figure generation #import plotly_express as px #Put plotly environment in 'offline mode' py.offline.init_notebook_mode(connected=True) #Reinitialize Jupyter Notebook mode init_notebook_mode() #For 'online' plotting: # Learn about API authentication here: https://plot.ly/pandas/getting-started # Find your api_key here: https://plot.ly/settings/api #Do I have the most up-to-date plotly package? #print("Current Plotly version:", __version__) ##===============================SYSTEM COMMANDS==================================================== import glob import sys import datetime import os ##===============================EXCEPTION HANDLING================================================= #Ignore dataframe slicing copying warnings --> these are annoying, and issue is acknowledged pd.options.mode.chained_assignment = None # default='warn' #Mute any annoying compiling warnings that arise when running code #import warnings #warnings.filterwarnings("ignore") # ### 3.4.2 Setup framework for parsing quasi-static fatigue experimental data into Python (Pandas) dataframes # In[41]: ##===============================Create dataframe from experiment data================================ #Coupon cyclic fatigue testing datasets - formatted according to "<NAME> - Tidy Data" #"<NAME> - Tidy Data" - http://vita.had.co.nz/papers/tidy-data.pdf #1. Each variable forms a column #2. Each observation forms a row #3. Each type of observational unit forms a table ##---------------------------------------------------------------------------------------------------- ##-Naming convention for experiment files-## # #[Fiber_direction]-[Orientation_degree]-[Tension/Compression]-[Fibre_type]-[Test_speed (mm/min)]-[Test_temp]... #-[Strain_in_load_direction]-[#_of_specimens_tested]-[specimen_avg_width (mm)]-[specimen_avg_thickness (mm)].xlsx #"Experiment data attribute tags ####---------------------------------------------------------------------------------------------------- # 1. Fiber_direction: # - Unidirectional (UD): 0°, 90° --> Provides longitudinal stiffness # - Bidirectional (BD): ±45° --> Provides torsional stiffness # * Attribute_type = [Alpha] # # 2. Orientation (°): 0°, ±45°, 90° # * Attribute_type = [Alphanumeric] # # 3. Tension/compression loading: # - T: Tension # - C: Compression # * Attribute_type = [Alpha] # # 8. Strain-in-load direction (x, y, x &/OR y): # - UD: ε,y # - BD: ε,y &/OR ε,x # * Attribute_type = [Alphanumeric] # # 9. No. of specimens tested (#): # * Attribute_type = [Numeric] # # 10. Specimens avg. width (mm): # * Attribute_type = [Numeric] # # 11. Specimens avg. thickness (mm): # * Attribute_type = [Numeric] # # #"Experiment data variables ####---------------------------------------------------------------------------------------------------- #Column 1: # - Tension or compression load [N] # ##Column 2: # - Strain [%] #Custom color palette for plotting ####---------------------------------------------------------------------------------------------------- #Column 1: dark_turquoise = '#00CED1' turquoise = '#40E0D0' medium_turquoise = '#48D1CC' pale_turquoise = '#AFEEEE' aqua_marine = '#7FFFD4' powder_blue = '#B0E0E6' cadet_blue = '#5F9EA0' steel_blue = '#4682B4' corn_flower_blue = '#6495ED' deep_sky_blue = '#00BFFF' dodger_blue = '#1E90FF' light_blue = '#ADD8E6' sky_blue = '#87CEEB' light_sky_blue = '#87CEFA' midnight_blue = '#191970' navy = '#000080' dark_blue = '#00008B' medium_blue = '#0000CD' blue = '#0000FF' royal_blue = '#4169E1' # ### 3.4.3 Parse quasi-static fatigue experimental data into data frame # In[42]: #Upload all 'cleaned' experimental data sets for composite coupon fatigue testing ##===============================DEFINE DATA DIRECTORY============================================= #Data import from local server #Used so that data files & code are not mixed together + makes it easy to change working #directory to where data is stored #Set desired directory path here desired_dir = r"/Users/delroy_m/Desktop/(CMT) Materials Characterization ELN/2. Cleaned data/Quasi_static_data" work_dirPath = os.chdir(desired_dir) #Set the current directory to the desired working directory path verify_cwd_path = os.getcwd() print("CWD: " + verify_cwd_path) ##===============================Import cleaned experiment data====================================== qsf_expt_data = glob.glob('*.xlsx') # Get all files from all subfolders. qsf_expt_data #Define DataFrame to store quasi-static fatigue .xlsx experiment files qsf_df = pd.DataFrame() #Enter test (lab) environment measurements for completeness of data parsing T_test = 21.23 RH_test = 55.7 #Pandas 'read_excel' syntax #pandas.read_excel(io, sheet_name=0, header=0, names=None, index_col=None, parse_cols=None, # true_values=None, false_values=None, skiprows=None, nrows=None, na_values=None, # keep_default_na=True, verbose=False, parse_dates=False, date_parser=None, # thousands=None, comment=None, skip_footer=0, skipfooter=0, convert_float=True, # mangle_dupe_cols=True, **kwds) #loop to establish columns for DataFrame for i, P in enumerate(qsf_expt_data): #i: counter, P: place holder #print(P) eqsf_df = pd.read_excel(P, header=None) #read .xlsx experiment data # if i == 0: try: eqsf_df.columns = ['Force load [N]','ε,y [%]','ε,x [%]', 'σ,qs [MPa]'] except: #print('Data in old format!') eqsf_df.columns = ['Static load [N]','ε,y [%]','ε,x [%]', 'σ,qsf [MPa]'] file_info = P.split("_") # Extract info from filename eqsf_df['Coupon tag'] = file_info[len(file_info)-5] + "_" + file_info[len(file_info)-4] + "_" + file_info[len(file_info)-3] + "_" + file_info[len(file_info)-2] eqsf_df['Fibre direction'] = file_info[0] #sample_info = file_info[len(file_info)-1].split("_") eqsf_df['Orientation (°)'] = file_info[1] eqsf_df['# of plys'] = file_info[2] eqsf_df['Loading'] = file_info[3] if file_info[3] == "T": eqsf_df['Loading'] = "Tension" else: eqsf_df['Loading'] = "Compression" qsf_df =
pd.concat([eqsf_df, qsf_df])
pandas.concat
import pandas as pd import json import requests import os from flask import Flask, request, Response # constants TOKEN = '<KEY>' # info about bot # #https://api.telegram.org/bot1643763356:AAHDHaS1qGa34XkcOgYWta5cpUY-kzSK7y4/getMe # # get updates # #https://api.telegram.org/bot1643763356:AAHDHaS1qGa34XkcOgYWta5cpUY-kzSK7y4/getUpdates # # webhook # #https://api.telegram.org/bot1643763356:AAHDHaS1qGa34XkcOgYWta5cpUY-kzSK7y4/setWebhook?url=https://98b06c9ed3c1fa.localhost.run # # webhook heroku # #https://api.telegram.org/bot1643763356:AAHDHaS1qGa34XkcOgYWta5cpUY-kzSK7y4/setWebhook?url=https://gh-rossmann-bot.herokuapp.com/ # # send message # #https://api.telegram.org/bot1643763356:AAHDHaS1qGa34XkcOgYWta5cpUY-kzSK7y4/sendMessage?chat_id=823327889&text=Hi Gabriel! def send_message(chat_id, text): url = "https://api.telegram.org/bot{}/".format(TOKEN) url = url + "sendMessage?chat_id={}".format(chat_id) r = requests.post(url, json = {"text": text}) print("Status Code {}".format(r.status_code)) return None def load_dataset(store_id): # loading test dataset df10 = pd.read_csv("test.csv") df_store_raw =
pd.read_csv("store.csv")
pandas.read_csv
from __future__ import print_function, division import torch from torch.nn import init import torch.nn as nn import torch.optim as optim import torch.nn.functional as F from torch.optim import lr_scheduler import numpy as np from torch.utils.data import DataLoader, Dataset import torchvision from torchvision import datasets, models, transforms import matplotlib.pyplot as plt import time import os import math import copy # from tensorboardX import SummaryWriter from utils import progress_bar, format_time import json from PIL import Image from efficientnet_pytorch import EfficientNet from ghost_net import ghost_net, ghost_net_Cifar from ghostnet import ghostnet from mnext import mnext from mobilenetv3 import MobileNetV3, mobilenetv3_s from mobilenetv3_2 import MobileNetV3_Small, MobileNetV3_Large from mobilenet import my_mobilenext, my_mobilenext_2 from mobilenetv3_torch import mobilenet_v3_large, mobilenet_v3_small import onnxruntime import cv2 import json import pandas as pd from mobilenetv2_cbam import MobileNetV2_cbam def softmax_np(x): x_row_max = x.max(axis=-1) x_row_max = x_row_max.reshape(list(x.shape)[:-1]+[1]) x = x - x_row_max x_exp = np.exp(x) x_exp_row_sum = x_exp.sum(axis=-1).reshape(list(x.shape)[:-1]+[1]) softmax = x_exp / x_exp_row_sum return softmax def softmax_flatten(x): x = x.flatten() x_row_max = x.max() x = x - x_row_max x_exp = np.exp(x) x_exp_row_sum = x_exp.sum() softmax = x_exp / x_exp_row_sum return softmax def efficientnet_test(): model = EfficientNet.from_pretrained('efficientnet-b0') # Preprocess image tfms = transforms.Compose([transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]), ]) img = tfms(Image.open('./data/imgs/elephant.jpg')).unsqueeze(0) print(img.shape) # torch.Size([1, 3, 224, 224]) # Load ImageNet class names labels_map = json.load(open('.\data\labels_map.txt')) labels_map = [labels_map[str(i)] for i in range(1000)] # Classify model.eval() with torch.no_grad(): outputs = model(img) # Print predictions print('-----') for idx in torch.topk(outputs, k=5).indices.squeeze(0).tolist(): prob = torch.softmax(outputs, dim=1)[0, idx].item() print('{label:<75} ({p:.2f}%)'.format(label=labels_map[idx], p=prob * 100)) # print('{label:<75} ({p:.2f}%)'.format(label=idx, p=prob * 100)) def imshow(inp, title=None): """Imshow for Tensor.""" # 先把tensor转为numpy,然后将通道维放到最后方便广播 inp = inp.numpy().transpose((1, 2, 0)) mean = np.array([0.485, 0.456, 0.406]) std = np.array([0.229, 0.224, 0.225]) inp = std * inp + mean inp = np.clip(inp, 0, 1) # 当inp为0-1之间的浮点数和0-255之间的整数都能显示成功 # inp = np.clip(inp, 0, 1)*255 # inp = inp.astype(np.int32) # print(inp) plt.imshow(inp) plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False if title is not None: plt.title(title) # plt.pause(0.001) # pause a bit so that plots are updated plt.show() def showimage(dataloader, class_names): # 获取一批训练数据 inputs, classes = next(iter(dataloader)) # 批量制作网格 Make a grid of images out = torchvision.utils.make_grid(inputs) imshow(out, title=[class_names[x] for x in classes]) # #一个通用的展示少量预测图片的函数 # def visualize_model(model, dataloader,device): # model.eval() # model.to(device) # with torch.no_grad(): # inputs, labels = next(iter(dataloader)) # inputs = inputs.to(device) # labels = labels.to(device) # outputs = model(inputs) # _, preds = torch.max(outputs, 1) # out = torchvision.utils.make_grid(inputs).cpu() # # title = "predect/label" # title = "" # for i,label in enumerate(labels): # # title+=" {}/{} ".format(preds[i],label) # title += " {} ".format(label_name[preds[i]]) # imshow(out, title=title) # def visualize_pred(): # # device = 'cuda' if torch.cuda.is_available() else 'cpu' # model = EfficientNet.from_name('efficientnet-b0',num_classes=10) # # 加载模型参数 # # path = r"checkpoint/B0/000/B0_acc=99.8528.pth" # path = r"checkpoint\B0\111\B0_acc=99.5540.pth" # checkpoint = torch.load(path) # model.load_state_dict(checkpoint["net"]) # print("loaded model with acc:{}".format(checkpoint["acc"])) # # data_path = r"E:\Datasets\state-farm-distracted-driver-detection\imgs\train" # # # data_path = r".\data\hymenoptera_data\train" # # train_dataset = datasets.ImageFolder(root=data_path, transform=data_transform) # # train_dataloader = DataLoader(dataset=train_dataset, # # batch_size=4, # # shuffle=True, # # num_workers=0) # val_dataset = MyDataset("./data/dval.txt", data_transform) # val_dataloader = DataLoader(dataset=val_dataset, # batch_size=4, # shuffle=True, # num_workers=0) # # visualize_model(model,train_dataloader,device) # visualize_model(model, val_dataloader, device) class MyDataset(Dataset): def __init__(self, names_file, transform=None): self.names_file = names_file self.transform = transform self.names_list = [] if not os.path.isfile(self.names_file): print(self.names_file + 'does not exist!') with open(self.names_file, "r", encoding="utf-8") as f: lists = f.readlines() for l in lists: self.names_list.append(l) def __len__(self): return len(self.names_list) def __getitem__(self, idx): image_path = self.names_list[idx].split(' ')[0] # print(image_path) if not os.path.isfile(image_path): print(image_path + 'does not exist!') return None image = Image.open(image_path).convert('RGB') # if self.transform: image = self.transform(image) label = int(self.names_list[idx].split(' ')[1]) sample = image, label return sample # # # train_transform = transforms.Compose([ # # transforms.RandomResizedCrop((224, 224), scale=(0.8, 1.0), ratio=(3. / 4., 4. / 3.), ), # # transforms.RandomResizedCrop((320, 320), scale=(0.8, 1.0), ratio=(3. / 4., 4. / 3.), ), # transforms.ColorJitter(brightness=0.1, contrast=0.1, saturation=0.1, hue=0.1), # # transforms.RandomRotation(10, resample=False, expand=False, center=None), # transforms.ToTensor(), # transforms.Normalize(mean=[0.485, 0.456, 0.406], # std=[0.229, 0.224, 0.225]) # ]) # # val_transform = transforms.Compose([ # transforms.Resize((224, 224)), # # transforms.Resize((320, 320)), # transforms.ToTensor(), # transforms.Normalize(mean=[0.485, 0.456, 0.406], # std=[0.229, 0.224, 0.225]) # ]) # train_transform = transforms.Compose([ # # transforms.Resize((224, 224)), # # transforms.ColorJitter(brightness=0.8, contrast=0.5, saturation=0.5, hue=0.1), # # transforms.RandomRotation(10, resample=False, expand=False, center=None), # # transforms.RandomCrop(224, padding=16), # transforms.ToTensor(), # transforms.Normalize(mean=[0.485, 0.456, 0.406], # std=[0.229, 0.224, 0.225]) # ]) # train_transform = transforms.Compose([ # # transforms.Resize((224, 224)), # transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.1), # # transforms.ColorJitter(brightness=0.8, contrast=0.5, saturation=0.5, hue=0.1), # # transforms.RandomRotation(20, resample=False, expand=False, center=None), # # transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1), # transforms.RandomRotation(10, resample=False, expand=False, center=None), # # transforms.RandomHorizontalFlip(p=0.5), # # # transforms.RandomVerticalFlip(p=0.5), # # # ToTensor()能够把灰度范围从0-255变换到0-1之间, # # # transform.Normalize()则把0-1变换到(-1,1).具体地说,对每个通道而言,Normalize执行以下操作: # # # image=(image-mean)/std # # transforms.RandomResizedCrop((224,224)), # # transforms.Resize((224, 224)), # transforms.RandomCrop(224, padding=16), # transforms.ToTensor(), # transforms.Normalize(mean=[0.485, 0.456, 0.406], # std=[0.229, 0.224, 0.225]) # ]) # train_transform = transforms.Compose([ transforms.Resize((240, 240)), transforms.RandomCrop(224), # transforms.Resize((224, 224)), transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.1), # transforms.RandomRotation(20, resample=False, expand=False, center=None), # transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1), transforms.RandomRotation(10, resample=False, expand=False, center=None), # transforms.RandomHorizontalFlip(p=0.5), # # transforms.RandomVerticalFlip(p=0.5), # # ToTensor()能够把灰度范围从0-255变换到0-1之间, # # transform.Normalize()则把0-1变换到(-1,1).具体地说,对每个通道而言,Normalize执行以下操作: # # image=(image-mean)/std # transforms.RandomResizedCrop((224,224)), # transforms.Resize((224, 224)), # transforms.RandomCrop(224, padding=16), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) # train_transform = transforms.Compose([ # transforms.Resize((224, 224)), # # transforms.RandomCrop(320), # transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.1), # transforms.RandomRotation(10, resample=False, expand=False, center=None), # transforms.ToTensor(), # transforms.Normalize(mean=[0.485, 0.456, 0.406], # std=[0.229, 0.224, 0.225]) # ]) val_transform = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) # train_transform = transforms.Compose([ # transforms.Resize((320, 320)), # # transforms.RandomCrop(320), # transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.1), # transforms.RandomRotation(10, resample=False, expand=False, center=None), # transforms.ToTensor(), # transforms.Normalize(mean=[0.485, 0.456, 0.406], # std=[0.229, 0.224, 0.225]) # ]) # # val_transform = transforms.Compose([ # transforms.Resize((320, 320)), # transforms.ToTensor(), # transforms.Normalize(mean=[0.485, 0.456, 0.406], # std=[0.229, 0.224, 0.225]) # ]) # train_transform = transforms.Compose([ # transforms.Resize((340, 340)), # transforms.RandomCrop(320), # transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.1), # transforms.RandomRotation(10, resample=False, expand=False, center=None), # transforms.ToTensor(), # transforms.Normalize(mean=[0.485, 0.456, 0.406], # std=[0.229, 0.224, 0.225]) # ]) # # val_transform = transforms.Compose([ # transforms.Resize((320, 320)), # transforms.ToTensor(), # transforms.Normalize(mean=[0.485, 0.456, 0.406], # std=[0.229, 0.224, 0.225]) # ]) # train_transform = transforms.Compose([ # transforms.Resize((160, 160)), # # transforms.RandomCrop(320), # transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.1), # transforms.RandomRotation(10, resample=False, expand=False, center=None), # transforms.ToTensor(), # transforms.Normalize(mean=[0.485, 0.456, 0.406], # std=[0.229, 0.224, 0.225]) # ]) # # val_transform = transforms.Compose([ # transforms.Resize((160, 160)), # transforms.ToTensor(), # transforms.Normalize(mean=[0.485, 0.456, 0.406], # std=[0.229, 0.224, 0.225]) # ]) # train_transform = transforms.Compose([ # # transforms.Resize((160, 160)), # transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.1), # # transforms.RandomRotation(20, resample=False, expand=False, center=None), # # transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1), # transforms.RandomRotation(10, resample=False, expand=False, center=None), # # transforms.RandomHorizontalFlip(p=0.5), # # # transforms.RandomVerticalFlip(p=0.5), # # # ToTensor()能够把灰度范围从0-255变换到0-1之间, # # # transform.Normalize()则把0-1变换到(-1,1).具体地说,对每个通道而言,Normalize执行以下操作: # # # image=(image-mean)/std # # # transforms.RandomResizedCrop((224,224)), # # # transforms.Resize((224, 224)), # transforms.RandomCrop(160, padding=16), # transforms.ToTensor(), # transforms.Normalize(mean=[0.485, 0.456, 0.406], # std=[0.229, 0.224, 0.225]) # ]) device = 'cuda' if torch.cuda.is_available() else 'cpu' # device = 'cpu' best_acc = 0 # best test accuracy best_val_acc = 0 start_epoch = 0 # start from epoch 0 or last checkpoint epoch # kaggle dataset # num_classes = 10 # label_name = ["正常","右持手机","右接电话","左持手机","左接电话","操作仪器","喝水","向后侧身","整理仪容","侧视"] # label_name = ["normal", "texting-R", "answering_R", "texting-L", "answering_L", "operating", "drinking", "leaning_back", "makeup", "side_view"] # # 新的类别 # # # label_name = ["正常","右接电话",左接电话","低头","操作仪器","喝水","吸烟","向后侧身","整理仪容","侧视"] # # label_name =["normal", "right to answer the phone", left to answer the phone "," head down "," operating instruments "," drinking water "," smoking "," leaning back ","makeup "," side view "] # # mydataset # classes_path = r"data/drive_classes.txt" # with open(classes_path) as f: # label_name = [c.strip() for c in f.readlines()] # num_classes = len(label_name) # num_classes = 100 # num_classes = 10 num_classes = 9 # num_classes = 8 # num_classes = 7 # num_classes = 6 # net = EfficientNet.from_pretrained('efficientnet-b0',num_classes=num_classes) # net = models.resnet18(pretrained=True) # net = models.resnext50_32x4d(pretrained=True) # net = models.resnext50_32x4d(pretrained=False,num_classes=num_classes) # net = models.resnet50(pretrained=False,num_classes=num_classes) # net = models.mobilenet_v2(pretrained=True) # net = models.mobilenet_v2(pretrained=False, num_classes=num_classes, width_mult=1.0) # net = models.mobilenet_v2(pretrained=False, num_classes=num_classes, width_mult=0.5) # net = models.mobilenet_v2(pretrained=False, num_classes=num_classes, width_mult=0.3) # net = models.mobilenet_v2(pretrained=False, num_classes=num_classes, width_mult=0.1) # net = models.mobilenet_v2(pretrained=False, width_mult=1.0) net = models.mobilenet_v2(pretrained=True, width_mult=1.0) num_in = net.classifier[1].in_features net.classifier[1] = nn.Linear(num_in, num_classes) # net = mobilenet_v3_small(pretrained=True) # # net = mobilenet_v3_large(pretrained=True) # num_in = net.classifier[3].in_features # net.classifier[3] = nn.Linear(num_in, num_classes) # # net = models.resnext50_32x4d(pretrained=True) # num_in = net.fc.in_features # net.fc = nn.Linear(num_in, num_classes) # # 加载模型权重,忽略不同 # model_path = r"checkpoint/data_12_23/mobilenetv2/000/mobilenetv2_1_12_23_acc=92.1389.pth" # model_dict =net.state_dict() # checkpoint = torch.load(model_path, map_location=device) # pretrained_dict = checkpoint["net"] # # pretrained_dict = checkpoint # pretrained_dict = {k: v for k, v in pretrained_dict.items() if np.shape(model_dict[k]) == np.shape(v)} # model_dict.update(pretrained_dict) # net.load_state_dict(model_dict) # net = MobileNetV2_cbam(num_classes=num_classes, width_mult=1.0, add_location=16) # # # 更新权重 add_location=16 # model_path = r"weights/mobilenet_v2-b0353104.pth" # model_dict =net.state_dict() # checkpoint = torch.load(model_path, map_location=device) # pretrained_dict = checkpoint # new_key = list(model_dict.keys()) # pre_key = list(pretrained_dict.keys()) # ignore_num = 3 # start_index = new_key.index('features.2.fc1.weight') # print(new_key[start_index+3], pre_key[start_index]) # for i in range(len(pre_key)): # if i<start_index: # j = i # else: # j = i+3 # if np.shape(model_dict[new_key[j]]) == np.shape(pretrained_dict[pre_key[i]]): # model_dict[new_key[j]] = pretrained_dict[pre_key[i]] # net.load_state_dict(model_dict) # net = MobileNetV2_cbam(num_classes=num_classes, width_mult=1.0, add_location=64) # # # 更新权重 add_location=64 # model_path = r"weights/mobilenet_v2-b0353104.pth" # model_dict =net.state_dict() # checkpoint = torch.load(model_path, map_location=device) # pretrained_dict = checkpoint # new_key = list(model_dict.keys()) # pre_key = list(pretrained_dict.keys()) # ignore_num = 3 # start_index = new_key.index('features.11.fc1.weight') # print(new_key[start_index+3], pre_key[start_index]) # for i in range(len(pre_key)): # if i<start_index: # j = i # else: # j = i+3 # if np.shape(model_dict[new_key[j]]) == np.shape(pretrained_dict[pre_key[i]]): # model_dict[new_key[j]] = pretrained_dict[pre_key[i]] # net.load_state_dict(model_dict) # net = models.shufflenet_v2_x1_0(pretrained=True) # # net = models.shufflenet_v2_x0_5(pretrained=True) # # net = models.resnet50(pretrained=True)w # num_in = net.fc.in_features # net.fc = nn.Linear(num_in, num_classes) # net = ghost_net_Cifar(num_classes=num_classes, width_mult=0.1) # net = ghost_net(num_classes=num_classes, width_mult=1.) # net = ghost_net(num_classes=num_classes, width_mult=0.5) # net = ghost_net(num_classes=num_classes, width_mult=0.3) # net = ghost_net(num_classes=num_classes, width_mult=0.1) # net = ghostnet(num_classes=num_classes, width=1.) # net = ghostnet(num_classes=num_classes, width=0.5) # net = ghostnet(num_classes=num_classes, width=0.3) # net = ghostnet(num_classes=num_classes, width=0.1) # net = mnext(num_classes=num_classes, width_mult=1.) # net = mnext(num_classes=num_classes, width_mult=0.5) # net = my_mobilenext_2(num_classes=num_classes, width_mult=1.) # num_in = net.fc.in_features # # 创建的层默认参数需要训练 # net.fc = nn.Linear(num_in, num_classes) # net = MobileNetV3(n_class=num_classes, mode="small", dropout=0.2, width_mult=1.0) # net = MobileNetV3(n_class=num_classes, mode="large", dropout=0.2, width_mult=1.0) # net = MobileNetV3_Small(num_classes=num_classes) # net = MobileNetV3(n_class=num_classes, mode="large", dropout=0.2, width_mult=1.0) # # 加载模型权重,忽略不同 # # model_path = r"checkpoint/imagenet/imagenet100/mobilenetv2/111/mobilenetv2_1_imagenet_acc=68.9234.pth" # # model_path = r"checkpoint/kaggle/v1/mobilenetv2/pre/000/mobilenetv2_1_kg1_acc=85.2244.pth" # # model_path = r"checkpoint/imagenet/imagenet100/ghostnet/000/ghostnet_1_imagenet_acc=63.0497.pth" # # model_path = r"checkpoint/imagenet/imagenet100/mnext/000/mnext_1_imagenet_acc=65.5769.pth" # model_path = r"weights/mnext.pth.tar" # model_dict =net.state_dict() # checkpoint = torch.load(model_path, map_location=device) # # pretrained_dict = checkpoint["net"] # pretrained_dict = checkpoint # pretrained_dict = {k: v for k, v in pretrained_dict.items() if np.shape(model_dict[k]) == np.shape(v)} # model_dict.update(pretrained_dict) # net.load_state_dict(model_dict) # # print("loaded model with acc:{}".format(checkpoint["acc"])) # # # # # 预加载 # # path = r"checkpoint\resnet18\000\B0_acc=83.4532.pth" # # path = r"checkpoint/mobilenetv2/000/mv2_acc=82.7338.pth" # path = r"checkpoint/resnext50/333/resnext50_my_acc=72.6619.pth" # checkpoint = torch.load(path) # net.load_state_dict(checkpoint["net"],strict=False) # 模型参数大小不一样仍然报错!可能因为其通过参数名确定是否加载,但参数名相同默认参数大小一样,而这里刚好不一样故报错 # print("loaded model with acc:{}".format(checkpoint["acc"])) # num_in = net.fc.in_features # # 创建的层默认参数需要训练 # net.fc = nn.Linear(num_in, num_classes) # 按照dim,将index指定位置的值取出 # gather() For a 3-D tensor the output is specified by: # out[i][j][k] = input[index[i][j][k]][j][k] # if dim == 0 # out[i][j][k] = input[i][index[i][j][k]][k] # if dim == 1 # out[i][j][k] = input[i][j][index[i][j][k]] # if dim == 2 # 按照dim,将值放入index指定的位置 # scatter_() For a 3-D tensor, self is updated as: # self[index[i][j][k]][j][k] = src[i][j][k] # if dim == 0 # self[i][index[i][j][k]][k] = src[i][j][k] # if dim == 1 # self[i][j][index[i][j][k]] = src[i][j][k] # if dim == 2 def CrossEntropy(outputs, targets): log_softmax_outputs = F.log_softmax(outputs, dim=1) batch_size, class_num = outputs.shape onehot_targets = torch.zeros(batch_size, class_num).to(targets.device).scatter_(1, targets.view(batch_size, 1), 1) return -(log_softmax_outputs * onehot_targets).sum(dim=1).mean() def CrossEntropy_KD(outputs, targets): log_softmax_outputs = F.log_softmax(outputs, dim=1) softmax_targets = F.softmax(targets, dim=1) return -(log_softmax_outputs * softmax_targets).sum(dim=1).mean() def change_lr2(epoch, T=20, factor=0.3, min=1e-4): mul = 1. if epoch < T: mul = mul elif epoch < T * 2: mul = mul * factor elif epoch < T * 3: mul = mul * factor * factor elif epoch < T * 4: mul = mul * factor * factor * factor elif epoch < T * 5: mul = mul * factor * factor * factor * factor else: return min # print(max((1 + math.cos(math.pi * (epoch % T) / T)) * mul/2, min)) return max((1 + math.cos(math.pi * (epoch % T) / T)) * mul / 2, min) def change_lr3(epoch, T=15, factor=0.3, min=1e-4): mul = 1. if epoch < T * 3: mul = mul elif epoch < T * 7: mul = mul * factor elif epoch < T * 11: mul = mul * factor * factor else: return min # print(max((1 + math.cos(math.pi * epoch/ T)) * mul/2, min)) return max((1 + math.cos(math.pi * epoch / T)) * mul / 2, min) def change_lr4(epoch, T=10, factor=0.3, min=1e-4): mul = 1. if epoch < T * 3: mul = mul elif epoch < T * 5: mul = mul * factor elif epoch < T * 7: mul = mul * factor * factor elif epoch < T * 9: mul = mul * factor * factor * factor else: return min # print(max((1 + math.cos(math.pi * epoch/ T)) * mul/2, min)) return max((1 + math.cos(math.pi * epoch / T)) * mul / 2, min) def change_lr5(epoch, T=10, factor=0.3, min=1e-3): mul = 1. if epoch < T * 3: mul = mul elif epoch < T * 5: mul = mul * factor elif epoch < T * 7: mul = mul * factor * factor else: return min # print(max((1 + math.cos(math.pi * epoch/ T)) * mul/2, min)) return max((1 + math.cos(math.pi * epoch / T)) * mul / 2, min) def change_lr6(epoch, T=6, factor=0.3, min=1e-3): mul = 1. if epoch < T * 3: mul = mul elif epoch < T * 5: mul = mul * factor elif epoch < T * 7: mul = mul * factor * factor else: return min # print(max((1 + math.cos(math.pi * epoch/ T)) * mul/2, min)) return max((1 + math.cos(math.pi * epoch / T)) * mul / 2, min) def change_lr7(epoch, T=8, factor=0.3, min=1e-3): mul = 1. if epoch < T * 3: mul = mul elif epoch < T * 5: mul = mul * factor else: return min return max((1 + math.cos(math.pi * epoch / T)) * mul / 2, min) # 注意 new_lr = lr * mul def change_lr8(epoch, T=6, factor=0.3, min=1e-2): mul = 1. if epoch < T * 3: mul = mul elif epoch < T * 5: mul = mul * factor elif epoch < T * 7: mul = mul * factor * factor else: return min # print(max((1 + math.cos(math.pi * epoch/ T)) * mul/2, min)) return max((1 + math.cos(math.pi * epoch / T)) * mul / 2, min) def change_lr9(epoch, T=6, factor=0.3, min=1e-3): mul = 1. if epoch < T * 3: mul = mul elif epoch < T * 7: mul = mul * factor else: return min return max((1 + math.cos(math.pi * epoch / T)) * mul / 2, min) def change_lr10(epoch, T=5, factor=0.3, min=1e-2): mul = 1. if epoch < T * 3: mul = mul elif epoch < T * 5: mul = mul * factor else: return min # print(max((1 + math.cos(math.pi * epoch/ T)) * mul/2, min)) return max((1 + math.cos(math.pi * epoch / T)) * mul / 2, min) def change_lr11(epoch, T=8, min=1e-3): mul = 1. if epoch < T: mul = mul else: return min # print(max((1 + math.cos(math.pi * epoch/ T)) * mul/2, min)) return max((1 + math.cos(math.pi * epoch / T)) * mul / 2, min) def change_lr12(epoch, T=6, factor=0.3, min=1e-3): mul = 1. if epoch < T: mul = mul elif epoch < T * 3: mul = mul * factor else: return min return max((1 + math.cos(math.pi * epoch / T)) * mul / 2, min) criterion = nn.CrossEntropyLoss() criterion = CrossEntropy # epoches = 48 # epoches = 30 # epoches = 30 # epoches = 16 # epoches = 30 # optimizer = optim.Adam(net.parameters(), lr=5e-3) # scheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=change_lr9) # optimizer = optim.Adam(net.parameters(), lr=1e-2) # scheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=change_lr9) # optimizer = optim.Adam(net.parameters(), lr=1e-3) # scheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=change_lr9) # optimizer = optim.Adam(net.parameters(), lr=1e-3) # scheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=change_lr10) # optimizer = optim.SGD(net.parameters(), lr=1e-3) # scheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=change_lr9) # optimizer = optim.Adam(net.parameters(), lr=1e-2) # optimizer = optim.Adam(net.parameters(), lr=1e-3) # scheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=change_lr6) # optimizer = optim.SGD(net.parameters(), lr=1e-1, # momentum=0.9, weight_decay=5e-4) # optimizer = optim.SGD(net.parameters(), lr=1e-2, # momentum=0.9, weight_decay=5e-4) # scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=[2, 8], gamma=0.1) # # scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.1, patience=3, # verbose=True, threshold=1e-4, threshold_mode='rel', # cooldown=0, min_lr=1e-7, eps=1e-8) # scheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=change_lr4) # scheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=change_lr7) # scheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=change_lr8) # epoches = 48 # optimizer = optim.Adam(net.parameters(), lr=1e-3) # scheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=change_lr9) # epoches = 30 # optimizer = optim.Adam(net.parameters(), lr=1e-3) # scheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=change_lr10) epoches = 18 optimizer = optim.Adam(net.parameters(), lr=1e-3) scheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=change_lr12) # epoches = 16 # optimizer = optim.Adam(net.parameters(), lr=1e-3) # scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=[6, 12], gamma=0.1) # epoches = 8 # optimizer = optim.Adam(net.parameters(), lr=1e-3) # scheduler = optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=change_lr11) net.to(device) # data_path = r"E:\Datasets\state-farm-distracted-driver-detection\imgs\train" # data_path = r".\data\hymenoptera_data\train" # datasets.ImageFolder读取图片文件夹,要求图片按照类别分文件夹存放 # root:在root指定的路径下寻找图片 # transform:对PIL Image进行的转换操作,transform的输入是使用loader读取图片的返回对象 # target_transform:对label的转换 # loader:给定路径后如何读取图片,默认读取为RGB格式的PIL Image对象 # # kaggle dataset 100 # train_dataset = MyDataset("data/imagenet/imagenet2012_100_train.txt", train_transform) # val_dataset = MyDataset("data/imagenet/imagenet2012_100_val.txt", val_transform) # # train_dataloader = DataLoader(dataset=train_dataset, # # batch_size=128, # # shuffle=True, # # num_workers=0) # # # # # # val_dataloader = DataLoader(dataset=val_dataset, # # batch_size=128, # # shuffle=True, # # num_workers=0) # # train_dataloader = DataLoader(dataset=train_dataset, # batch_size=96, # shuffle=True, # num_workers=0) # # # val_dataloader = DataLoader(dataset=val_dataset, # batch_size=96, # shuffle=True, # num_workers=0) # # # kaggle dataset # # train_dataset = datasets.ImageFolder(root=data_path, transform=data_transform) # train_dataset = MyDataset("data/txt/kg_train224.txt", train_transform) # val_dataset = MyDataset("data/txt/kg_val224.txt", val_transform) # train_dataloader = DataLoader(dataset=train_dataset, # batch_size=64, # shuffle=True, # num_workers=0) # # val_dataloader = DataLoader(dataset=val_dataset, # batch_size=64, # shuffle=True, # num_workers=0) # # kaggle dataset 2 # train_dataset = MyDataset("data/txt/kg_train2.txt", train_transform) # val_dataset = MyDataset("data/txt/kg_val2.txt", val_transform) # train_dataloader = DataLoader(dataset=train_dataset, # batch_size=64, # shuffle=True, # num_workers=0) # # # val_dataloader = DataLoader(dataset=val_dataset, # batch_size=64, # shuffle=True, # num_workers=0) # train_dataset = MyDataset("data/txt/kg_train2_224.txt", train_transform) # val_dataset = MyDataset("data/txt/kg_val2_224.txt", val_transform) # train_dataloader = DataLoader(dataset=train_dataset, # batch_size=64, # shuffle=True, # num_workers=0) # # # val_dataloader = DataLoader(dataset=val_dataset, # batch_size=64, # shuffle=True, # num_workers=0) # # AUC v1 dataset # train_dataset = MyDataset("data/txt/aucv1_trainVal224.txt", train_transform) # val_dataset = MyDataset("data/txt/aucv1_test224.txt", val_transform) # train_dataloader = DataLoader(dataset=train_dataset, # batch_size=64, # shuffle=True, # num_workers=0) # val_dataloader = DataLoader(dataset=val_dataset, # batch_size=64, # shuffle=True, # num_workers=0) # AUC v2 dataset # train_dataset = MyDataset("data/txt/auc_trainVal224.txt", train_transform) # val_dataset = MyDataset("data/txt/auc_test224.txt", val_transform) # train_dataloader = DataLoader(dataset=train_dataset, # batch_size=64, # shuffle=True, # num_workers=0) # val_dataloader = DataLoader(dataset=val_dataset, # batch_size=64, # shuffle=True, # num_workers=0) # # AUC v2 dataset # train_dataset = MyDataset("data/txt/auc_trainVal224.txt", train_transform) # val_dataset = MyDataset("data/txt/auc_test224.txt", val_transform) # train_dataloader = DataLoader(dataset=train_dataset, # batch_size=64, # shuffle=True, # num_workers=0) # val_dataloader = DataLoader(dataset=val_dataset, # batch_size=64, # shuffle=True, # num_workers=0) # drive119 # train_dataset = MyDataset("./data/train11_9.txt", train_transform) # train_dataloader = DataLoader(dataset=train_dataset, # batch_size=32, # shuffle=True, # num_workers=0) # val_dataset = MyDataset("./data/val11_9.txt", val_transform) # val_dataloader = DataLoader(dataset=val_dataset, # batch_size=32, # shuffle=True, # num_workers=0) # # drive224 将drive119图片预处理为224,提高速度 # train_dataset = MyDataset("./data/train224.txt", train_transform) # train_dataloader = DataLoader(dataset=train_dataset, # batch_size=32, # shuffle=True, # num_workers=0) # train_dataset = MyDataset("./data/kgAddmy_add.txt", train_transform) # train_dataloader = DataLoader(dataset=train_dataset, # batch_size=64, # shuffle=True, # num_workers=0) # val_dataset = MyDataset("./data/val224.txt", val_transform) # val_dataset = MyDataset("./data/test11_9s.txt", val_transform) # val_dataloader = DataLoader(dataset=val_dataset, # batch_size=32, # shuffle=True, # num_workers=0) # # test_dataset = MyDataset("./data/test11_9s.txt", val_transform) # test_dataloader = DataLoader(dataset=test_dataset, # batch_size=32, # shuffle=True, # num_workers=0) # # dataset 11_16 # # train_dataset = MyDataset("data/kgAddmy_add.txt", train_transform) # # train_dataset = MyDataset("data/total_train.txt", train_transform) # # train_dataset = MyDataset("data/train224_116_119.txt", train_transform) # # train_dataset = MyDataset("data/txt/116_119trainAddcrop224.txt", train_transform) # train_dataset = MyDataset("data/txt/116_119trainAddcrop224_kg.txt", train_transform) # # train_dataset = MyDataset("data/txt/116_traincrop224.txt", train_transform) # # train_dataset = MyDataset("data/txt/116_119traincrop224.txt", train_transform) # # train_dataset = MyDataset("data/txt/116_119traincrop224_kg.txt", train_transform) # # train_dataset = MyDataset("data/txt/116_119traincrop224_kg_auc2.txt", train_transform) # # train_dataset = MyDataset("data/train224_11_16_train.txt", train_transform) # train_dataloader = DataLoader(dataset=train_dataset, # batch_size=64, # # batch_size=32, # shuffle=True, # num_workers=0) # val_dataset = MyDataset("data/test224_11_16.txt", val_transform) # # val_dataset = MyDataset("data/txt/116_testcrop224.txt", val_transform) # # val_dataset = MyDataset("data/train224_11_16_val.txt", val_transform) # val_dataloader = DataLoader(dataset=val_dataset, # batch_size=64, # shuffle=True, # num_workers=0) # test_dataset = MyDataset("data/test224_11_16.txt", val_transform) # test_dataloader = DataLoader(dataset=test_dataset, # batch_size=32, # shuffle=True, # num_workers=0) # # # # dataset kg_total # train_dataset = MyDataset("data/kg_total_add_t.txt", train_transform) # train_dataloader = DataLoader(dataset=train_dataset, # batch_size=128, # shuffle=True, # num_workers=0) # val_dataset = MyDataset("data/test224_11_16.txt", val_transform) # # val_dataset = MyDataset("data/kg_total_add_v.txt", val_transform) # val_dataloader = DataLoader(dataset=val_dataset, # batch_size=128, # shuffle=True, # num_workers=0) # # dataset 12_23 # train_dataset = MyDataset("data/txt/12_23_2_train224.txt", train_transform) # val_dataset = MyDataset("data/txt/12_23_2_test224.txt", val_transform) # train_dataset = MyDataset("data/txt/12_23_1_train224.txt", train_transform) # val_dataset = MyDataset("data/txt/12_23_1_test224.txt", val_transform) # train_dataset = MyDataset("data/txt/12_23_12_train224.txt", train_transform) # val_dataset = MyDataset("data/txt/12_23_12_test224.txt", val_transform) # train_dataset = MyDataset("data/txt/12_23_12_addpre_train224.txt", train_transform) # val_dataset = MyDataset("data/txt/12_23_12_addpre_test224.txt", val_transform) # train_dataset = MyDataset("data/txt/12_23_12_addpre_train224_kg2my.txt", train_transform) # val_dataset = MyDataset("data/txt/12_23_12_addpre_test224.txt", val_transform) # train_dataset = MyDataset("data/txt/12_23_12_addpre_train224_kg2my_aucv2_my.txt", train_transform) # val_dataset = MyDataset("data/txt/12_23_12_addpre_test224.txt", val_transform) # crop 12_23 # train_dataset = MyDataset("data/txt/12_23_12_addpre_train_crop224.txt", train_transform) # train_dataset = MyDataset("data/txt/12_23_12_addpre_train_crop224_kg2my.txt", train_transform) # train_dataset = MyDataset("data/txt/12_23_12_addpre_train_crop224_kg2my_aucv2_my.txt", train_transform) # val_dataset = MyDataset("data/txt/12_23_12_addpre_test_crop224.txt", val_transform) # train_dataset = MyDataset("data/txt/12_23_12_addpre_train224_addcrop.txt", train_transform) # train_dataset = MyDataset("data/txt/12_23_12_addpre_train224_kg2my_aucv2_my_addcrop.txt", train_transform) # val_dataset = MyDataset("data/txt/12_23_12_addpre_test224_addcrop.txt", val_transform) # # class6 # train_dataset = MyDataset("data/txt6/12_23_12_addpre_train224_6.txt", train_transform) # val_dataset = MyDataset("data/txt6/12_23_12_addpre_test224_6.txt", val_transform) # train_dataset = MyDataset("data/txt6/12_23_12_addpre_train224_addcrop_6.txt", train_transform) # val_dataset = MyDataset("data/txt6/12_23_12_addpre_test224_addcrop_6.txt", val_transform) # train_dataset = MyDataset("data/txt6/12_23_12_addpre_train224_kg2my_aucv2_my_addcrop_6.txt", train_transform) # val_dataset = MyDataset("data/txt6/12_23_12_addpre_test224_addcrop_6.txt", val_transform) # train_dataset = MyDataset("data/txt6/12_23_12_addpre_train224_kg2my_aucv2_my_6.txt", train_transform) # val_dataset = MyDataset("data/txt6/12_23_12_addpre_test224_6.txt", val_transform) # class7 # train_dataset = MyDataset("data/txt7/12_23_12_addpre_train224_7.txt", train_transform) # val_dataset = MyDataset("data/txt7/12_23_12_addpre_test224_7.txt", val_transform) # train_dataset = MyDataset("data/txt7/12_23_12_addpre_train224_addcrop_7.txt", train_transform) # val_dataset = MyDataset("data/txt7/12_23_12_addpre_test224_addcrop_7.txt", val_transform) # train_dataset = MyDataset("data/txt7/12_23_12_addpre_train224_kg2my_aucv2_my_addcrop_7.txt", train_transform) # val_dataset = MyDataset("data/txt7/12_23_12_addpre_test224_addcrop_7.txt", val_transform) # train_dataset = MyDataset("data/txt7/12_23_12_addpre_train224_kg2my_aucv2_my_7.txt", train_transform) # val_dataset = MyDataset("data/txt7/12_23_12_addpre_test224_7.txt", val_transform) # txt_raw # train_dataset = MyDataset("data/txt_raw/total_train.txt", train_transform) # val_dataset = MyDataset("data/txt_raw/total_test.txt", val_transform) # train_dataset = MyDataset("data/txt_raw/total_train_c6.txt", train_transform) # val_dataset = MyDataset("data/txt_raw/total_test_c6.txt", val_transform) # # train_dataset = MyDataset("data/txt_raw/total_train_c7.txt", train_transform) # val_dataset = MyDataset("data/txt_raw/total_test_c7.txt", val_transform) #3_23 # train_dataset = MyDataset("data/txt_3_23/bus_train.txt", train_transform) # val_dataset = MyDataset("data/txt_3_23/bus_test.txt", val_transform) # train_dataset = MyDataset("data/txt_3_23/he_train.txt", train_transform) # val_dataset = MyDataset("data/txt_3_23/he_test.txt", val_transform) # train_dataset = MyDataset("data/txt_3_23/wen_train.txt", train_transform) # val_dataset = MyDataset("data/txt_3_23/wen_test.txt", val_transform) # train_dataset = MyDataset("data/txt_3_23/he_wen_train.txt", train_transform) # val_dataset = MyDataset("data/txt_3_23/he_wen_test.txt", val_transform) # # 3_25 # train_dataset = MyDataset("data/txt_3_25/train325.txt", train_transform) # val_dataset = MyDataset("data/txt_3_25/test325.txt", val_transform) # # 3_25 crop # train_dataset = MyDataset("data/txt_3_25/train325_crop.txt", train_transform) # val_dataset = MyDataset("data/txt_3_25/test325_crop.txt", val_transform) # 3-25_all # train_dataset = MyDataset("data/txt_3_25/train325_all.txt", train_transform) # val_dataset = MyDataset("data/txt_3_25/test325_all.txt", val_transform) # # 3-25_all crop # train_dataset = MyDataset("data/txt_raw_crop/total_train_crop.txt", train_transform) # val_dataset = MyDataset("data/txt_raw_crop/total_test_crop.txt", val_transform) # all class7_2 # train_dataset = MyDataset("data/txt_raw/total_train_c7_2.txt", train_transform) # val_dataset = MyDataset("data/txt_raw/total_test_c7_2.txt", val_transform) # all class72_crop # train_dataset = MyDataset("data/txt_raw_crop/total_train_crop_72.txt", train_transform) # val_dataset = MyDataset("data/txt_raw_crop/total_test_crop_72.txt", val_transform) # # all class73_crop # train_dataset = MyDataset("data/txt_raw_crop/total_train_crop_73.txt", train_transform) # val_dataset = MyDataset("data/txt_raw_crop/total_test_crop_73.txt", val_transform) # # all class8 # train_dataset = MyDataset("data/txt_raw/total_train_8.txt", train_transform) # val_dataset = MyDataset("data/txt_raw/total_test_8.txt", val_transform) # # all class8_crop # train_dataset = MyDataset("data/txt_raw_crop/total_train_crop_8.txt", train_transform) # val_dataset = MyDataset("data/txt_raw_crop/total_test_crop_8.txt", val_transform) # all class9 # train_dataset = MyDataset("data/txt_raw/total_train.txt", train_transform) # val_dataset = MyDataset("data/txt_raw/total_test.txt", val_transform) # # # all class9_crop train_dataset = MyDataset("data/txt_raw_crop/total_train_crop.txt", train_transform) val_dataset = MyDataset("data/txt_raw_crop/total_test_crop.txt", val_transform) train_dataloader = DataLoader(dataset=train_dataset, batch_size=64, shuffle=True, num_workers=0) val_dataloader = DataLoader(dataset=val_dataset, batch_size=64, shuffle=True, num_workers=0) # train_dataloader = DataLoader(dataset=train_dataset, # batch_size=32, # shuffle=True, # num_workers=0) # val_dataloader = DataLoader(dataset=val_dataset, # batch_size=32, # shuffle=True, # num_workers=0) # Training def train(epoch): print('\nEpoch: %d' % (epoch + 1)) net.train() global best_acc train_loss = 0 correct = 0 total = 0 for batch_idx, (inputs, targets) in enumerate(train_dataloader): inputs, targets = inputs.to(device), targets.to(device) # print("inputs.shape",inputs.shape) optimizer.zero_grad() outputs = net(inputs) loss = criterion(outputs, targets) loss.backward() optimizer.step() train_loss += loss.item() _, predicted = outputs.max(1) total += targets.size(0) correct += predicted.eq(targets).sum().item() average_loss = train_loss / (batch_idx + 1) train_acc = correct / total progress_bar(batch_idx, len(train_dataloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)' % (average_loss, 100. * train_acc, correct, total)) lr = optimizer.state_dict()['param_groups'][0]['lr'] scheduler.step() # scheduler.step(average_loss) return average_loss, train_acc, lr # # Save checkpoint. # acc = 100.*correct/total # if acc > best_acc: # print('Saving..') # state = { # 'net': net.state_dict(), # 'acc': acc, # 'epoch': epoch, # } # if not os.path.isdir('checkpoint/B0'): # os.mkdir('checkpoint/B0') # torch.save(state, './checkpoint/B0/111/B0_acc={:.4f}.pth'.format(acc)) # best_acc = acc # Save checkpoint. # kaggle v1 # savepath = 'checkpoint/kaggle/v1/mobilenetv2/pre/000/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/kaggle/v1/mobilenetv2/nopre/000/' # randcrop 16 rotation 10 colorjit 0.5 # kaggle v2 # savepath = 'checkpoint/kaggle/v2/mobilenetv2/000/' # no augment # savepath = 'checkpoint/kaggle/v2/mobilenetv2/111/' # rotation 10 # savepath = 'checkpoint/kaggle/v2/mobilenetv2/222/' # randcrop 16 # savepath = 'checkpoint/kaggle/v2/mobilenetv2/333/' # randcrop 16 rotation 10 # savepath = 'checkpoint/kaggle/v2/mobilenetv2/444/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/kaggle/v2/mobilenetv2/555/' # randcrop 16 rotation 10 colorjit 0.2 # savepath = 'checkpoint/kaggle/v2/mobilenetv2/666/' # randcrop 16 rotation 30 colorjit 0.5 # savepath = 'checkpoint/kaggle/v2/mobilenetv2/777/' # randcrop 16 rotation 20 colorjit 0.5 # savepath = 'checkpoint/kaggle/v2/mobilenetv2/888/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/kaggle/v2/mobilenetv2/pre/000/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/kaggle/v2/mobilenetv2/pre/111/' # randcrop 16 rotation 20 colorjit 0.5 # AUC v2 # savepath = 'checkpoint/AUC/v2/mobilenetv2/000/' # savepath = 'checkpoint/AUC/v2/mobilenetv2/111/' # savepath = 'checkpoint/AUC/v2/mobilenetv2/222/' # savepath = 'checkpoint/AUC/v2/mobilenetv2/333/' # savepath = 'checkpoint/AUC/v2/mobilenetv2/444/' # savepath = 'checkpoint/AUC/v2/mobilenetv2/pre/444/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/AUC/v2/mobilenetv2/pre/000/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/AUC/v2/mnext/000/' # savepath = 'checkpoint/AUC/v2/resnet50/000/' # savepath = 'checkpoint/AUC/v2/resnet50/111/' # AUC v1 # savepath = 'checkpoint/AUC/v1/mobilenetv2/000/' # savepath = 'checkpoint/AUC/v1/mobilenetv2/111/' # savepath = 'checkpoint/AUC/v1/mobilenetv2/222/' # savepath = 'checkpoint/AUC/v1/mobilenetv2/333/' # savepath = 'checkpoint/AUC/v1/mobilenetv2/444/' # savepath = 'checkpoint/AUC/v1/mnext/000/' # savepath = 'checkpoint/AUC/v1/ghostnet/000/' # savepath = 'checkpoint/AUC/v1/resnet50/000/' # 11_16 # savepath = 'checkpoint/data_11_16/mobilenetv2/nopre/333/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/data_11_16/mobilenetv2/pre/000/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/data_11_16/mobilenetv2/pre/111/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/data_11_16/mobilenetv2/pre/222/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/data_11_16/mobilenetv2/pre/333/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/data_11_16/ghostnet/pre/000/' # randcrop 16 rotation 10 colorjit 0.5 change_lr8 1e-3 # savepath = 'checkpoint/data_11_16/ghostnet/pre/111/' # randcrop 16 rotation 10 colorjit 0.5 change_lr6 1e-3 # savepath = 'checkpoint/data_11_16/ghostnet/pre/222/' # randcrop 16 rotation 10 colorjit 0.5 change_lr6 1e-3 addcrop # savepath = 'checkpoint/data_11_16/ghostnet/pre/333/' # randcrop 16 rotation 10 colorjit 0.5 change_lr6 1e-3 addcrop # savepath = 'checkpoint/data_11_16/mnext/pre/000/' # randcrop 16 rotation 10 colorjit 0.5 change_lr8 # savepath = 'checkpoint/data_11_16/mnext/pre/111/' # randcrop 16 rotation 10 colorjit 0.5 change_lr8 # savepath = 'checkpoint/data_11_16/mnext/pre/222/' # randcrop 16 rotation 10 colorjit 0.5 change_lr6 1e-3 addcrop # crop224 # savepath = 'checkpoint/data_11_16/mobilenetv2/pre/444/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/data_11_16/mobilenetv2/pre/555/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/data_11_16/mobilenetv2/pre/666/' # randcrop 16 rotation 10 colorjit 0.5 160 # savepath = 'checkpoint/data_11_16/mobilenetv2/pre/777/' # randcrop 16 rotation 10 colorjit 0.5 add kg # savepath = 'checkpoint/data_11_16/mobilenetv2/pre/888/' # randcrop 16 rotation 10 colorjit 0.5 add kg 160 # savepath = 'checkpoint/data_11_16/mobilenetv2/pre/999/' # randcrop 16 rotation 10 colorjit 0.5 add kg auc2 # savepath = 'checkpoint/data_11_16/mobilenetv2/pre/0/000/' # 16 rotation 10 colorjit 0.5 224 116_119 # savepath = 'checkpoint/data_11_16/mobilenetv2/pre/0/111/' # randcrop 16 rotation 10 colorjit 0.5 224 116_119 add crop # savepath = 'checkpoint/data_11_16/mobilenetv2/pre/0/222/' # add andcrop 16 rotation 10 colorjit 0.5 224 116_119 add crop kg # savepath = 'checkpoint/data_11_16/mobilenetv2/pre/0/333/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/data_11_16/mobilenetv2/pre/0/444/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/data_11_16/mobilenetv2/pre/0/555/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/data_11_16/mobilenetv2/pre/0/666/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/data_11_16/shufflenetv2/pre/000/' # add andcrop 16 rotation 10 colorjit 0.5 224 116_119 add crop kg # savepath = 'checkpoint/data_11_16/shufflenetv2/pre/000/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/data_11_16/shufflenetv2/pre/111/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/data_11_16/shufflenetv2/pre/222/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/data_11_16/shufflenetv2/pre/333/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/data_11_16/shufflenetv2/pre/444/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/data_11_16/shufflenetv2/pre/555/' # randcrop 16 rotation 10 colorjit 0.5 # imagenet # savepath = 'checkpoint/imagenet/imagenet100/mobilenetv2/000/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/imagenet/imagenet100/mobilenetv2/111/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/imagenet/imagenet100/ghostnet/000/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/imagenet/imagenet100/mnext/000/' # randcrop 16 rotation 10 colorjit 0.5 # savepath = 'checkpoint/imagenet/imagenet100/my_mnextv2/000/' # randcrop 16 rotation 10 colorjit 0.5 # dataset 12_23 # savepath = 'checkpoint/data_12_23/mobilenetv2/000/' # randcrop 16 rotation 10 colorjit 0.5 12_23_2 change_lr6 1e-3 # savepath = 'checkpoint/data_12_23/mobilenetv2/111/' # randcrop 16 rotation 10 colorjit 0.5 12_23_1 change_lr6 1e-3 # savepath = 'checkpoint/data_12_23/mobilenetv2/222/' # randcrop 16 rotation 10 colorjit 0.5 12_23_12 change_lr6 1e-3 # savepath = 'checkpoint/data_12_23/mobilenetv2/333/' # randcrop 16 rotation 10 colorjit 0.5 12_23_12_addpre change_lr6 1e-3 # savepath = 'checkpoint/data_12_23/mobilenetv2/444/' # randcrop 16 rotation 10 colorjit 0.5 12_23_12_addpre kg2my change_lr6 1e-3 # savepath = 'checkpoint/data_12_23/mobilenetv2/555/' # randcrop 16 rotation 10 colorjit 0.5 12_23_12_addpre change_lr9 5e-3 # savepath = 'checkpoint/data_12_23/mobilenetv2/666/' # randcrop 16 rotation 10 colorjit 0.5 12_23_12_addpre change_lr9 1e-3 # savepath = 'checkpoint/data_12_23/mobilenetv2/777/' # randcrop 16 rotation 10 colorjit 0.5 12_23_12_addpre kg2my change_lr9 1e-3 # savepath = 'checkpoint/data_12_23/mobilenetv2/888/' # randcrop 16 rotation 10 colorjit 0.5 12_23_12_addpre kg2my aucv2 change_lr9 1e-3 # savepath = 'checkpoint/data_12_23/mobilenetv2/0/999/' # randcrop 16 rotation 10 colorjit 0.5 12_23_12_addpre kg2my aucv2 change_lr9 sgd 1e-3 # savepath = 'checkpoint/data_12_23/mobilenetv2/0/000/' # randcrop 16 rotation 10 colorjit 0.5 12_23_12_addpre kg2my aucv2 change_lr9 sgd 1e-1 # savepath = 'checkpoint/data_12_23/mobilenetv2/0/111/' # randcrop 16 rotation 10 colorjit 0.5 12_23_12_addpre kg2my aucv2 change_lr9 # savepath = 'checkpoint/data_12_23/mobilenetv2/0/222/' # flip randcrop 16 rotation 10 colorjit 0.5 12_23_12_addpre change_lr9 # savepath = 'checkpoint/data_12_23/mobilenetv2/0/333/' # randcrop 16 rotation 10 colorjit 0.5 12_23_12_addpre kg2my aucv2 change_lr9 # savepath = 'checkpoint/data_12_23/mobilenetv2/0/444/' # change_lr10 brightness=0.8 mypre # savepath = 'checkpoint/data_12_23/mobilenetv2/0/555/' # change_lr10 brightness=0.8 mypre # savepath = 'checkpoint/data_12_23/mobilenetv2/nopre/000/' # nopre randcrop 16 rotation 10 colorjit 0.5 12_23_12_change_lr9 1e-3 # savepath = 'checkpoint/data_12_23/mobilenetv2/nopre/111/' # pre randcrop 16 rotation 10 colorjit 0.5 12_23_12_change_lr9 1e-3 # savepath = 'checkpoint/data_12_23/mobilenetv2/nopre/0/000/' # nopre randcrop 16 rotation 10 colorjit 0.5 12_23_12_change_lr9 1e-2 cbam c=64 # savepath = 'checkpoint/data_12_23/mobilenetv2/nopre/0/111/' # nopre randcrop 16 rotation 10 colorjit 0.5 12_23_12_change_lr9 1e-3 cbam c=64 # savepath = 'checkpoint/data_12_23/mobilenetv2/nopre/1/000/' # nopre randcrop 16 rotation 10 colorjit 0.5 12_23_12_change_lr9 1e-2 cbam c=16 # savepath = 'checkpoint/data_12_23/mobilenetv2/nopre/1/111/' # pre randcrop 16 rotation 10 colorjit 0.5 12_23_12_change_lr9 1e-2 cbam c=16 # savepath = 'checkpoint/data_12_23/mobilenetv2/nopre/1/222/' # pre randcrop 16 rotation 10 colorjit 0.5 12_23_12_ addpre kg2my aucv2 change_lr9 1e-2 cbam c=16 # savepath = 'checkpoint/data_12_23/mobilenetv2/nopre/1/333/' # pre randcrop 16 rotation 10 colorjit 0.5 12_23_12_ addpre kg2my aucv2 change_lr9 1e-2 cbam c=64 # savepath = 'checkpoint/data_12_23/mnext/000/' # randcrop 16 rotation 10 colorjit 0.5 12_23_12_addpre kg2my aucv2 change_lr9 # savepath = 'checkpoint/data_12_23/mnext/111/' # randcrop 16 rotation 10 colorjit 0.5 12_23_12_addpre change_lr9 # dataset 12_23 # savepath = 'checkpoint/data_12_23/mobilenetv2/crop/000/' # crop randcrop 16 rotation 10 colorjit 0.5 12_23_2 change_lr6 1e-3 # savepath = 'checkpoint/data_12_23/mobilenetv2/crop/111/' # crop randcrop 16 rotation 10 colorjit 0.5 12_23_2 change_lr6 1e-3 addpre kg2my # savepath = 'checkpoint/data_12_23/mobilenetv2/crop/222/' # crop randcrop 16 rotation 10 colorjit 0.5 12_23_2 change_lr6 1e-3 addpre kg2my aucv2 # savepath = 'checkpoint/data_12_23/mobilenetv2/crop/333/' # randcrop 16 rotation 10 colorjit 0.5 12_23_2 change_lr6 1e-3 addpre addcrop # savepath = 'checkpoint/data_12_23/mobilenetv2/crop/444/' # randcrop 16 rotation 10 colorjit 0.5 12_23_2 change_lr6 1e-3 addpre kg2my aucv2 addcrop # # dataset class6 # # savepath = 'checkpoint/data_12_23/class6/mobilenetv2/000/' # randcrop 16 rotation 10 colorjit 0.5 12_23_2 change_lr9 1e-3 # savepath = 'checkpoint/data_12_23/class6/mobilenetv2/111/' # randcrop 16 rotation 10 colorjit 0.5 12_23_2 change_lr9 1e-3 addcrop # savepath = 'checkpoint/data_12_23/class6/mobilenetv2/222/' # randcrop 16 rotation 10 colorjit 0.5 12_23_2 change_lr9 1e-3 addpre kg2my aucv2 addcrop # savepath = 'checkpoint/data_12_23/class6/mobilenetv2/333/' # randcrop 16 rotation 10 colorjit 0.5 12_23_2 change_lr9 1e-3 a ddpre kg2my aucv2 # dataset class7 # savepath = 'checkpoint/data_12_23/class7/mobilenetv2/000/' # randcrop 16 rotation 10 colorjit 0.5 12_23_2 change_lr9 1e-3 # savepath = 'checkpoint/data_12_23/class7/mobilenetv2/111/' # randcrop 16 rotation 10 colorjit 0.5 12_23_2 change_lr91e-3 addcrop # savepath = 'checkpoint/data_12_23/class7/mobilenetv2/222/' # randcrop 16 rotation 10 colorjit 0.5 12_23_2 change_lr9 1e-3 addpre kg2my aucv2 addcrop # savepath = 'checkpoint/data_12_23/class7/mobilenetv2/333/' # randcrop 16 rotation 10 colorjit 0.5 12_23_2 change_lr9 1e-3 a ddpre kg2my aucv2 # dataset txt_raw # savepath = 'checkpoint/txt_raw/mobilenetv2/224/000/' # change_lr9 totol_test # savepath = 'checkpoint/txt_raw/mobilenetv2/224/111/' # change_lr9 totol_test # savepath = 'checkpoint/txt_raw/mobilenetv2/224/222/' # change_lr9 totol_test 240-》224 # savepath = 'checkpoint/txt_raw/mobilenetv2/224/333/' # change_lr9 totol_test # savepath = 'checkpoint/txt_raw/mobilenetv2/224/444/' # change_lr9 totol_test 240-》224 # savepath = 'checkpoint/txt_raw/mobilenetv2/320/000/' # change_lr9 totol_test # savepath = 'checkpoint/txt_raw/mobilenetv2/000/' # change_lr10 totol_test 240-》224 # savepath = 'checkpoint/txt_raw/mobilenetv2/111/' # change_lr10 totol_test # savepath = 'checkpoint/txt_raw/mobilenetv2/222/' # change_lr10 totol_test brightness=0.8 # savepath = 'checkpoint/txt_raw/mobilenetv2/class6/000/' # change_lr10 totol_test 240-》224 c9 # savepath = 'checkpoint/txt_raw/mobilenetv2/class6/111/' # change_lr10 totol_test 240-》224 # savepath = 'checkpoint/txt_raw/mobilenetv2/class6/222/' # change_lr10 totol_test # savepath = 'checkpoint/txt_raw/mobilenetv2/class6/333/' # change_lr10 totol_test brightness=0.8 # savepath = 'checkpoint/txt_raw/mobilenetv2/class7/000/' # change_lr10 totol_test 240-》224 # savepath = 'checkpoint/txt_raw/mobilenetv2/class7/111/' # change_lr10 totol_test # savepath = 'checkpoint/txt_raw/mobilenetv2/class7/222/' # change_lr10 totol_test brightness=0.8 # 3_23 # savepath = 'checkpoint/data_3_23/mobilenetv2/000/' # change_lr10 brightness=0.8 # savepath = 'checkpoint/data_3_23/mobilenetv2/111/' # change_lr10 brightness=0.5 240>>224 # savepath = 'checkpoint/data_3_23/mobilenetv2/222/' # change_lr10 no data augment # savepath = 'checkpoint/data_3_23/mobilenetv2/he/000/' # change_lr10 brightness=0.5 240>>224 # savepath = 'checkpoint/data_3_23/mobilenetv2/he/111/' # change_lr10 brightness=0.8 # savepath = 'checkpoint/data_3_23/mobilenetv2/wen/000/' # change_lr10 brightness=0.5 240>>224 # savepath = 'checkpoint/data_3_23/mobilenetv2/wen/111/' # change_lr10 brightness=0.8 # savepath = 'checkpoint/data_3_23/mobilenetv2/he_wen/000/' # change_lr10 brightness=0.5 240>>224 # savepath = 'checkpoint/data_3_23/mobilenetv2/he_wen/111/' # change_lr10 brightness=0.8 # savepath = 'checkpoint/data_3_23/mobilenetv2/he_wen/222/' # change_lr10 brightness=0.8 nopre # savepath = 'checkpoint/data_3_23/mobilenetv2/he_wen/333/' # change_lr10 brightness=0.8 mypre # savepath = 'checkpoint/data_3_23/mobilenetv2/he_wen/333/' # change_lr10 brightness=0.5 mypre # savepath = 'checkpoint/data_3_23/mobilenetv2/he_wen/444/' # change_lr19 brightness=0.5 # 325 # savepath = 'checkpoint/data_3_25/mobilenetv2/000/' # change_lr10 brightness=0.5 # savepath = 'checkpoint/data_3_25/mobilenetv2/111/' # change_lr9 brightness=0.5 # 325 crop # savepath = 'checkpoint/data_3_25_crop/mobilenetv2/000/' # change_lr10 brightness=0.5 # savepath = 'checkpoint/data_3_25_crop/mobilenetv2/111/' # change_lr9 brightness=0.5 # savepath = 'checkpoint/data_3_25_crop/mobilenetv2/222/' # change_lr9 brightness=0.5 nopre # 325 all # savepath = 'checkpoint/data_3_25_all/mobilenetv2/111/' # change_lr9 brightness=0.5 # savepath = 'checkpoint/data_3_25_all/mobilenetv2/222/' # change_lr9 brightness=0.5 nopre # savepath = 'checkpoint/data_3_25_all/mobilenetv2/333/' # change_lr9 brightness=0.5 crop # savepath = 'checkpoint/data_3_25_all/mobilenetv2/444/' # change_lr9 brightness=0.5 crop nopre # savepath = 'checkpoint/data_3_25_all/mobilenetv2/555/' # change_lr9 brightness=0.5 crop 240->224 # savepath = 'checkpoint/data_3_25_all/mobilenetv2/666/' # change_lr9 brightness=0.5 crop 240->224 nopre # savepath = 'checkpoint/data_3_25_all/mobilenetv2/777/' # change_lr9 brightness=0.5 240->224 # savepath = 'checkpoint/data_3_25_all/mobilenetv2/888/' # change_lr9 brightness=0.5 240->224 nopre # savepath = 'checkpoint/data_3_25_all/mobilenetv2/16/000/' # 16 milestones brightness=0.5 240->224 # savepath = 'checkpoint/data_3_25_all/mobilenetv2/16/111/' # 16 milestones brightness=0.5 240->224 crop # savepath = 'checkpoint/data_3_25_all/mobilenetv2/18/000/' # 18 change_lr12 brightness=0.5 240->224 crop # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class9/160/000/' # 18 change_lr12 milestones brightness=0.5 160 no randcrop # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class9/160/111/' # 18 change_lr12 milestones brightness=0.5 160 crop no randcrop # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class9/224/000/' # 18 change_lr12 milestones brightness=0.5 224 no randcrop # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class9/224/111/' # 18 change_lr12 milestones brightness=0.5 224 crop no randcrop # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class9/224/222/' # 18 change_lr12 milestones brightness=0.5 224 no randcrop # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class9/224/333/' # 18 change_lr12 milestones brightness=0.5 224 crop no randcrop # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class9/224/444/' # 18 change_lr12 milestones brightness=0.5 224 crop 240->224 savepath = 'checkpoint/data_3_25_all/mobilenetv2/class9/224/555/' # 18 change_lr12 milestones brightness=0.5 224 crop 240->224 # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class9/320/000/' # 18 change_lr12 milestones brightness=0.5 320 no randcrop # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class9/320/111/' # 18 change_lr12 milestones brightness=0.5 320 crop no randcrop # class7_2 # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class7_2/000/' # 16 milestones brightness=0.5 240->224 # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class7_2/111/' # 16 milestones brightness=0.5 240->224 crop # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class7_2/222/' # change_lr10 brightness=0.5 240->224 crop # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class7_2/333/' # change_lr9 brightness=0.5 240->224 crop # class7_3 # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class7_3/000/' # 16 milestones brightness=0.5 240->224 crop # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class7_3/111/' # change_lr10 brightness=0.5 240->224 crop # class8 # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class8/000/' # 16 milestones brightness=0.5 240->224 crop # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class8/111/' # change_lr12 brightness=0.5 240->224 crop # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class8/222/' # 16 milestones brightness=0.5 240->224 # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class8/333/' # 18 change_lr12 milestones brightness=0.5 240->224 crop # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class8/444/' # 18 change_lr12 milestones brightness=0.5 240->224 # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class8/555/' # 18 change_lr12 milestones brightness=0.5 240->224 crop no randcrop # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class8/666/' # 18 change_lr12 milestones brightness=0.5 240->224 crop # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class8/777/' # 18 change_lr12 milestones brightness=0.5 240->224 crop nopre # class8 320 # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class8/320/000/' # 18 change_lr12 milestones brightness=0.5 320 crop no randcrop # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class8/320/111/' # 18 change_lr12 milestones brightness=0.5 320 randcrop # class8 160 # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class8/160/000/' # 18 change_lr12 milestones brightness=0.5 160 crop no randcrop # savepath = 'checkpoint/data_3_25_all/mobilenetv2/class8/160/111/' # 18 change_lr12 milestones brightness=0.5 160 nocrop no randcrop # resnext50 # savepath = 'checkpoint/data_3_25_all/resnext50/224/class8/000/' # 18 change_lr12 milestones brightness=0.5 240->224 crop # mobilenetv3_small # savepath = 'checkpoint/data_3_25_all/mobilenetv3_s/224/class8/000/' # 18 change_lr12 milestones brightness=0.5 240->224 crop # savepath = 'checkpoint/data_3_25_all/mobilenetv3_s/224/class8/111/' # 18 change_lr12 milestones brightness=0.5 240->224 # savepath = 'checkpoint/data_3_25_all/mobilenetv3_s/224/class9/000/' # 18 change_lr12 milestones brightness=0.5 240->224 crop # savepath = 'checkpoint/data_3_25_all/mobilenetv3_s/224/class9/111/' # 18 change_lr12 milestones brightness=0.5 240->224 # mobilenetv3_LARGE # savepath = 'checkpoint/data_3_25_all/mobilenetv3_L/224/class8/000/' # 18 change_lr12 milestones brightness=0.5 240->224 crop # savepath = 'checkpoint/data_3_25_all/mobilenetv3_L/224/class8/111/' # 18 change_lr12 milestones brightness=0.5 240->224 # savepath = 'checkpoint/data_3_25_all/mobilenetv3_L/320/class8/000/' # 18 change_lr12 milestones brightness=0.5 320 crop # savepath = 'checkpoint/data_3_25_all/mobilenetv3_L/224/class9/000/' # 18 change_lr12 milestones brightness=0.5 240->224 crop # savepath = 'checkpoint/data_3_25_all/mobilenetv3_L/224/class9/111/' # 18 change_lr12 milestones brightness=0.5 240->224 # savepath = 'checkpoint/data_3_25_all/mobilenetv3_L/320/class9/111/' # 18 change_lr12 milestones brightness=0.5 320 def val(epoch): global best_val_acc net.eval() test_loss = 0 correct = 0 total = 0 with torch.no_grad(): for batch_idx, (inputs, targets) in enumerate(val_dataloader): inputs, targets = inputs.to(device), targets.to(device) outputs = net(inputs) loss = criterion(outputs, targets) test_loss += loss.item() _, predicted = outputs.max(1) total += targets.size(0) correct += predicted.eq(targets).sum().item() average_loss = test_loss / (batch_idx + 1) test_acc = correct / total progress_bar(batch_idx, len(val_dataloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)' % (average_loss, 100. * test_acc, correct, total)) acc = 100. * correct / total if acc >= best_val_acc: print('Saving..') state = { 'net': net.state_dict(), 'acc': acc, 'epoch': epoch, } if not os.path.isdir(savepath): # os.mkdir(savepath) os.makedirs(savepath) print("best_acc:{:.4f}".format(acc)) # torch.save(state, savepath + 'mobilenetv2_1_imagenet_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'ghostnet_1_imagenet_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mnext_1_imagenet_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'my_mnextv2_1_imagenet_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_1_my_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'shufflenetv2_1_my_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'shufflenetv2_05_my_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'ghostnet_1_my_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mnext_1_my_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_1_kg1_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_1_kg2_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'resnet50_1_kg2_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mnext_1_kg2_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv3s_1_kg2_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_1_aucv2_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mnext_1_aucv2_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv3s_1_aucv2_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'resnet50_aucv2_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_1_aucv1_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mnext_1_aucv1_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv3s_1_aucv1_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'resnet50_1_aucv1_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'ghostnet_1_aucv1_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_1_12_23_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_cbam_1_12_23_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mnext_1_12_23_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_1_crop_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_1_c6_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_1_c7_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_224_crop_72_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_224_crop_73_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_224_crop_8_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_160_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_160_crop_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_224_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_224_crop_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_320_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_320_crop_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_320_crop_8_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_160_crop_8_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_160_8_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_224_8_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_224_9_acc={:.4f}.pth'.format(acc)) torch.save(state, savepath + 'mobilenetv2_224_9_crop_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mobilenetv2_320_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'resnext50_224_8_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mv3_s_224_8_crop_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mv3_s_224_crop_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mv3_s_224_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mv3_l_224_8_crop_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mv3_s_224_9_crop_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mv3_s_224_9_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mv3_s_224_8_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mv3_l_224_8_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mv3_l_224_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mv3_l_224_crop_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mv3_l_224_9_acc={:.4f}.pth'.format(acc)) # torch.save(state, savepath + 'mv3_l_224_9_crop_acc={:.4f}.pth'.format(acc)) best_val_acc = acc return average_loss, test_acc # B0/000 kaggle dataset without val # B0/111 kaggle dataset with val # B0/222 my dataset with normal transfrom # B0/333 my dataset with randomcrop random flip # B0/444 my dataset with random flip # B0/555 my dataset with random flip with val # ghost_net/000 kaggle dataset with random flip w = 1 # ghost_net/111 kaggle dataset with random flip w = 0.5 # ghost_net/222 kaggle dataset with random flip w = 0.3 # ghost_net/333 my dataset with random flip w = 0.5 # ghost_net/444 my dataset with random flip w = 1 # ghost_net/555 my dataset no val with random flip w = 0.5 # ghost_net/666 kaggle dataset with random flip w = 0.1 # ghost_net/777 kaggle dataset with random flip w = 0.1 # mobilenetv2/000 my dataset with random flip # mobilenetv2/111 kaggle dataset with random flip # mobilenetv2/111 kaggle dataset with random flip w =0.5 # resnet18/000 my dataset with random flip # resnet18/111 kaggle dataset with random flip # resnext50/000 my dataset with random flip # resnext50/111 kaggle dataset with random flip # resnext50/222 my dataset with random flip with 111 pretrain # resnext50/333 my dataset with random flip without pretrain # resnext50/444 kaggle dataset with random flip without pretrain def main(epoches=epoches): x = [] lrs = [] train_loss = [] test_loss = [] train_acc = [] test_acc = [] start_time = time.time() for epoch in range(start_epoch, start_epoch + epoches): train_l, train_a, lr = train(epoch) test_l, test_a = val(epoch) x.append(epoch) lrs.append(lr) train_loss.append(train_l) test_loss.append(test_l) train_acc.append(train_a) test_acc.append(test_a) print("epoch={}/{},lr={},train_loss={:.3f},test_loss={:.3f},train_acc={:.3f},test_acc={:.3f}" .format(epoch + 1, epoches, lr, train_l, test_l, train_a, test_a)) # # # earlystop # if lr < 1e-4-1e-5: # break # if lr < 1e-6 - 1e-7: # break print("total train time ={}".format(format_time(time.time() - start_time))) fig = plt.figure(figsize=(16, 9)) sub1 = fig.add_subplot(1, 3, 1) sub1.set_title("loss") sub1.plot(x, train_loss, label="train_loss") sub1.plot(x, test_loss, label="test_loss") plt.legend() sub2 = fig.add_subplot(1, 3, 2) sub2.set_title("acc") sub2.plot(x, train_acc, label="train_acc") sub2.plot(x, test_acc, label="test_acc") plt.legend() sub3 = fig.add_subplot(1, 3, 3) sub3.set_title("lr") sub3.plot(x, lrs, label="lr") plt.title(savepath) plt.legend() # 保存图片 plt.savefig(savepath + 'learing.jpg') plt.show() def net_test(): # num_classes = 6 # num_classes = 7 # num_classes = 8 num_classes = 9 net = models.mobilenet_v2(pretrained=False, num_classes=num_classes, width_mult=1.0) # # model_path = r"checkpoint/data_11_16/mobilenetv2/pre/555/mobilenetv2_1_my_acc=96.1749.pth" # crop 160=0.7486338797814208 # # model_path = r"checkpoint/data_11_16/mobilenetv2/pre/222/mobilenetv2_1_my_acc=92.3497.pth" # 160=0.5846994535519126 # # model_path = r"checkpoint/data_11_16/mobilenetv2/pre/666/mobilenetv2_1_my_acc=95.6284.pth" # 160=0.9562841530054644 224=0.7486338797814208 # # model_path = r"checkpoint/data_11_16/mobilenetv2/pre/777/mobilenetv2_1_my_acc=95.6284.pth" # 160=0.8142076502732241 # # model_path = r"checkpoint/data_11_16/mobilenetv2/pre/0/111/mobilenetv2_1_my_acc=93.4426.pth" # crop=0.9398907103825137 crop_160=0.907103825136612 # # # model_path = r"checkpoint/data_12_23/mobilenetv2/222/mobilenetv2_1_12_23_acc=93.6898.pth" # # model_path = r"checkpoint/data_12_23/mobilenetv2/333/mobilenetv2_1_12_23_acc=89.9061.pth" # model_path = r"checkpoint/data_12_23/mobilenetv2/888/mobilenetv2_1_12_23_acc=91.6275.pth" # model_path = r"checkpoint/data_12_23/mobilenetv2/0/222/mnext_1_12_23_acc=88.2629.pth" # mobilenetv2 # model_path = r"checkpoint/data_12_23/mobilenetv2/0/333/mobilenetv2_1_12_23_acc=84.8983.pth" # model_path = r"checkpoint/data_12_23/mobilenetv2/crop/333/mobilenetv2_1_crop_acc=90.8059.pth" # model_path = r"checkpoint/data_12_23/mobilenetv2/crop/444/mobilenetv2_1_crop_acc=90.9233.pth" # model_path = r"checkpoint/data_3_25_all/mobilenetv2/111/mobilenetv2_224_acc=85.6154.pth" model_path = r"checkpoint/data_3_25_all/mobilenetv2/class8/666/mobilenetv2_224_8_acc=90.5516.pth" model_path = r"checkpoint/data_3_25_all/mobilenetv2/555/mobilenetv2_224_crop_acc=89.1415.pth" model_path = r"checkpoint/data_3_25_all/mobilenetv2/111/mobilenetv2_224_acc=85.6154.pth" # class6 # model_path = r"checkpoint/data_12_23/class6/mobilenetv2/222/mobilenetv2_1_c6_acc=95.1313.pth" # class7 # model_path = r"checkpoint/data_12_23/class7/mobilenetv2/000/mobilenetv2_1_c7_acc=92.0188.pth" # net = mnext(num_classes=num_classes, width_mult=1.) # model_path = r"checkpoint/data_12_23/mnext/000/mnext_1_12_23_acc=92.1753.pth" # 加载模型权重,忽略不同 model_dict = net.state_dict() checkpoint = torch.load(model_path, map_location=device) pretrained_dict = checkpoint["net"] pretrained_dict = {k: v for k, v in pretrained_dict.items() if np.shape(model_dict[k]) == np.shape(v)} model_dict.update(pretrained_dict) net.load_state_dict(model_dict) print("loaded model with acc:{}".format(checkpoint["acc"])) net.to(device) net.eval() test_loss = 0 correct = 0 total = 0 test_transform = transforms.Compose([ transforms.Resize((224, 224)), # transforms.Resize((160, 160)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) # test_dataset = MyDataset("data/txt/116_testcrop224.txt", test_transform) # test_dataset = MyDataset("data/txt/119_testcrop224.txt", test_transform) # test_dataset = MyDataset("data/test224_11_16.txt", test_transform) # test_dataset = MyDataset("data/test_116_119.txt", test_transform) # test_dataset = MyDataset("data/train224_11_16.txt", test_transform) # test_dataset = MyDataset("data/txt/12_23_1_test224.txt", test_transform) # test_dataset = MyDataset("data/txt/12_23_2_test224.txt", test_transform) # test_dataset = MyDataset("data/txt/12_23_12_test224.txt", test_transform) # test_dataset = MyDataset("data/txt/12_23_12_addpre_test224.txt", test_transform) # test_dataset = MyDataset("data/txt/12_23_12_addpre_test_crop224.txt", test_transform) # test_dataset = MyDataset("data/txt_raw/total_test.txt", test_transform) # class6 # test_dataset = MyDataset("data/txt6/12_23_12_addpre_test224_6.txt", test_transform) # test_dataset = MyDataset("data/txt6/12_23_12_addpre_test224_addcrop_6.txt", test_transform) # class7 # test_dataset = MyDataset("data/txt7/12_23_12_addpre_test224_7.txt", test_transform) # test_dataset = MyDataset("data/txt7/12_23_12_addpre_test224_addcrop_7.txt", test_transform) # class8 # all class8 # test_dataset = MyDataset("data/txt_raw/total_test_8.txt", test_transform) # test_dataset = MyDataset("data/txt_raw_crop/total_test_crop_8.txt", test_transform) # test_dataset = MyDataset("data/txt_raw_crop/total_test_crop.txt", test_transform) test_dataset = MyDataset("data/txt_raw/total_test.txt", test_transform) test_dataloader = DataLoader(dataset=test_dataset, batch_size=64, shuffle=True, num_workers=0) # Confusion_matrix cm = np.zeros((num_classes, num_classes), dtype=np.int) with torch.no_grad(): for batch_idx, (inputs, targets) in enumerate(test_dataloader): inputs, targets = inputs.to(device), targets.to(device) outputs = net(inputs) loss = criterion(outputs, targets) test_loss += loss.item() _, predicted = outputs.max(1) # print(targets, predicted) for i in range(targets.shape[0]): cm[targets[i]][predicted[i]] += 1 total += targets.size(0) correct += predicted.eq(targets).sum().item() average_loss = test_loss / (batch_idx + 1) test_acc = correct / total progress_bar(batch_idx, len(test_dataloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)' % (average_loss, 100. * test_acc, correct, total)) # print(average_loss, test_acc) print("test_acc: ", test_acc) if num_classes == 9: labels = ["正常", "侧视", "喝水", "吸烟", "操作中控", "玩手机", "侧身拿东西", "整理仪容", "接电话"] elif num_classes == 6: labels = ["正常", "喝水", "吸烟", "操作中控", "玩手机", "接电话"] elif num_classes == 7: labels = ["正常", "喝水", "吸烟", "操作中控", "玩手机", "接电话", "其他"] else: labels = ["正常", "侧视", "喝水", "吸烟", "操作中控", "玩手机", "侧身拿东西", "接电话"] print(labels) print("row:target col:predict") print(cm) true_label = np.zeros((num_classes, ), dtype=np.int) predicted_label = np.zeros((num_classes,), dtype=np.int) total = 0 for i in range(num_classes): for j in range(num_classes): true_label[i] += cm[i][j] predicted_label[i] += cm[j][i] total += cm[i][j] print("true label:", true_label) print("predicted label:", predicted_label) TP = np.zeros((num_classes, ), dtype=np.int) FP = np.zeros((num_classes,), dtype=np.int) FN = np.zeros((num_classes,), dtype=np.int) TN = np.zeros((num_classes,), dtype=np.int) Accuracy = np.zeros((num_classes,), dtype=np.float) Precision = np.zeros((num_classes,), dtype=np.float) Recall = np.zeros((num_classes,), dtype=np.float) F1 = np.zeros((num_classes,), dtype=np.float) for i in range(num_classes): TP[i] = cm[i][i] FP[i] = true_label[i] - TP[i] FN[i] = predicted_label[i] - TP[i] TN[i] = total - true_label[i] - FN[i] Accuracy[i] = (TP[i]+TN[i])/total Precision[i] = TP[i]/predicted_label[i] Recall[i] = TP[i]/true_label[i] F1[i] = Precision[i]*Recall[i]/(Precision[i]+Recall[i])*2 print("TP:", TP) print("TN:", TN) print("FP:", FP) print("FN:", FN) print("Accuracy:", Accuracy) print("Precision:", Precision) print("Recall:", Recall) print("F1:", F1) dict = {} dict["准确率"] = Accuracy.tolist() # 样本被分类正确的概率, 包括TP和TF dict["精确率"] = Precision.tolist() # 样本识别正确的概率, dict["召回率"] = Recall.tolist() # 样本被正确识别出的概率,检出率 dict["F1-score"] = F1.tolist() test_path = os.path.dirname(model_path) with open(os.path.join(test_path, "test.json"), "w", encoding='utf-8') as f: json.dump(dict, f) # 保存excel df =
pd.DataFrame(dict, index=labels)
pandas.DataFrame
import os import pandas as pd import gavia.time as gavtime from gavia.version import __version__ import sys def getlogs(dir,logtype): ''' get list of logs for camera ''' files = [] loglist = os.listdir(dir) for log in loglist: if logtype in log: files.append(log) # for gps, remove gpsfix if logtype =='gps': delfiles = [] for i in range(len(files)): if 'gpsfix' in files[i]: delfiles.append(files[i]) for f in delfiles: del files[files.index(f)] return sorted(files) def parsedatetime(dateNtime): ''' example: 2019 04 16 10:59:20.619 ''' # year,month,day,time = dateNtime.split(' ') # this doesn't always work # hour,minute,secondsNmilli = time.split(':') # second,millisecond = secondsNmilli.split('.') year = dateNtime[0:4] month = dateNtime[5:7] day = dateNtime[8:10] hour = dateNtime[11:13] minute = dateNtime[14:16] second = dateNtime[17:19] millisecond = dateNtime[20:23] return int(year),int(month),int(day),int(hour),int(minute),int(second),int(millisecond) def matchmerge(df1,df1time,df2,df2time,df1cols=[None], df2cols=[None],df3=None,df3cols=[None], df3time=None): ''' Match multiple log files by specified time header such as epoch time. If an exact match does not exist, use the closest sample entry. Parameters ---------- df1 : DataFrame DataFrame to add to from other DataFrames dftime : string Column header in df1 used to match time of sample, this cannot be removed even if it is not contained in df1cols df2 : DataFrame DataFrame with entries to add to df1 according to closest matching time entry df1cols : list of strings List of strings containing df1 headers to keep in the DataFrame, Default [None] keeps all columns Returns ------- Modified df1 DataFrame with data from df2 (and df3 if df3 is a valid dataframe) ''' # Assign headers for new DataFrame if None in df1cols: df1cols = list(df1) if None in df2cols: df2cols = list(df2) if ((df3 is not None) and (None in df3cols)):df3cols = list(df3) if ((df3 is not None) and (df3time == None)): raise ValueError('Need to specify df3time when using df3 parameter') # Remove 'software_name', 'software_version', 'software_url' from df1cols for h in range(len(df1cols)): if df1cols[h] in ['software_name', 'software_version', 'software_url']: del df1cols[h] dfmcols = df1cols.copy() # add any aditional values and remove any replicates from df2cols dellist = [] for x in range(len(df2cols)): # change to one-liner if ((df2cols[x] not in dfmcols) and (df2cols[x] not in ['software_name', 'software_version', 'software_url'])): dfmcols.append(df2cols[x]) else: dellist.append(df2cols[x]) # remove replicates from df2 for i in dellist: del df2cols[df2cols.index(i)] # add any aditional values and remove any replicates from df3cols if used if df3 is not None: dellist = [] for x in range(len(df3cols)): # change to one-liner if ((df3cols[x] not in dfmcols) and (df3cols[x] not in ['software_name', 'software_version', 'software_url'])): dfmcols.append(df3cols[x]) else: dellist.append(df3cols[x]) # remove replicates from df2 for i in dellist: del df3cols[df3cols.index(i)] # Add 'software_name', 'software_version', 'software_url' from dfmcols dfmcols.extend(['software_name', 'software_version', 'software_url']) # New DataFrame dfm = pd.DataFrame(columns=dfmcols) for i in range(len(df1)): row = [] if pd.isna(df1.loc[i][df1time]): continue # skip nan values # Add to row from df1 for df1_head in df1cols: row.append(df1.loc[i][df1_head]) # df2 - index of smallest difference df2idx = abs(df2[df2time] - df1.loc[i][df1time]).sort_values().index[0] # Add to row from df2 for df2_head in df2cols: row.append(df2.loc[df2idx][df2_head]) if df3 is not None: # df3 - index of smallest difference df3idx = abs(df3[df3time] - df1.loc[i][df1time]).sort_values().index[0] # Add to row from df3 for df3_head in df3cols: row.append(df3.loc[df3idx][df3_head]) # Add 'software_name', 'software_version', 'software_url' to row row.extend(['gavia',__version__,'github.com/brett-hosking/gavia']) # Add row to dataframe dfm.loc[len(dfm)] = row return dfm def timeshift(df,shift): ''' Introduce a time shift (in seconds) in log file and recalculate human-readbale times. Parameters ---------- df : DataFrame gavia log with timestamp header and values given in seconds shift : float time shift in seconds Returns ------- New log as a DataFrame with adjusted time fields ''' headers = list(df) if 'timestamp' not in headers:raise ValueError('not a valid gavia logfile; the log should contain a timestamp header') # Shift the timestamp header df['timestamp'] = df['timestamp'] + shift if 'capture-time' in headers: # Shift the timestamp header df['capture-time'] = df['capture-time'] + shift # New DataFrame dfc =
pd.DataFrame(columns=headers)
pandas.DataFrame
# -*- coding: utf-8 -*- import requests import json import pandas as pd from io import StringIO import numpy as np import time # timezones={} #function = 'TIME_SERIES_INTRADAY' apii = 'https://www.alphavantage.co/query?function={function}&symbol={symbol}&interval={interval}&outputsize=full&datatype=csv&apikey=' apid = 'https://www.alphavantage.co/query?function={function}&symbol={symbol}&outputsize=full&datatype=csv&apikey=' #https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=ASML&interval=1min&outputsize=compact&datatype=csv&time_period=0&apikey= sector = 'https://www.alphavantage.co/query?function=SECTOR&datatype=csv&apikey=' s_type = ['close','high','low']#,'open'] ma_types = [0,1,2,3,4,5,6,7,8] #Moving average type By default, matype=0. INT 0 = SMA, 1 = EMA, 2 = Weighted Moving Average (WMA), 3 = Double Exponential Moving Average (DEMA), 4 = Triple Exponential Moving Average (TEMA), 5 = Triangular Moving Average (TRIMA), 6 = T3 Moving Average, 7 = Kaufman Adaptive Moving Average (KAMA), 8 = MESA Adaptive Moving Average (MAMA). indicator_dict = { 'sma':'https://www.alphavantage.co/query?function=SMA&symbol={symbol}&interval={interval}&time_period={time_period}&series_type={series_type}&datatype=csv&apikey=', 'ema':'https://www.alphavantage.co/query?function=EMA&symbol={symbol}&interval={interval}&time_period={time_period}&series_type={series_type}&datatype=csv&apikey=', 'tema':'https://www.alphavantage.co/query?function=TEMA&symbol={symbol}&interval={interval}&time_period={time_period}&series_type={series_type}&datatype=csv&apikey=', 'macd':'https://www.alphavantage.co/query?function=MACD&symbol={symbol}&interval={interval}&series_type=close&fastperiod=12&slowperiod=26&signalperiod=9&datatype=csv&apikey=', 'macdext':'https://www.alphavantage.co/query?function=MACDEXT&symbol={symbol}&interval={interval}&series_type={series_type}&fastperiod={fastperiod}&slowperiod={slowperiod}&signalperiod={signalperiod}&fastmatype={fastmatype}&slowmatype={slowmatype}&signalmatype={signalmatype}&datatype=csv&apikey=', 'stoch':'https://www.alphavantage.co/query?function=STOCH&symbol={symbol}&interval={interval}&fastkperiod={fastkperiod}&slowkperiod={slowkperiod}&slowdperiod={slowdperiod}&slowkmatype={slowkmatype}&slowdmatype={slowdmatype}&datatype=csv&apikey=', 'stochf':'https://www.alphavantage.co/query?function=STOCHF&symbol={symbol}&interval={interval}&fastkperiod={fastkperiod}&fastdperiod={fastdperiod}&fastdmatype={fastdmatype}&datatype=csv&apikey=', 'rsi':'https://www.alphavantage.co/query?function=RSI&symbol={symbol}&interval={interval}&time_period={time_period}&series_type={series_type}&datatype=csv&apikey=', 'stochrsi':'https://www.alphavantage.co/query?function=STOCHRSI&symbol={symbol}&interval={interval}&time_period={time_period}&series_type={series_type}&fastkperiod={fastkperiod}&fastdperiod={fastdperiod}&fastdmatype={fastdmatype}&datatype=csv&apikey=', 'willr':'https://www.alphavantage.co/query?function=WILLR&symbol={symbol}&interval={interval}&time_period={time_period}&datatype=csv&apikey=', 'adx':'https://www.alphavantage.co/query?function=ADX&symbol={symbol}&interval={interval}&time_period={time_period}&datatype=csv&apikey=', 'adxr':'https://www.alphavantage.co/query?function=ADXR&symbol={symbol}&interval={interval}&time_period={time_period}&datatype=csv&apikey=', 'apo':'https://www.alphavantage.co/query?function=APO&symbol={symbol}&interval={interval}&series_type={series_type}&fastperiod={fastperiod}&slowperiod={slowperiod}&matype={matype}&datatype=csv&apikey=', 'ppo':'https://www.alphavantage.co/query?function=PPO&symbol={symbol}&interval={interval}&series_type={series_type}&fastperiod={fastperiod}&slowperiod={slowperiod}&matype={matype}&datatype=csv&apikey=', 'mom':'https://www.alphavantage.co/query?function=MOM&symbol={symbol}&interval={interval}&time_period={time_period}&series_type={series_type}&datatype=csv&apikey=', 'bop':'https://www.alphavantage.co/query?function=BOP&symbol={symbol}&interval={interval}&datatype=csv&apikey=', 'cci':'https://www.alphavantage.co/query?function=CCI&symbol={symbol}&interval={interval}&time_period={time_period}&datatype=csv&apikey=', 'cmo':'https://www.alphavantage.co/query?function=CMO&symbol={symbol}&interval={interval}&time_period={time_period}&series_type={series_type}&datatype=csv&apikey=', 'roc':'https://www.alphavantage.co/query?function=ROC&symbol={symbol}&interval={interval}&time_period={time_period}&series_type={series_type}&datatype=csv&apikey=', 'rocr':'https://www.alphavantage.co/query?function=ROCR&symbol={symbol}&interval={interval}&time_period={time_period}&series_type={series_type}&datatype=csv&apikey=', 'aroon':'https://www.alphavantage.co/query?function=AROON&symbol={symbol}&interval={interval}&time_period={time_period}&datatype=csv&apikey=', 'aroonosc':'https://www.alphavantage.co/query?function=AROONOSC&symbol={symbol}&interval={interval}&time_period={time_period}&datatype=csv&apikey=', 'mfi':'https://www.alphavantage.co/query?function=MFI&symbol={symbol}&interval={interval}&time_period={time_period}&datatype=csv&apikey=', 'trix':'https://www.alphavantage.co/query?function=TRIX&symbol={symbol}&interval={interval}&time_period={time_period}&series_type={series_type}&datatype=csv&apikey=', 'ultosc':'https://www.alphavantage.co/query?function=ULTOSC&symbol={symbol}&interval={interval}&timeperiod1={timeperiod1}&timeperiod2={timeperiod2}&timeperiod3={timeperiod3}&datatype=csv&apikey=', 'dx':'https://www.alphavantage.co/query?function=DX&symbol={symbol}&interval={interval}&time_period={time_period}&datatype=csv&apikey=', 'minus_di':'https://www.alphavantage.co/query?function=MINUS_DI&symbol={symbol}&interval={interval}&time_period={time_period}&datatype=csv&apikey=', 'plus_di':'https://www.alphavantage.co/query?function=PLUS_DI&symbol={symbol}&interval={interval}&time_period={time_period}&datatype=csv&apikey=', 'minus_dm':'https://www.alphavantage.co/query?function=MINUS_DM&symbol={symbol}&interval={interval}&time_period={time_period}&datatype=csv&apikey=', 'plus_dm':'https://www.alphavantage.co/query?function=PLUS_DM&symbol={symbol}&interval={interval}&time_period={time_period}&datatype=csv&apikey=', 'bbands':'https://www.alphavantage.co/query?function=BBANDS&symbol={symbol}&interval={interval}&time_period={time_period}&series_type={series_type}&nbdevup={nbdevup}&nbdevdn={nbdevdn}&matype={matype}&datatype=csv&apikey=', 'midpoint':'https://www.alphavantage.co/query?function=MIDPOINT&symbol={symbol}&interval={interval}&time_period={time_period}&series_type={series_type}&datatype=csv&apikey=', 'midprice':'https://www.alphavantage.co/query?function=MIDPRICE&symbol={symbol}&interval={interval}&time_period={time_period}&datatype=csv&apikey=', 'sar':'https://www.alphavantage.co/query?function=SAR&symbol={symbol}&interval={interval}&acceleration={acceleration}&maximum={maximum}&datatype=csv&apikey=', 'trange':'https://www.alphavantage.co/query?function=TRANGE&symbol={symbol}&interval={interval}&datatype=csv&apikey=', 'atr':'https://www.alphavantage.co/query?function=ATR&symbol={symbol}&interval={interval}&time_period={time_period}&datatype=csv&apikey=', 'natr':'https://www.alphavantage.co/query?function=NATR&symbol={symbol}&interval={interval}&time_period={time_period}&datatype=csv&apikey=', 'ad':'https://www.alphavantage.co/query?function=AD&symbol={symbol}&interval={interval}&datatype=csv&apikey=', 'adosc':'https://www.alphavantage.co/query?function=ADOSC&symbol={symbol}&interval={interval}&fastperiod={fastperiod}&slowperiod={slowperiod}&datatype=csv&apikey=', 'obv':'https://www.alphavantage.co/query?function=OBV&symbol={symbol}&interval={interval}&datatype=csv&apikey=', 'ht_trendline':'https://www.alphavantage.co/query?function=HT_TRENDLINE&symbol={symbol}&interval={interval}&series_type={series_type}&datatype=csv&apikey=', 'ht_sine':'https://www.alphavantage.co/query?function=HI_SINE&symbol={symbol}&interval={interval}&series_type={series_type}&datatype=csv&apikey=', 'ht_trendmode':'https://www.alphavantage.co/query?function=HT_TRENDMODE&symbol={symbol}&interval={interval}&series_type={series_type}&datatype=csv&apikey=', 'ht_dcperiod':'https://www.alphavantage.co/query?function=HT_DCPERIOD&symbol={symbol}&interval={interval}&series_type={series_type}&datatype=csv&apikey=', 'ht_dcphase':'https://www.alphavantage.co/query?function=HT_DCPHASE&symbol={symbol}&interval={interval}&series_type={series_type}&datatype=csv&apikey=', 'ht_dcphasor':'https://www.alphavantage.co/query?function=HT_DCPHASOR&symbol={symbol}&interval={interval}&series_type={series_type}&datatype=csv&apikey=' } def moving_a(ma,symbol,interval): api = indicator_dict[ma] ma_range = [5,10,15,20,35,50,65,100,125,200,250] #125 out_df = pd.DataFrame() first = True for t in ma_range: for s in s_type: indicator = requests.get(api.format(symbol=symbol,interval=interval,time_period = t,series_type=s)) time.sleep(11.8) fixed = StringIO(indicator.content.decode('utf-8')) #pandas.read_csv needs a filepath for strings use StringIO from IO to convert Str to filepath if first: out_df = pd.read_csv(fixed) first = False elif first != True: indi_df = pd.read_csv(fixed) out_df = pd.merge(out_df,indi_df,on='time',how="inner") return out_df def macdext_get(macd,symbol, interval):#,types=False,time_period=False): out_df = pd.DataFrame() macd_range = [[5,10,3],[10,20,7],[12,26,9],[15,35,11]] api = indicator_dict[macd] macd_ma = 1 first=True for i in macd_range: for s in s_type: indicator = requests.get(api.format(symbol=symbol,interval=interval,series_type=s,fastperiod=i[0],slowperiod=i[1],signalperiod=i[2],fastmatype=ma_types[1],slowmatype=ma_types[1],signalmatype=ma_types[1])) time.sleep(11.8) fixed = StringIO(indicator.content.decode('utf-8')) #pandas.read_csv needs a filepath for strings use StringIO from IO to convert Str to filepath if first: out_df = pd.read_csv(fixed) first = False elif first != True: indi_df = pd.read_csv(fixed) out_df = pd.merge(out_df,indi_df,on='time',how="inner") return out_df def stoch_get(stoch,symbol,interval): slowd = 3 slowk = 3 fastk = 5 fastd = 3 stoch_ma = 1 #EMA api = indicator_dict[stoch] if stoch == 'stoch': indicator = requests.get(api.format(symbol=symbol,interval=interval,fastkperiod=fastk,slowkperiod=slowk,slowdperiod=slowd,slowkmatype=stoch_ma,slowdmatype=stoch_ma)) time.sleep(11.8) fixed = StringIO(indicator.content.decode('utf-8')) #pandas.read_csv needs a filepath for strings use StringIO from IO to convert Str to filepath indi_df = pd.read_csv(fixed) return indi_df elif stoch == 'stochf': indicator = requests.get(api.format(symbol=symbol,interval=interval,fastkperiod=fastk,fastdperiod=fastd,fastdmatype=stoch_ma)) time.sleep(11.8) fixed = StringIO(indicator.content.decode('utf-8')) #pandas.read_csv needs a filepath for strings use StringIO from IO to convert Str to filepath indi_df = pd.read_csv(fixed) return indi_df def rsi_get(rsi,symbol,interval): out_df = pd.DataFrame() rsi_period = [7,11,14,21] api = indicator_dict[rsi] first = True for t in rsi_period: for s in s_type: indicator = requests.get(api.format(symbol=symbol,interval=interval,time_period = t,series_type=s)) time.sleep(11.8) fixed = StringIO(indicator.content.decode('utf-8')) #pandas.read_csv needs a filepath for strings use StringIO from IO to convert Str to filepath if first: out_df = pd.read_csv(fixed) first = False elif first != True: indi_df = pd.read_csv(fixed) out_df = pd.merge(out_df,indi_df,on='time',how="inner") return out_df def stochrsi_get (indicator,symbol,interval): api = indicator_dict[indicator] fastk = 5 fastd = 3 fastma = 1 stype = 'close' rsi_period = [7,11,14,21] first = True for t in rsi_period: for s in s_type: indicator = requests.get(api.format(symbol=symbol,interval=interval,time_period = t,series_type=s,fastkperiod=fastk,fastdperiod=fastd,fastdmatype=fastma)) time.sleep(11.8) fixed = StringIO(indicator.content.decode('utf-8')) #pandas.read_csv needs a filepath for strings use StringIO from IO to convert Str to filepath if first: out_df = pd.read_csv(fixed) first = False elif first != True: indi_df = pd.read_csv(fixed) out_df = pd.merge(out_df,indi_df,on='time',how="inner") return out_df def adx_get(indicator,symbol,interval): api = indicator_dict[indicator] adx_period = [7,11,14,21] first = True for t in adx_period: indicator = requests.get(api.format(symbol=symbol,interval=interval,time_period = t)) time.sleep(11.8) fixed = StringIO(indicator.content.decode('utf-8')) #pandas.read_csv needs a filepath for strings use StringIO from IO to convert Str to filepath if first: out_df =
pd.read_csv(fixed)
pandas.read_csv
# Licensed to Modin Development Team under one or more contributor license agreements. # See the NOTICE file distributed with this work for additional information regarding # copyright ownership. The Modin Development Team licenses this file to you 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. import pytest import numpy as np import pandas from pandas.errors import ParserWarning import pandas._libs.lib as lib from pandas.core.dtypes.common import is_list_like from collections import OrderedDict from modin.db_conn import ModinDatabaseConnection, UnsupportedDatabaseException from modin.config import TestDatasetSize, Engine, StorageFormat, IsExperimental from modin.utils import to_pandas from modin.pandas.utils import from_arrow import pyarrow as pa import os import sys import shutil import sqlalchemy as sa import csv import tempfile from .utils import ( check_file_leaks, df_equals, json_short_string, json_short_bytes, json_long_string, json_long_bytes, get_unique_filename, io_ops_bad_exc, eval_io_from_str, dummy_decorator, create_test_dfs, COMP_TO_EXT, teardown_test_file, teardown_test_files, generate_dataframe, ) if StorageFormat.get() == "Omnisci": from modin.experimental.core.execution.native.implementations.omnisci_on_native.test.utils import ( eval_io, align_datetime_dtypes, ) else: from .utils import eval_io if StorageFormat.get() == "Pandas": import modin.pandas as pd else: import modin.experimental.pandas as pd try: import ray EXCEPTIONS = (ray.exceptions.WorkerCrashedError,) except ImportError: EXCEPTIONS = () from modin.config import NPartitions NPartitions.put(4) DATASET_SIZE_DICT = { "Small": 64, "Normal": 2000, "Big": 20000, } # Number of rows in the test file NROWS = DATASET_SIZE_DICT.get(TestDatasetSize.get(), DATASET_SIZE_DICT["Small"]) TEST_DATA = { "col1": [0, 1, 2, 3], "col2": [4, 5, 6, 7], "col3": [8, 9, 10, 11], "col4": [12, 13, 14, 15], "col5": [0, 0, 0, 0], } def assert_files_eq(path1, path2): with open(path1, "rb") as file1, open(path2, "rb") as file2: file1_content = file1.read() file2_content = file2.read() if file1_content == file2_content: return True else: return False def setup_clipboard(row_size=NROWS): df = pandas.DataFrame({"col1": np.arange(row_size), "col2": np.arange(row_size)}) df.to_clipboard() def parquet_eval_to_file(modin_obj, pandas_obj, fn, extension, **fn_kwargs): """ Helper function to test `to_parquet` method. Parameters ---------- modin_obj : pd.DataFrame A Modin DataFrame or a Series to test `to_parquet` method. pandas_obj: pandas.DataFrame A pandas DataFrame or a Series to test `to_parquet` method. fn : str Name of the method, that should be tested. extension : str Extension of the test file. """ unique_filename_modin = get_unique_filename(extension=extension) unique_filename_pandas = get_unique_filename(extension=extension) try: getattr(modin_obj, fn)(unique_filename_modin, **fn_kwargs) getattr(pandas_obj, fn)(unique_filename_pandas, **fn_kwargs) pandas_df = pandas.read_parquet(unique_filename_pandas) modin_df = pd.read_parquet(unique_filename_modin) df_equals(pandas_df, modin_df) finally: teardown_test_file(unique_filename_pandas) try: teardown_test_file(unique_filename_modin) except IsADirectoryError: shutil.rmtree(unique_filename_modin) def eval_to_file(modin_obj, pandas_obj, fn, extension, **fn_kwargs): """Helper function to test `to_<extension>` methods. Args: modin_obj: Modin DataFrame or Series to test `to_<extension>` method. pandas_obj: Pandas DataFrame or Series to test `to_<extension>` method. fn: name of the method, that should be tested. extension: Extension of the test file. """ unique_filename_modin = get_unique_filename(extension=extension) unique_filename_pandas = get_unique_filename(extension=extension) try: # parameter `max_retries=0` is set for `to_csv` function on Ray engine, # in order to increase the stability of tests, we repeat the call of # the entire function manually last_exception = None for _ in range(3): try: getattr(modin_obj, fn)(unique_filename_modin, **fn_kwargs) except EXCEPTIONS as exc: last_exception = exc continue break else: raise last_exception getattr(pandas_obj, fn)(unique_filename_pandas, **fn_kwargs) assert assert_files_eq(unique_filename_modin, unique_filename_pandas) finally: teardown_test_files([unique_filename_modin, unique_filename_pandas]) @pytest.mark.usefixtures("TestReadCSVFixture") @pytest.mark.skipif( IsExperimental.get() and StorageFormat.get() == "Pyarrow", reason="Segmentation fault; see PR #2347 ffor details", ) class TestCsv: # delimiter tests @pytest.mark.parametrize("sep", [None, "_", ",", ".", "\n"]) @pytest.mark.parametrize("delimiter", ["_", ",", ".", "\n"]) @pytest.mark.parametrize("decimal", [".", "_"]) @pytest.mark.parametrize("thousands", [None, ",", "_", " "]) def test_read_csv_delimiters( self, make_csv_file, sep, delimiter, decimal, thousands ): unique_filename = get_unique_filename() make_csv_file( filename=unique_filename, delimiter=delimiter, thousands_separator=thousands, decimal_separator=decimal, ) eval_io( fn_name="read_csv", # read_csv kwargs filepath_or_buffer=unique_filename, delimiter=delimiter, sep=sep, decimal=decimal, thousands=thousands, ) # Column and Index Locations and Names tests @pytest.mark.parametrize("header", ["infer", None, 0]) @pytest.mark.parametrize("index_col", [None, "col1"]) @pytest.mark.parametrize("prefix", [None, "_", "col"]) @pytest.mark.parametrize( "names", [lib.no_default, ["col1"], ["c1", "c2", "c3", "c4", "c5", "c6", "c7"]] ) @pytest.mark.parametrize( "usecols", [None, ["col1"], ["col1", "col2", "col6"], [0, 1, 5]] ) @pytest.mark.parametrize("skip_blank_lines", [True, False]) def test_read_csv_col_handling( self, header, index_col, prefix, names, usecols, skip_blank_lines, ): if names is lib.no_default: pytest.skip("some parameters combiantions fails: issue #2312") if header in ["infer", None] and names is not lib.no_default: pytest.skip( "Heterogeneous data in a column is not cast to a common type: issue #3346" ) eval_io( fn_name="read_csv", # read_csv kwargs filepath_or_buffer=pytest.csvs_names["test_read_csv_blank_lines"], header=header, index_col=index_col, prefix=prefix, names=names, usecols=usecols, skip_blank_lines=skip_blank_lines, ) @pytest.mark.parametrize("usecols", [lambda col_name: col_name in ["a", "b", "e"]]) def test_from_csv_with_callable_usecols(self, usecols): fname = "modin/pandas/test/data/test_usecols.csv" pandas_df = pandas.read_csv(fname, usecols=usecols) modin_df = pd.read_csv(fname, usecols=usecols) df_equals(modin_df, pandas_df) # General Parsing Configuration @pytest.mark.parametrize("dtype", [None, True]) @pytest.mark.parametrize("engine", [None, "python", "c"]) @pytest.mark.parametrize( "converters", [ None, { "col1": lambda x: np.int64(x) * 10, "col2": pandas.to_datetime, "col4": lambda x: x.replace(":", ";"), }, ], ) @pytest.mark.parametrize("skipfooter", [0, 10]) def test_read_csv_parsing_1( self, dtype, engine, converters, skipfooter, ): if dtype: dtype = { col: "object" for col in pandas.read_csv( pytest.csvs_names["test_read_csv_regular"], nrows=1 ).columns } eval_io( fn_name="read_csv", check_exception_type=None, # issue #2320 raising_exceptions=None, check_kwargs_callable=not callable(converters), # read_csv kwargs filepath_or_buffer=pytest.csvs_names["test_read_csv_regular"], dtype=dtype, engine=engine, converters=converters, skipfooter=skipfooter, ) @pytest.mark.parametrize("header", ["infer", None, 0]) @pytest.mark.parametrize( "skiprows", [ 2, lambda x: x % 2, lambda x: x > 25, lambda x: x > 128, np.arange(10, 50), np.arange(10, 50, 2), ], ) @pytest.mark.parametrize("nrows", [35, None]) @pytest.mark.parametrize( "names", [ [f"c{col_number}" for col_number in range(4)], [f"c{col_number}" for col_number in range(6)], None, ], ) @pytest.mark.parametrize("encoding", ["latin1", "windows-1251", None]) def test_read_csv_parsing_2( self, make_csv_file, request, header, skiprows, nrows, names, encoding, ): xfail_case = ( StorageFormat.get() == "Omnisci" and header is not None and isinstance(skiprows, int) and names is None and nrows is None ) if xfail_case: pytest.xfail( "read_csv fails because of duplicated columns names - issue #3080" ) if request.config.getoption( "--simulate-cloud" ).lower() != "off" and is_list_like(skiprows): pytest.xfail( reason="The reason of tests fail in `cloud` mode is unknown for now - issue #2340" ) if encoding: unique_filename = get_unique_filename() make_csv_file( filename=unique_filename, encoding=encoding, ) kwargs = { "filepath_or_buffer": unique_filename if encoding else pytest.csvs_names["test_read_csv_regular"], "header": header, "skiprows": skiprows, "nrows": nrows, "names": names, "encoding": encoding, } if Engine.get() != "Python": df = pandas.read_csv(**dict(kwargs, nrows=1)) # in that case first partition will contain str if df[df.columns[0]][df.index[0]] in ["c1", "col1", "c3", "col3"]: pytest.xfail("read_csv incorrect output with float data - issue #2634") eval_io( fn_name="read_csv", check_exception_type=None, # issue #2320 raising_exceptions=None, check_kwargs_callable=not callable(skiprows), # read_csv kwargs **kwargs, ) @pytest.mark.parametrize("true_values", [["Yes"], ["Yes", "true"], None]) @pytest.mark.parametrize("false_values", [["No"], ["No", "false"], None]) @pytest.mark.parametrize("skipfooter", [0, 10]) @pytest.mark.parametrize("nrows", [35, None]) def test_read_csv_parsing_3( self, true_values, false_values, skipfooter, nrows, ): xfail_case = ( (false_values or true_values) and Engine.get() != "Python" and StorageFormat.get() != "Omnisci" ) if xfail_case: pytest.xfail("modin and pandas dataframes differs - issue #2446") eval_io( fn_name="read_csv", check_exception_type=None, # issue #2320 raising_exceptions=None, # read_csv kwargs filepath_or_buffer=pytest.csvs_names["test_read_csv_yes_no"], true_values=true_values, false_values=false_values, skipfooter=skipfooter, nrows=nrows, ) def test_read_csv_skipinitialspace(self): unique_filename = get_unique_filename() str_initial_spaces = ( "col1,col2,col3,col4\n" "five, six, seven, eight\n" " five, six, seven, eight\n" "five, six, seven, eight\n" ) eval_io_from_str(str_initial_spaces, unique_filename, skipinitialspace=True) @pytest.mark.parametrize( "test_case", ["single_element", "single_column", "multiple_columns"], ) def test_read_csv_squeeze(self, request, test_case): if request.config.getoption("--simulate-cloud").lower() != "off": pytest.xfail( reason="Error EOFError: stream has been closed in `modin in the cloud` mode - issue #3329" ) unique_filename = get_unique_filename() str_single_element = "1" str_single_col = "1\n2\n3\n" str_four_cols = "1, 2, 3, 4\n5, 6, 7, 8\n9, 10, 11, 12\n" case_to_data = { "single_element": str_single_element, "single_column": str_single_col, "multiple_columns": str_four_cols, } eval_io_from_str(case_to_data[test_case], unique_filename, squeeze=True) eval_io_from_str( case_to_data[test_case], unique_filename, header=None, squeeze=True ) def test_read_csv_mangle_dupe_cols(self): if StorageFormat.get() == "Omnisci": pytest.xfail( "processing of duplicated columns in OmniSci storage format is not supported yet - issue #3080" ) unique_filename = get_unique_filename() str_non_unique_cols = "col,col,col,col\n5, 6, 7, 8\n9, 10, 11, 12\n" eval_io_from_str(str_non_unique_cols, unique_filename, mangle_dupe_cols=True) # NA and Missing Data Handling tests @pytest.mark.parametrize("na_values", ["custom_nan", "73"]) @pytest.mark.parametrize("keep_default_na", [True, False]) @pytest.mark.parametrize("na_filter", [True, False]) @pytest.mark.parametrize("verbose", [True, False]) @pytest.mark.parametrize("skip_blank_lines", [True, False]) def test_read_csv_nans_handling( self, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, ): eval_io( fn_name="read_csv", # read_csv kwargs filepath_or_buffer=pytest.csvs_names["test_read_csv_nans"], na_values=na_values, keep_default_na=keep_default_na, na_filter=na_filter, verbose=verbose, skip_blank_lines=skip_blank_lines, ) # Datetime Handling tests @pytest.mark.parametrize( "parse_dates", [True, False, ["col2"], ["col2", "col4"], [1, 3]] ) @pytest.mark.parametrize("infer_datetime_format", [True, False]) @pytest.mark.parametrize("keep_date_col", [True, False]) @pytest.mark.parametrize( "date_parser", [None, lambda x: pandas.datetime.strptime(x, "%Y-%m-%d")] ) @pytest.mark.parametrize("dayfirst", [True, False]) @pytest.mark.parametrize("cache_dates", [True, False]) def test_read_csv_datetime( self, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, ): if ( StorageFormat.get() == "Omnisci" and isinstance(parse_dates, list) and ("col4" in parse_dates or 3 in parse_dates) ): pytest.xfail( "In some cases read_csv with `parse_dates` with OmniSci storage format outputs incorrect result - issue #3081" ) raising_exceptions = io_ops_bad_exc # default value if isinstance(parse_dates, dict) and callable(date_parser): # In this case raised TypeError: <lambda>() takes 1 positional argument but 2 were given raising_exceptions = list(io_ops_bad_exc) raising_exceptions.remove(TypeError) eval_io( fn_name="read_csv", check_kwargs_callable=not callable(date_parser), raising_exceptions=raising_exceptions, # read_csv kwargs filepath_or_buffer=pytest.csvs_names["test_read_csv_regular"], parse_dates=parse_dates, infer_datetime_format=infer_datetime_format, keep_date_col=keep_date_col, date_parser=date_parser, dayfirst=dayfirst, cache_dates=cache_dates, ) # Iteration tests @pytest.mark.parametrize("iterator", [True, False]) def test_read_csv_iteration(self, iterator): filename = pytest.csvs_names["test_read_csv_regular"] # Tests __next__ and correctness of reader as an iterator # Use larger chunksize to read through file quicker rdf_reader = pd.read_csv(filename, chunksize=500, iterator=iterator) pd_reader = pandas.read_csv(filename, chunksize=500, iterator=iterator) for modin_df, pd_df in zip(rdf_reader, pd_reader): df_equals(modin_df, pd_df) # Tests that get_chunk works correctly rdf_reader = pd.read_csv(filename, chunksize=1, iterator=iterator) pd_reader = pandas.read_csv(filename, chunksize=1, iterator=iterator) modin_df = rdf_reader.get_chunk(1) pd_df = pd_reader.get_chunk(1) df_equals(modin_df, pd_df) # Tests that read works correctly rdf_reader = pd.read_csv(filename, chunksize=1, iterator=iterator) pd_reader = pandas.read_csv(filename, chunksize=1, iterator=iterator) modin_df = rdf_reader.read() pd_df = pd_reader.read() df_equals(modin_df, pd_df) def test_read_csv_encoding_976(self): file_name = "modin/pandas/test/data/issue_976.csv" names = [str(i) for i in range(11)] kwargs = { "sep": ";", "names": names, "encoding": "windows-1251", } df1 = pd.read_csv(file_name, **kwargs) df2 = pandas.read_csv(file_name, **kwargs) # these columns contain data of various types in partitions # see #1931 for details; df1 = df1.drop(["4", "5"], axis=1) df2 = df2.drop(["4", "5"], axis=1) df_equals(df1, df2) # Quoting, Compression parameters tests @pytest.mark.parametrize("compression", ["infer", "gzip", "bz2", "xz", "zip"]) @pytest.mark.parametrize("encoding", [None, "latin8", "utf16"]) @pytest.mark.parametrize("engine", [None, "python", "c"]) def test_read_csv_compression(self, make_csv_file, compression, encoding, engine): unique_filename = get_unique_filename() make_csv_file( filename=unique_filename, encoding=encoding, compression=compression ) compressed_file_path = ( f"{unique_filename}.{COMP_TO_EXT[compression]}" if compression != "infer" else unique_filename ) eval_io( fn_name="read_csv", # read_csv kwargs filepath_or_buffer=compressed_file_path, compression=compression, encoding=encoding, engine=engine, ) @pytest.mark.parametrize( "encoding", [ None, "ISO-8859-1", "latin1", "iso-8859-1", "cp1252", "utf8", pytest.param( "unicode_escape", marks=pytest.mark.skip( condition=sys.version_info < (3, 9), reason="https://bugs.python.org/issue45461", ), ), "raw_unicode_escape", "utf_16_le", "utf_16_be", "utf32", "utf_32_le", "utf_32_be", "utf-8-sig", ], ) def test_read_csv_encoding(self, make_csv_file, encoding): unique_filename = get_unique_filename() make_csv_file(filename=unique_filename, encoding=encoding) eval_io( fn_name="read_csv", # read_csv kwargs filepath_or_buffer=unique_filename, encoding=encoding, ) @pytest.mark.parametrize("thousands", [None, ",", "_", " "]) @pytest.mark.parametrize("decimal", [".", "_"]) @pytest.mark.parametrize("lineterminator", [None, "x", "\n"]) @pytest.mark.parametrize("escapechar", [None, "d", "x"]) @pytest.mark.parametrize("dialect", ["test_csv_dialect", None]) def test_read_csv_file_format( self, make_csv_file, thousands, decimal, lineterminator, escapechar, dialect, ): if Engine.get() != "Python" and lineterminator == "x": pytest.xfail("read_csv with Ray engine outputs empty frame - issue #2493") elif Engine.get() != "Python" and escapechar: pytest.xfail( "read_csv with Ray engine fails with some 'escapechar' parameters - issue #2494" ) elif Engine.get() != "Python" and dialect: pytest.xfail( "read_csv with Ray engine fails with `dialect` parameter - issue #2508" ) unique_filename = get_unique_filename() if dialect: test_csv_dialect_params = { "delimiter": "_", "doublequote": False, "escapechar": "\\", "quotechar": "d", "quoting": csv.QUOTE_ALL, } csv.register_dialect(dialect, **test_csv_dialect_params) dialect = csv.get_dialect(dialect) make_csv_file(filename=unique_filename, **test_csv_dialect_params) else: make_csv_file( filename=unique_filename, thousands_separator=thousands, decimal_separator=decimal, escapechar=escapechar, line_terminator=lineterminator, ) eval_io( check_exception_type=None, # issue #2320 raising_exceptions=None, fn_name="read_csv", # read_csv kwargs filepath_or_buffer=unique_filename, thousands=thousands, decimal=decimal, lineterminator=lineterminator, escapechar=escapechar, dialect=dialect, ) @pytest.mark.parametrize( "quoting", [csv.QUOTE_ALL, csv.QUOTE_MINIMAL, csv.QUOTE_NONNUMERIC, csv.QUOTE_NONE], ) @pytest.mark.parametrize("quotechar", ['"', "_", "d"]) @pytest.mark.parametrize("doublequote", [True, False]) @pytest.mark.parametrize("comment", [None, "#", "x"]) def test_read_csv_quoting( self, make_csv_file, quoting, quotechar, doublequote, comment, ): # in these cases escapechar should be set, otherwise error occures # _csv.Error: need to escape, but no escapechar set" use_escapechar = ( not doublequote and quotechar != '"' and quoting != csv.QUOTE_NONE ) escapechar = "\\" if use_escapechar else None unique_filename = get_unique_filename() make_csv_file( filename=unique_filename, quoting=quoting, quotechar=quotechar, doublequote=doublequote, escapechar=escapechar, comment_col_char=comment, ) eval_io( fn_name="read_csv", # read_csv kwargs filepath_or_buffer=unique_filename, quoting=quoting, quotechar=quotechar, doublequote=doublequote, escapechar=escapechar, comment=comment, ) # Error Handling parameters tests @pytest.mark.parametrize("warn_bad_lines", [True, False, None]) @pytest.mark.parametrize("error_bad_lines", [True, False, None]) @pytest.mark.parametrize("on_bad_lines", ["error", "warn", "skip", None]) def test_read_csv_error_handling( self, warn_bad_lines, error_bad_lines, on_bad_lines, ): # in that case exceptions are raised both by Modin and pandas # and tests pass raise_exception_case = on_bad_lines is not None and ( error_bad_lines is not None or warn_bad_lines is not None ) if ( not raise_exception_case and Engine.get() not in ["Python", "Cloudpython"] and StorageFormat.get() != "Omnisci" ): pytest.xfail("read_csv doesn't raise `bad lines` exceptions - issue #2500") eval_io( fn_name="read_csv", # read_csv kwargs filepath_or_buffer=pytest.csvs_names["test_read_csv_bad_lines"], warn_bad_lines=warn_bad_lines, error_bad_lines=error_bad_lines, on_bad_lines=on_bad_lines, ) # Internal parameters tests @pytest.mark.parametrize("use_str_data", [True, False]) @pytest.mark.parametrize("engine", [None, "python", "c"]) @pytest.mark.parametrize("delimiter", [",", " "]) @pytest.mark.parametrize("delim_whitespace", [True, False]) @pytest.mark.parametrize("low_memory", [True, False]) @pytest.mark.parametrize("memory_map", [True, False]) @pytest.mark.parametrize("float_precision", [None, "high", "round_trip"]) def test_read_csv_internal( self, make_csv_file, use_str_data, engine, delimiter, delim_whitespace, low_memory, memory_map, float_precision, ): # In this case raised TypeError: cannot use a string pattern on a bytes-like object, # so TypeError should be excluded from raising_exceptions list in order to check, that # the same exceptions are raised by Pandas and Modin case_with_TypeError_exc = ( engine == "python" and delimiter == "," and delim_whitespace and low_memory and memory_map and float_precision is None ) raising_exceptions = io_ops_bad_exc # default value if case_with_TypeError_exc: raising_exceptions = list(io_ops_bad_exc) raising_exceptions.remove(TypeError) kwargs = { "engine": engine, "delimiter": delimiter, "delim_whitespace": delim_whitespace, "low_memory": low_memory, "memory_map": memory_map, "float_precision": float_precision, } unique_filename = get_unique_filename() if use_str_data: str_delim_whitespaces = ( "col1 col2 col3 col4\n5 6 7 8\n9 10 11 12\n" ) eval_io_from_str( str_delim_whitespaces, unique_filename, raising_exceptions=raising_exceptions, **kwargs, ) else: make_csv_file( filename=unique_filename, delimiter=delimiter, ) eval_io( filepath_or_buffer=unique_filename, fn_name="read_csv", raising_exceptions=raising_exceptions, **kwargs, ) # Issue related, specific or corner cases @pytest.mark.parametrize("nrows", [2, None]) def test_read_csv_bad_quotes(self, nrows): csv_bad_quotes = ( '1, 2, 3, 4\none, two, three, four\nfive, "six", seven, "eight\n' ) unique_filename = get_unique_filename() eval_io_from_str(csv_bad_quotes, unique_filename, nrows=nrows) def test_read_csv_categories(self): eval_io( fn_name="read_csv", # read_csv kwargs filepath_or_buffer="modin/pandas/test/data/test_categories.csv", names=["one", "two"], dtype={"one": "int64", "two": "category"}, ) @pytest.mark.parametrize("encoding", [None, "utf-8"]) @pytest.mark.parametrize("encoding_errors", ["strict", "ignore"]) @pytest.mark.parametrize("parse_dates", [False, ["timestamp"]]) @pytest.mark.parametrize("index_col", [None, 0, 2]) @pytest.mark.parametrize("header", ["infer", 0]) @pytest.mark.parametrize( "names", [ None, ["timestamp", "symbol", "high", "low", "open", "close", "spread", "volume"], ], ) def test_read_csv_parse_dates( self, names, header, index_col, parse_dates, encoding, encoding_errors ): if names is not None and header == "infer": pytest.xfail( "read_csv with Ray engine works incorrectly with date data and names parameter provided - issue #2509" ) eval_io( fn_name="read_csv", # read_csv kwargs filepath_or_buffer="modin/pandas/test/data/test_time_parsing.csv", names=names, header=header, index_col=index_col, parse_dates=parse_dates, encoding=encoding, encoding_errors=encoding_errors, ) @pytest.mark.parametrize( "storage_options", [{"anon": False}, {"anon": True}, {"key": "123", "secret": "123"}, None], ) def test_read_csv_s3(self, storage_options): eval_io( fn_name="read_csv", # read_csv kwargs filepath_or_buffer="s3://noaa-ghcn-pds/csv/1788.csv", storage_options=storage_options, ) @pytest.mark.parametrize("names", [list("XYZ"), None]) @pytest.mark.parametrize("skiprows", [1, 2, 3, 4, None]) def test_read_csv_skiprows_names(self, names, skiprows): if StorageFormat.get() == "Omnisci" and names is None and skiprows in [1, None]: # If these conditions are satisfied, columns names will be inferred # from the first row, that will contain duplicated values, that is # not supported by `Omnisci` storage format yet. pytest.xfail( "processing of duplicated columns in OmniSci storage format is not supported yet - issue #3080" ) eval_io( fn_name="read_csv", # read_csv kwargs filepath_or_buffer="modin/pandas/test/data/issue_2239.csv", names=names, skiprows=skiprows, ) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #2340", ) def test_read_csv_default_to_pandas(self): with pytest.warns(UserWarning): # This tests that we default to pandas on a buffer from io import StringIO pd.read_csv( StringIO(open(pytest.csvs_names["test_read_csv_regular"], "r").read()) ) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #2340", ) def test_read_csv_default_to_pandas_url(self): # We haven't implemented read_csv from https, but if it's implemented, then this needs to change eval_io( fn_name="read_csv", modin_warning=UserWarning, # read_csv kwargs filepath_or_buffer="https://raw.githubusercontent.com/modin-project/modin/master/modin/pandas/test/data/blah.csv", # It takes about ~17Gb of RAM for Omnisci to import the whole table from this test # because of too many (~1000) string columns in it. Taking a subset of columns # to be able to run this test on low-RAM machines. usecols=[0, 1, 2, 3] if StorageFormat.get() == "Omnisci" else None, ) @pytest.mark.parametrize("nrows", [21, 5, None]) @pytest.mark.parametrize("skiprows", [4, 1, 500, None]) def test_read_csv_newlines_in_quotes(self, nrows, skiprows): eval_io( fn_name="read_csv", # read_csv kwargs filepath_or_buffer="modin/pandas/test/data/newlines.csv", nrows=nrows, skiprows=skiprows, cast_to_str=StorageFormat.get() != "Omnisci", ) def test_read_csv_sep_none(self): eval_io( fn_name="read_csv", modin_warning=ParserWarning, # read_csv kwargs filepath_or_buffer=pytest.csvs_names["test_read_csv_regular"], sep=None, ) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #2340", ) def test_read_csv_incorrect_data(self): eval_io( fn_name="read_csv", # read_csv kwargs filepath_or_buffer="modin/pandas/test/data/test_categories.json", ) @pytest.mark.parametrize( "kwargs", [ {"names": [5, 1, 3, 4, 2, 6]}, {"names": [0]}, {"names": None, "usecols": [1, 0, 2]}, {"names": [3, 1, 2, 5], "usecols": [4, 1, 3, 2]}, ], ) def test_read_csv_names_neq_num_cols(self, kwargs): eval_io( fn_name="read_csv", # read_csv kwargs filepath_or_buffer="modin/pandas/test/data/issue_2074.csv", **kwargs, ) def test_read_csv_wrong_path(self): raising_exceptions = [e for e in io_ops_bad_exc if e != FileNotFoundError] eval_io( fn_name="read_csv", raising_exceptions=raising_exceptions, # read_csv kwargs filepath_or_buffer="/some/wrong/path.csv", ) @pytest.mark.skipif( StorageFormat.get() == "Omnisci", reason="to_csv is not implemented with OmniSci storage format yet - issue #3082", ) @pytest.mark.parametrize("header", [False, True]) @pytest.mark.parametrize("mode", ["w", "wb+"]) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #2340", ) def test_to_csv(self, header, mode): pandas_df = generate_dataframe() modin_df = pd.DataFrame(pandas_df) eval_to_file( modin_obj=modin_df, pandas_obj=pandas_df, fn="to_csv", extension="csv", header=header, mode=mode, ) @pytest.mark.skipif( StorageFormat.get() == "Omnisci", reason="to_csv is not implemented with OmniSci storage format yet - issue #3082", ) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #2340", ) def test_dataframe_to_csv(self): pandas_df = pandas.read_csv(pytest.csvs_names["test_read_csv_regular"]) modin_df = pd.DataFrame(pandas_df) eval_to_file( modin_obj=modin_df, pandas_obj=pandas_df, fn="to_csv", extension="csv" ) @pytest.mark.skipif( StorageFormat.get() == "Omnisci", reason="to_csv is not implemented with OmniSci storage format yet - issue #3082", ) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #2340", ) def test_series_to_csv(self): pandas_s = pandas.read_csv( pytest.csvs_names["test_read_csv_regular"], usecols=["col1"] ).squeeze() modin_s = pd.Series(pandas_s) eval_to_file( modin_obj=modin_s, pandas_obj=pandas_s, fn="to_csv", extension="csv" ) def test_read_csv_within_decorator(self): @dummy_decorator() def wrapped_read_csv(file, method): if method == "pandas": return pandas.read_csv(file) if method == "modin": return pd.read_csv(file) pandas_df = wrapped_read_csv( pytest.csvs_names["test_read_csv_regular"], method="pandas" ) modin_df = wrapped_read_csv( pytest.csvs_names["test_read_csv_regular"], method="modin" ) if StorageFormat.get() == "Omnisci": # Aligning DateTime dtypes because of the bug related to the `parse_dates` parameter: # https://github.com/modin-project/modin/issues/3485 modin_df, pandas_df = align_datetime_dtypes(modin_df, pandas_df) df_equals(modin_df, pandas_df) @pytest.mark.parametrize( "read_mode", [ "r", pytest.param( "rb", marks=pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="Cannot pickle file handles. See comments in PR #2625", ), ), ], ) def test_read_csv_file_handle(self, read_mode, make_csv_file): unique_filename = get_unique_filename() make_csv_file(filename=unique_filename) with open(unique_filename, mode=read_mode) as buffer: df_pandas = pandas.read_csv(buffer) buffer.seek(0) df_modin = pd.read_csv(buffer) df_equals(df_modin, df_pandas) def test_unnamed_index(self): def get_internal_df(df): partition = read_df._query_compiler._modin_frame._partitions[0][0] return partition.to_pandas() path = "modin/pandas/test/data/issue_3119.csv" read_df = pd.read_csv(path, index_col=0) assert get_internal_df(read_df).index.name is None read_df = pd.read_csv(path, index_col=[0, 1]) for name1, name2 in zip(get_internal_df(read_df).index.names, [None, "a"]): assert name1 == name2 def test_read_csv_empty_frame(self): eval_io( fn_name="read_csv", # read_csv kwargs filepath_or_buffer=pytest.csvs_names["test_read_csv_regular"], usecols=["col1"], index_col="col1", ) @pytest.mark.parametrize( "skiprows", [ lambda x: x > 20, lambda x: True, lambda x: x in [10, 20], pytest.param( lambda x: x << 10, marks=pytest.mark.skipif( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #2340", ), ), ], ) def test_read_csv_skiprows_corner_cases(self, skiprows): eval_io( fn_name="read_csv", check_kwargs_callable=not callable(skiprows), # read_csv kwargs filepath_or_buffer=pytest.csvs_names["test_read_csv_regular"], skiprows=skiprows, ) class TestTable: def test_read_table(self, make_csv_file): unique_filename = get_unique_filename() make_csv_file(filename=unique_filename, delimiter="\t") eval_io( fn_name="read_table", # read_table kwargs filepath_or_buffer=unique_filename, ) def test_read_table_within_decorator(self, make_csv_file): unique_filename = get_unique_filename() make_csv_file(filename=unique_filename, delimiter="\t") @dummy_decorator() def wrapped_read_table(file, method): if method == "pandas": return pandas.read_table(file) if method == "modin": return pd.read_table(file) pandas_df = wrapped_read_table(unique_filename, method="pandas") modin_df = wrapped_read_table(unique_filename, method="modin") df_equals(modin_df, pandas_df) def test_read_table_empty_frame(self, make_csv_file): unique_filename = get_unique_filename() make_csv_file(filename=unique_filename, delimiter="\t") eval_io( fn_name="read_table", # read_table kwargs filepath_or_buffer=unique_filename, usecols=["col1"], index_col="col1", ) class TestParquet: @pytest.mark.parametrize("columns", [None, ["col1"]]) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264", ) def test_read_parquet(self, make_parquet_file, columns): unique_filename = get_unique_filename(extension="parquet") make_parquet_file(filename=unique_filename) eval_io( fn_name="read_parquet", # read_parquet kwargs path=unique_filename, columns=columns, ) @pytest.mark.parametrize("columns", [None, ["col1"]]) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264", ) def test_read_parquet_directory(self, make_parquet_file, columns): # unique_filename = get_unique_filename(extension=None) make_parquet_file(filename=unique_filename, directory=True) eval_io( fn_name="read_parquet", # read_parquet kwargs path=unique_filename, columns=columns, ) @pytest.mark.parametrize("columns", [None, ["col1"]]) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264", ) def test_read_parquet_partitioned_directory(self, make_parquet_file, columns): unique_filename = get_unique_filename(extension=None) make_parquet_file(filename=unique_filename, partitioned_columns=["col1"]) eval_io( fn_name="read_parquet", # read_parquet kwargs path=unique_filename, columns=columns, ) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264", ) def test_read_parquet_pandas_index(self): # Ensure modin can read parquet files written by pandas with a non-RangeIndex object unique_filename = get_unique_filename(extension="parquet") pandas_df = pandas.DataFrame( { "idx": np.random.randint(0, 100_000, size=2000), "A": np.random.randint(0, 100_000, size=2000), "B": ["a", "b"] * 1000, "C": ["c"] * 2000, } ) try: pandas_df.set_index("idx").to_parquet(unique_filename) # read the same parquet using modin.pandas df_equals( pd.read_parquet(unique_filename), pandas.read_parquet(unique_filename) ) pandas_df.set_index(["idx", "A"]).to_parquet(unique_filename) df_equals( pd.read_parquet(unique_filename), pandas.read_parquet(unique_filename) ) finally: os.remove(unique_filename) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264", ) def test_read_parquet_pandas_index_partitioned(self): # Ensure modin can read parquet files written by pandas with a non-RangeIndex object unique_filename = get_unique_filename(extension="parquet") pandas_df = pandas.DataFrame( { "idx": np.random.randint(0, 100_000, size=2000), "A": np.random.randint(0, 10, size=2000), "B": ["a", "b"] * 1000, "C": ["c"] * 2000, } ) try: pandas_df.set_index("idx").to_parquet(unique_filename, partition_cols=["A"]) # read the same parquet using modin.pandas df_equals( pd.read_parquet(unique_filename), pandas.read_parquet(unique_filename) ) finally: shutil.rmtree(unique_filename) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264", ) def test_read_parquet_hdfs(self): eval_io( fn_name="read_parquet", # read_parquet kwargs path="modin/pandas/test/data/hdfs.parquet", ) @pytest.mark.parametrize("path_type", ["url", "object"]) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264", ) def test_read_parquet_s3(self, path_type): dataset_url = "s3://modin-datasets/testing/test_data.parquet" if path_type == "object": import s3fs fs = s3fs.S3FileSystem(anon=True) with fs.open(dataset_url, "rb") as file_obj: eval_io("read_parquet", path=file_obj) else: eval_io("read_parquet", path=dataset_url, storage_options={"anon": True}) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264", ) def test_read_parquet_without_metadata(self): """Test that Modin can read parquet files not written by pandas.""" from pyarrow import csv from pyarrow import parquet parquet_fname = get_unique_filename(extension="parquet") csv_fname = get_unique_filename(extension="parquet") pandas_df = pandas.DataFrame( { "idx": np.random.randint(0, 100_000, size=2000), "A": np.random.randint(0, 10, size=2000), "B": ["a", "b"] * 1000, "C": ["c"] * 2000, } ) try: pandas_df.to_csv(csv_fname, index=False) # read into pyarrow table and write it to a parquet file t = csv.read_csv(csv_fname) parquet.write_table(t, parquet_fname) df_equals( pd.read_parquet(parquet_fname), pandas.read_parquet(parquet_fname) ) finally: teardown_test_files([parquet_fname, csv_fname]) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264", ) def test_to_parquet(self): modin_df, pandas_df = create_test_dfs(TEST_DATA) parquet_eval_to_file( modin_obj=modin_df, pandas_obj=pandas_df, fn="to_parquet", extension="parquet", ) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264", ) def test_read_parquet_2462(self): test_df = pandas.DataFrame({"col1": [["ad_1", "ad_2"], ["ad_3"]]}) with tempfile.TemporaryDirectory() as directory: path = f"{directory}/data" os.makedirs(path) test_df.to_parquet(path + "/part-00000.parquet") read_df = pd.read_parquet(path) df_equals(test_df, read_df) class TestJson: @pytest.mark.parametrize("lines", [False, True]) def test_read_json(self, make_json_file, lines): eval_io( fn_name="read_json", # read_json kwargs path_or_buf=make_json_file(lines=lines), lines=lines, ) @pytest.mark.parametrize( "storage_options", [{"anon": False}, {"anon": True}, {"key": "123", "secret": "123"}, None], ) def test_read_json_s3(self, storage_options): eval_io( fn_name="read_json", path_or_buf="s3://modin-datasets/testing/test_data.json", lines=True, orient="records", storage_options=storage_options, ) def test_read_json_categories(self): eval_io( fn_name="read_json", # read_json kwargs path_or_buf="modin/pandas/test/data/test_categories.json", dtype={"one": "int64", "two": "category"}, ) @pytest.mark.parametrize( "data", [json_short_string, json_short_bytes, json_long_string, json_long_bytes], ) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264", ) def test_read_json_string_bytes(self, data): with pytest.warns(UserWarning): modin_df = pd.read_json(data) # For I/O objects we need to rewind to reuse the same object. if hasattr(data, "seek"): data.seek(0) df_equals(modin_df, pandas.read_json(data)) def test_to_json(self): modin_df, pandas_df = create_test_dfs(TEST_DATA) eval_to_file( modin_obj=modin_df, pandas_obj=pandas_df, fn="to_json", extension="json" ) @pytest.mark.parametrize( "read_mode", [ "r", pytest.param( "rb", marks=pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="Cannot pickle file handles. See comments in PR #2625", ), ), ], ) def test_read_json_file_handle(self, make_json_file, read_mode): with open(make_json_file(), mode=read_mode) as buf: df_pandas = pandas.read_json(buf) buf.seek(0) df_modin = pd.read_json(buf) df_equals(df_pandas, df_modin) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264", ) def test_read_json_metadata(self, make_json_file): # `lines=True` is for triggering Modin implementation, # `orient="records"` should be set if `lines=True` df = pd.read_json( make_json_file(ncols=80, lines=True), lines=True, orient="records" ) parts_width_cached = df._query_compiler._modin_frame._column_widths_cache num_splits = len(df._query_compiler._modin_frame._partitions[0]) parts_width_actual = [ len(df._query_compiler._modin_frame._partitions[0][i].get().columns) for i in range(num_splits) ] assert parts_width_cached == parts_width_actual class TestExcel: @check_file_leaks def test_read_excel(self, make_excel_file): eval_io( fn_name="read_excel", # read_excel kwargs io=make_excel_file(), ) @check_file_leaks @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264", ) def test_read_excel_engine(self, make_excel_file): eval_io( fn_name="read_excel", modin_warning=UserWarning, # read_excel kwargs io=make_excel_file(), engine="openpyxl", ) @check_file_leaks @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264", ) def test_read_excel_index_col(self, make_excel_file): eval_io( fn_name="read_excel", modin_warning=UserWarning, # read_excel kwargs io=make_excel_file(), index_col=0, ) @check_file_leaks @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264", ) def test_read_excel_all_sheets(self, make_excel_file): unique_filename = make_excel_file() pandas_df = pandas.read_excel(unique_filename, sheet_name=None) modin_df = pd.read_excel(unique_filename, sheet_name=None) assert isinstance(pandas_df, (OrderedDict, dict)) assert isinstance(modin_df, type(pandas_df)) assert pandas_df.keys() == modin_df.keys() for key in pandas_df.keys(): df_equals(modin_df.get(key), pandas_df.get(key)) @pytest.mark.xfail( Engine.get() != "Python", reason="pandas throws the exception. See pandas issue #39250 for more info", ) @check_file_leaks def test_read_excel_sheetname_title(self): eval_io( fn_name="read_excel", # read_excel kwargs io="modin/pandas/test/data/excel_sheetname_title.xlsx", ) @check_file_leaks def test_excel_empty_line(self): path = "modin/pandas/test/data/test_emptyline.xlsx" modin_df = pd.read_excel(path) assert str(modin_df) @pytest.mark.parametrize( "sheet_name", [ "Sheet1", "AnotherSpecialName", "SpecialName", "SecondSpecialName", 0, 1, 2, 3, ], ) @check_file_leaks def test_read_excel_sheet_name(self, sheet_name): eval_io( fn_name="read_excel", # read_excel kwargs io="modin/pandas/test/data/modin_error_book.xlsx", sheet_name=sheet_name, ) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="TypeError: Expected list, got type - issue #3284", ) def test_ExcelFile(self, make_excel_file): unique_filename = make_excel_file() modin_excel_file = pd.ExcelFile(unique_filename) pandas_excel_file = pandas.ExcelFile(unique_filename) try: df_equals(modin_excel_file.parse(), pandas_excel_file.parse()) assert modin_excel_file.io == unique_filename assert isinstance(modin_excel_file, pd.ExcelFile) finally: modin_excel_file.close() pandas_excel_file.close() @pytest.mark.xfail(strict=False, reason="Flaky test, defaults to pandas") def test_to_excel(self): modin_df, pandas_df = create_test_dfs(TEST_DATA) unique_filename_modin = get_unique_filename(extension="xlsx") unique_filename_pandas = get_unique_filename(extension="xlsx") modin_writer = pandas.ExcelWriter(unique_filename_modin) pandas_writer = pandas.ExcelWriter(unique_filename_pandas) try: modin_df.to_excel(modin_writer) pandas_df.to_excel(pandas_writer) modin_writer.save() pandas_writer.save() assert assert_files_eq(unique_filename_modin, unique_filename_pandas) finally: teardown_test_files([unique_filename_modin, unique_filename_pandas]) @pytest.mark.xfail( Engine.get() != "Python", reason="Test fails because of issue 3305" ) @check_file_leaks @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264", ) def test_read_excel_empty_frame(self, make_excel_file): eval_io( fn_name="read_excel", modin_warning=UserWarning, # read_excel kwargs io=make_excel_file(), usecols=[0], index_col=0, ) class TestHdf: @pytest.mark.parametrize("format", [None, "table"]) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264", ) def test_read_hdf(self, make_hdf_file, format): eval_io( fn_name="read_hdf", # read_hdf kwargs path_or_buf=make_hdf_file(format=format), key="df", ) @pytest.mark.xfail( condition="config.getoption('--simulate-cloud').lower() != 'off'", reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264", ) def test_HDFStore(self): hdf_file = None unique_filename_modin = get_unique_filename(extension="hdf") unique_filename_pandas = get_unique_filename(extension="hdf") try: modin_store = pd.HDFStore(unique_filename_modin) pandas_store = pandas.HDFStore(unique_filename_pandas) modin_df, pandas_df = create_test_dfs(TEST_DATA) modin_store["foo"] = modin_df pandas_store["foo"] = pandas_df modin_df = modin_store.get("foo") pandas_df = pandas_store.get("foo") df_equals(modin_df, pandas_df) modin_store.close() pandas_store.close() modin_df =
pandas.read_hdf(unique_filename_modin, key="foo", mode="r")
pandas.read_hdf
import requests import math import functools import os import argparse from io import StringIO import pandas as pd pd.options.mode.chained_assignment = None from urllib.request import urlopen import xml.etree.ElementTree as et from itertools import combinations, product from itertools import chain import itertools import sys import pickle parser = argparse.ArgumentParser() parser.add_argument("-ena", help="Study Identifier from ENA, e.g. PRJNA176940", nargs=1) parser.add_argument('-local', help="Path to CSV file with one column for sample name (replicates should be given the same name) and one column containing the path to the read file (pair-end read files should be in the same cell, separated by a comma)", nargs=1) args = parser.parse_args() def read_type_det(read_type): for i in read_type: if len(i) ==2: paired = True single = False else: paired = False single = True return(single, paired) def is_redundant(df, A, B): df=df[df.columns[~df.columns.isin(sample_info_col_list)]] left=A right=B #print(df) if len(df.groupby(f'{left}')) == len(df.groupby([f'{left}', f'{right}'])): return True elif len(df.groupby(f'{right}')) == len(df.groupby([f'{right}', f'{left}'])): return True else: return False def drop_redundant(df, redundant_groups): df=df[df.columns[~df.columns.isin(sample_info_col_list)]] redundant_groups=redundant_groups redundant_groups.sort() if not redundant_groups: return(df) else: df = df.reindex(sorted(df.columns), axis=1) red_pairs=itertools.combinations(redundant_groups, r=2) red_pairs_list=[] for i in red_pairs: temp=tuple(i) red_pairs_list.append(temp) for i in red_pairs_list: int1=to_be_merged_df.groupby(f'{i[0]}') int2=to_be_merged_df.groupby([f'{i[0]}', f'{i[1]}']) grpby_first_count=to_be_merged_df.groupby(f'{i[0]}').count().iloc[:,0].tolist()[0] grpby_first_unique=to_be_merged_df.groupby(f'{i[0]}').nunique().iloc[:,0].tolist()[0] grpby_first_sec_count=to_be_merged_df.groupby([f'{i[0]}', f'{i[1]}']).count().iloc[:,0].tolist()[0] if int(grpby_first_count/grpby_first_unique)==grpby_first_sec_count: to_be_merged_df[f'{i[0]}' + f'{i[1]}'] = to_be_merged_df[[f'{i[0]}', f'{i[1]}']].sum(axis=1) to_be_merged_df.drop([f'{i[0]}', f'{i[1]}'], axis=1, inplace=True) continue elif len(to_be_merged_df.groupby(f'{i[0]}')) == len(to_be_merged_df.groupby([f'{i[0]}', f'{i[1]}'])): to_be_merged_df[f'{i[0]}' + f'{i[1]}'] = to_be_merged_df[[f'{i[0]}', f'{i[1]}']].sum(axis=1) to_be_merged_df.drop([f'{i[0]}', f'{i[1]}'], axis=1, inplace=True) return(df) else: return(df) #is_redundant(df,'B','D') #print(df) def redundant_groups_finder(df): df=df[df.columns[~df.columns.isin(sample_info_col_list)]] #print(df) cols = [c for c in df.columns] redundant_groups = [] if len(cols)==1: return(redundant_groups) else: idx_left = 0 while idx_left < len(cols)-1: new_group = [] idx_right = idx_left+1 while idx_right < len(cols): if is_redundant(df, cols[idx_left], cols[idx_right]): new_group.append(cols.pop(idx_right)) else: idx_right += 1 if new_group: redundant_groups.append(new_group + [cols[idx_left]]) idx_left += 1 return(redundant_groups) def factor_levels_dict(df): df=df[df.columns[~df.columns.isin(sample_info_col_list)]] factor_list=df.columns.values.tolist() factors_levels= {col: list(df[col].unique()) for col in factor_list} return(factors_levels) def factors_levels_size(dict): #takes dict of factor_levels all_levels=list(dict.values()) level_size=[] for i in all_levels: level_size.append(len(i)) return(level_size) def multiplier(list): min_factor=min(list) print(min_factor) print(len(list)) print("range", range(1 ,len(list)+1)) for i in range(1 ,len(list)+1): print("i",i) temp=min_factor*i min_factor=temp temp=1 for i in list: print(i) temp=i*temp print("multiplier") print(temp) return(temp) #this is an ugly argument def replicates_def(df, level_size): full_df=df[df.columns[~df.columns.isin(sample_info_col_list)]] all_levels=list(full_df.keys()) grouped=df.groupby(all_levels).size() number_of_replicates=min(grouped.values.tolist()) if number_of_replicates > 1: replicates=True else: replicates=False return(replicates, number_of_replicates) #get pairwise combinations by indexes of the full def, this needs to be wrapped into an entire function def pairs(*lists): for t in combinations(lists, 2): for pair in product(*t): #Don't output pairs containing duplicated elements if pair[0] != pair[1]: yield pair def unique_everseen_custom(it): seen = set() seen_add = seen.add for tup in it: if (tup not in seen) and (tup[::-1] not in seen): seen_add(tup) seen_add(tup[::-1]) yield tup def get_constrast_indices(df): df=df[df.columns[~df.columns.isin(sample_info_col_list)]] length=len(df.index) a = range(0,length,1) list_ = set(pairs(a, a)) res = list(unique_everseen_custom(list_)) contrasts_indices = sorted(res, key=lambda tup: tup[0]) return(contrasts_indices) #add name to it? #get the factor with the highest amount of levels def factor_with_highest_level(factors_levels): level_length=[] length_of_highest_level=None for i in list(factors_levels.values()): level_length.append(len(i)) length_of_highest_level=max(level_length) index=-1 for i in list(factors_levels.values()): #print(i) index+=1 if len(i) == max(level_length): break else: continue highest_level_factor=list(factors_levels.keys())[index] return(highest_level_factor) def subset_sample_table(subset_by_level, df): subset_column=None subsetted_df={} dict=factor_levels_dict(df) subset_column=[factor for factor in dict.keys() if f'{subset_by_level}' in dict[factor]] subset_column="".join(subset_column) subsetted_df=df[df[f"{subset_column}"]==subset_by_level] #subsetted_df=df.query(f'{subset_column}=={subset_by_level}') return(subsetted_df) def replicates_merger(df, non_unique_col_list, replicate_nr): print("reo", replicate_nr) keep_list=['sample_accession', 'run_accession', 'read_file'] a=non_unique_col_list[0] test=df.groupby(df[a]) print(test.describe()) df_temp=(df.groupby(df[a].tolist()) .apply(lambda x: tuple(x.index)) .reset_index(name='repl_index')) b=non_unique_col_list[len(non_unique_col_list)-1] df_temp2=(df.groupby(df[b].tolist()) .apply(lambda x: tuple(x.index)) .reset_index(name='repl_index')) #print(a) repl_index=df_temp['repl_index'].tolist() repl_index2=df_temp2['repl_index'].tolist() min_repl1=[] min_repl2=[] for i in range(0,len(repl_index)): min_repl1.append(len(repl_index[i])) for j in range(0, len(repl_index2)): min_repl2.append(len(repl_index2[j])) indices1 = [i for i, x in enumerate(min_repl1) if x == min(min_repl1)] max1 = [i for i, x in enumerate(min_repl1) if x == max(min_repl1)] indices2=[i for i, x in enumerate(min_repl2) if x == min(min_repl2)] max2 = [i for i, x in enumerate(min_repl2) if x == max(min_repl2)] max1=[repl_index[i] for i in max1] max2=[repl_index2[i] for i in max2] max1=[item for t in max1 for item in t] max2=[item for t in max2 for item in t] print(max1) print(max2) add=list(set(max1) & set(list(max2) )) add=tuple(add) print(add) repl_index=[repl_index[i] for i in indices1] print("r1") print(repl_index) repl_index2=[repl_index2[i] for i in indices2] print("r") print(repl_index2) repl_index=repl_index#+repl_index2+[add] print(repl_index) list_index=[] for i in repl_index: list_index.append(list(i)) sample_accession_new=[] run_accession_new=[] read_file_new=[] print(list_index) for i in list_index: sample_accession_new_row=[] sample_accession_new_row.append(df['sample_accession'].iloc[i].tolist()) sample_accession_new_row=list(chain.from_iterable(sample_accession_new_row)) sample_accession_new.append(sample_accession_new_row) run_accession_new_row=[] run_accession_new_row.append(df['run_accession'].iloc[i].tolist()) run_accession_new_row=list(chain.from_iterable(run_accession_new_row)) run_accession_new.append(run_accession_new_row) #this also needs the read_files read_file_new_row=[] read_file_new_row.append(df['read_file'].iloc[i].tolist()) read_file_new_row=list(chain.from_iterable(read_file_new_row)) read_file_new.append(read_file_new_row) replicates_list=list(range(0, replicate_nr)) replicates_columns=[] for i in replicates_list: replicates_columns.append(str(i)) print(replicates_columns) naming=["sample_accession_repl", "run_accession_repl","read_file_repl"] #3 three times out=list(itertools.product(naming, replicates_columns)) detouple=() for i in out: detouple+=i detouple=iter(detouple) print(detouple) replicates_columns=[i+next(detouple, '') for i in detouple] sample_accession_new=pd.DataFrame(sample_accession_new) run_accession_new=pd.DataFrame(run_accession_new) read_file_new=pd.DataFrame(read_file_new) print(read_file_new) #merge all together unique/dropna and the between newly created ones out_df=
pd.concat([sample_accession_new, run_accession_new, read_file_new], axis=1)
pandas.concat
#!/usr/bin/env python # -*- coding: utf-8 -*- import os import pickle import shutil import sys import tempfile import numpy as np from numpy import arange, nan import pandas.testing as pdt from pandas import DataFrame, MultiIndex, Series, to_datetime # dependencies testing specific import pytest import recordlinkage from recordlinkage.base import BaseCompareFeature STRING_SIM_ALGORITHMS = [ 'jaro', 'q_gram', 'cosine', 'jaro_winkler', 'dameraulevenshtein', 'levenshtein', 'lcs', 'smith_waterman' ] NUMERIC_SIM_ALGORITHMS = ['step', 'linear', 'squared', 'exp', 'gauss'] FIRST_NAMES = [ u'Ronald', u'Amy', u'Andrew', u'William', u'Frank', u'Jessica', u'Kevin', u'Tyler', u'Yvonne', nan ] LAST_NAMES = [ u'Graham', u'Smith', u'Holt', u'Pope', u'Hernandez', u'Gutierrez', u'Rivera', nan, u'Crane', u'Padilla' ] STREET = [ u'<NAME>', nan, u'<NAME>', u'<NAME>', u'<NAME>', u'<NAME>', u'Williams Trail', u'Durham Mountains', u'Anna Circle', u'<NAME>' ] JOB = [ u'Designer, multimedia', u'Designer, blown glass/stained glass', u'Chiropractor', u'Engineer, mining', u'Quantity surveyor', u'Phytotherapist', u'Teacher, English as a foreign language', u'Electrical engineer', u'Research officer, government', u'Economist' ] AGES = [23, 40, 70, 45, 23, 57, 38, nan, 45, 46] # Run all tests in this file with: # nosetests tests/test_compare.py class TestData(object): @classmethod def setup_class(cls): N_A = 100 N_B = 100 cls.A = DataFrame({ 'age': np.random.choice(AGES, N_A), 'given_name': np.random.choice(FIRST_NAMES, N_A), 'lastname': np.random.choice(LAST_NAMES, N_A), 'street': np.random.choice(STREET, N_A) }) cls.B = DataFrame({ 'age': np.random.choice(AGES, N_B), 'given_name': np.random.choice(FIRST_NAMES, N_B), 'lastname': np.random.choice(LAST_NAMES, N_B), 'street': np.random.choice(STREET, N_B) }) cls.A.index.name = 'index_df1' cls.B.index.name = 'index_df2' cls.index_AB = MultiIndex.from_arrays( [arange(len(cls.A)), arange(len(cls.B))], names=[cls.A.index.name, cls.B.index.name]) # Create a temporary directory cls.test_dir = tempfile.mkdtemp() @classmethod def teardown_class(cls): # Remove the test directory shutil.rmtree(cls.test_dir) class TestCompareApi(TestData): """General unittest for the compare API.""" def test_repr(self): comp = recordlinkage.Compare() comp.exact('given_name', 'given_name') comp.string('given_name', 'given_name', method='jaro') comp.numeric('age', 'age', method='step', offset=3, origin=2) comp.numeric('age', 'age', method='step', offset=0, origin=2) c_str = str(comp) c_repr = repr(comp) assert c_str == c_repr start_str = '<{}'.format(comp.__class__.__name__) assert c_str.startswith(start_str) def test_instance_linking(self): comp = recordlinkage.Compare() comp.exact('given_name', 'given_name') comp.string('given_name', 'given_name', method='jaro') comp.numeric('age', 'age', method='step', offset=3, origin=2) comp.numeric('age', 'age', method='step', offset=0, origin=2) result = comp.compute(self.index_AB, self.A, self.B) # returns a Series assert isinstance(result, DataFrame) # resulting series has a MultiIndex assert isinstance(result.index, MultiIndex) # indexnames are oke assert result.index.names == [self.A.index.name, self.B.index.name] assert len(result) == len(self.index_AB) def test_instance_dedup(self): comp = recordlinkage.Compare() comp.string('given_name', 'given_name', method='jaro') comp.numeric('age', 'age', method='step', offset=3, origin=2) comp.numeric('age', 'age', method='step', offset=0, origin=2) result = comp.compute(self.index_AB, self.A) # returns a Series assert isinstance(result, DataFrame) # resulting series has a MultiIndex assert isinstance(result.index, MultiIndex) # indexnames are oke assert result.index.names == [self.A.index.name, self.B.index.name] assert len(result) == len(self.index_AB) def test_label_linking(self): comp = recordlinkage.Compare() comp.compare_vectorized( lambda s1, s2: np.ones(len(s1), dtype=np.int), 'given_name', 'given_name', label='my_feature_label') result = comp.compute(self.index_AB, self.A, self.B) assert "my_feature_label" in result.columns.tolist() def test_label_dedup(self): comp = recordlinkage.Compare() comp.compare_vectorized( lambda s1, s2: np.ones(len(s1), dtype=np.int), 'given_name', 'given_name', label='my_feature_label') result = comp.compute(self.index_AB, self.A) assert "my_feature_label" in result.columns.tolist() def test_multilabel_none_linking(self): def ones_np_multi(s1, s2): return np.ones(len(s1)), np.ones((len(s1), 3)) def ones_pd_multi(s1, s2): return (Series(np.ones(len(s1))), DataFrame(np.ones((len(s1), 3)))) comp = recordlinkage.Compare() comp.string('given_name', 'given_name', method='jaro') comp.compare_vectorized( ones_np_multi, 'given_name', 'given_name') comp.compare_vectorized( ones_pd_multi, 'given_name', 'given_name') result = comp.compute(self.index_AB, self.A, self.B) assert [0, 1, 2, 3, 4, 5, 6, 7, 8] == \ result.columns.tolist() def test_multilabel_linking(self): def ones_np_multi(s1, s2): return np.ones(len(s1)), np.ones((len(s1), 3)) def ones_pd_multi(s1, s2): return (Series(np.ones(len(s1))), DataFrame(np.ones((len(s1), 3)))) comp = recordlinkage.Compare() comp.string('given_name', 'given_name', method='jaro') comp.compare_vectorized( ones_np_multi, 'given_name', 'given_name', label=['a', ['b', 'c', 'd']]) comp.compare_vectorized( ones_pd_multi, 'given_name', 'given_name', label=['e', ['f', 'g', 'h']]) result = comp.compute(self.index_AB, self.A, self.B) assert [0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] == \ result.columns.tolist() def test_multilabel_dedup(self): def ones_np_multi(s1, s2): return np.ones(len(s1)), np.ones((len(s1), 3)) def ones_pd_multi(s1, s2): return (Series(np.ones(len(s1))), DataFrame(np.ones((len(s1), 3)))) comp = recordlinkage.Compare() comp.string('given_name', 'given_name', method='jaro') comp.compare_vectorized( ones_np_multi, 'given_name', 'given_name', label=['a', ['b', 'c', 'd']]) comp.compare_vectorized( ones_pd_multi, 'given_name', 'given_name', label=['e', ['f', 'g', 'h']]) result = comp.compute(self.index_AB, self.A) assert [0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] == \ result.columns.tolist() def test_multilabel_none_dedup(self): def ones_np_multi(s1, s2): return np.ones(len(s1)), np.ones((len(s1), 3)) def ones_pd_multi(s1, s2): return (Series(np.ones(len(s1))), DataFrame(np.ones((len(s1), 3)))) comp = recordlinkage.Compare() comp.string('given_name', 'given_name', method='jaro') comp.compare_vectorized( ones_np_multi, 'given_name', 'given_name') comp.compare_vectorized( ones_pd_multi, 'given_name', 'given_name') result = comp.compute(self.index_AB, self.A) assert [0, 1, 2, 3, 4, 5, 6, 7, 8] == \ result.columns.tolist() def test_multilabel_error_dedup(self): def ones(s1, s2): return np.ones((len(s1), 2)) comp = recordlinkage.Compare() comp.string('given_name', 'given_name', method='jaro') comp.compare_vectorized( ones, 'given_name', 'given_name', label=['a', 'b', 'c']) with pytest.raises(ValueError): comp.compute(self.index_AB, self.A) def test_incorrect_collabels_linking(self): comp = recordlinkage.Compare() comp.string('given_name', 'given_name', method='jaro') comp.compare_vectorized(lambda s1, s2: np.ones(len(s1), dtype=np.int), "given_name", "not_existing_label") with pytest.raises(KeyError): comp.compute(self.index_AB, self.A, self.B) def test_incorrect_collabels_dedup(self): comp = recordlinkage.Compare() comp.compare_vectorized(lambda s1, s2: np.ones(len(s1), dtype=np.int), "given_name", "not_existing_label") with pytest.raises(KeyError): comp.compute(self.index_AB, self.A) def test_compare_custom_vectorized_linking(self): A = DataFrame({'col': ['abc', 'abc', 'abc', 'abc', 'abc']}) B = DataFrame({'col': ['abc', 'abd', 'abc', 'abc', '123']}) ix = MultiIndex.from_arrays([A.index.values, B.index.values]) # test without label comp = recordlinkage.Compare() comp.compare_vectorized(lambda s1, s2: np.ones(len(s1), dtype=np.int), 'col', 'col') result = comp.compute(ix, A, B) expected = DataFrame([1, 1, 1, 1, 1], index=ix) pdt.assert_frame_equal(result, expected) # test with label comp = recordlinkage.Compare() comp.compare_vectorized( lambda s1, s2: np.ones(len(s1), dtype=np.int), 'col', 'col', label='my_feature_label') result = comp.compute(ix, A, B) expected = DataFrame( [1, 1, 1, 1, 1], index=ix, columns=['my_feature_label']) pdt.assert_frame_equal(result, expected) # def test_compare_custom_nonvectorized_linking(self): # A = DataFrame({'col': [1, 2, 3, 4, 5]}) # B = DataFrame({'col': [1, 2, 3, 4, 5]}) # ix = MultiIndex.from_arrays([A.index.values, B.index.values]) # def custom_func(a, b): # return np.int64(1) # # test without label # comp = recordlinkage.Compare() # comp.compare_single( # custom_func, # 'col', # 'col' # ) # result = comp.compute(ix, A, B) # expected = DataFrame([1, 1, 1, 1, 1], index=ix) # pdt.assert_frame_equal(result, expected) # # test with label # comp = recordlinkage.Compare() # comp.compare_single( # custom_func, # 'col', # 'col', # label='test' # ) # result = comp.compute(ix, A, B) # expected = DataFrame([1, 1, 1, 1, 1], index=ix, columns=['test']) # pdt.assert_frame_equal(result, expected) def test_compare_custom_instance_type(self): A = DataFrame({'col': ['abc', 'abc', 'abc', 'abc', 'abc']}) B = DataFrame({'col': ['abc', 'abd', 'abc', 'abc', '123']}) ix = MultiIndex.from_arrays([A.index.values, B.index.values]) def call(s1, s2): # this should raise on incorrect types assert isinstance(s1, np.ndarray) assert isinstance(s2, np.ndarray) return np.ones(len(s1), dtype=np.int) comp = recordlinkage.Compare() comp.compare_vectorized(lambda s1, s2: np.ones(len(s1), dtype=np.int), 'col', 'col') result = comp.compute(ix, A, B) expected = DataFrame([1, 1, 1, 1, 1], index=ix) pdt.assert_frame_equal(result, expected) def test_compare_custom_vectorized_arguments_linking(self): A = DataFrame({'col': ['abc', 'abc', 'abc', 'abc', 'abc']}) B = DataFrame({'col': ['abc', 'abd', 'abc', 'abc', '123']}) ix = MultiIndex.from_arrays([A.index.values, B.index.values]) # test without label comp = recordlinkage.Compare() comp.compare_vectorized( lambda s1, s2, x: np.ones(len(s1), dtype=np.int) * x, 'col', 'col', 5) result = comp.compute(ix, A, B) expected = DataFrame([5, 5, 5, 5, 5], index=ix) pdt.assert_frame_equal(result, expected) # test with label comp = recordlinkage.Compare() comp.compare_vectorized( lambda s1, s2, x: np.ones(len(s1), dtype=np.int) * x, 'col', 'col', 5, label='test') result = comp.compute(ix, A, B) expected = DataFrame([5, 5, 5, 5, 5], index=ix, columns=['test']) pdt.assert_frame_equal(result, expected) # test with kwarg comp = recordlinkage.Compare() comp.compare_vectorized( lambda s1, s2, x: np.ones(len(s1), dtype=np.int) * x, 'col', 'col', x=5, label='test') result = comp.compute(ix, A, B) expected = DataFrame([5, 5, 5, 5, 5], index=ix, columns=['test']) pdt.assert_frame_equal(result, expected) def test_compare_custom_vectorized_dedup(self): A = DataFrame({'col': ['abc', 'abc', 'abc', 'abc', 'abc']}) ix = MultiIndex.from_arrays([[0, 1, 2, 3, 4], [1, 2, 3, 4, 0]]) # test without label comp = recordlinkage.Compare() comp.compare_vectorized(lambda s1, s2: np.ones(len(s1), dtype=np.int), 'col', 'col') result = comp.compute(ix, A) expected = DataFrame([1, 1, 1, 1, 1], index=ix) pdt.assert_frame_equal(result, expected) # test with label comp = recordlinkage.Compare() comp.compare_vectorized( lambda s1, s2: np.ones(len(s1), dtype=np.int), 'col', 'col', label='test') result = comp.compute(ix, A) expected = DataFrame([1, 1, 1, 1, 1], index=ix, columns=['test']) pdt.assert_frame_equal(result, expected) def test_compare_custom_vectorized_arguments_dedup(self): A = DataFrame({'col': ['abc', 'abc', 'abc', 'abc', 'abc']}) ix = MultiIndex.from_arrays([[0, 1, 2, 3, 4], [1, 2, 3, 4, 0]]) # test without label comp = recordlinkage.Compare() comp.compare_vectorized( lambda s1, s2, x: np.ones(len(s1), dtype=np.int) * x, 'col', 'col', 5) result = comp.compute(ix, A) expected = DataFrame([5, 5, 5, 5, 5], index=ix) pdt.assert_frame_equal(result, expected) # test with label comp = recordlinkage.Compare() comp.compare_vectorized( lambda s1, s2, x: np.ones(len(s1), dtype=np.int) * x, 'col', 'col', 5, label='test') result = comp.compute(ix, A) expected = DataFrame([5, 5, 5, 5, 5], index=ix, columns=['test']) pdt.assert_frame_equal(result, expected) def test_parallel_comparing_api(self): # use single job comp = recordlinkage.Compare(n_jobs=1) comp.exact('given_name', 'given_name', label='my_feature_label') result_single = comp.compute(self.index_AB, self.A, self.B) result_single.sort_index(inplace=True) # use two jobs comp = recordlinkage.Compare(n_jobs=2) comp.exact('given_name', 'given_name', label='my_feature_label') result_2processes = comp.compute(self.index_AB, self.A, self.B) result_2processes.sort_index(inplace=True) # compare results pdt.assert_frame_equal(result_single, result_2processes) def test_parallel_comparing(self): # use single job comp = recordlinkage.Compare(n_jobs=1) comp.exact('given_name', 'given_name', label='my_feature_label') result_single = comp.compute(self.index_AB, self.A, self.B) result_single.sort_index(inplace=True) # use two jobs comp = recordlinkage.Compare(n_jobs=2) comp.exact('given_name', 'given_name', label='my_feature_label') result_2processes = comp.compute(self.index_AB, self.A, self.B) result_2processes.sort_index(inplace=True) # use two jobs comp = recordlinkage.Compare(n_jobs=4) comp.exact('given_name', 'given_name', label='my_feature_label') result_4processes = comp.compute(self.index_AB, self.A, self.B) result_4processes.sort_index(inplace=True) # compare results pdt.assert_frame_equal(result_single, result_2processes) pdt.assert_frame_equal(result_single, result_4processes) def test_pickle(self): # test if it is possible to pickle the Compare class comp = recordlinkage.Compare() comp.string('given_name', 'given_name') comp.numeric('number', 'number') comp.geo('lat', 'lng', 'lat', 'lng') comp.date('before', 'after') # do the test pickle_path = os.path.join(self.test_dir, 'pickle_compare_obj.pickle') pickle.dump(comp, open(pickle_path, 'wb')) def test_manual_parallel_joblib(self): # test if it is possible to pickle the Compare class # This is only available for python 3. For python 2, it is not # possible to pickle instancemethods. A workaround can be found at # https://stackoverflow.com/a/29873604/8727928 if sys.version.startswith("3"): # import joblib dependencies from joblib import Parallel, delayed # split the data into smaller parts len_index = int(len(self.index_AB) / 2) df_chunks = [self.index_AB[0:len_index], self.index_AB[len_index:]] comp = recordlinkage.Compare() comp.string('given_name', 'given_name') comp.string('lastname', 'lastname') comp.exact('street', 'street') # do in parallel Parallel(n_jobs=2)( delayed(comp.compute)(df_chunks[i], self.A, self.B) for i in [0, 1]) def test_indexing_types(self): # test the two types of indexing # this test needs improvement A = DataFrame({'col': ['abc', 'abc', 'abc', 'abc', 'abc']}) B = DataFrame({'col': ['abc', 'abc', 'abc', 'abc', 'abc']}) B_reversed = B[::-1].copy() ix = MultiIndex.from_arrays([np.arange(5), np.arange(5)]) # test with label indexing type comp_label = recordlinkage.Compare(indexing_type='label') comp_label.exact('col', 'col') result_label = comp_label.compute(ix, A, B_reversed) # test with position indexing type comp_position = recordlinkage.Compare(indexing_type='position') comp_position.exact('col', 'col') result_position = comp_position.compute(ix, A, B_reversed) assert (result_position.values == 1).all(axis=0) pdt.assert_frame_equal(result_label, result_position) def test_pass_list_of_features(self): from recordlinkage.compare import FrequencyA, VariableA, VariableB # setup datasets and record pairs A = DataFrame({'col': ['abc', 'abc', 'abc', 'abc', 'abc']}) B = DataFrame({'col': ['abc', 'abc', 'abc', 'abc', 'abc']}) ix = MultiIndex.from_arrays([np.arange(5), np.arange(5)]) # test with label indexing type features = [ VariableA('col', label='y1'), VariableB('col', label='y2'), FrequencyA('col', label='y3') ] comp_label = recordlinkage.Compare(features=features) result_label = comp_label.compute(ix, A, B) assert list(result_label) == ["y1", "y2", "y3"] class TestCompareFeatures(TestData): def test_feature(self): # test using classes and the base class A = DataFrame({'col': ['abc', 'abc', 'abc', 'abc', 'abc']}) B = DataFrame({'col': ['abc', 'abd', 'abc', 'abc', '123']}) ix = MultiIndex.from_arrays([A.index.values, B.index.values]) feature = BaseCompareFeature('col', 'col') feature._f_compare_vectorized = lambda s1, s2: np.ones(len(s1)) feature.compute(ix, A, B) def test_feature_multicolumn_return(self): # test using classes and the base class A = DataFrame({'col': ['abc', 'abc', 'abc', 'abc', 'abc']}) B = DataFrame({'col': ['abc', 'abd', 'abc', 'abc', '123']}) ix = MultiIndex.from_arrays([A.index.values, B.index.values]) def ones(s1, s2): return DataFrame(np.ones((len(s1), 3))) feature = BaseCompareFeature('col', 'col') feature._f_compare_vectorized = ones result = feature.compute(ix, A, B) assert result.shape == (5, 3) def test_feature_multicolumn_input(self): # test using classes and the base class A = DataFrame({ 'col1': ['abc', 'abc', 'abc', 'abc', 'abc'], 'col2': ['abc', 'abc', 'abc', 'abc', 'abc'] }) B = DataFrame({ 'col1': ['abc', 'abd', 'abc', 'abc', '123'], 'col2': ['abc', 'abd', 'abc', 'abc', '123'] }) ix = MultiIndex.from_arrays([A.index.values, B.index.values]) feature = BaseCompareFeature(['col1', 'col2'], ['col1', 'col2']) feature._f_compare_vectorized = \ lambda s1_1, s1_2, s2_1, s2_2: np.ones(len(s1_1)) feature.compute(ix, A, B) class TestCompareExact(TestData): """Test the exact comparison method.""" def test_exact_str_type(self): A = DataFrame({'col': ['abc', 'abc', 'abc', 'abc', 'abc']}) B = DataFrame({'col': ['abc', 'abd', 'abc', 'abc', '123']}) ix = MultiIndex.from_arrays([A.index.values, B.index.values]) expected = DataFrame([1, 0, 1, 1, 0], index=ix) comp = recordlinkage.Compare() comp.exact('col', 'col') result = comp.compute(ix, A, B) pdt.assert_frame_equal(result, expected) def test_exact_num_type(self): A = DataFrame({'col': [42, 42, 41, 43, nan]}) B = DataFrame({'col': [42, 42, 42, 42, 42]}) ix = MultiIndex.from_arrays([A.index.values, B.index.values]) expected = DataFrame([1, 1, 0, 0, 0], index=ix) comp = recordlinkage.Compare() comp.exact('col', 'col') result = comp.compute(ix, A, B) pdt.assert_frame_equal(result, expected) def test_link_exact_missing(self): A = DataFrame({'col': [u'a', u'b', u'c', u'd', nan]}) B = DataFrame({'col': [u'a', u'b', u'd', nan, nan]}) ix = MultiIndex.from_arrays([A.index.values, B.index.values]) comp = recordlinkage.Compare() comp.exact('col', 'col', label='na_') comp.exact('col', 'col', missing_value=0, label='na_0') comp.exact('col', 'col', missing_value=9, label='na_9') comp.exact('col', 'col', missing_value=nan, label='na_na') comp.exact('col', 'col', missing_value='str', label='na_str') result = comp.compute(ix, A, B) # Missing values as default expected = Series([1, 1, 0, 0, 0], index=ix, name='na_') pdt.assert_series_equal(result['na_'], expected) # Missing values as 0 expected = Series([1, 1, 0, 0, 0], index=ix, name='na_0') pdt.assert_series_equal(result['na_0'], expected) # Missing values as 9 expected = Series([1, 1, 0, 9, 9], index=ix, name='na_9') pdt.assert_series_equal(result['na_9'], expected) # Missing values as nan expected = Series([1, 1, 0, nan, nan], index=ix, name='na_na') pdt.assert_series_equal(result['na_na'], expected) # Missing values as string expected = Series([1, 1, 0, 'str', 'str'], index=ix, name='na_str') pdt.assert_series_equal(result['na_str'], expected) def test_link_exact_disagree(self): A = DataFrame({'col': [u'a', u'b', u'c', u'd', nan]}) B = DataFrame({'col': [u'a', u'b', u'd', nan, nan]}) ix = MultiIndex.from_arrays([A.index.values, B.index.values]) comp = recordlinkage.Compare() comp.exact('col', 'col', label='d_') comp.exact('col', 'col', disagree_value=0, label='d_0') comp.exact('col', 'col', disagree_value=9, label='d_9') comp.exact('col', 'col', disagree_value=nan, label='d_na') comp.exact('col', 'col', disagree_value='str', label='d_str') result = comp.compute(ix, A, B) # disagree values as default expected = Series([1, 1, 0, 0, 0], index=ix, name='d_') pdt.assert_series_equal(result['d_'], expected) # disagree values as 0 expected = Series([1, 1, 0, 0, 0], index=ix, name='d_0') pdt.assert_series_equal(result['d_0'], expected) # disagree values as 9 expected = Series([1, 1, 9, 0, 0], index=ix, name='d_9') pdt.assert_series_equal(result['d_9'], expected) # disagree values as nan expected = Series([1, 1, nan, 0, 0], index=ix, name='d_na') pdt.assert_series_equal(result['d_na'], expected) # disagree values as string expected = Series([1, 1, 'str', 0, 0], index=ix, name='d_str') pdt.assert_series_equal(result['d_str'], expected) # tests/test_compare.py:TestCompareNumeric class TestCompareNumeric(TestData): """Test the numeric comparison methods.""" def test_numeric(self): A = DataFrame({'col': [1, 1, 1, nan, 0]}) B = DataFrame({'col': [1, 2, 3, nan, nan]}) ix = MultiIndex.from_arrays([A.index.values, B.index.values]) comp = recordlinkage.Compare() comp.numeric('col', 'col', 'step', offset=2) comp.numeric('col', 'col', method='step', offset=2) comp.numeric('col', 'col', 'step', 2) result = comp.compute(ix, A, B) # Basics expected = Series([1.0, 1.0, 1.0, 0.0, 0.0], index=ix, name=0) pdt.assert_series_equal(result[0], expected) # Basics expected = Series([1.0, 1.0, 1.0, 0.0, 0.0], index=ix, name=1) pdt.assert_series_equal(result[1], expected) # Basics expected = Series([1.0, 1.0, 1.0, 0.0, 0.0], index=ix, name=2) pdt.assert_series_equal(result[2], expected) def test_numeric_with_missings(self): A = DataFrame({'col': [1, 1, 1, nan, 0]}) B = DataFrame({'col': [1, 1, 1, nan, nan]}) ix = MultiIndex.from_arrays([A.index.values, B.index.values]) comp = recordlinkage.Compare() comp.numeric('col', 'col', scale=2) comp.numeric('col', 'col', scale=2, missing_value=0) comp.numeric('col', 'col', scale=2, missing_value=123.45) comp.numeric('col', 'col', scale=2, missing_value=nan) comp.numeric('col', 'col', scale=2, missing_value='str') result = comp.compute(ix, A, B) # Missing values as default expected = Series([1.0, 1.0, 1.0, 0.0, 0.0], index=ix, name=0) pdt.assert_series_equal(result[0], expected) # Missing values as 0 expected = Series( [1.0, 1.0, 1.0, 0.0, 0.0], index=ix, dtype=np.float64, name=1) pdt.assert_series_equal(result[1], expected) # Missing values as 123.45 expected = Series([1.0, 1.0, 1.0, 123.45, 123.45], index=ix, name=2) pdt.assert_series_equal(result[2], expected) # Missing values as nan expected = Series([1.0, 1.0, 1.0, nan, nan], index=ix, name=3) pdt.assert_series_equal(result[3], expected) # Missing values as string expected = Series( [1, 1, 1, 'str', 'str'], index=ix, dtype=object, name=4) pdt.assert_series_equal(result[4], expected) @pytest.mark.parametrize("alg", NUMERIC_SIM_ALGORITHMS) def test_numeric_algorithms(self, alg): A = DataFrame({'col': [1, 1, 1, 1, 1]}) B = DataFrame({'col': [1, 2, 3, 4, 5]}) ix = MultiIndex.from_arrays([A.index.values, B.index.values]) comp = recordlinkage.Compare() comp.numeric('col', 'col', method='step', offset=1, label='step') comp.numeric( 'col', 'col', method='linear', offset=1, scale=2, label='linear') comp.numeric( 'col', 'col', method='squared', offset=1, scale=2, label='squared') comp.numeric( 'col', 'col', method='exp', offset=1, scale=2, label='exp') comp.numeric( 'col', 'col', method='gauss', offset=1, scale=2, label='gauss') result_df = comp.compute(ix, A, B) result = result_df[alg] # All values between 0 and 1. assert (result >= 0.0).all() assert (result <= 1.0).all() if alg != 'step': print(alg) print(result) # sim(scale) = 0.5 expected_bool = Series( [False, False, False, True, False], index=ix, name=alg) pdt.assert_series_equal(result == 0.5, expected_bool) # sim(offset) = 1 expected_bool = Series( [True, True, False, False, False], index=ix, name=alg) pdt.assert_series_equal(result == 1.0, expected_bool) # sim(scale) larger than 0.5 expected_bool = Series( [False, False, True, False, False], index=ix, name=alg) pdt.assert_series_equal((result > 0.5) & (result < 1.0), expected_bool) # sim(scale) smaller than 0.5 expected_bool = Series( [False, False, False, False, True], index=ix, name=alg) pdt.assert_series_equal((result < 0.5) & (result >= 0.0), expected_bool) @pytest.mark.parametrize("alg", NUMERIC_SIM_ALGORITHMS) def test_numeric_algorithms_errors(self, alg): # scale negative if alg != "step": with pytest.raises(ValueError): comp = recordlinkage.Compare() comp.numeric('age', 'age', method=alg, offset=2, scale=-2) comp.compute(self.index_AB, self.A, self.B) # offset negative with pytest.raises(ValueError): comp = recordlinkage.Compare() comp.numeric('age', 'age', method=alg, offset=-2, scale=-2) comp.compute(self.index_AB, self.A, self.B) def test_numeric_does_not_exist(self): # raise when algorithm doesn't exists A = DataFrame({'col': [1, 1, 1, nan, 0]}) B = DataFrame({'col': [1, 1, 1, nan, nan]}) ix = MultiIndex.from_arrays([A.index.values, B.index.values]) comp = recordlinkage.Compare() comp.numeric('col', 'col', method='unknown_algorithm') pytest.raises(ValueError, comp.compute, ix, A, B) # tests/test_compare.py:TestCompareDates class TestCompareDates(TestData): """Test the exact comparison method.""" def test_dates(self): A = DataFrame({ 'col': to_datetime( ['2005/11/23', nan, '2004/11/23', '2010/01/10', '2010/10/30']) }) B = DataFrame({ 'col': to_datetime([ '2005/11/23', '2010/12/31', '2005/11/23', '2010/10/01', '2010/9/30' ]) }) ix = MultiIndex.from_arrays([A.index.values, B.index.values]) comp = recordlinkage.Compare() comp.date('col', 'col') result = comp.compute(ix, A, B)[0] expected = Series([1, 0, 0, 0.5, 0.5], index=ix, name=0) pdt.assert_series_equal(result, expected) def test_date_incorrect_dtype(self): A = DataFrame({ 'col': ['2005/11/23', nan, '2004/11/23', '2010/01/10', '2010/10/30'] }) B = DataFrame({ 'col': [ '2005/11/23', '2010/12/31', '2005/11/23', '2010/10/01', '2010/9/30' ] }) ix = MultiIndex.from_arrays([A.index.values, B.index.values]) A['col1'] = to_datetime(A['col']) B['col1'] = to_datetime(B['col']) comp = recordlinkage.Compare() comp.date('col', 'col1') pytest.raises(ValueError, comp.compute, ix, A, B) comp = recordlinkage.Compare() comp.date('col1', 'col') pytest.raises(ValueError, comp.compute, ix, A, B) def test_dates_with_missings(self): A = DataFrame({ 'col': to_datetime( ['2005/11/23', nan, '2004/11/23', '2010/01/10', '2010/10/30']) }) B = DataFrame({ 'col': to_datetime([ '2005/11/23', '2010/12/31', '2005/11/23', '2010/10/01', '2010/9/30' ]) }) ix = MultiIndex.from_arrays([A.index.values, B.index.values]) comp = recordlinkage.Compare() comp.date('col', 'col', label='m_') comp.date('col', 'col', missing_value=0, label='m_0') comp.date('col', 'col', missing_value=123.45, label='m_float') comp.date('col', 'col', missing_value=nan, label='m_na') comp.date('col', 'col', missing_value='str', label='m_str') result = comp.compute(ix, A, B) # Missing values as default expected = Series([1, 0, 0, 0.5, 0.5], index=ix, name='m_') pdt.assert_series_equal(result['m_'], expected) # Missing values as 0 expected = Series([1, 0, 0, 0.5, 0.5], index=ix, name='m_0') pdt.assert_series_equal(result['m_0'], expected) # Missing values as 123.45 expected = Series([1, 123.45, 0, 0.5, 0.5], index=ix, name='m_float') pdt.assert_series_equal(result['m_float'], expected) # Missing values as nan expected = Series([1, nan, 0, 0.5, 0.5], index=ix, name='m_na') pdt.assert_series_equal(result['m_na'], expected) # Missing values as string expected = Series( [1, 'str', 0, 0.5, 0.5], index=ix, dtype=object, name='m_str') pdt.assert_series_equal(result['m_str'], expected) def test_dates_with_swap(self): months_to_swap = [(9, 10, 123.45), (10, 9, 123.45), (1, 2, 123.45), (2, 1, 123.45)] A = DataFrame({ 'col': to_datetime( ['2005/11/23', nan, '2004/11/23', '2010/01/10', '2010/10/30']) }) B = DataFrame({ 'col': to_datetime([ '2005/11/23', '2010/12/31', '2005/11/23', '2010/10/01', '2010/9/30' ]) }) ix = MultiIndex.from_arrays([A.index.values, B.index.values]) comp = recordlinkage.Compare() comp.date('col', 'col', label='s_') comp.date( 'col', 'col', swap_month_day=0, swap_months='default', label='s_1') comp.date( 'col', 'col', swap_month_day=123.45, swap_months='default', label='s_2') comp.date( 'col', 'col', swap_month_day=123.45, swap_months=months_to_swap, label='s_3') comp.date( 'col', 'col', swap_month_day=nan, swap_months='default', missing_value=nan, label='s_4') comp.date('col', 'col', swap_month_day='str', label='s_5') result = comp.compute(ix, A, B) # swap_month_day as default expected = Series([1, 0, 0, 0.5, 0.5], index=ix, name='s_')
pdt.assert_series_equal(result['s_'], expected)
pandas.testing.assert_series_equal
#%load_ext autoreload #%autoreload 2 import dataclasses import glob import logging import os import shutil import warnings from dataclasses import dataclass from datetime import datetime, timedelta from typing import Dict, List, Optional, Tuple import numpy as np import pandas as pd from scipy.sparse.csr import csr_matrix from psykoda import detection, feature_extraction, preprocess, utils from psykoda.constants import COMMANDLINE_DATE_FORMAT, col from psykoda.io import labeled, reporting logger = logging.getLogger(__name__) to_stderr = {"_log_err": True} FILENAME_WEIGHT = "best_weight.h5" FILENAME_IDF_SID = "idf_sid.csv" FILENAME_IDF_DPORT = "idf_dport.csv" FILENAME_PLOT_DETECTION = "plot_detection.png" FILENAME_STATS = "stats.json" FILENAME_REPORT = "report.csv" FILENAME_FEATURE_MATRIX = "feature_matrix.csv" def configure_logging(debug: bool): """ Configure execution log settings. Parameters ---------- debug Whether to log "debug levels". """ PATH_LOG = "./log/log_" + datetime.strftime(datetime.today(), "%Y-%m-%d") + ".log" os.makedirs(os.path.dirname(PATH_LOG), exist_ok=True) log_level = logging.INFO if debug: log_level = logging.DEBUG # utilities stderr_filter = lambda record: getattr(record, "_log_err", False) # app config stderr_handler = logging.StreamHandler() stderr_handler.addFilter(stderr_filter) stderr_handler.setLevel(logging.INFO) stderr_handler.setFormatter(logging.Formatter("%(message)s")) handlers = [stderr_handler] logfile_handler = logging.FileHandler(PATH_LOG) logfile_handler.setLevel(logging.DEBUG) logfile_handler.setFormatter( logging.Formatter( "%(asctime)s %(levelname)-8s [%(module)s # %(funcName)s line %(lineno)d] %(message)s" ) ) handlers.append(logfile_handler) logging.basicConfig(handlers=handlers, level=log_level) class Incomplete_Args_Exception(Exception): pass load_config = utils.load_json @dataclass class OutputConfig: dir: str share_dir: Optional[str] subdir: Optional[str] @dataclass class PreprocessConfig: exclude_lists: Optional[str] screening: preprocess.ScreeningConfig @dataclass class InputConfig: dir: str @dataclasses.dataclass class LoadPreviousConfigItem: list: Optional[str] ndate: int = 730 @dataclasses.dataclass class LoadPreviousConfig: """ Log loading settings. Parameters ---------- list path to CSV file in which labeled IP addresses are listed ndate time range for labeled IP addresses, in days """ known_normal: Optional[LoadPreviousConfigItem] known_anomaly: Optional[LoadPreviousConfigItem] unknown: Optional[LoadPreviousConfigItem] @dataclass class PreviousConfig: load: LoadPreviousConfig log: labeled.Config @dataclass class IOConfig: input: InputConfig previous: PreviousConfig output: OutputConfig @dataclass class Service: """Service definition: set of destination port numbers Examples -------- >>> all = Service() >>> ssh = Service(include=[22]) >>> all_but_ssh = Service(exclude=[22]) >>> ssh_or_https = Service(include=[22, 443]) """ include: Optional[List[int]] exclude: Optional[List[int]] @dataclass class Subnet: """Subnet configuration: set of CIDR-formatted IP addresses with services to analyze Examples -------- >>> private_A = Subnet(["10.0.0.0/8"], get_names_of_services_from_config()) >>> private = Subnet(["private-A", "private-B", "private-C"], get_names_of_services_from_config()) # these constants are available for convenience and readability >>> my_network = Subnet(["10.0.0.0/16", "10.1.1.0/24"], get_names_of_services_from_config()) """ cidrs: List[str] services: List[str] @dataclass class DetectionUnitConfig: """Detection unit configuration Parameters ---------- services map from names of service to service definitions subnets map from names of subnet to subnet configurations """ services: Dict[str, Service] subnets: Dict[str, Subnet] @dataclass class TargetPeriod: days: int = 30 @dataclass class ArgumentsConfig: """Arguments modification configuration Parameters ---------- target_period: default target period used to determine date_from and date_to values if missing. """ target_period: TargetPeriod def set_default_date_detect(args, config: ArgumentsConfig): """ Configure training from/to dates according to args and config. Parameters ---------- args Command line args. config Settings for arguments. Returns ------- args Command line args with training from/to dates added. """ date_time_today = datetime.today() if args.date_from is None: args.date_from = date_time_today - timedelta(config.target_period.days) if args.date_to is None: args.date_to = date_time_today - timedelta(1) args.date_from_training = args.date_from - timedelta(args.period_train) args.date_to_training = args.date_from - timedelta(1) return args @dataclass class SkipDetectionConfig: train: int test: int @dataclass class ThresholdConfig: num_anomaly: int min_score: float @dataclass class AnomalyDetectionConfig: required_srcip: SkipDetectionConfig deepsad: detection.DeepSAD.Config train: detection.DeepSAD.TrainConfig threshold: ThresholdConfig @dataclasses.dataclass class DetectConfig: arguments: ArgumentsConfig detection_units: DetectionUnitConfig io: IOConfig preprocess: PreprocessConfig feature_extraction: feature_extraction.FeatureExtractionConfig anomaly_detection: AnomalyDetectionConfig def main_detection(args, config: DetectConfig, log: pd.DataFrame, label: pd.Series): """ Parameters ---------- args config log :index: :columns: label filled with 1 :index: """ dir_report = os.path.join(config.io.output.subdir, args.subnet, args.service) os.makedirs(dir_report, exist_ok=True) feature_label = main_detection_prepare_data( args, config.feature_extraction, log, label ) if feature_label is None: return feature_label.idf_sid.to_csv(os.path.join(dir_report, FILENAME_IDF_SID)) feature_label.idf_dport.to_csv(os.path.join(dir_report, FILENAME_IDF_DPORT)) train_test_splitted, x_train_labeled = main_detection_after_prepare_data( args, label, feature_label ) stats = main_detection_skip_or_detect( args, log, label, dir_report, feature_label, train_test_splitted, x_train_labeled, anomaly_detection_config=config.anomaly_detection, previous_config=config.io.previous.log, ) utils.save_json(stats, path=os.path.join(dir_report, FILENAME_STATS)) def main_detection_prepare_data( args, config: feature_extraction.FeatureExtractionConfig, log: pd.DataFrame, label: pd.Series, ) -> Optional[feature_extraction.FeatureLabel]: """Feature extraction""" logger.info("start detect on subnet %s and service %s", args.subnet, args.service) if len(log) == 0: logger.info("skip analysis; no logs exist") return None logger.info("extracting features") feature_label = feature_extraction.feature_extraction_all( log=log, iptable=pd.read_csv(config.address_to_location), idf_config=config.idf, ) if feature_label is None: logger.info("skip analysis; feature matrix is None") return None feature_label.extract_nonzeros() label = label.loc[label.index & feature_label.index] feature_label.put_labels(labeled_samples=label) feature_label.feature = feature_label.feature / feature_label.feature.max() return feature_label def main_detection_after_prepare_data( args, label: pd.Series, feature_label: feature_extraction.FeatureLabel ): """Split data and construct labeled training feature.""" train_test_splitted = feature_label.split_train_test(args.date_to_training) idx_labeled = [ feature_label.index.index(sample) for sample in label.index if sample in feature_label.index ] x_train_labeled = feature_label.feature[idx_labeled] return train_test_splitted, x_train_labeled def main_detection_skip_or_detect( args, log: pd.DataFrame, label: pd.Series, dir_report: str, feature_label: feature_extraction.FeatureLabel, train_test_splitted, x_train_labeled: csr_matrix, anomaly_detection_config: AnomalyDetectionConfig, previous_config: labeled.Config, ) -> dict: """Anomaly detection and output the result.""" x_train, y_train, x_test, index_test = train_test_splitted stats = { "subnet": args.subnet, "service": args.service, "date_from": args.date_from, "date_to": args.date_to, "num_samples_st_detection": len(index_test), "num_samples_training": len(y_train), "date_from_training": args.date_from_training, "date_to_training": args.date_to_training, "num_samples_labeled": x_train_labeled.shape[0], "samples_labeled": label.index.tolist(), } logger.info("stats: %s", stats) if len(y_train) < anomaly_detection_config.required_srcip.train: skip_message = f"#src_ip[train] = {len(y_train)} < config.anomaly_detection.required_srcip.train = {anomaly_detection_config.required_srcip.train}" logger.info(skip_message) stats["skipped"] = skip_message return stats if len(index_test) < anomaly_detection_config.required_srcip.test: skip_message = f"#src_ip[test] = {len(index_test)} < config.anomaly_detection.required_srcip.test = {anomaly_detection_config.required_srcip.test}" logger.info(skip_message) stats["skipped"] = skip_message return stats logger.info("training detector") verbose = 1 if logger.root.level < 20 else 0 detector = detection.DeepSAD(anomaly_detection_config.deepsad) detector.train( X=x_train, y=y_train, path_model=os.path.join(dir_report, FILENAME_WEIGHT), config=anomaly_detection_config.train, verbose=verbose, ) logger.info("outputting detection reports") anomaly_score = detector.compute_anomaly_score(x_test, scale=True) num_anomaly = min( sum(anomaly_score > anomaly_detection_config.threshold.min_score), anomaly_detection_config.threshold.num_anomaly, ) idx_sorted = np.argsort(anomaly_score)[::-1].tolist() idx_anomaly = idx_sorted[:num_anomaly] anomaly_score_sorted = pd.Series( anomaly_score[idx_sorted], index=pd.MultiIndex.from_tuples( [index_test[i] for i in idx_sorted], names=(col.DATETIME_ROUNDED, col.SRC_IP), ), name="anomaly_score", ) x_test_embeddings = detector.compute_embeddings(x_test) x_train_labeled_embeddings = detector.compute_embeddings(x_train_labeled) shap_value_idx_sorted = detector.explain_anomaly( x_test[idx_anomaly], background_samples=x_train ) shap_value_idx_sorted = pd.DataFrame( shap_value_idx_sorted, index=pd.MultiIndex.from_tuples( [index_test[i] for i in idx_anomaly], names=(col.DATETIME_ROUNDED, col.SRC_IP), ), columns=feature_label.columns, ) stats = output_result( args, log, label, dir_report, x_train_labeled_embeddings=x_train_labeled_embeddings, x_test_embeddings=x_test_embeddings, idx_anomaly=idx_anomaly, shap_value_idx_sorted=shap_value_idx_sorted, anomaly_score_sorted=anomaly_score_sorted, stats=stats, previous_config=previous_config, ) if args.debug: if isinstance(x_test, csr_matrix): x_test = x_test.toarray() ret = pd.DataFrame(x_test, index=index_test, columns=feature_label.columns) ret = ret.iloc[idx_sorted] ret.to_csv(os.path.join(dir_report, FILENAME_FEATURE_MATRIX)) return stats def output_result( args, log: pd.DataFrame, label: pd.Series, dir_report: str, *, x_train_labeled_embeddings, x_test_embeddings, idx_anomaly, shap_value_idx_sorted, anomaly_score_sorted, stats: dict, previous_config: labeled.Config, ): """Plot the detection result and output the report.""" reporting.plot.plot_detection( X=x_test_embeddings, idx_anomaly=idx_anomaly, name_anomaly=shap_value_idx_sorted.index, X_labeled=x_train_labeled_embeddings, name_labeled=label.index, path_saved=os.path.join(dir_report, FILENAME_PLOT_DETECTION), no_plot=args.no_plot, ) detection.detection_report( anomaly_score_sorted, shap_value_idx_sorted, shap_top_k=5, ).to_csv(os.path.join(dir_report, FILENAME_REPORT)) labeled.factory(previous_config)[1].save_previous_log( df=log, entries=shap_value_idx_sorted.index, ) stats["num_anomaly"] = len(idx_anomaly) stats["name_anomaly"] = shap_value_idx_sorted.index.tolist() logger.info( "successfully finish detection on subnet %s and service %s\n", args.subnet, args.service, ) return stats def report_all(path_list_stats: List[str], path_save: str): """ Summarizing all reports and save it. Parameters ---------- path_list_stats : list List of stats file paths path_save : str File path where the report will be saved """ os.makedirs(os.path.dirname(path_save), exist_ok=True) logger.info("summarizing all reports...") results_pd = pd.DataFrame( [], columns=["datetime_rounded", "src_ip", "subnet", "service"] ) idx = 0 for path in path_list_stats: # Load stats stats = utils.load_json(path) subnet, service = stats["subnet"], stats["service"] try: anomaly_list = stats["name_anomaly"] except (KeyError, TypeError): continue if not anomaly_list: continue # Load report path_report = path.replace(FILENAME_STATS, FILENAME_REPORT) report = pd.read_csv(path_report, index_col=[0, 1], parse_dates=[0]) logger.info(report.index) # Store anomalies in the DataFrame for (dt, src_ip) in anomaly_list: logger.info((dt, src_ip)) results_pd.loc[idx] = [dt, src_ip, subnet, service] if idx == 0: results_shaps = pd.DataFrame([], columns=report.columns) results_shaps.loc[idx] = report.loc[(dt, src_ip)] idx += 1 anomaly_found = idx > 0 if anomaly_found: # Anomaly found results_pd = pd.concat([results_pd, results_shaps], axis=1) results_pd = results_pd.sort_values( ["anomaly_score", "datetime_rounded"], ascending=False ) keys = results_pd["src_ip"].unique() results_pd_group = results_pd.groupby("src_ip") ret = pd.DataFrame([]) for key in keys: ret = pd.concat([ret, results_pd_group.get_group(key)]) ret.round(4).to_csv(path_save, index=False) else: # Anomaly not found pd.DataFrame([["no anomaly found"]]).to_csv(path_save, index=False) logger.info("[RESULT]", extra=to_stderr) logger.info("Detection summary file: %s", path_save, extra=to_stderr) num_anomaly_ipaddr = len(keys) if anomaly_found else 0 logger.info( "Number of unique anomaly IP addresses: %s", num_anomaly_ipaddr, extra=to_stderr ) if anomaly_found: for src_ip in keys: max_anomaly_score = max( results_pd.query("src_ip == @src_ip")["anomaly_score"] ) logger.info( "- %s (max anomaly score: %s)", src_ip, max_anomaly_score, extra=to_stderr, ) def report_transfer(path: str, dir_to: str): """ Copy Report Files to the Directory Parameters ---------- path : str File path of the report to copy. dir_to : str Directory Path of destination directory. If you specify a directory that does not exist, a new directory is created. Raises ------ TypeError Destination directory not specified. TypeError Report file does not exist. """ # Argument checking if dir_to is None: raise TypeError("Destination directory not specified.") if not os.path.isfile(path): raise TypeError("Report file does not exist.") # Copy a report to a directory try: os.makedirs(dir_to, exist_ok=True) shutil.copy(path, dir_to) except Exception as ex: logger.error( "failed transfer report %s to %s, the error message: %s", path, dir_to, ex ) raise ex else: logger.info("successfully transfered report %s to %s", path, dir_to) def main_preproc_and_detection(args, config: DetectConfig): """Data preprocessing and anomaly detection.""" log_all = load_preprocess_log(args, config) # Detecting for each subnet for subnet in config.detection_units.subnets.items(): for service_name in subnet[1].services: detect_per_unit(config, service_name, log_all, subnet, args) # Reporting path_report_all = os.path.join(config.io.output.subdir, FILENAME_REPORT) path_list_stats = glob.glob( os.path.join(config.io.output.subdir, "**", FILENAME_STATS), recursive=True, ) report_all(path_list_stats, path_save=path_report_all) if config.io.output.share_dir is not None: report_transfer( path_report_all, dir_to=os.path.join( config.io.output.share_dir, args.date_from.strftime(COMMANDLINE_DATE_FORMAT) + "__" + args.date_to.strftime(COMMANDLINE_DATE_FORMAT), ), ) def load_preprocess_log(args, config: DetectConfig): """Load and preprocess log. Warnings -------- Sets config.io.output.subdir """ # Loading logs logger.info( "loading logs during the period from %s to %s", args.date_from_training, args.date_to, ) config.io.output.subdir = os.path.join( config.io.output.dir, args.date_from.strftime(COMMANDLINE_DATE_FORMAT) + "__" + args.date_to.strftime(COMMANDLINE_DATE_FORMAT), ) log_all = load_log( dir_IDS_log=config.io.input.dir, date_from=args.date_from_training, date_to=args.date_to, nrows_read=args.nrows_read, ) log_all = apply_exclude_lists(log_all, config.preprocess.exclude_lists) logger.info("[TARGET INFO]", extra=to_stderr) logger.info("Number of log entries loaded: %d", len(log_all), extra=to_stderr) logger.info( "Number of unique source IP addresses: %d", len(log_all.reset_index()["src_ip"].unique()), extra=to_stderr, ) # Preprocessing logs logger.info("preprocessing logs") log_all = preprocess.screening_numlog(log_all, config.preprocess.screening) return log_all def detect_per_unit( config: DetectConfig, service_name: str, log_all: pd.DataFrame, subnet: Tuple[str, Subnet], args, ): service = config.detection_units.services[service_name] log = preprocess.extract_log( log_all, subnets=subnet[1].cidrs, include_ports=service.include, exclude_ports=service.exclude, ) # one can also load_previous(known_anomaly, label_value=1) # and/or load_previous(unknown, label_value=None). # known_anomaly can be concat-ed with known_normal # since both have label values. # log_unknown can be concat-ed with log without label values. known_normal = load_previous( config=config.io.previous.load.known_normal, date_to=args.date_to_training, label_value=1, ) log_labeled = labeled.factory(config.io.previous.log)[0].load_previous_log( entries=known_normal.index, ) log_labeled = apply_exclude_lists(log_labeled, config.preprocess.exclude_lists) log_labeled = preprocess.extract_log( log_labeled, subnets=subnet[1].cidrs, include_ports=service.include, exclude_ports=service.exclude, ) log.drop( known_normal.index.tolist(), level=col.SRC_IP, inplace=True, errors="ignore" ) args.subnet = subnet[0] args.service = service_name main_detection(args, config, log=pd.concat([log, log_labeled]), label=known_normal) def load_log( dir_IDS_log: str, date_from: datetime, date_to: datetime, nrows_read: Optional[int] = None, ) -> pd.DataFrame: """ load IDS logs of the dates in [ date_from, date_to ] Parameters ---------- dir_IDS_log The path of the directory containing logs to be load. date_from Start date. date_to End date. nrows_read Maximum number of rows to load, by default None Returns ------- log IDS log. """ from psykoda.io.reader._fujitsu_splunk import FujitsuSplunk from psykoda.preprocess import ( FastRoundDatetime, RoundDatetime, drop_null, set_index, ) from psykoda.utils import daterange2list daterange = daterange2list(date_from, date_to) def _load_log_catch(load, r): for dt in r: try: yield load(dt) except FileNotFoundError as ex: logger.warning("not found: %s", dt, exc_info=ex) io = FujitsuSplunk(dir_IDS_log=dir_IDS_log, nrows_read=nrows_read) log = set_index( FastRoundDatetime("hour")(pd.concat(_load_log_catch(io.load_log, daterange))) ) log = drop_null(log) return log def load_previous( config: LoadPreviousConfigItem, date_to: datetime, label_value: float ) -> pd.Series: from psykoda.preprocess import round_datetime from psykoda.utils import DateRange if config.list is None: return pd.Series() def date_filter(row): assert isinstance(row.name[0], datetime) return round_datetime(row.name[0], "day") in DateRange( end_inclusive=date_to, length=config.ndate ) try: df = pd.read_csv( config.list, index_col=[col.DATETIME_ROUNDED, col.SRC_IP], parse_dates=[col.DATETIME_ROUNDED], ) except FileNotFoundError as ex: logger.warning( "Specified list of %s was not found. " "Create one with at least %s columns, " "or remove the path or entry from config.", config.list, (col.DATETIME_ROUNDED, col.SRC_IP), exc_info=ex, ) return pd.Series() df = df[df.apply(date_filter, axis=1)] return pd.Series(label_value, index=df.index) def apply_exclude_lists( log: pd.DataFrame, dir_exclude_lists: Optional[str] ) -> pd.DataFrame: """ exclude logs according to exclude lists in dir_exclude_lists Parameters ---------- log Source log. dir_exclude_lists The path of directory containing exclude list csv files. Returns ------- log Log after applying exclude list. """ from os import path from psykoda.constants import EXCLUDE_LIST_FILE_SPLITSTR, EXCLUDE_LIST_PREFIX from psykoda.preprocess import exclude_log assert log is not None if dir_exclude_lists is None: return log path_list = glob.glob(path.join(dir_exclude_lists, EXCLUDE_LIST_PREFIX + "*.csv")) if not path_list: logger.warning("no exclude_list files exist") return log exclusion = ( { "column_name": path.splitext(path.basename(filter_path))[0].split( EXCLUDE_LIST_FILE_SPLITSTR )[1], "filter_patterns": validate_patterns( pd.read_csv(filter_path, index_col=0).index ), } for filter_path in path_list ) return exclude_log(log, exclusion) def validate_patterns(patterns: pd.Index): """ Strip whitespaces in Index from left and right sides. Parameters ---------- patterns Index. Returns ------- ret Index with whitespace removed. """ import pandas.api.types as types # pylint: disable=import-outside-toplevel # convention if not
types.is_string_dtype(patterns)
pandas.api.types.is_string_dtype
#!/usr/bin/env python # -*- coding: utf-8 -*- """Import OptionMetrics data. """ from __future__ import print_function, division import os import zipfile import numpy as np import pandas as pd import datetime as dt from scipy.interpolate import interp1d from impvol import lfmoneyness, delta, vega from datastorage.quandlweb import load_spx path = os.getenv("HOME") + '/Dropbox/Research/data/OptionMetrics/data/' # __location__ = os.path.realpath(os.path.join(os.getcwd(), # os.path.dirname(__file__))) # path = os.path.join(__location__, path + 'OptionMetrics/data/') def convert_dates(string): return dt.datetime.strptime(string, '%d-%m-%Y') def import_dividends(): """Import dividends. """ zf = zipfile.ZipFile(path + 'SPX_dividend.zip', 'r') name = zf.namelist()[0] dividends = pd.read_csv(zf.open(name), converters={'date': convert_dates}) dividends.set_index('date', inplace=True) dividends.sort_index(inplace=True) print(dividends.head()) dividends.to_hdf(path + 'dividends.h5', 'dividends') def import_yield_curve(): """Import zero yield curve. """ zf = zipfile.ZipFile(path + 'yield_curve.zip', 'r') name = zf.namelist()[0] yields = pd.read_csv(zf.open(name), converters={'date': convert_dates}) # Remove weird observations yields = yields[yields['rate'] < 10] # Fill in the blanks in the yield curve # yields = interpolate_curve(yields) yields.rename(columns={'rate': 'riskfree'}, inplace=True) yields.set_index(['date', 'days'], inplace=True) yields.sort_index(inplace=True) print(yields.head()) yields.to_hdf(path + 'yields.h5', 'yields') def interpolate_curve_group(group): """Interpolate yields for one day. """ y = np.array(group.riskfree) x = np.array(group.days) a = group.days.min() b = group.days.max() new_x = np.linspace(a, b, b-a+1).astype(int) try: new_y = interp1d(x, y, kind='cubic')(new_x) except: new_y = interp1d(x, y, kind='linear')(new_x) group = pd.DataFrame(new_y, index=pd.Index(new_x, name='days')) return group def interpolate_curve(yields): """Fill in the blanks in the yield curve. """ yields.reset_index(inplace=True) yields = yields.groupby('date').apply(interpolate_curve_group) yields = yields.unstack('days') yields.fillna(method='ffill', axis=1, inplace=True) yields.fillna(method='bfill', axis=1, inplace=True) yields = yields.stack('days') yields.rename(columns={0: 'riskfree'}, inplace=True) return yields def import_riskfree(): """Take the last value of the yield curve as a risk-free rate. Saves annualized rate in percentage points. """ yields = load_yields() riskfree = yields.groupby(level='date').last() print(riskfree.head()) riskfree.to_hdf(path + 'riskfree.h5', 'riskfree') def import_standard_options(): """Import standardized options. """ zf = zipfile.ZipFile(path + 'SPX_standard_options.zip', 'r') name = zf.namelist()[0] data = pd.read_csv(zf.open(name), converters={'date': convert_dates}) cols = {'forward_price': 'forward', 'impl_volatility': 'imp_vol'} data.rename(columns=cols, inplace=True) data = data.set_index(['cp_flag', 'date', 'days']).sort_index() print(data.head()) data.to_hdf(path + 'std_options.h5', 'std_options') def import_vol_surface(): """Import volatility surface. Infer risk-free rate directly from data. """ zf = zipfile.ZipFile(path + 'SPX_surface.zip', 'r') name = zf.namelist()[0] df = pd.read_csv(zf.open(name), converters={'date': convert_dates}) df.loc[:, 'weekday'] = df['date'].apply(lambda x: x.weekday()) # Apply some filters df = df[df['weekday'] == 2] df = df[df['days'] <= 365] df = df.drop('weekday', axis=1) surface = df#.set_index(['cp_flag', 'date', 'days']).sort_index() cols = {'impl_volatility': 'imp_vol', 'impl_strike': 'strike', 'impl_premium': 'premium'} surface.rename(columns=cols, inplace=True) # TODO : who term structure should be imported and merged! riskfree = load_riskfree().reset_index() dividends = load_dividends().reset_index() spx = load_spx().reset_index() surface = pd.merge(surface, riskfree) surface = pd.merge(surface, spx) surface = pd.merge(surface, dividends) # Adjust riskfree by dividend yield surface['riskfree'] -= surface['rate'] # Remove percentage point surface['riskfree'] /= 100 # Replace 'cp_flag' with True/False 'call' variable surface.loc[:, 'call'] = True surface.loc[surface['cp_flag'] == 'P', 'call'] = False # Normalize maturity to being a share of the year surface['maturity'] = surface['days'] / 365 # Rename columns surface.rename(columns={'spx': 'price'}, inplace=True) # Compute lf-moneyness surface['moneyness'] = lfmoneyness(surface['price'], surface['strike'], surface['riskfree'], surface['maturity']) # Compute option Delta normalized by current price surface['delta'] = delta(surface['moneyness'], surface['maturity'], surface['imp_vol'], surface['call']) # Compute option Vega normalized by current price surface['vega'] = vega(surface['moneyness'], surface['maturity'], surface['imp_vol']) # Sort index surface.sort_index(by=['date', 'maturity', 'moneyness'], inplace=True) print(surface.head()) surface.to_hdf(path + 'surface.h5', 'surface') def import_vol_surface_simple(): """Import volatility surface. Simple version. """ zf = zipfile.ZipFile(path + 'SPX_surface.zip', 'r') name = zf.namelist()[0] df = pd.read_csv(zf.open(name), converters={'date': convert_dates}) df.loc[:, 'weekday'] = df['date'].apply(lambda x: x.weekday()) # Apply some filters df = df[df['weekday'] == 2] df = df[df['days'] <= 365] surface = df.drop('weekday', axis=1) cols = {'impl_volatility': 'imp_vol', 'impl_strike': 'strike', 'impl_premium': 'premium'} surface.rename(columns=cols, inplace=True) spx = load_spx().reset_index() standard_options = load_standard_options()[['forward']].reset_index() surface = pd.merge(surface, standard_options) surface = pd.merge(surface, spx) # Normalize maturity to being a share of the year surface['maturity'] = surface['days'] / 365 surface['riskfree'] = np.log(surface['forward'] / surface['spx']) surface['riskfree'] /= surface['maturity'] # Remove percentage point # Replace 'cp_flag' with True/False 'call' variable surface.loc[:, 'call'] = True surface.loc[surface['cp_flag'] == 'P', 'call'] = False # Rename columns surface.rename(columns={'spx': 'price'}, inplace=True) # Compute lf-moneyness surface['moneyness'] = lfmoneyness(surface['price'], surface['strike'], surface['riskfree'], surface['maturity']) # Compute option Delta normalized by current price surface['delta'] = delta(surface['moneyness'], surface['maturity'], surface['imp_vol'], surface['call']) # Compute option Vega normalized by current price surface['vega'] = vega(surface['moneyness'], surface['maturity'], surface['imp_vol']) # Take out-of-the-money options calls = surface['call'] & (surface['moneyness'] >= 0) puts = np.logical_not(surface['call']) & (surface['moneyness'] < 0) surface = pd.concat([surface[calls], surface[puts]]) # Sort index surface.sort_index(by=['date', 'maturity', 'moneyness'], inplace=True) print(surface.head()) surface.to_hdf(path + 'surface.h5', 'surface') def load_dividends(): """Load dividends from the disk (annualized, percentage points). Typical output: rate date 1996-01-04 2.460 1996-01-05 2.492 1996-01-08 2.612 1996-01-09 2.455 1996-01-10 2.511 """ return pd.read_hdf(path + 'dividends.h5', 'dividends') def load_yields(): """Load zero yield curve from the disk (annualized, percentage points). Typical output: riskfree date days 1996-01-02 9 5.763 15 5.746 50 5.673 78 5.609 169 5.474 """ return pd.read_hdf(path + 'yields.h5', 'yields') def load_riskfree(): """Load risk-free rate (annualized, percentage points). Returns ------- DataFrame Annualized rate in percentage points Typical output: riskfree date 1996-01-02 6.138 1996-01-03 6.125 1996-01-04 6.142 1996-01-05 6.219 1996-01-08 6.220 """ return pd.read_hdf(path + 'riskfree.h5', 'riskfree') def load_standard_options(): """Load standardized options from the disk. Typical output: forward premium imp_vol cp_flag date days C 1996-01-04 30 619.345 7.285 0.103 60 620.935 10.403 0.105 91 622.523 12.879 0.105 122 624.080 16.156 0.114 152 625.545 18.259 0.116 """ return
pd.read_hdf(path + 'std_options.h5', 'std_options')
pandas.read_hdf
from flowsa.common import WITHDRAWN_KEYWORD from flowsa.flowbyfunctions import assign_fips_location_system from flowsa.location import US_FIPS import math import pandas as pd import io from flowsa.settings import log from string import digits YEARS_COVERED = { "asbestos": "2014-2018", "barite": "2014-2018", "bauxite": "2013-2017", "beryllium": "2014-2018", "boron": "2014-2018", "chromium": "2014-2018", "clay": "2015-2016", "cobalt": "2013-2017", "copper": "2011-2015", "diatomite": "2014-2018", "feldspar": "2013-2017", "fluorspar": "2013-2017", "fluorspar_inports": ["2016", "2017"], "gallium": "2014-2018", "garnet": "2014-2018", "gold": "2013-2017", "graphite": "2013-2017", "gypsum": "2014-2018", "iodine": "2014-2018", "ironore": "2014-2018", "kyanite": "2014-2018", "lead": "2012-2018", "lime": "2014-2018", "lithium": "2013-2017", "magnesium": "2013-2017", "manganese": "2012-2016", "manufacturedabrasive": "2017-2018", "mica": "2014-2018", "molybdenum": "2014-2018", "nickel": "2012-2016", "niobium": "2014-2018", "peat": "2014-2018", "perlite": "2013-2017", "phosphate": "2014-2018", "platinum": "2014-2018", "potash": "2014-2018", "pumice": "2014-2018", "rhenium": "2014-2018", "salt": "2013-2017", "sandgravelconstruction": "2013-2017", "sandgravelindustrial": "2014-2018", "silver": "2012-2016", "sodaash": "2010-2017", "sodaash_t4": ["2016", "2017"], "stonecrushed": "2013-2017", "stonedimension": "2013-2017", "strontium": "2014-2018", "talc": "2013-2017", "titanium": "2013-2017", "tungsten": "2013-2017", "vermiculite": "2014-2018", "zeolites": "2014-2018", "zinc": "2013-2017", "zirconium": "2013-2017", } def usgs_myb_year(years, current_year_str): """ Sets the column for the string based on the year. Checks that the year you picked is in the last file. :param years: string, with hypthon :param current_year_str: string, year of interest :return: string, year """ years_array = years.split("-") lower_year = int(years_array[0]) upper_year = int(years_array[1]) current_year = int(current_year_str) if lower_year <= current_year <= upper_year: column_val = current_year - lower_year + 1 return "year_" + str(column_val) else: log.info("Your year is out of scope. Pick a year between %s and %s", lower_year, upper_year) def usgs_myb_name(USGS_Source): """ Takes the USGS source name and parses it so it can be used in other parts of Flow by activity. :param USGS_Source: string, usgs source name :return: """ source_split = USGS_Source.split("_") name_cc = str(source_split[2]) name = "" for char in name_cc: if char.isupper(): name = name + " " + char else: name = name + char name = name.lower() name = name.strip() return name def usgs_myb_static_variables(): """ Populates the data values for Flow by activity that are the same for all of USGS_MYB Files :return: """ data = {} data["Class"] = "Geological" data['FlowType'] = "ELEMENTARY_FLOWS" data["Location"] = US_FIPS data["Compartment"] = "ground" data["Context"] = None data["ActivityConsumedBy"] = None return data def usgs_myb_remove_digits(value_string): """ Eliminates numbers in a string :param value_string: :return: """ remove_digits = str.maketrans('', '', digits) return_string = value_string.translate(remove_digits) return return_string def usgs_myb_url_helper(*, build_url, **_): """ This helper function uses the "build_url" input from flowbyactivity.py, which is a base url for data imports that requires parts of the url text string to be replaced with info specific to the data year. This function does not parse the data, only modifies the urls from which data is obtained. :param build_url: string, base url :param config: dictionary, items in FBA method yaml :param args: dictionary, arguments specified when running flowbyactivity.py flowbyactivity.py ('year' and 'source') :return: list, urls to call, concat, parse, format into Flow-By-Activity format """ return [build_url] def usgs_asbestos_call(*, resp, year, **_): """ Convert response for calling url to pandas dataframe, begin parsing df into FBA format :param resp: df, response from url call :param year: year :return: pandas dataframe of original source data """ df_raw_data = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T1') df_data = pd.DataFrame(df_raw_data.loc[4:11]).reindex() df_data = df_data.reset_index() del df_data["index"] if len(df_data.columns) > 12: for x in range(12, len(df_data.columns)): col_name = "Unnamed: " + str(x) del df_data[col_name] if len(df_data. columns) == 12: df_data.columns = ["Production", "Unit", "space_1", "year_1", "space_2", "year_2", "space_3", "year_3", "space_4", "year_4", "space_5", "year_5"] col_to_use = ["Production"] col_to_use.append(usgs_myb_year(YEARS_COVERED['asbestos'], year)) for col in df_data.columns: if col not in col_to_use: del df_data[col] return df_data def usgs_asbestos_parse(*, df_list, source, year, **_): """ Combine, parse, and format the provided dataframes :param df_list: list of dataframes to concat and format :param source: source :param year: year :return: df, parsed and partially formatted to flowbyactivity specifications """ data = {} row_to_use = ["Quantity"] product = "" name = usgs_myb_name(source) des = name dataframe = pd.DataFrame() col_name = usgs_myb_year(YEARS_COVERED['asbestos'], year) for df in df_list: for index, row in df.iterrows(): if df.iloc[index]["Production"].strip() == \ "Imports for consumption:": product = "imports" elif df.iloc[index]["Production"].strip() == \ "Exports and reexports:": product = "exports" if df.iloc[index]["Production"].strip() in row_to_use: data = usgs_myb_static_variables() data["SourceName"] = source data["Year"] = str(year) data["Unit"] = "Metric Tons" data['FlowName'] = name + " " + product data["Description"] = name data["ActivityProducedBy"] = name col_name = usgs_myb_year(YEARS_COVERED['asbestos'], year) if str(df.iloc[index][col_name]) == "--": data["FlowAmount"] = str(0) elif str(df.iloc[index][col_name]) == "nan": data["FlowAmount"] = WITHDRAWN_KEYWORD else: data["FlowAmount"] = str(df.iloc[index][col_name]) dataframe = dataframe.append(data, ignore_index=True) dataframe = assign_fips_location_system(dataframe, str(year)) return dataframe def usgs_barite_call(*, resp, year, **_): """ Convert response for calling url to pandas dataframe, begin parsing df into FBA format :param url: string, url :param resp: df, response from url call :param args: dictionary, arguments specified when running flowbyactivity.py ('year' and 'source') :return: pandas dataframe of original source data """ df_raw_data = pd.io.excel.read_excel( io.BytesIO(resp.content), sheet_name='T1') df_data = pd.DataFrame(df_raw_data.loc[7:14]).reindex() df_data = df_data.reset_index() del df_data["index"] if len(df_data. columns) == 11: df_data.columns = ["Production", "space_1", "year_1", "space_2", "year_2", "space_3", "year_3", "space_4", "year_4", "space_5", "year_5"] col_to_use = ["Production"] col_to_use.append(usgs_myb_year(YEARS_COVERED['barite'], year)) for col in df_data.columns: if col not in col_to_use: del df_data[col] return df_data def usgs_barite_parse(*, df_list, source, year, **_): """ Combine, parse, and format the provided dataframes :param df_list: list of dataframes to concat and format :param args: dictionary, used to run flowbyactivity.py ('year' and 'source') :return: df, parsed and partially formatted to flowbyactivity specifications """ data = {} row_to_use = ["Quantity"] prod = "" name = usgs_myb_name(source) des = name dataframe = pd.DataFrame() col_name = usgs_myb_year(YEARS_COVERED['barite'], year) for df in df_list: for index, row in df.iterrows(): if df.iloc[index]["Production"].strip() == \ "Imports for consumption:3": product = "imports" elif df.iloc[index]["Production"].strip() == \ "Crude, sold or used by producers:": product = "production" elif df.iloc[index]["Production"].strip() == "Exports:2": product = "exports" if df.iloc[index]["Production"].strip() in row_to_use: data = usgs_myb_static_variables() data["SourceName"] = source data["Year"] = str(year) data["Unit"] = "Metric Tons" data['FlowName'] = name + " " + product data["Description"] = name data["ActivityProducedBy"] = name col_name = usgs_myb_year(YEARS_COVERED['barite'], year) if str(df.iloc[index][col_name]) == "--" or \ str(df.iloc[index][col_name]) == "(3)": data["FlowAmount"] = str(0) else: data["FlowAmount"] = str(df.iloc[index][col_name]) dataframe = dataframe.append(data, ignore_index=True) dataframe = assign_fips_location_system( dataframe, str(year)) return dataframe def usgs_bauxite_call(*, resp, year, **_): """ Convert response for calling url to pandas dataframe, begin parsing df into FBA format :param resp: df, response from url call :param year: year :return: pandas dataframe of original source data """ df_raw_data_one = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T1') df_data_one = pd.DataFrame(df_raw_data_one.loc[6:14]).reindex() df_data_one = df_data_one.reset_index() del df_data_one["index"] if len(df_data_one. columns) == 11: df_data_one.columns = ["Production", "space_2", "year_1", "space_3", "year_2", "space_4", "year_3", "space_5", "year_4", "space_6", "year_5"] col_to_use = ["Production"] col_to_use.append(usgs_myb_year(YEARS_COVERED['bauxite'], year)) for col in df_data_one.columns: if col not in col_to_use: del df_data_one[col] frames = [df_data_one] df_data = pd.concat(frames) df_data = df_data.reset_index() del df_data["index"] return df_data def usgs_bauxite_parse(*, df_list, source, year, **_): """ Combine, parse, and format the provided dataframes :param df_list: list of dataframes to concat and format :param args: dictionary, used to run flowbyactivity.py ('year' and 'source') :return: df, parsed and partially formatted to flowbyactivity specifications """ data = {} row_to_use = ["Production", "Total"] prod = "" name = usgs_myb_name(source) des = name dataframe = pd.DataFrame() col_name = usgs_myb_year(YEARS_COVERED['bauxite'], year) for df in df_list: for index, row in df.iterrows(): if df.iloc[index]["Production"].strip() == "Production": prod = "production" elif df.iloc[index]["Production"].strip() == \ "Imports for consumption, as shipped:": prod = "import" elif df.iloc[index]["Production"].strip() == \ "Exports, as shipped:": prod = "export" if df.iloc[index]["Production"].strip() in row_to_use: product = df.iloc[index]["Production"].strip() data = usgs_myb_static_variables() data["SourceName"] = source data["Year"] = str(year) data["Unit"] = "Metric Tons" flow_amount = str(df.iloc[index][col_name]) if str(df.iloc[index][col_name]) == "W": data["FlowAmount"] = WITHDRAWN_KEYWORD else: data["FlowAmount"] = flow_amount data["Description"] = des data["ActivityProducedBy"] = name data['FlowName'] = name + " " + prod dataframe = dataframe.append(data, ignore_index=True) dataframe = assign_fips_location_system( dataframe, str(year)) return dataframe def usgs_beryllium_call(*, resp, year, **_): """ Convert response for calling url to pandas dataframe, begin parsing df into FBA format :param url: string, url :param resp: df, response from url call :param args: dictionary, arguments specified when running flowbyactivity.py ('year' and 'source') :return: pandas dataframe of original source data """ df_raw_data = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T4') df_raw_data_two = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T1') df_data_1 = pd.DataFrame(df_raw_data_two.loc[6:9]).reindex() df_data_1 = df_data_1.reset_index() del df_data_1["index"] df_data_2 = pd.DataFrame(df_raw_data.loc[12:12]).reindex() df_data_2 = df_data_2.reset_index() del df_data_2["index"] if len(df_data_2.columns) > 11: for x in range(11, len(df_data_2.columns)): col_name = "Unnamed: " + str(x) del df_data_2[col_name] if len(df_data_1. columns) == 11: df_data_1.columns = ["Production", "space_1", "year_1", "space_2", "year_2", "space_3", "year_3", "space_4", "year_4", "space_5", "year_5"] if len(df_data_2. columns) == 11: df_data_2.columns = ["Production", "space_1", "year_1", "space_2", "year_2", "space_3", "year_3", "space_4", "year_4", "space_5", "year_5"] col_to_use = ["Production"] col_to_use.append(usgs_myb_year(YEARS_COVERED['beryllium'], year)) for col in df_data_1.columns: if col not in col_to_use: del df_data_1[col] for col in df_data_2.columns: if col not in col_to_use: del df_data_2[col] frames = [df_data_1, df_data_2] df_data = pd.concat(frames) df_data = df_data.reset_index() del df_data["index"] return df_data def usgs_beryllium_parse(*, df_list, source, year, **_): """ Combine, parse, and format the provided dataframes :param df_list: list of dataframes to concat and format :param args: dictionary, used to run flowbyactivity.py ('year' and 'source') :return: df, parsed and partially formatted to flowbyactivity specifications """ data = {} row_to_use = ["United States6", "Mine shipments1", "Imports for consumption, beryl2"] prod = "" name = usgs_myb_name(source) des = name dataframe = pd.DataFrame() col_name = usgs_myb_year(YEARS_COVERED['beryllium'], year) for df in df_list: for index, row in df.iterrows(): prod = "production" if df.iloc[index]["Production"].strip() == \ "Imports for consumption, beryl2": prod = "imports" if df.iloc[index]["Production"].strip() in row_to_use: remove_digits = str.maketrans('', '', digits) product = df.iloc[index][ "Production"].strip().translate(remove_digits) data = usgs_myb_static_variables() data["SourceName"] = source data["Year"] = str(year) data["Unit"] = "Thousand Metric Tons" data["Description"] = name data["ActivityProducedBy"] = name data['FlowName'] = name + " " + prod data["FlowAmount"] = str(df.iloc[index][col_name]) dataframe = dataframe.append(data, ignore_index=True) dataframe = assign_fips_location_system( dataframe, str(year)) return dataframe def usgs_boron_call(*, resp, year, **_): """ Convert response for calling url to pandas dataframe, begin parsing df into FBA format :param url: string, url :param resp: df, response from url call :param args: dictionary, arguments specified when running flowbyactivity.py ('year' and 'source') :return: pandas dataframe of original source data """ df_raw_data = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T1') df_data_one = pd.DataFrame(df_raw_data.loc[8:8]).reindex() df_data_one = df_data_one.reset_index() del df_data_one["index"] df_data_two = pd.DataFrame(df_raw_data.loc[21:22]).reindex() df_data_two = df_data_two.reset_index() del df_data_two["index"] df_data_three = pd.DataFrame(df_raw_data.loc[27:28]).reindex() df_data_three = df_data_three.reset_index() del df_data_three["index"] if len(df_data_one. columns) == 11: df_data_one.columns = ["Production", "space_1", "year_1", "space_2", "year_2", "space_3", "year_3", "space_4", "year_4", "space_5", "year_5"] df_data_two.columns = ["Production", "space_1", "year_1", "space_2", "year_2", "space_3", "year_3", "space_4", "year_4", "space_5", "year_5"] df_data_three.columns = ["Production", "space_1", "year_1", "space_2", "year_2", "space_3", "year_3", "space_4", "year_4", "space_5", "year_5"] col_to_use = ["Production"] col_to_use.append(usgs_myb_year(YEARS_COVERED['boron'], year)) for col in df_data_one.columns: if col not in col_to_use: del df_data_one[col] del df_data_two[col] del df_data_three[col] frames = [df_data_one, df_data_two, df_data_three] df_data = pd.concat(frames) df_data = df_data.reset_index() del df_data["index"] return df_data def usgs_boron_parse(*, df_list, source, year, **_): """ Combine, parse, and format the provided dataframes :param df_list: list of dataframes to concat and format :param args: dictionary, used to run flowbyactivity.py ('year' and 'source') :return: df, parsed and partially formatted to flowbyactivity specifications """ data = {} row_to_use = ["B2O3 content", "Quantity"] prod = "" name = usgs_myb_name(source) des = name dataframe = pd.DataFrame() col_name = usgs_myb_year(YEARS_COVERED['boron'], year) for df in df_list: for index, row in df.iterrows(): if df.iloc[index]["Production"].strip() == "B2O3 content" or \ df.iloc[index]["Production"].strip() == "Quantity": product = "production" if df.iloc[index]["Production"].strip() == "Colemanite:4": des = "Colemanite" elif df.iloc[index]["Production"].strip() == "Ulexite:4": des = "Ulexite" if df.iloc[index]["Production"].strip() in row_to_use: data = usgs_myb_static_variables() data["SourceName"] = source data["Year"] = str(year) data["Unit"] = "Metric Tons" if des == name: data['FlowName'] = name + " " + product else: data['FlowName'] = name + " " + product + " " + des data["Description"] = des data["ActivityProducedBy"] = name if str(df.iloc[index][col_name]) == "--" or \ str(df.iloc[index][col_name]) == "(3)": data["FlowAmount"] = str(0) elif str(df.iloc[index][col_name]) == "W": data["FlowAmount"] = WITHDRAWN_KEYWORD else: data["FlowAmount"] = str(df.iloc[index][col_name]) dataframe = dataframe.append(data, ignore_index=True) dataframe = assign_fips_location_system( dataframe, str(year)) return dataframe def usgs_chromium_call(*, resp, year, **_): """" Convert response for calling url to pandas dataframe, begin parsing df into FBA format :param url: string, url :param resp: df, response from url call :param args: dictionary, arguments specified when running flowbyactivity.py ('year' and 'source') :return: pandas dataframe of original source data """ df_raw_data = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T1') df_data = pd.DataFrame(df_raw_data.loc[4:24]).reindex() df_data = df_data.reset_index() del df_data["index"] if len(df_data. columns) == 12: df_data.columns = ["Production", "Unit", "space_1", "year_1", "space_2", "year_2", "space_3", "year_3", "space_4", "year_4", "space_5", "year_5"] elif len(df_data. columns) == 13: df_data.columns = ["Production", "Unit", "space_1", "year_1", "space_2", "year_2", "space_3", "year_3", "space_4", "year_4", "space_5", "year_5", "space_6"] col_to_use = ["Production"] col_to_use.append(usgs_myb_year(YEARS_COVERED['chromium'], year)) for col in df_data.columns: if col not in col_to_use: del df_data[col] return df_data def usgs_chromium_parse(*, df_list, source, year, **_): """ Combine, parse, and format the provided dataframes :param df_list: list of dataframes to concat and format :param args: dictionary, used to run flowbyactivity.py ('year' and 'source') :return: df, parsed and partially formatted to flowbyactivity specifications """ data = {} row_to_use = ["Secondary2", "Total"] prod = "" name = usgs_myb_name(source) des = name dataframe = pd.DataFrame() col_name = usgs_myb_year(YEARS_COVERED['chromium'], year) for df in df_list: for index, row in df.iterrows(): if df.iloc[index]["Production"].strip() == "Imports:": product = "imports" elif df.iloc[index]["Production"].strip() == "Secondary2": product = "production" elif df.iloc[index]["Production"].strip() == "Exports:": product = "exports" if df.iloc[index]["Production"].strip() in row_to_use: data = usgs_myb_static_variables() data["SourceName"] = source data["Year"] = str(year) data["Unit"] = "Metric Tons" data['FlowName'] = name + " " + product data["Description"] = name data["ActivityProducedBy"] = name col_name = usgs_myb_year(YEARS_COVERED['chromium'], year) if str(df.iloc[index][col_name]) == "--" or \ str(df.iloc[index][col_name]) == "(3)": data["FlowAmount"] = str(0) else: data["FlowAmount"] = str(df.iloc[index][col_name]) dataframe = dataframe.append(data, ignore_index=True) dataframe = assign_fips_location_system( dataframe, str(year)) return dataframe def usgs_clay_call(*, resp, year, **_): """ Convert response for calling url to pandas dataframe, begin parsing df into FBA format :param url: string, url :param resp: df, response from url call :param args: dictionary, arguments specified when running flowbyactivity.py ('year' and 'source') :return: pandas dataframe of original source data """ df_raw_data_ball = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T3') df_data_ball = pd.DataFrame(df_raw_data_ball.loc[19:19]).reindex() df_data_ball = df_data_ball.reset_index() del df_data_ball["index"] df_raw_data_bentonite = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T4 ') df_data_bentonite = pd.DataFrame( df_raw_data_bentonite.loc[28:28]).reindex() df_data_bentonite = df_data_bentonite.reset_index() del df_data_bentonite["index"] df_raw_data_common = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T5 ') df_data_common = pd.DataFrame(df_raw_data_common.loc[40:40]).reindex() df_data_common = df_data_common.reset_index() del df_data_common["index"] df_raw_data_fire = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T6 ') df_data_fire = pd.DataFrame(df_raw_data_fire.loc[12:12]).reindex() df_data_fire = df_data_fire.reset_index() del df_data_fire["index"] df_raw_data_fuller = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T7 ') df_data_fuller = pd.DataFrame(df_raw_data_fuller.loc[17:17]).reindex() df_data_fuller = df_data_fuller.reset_index() del df_data_fuller["index"] df_raw_data_kaolin = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T8 ') df_data_kaolin = pd.DataFrame(df_raw_data_kaolin.loc[18:18]).reindex() df_data_kaolin = df_data_kaolin.reset_index() del df_data_kaolin["index"] df_raw_data_export = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T13') df_data_export = pd.DataFrame(df_raw_data_export.loc[6:15]).reindex() df_data_export = df_data_export.reset_index() del df_data_export["index"] df_raw_data_import = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T14') df_data_import = pd.DataFrame(df_raw_data_import.loc[6:13]).reindex() df_data_import = df_data_import.reset_index() del df_data_import["index"] df_data_ball.columns = ["Production", "space_1", "year_1", "space_2", "value_1", "space_3", "year_2", "space_4", "value_2"] df_data_bentonite.columns = ["Production", "space_1", "year_1", "space_2", "value_1", "space_3", "year_2", "space_4", "value_2"] df_data_common.columns = ["Production", "space_1", "year_1", "space_2", "value_1", "space_3", "year_2", "space_4", "value_2"] df_data_fire.columns = ["Production", "space_1", "year_1", "space_2", "value_1", "space_3", "year_2", "space_4", "value_2"] df_data_fuller.columns = ["Production", "space_1", "year_1", "space_2", "value_1", "space_3", "year_2", "space_4", "value_2"] df_data_kaolin.columns = ["Production", "space_1", "year_1", "space_2", "value_1", "space_3", "year_2", "space_4", "value_2"] df_data_export.columns = ["Production", "space_1", "year_1", "space_2", "value_1", "space_3", "year_2", "space_4", "value_2", "space_5", "extra"] df_data_import.columns = ["Production", "space_1", "year_1", "space_2", "value_1", "space_3", "year_2", "space_4", "value_2", "space_5", "extra"] df_data_ball["type"] = "Ball clay" df_data_bentonite["type"] = "Bentonite" df_data_common["type"] = "Common clay" df_data_fire["type"] = "Fire clay" df_data_fuller["type"] = "Fuller’s earth" df_data_kaolin["type"] = "Kaolin" df_data_export["type"] = "export" df_data_import["type"] = "import" col_to_use = ["Production", "type"] col_to_use.append(usgs_myb_year(YEARS_COVERED['clay'], year)) for col in df_data_import.columns: if col not in col_to_use: del df_data_import[col] del df_data_export[col] for col in df_data_ball.columns: if col not in col_to_use: del df_data_ball[col] del df_data_bentonite[col] del df_data_common[col] del df_data_fire[col] del df_data_fuller[col] del df_data_kaolin[col] frames = [df_data_import, df_data_export, df_data_ball, df_data_bentonite, df_data_common, df_data_fire, df_data_fuller, df_data_kaolin] df_data = pd.concat(frames) df_data = df_data.reset_index() del df_data["index"] return df_data def usgs_clay_parse(*, df_list, source, year, **_): """ Combine, parse, and format the provided dataframes :param df_list: list of dataframes to concat and format :param args: dictionary, used to run flowbyactivity.py ('year' and 'source') :return: df, parsed and partially formatted to flowbyactivity specifications """ data = {} row_to_use = ["Ball clay", "Bentonite", "Fire clay", "Kaolin", "Fuller’s earth", "Total", "Grand total", "Artificially activated clay and earth", "Clays, not elsewhere classified", "Clays, not elsewhere classified"] dataframe = pd.DataFrame() for df in df_list: for index, row in df.iterrows(): if df.iloc[index]["type"].strip() == "import": product = "imports" elif df.iloc[index]["type"].strip() == "export": product = "exports" else: product = "production" if str(df.iloc[index]["Production"]).strip() in row_to_use: data = usgs_myb_static_variables() data["SourceName"] = source data["Year"] = str(year) data["Unit"] = "Metric Tons" if product == "production": data['FlowName'] = \ df.iloc[index]["type"].strip() + " " + product data["Description"] = df.iloc[index]["type"].strip() data["ActivityProducedBy"] = df.iloc[index]["type"].strip() else: data['FlowName'] = \ df.iloc[index]["Production"].strip() + " " + product data["Description"] = df.iloc[index]["Production"].strip() data["ActivityProducedBy"] = \ df.iloc[index]["Production"].strip() col_name = usgs_myb_year(YEARS_COVERED['clay'], year) if str(df.iloc[index][col_name]) == "--" or \ str(df.iloc[index][col_name]) == "(3)" or \ str(df.iloc[index][col_name]) == "(2)": data["FlowAmount"] = str(0) else: data["FlowAmount"] = str(df.iloc[index][col_name]) dataframe = dataframe.append(data, ignore_index=True) dataframe = assign_fips_location_system( dataframe, str(year)) return dataframe def usgs_cobalt_call(*, resp, year, **_): """ Convert response for calling url to pandas dataframe, begin parsing df into FBA format :param url: string, url :param resp: df, response from url call :param args: dictionary, arguments specified when running flowbyactivity.py ('year' and 'source') :return: pandas dataframe of original source data """ df_raw_data = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T8') df_raw_data_two = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T1') df_data_1 = pd.DataFrame(df_raw_data_two.loc[6:11]).reindex() df_data_1 = df_data_1.reset_index() del df_data_1["index"] df_data_2 = pd.DataFrame(df_raw_data.loc[23:23]).reindex() df_data_2 = df_data_2.reset_index() del df_data_2["index"] if len(df_data_2.columns) > 11: for x in range(11, len(df_data_2.columns)): col_name = "Unnamed: " + str(x) del df_data_2[col_name] if len(df_data_1. columns) == 12: df_data_1.columns = ["Production", "space_6", "space_1", "year_1", "space_2", "year_2", "space_3", "year_3", "space_4", "year_4", "space_5", "year_5"] if len(df_data_2. columns) == 11: df_data_2.columns = ["Production", "space_1", "year_1", "space_2", "year_2", "space_3", "year_3", "space_4", "year_4", "space_5", "year_5"] col_to_use = ["Production"] col_to_use.append(usgs_myb_year(YEARS_COVERED['cobalt'], year)) for col in df_data_1.columns: if col not in col_to_use: del df_data_1[col] for col in df_data_2.columns: if col not in col_to_use: del df_data_2[col] frames = [df_data_1, df_data_2] df_data = pd.concat(frames) df_data = df_data.reset_index() del df_data["index"] return df_data def usgs_cobalt_parse(*, df_list, source, year, **_): """ Combine, parse, and format the provided dataframes :param df_list: list of dataframes to concat and format :param args: dictionary, used to run flowbyactivity.py ('year' and 'source') :return: df, parsed and partially formatted to flowbyactivity specifications """ data = {} name = usgs_myb_name(source) des = name row_to_use = ["United Statese, 16, 17", "Mine productione", "Imports for consumption", "Exports"] dataframe = pd.DataFrame() for df in df_list: for index, row in df.iterrows(): prod = "production" if df.iloc[index]["Production"].strip() == \ "United Statese, 16, 17": prod = "production" elif df.iloc[index]["Production"].strip() == \ "Imports for consumption": prod = "imports" elif df.iloc[index]["Production"].strip() == "Exports": prod = "exports" if df.iloc[index]["Production"].strip() in row_to_use: remove_digits = str.maketrans('', '', digits) product = df.iloc[index][ "Production"].strip().translate(remove_digits) data = usgs_myb_static_variables() data["SourceName"] = source data["Year"] = str(year) data["Unit"] = "Thousand Metric Tons" col_name = usgs_myb_year(YEARS_COVERED['cobalt'], year) data["Description"] = des data["ActivityProducedBy"] = name data['FlowName'] = name + " " + prod data["FlowAmount"] = str(df.iloc[index][col_name]) remove_rows = ["(18)", "(2)"] if data["FlowAmount"] not in remove_rows: dataframe = dataframe.append(data, ignore_index=True) dataframe = assign_fips_location_system( dataframe, str(year)) return dataframe def usgs_copper_call(*, resp, year, **_): """ Convert response for calling url to pandas dataframe, begin parsing df into FBA format :param resp: df, response from url call :param year: year :return: pandas dataframe of original source data """ df_raw_data = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T1') df_data_1 = pd.DataFrame(df_raw_data.loc[12:12]).reindex() df_data_1 = df_data_1.reset_index() del df_data_1["index"] df_data_2 = pd.DataFrame(df_raw_data.loc[30:31]).reindex() df_data_2 = df_data_2.reset_index() del df_data_2["index"] if len(df_data_1. columns) == 12: df_data_1.columns = ["Production", "Unit", "space_1", "year_1", "space_2", "year_2", "space_3", "year_3", "space_4", "year_4", "space_5", "year_5"] df_data_2.columns = ["Production", "Unit", "space_1", "year_1", "space_2", "year_2", "space_3", "year_3", "space_4", "year_4", "space_5", "year_5"] col_to_use = ["Production", "Unit"] col_to_use.append(usgs_myb_year(YEARS_COVERED['copper'], year)) for col in df_data_1.columns: if col not in col_to_use: del df_data_1[col] for col in df_data_2.columns: if col not in col_to_use: del df_data_2[col] frames = [df_data_1, df_data_2] df_data = pd.concat(frames) df_data = df_data.reset_index() del df_data["index"] return df_data def usgs_copper_parse(*, df_list, source, year, **_): """ Combine, parse, and format the provided dataframes :param df_list: list of dataframes to concat and format :param args: dictionary, used to run flowbyactivity.py ('year' and 'source') :return: df, parsed and partially formatted to flowbyactivity specifications """ data = {} name = usgs_myb_name(source) des = name dataframe = pd.DataFrame() for df in df_list: for index, row in df.iterrows(): remove_digits = str.maketrans('', '', digits) product = df.iloc[index][ "Production"].strip().translate(remove_digits) data = usgs_myb_static_variables() data["SourceName"] = source data["Year"] = str(year) if product == "Total": prod = "production" elif product == "Exports, refined": prod = "exports" elif product == "Imports, refined": prod = "imports" data["ActivityProducedBy"] = "Copper; Mine" data['FlowName'] = name + " " + prod data["Unit"] = "Metric Tons" col_name = usgs_myb_year(YEARS_COVERED['copper'], year) data["Description"] = "Copper; Mine" data["FlowAmount"] = str(df.iloc[index][col_name]) dataframe = dataframe.append(data, ignore_index=True) dataframe = assign_fips_location_system( dataframe, str(year)) return dataframe def usgs_diatomite_call(*, resp, year, **_): """ Convert response for calling url to pandas dataframe, begin parsing df into FBA format :param url: string, url :param resp: df, response from url call :param year: year :return: pandas dataframe of original source data """ df_raw_data_one = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T1') df_data_one = pd.DataFrame(df_raw_data_one.loc[7:10]).reindex() df_data_one = df_data_one.reset_index() del df_data_one["index"] if len(df_data_one.columns) == 10: df_data_one.columns = ["Production", "year_1", "space_2", "year_2", "space_3", "year_3", "space_4", "year_4", "space_5", "year_5"] col_to_use = ["Production"] col_to_use.append(usgs_myb_year(YEARS_COVERED['diatomite'], year)) for col in df_data_one.columns: if col not in col_to_use: del df_data_one[col] frames = [df_data_one] df_data = pd.concat(frames) df_data = df_data.reset_index() del df_data["index"] return df_data def usgs_diatomite_parse(*, df_list, source, year, **_): """ Combine, parse, and format the provided dataframes :param df_list: list of dataframes to concat and format :param source: source :param year: year :return: df, parsed and partially formatted to flowbyactivity specifications """ data = {} row_to_use = ["Quantity", "Exports2", "Imports for consumption2"] prod = "" name = usgs_myb_name(source) des = name dataframe = pd.DataFrame() for df in df_list: for index, row in df.iterrows(): if df.iloc[index]["Production"].strip() == "Exports2": prod = "exports" elif df.iloc[index]["Production"].strip() == \ "Imports for consumption2": prod = "imports" elif df.iloc[index]["Production"].strip() == "Quantity": prod = "production" if df.iloc[index]["Production"].strip() in row_to_use: product = df.iloc[index]["Production"].strip() data = usgs_myb_static_variables() data["SourceName"] = source data["Year"] = str(year) data["Unit"] = "Thousand metric tons" col_name = usgs_myb_year(YEARS_COVERED['diatomite'], year) data["FlowAmount"] = str(df.iloc[index][col_name]) data["Description"] = name data["ActivityProducedBy"] = name data['FlowName'] = name + " " + prod dataframe = dataframe.append(data, ignore_index=True) dataframe = assign_fips_location_system( dataframe, str(year)) return dataframe def usgs_feldspar_call(*, resp, year, **_): """ Convert response for calling url to pandas dataframe, begin parsing df into FBA format :param url: string, url :param resp: df, response from url call :param year: year :return: pandas dataframe of original source data """ df_raw_data_two = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T1') df_data_two = pd.DataFrame(df_raw_data_two.loc[4:8]).reindex() df_data_two = df_data_two.reset_index() del df_data_two["index"] df_data_one = pd.DataFrame(df_raw_data_two.loc[10:15]).reindex() df_data_one = df_data_one.reset_index() del df_data_one["index"] if len(df_data_two. columns) == 13: df_data_two.columns = ["Production", "space_1", "unit", "space_2", "year_1", "space_3", "year_2", "space_4", "year_3", "space_5", "year_4", "space_6", "year_5"] df_data_one.columns = ["Production", "space_1", "unit", "space_2", "year_1", "space_3", "year_2", "space_4", "year_3", "space_5", "year_4", "space_6", "year_5"] col_to_use = ["Production"] col_to_use.append(usgs_myb_year(YEARS_COVERED['feldspar'], year)) for col in df_data_two.columns: if col not in col_to_use: del df_data_two[col] del df_data_one[col] frames = [df_data_two, df_data_one] df_data = pd.concat(frames) df_data = df_data.reset_index() del df_data["index"] return df_data def usgs_feldspar_parse(*, df_list, source, year, **_): """ Combine, parse, and format the provided dataframes :param df_list: list of dataframes to concat and format :param args: dictionary, used to run flowbyactivity.py ('year' and 'source') :return: df, parsed and partially formatted to flowbyactivity specifications """ data = {} row_to_use = ["Quantity", "Quantity3"] prod = "" name = usgs_myb_name(source) des = name dataframe = pd.DataFrame() for df in df_list: for index, row in df.iterrows(): if df.iloc[index]["Production"].strip() == "Exports, feldspar:4": prod = "exports" elif df.iloc[index]["Production"].strip() == \ "Imports for consumption:4": prod = "imports" elif df.iloc[index]["Production"].strip() == \ "Production, feldspar:e, 2": prod = "production" elif df.iloc[index]["Production"].strip() == "Nepheline syenite:": prod = "production" des = "Nepheline syenite" if df.iloc[index]["Production"].strip() in row_to_use: product = df.iloc[index]["Production"].strip() data = usgs_myb_static_variables() data["SourceName"] = source data["Year"] = str(year) data["Unit"] = "Metric Tons" col_name = usgs_myb_year(YEARS_COVERED['feldspar'], year) data["FlowAmount"] = str(df.iloc[index][col_name]) data["Description"] = des data["ActivityProducedBy"] = name if name == des: data['FlowName'] = name + " " + prod else: data['FlowName'] = name + " " + prod + " " + des dataframe = dataframe.append(data, ignore_index=True) dataframe = assign_fips_location_system( dataframe, str(year)) return dataframe def usgs_fluorspar_call(*, resp, year, **_): """ Convert response for calling url to pandas dataframe, begin parsing df into FBA format :param url: string, url :param resp: df, response from url call :param year: year :return: pandas dataframe of original source data """ df_raw_data_one = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T1') if year in YEARS_COVERED['fluorspar_inports']: df_raw_data_two = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T2') df_raw_data_three = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T7') df_raw_data_four = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T8') df_data_one = pd.DataFrame(df_raw_data_one.loc[5:15]).reindex() df_data_one = df_data_one.reset_index() del df_data_one["index"] if year in YEARS_COVERED['fluorspar_inports']: df_data_two = pd.DataFrame(df_raw_data_two.loc[7:8]).reindex() df_data_three = pd.DataFrame(df_raw_data_three.loc[19:19]).reindex() df_data_four = pd.DataFrame(df_raw_data_four.loc[11:11]).reindex() if len(df_data_two.columns) == 13: df_data_two.columns = ["Production", "space_1", "not_1", "space_2", "not_2", "space_3", "not_3", "space_4", "not_4", "space_5", "year_4", "space_6", "year_5"] if len(df_data_three.columns) == 9: df_data_three.columns = ["Production", "space_1", "year_4", "space_2", "not_1", "space_3", "year_5", "space_4", "not_2"] df_data_four.columns = ["Production", "space_1", "year_4", "space_2", "not_1", "space_3", "year_5", "space_4", "not_2"] if len(df_data_one. columns) == 13: df_data_one.columns = ["Production", "space_1", "unit", "space_2", "year_1", "space_3", "year_2", "space_4", "year_3", "space_5", "year_4", "space_6", "year_5"] col_to_use = ["Production"] col_to_use.append(usgs_myb_year(YEARS_COVERED['fluorspar'], year)) for col in df_data_one.columns: if col not in col_to_use: del df_data_one[col] if year in YEARS_COVERED['fluorspar_inports']: for col in df_data_two.columns: if col not in col_to_use: del df_data_two[col] for col in df_data_three.columns: if col not in col_to_use: del df_data_three[col] for col in df_data_four.columns: if col not in col_to_use: del df_data_four[col] df_data_one["type"] = "data_one" if year in YEARS_COVERED['fluorspar_inports']: # aluminum fluoride # cryolite df_data_two["type"] = "data_two" df_data_three["type"] = "Aluminum Fluoride" df_data_four["type"] = "Cryolite" frames = [df_data_one, df_data_two, df_data_three, df_data_four] else: frames = [df_data_one] df_data = pd.concat(frames) df_data = df_data.reset_index() del df_data["index"] return df_data def usgs_fluorspar_parse(*, df_list, source, year, **_): """ Combine, parse, and format the provided dataframes :param df_list: list of dataframes to concat and format :param args: dictionary, used to run flowbyactivity.py ('year' and 'source') :return: df, parsed and partially formatted to flowbyactivity specifications """ data = {} row_to_use = ["Quantity", "Quantity3", "Total", "Hydrofluoric acid", "Metallurgical", "Production"] prod = "" name = usgs_myb_name(source) dataframe = pd.DataFrame() for df in df_list: for index, row in df.iterrows(): if df.iloc[index]["Production"].strip() == "Exports:3": prod = "exports" des = name elif df.iloc[index]["Production"].strip() == \ "Imports for consumption:3": prod = "imports" des = name elif df.iloc[index]["Production"].strip() == "Fluorosilicic acid:": prod = "production" des = "Fluorosilicic acid:" if str(df.iloc[index]["type"]).strip() == "data_two": prod = "imports" des = df.iloc[index]["Production"].strip() elif str(df.iloc[index]["type"]).strip() == \ "Aluminum Fluoride" or \ str(df.iloc[index]["type"]).strip() == "Cryolite": prod = "imports" des = df.iloc[index]["type"].strip() if df.iloc[index]["Production"].strip() in row_to_use: data = usgs_myb_static_variables() data["SourceName"] = source data["Year"] = str(year) data["Unit"] = "Metric Tons" col_name = usgs_myb_year(YEARS_COVERED['fluorspar'], year) if str(df.iloc[index][col_name]) == "W": data["FlowAmount"] = WITHDRAWN_KEYWORD else: data["FlowAmount"] = str(df.iloc[index][col_name]) data["Description"] = des data["ActivityProducedBy"] = name data['FlowName'] = name + " " + prod dataframe = dataframe.append(data, ignore_index=True) dataframe = assign_fips_location_system( dataframe, str(year)) return dataframe def usgs_gallium_call(*, resp, year, **_): """ Convert response for calling url to pandas dataframe, begin parsing df into FBA format :param url: string, url :param resp: df, response from url call :param args: dictionary, arguments specified when running flowbyactivity.py ('year' and 'source') :return: pandas dataframe of original source data """ df_raw_data = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T1') df_data = pd.DataFrame(df_raw_data.loc[5:7]).reindex() df_data = df_data.reset_index() del df_data["index"] if len(df_data.columns) > 11: for x in range(11, len(df_data.columns)): col_name = "Unnamed: " + str(x) del df_data[col_name] if len(df_data.columns) == 11: df_data.columns = ["Production", "space_1", "year_1", "space_2", "year_2", "space_3", "year_3", "space_4", "year_4", "space_5", "year_5"] col_to_use = ["Production"] col_to_use.append(usgs_myb_year(YEARS_COVERED['gallium'], year)) for col in df_data.columns: if col not in col_to_use: del df_data[col] return df_data def usgs_gallium_parse(*, df_list, source, year, **_): """ Combine, parse, and format the provided dataframes :param df_list: list of dataframes to concat and format :param source: source :param year: year :return: df, parsed and partially formatted to flowbyactivity specifications """ data = {} row_to_use = ["Production, primary crude", "Metal"] prod = "" name = usgs_myb_name(source) des = name dataframe = pd.DataFrame() col_name = usgs_myb_year(YEARS_COVERED['gallium'], year) for df in df_list: for index, row in df.iterrows(): if df.iloc[index]["Production"].strip() == \ "Imports for consumption:": product = "imports" elif df.iloc[index]["Production"].strip() == \ "Production, primary crude": product = "production" if df.iloc[index]["Production"].strip() in row_to_use: data = usgs_myb_static_variables() data["SourceName"] = source data["Year"] = str(year) data["Unit"] = "Kilograms" data['FlowName'] = name + " " + product data["Description"] = name data["ActivityProducedBy"] = name col_name = usgs_myb_year(YEARS_COVERED['gallium'], year) if str(df.iloc[index][col_name]).strip() == "--": data["FlowAmount"] = str(0) elif str(df.iloc[index][col_name]) == "nan": data["FlowAmount"] = WITHDRAWN_KEYWORD else: data["FlowAmount"] = str(df.iloc[index][col_name]) dataframe = dataframe.append(data, ignore_index=True) dataframe = assign_fips_location_system( dataframe, str(year)) return dataframe def usgs_garnet_call(*, resp, year, **_): """ Convert response for calling url to pandas dataframe, begin parsing df into FBA format :param resp: df, response from url call :param year: year :return: pandas dataframe of original source data """ df_raw_data_two = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T1') df_data_two = pd.DataFrame(df_raw_data_two.loc[4:5]).reindex() df_data_two = df_data_two.reset_index() del df_data_two["index"] df_data_one = pd.DataFrame(df_raw_data_two.loc[10:14]).reindex() df_data_one = df_data_one.reset_index() del df_data_one["index"] if len(df_data_one.columns) > 13: for x in range(13, len(df_data_one.columns)): col_name = "Unnamed: " + str(x) del df_data_one[col_name] del df_data_two[col_name] if len(df_data_two. columns) == 13: df_data_two.columns = ["Production", "space_1", "unit", "space_2", "year_1", "space_3", "year_2", "space_4", "year_3", "space_5", "year_4", "space_6", "year_5"] df_data_one.columns = ["Production", "space_1", "unit", "space_2", "year_1", "space_3", "year_2", "space_4", "year_3", "space_5", "year_4", "space_6", "year_5"] col_to_use = ["Production"] col_to_use.append(usgs_myb_year(YEARS_COVERED['garnet'], year)) for col in df_data_two.columns: if col not in col_to_use: del df_data_two[col] del df_data_one[col] frames = [df_data_two, df_data_one] df_data = pd.concat(frames) df_data = df_data.reset_index() del df_data["index"] return df_data def usgs_garnet_parse(*, df_list, source, year, **_): """ Combine, parse, and format the provided dataframes :param df_list: list of dataframes to concat and format :param source: source :param year: year :return: df, parsed and partially formatted to flowbyactivity specifications """ data = {} row_to_use = ["Quantity"] prod = "" name = usgs_myb_name(source) des = name dataframe = pd.DataFrame() for df in df_list: for index, row in df.iterrows(): if df.iloc[index]["Production"].strip() == "Exports:2": prod = "exports" elif df.iloc[index]["Production"].strip() == \ "Imports for consumption: 3": prod = "imports" elif df.iloc[index]["Production"].strip() == "Crude production:": prod = "production" if df.iloc[index]["Production"].strip() in row_to_use: product = df.iloc[index]["Production"].strip() data = usgs_myb_static_variables() data["SourceName"] = source data["Year"] = str(year) data["Unit"] = "Metric Tons" col_name = usgs_myb_year(YEARS_COVERED['garnet'], year) data["FlowAmount"] = str(df.iloc[index][col_name]) data["Description"] = des data["ActivityProducedBy"] = name data['FlowName'] = name + " " + prod dataframe = dataframe.append(data, ignore_index=True) dataframe = assign_fips_location_system( dataframe, str(year)) return dataframe def usgs_gold_call(*, resp, year, **_): """ Convert response for calling url to pandas dataframe, begin parsing df into FBA format :param url: string, url :param resp: df, response from url call :param args: dictionary, arguments specified when running flowbyactivity.py ('year' and 'source') :return: pandas dataframe of original source data """ df_raw_data = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T1') df_data = pd.DataFrame(df_raw_data.loc[6:14]).reindex() df_data = df_data.reset_index() del df_data["index"] if len(df_data.columns) == 13: df_data.columns = ["Production", "Space", "Units", "space_1", "year_1", "space_2", "year_2", "space_3", "year_3", "space_4", "year_4", "space_5", "year_5"] col_to_use = ["Production"] col_to_use.append(usgs_myb_year(YEARS_COVERED['gold'], year)) for col in df_data.columns: if col not in col_to_use: del df_data[col] return df_data def usgs_gold_parse(*, df_list, source, year, **_): """ Combine, parse, and format the provided dataframes :param df_list: list of dataframes to concat and format :param source: source :param year: year :return: df, parsed and partially formatted to flowbyactivity specifications """ data = {} row_to_use = ["Quantity", "Exports, refined bullion", "Imports for consumption, refined bullion"] dataframe = pd.DataFrame() product = "production" name = usgs_myb_name(source) des = name for df in df_list: for index, row in df.iterrows(): if df.iloc[index]["Production"].strip() == "Quantity": product = "production" elif df.iloc[index]["Production"].strip() == \ "Exports, refined bullion": product = "exports" elif df.iloc[index]["Production"].strip() == \ "Imports for consumption, refined bullion": product = "imports" if df.iloc[index]["Production"].strip() in row_to_use: data = usgs_myb_static_variables() data["SourceName"] = source data["Year"] = str(year) data["Unit"] = "kilograms" data['FlowName'] = name + " " + product data["Description"] = des data["ActivityProducedBy"] = name col_name = usgs_myb_year(YEARS_COVERED['gold'], year) if str(df.iloc[index][col_name]) == "--": data["FlowAmount"] = str(0) else: data["FlowAmount"] = str(df.iloc[index][col_name]) dataframe = dataframe.append(data, ignore_index=True) dataframe = assign_fips_location_system( dataframe, str(year)) return dataframe def usgs_graphite_call(*, resp, year, **_): """ Convert response for calling url to pandas dataframe, begin parsing df into FBA format :param resp: df, response from url call :param year: year :return: pandas dataframe of original source data """ df_raw_data = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T1') df_data = pd.DataFrame(df_raw_data.loc[5:9]).reindex() df_data = df_data.reset_index() del df_data["index"] if len(df_data. columns) == 13: df_data.columns = ["Production", "space_1", "Unit", "space_6", "year_1", "space_2", "year_2", "space_3", "year_3", "space_4", "year_4", "space_5", "year_5"] col_to_use = ["Production"] col_to_use.append(usgs_myb_year(YEARS_COVERED['graphite'], year)) for col in df_data.columns: if col not in col_to_use: del df_data[col] return df_data def usgs_graphite_parse(*, df_list, source, year, **_): """ Combine, parse, and format the provided dataframes :param df_list: list of dataframes to concat and format :param source: source :param year: year :return: df, parsed and partially formatted to flowbyactivity specifications """ data = {} row_to_use = ["Quantiy", "Quantity"] prod = "" name = usgs_myb_name(source) des = name dataframe = pd.DataFrame() col_name = usgs_myb_year(YEARS_COVERED['graphite'], year) for df in df_list: for index, row in df.iterrows(): if df.iloc[index]["Production"].strip() == \ "Imports for consumption:": product = "imports" elif df.iloc[index]["Production"].strip() == "Exports:": product = "exports" if df.iloc[index]["Production"].strip() in row_to_use: data = usgs_myb_static_variables() data["SourceName"] = source data["Year"] = str(year) data["Unit"] = "Metric Tons" data['FlowName'] = name + " " + product data["Description"] = name data["ActivityProducedBy"] = name col_name = usgs_myb_year(YEARS_COVERED['graphite'], year) if str(df.iloc[index][col_name]) == "--": data["FlowAmount"] = str(0) elif str(df.iloc[index][col_name]) == "nan": data["FlowAmount"] = WITHDRAWN_KEYWORD else: data["FlowAmount"] = str(df.iloc[index][col_name]) dataframe = dataframe.append(data, ignore_index=True) dataframe = assign_fips_location_system( dataframe, str(year)) return dataframe def usgs_gypsum_call(*, resp, year, **_): """ Convert response for calling url to pandas dataframe, begin parsing df into FBA format :param url: string, url :param resp: df, response from url call :param args: dictionary, arguments specified when running flowbyactivity.py ('year' and 'source') :return: pandas dataframe of original source data """ df_raw_data_one = pd.io.excel.read_excel(io.BytesIO(resp.content), sheet_name='T1') df_data_one =
pd.DataFrame(df_raw_data_one.loc[7:10])
pandas.DataFrame
############################################################# # # Robust Synthetic Control Tests (based on ALS) # # You need to ensure that this script is called from # the tslib/ parent directory or tslib/tests/ directory: # # 1. python tests/testScriptSynthControlALS.py # 2. python testScriptSynthControlALS.py # ############################################################# import sys, os sys.path.append("../..") sys.path.append("..") sys.path.append(os.getcwd()) from matplotlib import pyplot as plt import numpy as np import pandas as pd import copy from tslib.src import tsUtils from tslib.src.synthcontrol.syntheticControl import RobustSyntheticControl from tslib.tests import testdata def basque(filename): # BASQUE COUNTRY STUDY df =
pd.read_csv(filename)
pandas.read_csv
import numpy as np import matplotlib.pyplot as plt import pandas as pd import math import scipy.stats as stats from matplotlib import gridspec from matplotlib.lines import Line2D from .util import * import seaborn as sns from matplotlib.ticker import FormatStrFormatter import matplotlib.pylab as pl import matplotlib.dates as mdates from matplotlib.patches import Patch from matplotlib.lines import Line2D import matplotlib.patheffects as pe from .sanker import Sanker import imageio class Visualizer(): def __init__(self, district_list, private_list, city_list, contract_list, bank_list, leiu_list): self.district_list = district_list.copy() self.private_list = private_list.copy() for x in city_list: self.private_list.append(x) self.contract_list = contract_list self.bank_list = bank_list self.leiu_list = leiu_list self.private_districts = {} for x in self.private_list: self.private_districts[x.name] = [] for xx in x.district_list: self.private_districts[x.name].append(xx) inflow_inputs = pd.read_csv('calfews_src/data/input/calfews_src-data.csv', index_col=0, parse_dates=True) x2_results = pd.read_csv('calfews_src/data/input/x2DAYFLOW.csv', index_col=0, parse_dates=True) self.observations = inflow_inputs.join(x2_results) self.observations['delta_outflow'] = self.observations['delta_inflow'] + self.observations['delta_depletions'] - self.observations['HRO_pump'] - self.observations['TRP_pump'] self.index_o = self.observations.index self.T_o = len(self.observations) self.day_month_o = self.index_o.day self.month_o = self.index_o.month self.year_o = self.index_o.year kern_bank_observations = pd.read_csv('calfews_src/data/input/kern_water_bank_historical.csv') kern_bank_observations = kern_bank_observations.set_index('Year') semitropic_bank_observations = pd.read_csv('calfews_src/data/input/semitropic_bank_historical.csv') semitropic_bank_observations = semitropic_bank_observations.set_index('Year') total_bank_kwb = np.zeros(self.T_o) total_bank_smi = np.zeros(self.T_o) for x in range(0, self.T_o): if self.month_o[x] > 9: year_str = self.year_o[x] else: year_str = self.year_o[x] - 1 if self.month_o[x] == 9 and self.day_month_o[x] == 30: year_str = self.year_o[x] total_bank_kwb[x] = kern_bank_observations.loc[year_str, 'Ag'] + kern_bank_observations.loc[year_str, 'Mixed Purpose'] deposit_history = semitropic_bank_observations[semitropic_bank_observations.index <= year_str] total_bank_smi[x] = deposit_history['Metropolitan'].sum() + deposit_history['South Bay'].sum() self.observations['kwb_accounts'] = pd.Series(total_bank_kwb, index=self.observations.index) self.observations['smi_accounts'] = pd.Series(total_bank_smi, index=self.observations.index) def get_results_sensitivity_number(self, results_file, sensitivity_number, start_month, start_year, start_day): self.values = {} numdays_index = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] with h5py.File(results_file, 'r') as f: data = f['s' + sensitivity_number] names = data.attrs['columns'] names = list(map(lambda x: str(x).split("'")[1], names)) df_data = pd.DataFrame(data[:], columns=names) for x in df_data: self.values[x] = df_data[x] datetime_index = [] monthcount = start_month yearcount = start_year daycount = start_day leapcount = np.remainder(start_year, 4) for t in range(0, len(self.values[x])): datetime_index.append(str(yearcount) + '-' + str(monthcount) + '-' + str(daycount)) daycount += 1 if leapcount == 0 and monthcount == 2: numdays_month = numdays_index[monthcount - 1] + 1 else: numdays_month = numdays_index[monthcount - 1] if daycount > numdays_month: daycount = 1 monthcount += 1 if monthcount == 13: monthcount = 1 yearcount += 1 leapcount += 1 if leapcount == 4: leapcount = 0 self.values['Datetime'] = pd.to_datetime(datetime_index) self.values = pd.DataFrame(self.values) self.values = self.values.set_index('Datetime') self.index = self.values.index self.T = len(self.values.index) self.day_year = self.index.dayofyear self.day_month = self.index.day self.month = self.index.month self.year = self.index.year self.starting_year = self.index.year[0] self.ending_year = self.index.year[-1] self.number_years = self.ending_year - self.starting_year total_kwb_sim = np.zeros(len(self.values)) total_smi_sim = np.zeros(len(self.values)) for district_partner in ['DLR', 'KCWA', 'ID4', 'SMI', 'TJC', 'WON', 'WRM']: total_kwb_sim += self.values['kwb_' + district_partner] self.values['kwb_total'] = pd.Series(total_kwb_sim, index = self.values.index) for district_partner in ['SOB', 'MET']: total_smi_sim += self.values['semitropic_' + district_partner] self.values['smi_total'] = pd.Series(total_smi_sim, index = self.values.index) def set_figure_params(self): self.figure_params = {} self.figure_params['delta_pumping'] = {} self.figure_params['delta_pumping']['extended_simulation'] = {} self.figure_params['delta_pumping']['extended_simulation']['outflow_list'] = ['delta_outflow', 'delta_outflow'] self.figure_params['delta_pumping']['extended_simulation']['pump1_list'] = ['delta_HRO_pump', 'HRO_pump'] self.figure_params['delta_pumping']['extended_simulation']['pump2_list'] = ['delta_TRP_pump', 'TRP_pump'] self.figure_params['delta_pumping']['extended_simulation']['scenario_labels'] = ['Model Validation', 'Extended Simulation'] self.figure_params['delta_pumping']['extended_simulation']['simulation_labels'] = ['delta_HRO_pump', 'delta_TRP_pump', 'delta_outflow'] self.figure_params['delta_pumping']['extended_simulation']['observation_labels'] = ['HRO_pump', 'TRP_pump', 'delta_outflow'] self.figure_params['delta_pumping']['extended_simulation']['agg_list'] = ['AS-OCT', 'AS-OCT', 'D'] self.figure_params['delta_pumping']['extended_simulation']['unit_mult'] = [1.0, 1.0, cfs_tafd] self.figure_params['delta_pumping']['extended_simulation']['max_value_list'] = [5000, 5000, 15] self.figure_params['delta_pumping']['extended_simulation']['use_log_list'] = [False, False, True] self.figure_params['delta_pumping']['extended_simulation']['use_cdf_list'] = [False, False, True] self.figure_params['delta_pumping']['extended_simulation']['scenario_type_list'] = ['observation', 'validation', 'scenario'] self.figure_params['delta_pumping']['extended_simulation']['x_label_list'] = ['Total Pumping, SWP Delta Pumps (tAF/year)', 'Total Pumping, CVP Delta Pumps (tAF/year)', 'Daily Exceedence Probability', ''] self.figure_params['delta_pumping']['extended_simulation']['y_label_list'] = ['Probability Density', 'Probability Density', 'Daily Delta Outflow (tAF)', 'Relative Frequency of Water-year Types within Simulation'] self.figure_params['delta_pumping']['extended_simulation']['legend_label_names1'] = ['Historical (1996-2016) Observations', 'Historical (1996-2016) Model Validation', 'Extended Simulation'] self.figure_params['delta_pumping']['extended_simulation']['legend_label_names2'] = ['Critical', 'Dry', 'Below Normal', 'Above Normal', 'Wet'] self.figure_params['state_estimation'] = {} for x in ['publication', 'sacramento', 'sanjoaquin', 'tulare']: self.figure_params['state_estimation'][x] = {} self.figure_params['state_estimation'][x]['non_log'] = ['Snowpack (SWE)',] self.figure_params['state_estimation'][x]['predictor values'] = ['Mean Inflow, Prior 30 Days (tAF/day)','Snowpack (SWE)'] self.figure_params['state_estimation'][x]['colorbar_label_index'] = [0, 30, 60, 90, 120, 150, 180] self.figure_params['state_estimation'][x]['colorbar_label_list'] = ['Oct', 'Nov', 'Dec', 'Jan', 'Feb', 'Mar', 'Apr'] self.figure_params['state_estimation'][x]['subplot_annotations'] = ['A', 'B', 'C', 'D'] self.figure_params['state_estimation'][x]['forecast_periods'] = [30,'SNOWMELT'] self.figure_params['state_estimation'][x]['all_cols'] = ['DOWY', 'Snowpack', '30MA'] self.figure_params['state_estimation'][x]['forecast_values'] = [] for forecast_days in self.figure_params['state_estimation'][x]['forecast_periods']: if forecast_days == 'SNOWMELT': self.figure_params['state_estimation'][x]['forecast_values'].append('Flow Estimation, Snowmelt Season (tAF)') self.figure_params['state_estimation'][x]['all_cols'].append('Snowmelt Flow') else: self.figure_params['state_estimation'][x]['forecast_values'].append('Flow Estimation, Next ' + str(forecast_days) + ' Days (tAF)') self.figure_params['state_estimation'][x]['all_cols'].append(str(forecast_days) + ' Day Flow') self.figure_params['state_estimation']['publication']['watershed_keys'] = ['SHA', 'ORO', 'MIL', 'ISB'] self.figure_params['state_estimation']['publication']['watershed_labels'] = ['Shasta', 'Oroville', 'Millerton', 'Isabella'] self.figure_params['state_estimation']['sacramento']['watershed_keys'] = ['SHA', 'ORO', 'FOL', 'YRS'] self.figure_params['state_estimation']['sacramento']['watershed_labels'] = ['Shasta', 'Oroville', 'Folsom', 'New Bullards Bar'] self.figure_params['state_estimation']['sanjoaquin']['watershed_keys'] = ['NML', 'DNP', 'EXC', 'MIL'] self.figure_params['state_estimation']['sanjoaquin']['watershed_labels'] = ['New Melones', '<NAME>', 'Exchequer', 'Millerton'] self.figure_params['state_estimation']['tulare']['watershed_keys'] = ['PFT', 'KWH', 'SUC', 'ISB'] self.figure_params['state_estimation']['tulare']['watershed_labels'] = ['Pine Flat', 'Kaweah', 'Success', 'Isabella'] self.figure_params['model_validation'] = {} for x in ['delta', 'sierra', 'sanluis', 'bank']: self.figure_params['model_validation'][x] = {} self.figure_params['model_validation']['delta']['title_labels'] = ['State Water Project Pumping', 'Central Valley Project Pumping', 'Delta X2 Location'] num_subplots = len(self.figure_params['model_validation']['delta']['title_labels']) self.figure_params['model_validation']['delta']['label_name_1'] = ['delta_HRO_pump', 'delta_TRP_pump', 'delta_x2'] self.figure_params['model_validation']['delta']['label_name_2'] = ['HRO_pump', 'TRP_pump', 'DAY_X2'] self.figure_params['model_validation']['delta']['unit_converstion_1'] = [1.0, 1.0, 1.0] self.figure_params['model_validation']['delta']['unit_converstion_2'] = [cfs_tafd, cfs_tafd, 1.0] self.figure_params['model_validation']['delta']['y_label_timeseries'] = ['Pumping (tAF/week)', 'Pumping (tAF/week)', 'X2 inland distance (km)'] self.figure_params['model_validation']['delta']['y_label_scatter'] = ['(tAF/yr)', '(tAF/yr)', '(km)'] self.figure_params['model_validation']['delta']['timeseries_timestep'] = ['W', 'W', 'W'] self.figure_params['model_validation']['delta']['scatter_timestep'] = ['AS-OCT', 'AS-OCT', 'M'] self.figure_params['model_validation']['delta']['aggregation_methods'] = ['sum', 'sum', 'mean'] self.figure_params['model_validation']['delta']['notation_location'] = ['top'] * num_subplots self.figure_params['model_validation']['delta']['show_legend'] = [True] * num_subplots self.figure_params['model_validation']['sierra']['title_labels'] = ['Shasta', 'Oroville', 'Folsom', 'New Bullards Bar', 'New Melones', '<NAME>', 'Exchequer', 'Millerton', 'Pine Flat', 'Kaweah', 'Success', 'Isabella'] num_subplots = len(self.figure_params['model_validation']['sierra']['title_labels']) self.figure_params['model_validation']['sierra']['label_name_1'] = ['shasta_S', 'oroville_S', 'folsom_S', 'yuba_S', 'newmelones_S', 'donpedro_S', 'exchequer_S', 'millerton_S', 'pineflat_S', 'kaweah_S', 'success_S', 'isabella_S'] self.figure_params['model_validation']['sierra']['label_name_2'] = ['SHA_storage', 'ORO_storage', 'FOL_storage', 'YRS_storage', 'NML_storage', 'DNP_storage', 'EXC_storage', 'MIL_storage', 'PFT_storage', 'KWH_storage', 'SUC_storage', 'ISB_storage'] self.figure_params['model_validation']['sierra']['unit_converstion_1'] = [1.0/1000.0] * num_subplots self.figure_params['model_validation']['sierra']['unit_converstion_2'] = [1.0/1000000.0] * num_subplots self.figure_params['model_validation']['sierra']['y_label_timeseries'] = ['Storage (mAF)'] * num_subplots self.figure_params['model_validation']['sierra']['y_label_scatter'] = [] self.figure_params['model_validation']['sierra']['timeseries_timestep'] = ['W'] * num_subplots self.figure_params['model_validation']['sierra']['scatter_timestep'] = [] self.figure_params['model_validation']['sierra']['aggregation_methods'] = ['mean'] * num_subplots self.figure_params['model_validation']['sierra']['notation_location'] = ['bottom'] * num_subplots self.figure_params['model_validation']['sierra']['show_legend'] = [False] * num_subplots counter_kaweah = self.figure_params['model_validation']['sierra']['title_labels'].index('Kaweah') counter_success = self.figure_params['model_validation']['sierra']['title_labels'].index('Success') counter_isabella = self.figure_params['model_validation']['sierra']['title_labels'].index('Isabella') self.figure_params['model_validation']['sierra']['notation_location'][counter_kaweah] = 'top' self.figure_params['model_validation']['sierra']['notation_location'][counter_success] = 'topright' self.figure_params['model_validation']['sierra']['show_legend'][counter_isabella] = True self.figure_params['model_validation']['sanluis']['title_labels'] = ['State (SWP) Portion, San Luis Reservoir', 'Federal (CVP) Portion, San Luis Reservoir'] num_subplots = len(self.figure_params['model_validation']['sanluis']['title_labels']) self.figure_params['model_validation']['sanluis']['label_name_1'] = ['sanluisstate_S', 'sanluisfederal_S'] self.figure_params['model_validation']['sanluis']['label_name_2'] = ['SLS_storage', 'SLF_storage'] self.figure_params['model_validation']['sanluis']['unit_converstion_1'] = [1.0/1000.0] * num_subplots self.figure_params['model_validation']['sanluis']['unit_converstion_2'] = [1.0/1000000.0] * num_subplots self.figure_params['model_validation']['sanluis']['y_label_timeseries'] = ['Storage (mAF)'] * num_subplots self.figure_params['model_validation']['sanluis']['y_label_scatter'] = ['(mAF)'] * num_subplots self.figure_params['model_validation']['sanluis']['timeseries_timestep'] = ['W'] * num_subplots self.figure_params['model_validation']['sanluis']['scatter_timestep'] = ['M'] * num_subplots self.figure_params['model_validation']['sanluis']['aggregation_methods'] = ['point'] * num_subplots self.figure_params['model_validation']['sanluis']['notation_location'] = ['top'] * num_subplots self.figure_params['model_validation']['sanluis']['show_legend'] = [True] * num_subplots self.figure_params['model_validation']['bank']['title_labels'] = ['Kern Water Bank Accounts', 'Semitropic Water Bank Accounts'] num_subplots = len(self.figure_params['model_validation']['bank']['title_labels']) self.figure_params['model_validation']['bank']['label_name_1'] = ['kwb_total', 'smi_total'] self.figure_params['model_validation']['bank']['label_name_2'] = ['kwb_accounts', 'smi_accounts'] self.figure_params['model_validation']['bank']['unit_converstion_1'] = [1.0/1000.0] * num_subplots self.figure_params['model_validation']['bank']['unit_converstion_2'] = [1.0/1000000.0, 1.0/1000.0] self.figure_params['model_validation']['bank']['y_label_timeseries'] = ['Storage (mAF)'] * num_subplots self.figure_params['model_validation']['bank']['y_label_scatter'] = ['(mAF)'] * num_subplots self.figure_params['model_validation']['bank']['timeseries_timestep'] = ['W'] * num_subplots self.figure_params['model_validation']['bank']['scatter_timestep'] = ['AS-OCT'] * num_subplots self.figure_params['model_validation']['bank']['aggregation_methods'] = ['change'] * num_subplots self.figure_params['model_validation']['bank']['notation_location'] = ['top'] * num_subplots self.figure_params['model_validation']['bank']['show_legend'] = [False] * num_subplots self.figure_params['model_validation']['bank']['show_legend'][0] = True self.figure_params['state_response'] = {} self.figure_params['state_response']['sanluisstate_losthills'] = {} self.figure_params['state_response']['sanluisstate_losthills']['contract_list'] = ['swpdelta',] self.figure_params['state_response']['sanluisstate_losthills']['contributing_reservoirs'] = ['delta_uncontrolled_swp', 'oroville', 'yuba'] self.figure_params['state_response']['sanluisstate_losthills']['groundwater_account_names'] = ['LHL','WON'] self.figure_params['state_response']['sanluisstate_losthills']['reservoir_features'] = ['S', 'days_til_full', 'flood_deliveries'] self.figure_params['state_response']['sanluisstate_losthills']['reservoir_feature_colors'] = ['teal', '#3A506B', '#74B3CE', 'steelblue'] self.figure_params['state_response']['sanluisstate_losthills']['district_contracts'] = ['tableA',] self.figure_params['state_response']['sanluisstate_losthills']['subplot_titles'] = ['State Water Project Delta Operations', 'Lost Hills Drought Management', 'San Luis Reservoir Operations', 'Lost Hills Flood Management'] self.figure_params['state_response']['sanluisstate_losthills']['legend_list_1'] = ['Y.T.D Delta Pumping', 'Projected Unstored Exports', 'Projected Stored Exports, Oroville', 'Projected Stored Exports, New Bullards'] self.figure_params['state_response']['sanluisstate_losthills']['legend_list_2'] = ['Storage', 'Projected Days to Fill', 'Flood Release Deliveries'] self.figure_params['state_response']['sanluisstate_losthills']['legend_list_3'] = ['Remaining SW Allocation', 'SW Deliveries', 'Private GW Pumping', 'District GW Bank Recovery', 'Remaining GW Bank Recovery Capacity'] self.figure_params['state_response']['sanluisstate_losthills']['legend_list_4'] = ['Carryover Recharge Capacity', 'Recharged from Contract Allocation' 'Recharge of Uncontrolled Flood Spills'] self.figure_params['state_response'] = {} self.figure_params['state_response']['sanluisstate_wheeler'] = {} self.figure_params['state_response']['sanluisstate_wheeler']['contract_list'] = ['swpdelta',] self.figure_params['state_response']['sanluisstate_wheeler']['contributing_reservoirs'] = ['delta_uncontrolled_swp', 'oroville', 'yuba'] self.figure_params['state_response']['sanluisstate_wheeler']['groundwater_account_names'] = ['WRM'] self.figure_params['state_response']['sanluisstate_wheeler']['reservoir_features'] = ['S', 'days_til_full', 'flood_deliveries'] self.figure_params['state_response']['sanluisstate_wheeler']['reservoir_feature_colors'] = ['teal', '#3A506B', '#74B3CE', 'lightsteelblue'] self.figure_params['state_response']['sanluisstate_wheeler']['district_contracts'] = ['tableA',] self.figure_params['state_response']['sanluisstate_wheeler']['subplot_titles'] = ['State Water Project Delta Operations', 'Wheeler Ridge Drought Management', 'San Luis Reservoir Operations', 'Wheeler Ridge Flood Management'] self.figure_params['state_response']['sanluisstate_wheeler']['legend_list_1'] = ['Y.T.D Delta Pumping', 'Projected Unstored Exports', 'Projected Stored Exports, Oroville', 'Projected Stored Exports, New Bullards'] self.figure_params['state_response']['sanluisstate_wheeler']['legend_list_2'] = ['Storage', 'Projected Days to Fill', 'Flood Release Deliveries'] self.figure_params['state_response']['sanluisstate_wheeler']['legend_list_3'] = ['Remaining SW Allocation', 'SW Deliveries', 'Private GW Pumping', 'District GW Bank Recovery', 'Remaining GW Bank Recovery Capacity'] self.figure_params['state_response']['sanluisstate_wheeler']['legend_list_4'] = ['Carryover Recharge Capacity', 'Recharge of Uncontrolled Flood Spills', 'Recharged from Contract Allocation'] self.figure_params['district_water_use'] = {} self.figure_params['district_water_use']['physical'] = {} self.figure_params['district_water_use']['physical']['district_groups'] = ['Municipal Districts', 'Kern County Water Agency', 'CVP - Friant Contractors', 'CVP - San Luis Contractors', 'Groundwater Banks'] self.figure_params['district_water_use']['physical']['Municipal Districts'] = ['bakersfield', 'ID4', 'fresno', 'southbay', 'socal', 'centralcoast'] self.figure_params['district_water_use']['physical']['Kern County Water Agency'] = ['berrenda', 'belridge', 'buenavista', 'cawelo', 'henrymiller', 'losthills', 'rosedale', 'semitropic', 'tehachapi', 'tejon', 'westkern', 'wheeler', 'northkern', 'kerntulare'] self.figure_params['district_water_use']['physical']['CVP - Friant Contractors'] = ['arvin', 'delano', 'pixley', 'exeter', 'kerntulare', 'lindmore', 'lindsay', 'lowertule', 'porterville', 'saucelito', 'shaffer', 'sosanjoaquin', 'teapot', 'terra', 'chowchilla', 'maderairr', 'tulare', 'fresnoid'] self.figure_params['district_water_use']['physical']['CVP - San Luis Contractors'] = ['westlands', 'panoche', 'sanluiswater', 'delpuerto'] self.figure_params['district_water_use']['physical']['Groundwater Banks'] = ['stockdale', 'kernriverbed', 'poso', 'pioneer', 'kwb', 'b2800', 'irvineranch', 'northkernwb'] self.figure_params['district_water_use']['physical']['subplot columns'] = 2 self.figure_params['district_water_use']['physical']['color map'] = 'YlGbBu_r' self.figure_params['district_water_use']['physical']['write file'] = True self.figure_params['district_water_use']['annual'] = {} self.figure_params['district_water_use']['annual']['district_groups'] = ['Municipal Districts', 'Kern County Water Agency', 'CVP - Friant Contractors', 'CVP - San Luis Contractors'] self.figure_params['district_water_use']['annual']['Municipal Districts'] = ['bakersfield', 'ID4', 'fresno', 'southbay', 'socal', 'centralcoast'] self.figure_params['district_water_use']['annual']['Kern County Water Agency'] = ['berrenda', 'belridge', 'buenavista', 'cawelo', 'henrymiller', 'losthills', 'rosedale', 'semitropic', 'tehachapi', 'tejon', 'westkern', 'wheeler'] self.figure_params['district_water_use']['annual']['CVP - Friant Contractors'] = ['arvin', 'delano', 'pixley', 'exeter', 'kerntulare', 'lindmore', 'lindsay', 'lowertule', 'porterville', 'saucelito', 'shaffer', 'sosanjoaquin', 'teapot', 'terra', 'chowchilla', 'maderairr', 'tulare', 'fresnoid'] self.figure_params['district_water_use']['annual']['CVP - San Luis Contractors'] = ['westlands', 'panoche', 'sanluiswater', 'delpuerto'] self.figure_params['district_water_use']['annual']['subplot columns'] = 2 self.figure_params['district_water_use']['annual']['color map'] = 'BrBG_r' self.figure_params['district_water_use']['annual']['write file'] = True self.figure_params['flow_diagram'] = {} self.figure_params['flow_diagram']['tulare'] = {} self.figure_params['flow_diagram']['tulare']['column1'] = ['Shasta', 'Folsom', 'Oroville', 'New Bullards', 'Uncontrolled'] self.figure_params['flow_diagram']['tulare']['row1'] = ['Delta Outflow', 'Carryover',] self.figure_params['flow_diagram']['tulare']['column2'] = ['San Luis (Fed)', 'San Luis (State)', 'Millerton', 'Isabella', 'Pine Flat', 'Kaweah', 'Success'] self.figure_params['flow_diagram']['tulare']['row2'] = ['Carryover',] self.figure_params['flow_diagram']['tulare']['column3'] = ['Exchange', 'CVP-Delta', 'Cross Valley', 'State Water Project', 'Friant Class 1','Friant Class 2', 'Kern River', 'Kings River', 'Kaweah River', 'Tule River', 'Flood'] self.figure_params['flow_diagram']['tulare']['row3'] = ['Private Pumping', 'GW Banks'] self.figure_params['flow_diagram']['tulare']['column4'] = ['Exchange', 'CVP-Delta', 'Urban', 'KCWA', 'CVP-Friant','Other'] self.figure_params['flow_diagram']['tulare']['row4'] = ['Carryover',] self.figure_params['flow_diagram']['tulare']['column5'] = ['Irrigation', 'Urban', 'In-Lieu Recharge', 'Direct Recharge'] self.figure_params['flow_diagram']['tulare']['titles'] = ['Sacramento Basin\nSupplies', 'Tulare Basin\nSupplies', 'Surface Water\nContract Allocations', 'Contractor Groups', 'Water Use Type'] def scenario_compare(self, folder_name, figure_name, plot_name, validation_values, show_plot): outflow_list = self.figure_params[figure_name][plot_name]['outflow_list'] pump1_list = self.figure_params[figure_name][plot_name]['pump1_list'] pump2_list = self.figure_params[figure_name][plot_name]['pump2_list'] scenario_labels = self.figure_params[figure_name][plot_name]['scenario_labels'] simulation_labels = self.figure_params[figure_name][plot_name]['simulation_labels'] observation_labels = self.figure_params[figure_name][plot_name]['observation_labels'] agg_list = self.figure_params[figure_name][plot_name]['agg_list'] unit_mult = self.figure_params[figure_name][plot_name]['unit_mult'] max_value_list = self.figure_params[figure_name][plot_name]['max_value_list'] use_log_list = self.figure_params[figure_name][plot_name]['use_log_list'] use_cdf_list = self.figure_params[figure_name][plot_name]['use_cdf_list'] scenario_type_list = self.figure_params[figure_name][plot_name]['scenario_type_list'] x_label_list = self.figure_params[figure_name][plot_name]['x_label_list'] y_label_list = self.figure_params[figure_name][plot_name]['y_label_list'] legend_label_names1 = self.figure_params[figure_name][plot_name]['legend_label_names1'] legend_label_names2 = self.figure_params[figure_name][plot_name]['legend_label_names2'] color1 = sns.color_palette('spring', n_colors = 3) color2 = sns.color_palette('summer', n_colors = 3) color_list = np.array([color1[0], color1[2], color2[0]]) max_y_val = np.zeros(len(simulation_labels)) fig = plt.figure(figsize = (20, 16)) gs = gridspec.GridSpec(3,2, width_ratios=[3,1], figure = fig) ax1 = plt.subplot(gs[0, 0]) ax2 = plt.subplot(gs[1, 0]) ax3 = plt.subplot(gs[2, 0]) ax4 = plt.subplot(gs[:, 1]) axes_list = [ax1, ax2, ax3] counter = 0 for sim_label, obs_label, agg, max_value, use_log, use_cdf, ax_loop in zip(simulation_labels, observation_labels, agg_list, max_value_list, use_log_list, use_cdf_list, axes_list): data_type_dict = {} data_type_dict['scenario'] = self.values[sim_label].resample(agg).sum() * unit_mult[0] data_type_dict['validation'] = validation_values[sim_label].resample(agg).sum() * unit_mult[1] data_type_dict['observation'] = self.observations[obs_label].resample(agg).sum() * unit_mult[2] if use_log: for scen_type in scenario_type_list: values_int = data_type_dict[scen_type] data_type_dict[scen_type] = np.log(values_int[values_int > 0]) for scen_type in scenario_type_list: max_y_val[counter] = max([max(data_type_dict[scen_type]), max_y_val[counter]]) counter += 1 if use_cdf: for scen_type, color_loop in zip(scenario_type_list, color_list): cdf_values = np.zeros(100) values_int = data_type_dict[scen_type] for x in range(0, 100): x_val = int(np.ceil(max_value)) * (x/100) cdf_values[x] = len(values_int[values_int > x_val])/len(values_int) ax_loop.plot(cdf_values, np.arange(0, int(np.ceil(max_value)), int(np.ceil(max_value))/100), linewidth = 3, color = color_loop) else: pos = np.linspace(0, max_value, 101) for scen_type, color_loop in zip(scenario_type_list, color_list): kde_est = stats.gaussian_kde(data_type_dict[scen_type]) ax_loop.fill_between(pos, kde_est(pos), edgecolor = 'black', alpha = 0.6, facecolor = color_loop) sri_dict = {} sri_dict['validation'] = validation_values['delta_forecastSRI'] sri_dict['scenario'] = self.values['delta_forecastSRI'] sri_cutoffs = {} sri_cutoffs['W'] = [9.2, 100] sri_cutoffs['AN'] = [7.8, 9.2] sri_cutoffs['BN'] = [6.6, 7.8] sri_cutoffs['D'] = [5.4, 6.6] sri_cutoffs['C'] = [0.0, 5.4] wyt_list = ['W', 'AN', 'BN', 'D', 'C'] scenario_type_list = ['validation', 'scenario'] colors = sns.color_palette('RdBu_r', n_colors = 5) percent_years = {} for wyt in wyt_list: percent_years[wyt] = np.zeros(len(scenario_type_list)) for scen_cnt, scen_type in enumerate(scenario_type_list): ann_sri = [] for x_cnt, x in enumerate(sri_dict[scen_type]): if sri_dict[scen_type].index.month[x_cnt] == 9 and sri_dict[scen_type].index.day[x_cnt] == 30: ann_sri.append(x) ann_sri = np.array(ann_sri) for x_cnt, wyt in enumerate(wyt_list): mask_value = (ann_sri >= sri_cutoffs[wyt][0]) & (ann_sri < sri_cutoffs[wyt][1]) percent_years[wyt][scen_cnt] = len(ann_sri[mask_value])/len(ann_sri) colors = sns.color_palette('RdBu_r', n_colors = 5) last_type = np.zeros(len(scenario_type_list)) for cnt, x in enumerate(wyt_list): ax4.bar(['Validated Period\n(1997-2016)', 'Extended Simulation\n(1906-2016)'], percent_years[x], alpha = 1.0, label = wyt, facecolor = colors[cnt], edgecolor = 'black', bottom = last_type) last_type += percent_years[x] ax1.set_xlim([0.0, 500.0* np.ceil(max_y_val[0]/500.0)]) ax2.set_xlim([0.0, 500.0* np.ceil(max_y_val[1]/500.0)]) ax3.set_xlim([0.0, 1.0]) ax4.set_ylim([0, 1.15]) ax1.set_yticklabels('') ax2.set_yticklabels('') label_list = [] loc_list = [] for value_x in range(0, 120, 20): label_list.append(str(value_x) + ' %') loc_list.append(value_x/100.0) ax4.set_yticklabels(label_list) ax4.set_yticks(loc_list) ax3.set_xticklabels(label_list) ax3.set_xticks(loc_list) ax3.set_yticklabels(['4', '8', '16', '32', '64', '125', '250', '500', '1000', '2000', '4000']) ax3.set_yticks([np.log(4), np.log(8), np.log(16), np.log(32), np.log(64), np.log(125), np.log(250), np.log(500), np.log(1000), np.log(2000), np.log(4000)]) ax3.set_ylim([np.log(4), np.log(4000)]) for ax, x_lab, y_lab in zip([ax1, ax2, ax3, ax4], x_label_list, y_label_list): ax.set_xlabel(x_lab, fontsize = 16, fontname = 'Gill Sans MT', fontweight = 'bold') ax.set_ylabel(y_lab, fontsize = 16, fontname = 'Gill Sans MT', fontweight = 'bold') ax.grid(False) for tick in ax.get_xticklabels(): tick.set_fontname('Gill Sans MT') tick.set_fontsize(14) for tick in ax.get_yticklabels(): tick.set_fontname('Gill Sans MT') tick.set_fontsize(14) legend_elements = [] for x_cnt, x in enumerate(legend_label_names1): legend_elements.append(Patch(facecolor = color_list[x_cnt], edgecolor = 'black', label = x)) ax1.legend(handles = legend_elements, loc = 'upper left', framealpha = 0.7, shadow = True, prop={'family':'Gill Sans MT','weight':'bold','size':14}) legend_elements_2 = [] for x_cnt, x in enumerate(legend_label_names2): legend_elements_2.append(Patch(facecolor = colors[x_cnt], edgecolor = 'black', label = x)) ax4.legend(handles = legend_elements_2, loc = 'upper left', framealpha = 0.7, shadow = True, prop={'family':'Gill Sans MT','weight':'bold','size':14}) plt.savefig(folder_name + figure_name + '_' + plot_name + '.png', dpi = 150, bbox_inches = 'tight', pad_inches = 0.0) if show_plot: plt.show() plt.close() def make_deliveries_by_district(self, folder_name, figure_name, plot_name, scenario_name, show_plot): if plot_name == 'annual': name_bridge = {} name_bridge['semitropic'] = 'KER01' name_bridge['westkern'] = 'KER02' name_bridge['wheeler'] = 'KER03' name_bridge['kerndelta'] = 'KER04' name_bridge['arvin'] = 'KER05' name_bridge['belridge'] = 'KER06' name_bridge['losthills'] = 'KER07' name_bridge['northkern'] = 'KER08' name_bridge['northkernwb'] = 'KER08' name_bridge['ID4'] = 'KER09' name_bridge['sosanjoaquin'] = 'KER10' name_bridge['berrenda'] = 'KER11' name_bridge['buenavista'] = 'KER12' name_bridge['cawelo'] = 'KER13' name_bridge['rosedale'] = 'KER14' name_bridge['shaffer'] = 'KER15' name_bridge['henrymiller'] = 'KER16' name_bridge['kwb'] = 'KER17' name_bridge['b2800'] = 'KER17' name_bridge['pioneer'] = 'KER17' name_bridge['irvineranch'] = 'KER17' name_bridge['kernriverbed'] = 'KER17' name_bridge['poso'] = 'KER17' name_bridge['stockdale'] = 'KER17' name_bridge['delano'] = 'KeT01' name_bridge['kerntulare'] = 'KeT02' name_bridge['lowertule'] = 'TUL01' name_bridge['tulare'] = 'TUL02' name_bridge['lindmore'] = 'TUL03' name_bridge['saucelito'] = 'TUL04' name_bridge['porterville'] = 'TUL05' name_bridge['lindsay'] = 'TUL06' name_bridge['exeter'] = 'TUL07' name_bridge['terra'] = 'TUL08' name_bridge['teapot'] = 'TUL09' name_bridge['bakersfield'] = 'BAK' name_bridge['fresno'] = 'FRE' name_bridge['southbay'] = 'SOB' name_bridge['socal'] = 'SOC' name_bridge['tehachapi'] = 'TEH' name_bridge['tejon'] = 'TEJ' name_bridge['centralcoast'] = 'SLO' name_bridge['pixley'] = 'PIX' name_bridge['chowchilla'] = 'CHW' name_bridge['maderairr'] = 'MAD' name_bridge['fresnoid'] = 'FSI' name_bridge['westlands'] = 'WTL' name_bridge['panoche'] = 'PAN' name_bridge['sanluiswater'] = 'SLW' name_bridge['delpuerto'] = 'DEL' elif plot_name == 'monthly': name_bridge = {} name_bridge['semitropic'] = 'Semitropic Water Storage District' name_bridge['westkern'] = 'West Kern Water District' name_bridge['wheeler'] = 'Wheeler Ridge-Maricopa Water Storage District' name_bridge['kerndelta'] = 'Kern Delta Water District' name_bridge['arvin'] = 'Arvin-Edison Water Storage District' name_bridge['belridge'] = 'Belridge Water Storage District' name_bridge['losthills'] = 'Lost Hills Water District' name_bridge['northkern'] = 'North Kern Water Storage District' name_bridge['northkernwb'] = 'North Kern Water Storage District' name_bridge['ID4'] = 'Urban' name_bridge['sosanjoaquin'] = 'Southern San Joaquin Municipal Utility District' name_bridge['berrenda'] = 'Berrenda Mesa Water District' name_bridge['buenavista'] = 'Buena Vista Water Storage District' name_bridge['cawelo'] = 'Cawelo Water District' name_bridge['rosedale'] = 'Rosedale-Rio Bravo Water Storage District' name_bridge['shaffer'] = 'Shafter-Wasco Irrigation District' name_bridge['henrymiller'] = 'Henry Miller Water District' name_bridge['kwb'] = 'Kern Water Bank Authority' name_bridge['b2800'] = 'Kern Water Bank Authority' name_bridge['pioneer'] = 'Kern Water Bank Authority' name_bridge['irvineranch'] = 'Kern Water Bank Authority' name_bridge['kernriverbed'] = 'Kern Water Bank Authority' name_bridge['poso'] = 'Kern Water Bank Authority' name_bridge['stockdale'] = 'Kern Water Bank Authority' name_bridge['delano'] = 'Delano-Earlimart Irrigation District' name_bridge['kerntulare'] = 'Kern-Tulare Water District' name_bridge['lowertule'] = 'Lower Tule River Irrigation District' name_bridge['tulare'] = 'Tulare Irrigation District' name_bridge['lindmore'] = 'Lindmore Irrigation District' name_bridge['saucelito'] = 'Saucelito Irrigation District' name_bridge['porterville'] = 'Porterville Irrigation District' name_bridge['lindsay'] = 'Lindsay-Strathmore Irrigation District' name_bridge['exeter'] = 'Exeter Irrigation District' name_bridge['terra'] = 'Terra Bella Irrigation District' name_bridge['teapot'] = 'Tea Pot Dome Water District' name_bridge['bakersfield'] = 'Urban' name_bridge['fresno'] = 'Urban' name_bridge['southbay'] = 'Urban' name_bridge['socal'] = 'Urban' name_bridge['tehachapi'] = 'Tehachapi - Cummings County Water District' name_bridge['tejon'] = 'Tejon-Castac Water District' name_bridge['centralcoast'] = 'SLO' name_bridge['pixley'] = 'Pixley Irrigation District' name_bridge['chowchilla'] = 'Chowchilla Water District' name_bridge['maderairr'] = 'Madera Irrigation District' name_bridge['fresnoid'] = 'Fresno Irrigation District' name_bridge['westlands'] = 'Westlands Water District' name_bridge['panoche'] = 'Panoche Water District' name_bridge['sanluiswater'] = 'San Luis Water District' name_bridge['delpuerto'] = 'Del Puerto Water District' name_bridge['alta'] = 'Alta Irrigation District' name_bridge['consolidated'] = 'Consolidated Irrigation District' location_type = plot_name self.total_irrigation = {} self.total_recharge = {} self.total_pumping = {} self.total_flood_purchases = {} self.total_recovery_rebate = {} self.total_recharge_sales = {} self.total_recharge_purchases = {} self.total_recovery_sales = {} self.total_recovery_purchases = {} for bank in self.bank_list: self.total_irrigation[bank.name] = np.zeros(self.number_years*12) self.total_recharge[bank.name] = np.zeros(self.number_years*12) self.total_pumping[bank.name] = np.zeros(self.number_years*12) self.total_flood_purchases[bank.name] = np.zeros(self.number_years*12) self.total_recovery_rebate[bank.name] = np.zeros(self.number_years*12) self.total_recharge_sales[bank.name] = np.zeros(self.number_years*12) self.total_recharge_purchases[bank.name] = np.zeros(self.number_years*12) self.total_recovery_sales[bank.name] = np.zeros(self.number_years*12) self.total_recovery_purchases[bank.name] = np.zeros(self.number_years*12) for district in self.district_list: self.total_irrigation[district.name] = np.zeros(self.number_years*12) self.total_recharge[district.name] = np.zeros(self.number_years*12) self.total_pumping[district.name] = np.zeros(self.number_years*12) self.total_flood_purchases[district.name] = np.zeros(self.number_years*12) self.total_recovery_rebate[district.name] = np.zeros(self.number_years*12) self.total_recharge_sales[district.name] = np.zeros(self.number_years*12) self.total_recharge_purchases[district.name] = np.zeros(self.number_years*12) self.total_recovery_sales[district.name] = np.zeros(self.number_years*12) self.total_recovery_purchases[district.name] = np.zeros(self.number_years*12) date_list_labels = [] for year_num in range(self.starting_year, 2017): start_month = 1 end_month = 13 if year_num == self.starting_year: start_month = 10 if year_num == 2016: end_month = 10 for month_num in range(start_month, end_month): date_string_start = str(year_num) + '-' + str(month_num) + '-01' date_list_labels.append(date_string_start) for district in self.district_list: inleiu_name = district.name + '_inleiu_irrigation' inleiu_recharge_name = district.name + '_inleiu_recharge' direct_recover_name = district.name + '_recover_banked' indirect_surface_name = district.name + '_exchanged_SW' indirect_ground_name = district.name + '_exchanged_GW' inleiu_pumping_name = district.name + '_leiupumping' pumping_name = district.name + '_pumping' recharge_name = district.name + '_' + district.key + '_recharged' numdays_month = [31, 28, 31, 30, 31, 30, 31, 31, 29, 31, 30, 31] for year_num in range(0, self.number_years+1): year_str = str(year_num + self.starting_year) start_month = 1 end_month = 13 if year_num == 0: start_month = 10 if year_num == self.number_years: end_month = 10 for month_num in range(start_month, end_month): if month_num == 1: month_num_prev = '12' year_str_prior = str(year_num + self.starting_year - 1) end_day_prior = str(numdays_month[11]) else: month_num_prev = str(month_num - 1) year_str_prior = str(year_num + self.starting_year) end_day_prior = str(numdays_month[month_num-2]) date_string_current = year_str + '-' + str(month_num) + '-' + str(numdays_month[month_num-1]) date_string_prior = year_str_prior + '-' + month_num_prev + '-' + end_day_prior ###GW/SW exchanges, if indirect_surface_name in self.values: if month_num == 10: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), indirect_surface_name].values[0] else: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), indirect_surface_name].values[0] - self.values.loc[pd.DatetimeIndex([date_string_prior]), indirect_surface_name].values[0] #count irrigation deliveries for district that gave up SW (for GW in canal) self.total_irrigation[district.name][year_num*12 + month_num - 10] += total_delivery ###GW/SW exchanges, if indirect_ground_name in self.values: if month_num == 10: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), indirect_ground_name].values[0] else: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), indirect_ground_name].values[0] - self.values.loc[pd.DatetimeIndex([date_string_prior]), indirect_ground_name].values[0] self.total_recovery_purchases[district.name][year_num*12 + month_num - 10] += total_delivery ##In leiu deliveries for irrigation if inleiu_name in self.values: if month_num == 10: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), inleiu_name].values[0] else: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), inleiu_name].values[0] - self.values.loc[pd.DatetimeIndex([date_string_prior]), inleiu_name].values[0] #attibute inleiu deliveries for irrigation to district operating the bank self.total_irrigation[district.name][year_num*12 + month_num - 10] += total_delivery self.total_recharge_sales[district.name][year_num*12 + month_num - 10] += total_delivery if inleiu_recharge_name in self.values: if month_num == 10: total_recharge = self.values.loc[pd.DatetimeIndex([date_string_current]), inleiu_recharge_name].values[0] else: total_recharge = self.values.loc[pd.DatetimeIndex([date_string_current]), inleiu_recharge_name].values[0] - self.values.loc[pd.DatetimeIndex([date_string_prior]), inleiu_recharge_name].values[0] #attibute inleiu deliveries for irrigation to district operating the bank self.total_recharge[district.name][year_num*12 + month_num - 10] += total_recharge self.total_recharge_sales[district.name][year_num*12 + month_num - 10] += total_recharge #GW recovery if direct_recover_name in self.values: if month_num == 10: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), direct_recover_name].values[0] else: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), direct_recover_name].values[0] - self.values.loc[pd.DatetimeIndex([date_string_prior]), direct_recover_name].values[0] #if classifying by physical location, attribute to district recieving water (as irrigation) self.total_irrigation[district.name][year_num*12 + month_num - 10] += total_delivery self.total_recovery_purchases[district.name][year_num*12 + month_num - 10] += total_delivery ##Pumnping for inleiu recovery if inleiu_pumping_name in self.values: if month_num == 10: total_leiupumping = self.values.loc[pd.DatetimeIndex([date_string_current]), inleiu_pumping_name].values[0] else: total_leiupumping = self.values.loc[pd.DatetimeIndex([date_string_current]), inleiu_pumping_name].values[0] - self.values.loc[pd.DatetimeIndex([date_string_prior]), inleiu_pumping_name].values[0] #if classifying by physical location, to district operating the bank self.total_pumping[district.name][year_num*12 + month_num - 10] += total_leiupumping self.total_recovery_sales[district.name][year_num*12 + month_num - 10] += total_leiupumping self.total_recovery_rebate[district.name][year_num*12 + month_num - 10] += total_leiupumping #Recharge, in- and out- of district if recharge_name in self.values: if month_num == 10: total_recharge = self.values.loc[pd.DatetimeIndex([date_string_current]), recharge_name].values[0] else: total_recharge = self.values.loc[pd.DatetimeIndex([date_string_current]), recharge_name].values[0] - self.values.loc[pd.DatetimeIndex([date_string_prior]), recharge_name].values[0] self.total_recharge[district.name][year_num*12 + month_num - 10] += total_recharge for bank_name in self.bank_list: bank_recharge_name = district.name + '_' + bank_name.key + '_recharged' if bank_recharge_name in self.values: if month_num == 10: total_recharge = self.values.loc[pd.DatetimeIndex([date_string_current]), bank_recharge_name].values[0] else: total_recharge = self.values.loc[pd.DatetimeIndex([date_string_current]), bank_recharge_name].values[0] - self.values.loc[pd.DatetimeIndex([date_string_prior]), bank_recharge_name].values[0] self.total_recharge[bank_name.name][year_num*12 + month_num - 10] += total_recharge self.total_recharge_purchases[district.name][year_num*12 + month_num - 10] += total_recharge for bank_name in self.leiu_list: bank_recharge_name = district.name + '_' + bank_name.key + '_recharged' if bank_recharge_name in self.values: if month_num == 10: total_recharge = self.values.loc[pd.DatetimeIndex([date_string_current]), bank_recharge_name].values[0] else: total_recharge = self.values.loc[pd.DatetimeIndex([date_string_current]), bank_recharge_name].values[0] - self.values.loc[pd.DatetimeIndex([date_string_prior]), bank_recharge_name].values[0] self.total_recharge_purchases[district.name][year_num*12 + month_num - 10] += total_recharge #Contract deliveries for contract in self.contract_list: delivery_name = district.name + '_' + contract.name + '_delivery' recharge_contract_name = district.name + '_' + contract.name + '_recharged' flood_irr_name = district.name + '_' + contract.name + '_flood_irrigation' flood_name = district.name + '_' + contract.name + '_flood' ###All deliveries made from a district's contract if delivery_name in self.values: if month_num == 10: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), delivery_name].values[0] else: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), delivery_name].values[0] - self.values.loc[pd.DatetimeIndex([date_string_prior]), delivery_name].values[0] self.total_irrigation[district.name][year_num*12 + month_num - 10] += total_delivery ##Deliveries made for recharge are subtracted from the overall contract deliveries if recharge_contract_name in self.values: if month_num == 10: total_recharge = self.values.loc[pd.DatetimeIndex([date_string_current]), recharge_contract_name].values[0] else: total_recharge = self.values.loc[pd.DatetimeIndex([date_string_current]), recharge_contract_name].values[0] - self.values.loc[pd.DatetimeIndex([date_string_prior]), recharge_contract_name].values[0] self.total_irrigation[district.name][year_num*12 + month_num - 10] -= total_recharge #flood water used for irrigation - always attribute as irrigation if flood_irr_name in self.values: if month_num == 10: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), flood_irr_name].values[0] else: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), flood_irr_name].values[0] - self.values.loc[pd.DatetimeIndex([date_string_prior]), flood_irr_name].values[0] self.total_irrigation[district.name][year_num*12 + month_num - 10] += total_delivery self.total_flood_purchases[district.name][year_num*12 + month_num - 10] += total_delivery if flood_name in self.values: if month_num == 10: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), flood_name].values[0] else: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), flood_name].values[0] - self.values.loc[pd.DatetimeIndex([date_string_prior]), flood_name].values[0] self.total_flood_purchases[district.name][year_num*12 + month_num - 10] += total_delivery ##Pumping (daily values aggregated by year) if pumping_name in self.values: annual_pumping = 0.0 for x in range(0, len(self.index)): monthly_index = (self.year[x] - self.starting_year)*12 + self.month[x] - 10 if self.day_month[x] == 1: self.total_pumping[district.name][monthly_index] += annual_pumping annual_pumping = 0.0 else: annual_pumping += self.values.loc[self.index[x], pumping_name] self.total_pumping[district.name][-1] += annual_pumping #Get values for any private entities within the district for private_name in self.private_list: private = private_name.name if district.key in self.private_districts[private]: inleiu_name = private + '_' + district.key + '_inleiu_irrigation' inleiu_recharge_name = private + '_' + district.key + '_inleiu_irrigation' direct_recover_name = private + '_' + district.key + '_recover_banked' indirect_surface_name = private + '_' + district.key + '_exchanged_SW' indirect_ground_name = private + '_' + district.key + '_exchanged_GW' inleiu_pumping_name = private + '_' + district.key + '_leiupumping' pumping_name = private + '_' + district.key + '_pumping' recharge_name = private + '_' + district.key + '_' + district.key + '_recharged' for year_num in range(0, self.number_years - 1): year_str = str(year_num + self.starting_year + 1) start_month = 1 end_month = 13 if year_num == 0: start_month = 10 if year_num == self.number_years - 1: end_month = 10 for month_num in range(start_month, end_month): if month_num == 1: month_num_prev = '12' year_str_prior = str(year_num + self.starting_year) end_day_prior = str(numdays_month[11]) else: month_num_prev = str(month_num - 1) year_str_prior = str(year_num + self.starting_year + 1) end_day_prior = str(numdays_month[month_num-2]) date_string_current = year_str + '-' + str(month_num) + '-' + str(numdays_month[month_num-1]) date_string_prior = year_str_prior + '-' + month_num_prev + '-' + end_day_prior ###GW/SW exchanges, if indirect_surface_name in self.values: if month_num == 10: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), indirect_surface_name].values[0] else: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), indirect_surface_name].values[0] - self.values.loc[pd.DatetimeIndex([date_string_prior]), indirect_surface_name].values[0] #count irrigation deliveries for district that gave up SW (for GW in canal) self.total_irrigation[district.name][year_num*12 + month_num - 10] += total_delivery ###GW/SW exchanges, if indirect_ground_name in self.values: if month_num == 10: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), indirect_ground_name].values[0] else: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), indirect_ground_name].values[0] - self.values.loc[pd.DatetimeIndex([date_string_prior]), indirect_ground_name].values[0] #count irrigation deliveries for district that gave up SW (for GW in canal) self.total_recovery_purchases[district.name][year_num*12 + month_num - 10] += total_delivery ##In leiu deliveries for irrigation if inleiu_name in self.values: if month_num == 10: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), inleiu_name].values[0] else: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), inleiu_name].values[0] - self.values.loc[pd.DatetimeIndex([date_string_prior]), inleiu_name].values[0] #attibute inleiu deliveries for irrigation to district operating the bank self.total_irrigation[district.name][year_num*12 + month_num - 10] += total_delivery self.total_recharge_sales[district.name][year_num*12 + month_num - 10] += total_delivery if inleiu_recharge_name in self.values: if month_num == 10: total_recharge = self.values.loc[pd.DatetimeIndex([date_string_current]), inleiu_recharge_name].values[0] else: total_recharge = self.values.loc[pd.DatetimeIndex([date_string_current]), inleiu_recharge_name].values[0] - self.values.loc[pd.DatetimeIndex([date_string_prior]), inleiu_recharge_name].values[0] #attibute inleiu deliveries for irrigation to district operating the bank self.total_recharge[district.name][year_num*12 + month_num - 10] += total_recharge self.total_recharge_sales[district.name][year_num*12 + month_num - 10] += total_recharge #GW recovery if direct_recover_name in self.values: if month_num == 10: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), direct_recover_name].values[0] else: total_delivery = self.values.loc[pd.DatetimeIndex([date_string_current]), direct_recover_name].values[0] - self.values.loc[pd.DatetimeIndex([date_string_prior]), direct_recover_name].values[0] #if classifying by physical location, attribute to district recieving water (as irrigation) self.total_irrigation[district.name][year_num*12 + month_num - 10] += total_delivery self.total_recovery_purchases[district.name][year_num*12 + month_num - 10] += total_delivery ##Pumnping for inleiu recovery if inleiu_pumping_name in self.values: if month_num == 10: total_leiupumping = self.values.loc[pd.DatetimeIndex([date_string_current]), inleiu_pumping_name].values[0] else: total_leiupumping = self.values.loc[pd.DatetimeIndex([date_string_current]), inleiu_pumping_name].values[0] - self.values.loc[pd.DatetimeIndex([date_string_prior]), inleiu_pumping_name].values[0] #if classifying by phyiscal location, to district operating the bank self.total_pumping[district.name][year_num*12 + month_num - 10] += total_leiupumping self.total_recovery_sales[district.name][year_num*12 + month_num - 10] += total_leiupumping self.total_recovery_rebate[district.name][year_num*12 + month_num - 10] += total_leiupumping #Recharge, in- and out- of district if recharge_name in self.values: if month_num == 10: total_recharge = self.values.loc[pd.DatetimeIndex([date_string_current]), recharge_name].values[0] else: total_recharge = self.values.loc[pd.DatetimeIndex([date_string_current]), recharge_name].values[0] - self.values.loc[
pd.DatetimeIndex([date_string_prior])
pandas.DatetimeIndex
from collections import defaultdict from multiprocessing import Pool import os.path import random import igraph from numpy import * import numpy.random as nprandom import pandas as pd from sklearn.metrics import adjusted_rand_score from sklearn import svm """ The names of the datasets used for training. """ TRAIN_SETS = ['afrasian', 'bai', 'chinese_1964', 'chinese_2004', 'huon', 'japanese', 'kadai', 'kamasau', 'lolo_burmese', 'mayan', 'miao_yao', 'mixe_zoque', 'mon_khmer', 'ob_ugrian', 'tujia'] """ The names of the datasets used for testing. Note that central_asian is manually split in two files because of file size limits. """ TEST_SETS = ['abvd', 'central_asian', 'central_asian_2', 'ielex'] """ The relevant subset of features; for feature selection, simply alter this list. """ FEATURES = ['feature1', 'feature4', 'feature6', 'feature7', 'feature8'] """ Module-level variables, used within the workhorse functions. """ training = None trainingVectors = None test = None def infer(vectors_dir, output_dir): """ Inits and orchestrates the cognate class inferring algorithm. """ global training global trainingVectors global test dDict = {'gloss':unicode, 'l1':unicode, 'w1':unicode, 'cc1':unicode, 'l2':unicode, 'w2':unicode, 'cc2':unicode, 'feature1':double, 'feature2':double, 'feature3':double, 'feature4':double, 'feature5':double, 'lexstat_simAA':double, 'lexstat_simBB':double, 'lexstat_simAB':double, 'feature7':double, 'target':int, 'db':unicode } # load the training data training = pd.DataFrame() for dataset_name in TRAIN_SETS: file_path = os.path.join(vectors_dir, '{}.csv'.format(dataset_name)) training = training.append(pd.read_csv(file_path, encoding='utf-8', dtype=dDict)) training['feature8'] = 1-((2*training.lexstat_simAB)/(training.lexstat_simAA+training.lexstat_simBB)) nprandom.seed(1234) random.seed(1234) trainingVectors = training.ix[nprandom.permutation(training.index)].drop_duplicates(['db','gloss']) # cross-validation over training data pool = Pool() totalCC = pool.map(f,training.db.unique()) pool.close() pool.terminate() for db,wl in zip(training.db.unique(),totalCC): file_path = os.path.join(output_dir, '{}.svmCC.csv'.format(db)) wl['fullCC'] = [':'.join(x) for x in wl[['db','concept','cc']].values] wl[['db','concept','doculect','counterpart', 'fullCC','inferredCC']].to_csv(file_path, encoding='utf-8', index=False) # load the test data test = pd.DataFrame() for dataset_name in TEST_SETS: file_path = os.path.join(vectors_dir, '{}.csv'.format(dataset_name)) test = test.append(pd.read_csv(file_path, encoding='utf-8', dtype=dDict)) test['feature8'] = 1-((2*test.lexstat_simAB)/(test.lexstat_simAA+test.lexstat_simBB)) for db in test.db.unique(): file_path = os.path.join(output_dir, '{}.svmCC.csv'.format(db)) wl = testCluster(db) wl.to_csv(file_path, encoding='utf-8', index=False) def f(x): return svmInfomapCluster(x) def infomap_clustering(threshold, matrix, taxa=False, revert=False): """ Compute the Infomap clustering analysis of the data. Taken from LingPy's implementation of the algorithm. """ if not igraph: raise ValueError("The package igraph is needed to run this analysis.") if not taxa: taxa = list(range(1, len(matrix) + 1)) G = igraph.Graph() vertex_weights = [] for i in range(len(matrix)): G.add_vertex(i) vertex_weights += [0] # variable stores edge weights, if they are not there, the network is # already separated by the threshold for i,row in enumerate(matrix): for j,cell in enumerate(row): if i < j: if cell <= threshold: G.add_edge(i, j) comps = G.community_infomap(edge_weights=None, vertex_weights=None) D = {} for i,comp in enumerate(comps.subgraphs()): vertices = [v['name'] for v in comp.vs] for vertex in vertices: D[vertex] = i+1 if revert: return D clr = defaultdict(list) for i,t in enumerate(taxa): clr[D[i]] += [t] return clr def svmInfomapCluster(vdb,featureSubset=FEATURES,th=.34,C=.82,kernel='linear',gamma=1E-3): """ The first argument is the validation data base, the rest of the training databases are used for training. """ newWordList = pd.DataFrame() fitting = trainingVectors[trainingVectors.db!=vdb] validation = training[training.db==vdb].copy() X = fitting[featureSubset].values y = fitting.target.values svClf = svm.SVC(kernel=kernel,C=C,gamma=gamma, probability=True) svClf.fit(X,y) nprandom.seed(1234) random.seed(1234) svScores = svClf.predict_proba(validation[featureSubset].values)[:,1] validation['svScores'] = svScores scores = pd.DataFrame() wordlist = pd.DataFrame() concepts = validation.gloss.unique() taxa = unique(validation[['l1','l2']].values.flatten()) dataWordlist = vstack([validation[['gloss','l1','w1','cc1']].values, validation[['gloss','l2','w2','cc2']].values]) dataWordlist = pd.DataFrame(dataWordlist,columns=['concept','doculect', 'counterpart','cc']) dataWordlist = dataWordlist.drop_duplicates() dataWordlist.index = ['_'.join(map(unicode,x)) for x in dataWordlist[['concept','doculect','counterpart']].values] validation['id_1'] = [c+'_'+l+'_'+unicode(w) for (c,l,w) in validation[['gloss','l1','w1']].values] validation['id_2'] = [c+'_'+l+'_'+unicode(w) for (c,l,w) in validation[['gloss','l2','w2']].values] for c in concepts: dataC= validation[validation.gloss==c].copy() dataC['id_1'] = [x.replace(' ','').replace(',','') for x in dataC.id_1] dataC['id_2'] = [x.replace(' ','').replace(',','') for x in dataC.id_2] wlC = dataWordlist[dataWordlist.concept==c].copy() if len(wlC)>1: wlC.index = [x.replace(' ','').replace(',','') for x in wlC.index] svMtx = zeros((len(wlC.index),len(wlC.index))) svMtx[pd.match(dataC.id_1,wlC.index), pd.match(dataC.id_2,wlC.index)] = dataC.svScores.values svMtx[pd.match(dataC.id_2,wlC.index),
pd.match(dataC.id_1,wlC.index)
pandas.match
import pandas as pd from typing import Dict, List, Optional, Tuple def integrate_col(df: pd.DataFrame, val_col: str, class_col: str = 'edc_id') -> Dict[str, float]: res: Dict[str, float] = dict() for class_id in df[class_col].unique(): val = 0 prev_t = 0 prev_val = 0 sub_df = df[df[class_col] == class_id] for _, row in sub_df.iterrows(): new_t = row['time'] new_val = row[val_col] val += prev_val * (new_t - prev_t) prev_t = new_t prev_val = new_val res[class_id] = val return res def mean_value(df: pd.DataFrame, val_col: str, class_col: str = 'edc_id') -> Dict[str, float]: total_t = df['time'].max() - df['time'].min() return {class_id: val / total_t for class_id, val in integrate_col(df, val_col, class_col).items()} def energy_per_edc(path: str, sep: str = ',', edc_col: str = 'edc_id', pwr_col: str = 'power_demand') -> Dict[str, float]: return {edc_id: joules / 3600 for edc_id, joules in integrate_col(pd.read_csv(path, sep), pwr_col, edc_col).items()} def mean_delay_per_service(path: str, sep: str = ',', ue_id: Optional[str] = None, action: Optional[str] = None) -> Dict[str, float]: res: Dict[str, float] = dict() df = pd.read_csv(path, sep) if ue_id is not None: df = df[df['ue_id'] == ue_id] if action is not None: df = df[df['action'] == action] for srv_id in df['service_id'].unique(): srv_df = df[df['service_id'] == srv_id] res[srv_id] = srv_df['delay'].mean() return res def create_energy_report(consumers_path: str, offers_path: str, output_path: str, sep: str = ',', consumer_col: str = 'consumer_id', pwr_col: str = 'power_consumption'): res =
pd.DataFrame(columns=['time', consumer_col, pwr_col, 'acc_return', 'acc_energy', 'acc_cost'], index=None)
pandas.DataFrame
''' Feature scoring functionality ''' from operator import itemgetter import math import numpy as np import pandas as pd from sklearn.ensemble import (ExtraTreesClassifier, ExtraTreesRegressor, RandomForestClassifier, RandomForestRegressor) from sklearn.feature_selection import (f_regression, f_classif, chi2) from .scenario_discovery_util import RuleInductionType from ..util import get_module_logger # Created on Jul 9, 2014 # # .. codeauthor:: jhkwakkel <j.h.kwakkel (at) tudelft (dot) nl> # # TODO:: look at # http://scikit-learn.org/stable/auto_examples/linear_model/plot_sparse_recovery.html#example-linear-model-plot-sparse-recovery-py __all__ = ['F_REGRESSION', 'F_CLASSIFICATION', 'CHI2', 'get_univariate_feature_scores', 'get_rf_feature_scores', 'get_ex_feature_scores', 'get_feature_scores_all'] _logger = get_module_logger(__name__) F_REGRESSION = f_regression F_CLASSIFICATION = f_classif CHI2 = chi2 def _prepare_experiments(experiments): ''' transform the experiments structured array into a numpy array. Parameters ---------- experiments :DataFrame Returns ------- ndarray, list ''' try: experiments = experiments.drop('scenario', axis=1) except KeyError: pass x = experiments.copy() x_nominal = x.select_dtypes(exclude=np.number) x_nominal_columns = x_nominal.columns.values for column in x_nominal_columns: if np.unique(x[column]).shape == (1,): x = x.drop(column, axis=1) _logger.info(("{} dropped from analysis " "because only a single category").format(column)) else: x[column] = x[column].astype('category').cat.codes return x.values, x.columns.tolist() def _prepare_outcomes(outcomes, classify): ''' transform the outcomes dict into a vector with either the class allocation or the value. Parameters ---------- outcomes : dict the outcomes dict classify : callable or str a classify function or variable analogous to PRIM Returns ------- 1d ndarray the return from classify bool data is categorical (True) or continuous (False) Raises -------- TypeError if classify is neither a StringType nor a callable KeyError if classify is a string which is not a key in the outcomes dict. ''' if isinstance(classify, str): try: y = outcomes[classify] except KeyError as e: raise e categorical = False elif callable(classify): y = classify(outcomes) categorical = True else: raise TypeError("unknown type for classify") return y, categorical def get_univariate_feature_scores(x, y, score_func=F_CLASSIFICATION): ''' calculate feature scores using univariate statistical tests. In case of categorical data, chi square or the Anova F value is used. In case of continuous data the Anova F value is used. Parameters ---------- x : structured array y : 1D nd.array score_func : {F_CLASSIFICATION, F_REGRESSION, CHI2} the score function to use, one of f_regression (regression), or f_classification or chi2 (classification). Returns ------- pandas DataFrame sorted in descending order of tuples with uncertainty and feature scores (i.e. p values in this case). ''' x, uncs = _prepare_experiments(x) pvalues = score_func(x, y)[1] pvalues = np.asarray(pvalues) pvalues = zip(uncs, pvalues) pvalues = list(pvalues) pvalues.sort(key=itemgetter(1)) pvalues =
pd.DataFrame(pvalues)
pandas.DataFrame
from bs4 import BeautifulSoup from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.chrome.service import Service import pandas as pd import requests import time import re import os def grab_all_urls_of_this_type(car_type): # Open the chrome browser path = Service('/Users/yangm/Desktop/chromedriver/chromedriver') driver = webdriver.Chrome(service=path) driver.get("http://www.edmunds.com/") driver.maximize_window() # Enter to the webpage with this car_type time.sleep(1) driver.execute_script("window.scrollTo(0, 600)") driver.find_element(By.CSS_SELECTOR,"a[data-tracking-value='{}']".format(car_type)).click() # Close the ads time.sleep(1) driver.find_element(By.CSS_SELECTOR, ".fixed-bottom.sticky-banner>.edm-entry.aunit.pos-r>button[data-tracking-id='close_adhesion']").click() # Find all collapsed elements collapse_buttons = driver.find_elements(By.CSS_SELECTOR, ".collapse-control.mb-0.row.align-items-center.mx-0.mb-1_5.justify-content-start>button[data-tracking-id='subtype_list_expand']") # Click each of them for i in range(len(collapse_buttons)): ActionChains(driver).move_to_element(collapse_buttons[i]).perform() time.sleep(1) collapse_buttons[i].click() urls_and_years = driver.find_elements(By.CSS_SELECTOR,".vehicle-card.rounded.mb-1_5.pb-1") urls = [] for i in range(len(urls_and_years)): url = urls_and_years[i].find_element(By.CSS_SELECTOR,"a[data-tracking-value='model_image']").get_attribute( "href") try: years = urls_and_years[i].find_element(By.CSS_SELECTOR, ".mb-0.list-unstyled.col-12.col-md-8>.mb-1>.label").text[-4:] years = int(years) print(years) while years <= 2022: url = re.sub('[0-9]{4}', str(years), url) url = os.path.join(url + "consumer-reviews/?pagesize=50") urls.append(url) years += 1 except: url = os.path.join(url + "consumer-reviews/?pagesize=50") urls.append(url) print('error') continue return urls def get_posts(url,count=False): header = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36" } response = requests.get(url, timeout=10, headers=header) soup = BeautifulSoup(response.text, 'html.parser') if count: posts = soup.find('div', class_ ='review-count text-gray-darker') else: posts = soup.find_all('div', class_ ='review-item text-gray-darker') return posts def find_review_and_ratings_of_this_page(posts): # Create all lists reviews = [] Total_star = [] Safety = [] Technology = [] Performance = [] Interior = [] Comfort = [] Reliability = [] Value = [] for i in range(len(posts)): # Reviews and total stars reviews.append(posts[i].find('p').text.strip()) Total_star.append(posts[i].find('span', class_="sr-only").text[0]) # Safety rating star (if none, output 0) Safety_tag = posts[i].find('dt', string="Safety") if Safety_tag is None: Safety.append('0') else: Safety.append(Safety_tag.next_sibling.find('span', class_='sr-only').text[0]) # Technology rating star (if none, output 0) Technology_tag = posts[i].find('dt', string="Technology") if Technology_tag is None: Technology.append('0') else: Technology.append(Technology_tag.next_sibling.find('span', class_='sr-only').text[0]) # Performance rating star (if none, output 0) Performance_tag = posts[i].find('dt', string="Performance") if Performance_tag is None: Performance.append('0') else: Performance.append(Performance_tag.next_sibling.find('span', class_='sr-only').text[0]) # Interior rating star (if none, output 0) Interior_tag = posts[i].find('dt', string="Interior") if Interior_tag is None: Interior.append('0') else: Interior.append(Interior_tag.next_sibling.find('span', class_='sr-only').text[0]) # Comfort rating star (if none, output 0) Comfort_tag = posts[i].find('dt', string="Comfort") if Comfort_tag is None: Comfort.append('0') else: Comfort.append(Comfort_tag.next_sibling.find('span', class_='sr-only').text[0]) # Reliability rating star (if none, output 0) Reliability_tag = posts[i].find('dt', string="Reliability") if Reliability_tag is None: Reliability.append('0') else: Reliability.append(Reliability_tag.next_sibling.find('span', class_='sr-only').text[0]) # Value rating star (if none, output 0) Value_tag = posts[i].find('dt', string="Value") if Value_tag is None: Value.append('0') else: Value.append(Value_tag.next_sibling.find('span', class_='sr-only').text[0]) df = pd.DataFrame({'Total_star': Total_star, 'Review': reviews, 'Safety': Safety, 'Technology': Technology, 'Performance': Performance, 'Interior': Interior, 'Comfort': Comfort, 'Reliability': Reliability, 'Value': Value, }) return df def the_number_of_pages(post): number_of_reviews = int(post.text[:2]) number_of_pages = number_of_reviews // 50 if number_of_reviews % 50 > 0: number_of_pages += 1 return number_of_pages def grab_reviews_and_ratings_of_this_model(url): count_post = get_posts(url, count=True) number_of_pages = the_number_of_pages(count_post) df = pd.DataFrame() for page_number in range(number_of_pages): page_number += 1 url = os.path.join(url + "&pagenum=%s" % page_number) posts = get_posts(url) temp_df = find_review_and_ratings_of_this_page(posts) df = pd.concat([df, temp_df], ignore_index=True) # Obtain the basic information url_splited = re.split('/', url) make = url_splited[3] df.insert(0, 'Make', make) model = url_splited[4] df.insert(1, 'Model', model) year = url_splited[5] df.insert(2, 'Year', year) return df if __name__ == '__main__': car_types = ['SUVs', 'Trucks', 'Sedans', 'Electric Cars', 'Hybrids', 'Hatchbacks', 'Coupes', 'Wagons', 'Convertibles', 'Minivans'] error = 0 errors = [] list_of_dfs = [] for car_type in car_types: dfs = pd.DataFrame() urls = grab_all_urls_of_this_type(car_type) for url in urls: try: df_temp = grab_reviews_and_ratings_of_this_model(url) dfs = pd.concat([dfs, df_temp], ignore_index=True) except: error += 1 errors.append(url) print(error) list_of_dfs.append(dfs) final_df = pd.DataFrame() for dfs, car_type in zip(list_of_dfs, car_types): dfs.insert(0, "Type", car_type) final_df =
pd.concat([final_df, dfs])
pandas.concat
import pandas as pd import plotly.graph_objects as go import plotly.express as px import plotly.io as pio import plotly as pl import re import requests from .DataFrameUtil import DataFrameUtil as dfUtil class CreateDataFrame(): """Classe de serviços para a criação de dataframes utilizados para a construção dos gráficos""" def __init__(self): self.dfTimeSeriesCases = pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv') self.dfTimeSeriesRecover = pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv') self.dfTimeSeriesDeath = pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv') url = 'https://covid19.who.int/WHO-COVID-19-global-table-data.csv' self.dfRegioes =
pd.read_csv(url)
pandas.read_csv
import argparse import numpy as np import pandas as pd from PIL import Image from pathlib import Path from skimage import measure from mmdet.apis import inference_detector, init_detector ################################################################################ parser = argparse.ArgumentParser() parser.add_argument('--meta_csv', default='', type=Path) parser.add_argument('--model_config', default='config.py', type=Path) parser.add_argument('--model_weights', default='published_weights-70db75db.pth', type=Path) parser.add_argument('--save_folder', required=True, type=Path) parser.add_argument('--GPU_ID', default=0, type=int) args = parser.parse_args() ################################################################################ config = str(args.model_config) checkpoint = str(args.model_weights) model = init_detector(config, checkpoint, device=f'cuda:{args.GPU_ID}') model.test_cfg.rcnn.mask_thr_binary = 0.4 model.test_cfg.rcnn.score_thr = 0.5 model.test_cfg.rcnn.nms.iou_threshold = 0.99 model.test_cfg.rcnn.max_per_img = 250 model.test_cfg.rpn.nms.iou_threshold = 0.99 ################################################################################ # make prediction for a vr2 def get_ID_predmask(files, model=model): Ns = np.array([int(i.split('_')[-1].replace('.png', '')) for i in files]) files = files[np.argsort(Ns)] results = {} for idx, f in enumerate(files): tmpres = inference_detector(model, f) results[idx+1] = np.array(tmpres[1][0]) all_res = np.zeros((1024, 1024 + 500*(len(files)-1))) for i in range(1, len(files) + 1): if len(results[i]) > 0: all_res[:,(i-1)*500:((i-1)*500 + results[i].sum(0).shape[1])] += results[i].sum(0) return (all_res != 0)[::-1, :] #flip frequency def make_blobs_from_preds(predictions): blobs = measure.label(predictions) for i in pd.unique(blobs.ravel()): # if a blob has less than 200 pixel, throw it if (blobs == i).sum() < 200: blobs[blobs == i] = 0 else: freqs = (blobs == i).sum(1) minfreq = np.array(range(1024))[freqs > 0].min() maxfreq = np.array(range(1024))[freqs > 0].max() # if a blob covers less than 100 frequency bins, throw it if (maxfreq-minfreq) < 100: blobs[blobs == i] = 0 continue times = (blobs == i).sum(0) mintime = np.array(range(len(times)))[times > 0].min() maxtime = np.array(range(len(times)))[times > 0].max() # if a blob is shorter than 100ms, throw it if (maxtime-mintime) < 100: blobs[blobs == i] = 0 return blobs def narrow_trace(preds): times = [] freqs = np.arange(1024) for f in freqs: if preds[int(f), :].sum() == 0: t = 0 else: # narrowed trace = middle of the predicted, wider trace t = np.arange(preds.shape[1])[preds[f, :]].mean() times.append(t) times = np.array(times) freqs = freqs[times != 0] times = times[times != 0] return {'f':freqs, 't':times} def save_trace(trace, fname): traceDF =
pd.DataFrame(trace)
pandas.DataFrame
import os import pandas import sqlalchemy as sa import json import wx, wx.adv, wx.lib from datetime import datetime from wx.lib.wordwrap import wordwrap from threading import Thread from pydispatch import dispatcher from openpyxl import load_workbook CLOSE_DIALOG_SIGNAL = 'close-notification-dialog' from components.datatable import DataGrid from components.fieldcreation import (FieldCreateDialog, OrganismFieldFormDialog, DrugRegFormDialog, IndexFieldList, HeatmapFieldList, DateRangeFieldList) APPDATA_DIR = 'appdata' DRUG_REGISTRY_FILE = 'drugs.json' basepath = os.path.dirname(os.path.abspath(__file__)) drug_dict = {} drug_df = None def load_drug_registry(): global drug_dict global drug_df if DRUG_REGISTRY_FILE: try: drug_df = pandas.read_json(os.path.join(APPDATA_DIR, DRUG_REGISTRY_FILE)) except: return pandas.DataFrame(columns=['drug', 'abbreviation', 'group']) else: drug_dict = {} if drug_df.empty: drug_df = pandas.DataFrame(columns=['drug', 'abbreviation', 'group']) else: drug_df = drug_df.sort_values(['group']) for idx, row in drug_df.iterrows(): drug = row if row['abbreviation']: abbrs = [a.strip().lower() for a in row['abbreviation'].split(',')] else: abbrs = [] for ab in abbrs: drug_dict[ab] = drug class FieldAttribute(): def __init__(self): self.data = {} self.columns = [] self.organisms = {} def update_from_json(self, json_data): if not self.columns: return False json_data = json.loads(json_data) profile_cols = json_data['columns'] profile_cols_no_agg = set([col for col in profile_cols if not col.startswith('@')]) if profile_cols_no_agg.difference(self.columns): return False else: self.columns = profile_cols self.data = json_data['data'] self.organisms = json_data['organisms'] return True def update_from_json_for_database(self, json_data): """ Update columns with data from the saved profile :param json_data: :return: Boolean """ # Data must be loaded first if not self.columns: return False json_data = json.loads(json_data) profile_cols = json_data['columns'] # columns must match profile_cols_no_agg = [col for col in profile_cols if not col.startswith('@')] assert len(set(profile_cols_no_agg).difference(set(self.columns))) == 0 self.columns = profile_cols self.data = json_data['data'] self.organisms = json_data['organisms'] return True def update_from_dataframe(self, data_frame): self.columns = [] for n, column in enumerate(data_frame.columns): self.columns.append(column) self.data[column] = {'name': column, 'alias': column, 'organism': False, 'key': False, 'drug': True if column.lower() in drug_dict else False, 'date': False, 'type': str(data_frame[column].dtype), 'keep': True, 'desc': "", } def values(self): return self.data.values() def get_column(self, colname): try: return self.data[colname] except KeyError as e: raise AttributeError(e) def iget_column(self, index): try: return self.columns[index] except IndexError: return None def get_col_index(self, colname): try: return self.columns.index(colname) except ValueError: return -1 def is_col_aggregate(self, colname): if colname in self.data: if self.data[colname].get('aggregate'): return True else: return False else: raise KeyError def update_organisms(self, df): self.organisms = {} for idx, row in df.iterrows(): self.organisms[row[0]] = {'genus': row[1], 'species': row[2]} def browse(filetype='MLAB'): file_meta = { 'MLAB': { 'wildcard': "Excel files (*.xls;*xlsx)|*.xls;*.xlsx" }, 'CSV': { 'wildcard': "CSV files (*.csv)|*.csv" } } with wx.FileDialog(None, "Open data file", wildcard=file_meta[filetype]['wildcard'], style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) \ as fileDialog: if fileDialog.ShowModal() == wx.ID_CANCEL: return return fileDialog.GetPath() def show_sheets(parent, worksheets): dlg = wx.SingleChoiceDialog(None, "Select a worksheet", "Worksheets", worksheets) if dlg.ShowModal() == wx.ID_OK: return dlg.GetStringSelection() dlg.Destroy() class NotificationBox(wx.Dialog): def __init__(self, parent, caption, message): super(NotificationBox, self).__init__(parent=parent, title=caption, size=(300, 90), style=wx.CAPTION) self.label = wx.StaticText(self, label=message) vsizer = wx.BoxSizer(wx.VERTICAL) vsizer.Add(self.label, 1, wx.ALL | wx.EXPAND | wx.CENTER, 20) self.SetSizer(vsizer) self.Center(wx.HORIZONTAL) dispatcher.connect(self.endModal, signal=CLOSE_DIALOG_SIGNAL, sender=dispatcher.Any) def updateLabel(self, msg): self.label.SetLabelText(msg) def endModal(self, rc): # Not sure why sometimes the dialog is not modal, # but failing to check it causes an error. if self.IsModal(): self.EndModal(rc) else: return rc class MainWindow(wx.Frame): def __init__(self, parent): super(MainWindow, self).__init__(parent) scr_width, scr_height = wx.DisplaySize() self.SetIcon(wx.Icon(os.path.join(basepath, 'icons/appicon.ico'))) self.version_no = '2019.1.8' self.description = 'Faculty of Medical Technology, Mahidol University' self.SetTitle('Mivisor Version {}'.format(self.version_no)) self.SetSize((int(scr_width * 0.75), int(scr_height * 0.85))) self.Center() self.current_column = None self.data_filepath = None self.profile_filepath = None self.db_filepath = None self.current_session_id = None self.dbengine = None self.data_loaded = False self.field_attr = FieldAttribute() df = pandas.DataFrame({'Name': ['Mivisor'], 'Version': [self.version_no], 'Released': ['2019-08-04'], 'Brought to you by': [self.description], 'Contact': ['<EMAIL>']}) menubar = wx.MenuBar() fileMenu = wx.Menu() dataMenu = wx.Menu() fieldMenu = wx.Menu() exportMenu = wx.Menu() antibiogramMenu = wx.Menu() registryMenu = wx.Menu() analyzeMenu = wx.Menu() aboutMenu = wx.Menu() databaseMenu = wx.Menu() imp = wx.Menu() mlabItem = imp.Append(wx.ID_ANY, 'Excel (MLAB)') # csvItem = imp.Append(wx.ID_ANY, 'CSV') # csvItem.Enable(False) fileMenu.AppendSeparator() fileMenu.Append(wx.ID_ANY, 'I&mport', imp) fileMenu.AppendSeparator() self.loadProfileItem = fileMenu.Append(wx.ID_ANY, 'Load Profile') self.loadProfileItem.Enable(False) self.saveProfileItem = fileMenu.Append(wx.ID_ANY, 'Save Profile') self.saveProfileItem.Enable(False) exitItem = fileMenu.Append(wx.ID_EXIT, 'Quit', 'Quit Application') self.createFieldItem = fieldMenu.Append(wx.ID_ANY, 'Matching') self.createFieldItem.Enable(False) dataMenu.Append(wx.ID_ANY, 'New field', fieldMenu) self.saveToDatabaseMenuItem = dataMenu.Append(wx.ID_ANY, 'Save to database') self.saveToDatabaseMenuItem.Enable(False) self.appendToDatabaseMenuItem = dataMenu.Append(wx.ID_ANY, 'Append to database') self.appendToDatabaseMenuItem.Enable(False) dataMenu.AppendSeparator() self.organismItem = dataMenu.Append(wx.ID_ANY, 'Organism') self.organismItem.Enable(False) dataMenu.AppendSeparator() self.exportToExcelMenuItem = exportMenu.Append(wx.ID_ANY, 'To Excel') self.saveToFlatDbMenuItem = exportMenu.Append(wx.ID_ANY, 'Create flat database') self.addToFlatDbMenuItem = exportMenu.Append(wx.ID_ANY, 'Add to flat database') self.exportToExcelMenuItem.Enable(False) self.saveToFlatDbMenuItem.Enable(False) self.addToFlatDbMenuItem.Enable(False) dataMenu.Append(wx.ID_ANY, 'Export flat table', exportMenu) drugRegMenuItem = registryMenu.Append(wx.ID_ANY, 'Drugs') self.biogramDbMenuItem = antibiogramMenu.Append(wx.ID_ANY, 'Create summary report') self.biogramDbMenuItem.Enable(True) self.biogramHeatmapMenuItem = antibiogramMenu.Append(wx.ID_ANY, 'Create heatmap plot') self.biogramHeatmapMenuItem.Enable(True) analyzeMenu.Append(wx.ID_ANY, 'Antibiogram', antibiogramMenu) aboutMenuItem = aboutMenu.Append(wx.ID_ANY, "About the program") self.connectDbMenuItem = databaseMenu.Append(wx.ID_ANY, 'Connect') self.disconnectDbMenuItem = databaseMenu.Append(wx.ID_ANY, 'Disconnect') self.Bind(wx.EVT_MENU, self.onConnectDbMenuItemClick, self.connectDbMenuItem) self.Bind(wx.EVT_MENU, self.onDisconnectDbMenuItemClick, self.disconnectDbMenuItem) self.Bind(wx.EVT_MENU, lambda x: self.onSaveToDatabaseMenuItemClick(x, action='replace'), self.saveToDatabaseMenuItem) self.Bind(wx.EVT_MENU, lambda x: self.onSaveToDatabaseMenuItemClick(x, action='append'), self.appendToDatabaseMenuItem) menubar.Append(fileMenu, '&File') menubar.Append(dataMenu, '&Data') menubar.Append(databaseMenu, 'Database') menubar.Append(analyzeMenu, 'Analy&ze') menubar.Append(registryMenu, '&Registry') menubar.Append(aboutMenu, '&About') self.SetMenuBar(menubar) accel_tbl = wx.AcceleratorTable([ (wx.ACCEL_CTRL, ord('M'), mlabItem.GetId()), ]) self.SetAcceleratorTable(accel_tbl) import sys self.Bind(wx.EVT_CLOSE, lambda x: sys.exit()) self.Bind(wx.EVT_MENU, self.on_about_menu_click, aboutMenuItem) self.Bind(wx.EVT_MENU, self.OnQuit, exitItem) self.Bind(wx.EVT_MENU, self.onLoadMLABItemClick, mlabItem) # self.Bind(wx.EVT_MENU, self.OnLoadCSV, csvItem) self.Bind(wx.EVT_MENU, self.OnCreateField, self.createFieldItem) self.Bind(wx.EVT_MENU, self.OnSaveProfile, self.saveProfileItem) self.Bind(wx.EVT_MENU, self.OnLoadProfile, self.loadProfileItem) self.Bind(wx.EVT_MENU, self.OnOrganismClick, self.organismItem) # TODO: rename OnExportRawData method self.Bind(wx.EVT_MENU, self.OnExportRawData, self.exportToExcelMenuItem) self.Bind(wx.EVT_MENU, lambda x: self.onSaveToFlatDbMenuItemClick(x, action='replace'), self.saveToFlatDbMenuItem) self.Bind(wx.EVT_MENU, lambda x: self.onSaveToFlatDbMenuItemClick(x, action='append'), self.addToFlatDbMenuItem) self.Bind(wx.EVT_MENU, self.on_drug_reg_menu_click, drugRegMenuItem) self.Bind(wx.EVT_MENU, self.onBiogramDbMenuItemClick, self.biogramDbMenuItem) self.Bind(wx.EVT_MENU, self.onBiogramHeatmapMenuItemClick, self.biogramHeatmapMenuItem) # init panels self.info_panel = wx.Panel(self, wx.ID_ANY) self.preview_panel = wx.Panel(self, wx.ID_ANY) self.summary_panel = wx.Panel(self, wx.ID_ANY) self.attribute_panel = wx.Panel(self, wx.ID_ANY) self.edit_panel = wx.Panel(self, wx.ID_ANY) # init sizers self.info_sizer = wx.StaticBoxSizer(wx.VERTICAL, self.info_panel, "Session Information") self.summary_sizer = wx.StaticBoxSizer(wx.VERTICAL, self.summary_panel, "Field Summary") self.field_attr_sizer = wx.StaticBoxSizer(wx.VERTICAL, self.attribute_panel, "Field Attributes") edit_box_sizer = wx.StaticBoxSizer(wx.HORIZONTAL, self.edit_panel, "Edit") self.data_grid_box_sizer = wx.StaticBoxSizer(wx.VERTICAL, self.preview_panel, "Data Preview") self.profile_lbl = wx.StaticText(self.info_panel, -1, "Profile filepath: {}".format(self.profile_filepath)) self.datafile_lbl = wx.StaticText(self.info_panel, -1, "Data filepath: {}".format(self.data_filepath)) self.dbfile_lbl = wx.StaticText(self.info_panel, -1, "Database filepath: {}".format(self.db_filepath)) self.info_sizer.Add(self.datafile_lbl) self.info_sizer.Add(self.profile_lbl) self.info_sizer.Add(self.dbfile_lbl) self.data_grid = DataGrid(self.preview_panel) self.data_grid.set_table(df) self.data_grid.AutoSize() self.data_grid_box_sizer.Add(self.data_grid, 1, flag=wx.EXPAND | wx.ALL) self.key_chkbox = wx.CheckBox(self.edit_panel, -1, label="Key", name="key") self.drug_chkbox = wx.CheckBox(self.edit_panel, -1, label="Drug", name="drug") self.organism_chkbox = wx.CheckBox(self.edit_panel, -1, label="Organism", name="organism") self.keep_chkbox = wx.CheckBox(self.edit_panel, -1, label="Included", name="keep") self.date_chkbox = wx.CheckBox(self.edit_panel, -1, label="Date", name="date") self.field_edit_checkboxes = [self.key_chkbox, self.drug_chkbox, self.keep_chkbox, self.organism_chkbox, self.date_chkbox] checkbox_sizer = wx.FlexGridSizer(cols=len(self.field_edit_checkboxes), hgap=4, vgap=0) for chkbox in self.field_edit_checkboxes: checkbox_sizer.Add(chkbox) chkbox.Bind(wx.EVT_CHECKBOX, self.on_edit_save_button_clicked) checkbox_label = wx.StaticText(self.edit_panel, -1, "Marked as") self.field_desc = wx.TextCtrl(self.edit_panel, -1, "", style=wx.TE_MULTILINE, size=(200, 100)) self.field_alias = wx.TextCtrl(self.edit_panel, -1, "") self.edit_save_button = wx.Button(self.edit_panel, -1, "Update") self.edit_save_button.Bind(wx.EVT_BUTTON, self.on_edit_save_button_clicked) alias_label = wx.StaticText(self.edit_panel, -1, "Alias") desc_label = wx.StaticText(self.edit_panel, -1, "Description") form_sizer = wx.FlexGridSizer(cols=2, hgap=2, vgap=2) form_sizer.AddMany([checkbox_label, checkbox_sizer]) form_sizer.AddMany([desc_label, self.field_desc]) form_sizer.AddMany([alias_label, self.field_alias]) form_sizer.AddMany([wx.StaticText(self.edit_panel, -1, ""), self.edit_save_button]) edit_box_sizer.Add(form_sizer, 1, flag=wx.ALIGN_LEFT) self.summary_table = wx.ListCtrl(self.summary_panel, style=wx.LC_REPORT) self.summary_table.InsertColumn(0, 'Field') self.summary_table.InsertColumn(1, 'Value') self.summary_sizer.Add(self.summary_table, 1, wx.EXPAND) self.field_attr_list = wx.ListCtrl(self.attribute_panel, style=wx.LC_REPORT) self.add_field_attr_list_column() self.field_attr_sizer.Add(self.field_attr_list, 1, wx.EXPAND) self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.onFieldAttrListItemSelected) self.preview_panel.SetSizer(self.data_grid_box_sizer) self.attribute_panel.SetSizer(self.field_attr_sizer) self.summary_panel.SetSizer(self.summary_sizer) self.edit_panel.SetSizer(edit_box_sizer) self.info_panel.SetSizer(self.info_sizer) self.vbox = wx.BoxSizer(wx.VERTICAL) self.hbox = wx.BoxSizer(wx.HORIZONTAL) self.hbox.Add(self.edit_panel, 2, flag=wx.ALL | wx.EXPAND) self.hbox.Add(self.summary_panel, 1, flag=wx.ALL | wx.EXPAND) self.vbox.Add(self.info_panel, 0, flag=wx.EXPAND | wx.ALL) self.vbox.Add(self.preview_panel, 1, flag=wx.EXPAND | wx.ALL) self.vbox.Add(self.attribute_panel, flag=wx.ALL | wx.EXPAND) self.vbox.Add(self.hbox, flag=wx.ALL | wx.EXPAND | wx.ALL) self.SetSizer(self.vbox) load_drug_registry() def OnQuit(self, e): self.Close() def OnOrganismClick(self, event): columns = [] sel_col = None for c in self.field_attr.columns: col = self.field_attr.get_column(c) if col['keep'] and col['organism']: columns.append(col['alias']) if not columns: dlg = wx.MessageDialog(None, "No organism field specified.", "Please select a field for organism.", wx.OK) ret = dlg.ShowModal() if ret == wx.ID_OK: return dlg = wx.SingleChoiceDialog(None, "Select a column", "Kept columns", columns) if dlg.ShowModal() == wx.ID_OK: sel_col = dlg.GetStringSelection() dlg.Destroy() if sel_col: sel_col_index = self.field_attr.get_col_index(sel_col) column = self.field_attr.get_column(sel_col) values = self.data_grid.table.df[sel_col].unique() fc = OrganismFieldFormDialog() if not self.field_attr.organisms: _df = pandas.DataFrame({column['alias']: values, 'genus': None, 'species': None}) else: orgs = [] genuses = [] species = [] for org in self.field_attr.organisms: orgs.append(org) genuses.append(self.field_attr.organisms[org]['genus']) species.append(self.field_attr.organisms[org]['species']) _df = pandas.DataFrame({column['alias']: orgs, 'genus': genuses, 'species': species}) fc.grid.set_table(_df) resp = fc.ShowModal() self.field_attr.update_organisms(fc.grid.table.df) def load_profile_from_filepath(self, df): try: fp = open(self.profile_filepath, 'r') except IOError: wx.MessageDialog(self, 'Cannot read data from {}. Please double check the file path.'.format( self.profile_filepath), 'The profile file cannot be loaded', wx.ICON_ERROR).ShowModal() return json_data = fp.read() fp.close() if not self.field_attr.update_from_json_for_database(json_data): wx.MessageDialog(self, 'Fields in the profile and the data do not match.', 'The profile cannot be loaded', wx.ICON_INFORMATION).ShowModal() return for c in self.field_attr.columns: if self.field_attr.is_col_aggregate(c): column = self.field_attr.get_column(c) column_index = self.field_attr.get_col_index(c) if c not in df.columns: d = [] from_col = column['aggregate']['from'] dict_ = column['aggregate']['data'] for value in df[from_col]: d.append(dict_.get(value, value)) df.insert(column_index, c, value=d) return df def OnLoadProfile(self, event): if not self.data_filepath: dlg = wx.MessageDialog(None, "No data for this session.", "Please provide data for this session first.", wx.OK | wx.CENTER) ret = dlg.ShowModal() return wildcard = "JSON (*.json)|*.json" _profile_pth = self.data_filepath or os.getcwd() with wx.FileDialog(None, "Choose a file", _profile_pth, "", wildcard, wx.FC_OPEN) as file_dlg: if file_dlg.ShowModal() == wx.ID_CANCEL: return try: fp = open(file_dlg.GetPath(), 'r') json_data = fp.read() fp.close() if not self.field_attr.update_from_json(json_data): wx.MessageDialog(self, 'Fields in the profile and the data do not match.', 'The profile cannot be loaded', wx.ICON_INFORMATION).ShowModal() return for c in self.field_attr.columns: if self.field_attr.is_col_aggregate(c): column = self.field_attr.get_column(c) column_index = self.field_attr.get_col_index(c) if c not in self.data_grid.table.df.columns: d = [] from_col = column['aggregate']['from'] dict_ = column['aggregate']['data'] for value in self.data_grid.table.df[from_col]: d.append(dict_.get(value, value)) self.data_grid.table.df.insert(column_index, c, value=d) self.data_grid.table.InsertCols(column_index, 1) self.refresh_field_attr_list_column() self.update_edit_panel(self.field_attr.iget_column(0)) self.profile_filepath = file_dlg.GetPath() self.profile_lbl.SetLabelText("Profile filepath: {}".format(self.profile_filepath)) except IOError: print('Cannot load data from file.') def OnSaveProfile(self, event): wildcard = "JSON (*.json)|*.json" with wx.FileDialog(None, "Choose a file to save a profile.", os.getcwd(), "", wildcard, wx.FC_SAVE) as file_dlg: if file_dlg.ShowModal() == wx.ID_CANCEL: return try: fp = open(file_dlg.GetPath(), 'w') for col in self.field_attr.columns: column = self.field_attr.get_column(col) fp.write(json.dumps({'data': self.field_attr.data, 'columns': self.field_attr.columns, 'organisms': self.field_attr.organisms}, indent=2)) fp.close() self.profile_filepath = file_dlg.GetPath() self.profile_lbl.SetLabelText("Profile filepath: {}".format(self.profile_filepath)) except IOError: print('Cannot save data to file.') def load_datafile(self, filetype='MLAB'): filepath = browse(filetype) if filepath and filepath: try: worksheets = load_workbook(filepath).sheetnames except FileNotFoundError: wx.MessageDialog(self, 'Cannot download the data file.\nPlease check the file path again.', 'File Not Found!', wx.OK | wx.CENTER).ShowModal() else: if len(worksheets) > 1: sel_worksheet = show_sheets(self, worksheets) else: sel_worksheet = worksheets[0] bag = {'data': None, 'filepath': ''} def read_excel(): # TODO: need to handle an error df = pandas.read_excel(filepath, sheet_name=sel_worksheet) bag['data'] = df bag['filepath'] = filepath wx.CallAfter(dispatcher.send, CLOSE_DIALOG_SIGNAL, rc=0) thread = Thread(target=read_excel) thread.start() with NotificationBox(self, caption='Import Data', message='Reading from the Excel file...') as md: md.ShowModal() return bag['data'], bag['filepath'] else: wx.MessageDialog(self, 'File path is not valid!', 'Please check the file path.', wx.OK | wx.CENTER).ShowModal() return pandas.DataFrame(), '' def onLoadMLABItemClick(self, e): if self.data_loaded: dlg = wx.MessageDialog(None, "Click \"Yes\" to continue or click \"No\" to return to your session.", "Data in this current session will be discarded!", wx.YES_NO | wx.ICON_QUESTION) ret_ = dlg.ShowModal() if ret_ == wx.ID_NO: return self.profile_filepath = None self.db_filepath = None self.dbengine = None self.dbfile_lbl.SetLabelText('Database filepath:') self.profile_lbl.SetLabelText('Profile filepath:') df, filepath = self.load_datafile() if filepath: if df.empty: dlg = wx.MessageDialog(None, "Do you want to proceed?\nClick \"Yes\" to continue or \"No\" to cancel.", "Warning: dataset is empty.", wx.YES_NO | wx.ICON_QUESTION) ret_ = dlg.ShowModal() if ret_ == wx.ID_NO: return self.data_filepath = filepath self.datafile_lbl.SetLabelText("Data filepath: {}".format(self.data_filepath)) self.data_loaded = True self.data_grid_box_sizer.Remove(0) self.data_grid.Destroy() self.data_grid = DataGrid(self.preview_panel) self.data_grid.set_table(df) self.data_grid.AutoSizeColumns() self.data_grid_box_sizer.Add(self.data_grid, 1, flag=wx.EXPAND | wx.ALL) self.data_grid_box_sizer.Layout() # repaint the sizer self.field_attr.update_from_dataframe(df) self.field_attr_list.ClearAll() self.refresh_field_attr_list_column() if self.field_attr.columns: self.current_column = self.field_attr.iget_column(0) self.field_attr_list.Select(0) self.saveProfileItem.Enable(True) self.loadProfileItem.Enable(True) self.organismItem.Enable(True) self.loadProfileItem.Enable(True) self.createFieldItem.Enable(True) self.appendToDatabaseMenuItem.Enable(True) self.saveToDatabaseMenuItem.Enable(True) # need to enable load profile menu item here # after refactoring the menu bar def OnLoadCSV(self, e): filepath = browse('CSV') if filepath: try: df =
pandas.read_csv(filepath)
pandas.read_csv
import numpy as np from pandas import DataFrame, Series import pandas as pd from utilities import (LICENSE_KEY, generate_token, master_player_lookup, YAHOO_FILE, YAHOO_KEY, YAHOO_SECRET) import json from yahoo_oauth import OAuth2 from pathlib import Path # store credentials if don't already exist if not Path(YAHOO_FILE).exists(): yahoo_credentials_dict = { 'consumer_key': YAHOO_KEY, 'consumer_secret': YAHOO_SECRET, } with open(YAHOO_FILE) as f: json.dump(yahoo_credentials_dict, f) OAUTH = OAuth2(None, None, from_file=YAHOO_FILE) game_url = 'https://fantasysports.yahooapis.com/fantasy/v2/game/nfl' OAUTH.session.get(game_url, params={'format': 'json'}).json() ############################################################################### # roster data ############################################################################### LEAGUE_ID = 43886 TEAM_ID = 11 WEEK = 1 roster_url = ('https://fantasysports.yahooapis.com/fantasy/v2' + f'/team/406.l.{LEAGUE_ID}.t.{TEAM_ID}/roster;week={WEEK}') # gets current data # should run/look at, but we're overwriting with saved data next line roster_json = OAUTH.session.get(roster_url, params={'format': 'json'}).json() with open('./projects/integration/raw/yahoo/roster.json') as f: roster_json = json.load(f) # open up in browser and look at it players_dict = ( roster_json['fantasy_content']['team'][1]['roster']['0']['players']) players_dict.keys() player0 = players_dict['0'] # <NAME> def player_list_to_dict(player): player_info = player['player'][0] player_info_dict = {} for x in player_info: if (type(x) is dict) and (len(x.keys()) == 1): for key in x.keys(): # tricky way to get access to key player_info_dict[key] = x[key] return player_info_dict player_list_to_dict(player0) def process_player(player): player_info = player_list_to_dict(player) pos_info = player['player'][1]['selected_position'][1] dict_to_return = {} dict_to_return['yahoo_id'] = int(player_info['player_id']) dict_to_return['name'] = player_info['name']['full'] dict_to_return['player_position'] = player_info['primary_position'] dict_to_return['team_position'] = pos_info['position'] return dict_to_return process_player(player0) [process_player(player) for key, player in players_dict.items() if key != 'count'] players_df = DataFrame( [process_player(player) for key, player in players_dict.items() if key != 'count' ]) players_df wrs = players_df.query("team_position == 'WR'") wrs suffix = Series(range(1, len(wrs) + 1), index=wrs.index) suffix wrs['team_position'] + suffix.astype(str) def add_pos_suffix(df_subset): if len(df_subset) > 1: suffix = Series(range(1, len(df_subset) + 1), index=df_subset.index) df_subset['team_position'] = ( df_subset['team_position'] + suffix.astype(str)) return df_subset players_df2 = pd.concat([ add_pos_suffix(players_df.query(f"team_position == '{x}'")) for x in players_df['team_position'].unique()]) players_df2 players_df2['start'] = ~(players_df2['team_position'].str.startswith('BN') | players_df2['team_position'].str.startswith('IR')) players_df2 def process_players(players): players_raw = DataFrame( [process_player(player) for key, player in players.items() if key != 'count' ]) players_df = pd.concat([ add_pos_suffix(players_raw.query(f"team_position == '{x}'")) for x in players_raw['team_position'].unique()]) players_df['start'] = ~(players_df['team_position'].str.startswith('BN') | players_df['team_position'].str.startswith('IR')) return players_df process_players(players_dict) # players_dict = roster_json['fantasy_content']['team'][1]['roster']['0']['players'] team_id = roster_json['fantasy_content']['team'][0][1]['team_id'] players_df2['team_id'] = team_id def process_roster(team): players_df = process_players(team[1]['roster']['0']['players']) team_id = team[0][1]['team_id'] players_df['team_id'] = team_id return players_df roster_df = process_roster(roster_json['fantasy_content']['team']) points_url = ('https://fantasysports.yahooapis.com/fantasy/v2/' + f'team/406.l.{LEAGUE_ID}.t.{TEAM_ID}' + "/players;out=metadata,stats,ownership,percent_owned,draft_analysis") # gets current data # should run/look at, but we're overwriting with saved data next line points_json = OAUTH.session.get(points_url, params={'format': 'json'}).json() # saved data with open('./projects/integration/raw/yahoo/points.json') as f: points_json = json.load(f) player_dict = points_json['fantasy_content']['team'][1]['players'] gronk = player_dict['1'] def process_player_stats(player): dict_to_return = {} dict_to_return['yahoo_id'] = int(player['player'][0][1]['player_id']) dict_to_return['actual'] = float( player['player'][1]['player_points']['total']) return dict_to_return process_player_stats(gronk) def process_team_stats(team): stats = DataFrame([process_player_stats(player) for key, player in team.items() if key != 'count']) stats.loc[stats['actual'] == 0, 'actual'] = np.nan return stats stats = process_team_stats(player_dict) roster_df_w_stats = pd.merge(roster_df, stats) roster_df_w_stats.head(10) roster_df_w_stats['name'].str.lower().str.replace(' ','-').head() from utilities import (LICENSE_KEY, generate_token, master_player_lookup) token = generate_token(LICENSE_KEY)['token'] fantasymath_players = master_player_lookup(token) fantasymath_players =
pd.read_csv('./projects/integration/raw/lookup.csv')
pandas.read_csv
import spacy import pandas as pd import glob import re import numpy as np import os ######## ######## ######## ######## ######## NLP INFORMATION EXTRACTION MODULE ######## ######## ######## ######## ######## ## lots borrowed from git jists in ## https://www.analyticsvidhya.com/blog/2020/06/nlp-project-information-extraction/ pd.set_option('display.max_rows', None) pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pandas.set_option
from statsmodels.compat.numpy import lstsq from statsmodels.compat.pandas import assert_index_equal from statsmodels.compat.platform import PLATFORM_WIN from statsmodels.compat.python import lrange import os import warnings import numpy as np from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_equal, assert_raises, ) import pandas as pd from pandas import DataFrame, Series, date_range import pytest from scipy.interpolate import interp1d from statsmodels.datasets import macrodata, modechoice, nile, randhie, sunspots from statsmodels.tools.sm_exceptions import ( CollinearityWarning, InfeasibleTestError, InterpolationWarning, MissingDataError, ) # Remove imports when range unit root test gets an R implementation from statsmodels.tools.validation import array_like, bool_like from statsmodels.tsa.arima_process import arma_acovf from statsmodels.tsa.statespace.sarimax import SARIMAX from statsmodels.tsa.stattools import ( acf, acovf, adfuller, arma_order_select_ic, breakvar_heteroskedasticity_test, ccovf, coint, grangercausalitytests, innovations_algo, innovations_filter, kpss, levinson_durbin, levinson_durbin_pacf, pacf, pacf_burg, pacf_ols, pacf_yw, range_unit_root_test, zivot_andrews, ) DECIMAL_8 = 8 DECIMAL_6 = 6 DECIMAL_5 = 5 DECIMAL_4 = 4 DECIMAL_3 = 3 DECIMAL_2 = 2 DECIMAL_1 = 1 CURR_DIR = os.path.dirname(os.path.abspath(__file__)) @pytest.fixture(scope="module") def acovf_data(): rnd = np.random.RandomState(12345) return rnd.randn(250) class CheckADF(object): """ Test Augmented Dickey-Fuller Test values taken from Stata. """ levels = ["1%", "5%", "10%"] data = macrodata.load_pandas() x = data.data["realgdp"].values y = data.data["infl"].values def test_teststat(self): assert_almost_equal(self.res1[0], self.teststat, DECIMAL_5) def test_pvalue(self): assert_almost_equal(self.res1[1], self.pvalue, DECIMAL_5) def test_critvalues(self): critvalues = [self.res1[4][lev] for lev in self.levels] assert_almost_equal(critvalues, self.critvalues, DECIMAL_2) class TestADFConstant(CheckADF): """ Dickey-Fuller test for unit root """ @classmethod def setup_class(cls): cls.res1 = adfuller(cls.x, regression="c", autolag=None, maxlag=4) cls.teststat = 0.97505319 cls.pvalue = 0.99399563 cls.critvalues = [-3.476, -2.883, -2.573] class TestADFConstantTrend(CheckADF): """""" @classmethod def setup_class(cls): cls.res1 = adfuller(cls.x, regression="ct", autolag=None, maxlag=4) cls.teststat = -1.8566374 cls.pvalue = 0.67682968 cls.critvalues = [-4.007, -3.437, -3.137] # FIXME: do not leave commented-out # class TestADFConstantTrendSquared(CheckADF): # """ # """ # pass # TODO: get test values from R? class TestADFNoConstant(CheckADF): """""" @classmethod def setup_class(cls): with pytest.warns(FutureWarning): adfuller(cls.x, regression="nc", autolag=None, maxlag=4) cls.res1 = adfuller(cls.x, regression="n", autolag=None, maxlag=4) cls.teststat = 3.5227498 cls.pvalue = 0.99999 # Stata does not return a p-value for noconstant. # Tau^max in MacKinnon (1994) is missing, so it is # assumed that its right-tail is well-behaved cls.critvalues = [-2.587, -1.950, -1.617] # No Unit Root class TestADFConstant2(CheckADF): @classmethod def setup_class(cls): cls.res1 = adfuller(cls.y, regression="c", autolag=None, maxlag=1) cls.teststat = -4.3346988 cls.pvalue = 0.00038661 cls.critvalues = [-3.476, -2.883, -2.573] class TestADFConstantTrend2(CheckADF): @classmethod def setup_class(cls): cls.res1 = adfuller(cls.y, regression="ct", autolag=None, maxlag=1) cls.teststat = -4.425093 cls.pvalue = 0.00199633 cls.critvalues = [-4.006, -3.437, -3.137] class TestADFNoConstant2(CheckADF): @classmethod def setup_class(cls): cls.res1 = adfuller(cls.y, regression="n", autolag=None, maxlag=1) cls.teststat = -2.4511596 cls.pvalue = 0.013747 # Stata does not return a p-value for noconstant # this value is just taken from our results cls.critvalues = [-2.587, -1.950, -1.617] _, _1, _2, cls.store = adfuller( cls.y, regression="n", autolag=None, maxlag=1, store=True ) def test_store_str(self): assert_equal( self.store.__str__(), "Augmented Dickey-Fuller Test Results" ) class CheckCorrGram(object): """ Set up for ACF, PACF tests. """ data = macrodata.load_pandas() x = data.data["realgdp"] filename = os.path.join(CURR_DIR, "results", "results_corrgram.csv") results = pd.read_csv(filename, delimiter=",") class TestACF(CheckCorrGram): """ Test Autocorrelation Function """ @classmethod def setup_class(cls): cls.acf = cls.results["acvar"] # cls.acf = np.concatenate(([1.], cls.acf)) cls.qstat = cls.results["Q1"] cls.res1 = acf(cls.x, nlags=40, qstat=True, alpha=0.05, fft=False) cls.confint_res = cls.results[["acvar_lb", "acvar_ub"]].values def test_acf(self): assert_almost_equal(self.res1[0][1:41], self.acf, DECIMAL_8) def test_confint(self): centered = self.res1[1] - self.res1[1].mean(1)[:, None] assert_almost_equal(centered[1:41], self.confint_res, DECIMAL_8) def test_qstat(self): assert_almost_equal(self.res1[2][:40], self.qstat, DECIMAL_3) # 3 decimal places because of stata rounding # FIXME: enable/xfail/skip or delete # def pvalue(self): # pass # NOTE: should not need testing if Q stat is correct class TestACF_FFT(CheckCorrGram): # Test Autocorrelation Function using FFT @classmethod def setup_class(cls): cls.acf = cls.results["acvarfft"] cls.qstat = cls.results["Q1"] cls.res1 = acf(cls.x, nlags=40, qstat=True, fft=True) def test_acf(self): assert_almost_equal(self.res1[0][1:], self.acf, DECIMAL_8) def test_qstat(self): # todo why is res1/qstat 1 short assert_almost_equal(self.res1[1], self.qstat, DECIMAL_3) class TestACFMissing(CheckCorrGram): # Test Autocorrelation Function using Missing @classmethod def setup_class(cls): cls.x = np.concatenate((np.array([np.nan]), cls.x)) cls.acf = cls.results["acvar"] # drop and conservative cls.qstat = cls.results["Q1"] cls.res_drop = acf( cls.x, nlags=40, qstat=True, alpha=0.05, missing="drop", fft=False ) cls.res_conservative = acf( cls.x, nlags=40, qstat=True, alpha=0.05, fft=False, missing="conservative", ) cls.acf_none = np.empty(40) * np.nan # lags 1 to 40 inclusive cls.qstat_none = np.empty(40) * np.nan cls.res_none = acf( cls.x, nlags=40, qstat=True, alpha=0.05, missing="none", fft=False ) def test_raise(self): with pytest.raises(MissingDataError): acf( self.x, nlags=40, qstat=True, fft=False, alpha=0.05, missing="raise", ) def test_acf_none(self): assert_almost_equal(self.res_none[0][1:41], self.acf_none, DECIMAL_8) def test_acf_drop(self): assert_almost_equal(self.res_drop[0][1:41], self.acf, DECIMAL_8) def test_acf_conservative(self): assert_almost_equal( self.res_conservative[0][1:41], self.acf, DECIMAL_8 ) def test_qstat_none(self): # todo why is res1/qstat 1 short assert_almost_equal(self.res_none[2], self.qstat_none, DECIMAL_3) # FIXME: enable/xfail/skip or delete # how to do this test? the correct q_stat depends on whether nobs=len(x) is # used when x contains NaNs or whether nobs<len(x) when x contains NaNs # def test_qstat_drop(self): # assert_almost_equal(self.res_drop[2][:40], self.qstat, DECIMAL_3) class TestPACF(CheckCorrGram): @classmethod def setup_class(cls): cls.pacfols = cls.results["PACOLS"] cls.pacfyw = cls.results["PACYW"] def test_ols(self): pacfols, confint = pacf(self.x, nlags=40, alpha=0.05, method="ols") assert_almost_equal(pacfols[1:], self.pacfols, DECIMAL_6) centered = confint - confint.mean(1)[:, None] # from edited Stata ado file res = [[-0.1375625, 0.1375625]] * 40 assert_almost_equal(centered[1:41], res, DECIMAL_6) # check lag 0 assert_equal(centered[0], [0.0, 0.0]) assert_equal(confint[0], [1, 1]) assert_equal(pacfols[0], 1) def test_ols_inefficient(self): lag_len = 5 pacfols = pacf_ols(self.x, nlags=lag_len, efficient=False) x = self.x.copy() x -= x.mean() n = x.shape[0] lags = np.zeros((n - 5, 5)) lead = x[5:] direct = np.empty(lag_len + 1) direct[0] = 1.0 for i in range(lag_len): lags[:, i] = x[5 - (i + 1) : -(i + 1)] direct[i + 1] = lstsq(lags[:, : (i + 1)], lead, rcond=None)[0][-1] assert_allclose(pacfols, direct, atol=1e-8) def test_yw(self): pacfyw = pacf_yw(self.x, nlags=40, method="mle") assert_almost_equal(pacfyw[1:], self.pacfyw, DECIMAL_8) def test_ld(self): pacfyw = pacf_yw(self.x, nlags=40, method="mle") pacfld = pacf(self.x, nlags=40, method="ldb") assert_almost_equal(pacfyw, pacfld, DECIMAL_8) pacfyw = pacf(self.x, nlags=40, method="yw") pacfld = pacf(self.x, nlags=40, method="lda") assert_almost_equal(pacfyw, pacfld, DECIMAL_8) class TestBreakvarHeteroskedasticityTest(object): from scipy.stats import chi2, f def test_1d_input(self): input_residuals = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0] expected_statistic = (4.0 ** 2 + 5.0 ** 2) / (0.0 ** 2 + 1.0 ** 2) # ~ F(2, 2), two-sided test expected_pvalue = 2 * min( self.f.cdf(expected_statistic, 2, 2), self.f.sf(expected_statistic, 2, 2), ) actual_statistic, actual_pvalue = breakvar_heteroskedasticity_test( input_residuals ) assert actual_statistic == expected_statistic assert actual_pvalue == expected_pvalue def test_2d_input_with_missing_values(self): input_residuals = np.array( [ [0.0, 0.0, np.nan], [1.0, np.nan, 1.0], [2.0, 2.0, np.nan], [3.0, 3.0, 3.0], [4.0, 4.0, 4.0], [5.0, 5.0, 5.0], [6.0, 6.0, 6.0], [7.0, 7.0, 7.0], [8.0, 8.0, 8.0], ] ) expected_statistic = np.array( [ (8.0 ** 2 + 7.0 ** 2 + 6.0 ** 2) / (0.0 ** 2 + 1.0 ** 2 + 2.0 ** 2), (8.0 ** 2 + 7.0 ** 2 + 6.0 ** 2) / (0.0 ** 2 + 2.0 ** 2), np.nan, ] ) expected_pvalue = np.array( [ 2 * min( self.f.cdf(expected_statistic[0], 3, 3), self.f.sf(expected_statistic[0], 3, 3), ), 2 * min( self.f.cdf(expected_statistic[1], 3, 2), self.f.sf(expected_statistic[1], 3, 2), ), np.nan, ] ) actual_statistic, actual_pvalue = breakvar_heteroskedasticity_test( input_residuals ) assert_equal(actual_statistic, expected_statistic) assert_equal(actual_pvalue, expected_pvalue) @pytest.mark.parametrize( "subset_length,expected_statistic,expected_pvalue", [ (2, 41, 2 * min(f.cdf(41, 2, 2), f.sf(41, 2, 2))), (0.5, 10, 2 * min(f.cdf(10, 3, 3), f.sf(10, 3, 3))), ], ) def test_subset_length( self, subset_length, expected_statistic, expected_pvalue ): input_residuals = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0] actual_statistic, actual_pvalue = breakvar_heteroskedasticity_test( input_residuals, subset_length=subset_length, ) assert actual_statistic == expected_statistic assert actual_pvalue == expected_pvalue @pytest.mark.parametrize( "alternative,expected_statistic,expected_pvalue", [ ("two-sided", 41, 2 * min(f.cdf(41, 2, 2), f.sf(41, 2, 2))), ("decreasing", 1 / 41, f.sf(1 / 41, 2, 2)), ("increasing", 41, f.sf(41, 2, 2)), ], ) def test_alternative( self, alternative, expected_statistic, expected_pvalue ): input_residuals = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0] actual_statistic, actual_pvalue = breakvar_heteroskedasticity_test( input_residuals, alternative=alternative, ) assert actual_statistic == expected_statistic assert actual_pvalue == expected_pvalue def test_use_chi2(self): input_residuals = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0] expected_statistic = (4.0 ** 2 + 5.0 ** 2) / (0.0 ** 2 + 1.0 ** 2) expected_pvalue = 2 * min( self.chi2.cdf(2 * expected_statistic, 2), self.chi2.sf(2 * expected_statistic, 2), ) actual_statistic, actual_pvalue = breakvar_heteroskedasticity_test( input_residuals, use_f=False, ) assert actual_statistic == expected_statistic assert actual_pvalue == expected_pvalue class CheckCoint(object): """ Test Cointegration Test Results for 2-variable system Test values taken from Stata """ levels = ["1%", "5%", "10%"] data = macrodata.load_pandas() y1 = data.data["realcons"].values y2 = data.data["realgdp"].values def test_tstat(self): assert_almost_equal(self.coint_t, self.teststat, DECIMAL_4) # this does not produce the old results anymore class TestCoint_t(CheckCoint): """ Get AR(1) parameter on residuals """ @classmethod def setup_class(cls): # cls.coint_t = coint(cls.y1, cls.y2, trend="c")[0] cls.coint_t = coint(cls.y1, cls.y2, trend="c", maxlag=0, autolag=None)[ 0 ] cls.teststat = -1.8208817 cls.teststat = -1.830170986148 def test_coint(): nobs = 200 scale_e = 1 const = [1, 0, 0.5, 0] np.random.seed(123) unit = np.random.randn(nobs).cumsum() y = scale_e * np.random.randn(nobs, 4) y[:, :2] += unit[:, None] y += const y = np.round(y, 4) # FIXME: enable/xfail/skip or delete for trend in []: # ['c', 'ct', 'ctt', 'n']: print("\n", trend) print(coint(y[:, 0], y[:, 1], trend=trend, maxlag=4, autolag=None)) print(coint(y[:, 0], y[:, 1:3], trend=trend, maxlag=4, autolag=None)) print(coint(y[:, 0], y[:, 2:], trend=trend, maxlag=4, autolag=None)) print(coint(y[:, 0], y[:, 1:], trend=trend, maxlag=4, autolag=None)) # results from Stata egranger res_egranger = {} # trend = 'ct' res = res_egranger["ct"] = {} res[0] = [ -5.615251442239, -4.406102369132, -3.82866685109, -3.532082997903, ] res[1] = [ -5.63591313706, -4.758609717199, -4.179130554708, -3.880909696863, ] res[2] = [ -2.892029275027, -4.758609717199, -4.179130554708, -3.880909696863, ] res[3] = [-5.626932544079, -5.08363327039, -4.502469783057, -4.2031051091] # trend = 'c' res = res_egranger["c"] = {} # first critical value res[0][1] has a discrepancy starting at 4th decimal res[0] = [ -5.760696844656, -3.952043522638, -3.367006313729, -3.065831247948, ] # manually adjusted to have higher precision as in other cases res[0][1] = -3.952321293401682 res[1] = [ -5.781087068772, -4.367111915942, -3.783961136005, -3.483501524709, ] res[2] = [ -2.477444137366, -4.367111915942, -3.783961136005, -3.483501524709, ] res[3] = [ -5.778205811661, -4.735249216434, -4.152738973763, -3.852480848968, ] # trend = 'ctt' res = res_egranger["ctt"] = {} res[0] = [ -5.644431269946, -4.796038299708, -4.221469431008, -3.926472577178, ] res[1] = [-5.665691609506, -5.111158174219, -4.53317278104, -4.23601008516] res[2] = [-3.161462374828, -5.111158174219, -4.53317278104, -4.23601008516] res[3] = [ -5.657904558563, -5.406880189412, -4.826111619543, -4.527090164875, ] # The following for 'n' are only regression test numbers # trend = 'n' not allowed in egranger # trend = 'n' res = res_egranger["n"] = {} nan = np.nan # shortcut for table res[0] = [-3.7146175989071137, nan, nan, nan] res[1] = [-3.8199323012888384, nan, nan, nan] res[2] = [-1.6865000791270679, nan, nan, nan] res[3] = [-3.7991270451873675, nan, nan, nan] with pytest.warns(FutureWarning): # Ensure warning raised for nc rather than n coint(y[:, 0], y[:, 1], trend="nc", maxlag=4, autolag=None) for trend in ["c", "ct", "ctt", "n"]: res1 = {} res1[0] = coint(y[:, 0], y[:, 1], trend=trend, maxlag=4, autolag=None) res1[1] = coint( y[:, 0], y[:, 1:3], trend=trend, maxlag=4, autolag=None ) res1[2] = coint(y[:, 0], y[:, 2:], trend=trend, maxlag=4, autolag=None) res1[3] = coint(y[:, 0], y[:, 1:], trend=trend, maxlag=4, autolag=None) for i in range(4): res = res_egranger[trend] assert_allclose(res1[i][0], res[i][0], rtol=1e-11) r2 = res[i][1:] r1 = res1[i][2] assert_allclose(r1, r2, rtol=0, atol=6e-7) # use default autolag #4490 res1_0 = coint(y[:, 0], y[:, 1], trend="ct", maxlag=4) assert_allclose(res1_0[2], res_egranger["ct"][0][1:], rtol=0, atol=6e-7) # the following is just a regression test assert_allclose( res1_0[:2], [-13.992946638547112, 2.270898990540678e-27], rtol=1e-10, atol=1e-27, ) def test_coint_identical_series(): nobs = 200 scale_e = 1 np.random.seed(123) y = scale_e * np.random.randn(nobs) warnings.simplefilter("always", CollinearityWarning) with pytest.warns(CollinearityWarning): c = coint(y, y, trend="c", maxlag=0, autolag=None) assert_equal(c[1], 0.0) assert_(np.isneginf(c[0])) def test_coint_perfect_collinearity(): # test uses nearly perfect collinearity nobs = 200 scale_e = 1 np.random.seed(123) x = scale_e * np.random.randn(nobs, 2) y = 1 + x.sum(axis=1) + 1e-7 * np.random.randn(nobs) warnings.simplefilter("always", CollinearityWarning) with warnings.catch_warnings(record=True) as w: c = coint(y, x, trend="c", maxlag=0, autolag=None) assert_equal(c[1], 0.0) assert_(np.isneginf(c[0])) class TestGrangerCausality(object): def test_grangercausality(self): # some example data mdata = macrodata.load_pandas().data mdata = mdata[["realgdp", "realcons"]].values data = mdata.astype(float) data = np.diff(np.log(data), axis=0) # R: lmtest:grangertest r_result = [0.243097, 0.7844328, 195, 2] # f_test gr = grangercausalitytests(data[:, 1::-1], 2, verbose=False) assert_almost_equal(r_result, gr[2][0]["ssr_ftest"], decimal=7) assert_almost_equal( gr[2][0]["params_ftest"], gr[2][0]["ssr_ftest"], decimal=7 ) def test_grangercausality_single(self): mdata = macrodata.load_pandas().data mdata = mdata[["realgdp", "realcons"]].values data = mdata.astype(float) data = np.diff(np.log(data), axis=0) gr = grangercausalitytests(data[:, 1::-1], 2, verbose=False) gr2 = grangercausalitytests(data[:, 1::-1], [2], verbose=False) assert 1 in gr assert 1 not in gr2 assert_almost_equal( gr[2][0]["ssr_ftest"], gr2[2][0]["ssr_ftest"], decimal=7 ) assert_almost_equal( gr[2][0]["params_ftest"], gr2[2][0]["ssr_ftest"], decimal=7 ) def test_granger_fails_on_nobs_check(self, reset_randomstate): # Test that if maxlag is too large, Granger Test raises a clear error. x = np.random.rand(10, 2) grangercausalitytests(x, 2, verbose=False) # This should pass. with pytest.raises(ValueError): grangercausalitytests(x, 3, verbose=False) def test_granger_fails_on_finite_check(self, reset_randomstate): x = np.random.rand(1000, 2) x[500, 0] = np.nan x[750, 1] = np.inf with pytest.raises(ValueError, match="x contains NaN"): grangercausalitytests(x, 2) def test_granger_fails_on_zero_lag(self, reset_randomstate): x = np.random.rand(1000, 2) with pytest.raises( ValueError, match="maxlag must be a non-empty list containing only positive integers", ): grangercausalitytests(x, [0, 1, 2]) class TestKPSS: """ R-code ------ library(tseries) kpss.stat(x, "Level") kpss.stat(x, "Trend") In this context, x is the vector containing the macrodata['realgdp'] series. """ @classmethod def setup(cls): cls.data = macrodata.load_pandas() cls.x = cls.data.data["realgdp"].values def test_fail_nonvector_input(self, reset_randomstate): # should be fine with pytest.warns(InterpolationWarning): kpss(self.x, nlags="legacy") x = np.random.rand(20, 2) assert_raises(ValueError, kpss, x) def test_fail_unclear_hypothesis(self): # these should be fine, with pytest.warns(InterpolationWarning): kpss(self.x, "c", nlags="legacy") with pytest.warns(InterpolationWarning): kpss(self.x, "C", nlags="legacy") with pytest.warns(InterpolationWarning): kpss(self.x, "ct", nlags="legacy") with pytest.warns(InterpolationWarning): kpss(self.x, "CT", nlags="legacy") assert_raises( ValueError, kpss, self.x, "unclear hypothesis", nlags="legacy" ) def test_teststat(self): with pytest.warns(InterpolationWarning): kpss_stat, _, _, _ = kpss(self.x, "c", 3) assert_almost_equal(kpss_stat, 5.0169, DECIMAL_3) with pytest.warns(InterpolationWarning): kpss_stat, _, _, _ = kpss(self.x, "ct", 3) assert_almost_equal(kpss_stat, 1.1828, DECIMAL_3) def test_pval(self): with pytest.warns(InterpolationWarning): _, pval, _, _ = kpss(self.x, "c", 3) assert_equal(pval, 0.01) with pytest.warns(InterpolationWarning): _, pval, _, _ = kpss(self.x, "ct", 3) assert_equal(pval, 0.01) def test_store(self): with pytest.warns(InterpolationWarning): _, _, _, store = kpss(self.x, "c", 3, True) # assert attributes, and make sure they're correct assert_equal(store.nobs, len(self.x)) assert_equal(store.lags, 3) # test autolag function _kpss_autolag against SAS 9.3 def test_lags(self): # real GDP from macrodata data set with pytest.warns(InterpolationWarning): res = kpss(self.x, "c", nlags="auto") assert_equal(res[2], 9) # real interest rates from macrodata data set res = kpss(sunspots.load().data["SUNACTIVITY"], "c", nlags="auto") assert_equal(res[2], 7) # volumes from nile data set with pytest.warns(InterpolationWarning): res = kpss(nile.load().data["volume"], "c", nlags="auto") assert_equal(res[2], 5) # log-coinsurance from randhie data set with pytest.warns(InterpolationWarning): res = kpss(randhie.load().data["lncoins"], "ct", nlags="auto") assert_equal(res[2], 75) # in-vehicle time from modechoice data set with pytest.warns(InterpolationWarning): res = kpss(modechoice.load().data["invt"], "ct", nlags="auto") assert_equal(res[2], 18) def test_kpss_fails_on_nobs_check(self): # Test that if lags exceeds number of observations KPSS raises a # clear error # GH5925 nobs = len(self.x) msg = r"lags \({}\) must be < number of observations \({}\)".format( nobs, nobs ) with pytest.raises(ValueError, match=msg): kpss(self.x, "c", nlags=nobs) def test_kpss_autolags_does_not_assign_lags_equal_to_nobs(self): # Test that if *autolags* exceeds number of observations, we set # suitable lags # GH5925 base = np.array([0, 0, 0, 0, 0, 1, 1.0]) data_which_breaks_autolag = np.r_[np.tile(base, 297 // 7), [0, 0, 0]] kpss(data_which_breaks_autolag, nlags="auto") def test_legacy_lags(self): # Test legacy lags are the same with pytest.warns(InterpolationWarning): res = kpss(self.x, "c", nlags="legacy") assert_equal(res[2], 15) def test_unknown_lags(self): # Test legacy lags are the same with pytest.raises(ValueError): kpss(self.x, "c", nlags="unknown") def test_none(self): with pytest.warns(FutureWarning): kpss(self.x, nlags=None) class TestRUR: """ Simple implementation ------ Since an R implementation of the test cannot be found, the method is tested against a simple implementation using a for loop. In this context, x is the vector containing the macrodata['realgdp'] series. """ @classmethod def setup(cls): cls.data = macrodata.load_pandas() cls.x = cls.data.data["realgdp"].values # To be removed when range unit test gets an R implementation def simple_rur(self, x, store=False): x = array_like(x, "x") store = bool_like(store, "store") nobs = x.shape[0] # if m is not one, n != m * n if nobs != x.size: raise ValueError("x of shape {0} not understood".format(x.shape)) # Table from [1] has been replicated using 200,000 samples # Critical values for new n_obs values have been identified pvals = [0.01, 0.025, 0.05, 0.10, 0.90, 0.95] n = np.array( [25, 50, 100, 150, 200, 250, 500, 1000, 2000, 3000, 4000, 5000] ) crit = np.array( [ [0.6626, 0.8126, 0.9192, 1.0712, 2.4863, 2.7312], [0.7977, 0.9274, 1.0478, 1.1964, 2.6821, 2.9613], [0.907, 1.0243, 1.1412, 1.2888, 2.8317, 3.1393], [0.9543, 1.0768, 1.1869, 1.3294, 2.8915, 3.2049], [0.9833, 1.0984, 1.2101, 1.3494, 2.9308, 3.2482], [0.9982, 1.1137, 1.2242, 1.3632, 2.9571, 3.2482], [1.0494, 1.1643, 1.2712, 1.4076, 3.0207, 3.3584], [1.0846, 1.1959, 1.2988, 1.4344, 3.0653, 3.4073], [1.1121, 1.2200, 1.3230, 1.4556, 3.0948, 3.4439], [1.1204, 1.2295, 1.3318, 1.4656, 3.1054, 3.4632], [1.1309, 1.2347, 1.3318, 1.4693, 3.1165, 3.4717], [1.1377, 1.2402, 1.3408, 1.4729, 3.1252, 3.4807], ] ) # Interpolation for nobs inter_crit = np.zeros((1, crit.shape[1])) for i in range(crit.shape[1]): f = interp1d(n, crit[:, i]) inter_crit[0, i] = f(nobs) # Calculate RUR stat count = 0 max_p = x[0] min_p = x[0] for v in x[1:]: if v > max_p: max_p = v count = count + 1 if v < min_p: min_p = v count = count + 1 rur_stat = count / np.sqrt(len(x)) k = len(pvals) - 1 for i in range(len(pvals) - 1, -1, -1): if rur_stat < inter_crit[0, i]: k = i else: break p_value = pvals[k] warn_msg = """\ The test statistic is outside of the range of p-values available in the look-up table. The actual p-value is {direction} than the p-value returned. """ direction = "" if p_value == pvals[-1]: direction = "smaller" elif p_value == pvals[0]: direction = "larger" if direction: warnings.warn( warn_msg.format(direction=direction), InterpolationWarning ) crit_dict = { "10%": inter_crit[0, 3], "5%": inter_crit[0, 2], "2.5%": inter_crit[0, 1], "1%": inter_crit[0, 0], } if store: from statsmodels.stats.diagnostic import ResultsStore rstore = ResultsStore() rstore.nobs = nobs rstore.H0 = "The series is not stationary" rstore.HA = "The series is stationary" return rur_stat, p_value, crit_dict, rstore else: return rur_stat, p_value, crit_dict def test_fail_nonvector_input(self, reset_randomstate): with pytest.warns(InterpolationWarning): range_unit_root_test(self.x) x = np.random.rand(20, 2) assert_raises(ValueError, range_unit_root_test, x) def test_teststat(self): with pytest.warns(InterpolationWarning): rur_stat, _, _ = range_unit_root_test(self.x) simple_rur_stat, _, _ = self.simple_rur(self.x) assert_almost_equal(rur_stat, simple_rur_stat, DECIMAL_3) def test_pval(self): with pytest.warns(InterpolationWarning): _, pval, _ = range_unit_root_test(self.x) _, simple_pval, _ = self.simple_rur(self.x) assert_equal(pval, simple_pval) def test_store(self): with pytest.warns(InterpolationWarning): _, _, _, store = range_unit_root_test(self.x, True) # assert attributes, and make sure they're correct assert_equal(store.nobs, len(self.x)) def test_pandasacovf(): s = Series(lrange(1, 11)) assert_almost_equal(acovf(s, fft=False), acovf(s.values, fft=False)) def test_acovf2d(reset_randomstate): dta = sunspots.load_pandas().data dta.index = date_range(start="1700", end="2009", freq="A")[:309] del dta["YEAR"] res = acovf(dta, fft=False) assert_equal(res, acovf(dta.values, fft=False)) x = np.random.random((10, 2)) with pytest.raises(ValueError): acovf(x, fft=False) @pytest.mark.parametrize("demean", [True, False]) @pytest.mark.parametrize("adjusted", [True, False]) def test_acovf_fft_vs_convolution(demean, adjusted, reset_randomstate): q = np.random.normal(size=100) F1 = acovf(q, demean=demean, adjusted=adjusted, fft=True) F2 = acovf(q, demean=demean, adjusted=adjusted, fft=False) assert_almost_equal(F1, F2, decimal=7) @pytest.mark.parametrize("demean", [True, False]) @pytest.mark.parametrize("adjusted", [True, False]) def test_ccovf_fft_vs_convolution(demean, adjusted, reset_randomstate): x = np.random.normal(size=128) y = np.random.normal(size=128) F1 = ccovf(x, y, demean=demean, adjusted=adjusted, fft=False) F2 = ccovf(x, y, demean=demean, adjusted=adjusted, fft=True) assert_almost_equal(F1, F2, decimal=7) @pytest.mark.parametrize("demean", [True, False]) @pytest.mark.parametrize("adjusted", [True, False]) @pytest.mark.parametrize("fft", [True, False]) def test_compare_acovf_vs_ccovf(demean, adjusted, fft, reset_randomstate): x = np.random.normal(size=128) F1 = acovf(x, demean=demean, adjusted=adjusted, fft=fft) F2 = ccovf(x, x, demean=demean, adjusted=adjusted, fft=fft) assert_almost_equal(F1, F2, decimal=7) @pytest.mark.smoke @pytest.mark.slow def test_arma_order_select_ic(): # smoke test, assumes info-criteria are right from statsmodels.tsa.arima_process import arma_generate_sample arparams = np.array([0.75, -0.25]) maparams = np.array([0.65, 0.35]) arparams = np.r_[1, -arparams] maparam = np.r_[1, maparams] # FIXME: Never used nobs = 250 np.random.seed(2014) y = arma_generate_sample(arparams, maparams, nobs) res = arma_order_select_ic(y, ic=["aic", "bic"], trend="n") # regression tests in case we change algorithm to minic in sas aic_x = np.array( [ [764.36517643, 552.7342255, 484.29687843], [562.10924262, 485.5197969, 480.32858497], [507.04581344, 482.91065829, 481.91926034], [484.03995962, 482.14868032, 483.86378955], [481.8849479, 483.8377379, 485.83756612], ] ) bic_x = np.array( [ [767.88663735, 559.77714733, 494.86126118], [569.15216446, 496.08417966, 494.41442864], [517.61019619, 496.99650196, 499.52656493], [498.12580329, 499.75598491, 504.99255506], [499.49225249, 504.96650341, 510.48779255], ] ) aic = DataFrame(aic_x, index=lrange(5), columns=lrange(3)) bic = DataFrame(bic_x, index=lrange(5), columns=lrange(3)) assert_almost_equal(res.aic.values, aic.values, 5) assert_almost_equal(res.bic.values, bic.values, 5) assert_equal(res.aic_min_order, (1, 2)) assert_equal(res.bic_min_order, (1, 2)) assert_(res.aic.index.equals(aic.index)) assert_(res.aic.columns.equals(aic.columns)) assert_(res.bic.index.equals(bic.index)) assert_(res.bic.columns.equals(bic.columns)) index = pd.date_range("2000-1-1", freq="M", periods=len(y)) y_series = pd.Series(y, index=index) res_pd = arma_order_select_ic( y_series, max_ar=2, max_ma=1, ic=["aic", "bic"], trend="n" ) assert_almost_equal(res_pd.aic.values, aic.values[:3, :2], 5) assert_almost_equal(res_pd.bic.values, bic.values[:3, :2], 5) assert_equal(res_pd.aic_min_order, (2, 1)) assert_equal(res_pd.bic_min_order, (1, 1)) res = arma_order_select_ic(y, ic="aic", trend="n") assert_almost_equal(res.aic.values, aic.values, 5) assert_(res.aic.index.equals(aic.index)) assert_(res.aic.columns.equals(aic.columns)) assert_equal(res.aic_min_order, (1, 2)) def test_arma_order_select_ic_failure(): # this should trigger an SVD convergence failure, smoke test that it # returns, likely platform dependent failure... # looks like AR roots may be cancelling out for 4, 1? y = np.array( [ 0.86074377817203640006, 0.85316549067906921611, 0.87104653774363305363, 0.60692382068987393851, 0.69225941967301307667, 0.73336177248909339976, 0.03661329261479619179, 0.15693067239962379955, 0.12777403512447857437, -0.27531446294481976, -0.24198139631653581283, -0.23903317951236391359, -0.26000241325906497947, -0.21282920015519238288, -0.15943768324388354896, 0.25169301564268781179, 0.1762305709151877342, 0.12678133368791388857, 0.89755829086753169399, 0.82667068795350151511, ] ) import warnings with warnings.catch_warnings(): # catch a hessian inversion and convergence failure warning warnings.simplefilter("ignore") res = arma_order_select_ic(y) def test_acf_fft_dataframe(): # regression test #322 result = acf( sunspots.load_pandas().data[["SUNACTIVITY"]], fft=True, nlags=20 ) assert_equal(result.ndim, 1) def test_levinson_durbin_acov(): rho = 0.9 m = 20 acov = rho ** np.arange(200) sigma2_eps, ar, pacf, _, _ = levinson_durbin(acov, m, isacov=True) assert_allclose(sigma2_eps, 1 - rho ** 2) assert_allclose(ar, np.array([rho] + [0] * (m - 1)), atol=1e-8) assert_allclose(pacf, np.array([1, rho] + [0] * (m - 1)), atol=1e-8) @pytest.mark.parametrize("missing", ["conservative", "drop", "raise", "none"]) @pytest.mark.parametrize("fft", [False, True]) @pytest.mark.parametrize("demean", [True, False]) @pytest.mark.parametrize("adjusted", [True, False]) def test_acovf_nlags(acovf_data, adjusted, demean, fft, missing): full = acovf( acovf_data, adjusted=adjusted, demean=demean, fft=fft, missing=missing ) limited = acovf( acovf_data, adjusted=adjusted, demean=demean, fft=fft, missing=missing, nlag=10, ) assert_allclose(full[:11], limited) @pytest.mark.parametrize("missing", ["conservative", "drop"]) @pytest.mark.parametrize("fft", [False, True]) @pytest.mark.parametrize("demean", [True, False]) @pytest.mark.parametrize("adjusted", [True, False]) def test_acovf_nlags_missing(acovf_data, adjusted, demean, fft, missing): acovf_data = acovf_data.copy() acovf_data[1:3] = np.nan full = acovf( acovf_data, adjusted=adjusted, demean=demean, fft=fft, missing=missing ) limited = acovf( acovf_data, adjusted=adjusted, demean=demean, fft=fft, missing=missing, nlag=10, ) assert_allclose(full[:11], limited) def test_acovf_error(acovf_data): with pytest.raises(ValueError): acovf(acovf_data, nlag=250, fft=False) def test_pacf2acf_ar(): pacf = np.zeros(10) pacf[0] = 1 pacf[1] = 0.9 ar, acf = levinson_durbin_pacf(pacf) assert_allclose(acf, 0.9 ** np.arange(10.0)) assert_allclose(ar, pacf[1:], atol=1e-8) ar, acf = levinson_durbin_pacf(pacf, nlags=5) assert_allclose(acf, 0.9 ** np.arange(6.0)) assert_allclose(ar, pacf[1:6], atol=1e-8) def test_pacf2acf_levinson_durbin(): pacf = -(0.9 ** np.arange(11.0)) pacf[0] = 1 ar, acf = levinson_durbin_pacf(pacf) _, ar_ld, pacf_ld, _, _ = levinson_durbin(acf, 10, isacov=True) assert_allclose(ar, ar_ld, atol=1e-8) assert_allclose(pacf, pacf_ld, atol=1e-8) # From R, FitAR, PacfToAR ar_from_r = [ -4.1609, -9.2549, -14.4826, -17.6505, -17.5012, -14.2969, -9.5020, -4.9184, -1.7911, -0.3486, ] assert_allclose(ar, ar_from_r, atol=1e-4) def test_pacf2acf_errors(): pacf = -(0.9 ** np.arange(11.0)) pacf[0] = 1 with pytest.raises(ValueError): levinson_durbin_pacf(pacf, nlags=20) with pytest.raises(ValueError): levinson_durbin_pacf(pacf[1:]) with pytest.raises(ValueError): levinson_durbin_pacf(np.zeros(10)) with pytest.raises(ValueError): levinson_durbin_pacf(np.zeros((10, 2))) def test_pacf_burg(): rnd = np.random.RandomState(12345) e = rnd.randn(10001) y = e[1:] + 0.5 * e[:-1] pacf, sigma2 = pacf_burg(y, 10) yw_pacf = pacf_yw(y, 10) assert_allclose(pacf, yw_pacf, atol=5e-4) # Internal consistency check between pacf and sigma2 ye = y - y.mean() s2y = ye.dot(ye) / 10000 pacf[0] = 0 sigma2_direct = s2y * np.cumprod(1 - pacf ** 2) assert_allclose(sigma2, sigma2_direct, atol=1e-3) def test_pacf_burg_error(): with pytest.raises(ValueError): pacf_burg(np.empty((20, 2)), 10) with pytest.raises(ValueError): pacf_burg(np.empty(100), 101) def test_innovations_algo_brockwell_davis(): ma = -0.9 acovf = np.array([1 + ma ** 2, ma]) theta, sigma2 = innovations_algo(acovf, nobs=4) exp_theta = np.array([[0], [-0.4972], [-0.6606], [-0.7404]]) assert_allclose(theta, exp_theta, rtol=1e-4) assert_allclose(sigma2, [1.81, 1.3625, 1.2155, 1.1436], rtol=1e-4) theta, sigma2 = innovations_algo(acovf, nobs=500) assert_allclose(theta[-1, 0], ma) assert_allclose(sigma2[-1], 1.0) def test_innovations_algo_rtol(): ma = np.array([-0.9, 0.5]) acovf = np.array([1 + (ma ** 2).sum(), ma[0] + ma[1] * ma[0], ma[1]]) theta, sigma2 = innovations_algo(acovf, nobs=500) theta_2, sigma2_2 = innovations_algo(acovf, nobs=500, rtol=1e-8) assert_allclose(theta, theta_2) assert_allclose(sigma2, sigma2_2) def test_innovations_errors(): ma = -0.9 acovf = np.array([1 + ma ** 2, ma]) with pytest.raises(TypeError): innovations_algo(acovf, nobs=2.2) with pytest.raises(ValueError): innovations_algo(acovf, nobs=-1) with pytest.raises(ValueError): innovations_algo(np.empty((2, 2))) with pytest.raises(TypeError): innovations_algo(acovf, rtol="none") def test_innovations_filter_brockwell_davis(reset_randomstate): ma = -0.9 acovf = np.array([1 + ma ** 2, ma]) theta, _ = innovations_algo(acovf, nobs=4) e = np.random.randn(5) endog = e[1:] + ma * e[:-1] resid = innovations_filter(endog, theta) expected = [endog[0]] for i in range(1, 4): expected.append(endog[i] - theta[i, 0] * expected[-1]) expected = np.array(expected) assert_allclose(resid, expected) def test_innovations_filter_pandas(reset_randomstate): ma = np.array([-0.9, 0.5]) acovf = np.array([1 + (ma ** 2).sum(), ma[0] + ma[1] * ma[0], ma[1]]) theta, _ = innovations_algo(acovf, nobs=10) endog = np.random.randn(10) endog_pd = pd.Series(endog, index=pd.date_range("2000-01-01", periods=10)) resid = innovations_filter(endog, theta) resid_pd = innovations_filter(endog_pd, theta) assert_allclose(resid, resid_pd.values) assert_index_equal(endog_pd.index, resid_pd.index) def test_innovations_filter_errors(): ma = -0.9 acovf = np.array([1 + ma ** 2, ma]) theta, _ = innovations_algo(acovf, nobs=4) with pytest.raises(ValueError): innovations_filter(np.empty((2, 2)), theta) with pytest.raises(ValueError): innovations_filter(np.empty(4), theta[:-1]) with pytest.raises(ValueError): innovations_filter(pd.DataFrame(np.empty((1, 4))), theta) def test_innovations_algo_filter_kalman_filter(reset_randomstate): # Test the innovations algorithm and filter against the Kalman filter # for exact likelihood evaluation of an ARMA process ar_params = np.array([0.5]) ma_params = np.array([0.2]) # TODO could generalize to sigma2 != 1, if desired, after #5324 is merged # and there is a sigma2 argument to arma_acovf # (but maybe this is not really necessary for the point of this test) sigma2 = 1 endog = np.random.normal(size=10) # Innovations algorithm approach acovf = arma_acovf( np.r_[1, -ar_params], np.r_[1, ma_params], nobs=len(endog) ) theta, v = innovations_algo(acovf) u = innovations_filter(endog, theta) llf_obs = -0.5 * u ** 2 / (sigma2 * v) - 0.5 * np.log(2 * np.pi * v) # Kalman filter apparoach mod = SARIMAX(endog, order=(len(ar_params), 0, len(ma_params))) res = mod.filter(np.r_[ar_params, ma_params, sigma2]) # Test that the two approaches are identical atol = 1e-6 if PLATFORM_WIN else 0.0 assert_allclose(u, res.forecasts_error[0], rtol=1e-6, atol=atol) assert_allclose( theta[1:, 0], res.filter_results.kalman_gain[0, 0, :-1], atol=atol ) assert_allclose(llf_obs, res.llf_obs, atol=atol) def test_adfuller_short_series(reset_randomstate): y = np.random.standard_normal(7) res = adfuller(y, store=True) assert res[-1].maxlag == 1 y = np.random.standard_normal(2) with pytest.raises(ValueError, match="sample size is too short"): adfuller(y) y = np.random.standard_normal(3) with pytest.raises(ValueError, match="sample size is too short"): adfuller(y, regression="ct") def test_adfuller_maxlag_too_large(reset_randomstate): y = np.random.standard_normal(100) with pytest.raises(ValueError, match="maxlag must be less than"): adfuller(y, maxlag=51) class SetupZivotAndrews(object): # test directory cur_dir = CURR_DIR run_dir = os.path.join(cur_dir, "results") # use same file for testing failure modes fail_file = os.path.join(run_dir, "rgnp.csv") fail_mdl = np.asarray(pd.read_csv(fail_file)) class TestZivotAndrews(SetupZivotAndrews): # failure mode tests def test_fail_regression_type(self): with pytest.raises(ValueError): zivot_andrews(self.fail_mdl, regression="x") def test_fail_trim_value(self): with pytest.raises(ValueError): zivot_andrews(self.fail_mdl, trim=0.5) def test_fail_array_shape(self): with pytest.raises(ValueError): zivot_andrews(np.random.rand(50, 2)) def test_fail_autolag_type(self): with pytest.raises(ValueError): zivot_andrews(self.fail_mdl, autolag="None") @pytest.mark.parametrize("autolag", ["AIC", "aic", "Aic"]) def test_autolag_case_sensitivity(self, autolag): res = zivot_andrews(self.fail_mdl, autolag=autolag) assert res[3] == 1 # following tests compare results to R package urca.ur.za (1.13-0) def test_rgnp_case(self): res = zivot_andrews( self.fail_mdl, maxlag=8, regression="c", autolag=None ) assert_allclose( [res[0], res[1], res[4]], [-5.57615, 0.00312, 20], rtol=1e-3 ) def test_gnpdef_case(self): mdlfile = os.path.join(self.run_dir, "gnpdef.csv") mdl = np.asarray(pd.read_csv(mdlfile)) res = zivot_andrews(mdl, maxlag=8, regression="c", autolag="t-stat") assert_allclose( [res[0], res[1], res[3], res[4]], [-4.12155, 0.28024, 5, 40], rtol=1e-3, ) def test_stkprc_case(self): mdlfile = os.path.join(self.run_dir, "stkprc.csv") mdl = np.asarray(pd.read_csv(mdlfile)) res = zivot_andrews(mdl, maxlag=8, regression="ct", autolag="t-stat") assert_allclose( [res[0], res[1], res[3], res[4]], [-5.60689, 0.00894, 1, 65], rtol=1e-3, ) def test_rgnpq_case(self): mdlfile = os.path.join(self.run_dir, "rgnpq.csv") mdl = np.asarray(pd.read_csv(mdlfile)) res = zivot_andrews(mdl, maxlag=12, regression="t", autolag="t-stat") assert_allclose( [res[0], res[1], res[3], res[4]], [-3.02761, 0.63993, 12, 102], rtol=1e-3, ) def test_rand10000_case(self): mdlfile = os.path.join(self.run_dir, "rand10000.csv") mdl = np.asarray(pd.read_csv(mdlfile)) res = zivot_andrews(mdl, regression="c", autolag="t-stat") assert_allclose( [res[0], res[1], res[3], res[4]], [-3.48223, 0.69111, 25, 7071], rtol=1e-3, ) def test_acf_conservate_nanops(reset_randomstate): # GH 6729 e = np.random.standard_normal(100) for i in range(1, e.shape[0]): e[i] += 0.9 * e[i - 1] e[::7] = np.nan result = acf(e, missing="conservative", nlags=10, fft=False) resid = e - np.nanmean(e) expected = np.ones(11) nobs = e.shape[0] gamma0 = np.nansum(resid * resid) for i in range(1, 10 + 1): expected[i] = np.nansum(resid[i:] * resid[: nobs - i]) / gamma0 assert_allclose(result, expected, rtol=1e-4, atol=1e-4) def test_pacf_nlags_error(reset_randomstate): e = np.random.standard_normal(100) with pytest.raises(ValueError, match="Can only compute partial"): pacf(e, 50) def test_coint_auto_tstat(): rs = np.random.RandomState(3733696641) x = np.cumsum(rs.standard_normal(100)) y = np.cumsum(rs.standard_normal(100)) res = coint( x, y, trend="c", method="aeg", maxlag=0, autolag="t-stat", return_results=False, ) assert np.abs(res[0]) < 1.65 rs = np.random.RandomState(1) a = rs.random_sample(120) b = np.zeros_like(a) df1 = pd.DataFrame({"b": b, "a": a}) df2 =
pd.DataFrame({"a": a, "b": b})
pandas.DataFrame
""" Code for loading data """ import os, sys import shutil import argparse import functools import multiprocessing import gzip import inspect import glob import json import itertools import collections import logging from typing import * import torch from torch.utils.data import Dataset import numpy as np import pandas as pd import Levenshtein import featurization as ft import utils LOCAL_DATA_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), "data") assert os.path.isdir(LOCAL_DATA_DIR) EXTERNAL_EVAL_DIR = os.path.join(os.path.dirname(LOCAL_DATA_DIR), "external_eval") assert os.path.join(EXTERNAL_EVAL_DIR) # Names of datasets DATASET_NAMES = {"LCMV", "VDJdb", "PIRD", "TCRdb"} logging.basicConfig(level=logging.INFO) class TcrABSupervisedIdxDataset(Dataset): """Dataset that returns TcrAB and label""" def __init__( self, tcr_table: pd.DataFrame, label_col: str = "tetramer", pos_labels: Collection[str] = ["TetMid", "TetPos"], idx_encode: bool = False, max_a_len: Optional[int] = None, max_b_len: Optional[int] = None, disambiguate_labels: bool = True, ): self.table = tcr_table self.label_col = label_col if disambiguate_labels: logging.info("Deduping and removing examples with conflicting labels") lcmv_dedup_ab, self.labels = dedup_lcmv_table(tcr_table) self.tras, self.trbs = zip(*lcmv_dedup_ab) else: raise NotImplementedError( "Running withough disambiguating labels causes duplicated and conflicting labels! This was the prior behavior, but is now deprecated" ) tcr_a_lengths = [len(self.get_ith_tcr_a(i)) for i in range(len(self))] tcr_b_lengths = [len(self.get_ith_tcr_b(i)) for i in range(len(self))] self.max_a_len = max(tcr_a_lengths) if max_a_len is None else max_a_len self.max_b_len = max(tcr_b_lengths) if max_b_len is None else max_b_len self.idx_encode = idx_encode logging.info(f"Maximum TCR A/B lengths: {self.max_a_len} {self.max_b_len}") self.pos_labels = pos_labels logging.info(f"Positive {label_col} labels: {pos_labels}") def __len__(self) -> int: return len(self.labels) def get_ith_tcr_a(self, idx: int, pad: bool = False) -> str: """Gets the ith TRA sequence""" seq = self.tras[idx] if pad: seq = ft.pad_or_trunc_sequence(seq, self.max_a_len, right_align=False) return seq def get_ith_tcr_b(self, idx: int, pad: bool = False) -> str: """Gets the ith TRB sequence""" seq = self.trbs[idx] if pad: seq = ft.pad_or_trunc_sequence(seq, self.max_b_len, right_align=False) return seq def get_ith_sequence(self, idx: int) -> Tuple[str, str]: """Get the ith TRA/TRB pair""" return self.tras[idx], self.trbs[idx] def get_ith_label(self, idx: int, idx_encode: Optional[bool] = None) -> np.ndarray: """Get the ith label""" label = self.labels[idx] retval = float(np.any([l in label for l in self.pos_labels])) retval = np.array([1.0 - retval, retval], dtype=np.float32) idx_encode = self.idx_encode if idx_encode is None else idx_encode if idx_encode: retval = np.where(retval)[0] return retval def __getitem__(self, idx: int): tcr_a_idx = ft.idx_encode(self.get_ith_tcr_a(idx, pad=True)) tcr_b_idx = ft.idx_encode(self.get_ith_tcr_b(idx, pad=True)) label = self.get_ith_label(idx) return ( { "tcr_a": torch.from_numpy(tcr_a_idx), "tcr_b": torch.from_numpy(tcr_b_idx), }, torch.from_numpy(label).type(torch.long).squeeze(), ) class TcrABSupervisedOneHotDataset(TcrABSupervisedIdxDataset): """Dataset that encodes tcrAB as one hot encoded vectors""" def __getitem__(self, idx: int): tcr_a_idx = ft.one_hot(self.get_ith_tcr_a(idx, pad=True)) tcr_b_idx = ft.one_hot(self.get_ith_tcr_b(idx, pad=True)) label = self.get_ith_label(idx) return ( { "tcr_a": torch.from_numpy(tcr_a_idx), "tcr_b": torch.from_numpy(tcr_b_idx), }, torch.from_numpy(label).type(torch.long).squeeze(), ) class TCRSupervisedIdxDataset(Dataset): """Dataset meant for either TRA or TRB supervised learning""" def __init__( self, tcrs: Sequence[str], labels: Sequence[bool], idx_encode_labels: bool = True, max_len: Optional[int] = None, ): self.tcrs = tcrs self.labels = labels assert len(self.tcrs) == len(self.labels) self.max_len = max_len # Defaults to None determined_max_len = max([len(t) for t in tcrs]) if self.max_len is not None: # If a max_len is explicitly given, check that it is greater than the actual max len assert isinstance(self.max_len, int) assert determined_max_len <= self.max_len logging.info( f"Given max_len of {self.max_len} exceeds (as expected) empirical max_len of {determined_max_len}" ) else: # If max_len is not given, directly set the max_len logging.info( f"Max len not set, using empirical max len of {determined_max_len}" ) self.max_len = determined_max_len logging.info(f"Using maximum length of {self.max_len}") self.idx_encode_labels = idx_encode_labels def all_labels(self) -> Sequence[bool]: """Return all labels""" return self.labels def __len__(self) -> int: return len(self.tcrs) def get_ith_tcr(self, idx: int, pad: bool = True) -> str: """Returns the ith tcr sequence, padded with null residues""" retval = self.tcrs[idx] if pad: retval = ft.pad_or_trunc_sequence(retval, self.max_len, right_align=False) return retval def get_ith_sequence(self, idx: int) -> str: return self.tcrs[idx] def get_ith_label(self, idx: int) -> np.ndarray: retval = float(self.labels[idx]) if not self.idx_encode_labels: retval = np.array([1.0 - retval, retval], dtype=np.float32) return np.atleast_1d(retval) def __getitem__(self, idx: int) -> Tuple[Dict[str, torch.Tensor], torch.Tensor]: tcr_idx = ft.idx_encode(self.get_ith_tcr(idx, pad=True)) label = self.get_ith_label(idx) return ( {"seq": torch.from_numpy(tcr_idx)}, torch.from_numpy(label).type(torch.long).squeeze(), ) class TcrSelfSupervisedDataset(TcrABSupervisedIdxDataset): """ Mostly for compatibility with transformers library LineByLineTextDataset returns a dict of "input_ids" -> input_ids """ # Reference: https://github.com/huggingface/transformers/blob/447808c85f0e6d6b0aeeb07214942bf1e578f9d2/src/transformers/data/datasets/language_modeling.py def __init__(self, tcr_seqs: Iterable[str], tokenizer, round_len: bool = True): self.tcr_seqs = utils.dedup(tcr_seqs) logging.info( f"Creating self supervised dataset with {len(self.tcr_seqs)} sequences" ) self.max_len = max([len(s) for s in self.tcr_seqs]) logging.info(f"Maximum sequence length: {self.max_len}") if round_len: self.max_len = int(utils.min_power_greater_than(self.max_len, 2)) logging.info(f"Rounded maximum length to {self.max_len}") self.tokenizer = tokenizer self._has_logged_example = False def __len__(self) -> int: return len(self.tcr_seqs) def __getitem__(self, i: int) -> Dict[str, torch.Tensor]: tcr = self.tcr_seqs[i] retval = self.tokenizer.encode(ft.insert_whitespace(tcr)) if not self._has_logged_example: logging.info(f"Example of tokenized input: {tcr} -> {retval}") self._has_logged_example = True return {"input_ids": torch.tensor(retval, dtype=torch.long)} def merge(self, other): """Merge this dataset with the other dataset""" all_tcrs = utils.dedup(self.tcr_seqs + other.tcr_seqs) logging.info( f"Merged two self-supervised datasets of sizes {len(self)} {len(other)} for dataset of {len(all_tcrs)}" ) return TcrSelfSupervisedDataset(all_tcrs) class TcrNextSentenceDataset(Dataset): """ Dataset for next sentence prediction. Input is two lists of pairwise corresponding TRA TRB sequences Note that the labelling scheme here is (False, True) This DIFFERS from the convention used in the transformers library for NSP Note that TRA/TRB naming convention is somewhat of a minomoer - in reality, these are just first/second pairs. This also supports generating NEGATIVE examples dynamically. This is automatically enabled when this is wrapped in a DatasetSplit object with training split. This may yield improved sampling of the negative manifold and yield a more general model """ def __init__( self, tra_seqs: List[str], trb_seqs: List[str], neg_ratio: float = 1.0, labels: Optional[Iterable[bool]] = None, tra_blacklist: Optional[Iterable[str]] = None, mlm: float = 0.0, max_len: int = 64, seed: int = 4242, remove_null: bool = True, shuffle: bool = True, ): assert len(tra_seqs) == len(trb_seqs) # Remove cases of nan logging.info(f"Build NSP dataset with {len(tra_seqs)} pairs") if remove_null: bad_idx_a = [ i for i, seq in enumerate(tra_seqs) if seq is None or pd.isnull(seq) or seq == "" ] bad_idx_b = [ i for i, seq in enumerate(trb_seqs) if seq is None or pd.isnull(seq) or seq == "" ] bad_idx = set(bad_idx_a).union(bad_idx_b) logging.info( f"Removing {len(bad_idx)} bad pairs: {len(bad_idx_a)} union {len(bad_idx_b)}" ) tra_seqs = [a for i, a in enumerate(tra_seqs) if i not in bad_idx] trb_seqs = [a for i, a in enumerate(trb_seqs) if i not in bad_idx] if tra_blacklist is not None: bad_idx = [i for i, seq in enumerate(tra_seqs) if seq in set(tra_blacklist)] logging.info(f"Removing {len(bad_idx)} blacklisted items") tra_seqs = [a for i, a in enumerate(tra_seqs) if i not in bad_idx] trb_seqs = [a for i, a in enumerate(trb_seqs) if i not in bad_idx] logging.info(f"Building NSP datset with {len(tra_seqs)} pairs after filtering") # Insert whitespace as we store the sequences # Whitespace separated inputs is expected by tokenizer # These are never shuffled, regardless of the shuffle param self.tra = [ft.insert_whitespace(aa) for aa in tra_seqs] self.trb = [ft.insert_whitespace(aa) for aa in trb_seqs] assert 0.0 <= mlm <= 1.0 self.mlm = mlm self.neg_ratio = neg_ratio self.rng = np.random.default_rng(seed=seed) if self.neg_ratio > 0: assert labels is None, "Cannot sample negatives if labels are given" pos_pairs = list(zip(self.tra, self.trb)) num_negs = int(round(len(pos_pairs) * neg_ratio)) logging.info(f"Sampling {num_negs} negatives") neg_pairs = [self.__sample_negative() for _i in range(num_negs)] logging.info(f"Positive pairs: {len(pos_pairs)}") logging.info(f"Sampled negative pairs: {len(neg_pairs)}") # WARNING in tokenizers convention, output is (True, False) # This means that a correct pair is a "0" and a wrong pair is a "1" # we DO NOT adhere to this convention, rather using a conventional labelling self.labels = np.array([1] * len(pos_pairs) + [0] * len(neg_pairs)) self.all_pairs = pos_pairs + neg_pairs elif labels is not None: logging.info(f"Taking given labels with {np.mean(labels)} positive rate") self.labels = labels self.all_pairs = list(zip(self.tra, self.trb)) else: # raise RuntimeError("Must provide either neg_ratio or labels argument") logging.warn( "No labels or negative ratio provided, defaulting to all negative labels" ) self.all_pairs = list(zip(self.tra, self.trb)) self.labels = np.array([0.0] * len(self.all_pairs)) assert len(self.labels) == len(self.all_pairs) self.max_len = max_len max_len_actual = max( max([len(aa.split()) for aa in self.tra]), max([len(aa.split()) for aa in self.trb]), ) logging.info(f"Maximum length of NSP single sequence: {max_len_actual}") self.tok = ft.get_aa_bert_tokenizer(max_len=max_len_actual) # Shuffle the examples if shuffle: logging.info("Shuffling NSP dataset") shuf_idx = np.arange(len(self.labels)) self.rng.shuffle(shuf_idx) self.labels = self.labels[shuf_idx] # Contains whether this is a valid pair self.all_pairs = [self.all_pairs[i] for i in shuf_idx] logging.info( f"NSP dataset of {len(self.all_pairs)} pairs, {np.sum(self.labels)} positive examples" ) logging.info(f"Example training example") for k, v in self[0].items(): logging.info(f"{k}: {v}") def __sample_negative(self) -> Tuple[str, str]: """ Generate a negative example """ if self.neg_ratio <= 0.0: raise RuntimeError("Cannot sample negatives for labelled dataset") i, j = self.rng.integers(len(self.tra), size=2) while self.tra[i] == self.tra[j]: # Is not a valid pair j = self.rng.integers(len(self.tra)) return self.tra[i], self.trb[j] def __len__(self) -> int: assert len(self.labels) == len(self.all_pairs) return len(self.labels) def get_ith_label(self, idx): return self.labels[idx] def get_ith_sequence(self, idx) -> Tuple[str, str]: return self.all_pairs[idx] def __getitem__(self, idx: int, dynamic: bool = False) -> Dict[str, torch.Tensor]: """ dynamic is a general flag for generating examples dynamically """ label = self.labels[idx] label_tensor = torch.LongTensor(np.atleast_1d(label)) if dynamic and label == 0: # Dynamically generate a negative example pair = self.__sample_negative() else: # Positive example OR not dynamic pair = self.all_pairs[idx] if self.mlm > 0.0: # Mask out each sequence BEFORE we pad/concatenate them # This ensures that the mask is always an amino acid mlm_targets, pair = zip(*[ft.mask_for_training(a) for a in pair]) t = np.atleast_1d(-100).astype(np.int64) # CLS seq1 SEP seq2 SEP mlm_targets_combined = np.concatenate( [t, mlm_targets[0], t, mlm_targets[1], t] ) mlm_targets_padded = torch.LongTensor( np.pad( mlm_targets_combined, (0, self.max_len - len(mlm_targets_combined)), mode="constant", constant_values=-100, ) ) enc = self.tok( text=pair[0], text_pair=pair[1], padding="max_length", max_length=self.max_len, return_tensors="pt", ) # Default tokenization has (batch, ...) as first dim # Since __getitem__ only gets a single example, remove this enc = {k: v.squeeze() for k, v in enc.items()} if self.mlm > 0.0: # NSP + MLM assert ( mlm_targets_padded.size() == enc["input_ids"].size() ), f"Mismatched sizes {mlm_targets_padded.size()} {enc['input_ids'].size()}" enc["next_sentence_label"] = label_tensor enc["labels"] = torch.LongTensor(mlm_targets_padded) else: # NSP only enc["labels"] = label_tensor return enc def get_all_items(self) -> Dict[str, torch.Tensor]: """ Get all the data instead of individual entries """ collector = collections.defaultdict(list) for i in range(len(self)): x = self[i] for k, v in x.items(): collector[k].append(v.reshape(1, -1)) retval = {k: torch.cat(v, dim=0) for k, v in collector.items()} return retval class TcrFineTuneSingleDataset(TcrSelfSupervisedDataset): """Dataset for fine tuning from only TRA or TRB sequences""" def __init__( self, aa: Sequence[str], labels: MutableSequence[float], label_continuous: bool = False, label_labels: Optional[Sequence[str]] = None, drop_rare_labels: bool = True, ): assert len(aa) == len( labels ), f"Got differing lengths for aa and labels: {len(aa)}, {len(labels)}" self.aa = [ft.insert_whitespace(item) for item in aa] self.tokenizer = ft.get_aa_bert_tokenizer(64) self.continuous = label_continuous label_dtype = np.float32 if self.continuous else np.int64 self.labels = np.array(labels, dtype=label_dtype).squeeze() assert len(self.labels) == len(self.aa) self.label_labels = label_labels if self.continuous: assert self.label_labels is None if drop_rare_labels and not self.continuous and not self.is_multilabel: # Get the mean positive rate for each label labels_expanded = np.zeros((len(labels), np.max(labels) + 1)) labels_expanded[np.arange(len(labels)), self.labels] = 1 per_label_prop = np.mean(labels_expanded, axis=0) # Find the labels with high enough positive rate good_idx = np.where(per_label_prop >= 1e-3)[0] if len(good_idx) < labels_expanded.shape[1]: logging.info( f"Retaining {len(good_idx)}/{labels_expanded.shape[1]} labels with sufficient examples" ) # Reconstruct labels based only on retained good_idx # nonzero returns indices of element that are nonzero self.labels = np.array( [ np.nonzero(good_idx == label)[0][0] if label in good_idx else len(good_idx) # "other" labels for label in self.labels ], dtype=label_dtype, ) assert np.max(self.labels) == len(good_idx) # Subset label labels self.label_labels = [self.label_labels[i] for i in good_idx] + ["other"] assert len(self.label_labels) == len(good_idx) + 1 @property def is_multilabel(self) -> bool: """Return True if labels represent multilabel classification""" return len(self.labels.shape) > 1 def get_ith_sequence(self, idx: int) -> str: """Get the ith sequence""" return self.aa[idx] def get_ith_label(self, idx: int) -> np.ndarray: """Gets the ith label""" return np.atleast_1d(self.labels[idx]) def __len__(self) -> int: return len(self.labels) def __getitem__(self, idx: int) -> Dict[str, torch.Tensor]: label = torch.tensor(self.get_ith_label(idx)) if self.is_multilabel: # Multilabel -> BCEWithLogitsLoss which wants float target label = label.float() # We already inserted whitespaces in init enc = self.tokenizer( self.aa[idx], padding="max_length", max_length=64, return_tensors="pt" ) enc = {k: v.squeeze() for k, v in enc.items()} enc["labels"] = label return enc class TcrFineTuneDataset(TcrSelfSupervisedDataset): """Can supply tokenizer to work with ESM""" def __init__( self, tcr_a_seqs: Sequence[str], tcr_b_seqs: Sequence[str], labels: Optional[np.ndarray] = None, label_continuous: bool = False, tokenizer: Optional[Callable] = None, skorch_mode: bool = True, idx_encode: bool = False, ): assert len(tcr_a_seqs) == len(tcr_b_seqs) self.tcr_a = list(tcr_a_seqs) self.tcr_b = list(tcr_b_seqs) self.max_len = max([len(s) for s in self.tcr_a + self.tcr_b]) + 2 if tokenizer is None: tokenizer = ft.get_aa_bert_tokenizer(self.max_len) self.tcr_a_tokenized = [ tokenizer.encode( ft.insert_whitespace(aa), padding="max_length", max_length=self.max_len, ) for aa in self.tcr_a ] self.tcr_b_tokenized = [ tokenizer.encode( ft.insert_whitespace(aa), padding="max_length", max_length=self.max_len, ) for aa in self.tcr_b ] else: logging.info(f"Using pre-supplied tokenizer: {tokenizer}") _label, _seq, self.tcr_a_tokenized = tokenizer(list(enumerate(self.tcr_a))) _label, _seq, self.tcr_b_tokenized = tokenizer(list(enumerate(self.tcr_b))) if labels is not None: assert len(labels) == len(tcr_a_seqs) self.labels = np.atleast_1d(labels.squeeze()) else: logging.warning( "Labels not given, defaulting to False labels (DO NOT USE FOR TRAINING)" ) self.labels = None self.continuous = label_continuous self.skorch_mode = skorch_mode self.idx_encode = idx_encode def get_ith_sequence(self, idx: int) -> Tuple[str, str]: """Get the ith TRA/TRB pair""" return self.tcr_a[idx], self.tcr_b[idx] def get_ith_label(self, idx: int, idx_encode: Optional[bool] = None) -> np.ndarray: """Get the ith label""" if self.labels is None: return np.array([0]) # Dummy value if not self.continuous: label = self.labels[idx] if not isinstance(label, np.ndarray): label = np.atleast_1d(label) if self.skorch_mode and len(label) == 1: label = np.array([1.0 - label, label]).squeeze() # Take given value if supplied, else default to self.idx_encode idx_encode = self.idx_encode if idx_encode is None else idx_encode if idx_encode: label = np.where(label)[0] return label else: # For the continuous case we simply return the ith value(s) return self.labels[idx] def __len__(self) -> int: return len(self.tcr_a) def __getitem__( self, idx: int ) -> Union[Dict[str, torch.Tensor], Tuple[Dict[str, torch.Tensor], torch.Tensor]]: label_dtype = torch.float if self.continuous else torch.long tcr_a = self.tcr_a_tokenized[idx] tcr_b = self.tcr_b_tokenized[idx] label = self.get_ith_label(idx) if not self.skorch_mode: retval = { "tcr_a": utils.ensure_tensor(tcr_a, dtype=torch.long), "tcr_b": utils.ensure_tensor(tcr_b, dtype=torch.long), "labels": utils.ensure_tensor(label, dtype=label_dtype), } else: model_inputs = { "tcr_a": utils.ensure_tensor(tcr_a, dtype=torch.long), "tcr_b": utils.ensure_tensor(tcr_b, dtype=torch.long), } retval = (model_inputs, torch.tensor(label, dtype=label_dtype).squeeze()) return retval class DatasetSplit(Dataset): """ Dataset split. Thin wrapper on top a dataset to provide data split functionality. Can also enable dynamic example generation for train fold if supported by the wrapped dataset (NOT for valid/test folds) via dynamic_training flag kwargs are forwarded to shuffle_indices_train_valid_test """ def __init__( self, full_dataset: Dataset, split: str, dynamic_training: bool = False, **kwargs, ): self.dset = full_dataset split_to_idx = {"train": 0, "valid": 1, "test": 2} assert split in split_to_idx self.split = split self.dynamic = dynamic_training if self.split != "train": assert not self.dynamic, "Cannot have dynamic examples for valid/test" self.idx = shuffle_indices_train_valid_test( np.arange(len(self.dset)), **kwargs )[split_to_idx[self.split]] logging.info(f"Split {self.split} with {len(self)} examples") def all_labels(self, **kwargs) -> np.ndarray: """Get all labels""" if not hasattr(self.dset, "get_ith_label"): raise NotImplementedError("Wrapped dataset must implement get_ith_label") labels = [ self.dset.get_ith_label(self.idx[i], **kwargs) for i in range(len(self)) ] return np.stack(labels) def all_sequences(self, **kwargs) -> Union[List[str], List[Tuple[str, str]]]: """Get all sequences""" if not hasattr(self.dset, "get_ith_sequence"): raise NotImplementedError( f"Wrapped dataset {type(self.dset)} must implement get_ith_sequence" ) # get_ith_sequence could return a str or a tuple of two str (TRA/TRB) sequences = [ self.dset.get_ith_sequence(self.idx[i], **kwargs) for i in range(len(self)) ] return sequences def to_file(self, fname: str, compress: bool = True) -> str: """ Write to the given file """ if not ( hasattr(self.dset, "get_ith_label") and hasattr(self.dset, "get_ith_sequence") ): raise NotImplementedError( "Wrapped dataset must implement both get_ith_label & get_ith_sequence" ) assert fname.endswith(".json") all_examples = [] for idx in range(len(self)): seq = self.dset.get_ith_sequence(self.idx[idx]) label_list = self.dset.get_ith_label(self.idx[idx]).tolist() all_examples.append((seq, label_list)) with open(fname, "w") as sink: json.dump(all_examples, sink, indent=4) if compress: with open(fname, "rb") as source: with gzip.open(fname + ".gz", "wb") as sink: shutil.copyfileobj(source, sink) os.remove(fname) fname += ".gz" assert os.path.isfile(fname) return os.path.abspath(fname) def __len__(self) -> int: return len(self.idx) def __getitem__(self, idx: int): if ( self.dynamic and self.split == "train" and "dynamic" in inspect.getfullargspec(self.dset.__getitem__).args ): return self.dset.__getitem__(self.idx[idx], dynamic=True) return self.dset.__getitem__(self.idx[idx]) class DatasetSplitByAttribute(DatasetSplit): """ Dataset split. Thin wrapper on top of a datset to provide data split functionality. Unlike the above, which is a purely random split, this splits by a given attribute. attr_getter function should take the dataset and return a list of attrs to split by """ def __init__( self, full_datset: Dataset, attr_getter: Callable, split: str, dynamic_training: bool = False, valid: float = 0.15, test: float = 0.15, seed: int = 1234, ): self.dset = full_datset self.dynamic = dynamic_training self.split = split self.split_attr = attr_getter(self.dset) assert len(self.split_attr) == len(self.dset) # Get the unique attrs and count occurrences of each split_attr_counts = collections.Counter(self.split_attr) assert ( len(split_attr_counts) >= 2 ), f"Must have at least two classes of attribute to split, but got {len(split_attr_counts)}" # Sort the attrs by most counts to least _, self.train_attrs = zip( *sorted( [(count, attr) for attr, count in split_attr_counts.items()], reverse=True, ) ) self.train_attrs = list(self.train_attrs) # Build valid, then test sets, by greedily taking the largest groups # until we have at least the required number of examples valid_n, test_n = len(self.dset) * valid, len(self.dset) * test self.valid_attrs, self.test_attrs = [], [] while sum([split_attr_counts[a] for a in self.valid_attrs]) < valid_n: # Take the biggest item in the list self.valid_attrs.append(self.train_attrs.pop(0)) while sum([split_attr_counts[a] for a in self.test_attrs]) < test_n: # Take the biggest item in the list self.test_attrs.append(self.train_attrs.pop(0)) train_idx = np.array( [ i for i, attr in enumerate(self.split_attr) if attr in set(self.train_attrs) ] ) valid_idx = np.array( [ i for i, attr in enumerate(self.split_attr) if attr in set(self.valid_attrs) ] ) test_idx = np.array( [ i for i, attr in enumerate(self.split_attr) if attr in set(self.test_attrs) ] ) assert len(train_idx) + len(valid_idx) + len(test_idx) == len(self.dset) logging.info( f"Train split with {len(train_idx)} examples across {len(self.train_attrs)} attrs" ) logging.info( f"Valid split with {len(valid_idx)} examples across {len(self.valid_attrs)} attrs" ) logging.info( f"Test split with {len(test_idx)} examples across {len(self.test_attrs)} attrs" ) rng = np.random.default_rng(seed) rng.shuffle(train_idx) rng.shuffle(valid_idx) rng.shuffle(test_idx) self.idx = {"train": train_idx, "valid": valid_idx, "test": test_idx}[split] class DownsampledDataset(Dataset): """ Downsampled and shuffled dataset. Useful for evaluating impact of having less data. Downsampling is done to a *fixed* subset of the original dataset """ def __init__(self, dset: Dataset, downsample: float = 0.1, seed: int = 3939): assert 0.0 < downsample < 1.0 self.dset = dset self.downsample = downsample self.idx = np.arange(len(self.dset)) np.random.seed(seed) np.random.shuffle(self.idx) self.idx = self.idx[: int(np.round(downsample * len(self.dset)))] logging.info(f"Downsampled from {len(self.dset)} -> {len(self)} samples") def __len__(self) -> int: return len(self.idx) def __getitem__(self, idx: int): return self.dset[self.idx[idx]] def shuffle_indices_train_valid_test( idx: np.ndarray, valid: float = 0.15, test: float = 0.15, seed: int = 1234 ) -> Tuple[np.ndarray]: """ Given an array of indices, return indices partitioned into train, valid, and test indices The following tests ensure that ordering is consistent across different calls >>> np.all(shuffle_indices_train_valid_test(np.arange(100))[0] == shuffle_indices_train_valid_test(np.arange(100))[0]) True >>> np.all(shuffle_indices_train_valid_test(np.arange(10000))[1] == shuffle_indices_train_valid_test(np.arange(10000))[1]) True >>> np.all(shuffle_indices_train_valid_test(np.arange(20000))[2] == shuffle_indices_train_valid_test(np.arange(20000))[2]) True >>> np.all(shuffle_indices_train_valid_test(np.arange(1000), 0.1, 0.1)[1] == shuffle_indices_train_valid_test(np.arange(1000), 0.1, 0.1)[1]) True """ np.random.seed(seed) # For reproducible subsampling indices = np.copy(idx) # Make a copy because shuffling occurs in place np.random.shuffle(indices) # Shuffles inplace num_valid = int(round(len(indices) * valid)) if valid > 0 else 0 num_test = int(round(len(indices) * test)) if test > 0 else 0 num_train = len(indices) - num_valid - num_test assert num_train > 0 and num_valid >= 0 and num_test >= 0 assert num_train + num_valid + num_test == len( indices ), f"Got mismatched counts: {num_train} + {num_valid} + {num_test} != {len(indices)}" indices_train = indices[:num_train] indices_valid = indices[num_train : num_train + num_valid] indices_test = indices[-num_test:] assert indices_train.size + indices_valid.size + indices_test.size == len(idx) return indices_train, indices_valid, indices_test def split_arr( arr: Union[np.ndarray, pd.DataFrame, list, tuple], split: Literal["train", "valid", "test"], **kwargs, ) -> Union[np.ndarray, pd.DataFrame, list]: """ Randomly split the array into the given split kwargs are fed to shuffle_indices_train_valid_test """ split_to_idx = {"train": 0, "valid": 1, "test": 2} assert split in split_to_idx, f"Unrecognized split: {split}" n = len(arr) if isinstance(arr, (list, tuple)) else arr.shape[0] indices = np.arange(n) keep_idx = shuffle_indices_train_valid_test(indices, **kwargs)[split_to_idx[split]] if isinstance(arr, pd.DataFrame): return arr.iloc[keep_idx] if isinstance(arr, (list, tuple)): return [arr[i] for i in keep_idx] return arr[keep_idx] def sample_unlabelled_tcrdb_trb( n: int, blacklist: Optional[Collection[str]] = None, seed: int = 6464 ) -> List[str]: """ Convenience function to sample the given number of TRBs from TCRdb Blacklist can be given to exclude certain sequences from sampling The following tests ensure reproducibility >>> all([a == b for a, b in zip(sample_unlabelled_tcrdb_trb(10), sample_unlabelled_tcrdb_trb(10))]) True """ tcrdb = load_tcrdb() rng = np.random.default_rng(seed=seed) if blacklist is None: # Maintain separate code paths for back compatibility idx = rng.choice(np.arange(len(tcrdb)), size=n, replace=False) else: # Removing whitespace has no effect if there was no whitespace to start blacklist_set = set([ft.remove_whitespace(b) for b in blacklist]) # Oversample in case of overlap with blacklist idx = rng.choice(np.arange(len(tcrdb)), size=n + len(blacklist), replace=False) # Filter out blacklist overlaps and take the first n idx = [i for i in idx if tcrdb.iloc[i]["AASeq"] not in blacklist_set][:n] assert len(idx) == n retval = [tcrdb.iloc[i]["AASeq"] for i in idx] if blacklist: # No overlap assert not set(retval).intersection(blacklist) return retval def load_lcmv_vdj( vdj_fname: str = os.path.join(LOCAL_DATA_DIR, "lcmv_tcr_vdj_unsplit.txt.gz") ) -> Dict[str, Dict[str, Dict[str, Tuple[str, str, str]]]]: """ Load the vdj table and return it in a 3-level dictionary identifier -> TRA/TRB -> cdr3 sequence -> (v, d, j) { tcr_cdr3s_aa_identifier : { "TRA": { cdr3_sequence: (v, d, j), ... } "TRB": ... } } v d or j may be None if not provided """ check_none: Callable[ [str], Union[str, None] ] = lambda x: None if x.lower() == "none" or not x else x df = pd.read_csv(vdj_fname, delimiter="\t", low_memory=False) retval = collections.defaultdict(lambda: {"TRA": dict(), "TRB": dict()}) for i, row in df.iterrows(): k = row["tcr_cdr3s_aa"] retval[k][row["chain"]][row["cdr3"]] = ( check_none(row["v_gene"]), check_none(row["d_gene"]), check_none(row["j_gene"]), ) return retval @functools.lru_cache(maxsize=16) def load_lcmv_table( fname: str = os.path.join(LOCAL_DATA_DIR, "lcmv_tetramer_tcr.txt"), metadata_fname: str = os.path.join(LOCAL_DATA_DIR, "lcmv_all_metadata.txt.gz"), vdj_fname: str = os.path.join(LOCAL_DATA_DIR, "lcmv_tcr_vdj_unsplit.txt.gz"), drop_na: bool = True, drop_unsorted: bool = True, ) -> pd.DataFrame: """Load the LCMV data table""" table = pd.read_csv(fname, delimiter="\t") logging.info(f"Loaded in table of {len(table)} entries") if drop_na: table.dropna(axis=0, how="any", subset=["tetramer", "TRB", "TRA"], inplace=True) logging.info(f"{len(table)} entries remain after dropping na") if drop_unsorted: drop_idx = table.index[table["tetramer"] == "Unsorted"] table.drop(index=drop_idx, inplace=True) logging.info(f"{len(table)} entries remain after dropping unsorted") # Take entires with multiple TRA or TRB sequences and split them, carrying over # all of the other metadata to each row dedup_rows = [] for _i, row in table.iterrows(): # For this row, determine nucleotide sequences tcr_nt = collect_tra_trb(row["tcr_cdr3s_nt"]) # The nucleotide and the protein sequences should match up correctly tcr_aa_combos = list( itertools.product(row["TRA"].split(";"), row["TRB"].split(";")) ) tcr_nt_combos = list(itertools.product(tcr_nt["TRA"], tcr_nt["TRB"])) assert len(tcr_aa_combos) == len(tcr_nt_combos) for ((tra_aa, trb_aa), (tra_nt, trb_nt)) in zip(tcr_aa_combos, tcr_nt_combos): new_row = row.copy(deep=True) # Check that nucleotide and protein sequences match up assert utils.nt2aa(tra_nt) == tra_aa assert utils.nt2aa(trb_nt) == trb_aa new_row["TRA"] = tra_aa new_row["TRB"] = trb_aa new_row["TRA_nt"] = tra_nt new_row["TRB_nt"] = trb_nt dedup_rows.append(new_row) dedup_table = pd.DataFrame(dedup_rows) logging.info(f"{len(dedup_table)} entries after expanding multiple entries") gp33_antigen = utils.read_newline_file( os.path.join(os.path.dirname(fname), "lcmv_antigen.txt") ).pop() dedup_table["antigen.sequence"] = gp33_antigen # gp33 tetramer # Load metadata and match it up with prior table metadata_df = pd.read_csv(metadata_fname, delimiter="\t", low_memory=False) if drop_na: metadata_df.dropna(axis=0, how="any", subset=["TRA", "TRB"], inplace=True) table_ab_pairs = list(dedup_table["tcr_cdr3s_aa"]) metadata_ab_pairs = list(metadata_df["tcr_cdr3s_aa"]) idx_map = np.array([metadata_ab_pairs.index(p) for p in table_ab_pairs]) metadata_df_reorder = metadata_df.iloc[idx_map] assert ( all( [ i == j for i, j in zip( metadata_df_reorder["tcr_cdr3s_aa"], dedup_table["tcr_cdr3s_aa"] ) ] ) and metadata_df_reorder.shape[0] == dedup_table.shape[0] ) metadata_df_reorder = metadata_df_reorder.drop( columns=[ col for col in metadata_df_reorder.columns if col in dedup_table.columns ] ) metadata_df_reorder.index = dedup_table.index # Load in VDJ annotations and match it up with prior table vdj_mapping = load_lcmv_vdj(vdj_fname) # 3 layer dict vdj_df_reorder_rows = [] for i, row in dedup_table.iterrows(): b_vdj = vdj_mapping[row["tcr_cdr3s_aa"]]["TRB"][row["TRB"]] a_vdj = vdj_mapping[row["tcr_cdr3s_aa"]]["TRA"][row["TRA"]] s = pd.Series( [row["tcr_cdr3s_aa"], *a_vdj, *b_vdj], index=[ "tcr_cdr3s_aa", "v_a_gene", "d_a_gene", "j_a_gene", "v_b_gene", "d_b_gene", "j_b_gene", ], ) vdj_df_reorder_rows.append(s) vdj_df_reorder = pd.DataFrame(vdj_df_reorder_rows) assert all( [ i == j for i, j in zip(vdj_df_reorder["tcr_cdr3s_aa"], dedup_table["tcr_cdr3s_aa"]) ] ) vdj_df_drop_cols = [ col for col in vdj_df_reorder.columns if col in dedup_table.columns or col in metadata_df_reorder.columns ] logging.debug(f"Dropping cols from VDJ info: {vdj_df_drop_cols}") vdj_df_reorder = vdj_df_reorder.drop(columns=vdj_df_drop_cols) vdj_df_reorder.index = dedup_table.index retval = pd.concat([dedup_table, metadata_df_reorder, vdj_df_reorder], axis=1) # Check that the TRA/TRB are the same as the "dedup_table" object that we were previously returning assert all([i == j for i, j in zip(retval["TRA"], dedup_table["TRA"])]) assert all([i == j for i, j in zip(retval["TRB"], dedup_table["TRB"])]) # Report basic metadata cnt = collections.Counter(dedup_table["tetramer"]) for k, v in cnt.items(): logging.info(f"Class {k}: {v}") return retval def dedup_lcmv_table( lcmv_tab: pd.DataFrame, blacklist_label_combos: Sequence[str] = ( "TetMid,TetNeg", "TetNeg,TetPos", "TetMid,TetNeg,TetPos", ), return_mode: Literal["nt", "aa", "full"] = "aa", ) -> Tuple[Union[List[Tuple[str, str]], pd.DataFrame], List[str]]: """ Return TRA and TRB pairs that are deduped according to their AA sequence and removes pairs with ambiguous labels This was implemented to centrally solve the issue where the LCMV table had duplicate rows and a few cases of ambiguous labels Returns two items of equal length: - List of (TRA, TRB) pairs either in AA form or NT form, or a subset of the full dataframe - List of corresponding labels (may be merged) """ lcmv_ab = ["|".join(p) for p in zip(lcmv_tab["TRA"], lcmv_tab["TRB"])] # Create a mapping from amino acid to NT sequence lcmv_ab_to_nt = { n: "|".join(p) for n, p in zip(lcmv_ab, zip(lcmv_tab["TRA_nt"], lcmv_tab["TRB_nt"])) } lcmv_ab_to_full = { "|".join([row["TRA"], row["TRB"]]): row for i, row in lcmv_tab.iterrows() } lcmv_ab_dedup, lcmv_labels_dedup = dedup_and_merge_labels( lcmv_ab, list(lcmv_tab["tetramer"]) ) all_label_counter = collections.Counter(lcmv_labels_dedup) logging.info(f"Combined labels {all_label_counter.most_common()}") logging.info(f"Filtering out labels {blacklist_label_combos}") good_label_idx = [ i for i, l in enumerate(lcmv_labels_dedup) if l not in blacklist_label_combos ] logging.info(f"Retaining {len(good_label_idx)} pairs with unambiguous labels") lcmv_ab_good = [lcmv_ab_dedup[i] for i in good_label_idx] lcmv_labels_good = [lcmv_labels_dedup[i] for i in good_label_idx] assert len(lcmv_ab_good) == len(lcmv_labels_good) == len(good_label_idx) label_counter = collections.Counter(lcmv_labels_good) logging.info(f"LCMV deduped labels: {label_counter.most_common()}") # Resplit into pairs if return_mode == "nt": lcmv_ab_good_split = [tuple(lcmv_ab_to_nt[p].split("|")) for p in lcmv_ab_good] elif return_mode == "aa": lcmv_ab_good_split = [tuple(p.split("|")) for p in lcmv_ab_good] elif return_mode == "full": lcmv_ab_good_split = pd.DataFrame([lcmv_ab_to_full[p] for p in lcmv_ab_good]) else: raise ValueError(f"Unrecognized return mode: {return_mode}") return lcmv_ab_good_split, lcmv_labels_good def dedup_lcmv_table_trb_only( lcmv_tab: pd.DataFrame, blacklist_label_combos: Sequence[str] = ( "TetMid,TetNeg", "TetNeg,TetPos", "TetMid,TetNeg,TetPos", ), ) -> Tuple[List[str], List[str]]: """ Return a list of unique TRBs and corresponding set of labels """ trb_dedup, labels_dedup = dedup_and_merge_labels( list(lcmv_tab["TRB"]), list(lcmv_tab["tetramer"]) ) assert utils.is_all_unique(trb_dedup) all_label_counter = collections.Counter(labels_dedup) logging.info(f"Combined labels {all_label_counter.most_common()}") logging.info(f"Filtering out labels {blacklist_label_combos}") good_label_idx = [ i for i, l in enumerate(labels_dedup) if l not in blacklist_label_combos ] logging.info(f"Retaining {len(good_label_idx)} sequences with unambiguous labels") trb_good = [trb_dedup[i] for i in good_label_idx] labels_good = [labels_dedup[i] for i in good_label_idx] assert len(trb_good) == len(labels_good) == len(good_label_idx) label_counter = collections.Counter(labels_good) logging.info(f"LCMV TRB only deduped labels: {label_counter.most_common()}") return trb_good, labels_good def load_vdjdb( fname: str = os.path.join(LOCAL_DATA_DIR, "vdjdb-2021-02-02", "vdjdb.slim.txt"), species_filter: Optional[Iterable[str]] = ["MusMusculus", "HomoSapiens"], tra_trb_filter: Optional[Iterable[str]] = ["TRA", "TRB"], addtl_filters: Optional[Dict[str, Iterable[str]]] = None, drop_null: bool = True, vocab_check: bool = True, ) -> pd.DataFrame: """ Load VDJdb as a dataframe. 'cdr3' column is the column containing sequences ~62k examples, spanning 352 distinct antigens Additional filters can be provided in the format {column_name: ['acceptableValue1', 'acceptableValue2', ...]} """ df = pd.read_csv(fname, sep="\t") if species_filter is not None: logging.info(f"Filtering VDJdb species to: {species_filter}") keep_idx = [i for i in df.index if df.loc[i, "species"] in species_filter] df = df.loc[keep_idx] logging.info(f"Species distribution: {collections.Counter(df['species'])}") if drop_null: keep_idx = [~pd.isnull(aa) for aa in df["cdr3"]] logging.info( f"VDJdb: dropping {np.sum(keep_idx==False)} entries for null cdr3 sequence" ) df = df.iloc[np.where(keep_idx)] if vocab_check: pass_idx = np.array([ft.adheres_to_vocab(aa) for aa in df["cdr3"]]) logging.info( f"VDJdb: dropping {np.sum(pass_idx==False)} entries for unrecognized AAs" ) df = df.iloc[np.where(pass_idx)] nonnull_antigens_df = df.loc[~pd.isnull(df["antigen.epitope"])] logging.info( f"Entries with antigen sequence: {nonnull_antigens_df.shape[0]}/{df.shape[0]}" ) logging.info( f"Unique antigen sequences: {len(set(nonnull_antigens_df['antigen.epitope']))}" ) if tra_trb_filter is not None: logging.info(f"Filtering TRA/TRB to: {tra_trb_filter}") keep_idx = [i for i in df.index if df.loc[i, "gene"] in tra_trb_filter] df = df.loc[keep_idx] # For each of the additional fitlers if addtl_filters is not None: for colname, keep_vals in addtl_filters.items(): logging.info(f"Filtering {colname} to {keep_vals}") keep_idx = [i for i in df.index if df.loc[i, colname] in keep_vals] df = df.loc[keep_idx] ab_counter = collections.Counter(df["gene"]) logging.info(f"TRA: {ab_counter['TRA']} | TRB: {ab_counter['TRB']}") return df def load_pird( fname: str = os.path.join(LOCAL_DATA_DIR, "pird", "pird_tcr_ab.csv"), tra_trb_only: bool = True, vocab_check: bool = True, addtl_filters: Optional[Dict[str, Iterable[str]]] = None, with_antigen_only: bool = False, ) -> pd.DataFrame: """ Load PIRD (pan immune repertoire database) TCR A/B data https://db.cngb.org/pird/tbadb/ For TRA we want the column CDR3.alpha.aa For TRB we want the column CDR3.beta.aa The PIRD dataset also has ~8k examples with antigens (73 unique) """ if not tra_trb_only: raise NotImplementedError df = pd.read_csv(fname, na_values="-", low_memory=False) # df_orig = pd.read_csv(fname, na_values="-", low_memory=False) # df = df_orig.dropna(axis=0, how="all", subset=["CDR3.alpha.aa", "CDR3.beta.aa"]) # logging.info( # f"Dropped {len(df_orig) - len(df)} entires with null sequence in both TRA/TRB" # ) antigen_null_rate = np.sum(pd.isnull(df["Antigen.sequence"])) / df.shape[0] logging.info( f"PIRD data {1.0 - antigen_null_rate:.4f} data labelled with antigen sequence" ) # Filter out entries that have weird characters in their aa sequences if vocab_check: tra_pass = [ pd.isnull(aa) or ft.adheres_to_vocab(aa) for aa in df["CDR3.alpha.aa"] ] trb_pass = [
pd.isnull(aa)
pandas.isnull
""" ******************************************************************************** * Name: spatial_dataset_mwv_tests.py * Author: mlebaron * Created On: August 15, 2019 * Copyright: (c) Aquaveo 2019 ******************************************************************************** """ from unittest import mock import pandas as pd from django.http import HttpRequest, HttpResponse, HttpResponseRedirect, JsonResponse, QueryDict from tethysext.atcore.controllers.resource_workflows.map_workflows.spatial_dataset_mwv import SpatialDatasetMWV from tethysext.atcore.models.resource_workflow_steps.spatial_dataset_rws import SpatialDatasetRWS from tethysext.atcore.models.app_users.resource import Resource from tethysext.atcore.services.model_database import ModelDatabase from tethysext.atcore.tests.integrated_tests.controllers.resource_workflows.workflow_view_test_case import \ WorkflowViewTestCase from tethysext.atcore.tests.utilities.sqlalchemy_helpers import setup_module_for_sqlalchemy_tests, \ tear_down_module_for_sqlalchemy_tests def setUpModule(): setup_module_for_sqlalchemy_tests() def tearDownModule(): tear_down_module_for_sqlalchemy_tests() class SpatialDatasetMwvTests(WorkflowViewTestCase): def setUp(self): super().setUp() self.request = mock.MagicMock(spec=HttpRequest) self.request.GET = {'feature_id': 'feature1'} self.request.POST = QueryDict('next-submit') self.request.method = 'method1' self.request.path = 'path' self.request.META = {} self.resource = Resource() self.back_url = './back' self.next_url = './next' self.current_url = './current' self.step1 = SpatialDatasetRWS( geoserver_name='geo_server', map_manager=mock.MagicMock(), spatial_manager=mock.MagicMock(), name='name1', help='help1', order=1, options={ 'max_rows': 1000, 'template_dataset':
pd.DataFrame(columns=['Time (min)'])
pandas.DataFrame
# JACSNET Evaluation # Author: <NAME> 04.11.19 # get libraries import sys import numpy as np import pandas as pd import scipy import csv import matplotlib.pyplot as plt import tensorflow as tf import itertools import librosa import librosa.display import keras from keras.models import Model from keras.layers import * from keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau from keras import optimizers import keras.backend as K import museval import musdb import norbert from sklearn.metrics import confusion_matrix, multilabel_confusion_matrix, roc_auc_score, accuracy_score, roc_curve, precision_recall_curve, f1_score, auc, average_precision_score from scipy.optimize import brentq from scipy.interpolate import interp1d from model import UNETmodule, RECOVmodule from utils import checkNoise def custom_loss_wrapper_a(mask): def custom_loss_a(y_true, y_pred): mae = K.mean(K.abs(np.multiply(mask, y_pred) - y_true), axis=-1) y_true = K.clip(y_true, K.epsilon(), 1) y_pred = K.clip(np.multiply(mask, y_pred), K.epsilon(), 1) KL = K.sum(y_true * K.log(y_true / y_pred), axis=-1) return mae + (0.5*KL) return custom_loss_a def binary_focal_loss(gamma=2., alpha=.25): """ Binary form of focal loss. FL(p_t) = -alpha * (1 - p_t)**gamma * log(p_t) where p = sigmoid(x), p_t = p or 1 - p depending on if the label is 1 or 0, respectively. References: https://arxiv.org/pdf/1708.02002.pdf Usage: model.compile(loss=[binary_focal_loss(alpha=.25, gamma=2)], metrics=["accuracy"], optimizer=adam) """ def binary_focal_loss_fixed(y_true, y_pred): """ :param y_true: A tensor of the same shape as `y_pred` :param y_pred: A tensor resulting from a sigmoid :return: Output tensor. """ pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred)) pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred)) epsilon = K.epsilon() # clip to prevent NaN's and Inf's pt_1 = K.clip(pt_1, epsilon, 1. - epsilon) pt_0 = K.clip(pt_0, epsilon, 1. - epsilon) return -K.sum(alpha * K.pow(1. - pt_1, gamma) * K.log(pt_1)) \ -K.sum((1 - alpha) * K.pow(pt_0, gamma) * K.log(1. - pt_0)) return binary_focal_loss_fixed def mulphase(inp, phase): voc_spec = np.abs(inp) voc_spec = np.multiply(voc_spec, phase) return voc_spec def roc_auc_score_FIXED(y_true, y_pred): if len(np.unique(y_true)) == 1: # bug in roc_auc_score return accuracy_score(y_true, np.rint(y_pred)) return roc_auc_score(y_true, y_pred) def eval_metrics(y_true, y_pred): fpr, tpr, threshold = roc_curve(y_true, y_pred, pos_label=1) eer = brentq(lambda x : 1. - x - interp1d(fpr, tpr)(x), 0., 1.) auc_res = roc_auc_score_FIXED(y_true, y_pred) return auc_res, eer def plot_confusion_matrix(cm, classes, title, ax): ax.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues) for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): ax.text(j, i, cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > cm.max() / 2. else "black") tick_marks = np.arange(len(classes)) ax.set_xticks(tick_marks), ax.xaxis.set_ticklabels(classes) ax.set_yticks(tick_marks), ax.yaxis.set_ticklabels(classes) ax.set_xlabel('Predicted') ax.set_ylabel('Truth') ax.set_title(title) ax.grid(False) def plot_multiclass_confusion_matrix(y_true, y_pred, label_to_class, save_plot=False): fig, axes = plt.subplots(int(np.ceil(len(label_to_class) / 2)), 2, figsize=(5, 5)) axes = axes.flatten() for i, conf_matrix in enumerate(multilabel_confusion_matrix(y_true, y_pred)): tn, fp, fn, tp = conf_matrix.ravel() f1 = 2 * tp / (2 * tp + fp + fn + sys.float_info.epsilon) recall = tp / (tp + fn + sys.float_info.epsilon) precision = tp / (tp + fp + sys.float_info.epsilon) plot_confusion_matrix( np.array([[tp, fn], [fp, tn]]), classes=['+', '-'], title=f'Label: {label_to_class[i]}\nf1={f1:.5f}\nrecall={recall:.5f}\nprecision={precision:.5f}', ax=axes[i] ) plt.tight_layout() plt.show() if save_plot: plt.savefig('confusion_matrices.png', dpi=50) def main(args): # Parameters seed = 3 num_classes = 4 num_epochs = 100 drop_prob = 0 learning_rate = 1e-4 window_size = 2048 hop_size = 512 window = np.blackman(window_size) # Model Architecture inputs = Input(shape=[1025, 94, 1]) UNETout1 = UNETmodule(inputs, num_classes, drop_prob) sep_sources = Activation('softmax', name='sep_sources')(UNETout1) UNETout2 = UNETmodule(sep_sources, 1, drop_prob) recov_input = Activation('sigmoid', name='recov_input')(UNETout2) sourceclass = GlobalAveragePooling2D()(sep_sources) sourceclass = Dense(128, activation='relu')(sourceclass) sourceclass = Dense(128, activation='relu')(sourceclass) sourceclass = Dense(128, activation='relu')(sourceclass) sourceclass = Dense(num_classes)(sourceclass) sourceclass = Activation('sigmoid', name='sourceclass')(sourceclass) # Train Model Architecture loss_funcs = { "sep_sources": custom_loss_wrapper_a(mask = inputs), "sourceclass": binary_focal_loss(), "recov_input": custom_loss_wrapper_a(mask = inputs) } lossWeights = {"sep_sources": 10, "sourceclass": 0.01, "recov_input": 10} optimizer = optimizers.Adam(lr=learning_rate) reduce_lr = ReduceLROnPlateau(monitor='val_loss', patience=5) model = Model(inputs=inputs, outputs=[sep_sources, sourceclass, recov_input]) model.compile(loss=loss_funcs, optimizer=optimizer, loss_weights=lossWeights) # Load Model model.load_weights("model_weightsv2.hdf5") # initiate musdb mus = musdb.DB(root_dir="D:/Data/Musdb18") # load the testing tracks tracks = mus.load_mus_tracks(subsets=['test']) SDR_results = [] SIR_results = [] ISR_results = [] SAR_results = [] classresults = [] classresults_hat = [] labelresults = [] duration = 3 for track in tracks: print(track.name) audio, sr = librosa.load(str('D:/Data/Musdb18/mixturev2/test/' + track.name + '.wav')) # audio = librosa.util.normalize(audio, norm=np.inf, axis=None) dur = librosa.get_duration(y=audio, sr=sr) for i in range(0, int(dur - duration), duration): labelclass = [0, 0, 0, 0] # STFT Mixture mixture, sr = librosa.load(str('D:/Data/Musdb18/mixturev2/test/' + track.name + '.wav'), offset=i, duration=duration, mono=True, sr=16000) orig = librosa.core.stft(mixture, hop_length=hop_size, n_fft=window_size, window=window) magnitude, phase = librosa.magphase(orig) orig_norm = 2 * magnitude / np.sum(window) X = np.reshape(orig_norm, (1, 1025, 94, 1)) X = X.astype('float32') (sources, sourceclass, inp) = model.predict(X, batch_size=1) classresults.append(sourceclass) sourceclass_hat = np.around(sourceclass, decimals = 1) sourceclass_hat[sourceclass_hat <= 0.5] = 0 sourceclass_hat[sourceclass_hat > 0.5] = 1 classresults_hat.append(sourceclass_hat) sources_reshape = np.reshape(sources, (1025, 94, 1, 4)) orig_reshape = np.reshape(orig, (1025, 94, 1)) source_spec = norbert.wiener(sources_reshape, orig_reshape, use_softmask=True) inp_reshape = np.reshape(inp, (1025, 94, 1, 1)) inp = norbert.wiener(inp_reshape, orig_reshape, use_softmask=False) inp_spec = np.reshape(inp, (1025, 94)) #inp_spec = mulphase(target_pred_mag_inp, phase) voc_spec = np.reshape(source_spec[:,:,:,0], (1025, 94)) bas_spec = np.reshape(source_spec[:,:,:,1], (1025, 94)) dru_spec = np.reshape(source_spec[:,:,:,2], (1025, 94)) oth_spec = np.reshape(source_spec[:,:,:,3], (1025, 94)) # Get ground truth gt_voc, sr = librosa.load(str('D:/Data/Musdb18/groundtruth/test/' + track.name + '/vocals.wav'), offset=i, duration=3, mono=True, sr=16000) if ((checkNoise(gt_voc) > 0.05)): labelclass[0] = 1 # gt_voc_down = scipy.signal.decimate(gt_voc, 2) gt_voc_final = np.reshape(gt_voc, (1, gt_voc.shape[0], 1)) gt_bas, sr = librosa.load(str('D:/Data/Musdb18/groundtruth/test/' + track.name + '/bass.wav'), offset=i, duration=3, mono=True, sr=16000) if ((checkNoise(gt_bas) > 0.05)): labelclass[1] = 1 # gt_bas_down = scipy.signal.decimate(gt_bas, 2) gt_bas_final = np.reshape(gt_bas, (1, gt_bas.shape[0], 1)) gt_dru, sr = librosa.load(str('D:/Data/Musdb18/groundtruth/test/' + track.name + '/drums.wav'), offset=i, duration=3, mono=True, sr=16000) if ((checkNoise(gt_dru) > 0.05)): labelclass[2] = 1 # gt_dru_down = scipy.signal.decimate(gt_dru, 2) gt_dru_final = np.reshape(gt_dru, (1, gt_dru.shape[0], 1)) gt_others, sr = librosa.load(str('D:/Data/Musdb18/groundtruth/test/' + track.name + '/other.wav'), offset=i, duration=3, mono=True, sr=16000) if ((checkNoise(gt_others) > 0.05)): labelclass[3] = 1 # gt_others_down = scipy.signal.decimate(gt_others, 2) gt_others_final = np.reshape(gt_others, (1, gt_others.shape[0], 1)) gt_inp_final = np.reshape(mixture, (1, mixture.shape[0], 1)) labelresults.append(labelclass) # Get predictions vocals = librosa.core.istft(voc_spec, hop_length=hop_size, length=gt_voc_final.shape[1], window=window, win_length=window_size) # vocals = scipy.signal.wiener(vocals) vocals = np.reshape(vocals, (1, vocals.shape[0], 1)) bass = librosa.core.istft(bas_spec, hop_length=hop_size, length=gt_bas_final.shape[1], window=window, win_length=window_size) # bass = scipy.signal.wiener(bass) bass = np.reshape(bass, (1, bass.shape[0], 1)) drums = librosa.core.istft(dru_spec, hop_length=hop_size, length=gt_dru_final.shape[1], window=window, win_length=window_size) # drums = scipy.signal.wiener(drums) drums = np.reshape(drums, (1, drums.shape[0], 1)) others = librosa.core.istft(oth_spec, hop_length=hop_size, length=gt_others_final.shape[1], window=window, win_length=window_size) # others = scipy.signal.wiener(others) others = np.reshape(others, (1, others.shape[0], 1)) # if(sourceclass[0][0] <= 0.5): # vocals = np.zeros((gt_voc_final.shape[1], 1)) # # vocals.fill(0.04) # vocals = np.reshape(vocals, (1, vocals.shape[0], 1)) # else: # vocals = librosa.core.istft(voc_spec, hop_length=hop_size, length=gt_voc_final.shape[1], window=window) # # vocals = scipy.signal.wiener(vocals) # vocals = np.reshape(vocals, (1, vocals.shape[0], 1)) # # if(sourceclass[0][1] <= 0.5): # bass = np.zeros((gt_bas_final.shape[1], 1)) # # bass.fill(0.04) # bass = np.reshape(bass, (1, bass.shape[0], 1)) # else: # bass = librosa.core.istft(bas_spec, hop_length=hop_size, length=gt_bas_final.shape[1], window=window) # # bass = scipy.signal.wiener(bass) # bass = np.reshape(bass, (1, bass.shape[0], 1)) # # if(sourceclass[0][2] <= 0.5): # drums = np.zeros((gt_dru_final.shape[1], 1)) # # drums.fill(0.04) # drums = np.reshape(drums, (1, drums.shape[0], 1)) # else: # drums = librosa.core.istft(dru_spec, hop_length=hop_size, length=gt_dru_final.shape[1], window=window) # # drums = scipy.signal.wiener(drums) # drums = np.reshape(drums, (1, drums.shape[0], 1)) # # if(sourceclass[0][3] <= 0.5): # others = np.zeros((gt_others_final.shape[1], 1)) # # others.fill(0.04) # others = np.reshape(others, (1, others.shape[0], 1)) # else: # others = librosa.core.istft(oth_spec, hop_length=hop_size, length=gt_others_final.shape[1], window=window) # # others = scipy.signal.wiener(others) # others = np.reshape(others, (1, others.shape[0], 1)) recov = librosa.core.istft(inp_spec, hop_length=hop_size, length=gt_inp_final.shape[1], window=window, win_length=window_size) # recov = scipy.signal.wiener(recov) recov = np.reshape(recov, (1, recov.shape[0], 1)) all_zeros = np.all(gt_voc<=0) or np.all(gt_bas<=0) or np.all(gt_dru<=0) or np.all(gt_others<=0) or np.all(vocals<=0) or np.all(bass<=0) or np.all(drums<=0) or np.all(others<=0) # print(all_zeros) if all_zeros == False: # noise_thresh = checkNoise(gt_voc) # # if noise_thresh > 0.05: # noise_thresh = checkNoise(gt_bas) # # if noise_thresh > 0.05: # noise_thresh = checkNoise(gt_dru) # # if noise_thresh > 0.05: # Evaluate REF = np.concatenate((gt_voc_final, gt_bas_final, gt_dru_final, gt_others_final, gt_inp_final), axis=0) EST = np.concatenate((vocals, bass, drums, others, recov), axis=0) [SDR, ISR, SIR, SAR] = museval.evaluate(REF, EST, win=52565, hop=52565) SDR_results.append(SDR) ISR_results.append(ISR) SIR_results.append(SIR) SAR_results.append(SAR) y_true = np.array(labelresults, dtype=float) y_pred = np.array(classresults, dtype=float) y_pred = np.reshape(y_pred, (y_pred.shape[0], y_pred.shape[2])) y_pred_hat = np.array(classresults_hat, dtype=float) y_pred_hat = np.reshape(y_pred_hat, (y_pred_hat.shape[0], y_pred_hat.shape[2])) auc_voc, eer_voc = eval_metrics(y_true[:,0], y_pred[:,0]) auc_bas, eer_bas = eval_metrics(y_true[:,1], y_pred[:,1]) auc_dru, eer_dru = eval_metrics(y_true[:,2], y_pred[:,2]) auc_oth, eer_oth = eval_metrics(y_true[:,3], y_pred[:,3]) target_names = ['Vocals', 'Bass', 'Drums', 'Others'] plot_multiclass_confusion_matrix(y_true, y_pred_hat, target_names, save_plot=False) SDR_array = np.array(SDR_results) SDR_array = np.reshape(SDR_array, (SDR_array.shape[0], SDR_array.shape[1])) SDR_df = pd.DataFrame(SDR_array) SDR_df.to_csv('SDR_revise1.csv') ISR_array = np.array(ISR_results) ISR_array = np.reshape(ISR_array, (ISR_array.shape[0], ISR_array.shape[1])) ISR_df = pd.DataFrame(ISR_array) ISR_df.to_csv('ISR_revise1.csv') SIR_array = np.array(SIR_results) SIR_array = np.reshape(SIR_array, (SIR_array.shape[0], SIR_array.shape[1])) SIR_df = pd.DataFrame(SIR_array) SIR_df.to_csv('SIR_revise1.csv') SAR_array = np.array(SAR_results) SAR_array = np.reshape(SAR_array, (SAR_array.shape[0], SAR_array.shape[1])) SAR_df =
pd.DataFrame(SAR_array)
pandas.DataFrame
import numpy as np import pandas as pd from numba import njit from datetime import datetime import pytest from itertools import product from sklearn.model_selection import TimeSeriesSplit import vectorbt as vbt from vectorbt.generic import nb seed = 42 day_dt = np.timedelta64(86400000000000) df = pd.DataFrame({ 'a': [1, 2, 3, 4, np.nan], 'b': [np.nan, 4, 3, 2, 1], 'c': [1, 2, np.nan, 2, 1] }, index=pd.DatetimeIndex([ datetime(2018, 1, 1), datetime(2018, 1, 2), datetime(2018, 1, 3), datetime(2018, 1, 4), datetime(2018, 1, 5) ])) group_by = np.array(['g1', 'g1', 'g2']) @njit def i_or_col_pow_nb(i_or_col, x, pow): return np.power(x, pow) @njit def pow_nb(x, pow): return np.power(x, pow) @njit def nanmean_nb(x): return np.nanmean(x) @njit def i_col_nanmean_nb(i, col, x): return np.nanmean(x) @njit def i_nanmean_nb(i, x): return np.nanmean(x) @njit def col_nanmean_nb(col, x): return np.nanmean(x) # ############# accessors.py ############# # class TestAccessors: def test_shuffle(self): pd.testing.assert_series_equal( df['a'].vbt.shuffle(seed=seed), pd.Series( np.array([2.0, np.nan, 3.0, 1.0, 4.0]), index=df['a'].index, name=df['a'].name ) ) np.testing.assert_array_equal( df['a'].vbt.shuffle(seed=seed).values, nb.shuffle_1d_nb(df['a'].values, seed=seed) ) pd.testing.assert_frame_equal( df.vbt.shuffle(seed=seed), pd.DataFrame( np.array([ [2., 2., 2.], [np.nan, 4., 1.], [3., 3., 2.], [1., np.nan, 1.], [4., 1., np.nan] ]), index=df.index, columns=df.columns ) ) @pytest.mark.parametrize( "test_value", [-1, 0., np.nan], ) def test_fillna(self, test_value): pd.testing.assert_series_equal(df['a'].vbt.fillna(test_value), df['a'].fillna(test_value)) pd.testing.assert_frame_equal(df.vbt.fillna(test_value), df.fillna(test_value)) @pytest.mark.parametrize( "test_n", [1, 2, 3, 4, 5], ) def test_bshift(self, test_n): pd.testing.assert_series_equal(df['a'].vbt.bshift(test_n), df['a'].shift(-test_n)) np.testing.assert_array_equal( df['a'].vbt.bshift(test_n).values, nb.bshift_nb(df['a'].values, test_n) ) pd.testing.assert_frame_equal(df.vbt.bshift(test_n), df.shift(-test_n)) @pytest.mark.parametrize( "test_n", [1, 2, 3, 4, 5], ) def test_fshift(self, test_n): pd.testing.assert_series_equal(df['a'].vbt.fshift(test_n), df['a'].shift(test_n)) np.testing.assert_array_equal( df['a'].vbt.fshift(test_n).values, nb.fshift_1d_nb(df['a'].values, test_n) ) pd.testing.assert_frame_equal(df.vbt.fshift(test_n), df.shift(test_n)) def test_diff(self): pd.testing.assert_series_equal(df['a'].vbt.diff(), df['a'].diff()) np.testing.assert_array_equal(df['a'].vbt.diff().values, nb.diff_1d_nb(df['a'].values)) pd.testing.assert_frame_equal(df.vbt.diff(), df.diff()) def test_pct_change(self): pd.testing.assert_series_equal(df['a'].vbt.pct_change(), df['a'].pct_change(fill_method=None)) np.testing.assert_array_equal(df['a'].vbt.pct_change().values, nb.pct_change_1d_nb(df['a'].values)) pd.testing.assert_frame_equal(df.vbt.pct_change(), df.pct_change(fill_method=None)) def test_ffill(self): pd.testing.assert_series_equal(df['a'].vbt.ffill(), df['a'].ffill()) pd.testing.assert_frame_equal(df.vbt.ffill(), df.ffill()) def test_product(self): assert df['a'].vbt.product() == df['a'].product() np.testing.assert_array_equal(df.vbt.product(), df.product()) def test_cumsum(self): pd.testing.assert_series_equal(df['a'].vbt.cumsum(), df['a'].cumsum()) pd.testing.assert_frame_equal(df.vbt.cumsum(), df.cumsum()) def test_cumprod(self): pd.testing.assert_series_equal(df['a'].vbt.cumprod(), df['a'].cumprod()) pd.testing.assert_frame_equal(df.vbt.cumprod(), df.cumprod()) @pytest.mark.parametrize( "test_window,test_minp", list(product([1, 2, 3, 4, 5], [1, None])) ) def test_rolling_min(self, test_window, test_minp): if test_minp is None: test_minp = test_window pd.testing.assert_series_equal( df['a'].vbt.rolling_min(test_window, minp=test_minp), df['a'].rolling(test_window, min_periods=test_minp).min() ) pd.testing.assert_frame_equal( df.vbt.rolling_min(test_window, minp=test_minp), df.rolling(test_window, min_periods=test_minp).min() ) pd.testing.assert_frame_equal( df.vbt.rolling_min(test_window), df.rolling(test_window).min() ) @pytest.mark.parametrize( "test_window,test_minp", list(product([1, 2, 3, 4, 5], [1, None])) ) def test_rolling_max(self, test_window, test_minp): if test_minp is None: test_minp = test_window pd.testing.assert_series_equal( df['a'].vbt.rolling_max(test_window, minp=test_minp), df['a'].rolling(test_window, min_periods=test_minp).max() ) pd.testing.assert_frame_equal( df.vbt.rolling_max(test_window, minp=test_minp), df.rolling(test_window, min_periods=test_minp).max() ) pd.testing.assert_frame_equal( df.vbt.rolling_max(test_window), df.rolling(test_window).max() ) @pytest.mark.parametrize( "test_window,test_minp", list(product([1, 2, 3, 4, 5], [1, None])) ) def test_rolling_mean(self, test_window, test_minp): if test_minp is None: test_minp = test_window pd.testing.assert_series_equal( df['a'].vbt.rolling_mean(test_window, minp=test_minp), df['a'].rolling(test_window, min_periods=test_minp).mean() ) pd.testing.assert_frame_equal( df.vbt.rolling_mean(test_window, minp=test_minp), df.rolling(test_window, min_periods=test_minp).mean() ) pd.testing.assert_frame_equal( df.vbt.rolling_mean(test_window), df.rolling(test_window).mean() ) @pytest.mark.parametrize( "test_window,test_minp,test_ddof", list(product([1, 2, 3, 4, 5], [1, None], [0, 1])) ) def test_rolling_std(self, test_window, test_minp, test_ddof): if test_minp is None: test_minp = test_window pd.testing.assert_series_equal( df['a'].vbt.rolling_std(test_window, minp=test_minp, ddof=test_ddof), df['a'].rolling(test_window, min_periods=test_minp).std(ddof=test_ddof) ) pd.testing.assert_frame_equal( df.vbt.rolling_std(test_window, minp=test_minp, ddof=test_ddof), df.rolling(test_window, min_periods=test_minp).std(ddof=test_ddof) ) pd.testing.assert_frame_equal( df.vbt.rolling_std(test_window), df.rolling(test_window).std() ) @pytest.mark.parametrize( "test_window,test_minp,test_adjust", list(product([1, 2, 3, 4, 5], [1, None], [False, True])) ) def test_ewm_mean(self, test_window, test_minp, test_adjust): if test_minp is None: test_minp = test_window pd.testing.assert_series_equal( df['a'].vbt.ewm_mean(test_window, minp=test_minp, adjust=test_adjust), df['a'].ewm(span=test_window, min_periods=test_minp, adjust=test_adjust).mean() ) pd.testing.assert_frame_equal( df.vbt.ewm_mean(test_window, minp=test_minp, adjust=test_adjust), df.ewm(span=test_window, min_periods=test_minp, adjust=test_adjust).mean() ) pd.testing.assert_frame_equal( df.vbt.ewm_mean(test_window), df.ewm(span=test_window).mean() ) @pytest.mark.parametrize( "test_window,test_minp,test_adjust,test_ddof", list(product([1, 2, 3, 4, 5], [1, None], [False, True], [0, 1])) ) def test_ewm_std(self, test_window, test_minp, test_adjust, test_ddof): if test_minp is None: test_minp = test_window pd.testing.assert_series_equal( df['a'].vbt.ewm_std(test_window, minp=test_minp, adjust=test_adjust, ddof=test_ddof), df['a'].ewm(span=test_window, min_periods=test_minp, adjust=test_adjust).std(ddof=test_ddof) ) pd.testing.assert_frame_equal( df.vbt.ewm_std(test_window, minp=test_minp, adjust=test_adjust, ddof=test_ddof), df.ewm(span=test_window, min_periods=test_minp, adjust=test_adjust).std(ddof=test_ddof) ) pd.testing.assert_frame_equal( df.vbt.ewm_std(test_window), df.ewm(span=test_window).std() ) @pytest.mark.parametrize( "test_minp", [1, 3] ) def test_expanding_min(self, test_minp): pd.testing.assert_series_equal( df['a'].vbt.expanding_min(minp=test_minp), df['a'].expanding(min_periods=test_minp).min() ) pd.testing.assert_frame_equal( df.vbt.expanding_min(minp=test_minp), df.expanding(min_periods=test_minp).min() ) pd.testing.assert_frame_equal( df.vbt.expanding_min(), df.expanding().min() ) @pytest.mark.parametrize( "test_minp", [1, 3] ) def test_expanding_max(self, test_minp): pd.testing.assert_series_equal( df['a'].vbt.expanding_max(minp=test_minp), df['a'].expanding(min_periods=test_minp).max() ) pd.testing.assert_frame_equal( df.vbt.expanding_max(minp=test_minp), df.expanding(min_periods=test_minp).max() ) pd.testing.assert_frame_equal( df.vbt.expanding_max(), df.expanding().max() ) @pytest.mark.parametrize( "test_minp", [1, 3] ) def test_expanding_mean(self, test_minp): pd.testing.assert_series_equal( df['a'].vbt.expanding_mean(minp=test_minp), df['a'].expanding(min_periods=test_minp).mean() ) pd.testing.assert_frame_equal( df.vbt.expanding_mean(minp=test_minp), df.expanding(min_periods=test_minp).mean() ) pd.testing.assert_frame_equal( df.vbt.expanding_mean(), df.expanding().mean() ) @pytest.mark.parametrize( "test_minp,test_ddof", list(product([1, 3], [0, 1])) ) def test_expanding_std(self, test_minp, test_ddof): pd.testing.assert_series_equal( df['a'].vbt.expanding_std(minp=test_minp, ddof=test_ddof), df['a'].expanding(min_periods=test_minp).std(ddof=test_ddof) ) pd.testing.assert_frame_equal( df.vbt.expanding_std(minp=test_minp, ddof=test_ddof), df.expanding(min_periods=test_minp).std(ddof=test_ddof) ) pd.testing.assert_frame_equal( df.vbt.expanding_std(), df.expanding().std() ) def test_apply_along_axis(self): pd.testing.assert_frame_equal( df.vbt.apply_along_axis(i_or_col_pow_nb, 2, axis=0), df.apply(pow_nb, args=(2,), axis=0, raw=True) ) pd.testing.assert_frame_equal( df.vbt.apply_along_axis(i_or_col_pow_nb, 2, axis=1), df.apply(pow_nb, args=(2,), axis=1, raw=True) ) @pytest.mark.parametrize( "test_window,test_minp", list(product([1, 2, 3, 4, 5], [1, None])) ) def test_rolling_apply(self, test_window, test_minp): if test_minp is None: test_minp = test_window pd.testing.assert_series_equal( df['a'].vbt.rolling_apply(test_window, i_col_nanmean_nb, minp=test_minp), df['a'].rolling(test_window, min_periods=test_minp).apply(nanmean_nb, raw=True) ) pd.testing.assert_frame_equal( df.vbt.rolling_apply(test_window, i_col_nanmean_nb, minp=test_minp), df.rolling(test_window, min_periods=test_minp).apply(nanmean_nb, raw=True) ) pd.testing.assert_frame_equal( df.vbt.rolling_apply(test_window, i_col_nanmean_nb), df.rolling(test_window).apply(nanmean_nb, raw=True) ) pd.testing.assert_frame_equal( df.vbt.rolling_apply(3, i_nanmean_nb, on_matrix=True), pd.DataFrame( np.array([ [np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan], [2.75, 2.75, 2.75], [np.nan, np.nan, np.nan] ]), index=df.index, columns=df.columns ) ) @pytest.mark.parametrize( "test_minp", [1, 3] ) def test_expanding_apply(self, test_minp): pd.testing.assert_series_equal( df['a'].vbt.expanding_apply(i_col_nanmean_nb, minp=test_minp), df['a'].expanding(min_periods=test_minp).apply(nanmean_nb, raw=True) ) pd.testing.assert_frame_equal( df.vbt.expanding_apply(i_col_nanmean_nb, minp=test_minp), df.expanding(min_periods=test_minp).apply(nanmean_nb, raw=True) ) pd.testing.assert_frame_equal( df.vbt.expanding_apply(i_col_nanmean_nb), df.expanding().apply(nanmean_nb, raw=True) ) pd.testing.assert_frame_equal( df.vbt.expanding_apply(i_nanmean_nb, on_matrix=True), pd.DataFrame( np.array([ [np.nan, np.nan, np.nan], [2.0, 2.0, 2.0], [2.2857142857142856, 2.2857142857142856, 2.2857142857142856], [2.4, 2.4, 2.4], [2.1666666666666665, 2.1666666666666665, 2.1666666666666665] ]), index=df.index, columns=df.columns ) ) def test_groupby_apply(self): pd.testing.assert_series_equal( df['a'].vbt.groupby_apply(np.asarray([1, 1, 2, 2, 3]), i_col_nanmean_nb), df['a'].groupby(np.asarray([1, 1, 2, 2, 3])).apply(lambda x: nanmean_nb(x.values)) ) pd.testing.assert_frame_equal( df.vbt.groupby_apply(np.asarray([1, 1, 2, 2, 3]), i_col_nanmean_nb), df.groupby(np.asarray([1, 1, 2, 2, 3])).agg({ 'a': lambda x: nanmean_nb(x.values), 'b': lambda x: nanmean_nb(x.values), 'c': lambda x: nanmean_nb(x.values) }), # any clean way to do column-wise grouping in pandas? ) def test_groupby_apply_on_matrix(self): pd.testing.assert_frame_equal( df.vbt.groupby_apply(np.asarray([1, 1, 2, 2, 3]), i_nanmean_nb, on_matrix=True), pd.DataFrame( np.array([ [2., 2., 2.], [2.8, 2.8, 2.8], [1., 1., 1.] ]), index=pd.Int64Index([1, 2, 3], dtype='int64'), columns=df.columns ) ) @pytest.mark.parametrize( "test_freq", ['1h', '3d', '1w'], ) def test_resample_apply(self, test_freq): pd.testing.assert_series_equal( df['a'].vbt.resample_apply(test_freq, i_col_nanmean_nb), df['a'].resample(test_freq).apply(lambda x: nanmean_nb(x.values)) ) pd.testing.assert_frame_equal( df.vbt.resample_apply(test_freq, i_col_nanmean_nb), df.resample(test_freq).apply(lambda x: nanmean_nb(x.values)) ) pd.testing.assert_frame_equal( df.vbt.resample_apply('3d', i_nanmean_nb, on_matrix=True), pd.DataFrame( np.array([ [2.28571429, 2.28571429, 2.28571429], [2., 2., 2.] ]), index=pd.DatetimeIndex(['2018-01-01', '2018-01-04'], dtype='datetime64[ns]', freq='3D'), columns=df.columns ) ) def test_applymap(self): @njit def mult_nb(i, col, x): return x * 2 pd.testing.assert_series_equal( df['a'].vbt.applymap(mult_nb), df['a'].map(lambda x: x * 2) ) pd.testing.assert_frame_equal( df.vbt.applymap(mult_nb), df.applymap(lambda x: x * 2) ) def test_filter(self): @njit def greater_nb(i, col, x): return x > 2 pd.testing.assert_series_equal( df['a'].vbt.filter(greater_nb), df['a'].map(lambda x: x if x > 2 else np.nan) ) pd.testing.assert_frame_equal( df.vbt.filter(greater_nb), df.applymap(lambda x: x if x > 2 else np.nan) ) def test_apply_and_reduce(self): @njit def every_nth_nb(col, a, n): return a[::n] @njit def sum_nb(col, a, b): return np.nansum(a) + b assert df['a'].vbt.apply_and_reduce(every_nth_nb, sum_nb, apply_args=(2,), reduce_args=(3,)) == \ df['a'].iloc[::2].sum() + 3 pd.testing.assert_series_equal( df.vbt.apply_and_reduce(every_nth_nb, sum_nb, apply_args=(2,), reduce_args=(3,)), df.iloc[::2].sum().rename('apply_and_reduce') + 3 ) pd.testing.assert_series_equal( df.vbt.apply_and_reduce( every_nth_nb, sum_nb, apply_args=(2,), reduce_args=(3,), wrap_kwargs=dict(time_units=True)), (df.iloc[::2].sum().rename('apply_and_reduce') + 3) * day_dt ) def test_reduce(self): @njit def sum_nb(col, a): return np.nansum(a) assert df['a'].vbt.reduce(sum_nb) == df['a'].sum() pd.testing.assert_series_equal( df.vbt.reduce(sum_nb), df.sum().rename('reduce') ) pd.testing.assert_series_equal( df.vbt.reduce(sum_nb, wrap_kwargs=dict(time_units=True)), df.sum().rename('reduce') * day_dt ) pd.testing.assert_series_equal( df.vbt.reduce(sum_nb, group_by=group_by), pd.Series([20.0, 6.0], index=['g1', 'g2']).rename('reduce') ) @njit def argmax_nb(col, a): a = a.copy() a[np.isnan(a)] = -np.inf return np.argmax(a) assert df['a'].vbt.reduce(argmax_nb, to_idx=True) == df['a'].idxmax() pd.testing.assert_series_equal( df.vbt.reduce(argmax_nb, to_idx=True), df.idxmax().rename('reduce') ) pd.testing.assert_series_equal( df.vbt.reduce(argmax_nb, to_idx=True, flatten=True, group_by=group_by), pd.Series(['2018-01-02', '2018-01-02'], dtype='datetime64[ns]', index=['g1', 'g2']).rename('reduce') ) @njit def min_and_max_nb(col, a): out = np.empty(2) out[0] = np.nanmin(a) out[1] = np.nanmax(a) return out pd.testing.assert_series_equal( df['a'].vbt.reduce( min_and_max_nb, to_array=True, wrap_kwargs=dict(name_or_index=['min', 'max'])), pd.Series([np.nanmin(df['a']), np.nanmax(df['a'])], index=['min', 'max'], name='a') ) pd.testing.assert_frame_equal( df.vbt.reduce( min_and_max_nb, to_array=True, wrap_kwargs=dict(name_or_index=['min', 'max'])), df.apply(lambda x: pd.Series(np.asarray([np.nanmin(x), np.nanmax(x)]), index=['min', 'max']), axis=0) ) pd.testing.assert_frame_equal( df.vbt.reduce( min_and_max_nb, to_array=True, group_by=group_by, wrap_kwargs=dict(name_or_index=['min', 'max'])), pd.DataFrame([[1.0, 1.0], [4.0, 2.0]], index=['min', 'max'], columns=['g1', 'g2']) ) @njit def argmin_and_argmax_nb(col, a): # nanargmin and nanargmax out = np.empty(2) _a = a.copy() _a[np.isnan(_a)] = np.inf out[0] = np.argmin(_a) _a = a.copy() _a[np.isnan(_a)] = -np.inf out[1] = np.argmax(_a) return out pd.testing.assert_series_equal( df['a'].vbt.reduce( argmin_and_argmax_nb, to_idx=True, to_array=True, wrap_kwargs=dict(name_or_index=['idxmin', 'idxmax'])), pd.Series([df['a'].idxmin(), df['a'].idxmax()], index=['idxmin', 'idxmax'], name='a') ) pd.testing.assert_frame_equal( df.vbt.reduce( argmin_and_argmax_nb, to_idx=True, to_array=True, wrap_kwargs=dict(name_or_index=['idxmin', 'idxmax'])), df.apply(lambda x: pd.Series(np.asarray([x.idxmin(), x.idxmax()]), index=['idxmin', 'idxmax']), axis=0) ) pd.testing.assert_frame_equal( df.vbt.reduce(argmin_and_argmax_nb, to_idx=True, to_array=True, flatten=True, order='C', group_by=group_by, wrap_kwargs=dict(name_or_index=['idxmin', 'idxmax'])), pd.DataFrame([['2018-01-01', '2018-01-01'], ['2018-01-02', '2018-01-02']], dtype='datetime64[ns]', index=['idxmin', 'idxmax'], columns=['g1', 'g2']) ) pd.testing.assert_frame_equal( df.vbt.reduce(argmin_and_argmax_nb, to_idx=True, to_array=True, flatten=True, order='F', group_by=group_by, wrap_kwargs=dict(name_or_index=['idxmin', 'idxmax'])), pd.DataFrame([['2018-01-01', '2018-01-01'], ['2018-01-04', '2018-01-02']], dtype='datetime64[ns]', index=['idxmin', 'idxmax'], columns=['g1', 'g2']) ) def test_squeeze_grouped(self): pd.testing.assert_frame_equal( df.vbt.squeeze_grouped(i_col_nanmean_nb, group_by=group_by), pd.DataFrame([ [1.0, 1.0], [3.0, 2.0], [3.0, np.nan], [3.0, 2.0], [1.0, 1.0] ], index=df.index, columns=['g1', 'g2']) ) def test_flatten_grouped(self): pd.testing.assert_frame_equal( df.vbt.flatten_grouped(group_by=group_by, order='C'), pd.DataFrame([ [1.0, 1.0], [np.nan, np.nan], [2.0, 2.0], [4.0, np.nan], [3.0, np.nan], [3.0, np.nan], [4.0, 2.0], [2.0, np.nan], [np.nan, 1.0], [1.0, np.nan] ], index=np.repeat(df.index, 2), columns=['g1', 'g2']) ) pd.testing.assert_frame_equal( df.vbt.flatten_grouped(group_by=group_by, order='F'), pd.DataFrame([ [1.0, 1.0], [2.0, 2.0], [3.0, np.nan], [4.0, 2.0], [np.nan, 1.0], [np.nan, np.nan], [4.0, np.nan], [3.0, np.nan], [2.0, np.nan], [1.0, np.nan] ], index=np.tile(df.index, 2), columns=['g1', 'g2']) ) @pytest.mark.parametrize( "test_name,test_func,test_func_nb", [ ('min', lambda x, **kwargs: x.min(**kwargs), nb.nanmin_nb), ('max', lambda x, **kwargs: x.max(**kwargs), nb.nanmax_nb), ('mean', lambda x, **kwargs: x.mean(**kwargs), nb.nanmean_nb), ('median', lambda x, **kwargs: x.median(**kwargs), nb.nanmedian_nb), ('std', lambda x, **kwargs: x.std(**kwargs, ddof=0), nb.nanstd_nb), ('count', lambda x, **kwargs: x.count(**kwargs), nb.nancnt_nb), ('sum', lambda x, **kwargs: x.sum(**kwargs), nb.nansum_nb) ], ) def test_funcs(self, test_name, test_func, test_func_nb): # numeric assert test_func(df['a'].vbt) == test_func(df['a']) pd.testing.assert_series_equal( test_func(df.vbt), test_func(df).rename(test_name) ) pd.testing.assert_series_equal( test_func(df.vbt, group_by=group_by), pd.Series([ test_func(df[['a', 'b']].stack()), test_func(df['c']) ], index=['g1', 'g2']).rename(test_name) ) np.testing.assert_array_equal(test_func(df).values, test_func_nb(df.values)) pd.testing.assert_series_equal( test_func(df.vbt, wrap_kwargs=dict(time_units=True)), test_func(df).rename(test_name) * day_dt ) # boolean bool_ts = df == df assert test_func(bool_ts['a'].vbt) == test_func(bool_ts['a']) pd.testing.assert_series_equal( test_func(bool_ts.vbt), test_func(bool_ts).rename(test_name) ) pd.testing.assert_series_equal( test_func(bool_ts.vbt, wrap_kwargs=dict(time_units=True)), test_func(bool_ts).rename(test_name) * day_dt ) @pytest.mark.parametrize( "test_name,test_func", [ ('idxmin', lambda x, **kwargs: x.idxmin(**kwargs)), ('idxmax', lambda x, **kwargs: x.idxmax(**kwargs)) ], ) def test_arg_funcs(self, test_name, test_func): assert test_func(df['a'].vbt) == test_func(df['a']) pd.testing.assert_series_equal( test_func(df.vbt), test_func(df).rename(test_name) ) pd.testing.assert_series_equal( test_func(df.vbt, group_by=group_by), pd.Series([ test_func(df[['a', 'b']].stack())[0], test_func(df['c']) ], index=['g1', 'g2'], dtype='datetime64[ns]').rename(test_name) ) def test_describe(self): pd.testing.assert_series_equal( df['a'].vbt.describe(), df['a'].describe() ) pd.testing.assert_frame_equal( df.vbt.describe(percentiles=None), df.describe(percentiles=None) ) pd.testing.assert_frame_equal( df.vbt.describe(percentiles=[]), df.describe(percentiles=[]) ) test_against = df.describe(percentiles=np.arange(0, 1, 0.1)) pd.testing.assert_frame_equal( df.vbt.describe(percentiles=np.arange(0, 1, 0.1)), test_against ) pd.testing.assert_frame_equal( df.vbt.describe(percentiles=np.arange(0, 1, 0.1), group_by=group_by), pd.DataFrame({ 'g1': df[['a', 'b']].stack().describe(percentiles=np.arange(0, 1, 0.1)).values, 'g2': df['c'].describe(percentiles=np.arange(0, 1, 0.1)).values }, index=test_against.index) ) def test_drawdown(self): pd.testing.assert_series_equal( df['a'].vbt.drawdown(), df['a'] / df['a'].expanding().max() - 1 ) pd.testing.assert_frame_equal( df.vbt.drawdown(), df / df.expanding().max() - 1 ) def test_drawdowns(self): assert type(df['a'].vbt.drawdowns) is vbt.Drawdowns assert df['a'].vbt.drawdowns.wrapper.freq == df['a'].vbt.wrapper.freq assert df['a'].vbt.drawdowns.wrapper.ndim == df['a'].ndim assert df.vbt.drawdowns.wrapper.ndim == df.ndim def test_to_mapped_array(self): np.testing.assert_array_equal( df.vbt.to_mapped_array().values, np.array([1., 2., 3., 4., 4., 3., 2., 1., 1., 2., 2., 1.]) ) np.testing.assert_array_equal( df.vbt.to_mapped_array().col_arr, np.array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2]) ) np.testing.assert_array_equal( df.vbt.to_mapped_array().idx_arr, np.array([0, 1, 2, 3, 1, 2, 3, 4, 0, 1, 3, 4]) ) np.testing.assert_array_equal( df.vbt.to_mapped_array(dropna=False).values, np.array([1., 2., 3., 4., np.nan, np.nan, 4., 3., 2., 1., 1., 2., np.nan, 2., 1.]) ) np.testing.assert_array_equal( df.vbt.to_mapped_array(dropna=False).col_arr, np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2]) ) np.testing.assert_array_equal( df.vbt.to_mapped_array(dropna=False).idx_arr, np.array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4]) ) def test_zscore(self): pd.testing.assert_series_equal( df['a'].vbt.zscore(), (df['a'] - df['a'].mean()) / df['a'].std(ddof=0) ) pd.testing.assert_frame_equal( df.vbt.zscore(), (df - df.mean()) / df.std(ddof=0) ) def test_split(self): splitter = TimeSeriesSplit(n_splits=2) (train_df, train_indexes), (test_df, test_indexes) = df['a'].vbt.split(splitter) pd.testing.assert_frame_equal( train_df, pd.DataFrame( np.array([ [1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [np.nan, 4.0] ]), index=pd.RangeIndex(start=0, stop=4, step=1), columns=pd.Int64Index([0, 1], dtype='int64', name='split_idx') ) ) target = [ pd.DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03'], dtype='datetime64[ns]', name='split_0', freq=None), pd.DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04'], dtype='datetime64[ns]', name='split_1', freq=None) ] for i in range(len(target)): pd.testing.assert_index_equal( train_indexes[i], target[i] ) pd.testing.assert_frame_equal( test_df, pd.DataFrame( np.array([ [4.0, np.nan] ]), index=pd.RangeIndex(start=0, stop=1, step=1), columns=pd.Int64Index([0, 1], dtype='int64', name='split_idx') ) ) target = [ pd.DatetimeIndex(['2018-01-04'], dtype='datetime64[ns]', name='split_0', freq=None), pd.DatetimeIndex(['2018-01-05'], dtype='datetime64[ns]', name='split_1', freq=None) ] for i in range(len(target)): pd.testing.assert_index_equal( test_indexes[i], target[i] ) (train_df, train_indexes), (test_df, test_indexes) = df.vbt.split(splitter) pd.testing.assert_frame_equal( train_df, pd.DataFrame( np.array([ [1.0, np.nan, 1.0, 1.0, np.nan, 1.0], [2.0, 4.0, 2.0, 2.0, 4.0, 2.0], [3.0, 3.0, np.nan, 3.0, 3.0, np.nan], [np.nan, np.nan, np.nan, 4.0, 2.0, 2.0] ]), index=pd.RangeIndex(start=0, stop=4, step=1), columns=pd.MultiIndex.from_tuples([ (0, 'a'), (0, 'b'), (0, 'c'), (1, 'a'), (1, 'b'), (1, 'c') ], names=['split_idx', None]) ) ) target = [ pd.DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03'], dtype='datetime64[ns]', name='split_0', freq=None), pd.DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04'], dtype='datetime64[ns]', name='split_1', freq=None) ] for i in range(len(target)): pd.testing.assert_index_equal( train_indexes[i], target[i] ) pd.testing.assert_frame_equal( test_df, pd.DataFrame( np.array([ [4.0, 2.0, 2.0, np.nan, 1.0, 1.0] ]), index=pd.RangeIndex(start=0, stop=1, step=1), columns=pd.MultiIndex.from_tuples([ (0, 'a'), (0, 'b'), (0, 'c'), (1, 'a'), (1, 'b'), (1, 'c') ], names=['split_idx', None]) ) ) target = [ pd.DatetimeIndex(['2018-01-04'], dtype='datetime64[ns]', name='split_0', freq=None), pd.DatetimeIndex(['2018-01-05'], dtype='datetime64[ns]', name='split_1', freq=None) ] for i in range(len(target)): pd.testing.assert_index_equal( test_indexes[i], target[i] ) def test_range_split(self): pd.testing.assert_frame_equal( df['a'].vbt.range_split(n=2)[0], pd.DataFrame( np.array([ [1., 4.], [2., np.nan] ]), index=pd.RangeIndex(start=0, stop=2, step=1), columns=pd.Int64Index([0, 1], dtype='int64', name='split_idx') ) ) target = [ pd.DatetimeIndex(['2018-01-01', '2018-01-02'], dtype='datetime64[ns]', name='split_0', freq=None), pd.DatetimeIndex(['2018-01-04', '2018-01-05'], dtype='datetime64[ns]', name='split_1', freq=None) ] for i in range(len(target)): pd.testing.assert_index_equal( df['a'].vbt.range_split(n=2)[1][i], target[i] ) pd.testing.assert_frame_equal( df['a'].vbt.range_split(range_len=2)[0], pd.DataFrame( np.array([ [1., 2., 3., 4.], [2., 3., 4., np.nan] ]), index=pd.RangeIndex(start=0, stop=2, step=1), columns=pd.Int64Index([0, 1, 2, 3], dtype='int64', name='split_idx') ) ) target = [ pd.DatetimeIndex(['2018-01-01', '2018-01-02'], dtype='datetime64[ns]', name='split_0', freq=None), pd.DatetimeIndex(['2018-01-02', '2018-01-03'], dtype='datetime64[ns]', name='split_1', freq=None), pd.DatetimeIndex(['2018-01-03', '2018-01-04'], dtype='datetime64[ns]', name='split_2', freq=None), pd.DatetimeIndex(['2018-01-04', '2018-01-05'], dtype='datetime64[ns]', name='split_3', freq=None) ] for i in range(len(target)): pd.testing.assert_index_equal( df['a'].vbt.range_split(range_len=2)[1][i], target[i] ) pd.testing.assert_frame_equal( df['a'].vbt.range_split(range_len=2, n=3)[0], pd.DataFrame( np.array([ [1., 3., 4.], [2., 4., np.nan] ]), index=pd.RangeIndex(start=0, stop=2, step=1), columns=pd.Int64Index([0, 1, 2], dtype='int64', name='split_idx') ) ) target = [ pd.DatetimeIndex(['2018-01-01', '2018-01-02'], dtype='datetime64[ns]', name='split_0', freq=None), pd.DatetimeIndex(['2018-01-03', '2018-01-04'], dtype='datetime64[ns]', name='split_1', freq=None), pd.DatetimeIndex(['2018-01-04', '2018-01-05'], dtype='datetime64[ns]', name='split_2', freq=None) ] for i in range(len(target)): pd.testing.assert_index_equal( df['a'].vbt.range_split(range_len=2, n=3)[1][i], target[i] ) pd.testing.assert_frame_equal( df['a'].vbt.range_split(range_len=3, n=2)[0], pd.DataFrame( np.array([ [1., 3.], [2., 4.], [3., np.nan] ]), index=pd.RangeIndex(start=0, stop=3, step=1), columns=pd.Int64Index([0, 1], dtype='int64', name='split_idx') ) ) target = [ pd.DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03'], dtype='datetime64[ns]', name='split_0', freq=None), pd.DatetimeIndex(['2018-01-03', '2018-01-04', '2018-01-05'], dtype='datetime64[ns]', name='split_1', freq=None) ] for i in range(len(target)): pd.testing.assert_index_equal( df['a'].vbt.range_split(range_len=3, n=2)[1][i], target[i] ) pd.testing.assert_frame_equal( df.vbt.range_split(n=2)[0], pd.DataFrame( np.array([ [1.0, np.nan, 1.0, 4.0, 2.0, 2.0], [2.0, 4.0, 2.0, np.nan, 1.0, 1.0] ]), index=pd.RangeIndex(start=0, stop=2, step=1), columns=pd.MultiIndex.from_arrays([ pd.Int64Index([0, 0, 0, 1, 1, 1], dtype='int64', name='split_idx'), pd.Index(['a', 'b', 'c', 'a', 'b', 'c'], dtype='object') ]) ) ) target = [ pd.DatetimeIndex(['2018-01-01', '2018-01-02'], dtype='datetime64[ns]', name='split_0', freq=None), pd.DatetimeIndex(['2018-01-04', '2018-01-05'], dtype='datetime64[ns]', name='split_1', freq=None) ] for i in range(len(target)): pd.testing.assert_index_equal( df.vbt.range_split(n=2)[1][i], target[i] ) pd.testing.assert_frame_equal( df.vbt.range_split(start_idxs=[0, 1], end_idxs=[2, 3])[0], pd.DataFrame( np.array([ [1.0, np.nan, 1.0, 2.0, 4.0, 2.0], [2.0, 4.0, 2.0, 3.0, 3.0, np.nan], [3.0, 3.0, np.nan, 4.0, 2.0, 2.0] ]), index=pd.RangeIndex(start=0, stop=3, step=1), columns=pd.MultiIndex.from_arrays([ pd.Int64Index([0, 0, 0, 1, 1, 1], dtype='int64', name='split_idx'), pd.Index(['a', 'b', 'c', 'a', 'b', 'c'], dtype='object') ]) ) ) target = [ pd.DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03'], dtype='datetime64[ns]', name='split_0', freq=None), pd.DatetimeIndex(['2018-01-02', '2018-01-03', '2018-01-04'], dtype='datetime64[ns]', name='split_1', freq=None) ] for i in range(len(target)): pd.testing.assert_index_equal( df.vbt.range_split(start_idxs=[0, 1], end_idxs=[2, 3])[1][i], target[i] ) pd.testing.assert_frame_equal( df.vbt.range_split(start_idxs=df.index[[0, 1]], end_idxs=df.index[[2, 3]])[0], pd.DataFrame( np.array([ [1.0, np.nan, 1.0, 2.0, 4.0, 2.0], [2.0, 4.0, 2.0, 3.0, 3.0, np.nan], [3.0, 3.0, np.nan, 4.0, 2.0, 2.0] ]), index=pd.RangeIndex(start=0, stop=3, step=1), columns=pd.MultiIndex.from_arrays([ pd.Int64Index([0, 0, 0, 1, 1, 1], dtype='int64', name='split_idx'), pd.Index(['a', 'b', 'c', 'a', 'b', 'c'], dtype='object') ]) ) ) target = [ pd.DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03'], dtype='datetime64[ns]', name='split_0', freq=None), pd.DatetimeIndex(['2018-01-02', '2018-01-03', '2018-01-04'], dtype='datetime64[ns]', name='split_1', freq=None) ] for i in range(len(target)): pd.testing.assert_index_equal( df.vbt.range_split(start_idxs=df.index[[0, 1]], end_idxs=df.index[[2, 3]])[1][i], target[i] ) pd.testing.assert_frame_equal( df.vbt.range_split(start_idxs=df.index[[0]], end_idxs=df.index[[2, 3]])[0], pd.DataFrame( np.array([ [1.0, np.nan, 1.0, 1.0, np.nan, 1.0], [2.0, 4.0, 2.0, 2.0, 4.0, 2.0], [3.0, 3.0, np.nan, 3.0, 3.0, np.nan], [np.nan, np.nan, np.nan, 4.0, 2.0, 2.0] ]), index=pd.RangeIndex(start=0, stop=4, step=1), columns=pd.MultiIndex.from_arrays([ pd.Int64Index([0, 0, 0, 1, 1, 1], dtype='int64', name='split_idx'), pd.Index(['a', 'b', 'c', 'a', 'b', 'c'], dtype='object') ]) ) ) target = [ pd.DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03'], dtype='datetime64[ns]', name='split_0', freq=None), pd.DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03', '2018-01-04'], dtype='datetime64[ns]', name='split_1', freq=None) ] for i in range(len(target)): pd.testing.assert_index_equal( df.vbt.range_split(start_idxs=df.index[[0]], end_idxs=df.index[[2, 3]])[1][i], target[i] ) with pytest.raises(Exception) as e_info: df.vbt.range_split() with pytest.raises(Exception) as e_info: df.vbt.range_split(start_idxs=[0, 1]) with pytest.raises(Exception) as e_info: df.vbt.range_split(end_idxs=[2, 4]) with pytest.raises(Exception) as e_info: df.vbt.range_split(min_len=10) with pytest.raises(Exception) as e_info: df.vbt.range_split(n=10) def test_rolling_split(self): (df1, indexes1), (df2, indexes2), (df3, indexes3) = df['a'].vbt.rolling_split( window_len=4, set_lens=(1, 1), left_to_right=False) pd.testing.assert_frame_equal( df1, pd.DataFrame( np.array([ [1.0, 2.0], [2.0, 3.0] ]), index=pd.RangeIndex(start=0, stop=2, step=1), columns=pd.Int64Index([0, 1], dtype='int64', name='split_idx') ) ) target = [ pd.DatetimeIndex(['2018-01-01', '2018-01-02'], dtype='datetime64[ns]', name='split_0', freq=None), pd.DatetimeIndex(['2018-01-02', '2018-01-03'], dtype='datetime64[ns]', name='split_1', freq=None) ] for i in range(len(target)): pd.testing.assert_index_equal( indexes1[i], target[i] ) pd.testing.assert_frame_equal( df2, pd.DataFrame( np.array([ [3.0, 4.0] ]), index=pd.RangeIndex(start=0, stop=1, step=1), columns=pd.Int64Index([0, 1], dtype='int64', name='split_idx') ) ) target = [ pd.DatetimeIndex(['2018-01-03'], dtype='datetime64[ns]', name='split_0', freq=None), pd.DatetimeIndex(['2018-01-04'], dtype='datetime64[ns]', name='split_1', freq=None) ] for i in range(len(target)): pd.testing.assert_index_equal( indexes2[i], target[i] ) pd.testing.assert_frame_equal( df3, pd.DataFrame( np.array([ [4.0, np.nan] ]), index=pd.RangeIndex(start=0, stop=1, step=1), columns=pd.Int64Index([0, 1], dtype='int64', name='split_idx') ) ) target = [
pd.DatetimeIndex(['2018-01-04'], dtype='datetime64[ns]', name='split_0', freq=None)
pandas.DatetimeIndex
"""Module containing class to build feature matrices for prediction. There are two kinds of features: - either features for direct prediction model - either features for recursive prediction model Only the first one is used for now. """ from os import path, makedirs import logging from datetime import datetime, timedelta import numpy as np import pandas as pd from api_etl.utils_misc import ( get_paris_local_datetime_now, DateConverter, S3Bucket ) from api_etl.querier_schedule import DBQuerier from api_etl.querier_realtime import ResultsSet from api_etl.settings import __S3_BUCKETS__, __TRAINING_SET_FOLDER_PATH__, __RAW_DAYS_FOLDER_PATH__, __DATA_PATH__ logger = logging.getLogger(__name__) pd.options.mode.chained_assignment = None class DayMatrixBuilder: """Build features and label matrices from data available from schedule and from realtime info. 1st step (init): get all information from day (schedule+realtime): needs day parameter (else set to today). 2nd step: build matrices using only data available at given time: needs time parameter (else set to time now). Still "beta" functionality: provide df directly. """ def __init__(self, day=None, df=None): """ Given a day, will query schedule and realtime information to provide a dataframe containing all stops. """ # Arguments validation and parsing if day: # will raise error if wrong format datetime.strptime(day, "%Y%m%d") self.day = str(day) else: dt_today = get_paris_local_datetime_now() self.day = dt_today.strftime("%Y%m%d") logger.info("Day considered: %s" % self.day) if isinstance(df, pd.DataFrame): self._initial_df = df self._builder_realtime_request_time = None logger.info("Dataframe provided for day %s" % self.day) else: logger.info("Requesting data for day %s" % self.day) self.querier = DBQuerier(scheduled_day=self.day) # Get schedule self.stops_results = self.querier.stoptimes(on_day=self.day, level=4) self.serialized_stoptimes = ResultsSet(self.stops_results) logger.info("Schedule queried.") # Perform realtime queries dt_realtime_request = get_paris_local_datetime_now() self._builder_realtime_request_time = dt_realtime_request\ .strftime("%H:%M:%S") self.serialized_stoptimes.batch_realtime_query(self.day) logger.info("RealTime queried.") # Export flat dict as dataframe self._initial_df = pd\ .DataFrame(self.serialized_stoptimes.get_flat_dicts()) logger.info("Initial dataframe created.") # Datetime considered as now self.paris_datetime_now = get_paris_local_datetime_now() self._clean_initial_df() logger.info("Initial dataframe cleaned.") self._compute_initial_dates() logger.info("Initial dataframe calculations computed.") def _clean_initial_df(self): """ Set Nan values, and convert necessary columns as float. """ # Replace Unknown by Nan self._initial_df.replace("Unknown", np.nan, inplace=True) # Convert to numeric cols_to_num = ["StopTime_stop_sequence", "RealTime_data_freshness"] for col in cols_to_num: self._initial_df.loc[:, col] = pd\ .to_numeric(self._initial_df.loc[:, col], errors="coerce") def _compute_initial_dates(self): """ Adds following columns: - D_business_day: bool - D_stop_special_day: scheduled_day str, day in special date (25h) - D_total_sequence: int: number of stops scheduled per trip - D_stop_scheduled_datetime: datetime of scheduled stoptime - D_trip_passed_scheduled_stop: bool """ # Detect if working day self._initial_df.loc[:, "D_business_day"] = bool( len(pd.bdate_range(self.day, self.day))) # Write stoptime_day self._initial_df.loc[:, "D_stop_special_day"] = self.day # Scheduled stop datetime self._initial_df.loc[:, "D_stop_scheduled_datetime"] = self._initial_df\ .StopTime_departure_time\ .apply(lambda x: DateConverter( special_time=x, special_date=self.day, force_regular_date=True ).dt ) # Has really passed schedule self._initial_df.loc[:, "D_trip_passed_scheduled_stop"] = self._initial_df.D_stop_scheduled_datetime\ .apply(lambda x: (self.paris_datetime_now - x).total_seconds() >= 0 ) # Observed stop datetime self._initial_df.loc[:, "D_stop_observed_datetime"] = self\ ._initial_df[self._initial_df.RealTime_data_freshness.notnull()]\ .apply(lambda x: DateConverter( special_time=x.RealTime_expected_passage_time, special_date=x.RealTime_expected_passage_day ).dt, axis=1 ) self._initial_df.loc[:, "D_trip_time_to_observed_stop"] = self\ ._initial_df[self._initial_df.D_stop_observed_datetime.notnull()]\ .D_stop_observed_datetime\ .apply(lambda x: (self.paris_datetime_now - x).total_seconds() ) # Has really passed observed stop self._initial_df.loc[:, "D_trip_passed_observed_stop"] = self\ ._initial_df[self._initial_df.D_stop_observed_datetime.notnull()]\ .D_trip_time_to_observed_stop\ .apply(lambda x: (x >= 0)) # Trip delay self._initial_df.loc[:, "D_trip_delay"] = self\ ._initial_df[self._initial_df.RealTime_data_freshness.notnull()]\ .apply( lambda x: (x["D_stop_observed_datetime"] - x["D_stop_scheduled_datetime"]) .total_seconds(), axis=1 ) # Trips total number of stops trips_total_number_stations = self._initial_df\ .groupby("Trip_trip_id")["Stop_stop_id"].count() trips_total_number_stations.name = "D_trip_number_of_stops" self._initial_df = self._initial_df\ .join(trips_total_number_stations, on="Trip_trip_id") def stats(self): message = """ SUMMARY FOR DAY %(day)s: based on information available and requested at time %(request_time)s, and trips passage being evaluated given time %(date_now)s TRIPS Number of trips today: %(trips_today)s STOPTIMES Number of stop times that day: %(stoptimes_today)s - Passed: - scheduled: %(stoptimes_passed)s - observed: %(stoptimes_passed_observed)s - Not passed yet: - scheduled: %(stoptimes_not_passed)s - observed (predictions on boards) %(stoptimes_not_passed_observed)s """ self.summary = { "day": self.day, "request_time": self._builder_realtime_request_time, "date_now": self.paris_datetime_now, "trips_today": len(self._initial_df.Trip_trip_id.unique()), "stoptimes_today": self._initial_df.Trip_trip_id.count(), "stoptimes_passed": self._initial_df .D_trip_passed_scheduled_stop.sum(), "stoptimes_passed_observed": self._initial_df. D_trip_passed_observed_stop.sum(), "stoptimes_not_passed": (~self._initial_df.D_trip_passed_scheduled_stop).sum(), "stoptimes_not_passed_observed": (self._initial_df.D_trip_passed_observed_stop == False).sum(), } print(message % self.summary) def missing_data_per(self, per="Route_route_short_name"): # per can be also "Stop_stop_id", "Route_route_short_name" md = self._initial_df.copy() md.loc[:, "observed"] = md\ .loc[:, "RealTime_day_train_num"]\ .notnull().apply(int) group = md.groupby(per)["observed"] agg_observed = group.sum() agg_scheduled = group.count() agg_ratio = group.mean() agg = pd.concat([agg_observed, agg_scheduled, agg_ratio], axis=1) agg.columns = ["Observed", "Scheduled", "Ratio"] return agg class DirectPredictionMatrix(DayMatrixBuilder): # CONFIGURATION # Number of past seconds considered for station median delay _secs = 1200 # Features columns _feature_cols = [ "Route_route_short_name", "TS_last_observed_delay", "TS_line_station_median_delay", "TS_line_median_delay", "Trip_direction_id", "TS_sequence_diff", "TS_stations_scheduled_trip_time", "TS_rolling_trips_on_line", "RealTime_miss", "D_business_day" ] # Core identification columns _id_cols = [ "TS_matrix_datetime", "Route_route_short_name", "RealTime_miss", "Trip_trip_id", "Stop_stop_id", "TS_sequence_diff", "TS_stations_scheduled_trip_time", ] # Label columns _label_cols = ["label", "label_ev"] # Scoring columns _scoring_cols = ["S_naive_pred_mae", "S_naive_pred_mse"] # Prediction columns _prediction_cols = ["P_api_pred", "P_api_pred_ev", "P_naive_pred"] # Other useful columns _other_useful_cols = [ "StopTime_departure_time", "StopTime_stop_sequence", "Stop_stop_name", "RealTime_expected_passage_time", "RealTime_data_freshness", ] # For time debugging: _time_debug_cols = [ "StopTime_departure_time", "RealTime_expected_passage_time", 'D_stop_special_day', 'D_stop_scheduled_datetime', 'D_trip_passed_scheduled_stop', 'D_stop_observed_datetime', 'D_trip_time_to_observed_stop', 'D_trip_passed_observed_stop', 'D_trip_delay', 'TS_matrix_datetime', 'TS_trip_passed_scheduled_stop', 'TS_observed_vs_matrix_datetime', 'TS_trip_passed_observed_stop', 'TS_observed_delay', 'TS_expected_delay', 'TS_trip_status' ] def __init__(self, day=None, df=None): DayMatrixBuilder.__init__(self, day=day, df=df) self._state_at_time_computed = False def direct_compute_for_time(self, time="12:00:00"): """Given the data obtained from schedule and realtime, this method will compute network state at a given time, and provide prediction and label matrices. :param time: """ # Parameters parsing full_str_dt = "%s%s" % (self.day, time) # will raise error if wrong format self.state_at_datetime = datetime\ .strptime(full_str_dt, "%Y%m%d%H:%M:%S") self.time = time logger.info( "Building Matrix for day %s and time %s" % ( self.day, self.time) ) # Recreate dataframe from initial one (deletes changes) self.df = self._initial_df.copy() # Computing self._compute_trip_state() logger.info("TripPredictor computed.") self._trip_level() logger.info("Trip level computations performed.") self._line_level() logger.info("Line level computations performed.") # Will add labels if information is available self._compute_labels() logger.info("Labels assigned.") self._compute_api_pred() logger.info("Api and naive predictions assigned.") self._compute_pred_scores() logger.info("Naive predictions scored.") def _compute_trip_state(self): """Computes: - TS_matrix_datetime: datetime = datetime for which state is computed - TS_trip_passed_scheduled_stop: Bool = at matrix datetime, has train passed scheduled stop? - TS_observed_vs_matrix_datetime: int (seconds) - TS_trip_passed_observed_stop: Bool = at matrix datetime, has train passed observed stop? - TS_observed_delay: int (seconds) - TS_expected_delay: int (seconds) """ self.df.loc[:, "TS_matrix_datetime"] = self.state_at_datetime\ .strftime("%Y%m%d-%H:%M:%S") # Has passed scheduled stop at state datetime self.df.loc[:, "TS_trip_passed_scheduled_stop"] = self.df\ .D_stop_scheduled_datetime\ .apply(lambda x: ((self.state_at_datetime - x).total_seconds() >= 0), ) # Time between matrix datetime (for which we compute the prediction # features matrix), and stop times observed passages (only for observed # passages). <0 means passed, >0 means not passed yet at the given time self.df.loc[:, "TS_observed_vs_matrix_datetime"] = self\ .df[self.df["D_stop_observed_datetime"].notnull()]\ .D_stop_observed_datetime\ .apply(lambda x: (self.state_at_datetime - x).total_seconds()) # Has passed observed stop time at state datetime self.df.loc[:, "TS_trip_passed_observed_stop"] = self\ .df[self.df["TS_observed_vs_matrix_datetime"] .notnull()]\ .loc[:, "TS_observed_vs_matrix_datetime"]\ .apply(lambda x: (x >= 0)) # TripState_observed_delay self.df.loc[:, "TS_observed_delay"] = self\ .df[self.df["TS_trip_passed_observed_stop"] == True]\ .D_trip_delay # TripState_expected_delay self.df.loc[:, "TS_expected_delay"] = self\ .df.query("(TS_trip_passed_observed_stop != True) & (RealTime_data_freshness.notnull())")\ .D_trip_delay self._state_at_time_computed = True def _trip_level(self): """Compute trip level information: - TS_trip_status: 0<=x<=1: proportion of passed stations at time - D_total_sequence: number of stops scheduled for this trip - last_sequence_number: last observed stop sequence for this trip at time - last_observed_delay """ # Trips total number of stops trips_total_number_stations = self.df\ .groupby("Trip_trip_id")["Stop_stop_id"].count() # already added to day matrix # Trips status at time trips_number_passed_stations = self.df\ .groupby("Trip_trip_id")["TS_trip_passed_scheduled_stop"].sum() trips_status = trips_number_passed_stations \ / trips_total_number_stations trips_status.name = "TS_trip_status" self.trips_status = trips_status self.df = self.df.join(trips_status, on="Trip_trip_id") # Trips last observed stop_sequence last_sequence_number = self\ .df.query("(TS_trip_status < 1) & (TS_trip_status > 0) & (TS_trip_passed_observed_stop == True)")\ .groupby("Trip_trip_id")["StopTime_stop_sequence"].max() last_sequence_number.name = "TS_last_sequence_number" self.df = self.df.join(last_sequence_number, on="Trip_trip_id") # Compute number of stops between last observed station and predicted # station. self.df.loc[:, "TS_sequence_diff"] = self.df.StopTime_stop_sequence - \ self.df.loc[:, "TS_last_sequence_number"] # Trips last observed delay last_observed_delay = self.df\ .query("TS_last_sequence_number==StopTime_stop_sequence")\ .loc[:, ["Trip_trip_id", "TS_observed_delay"]] last_observed_delay.set_index("Trip_trip_id", inplace=True) last_observed_delay.columns = ["TS_last_observed_delay"] self.df = self.df.join(last_observed_delay, on="Trip_trip_id") # Trips last observed scheduled departure time # useful to know how much time was scheduled between stations last_observed_scheduled_dep_time = self.df\ .query("TS_last_sequence_number==StopTime_stop_sequence")\ .loc[:, ["Trip_trip_id", "StopTime_departure_time"]] last_observed_scheduled_dep_time\ .set_index("Trip_trip_id", inplace=True) last_observed_scheduled_dep_time.columns = [ "TS_last_observed_scheduled_dep_time"] self.df = self.df\ .join(last_observed_scheduled_dep_time, on="Trip_trip_id") # Compute number of seconds between last observed passed trip scheduled # departure time, and departure time of predited station self.df.loc[:, "TS_stations_scheduled_trip_time"] = self.df\ .query("TS_last_observed_scheduled_dep_time.notnull()")\ .apply(lambda x: DateConverter(dt=x["D_stop_scheduled_datetime"]) .compute_delay_from( special_date=self.day, special_time=x["TS_last_observed_scheduled_dep_time"], force_regular_date=True ), axis=1 ) def _line_level(self): """ Computes line level information: - median delay on line on last n seconds - median delay on line station on last n seconds - number of currently rolling trips on line Requires time to now (_add_time_to_now_col). """ # Compute delays on last n seconds (defined in init self._secs) # Line aggregation line_median_delay = self.df\ .query("(TS_observed_vs_matrix_datetime<%s) & (TS_observed_vs_matrix_datetime>=0) " % self._secs)\ .groupby("Route_route_short_name")\ .TS_observed_delay.median() line_median_delay.name = "TS_line_median_delay" self.df = self.df\ .join(line_median_delay, on="Route_route_short_name") self.line_median_delay = line_median_delay # Line and station aggregation # same station can have different values given on which lines it # is located. line_station_median_delay = self.df\ .query("(TS_observed_vs_matrix_datetime < %s) & (TS_observed_vs_matrix_datetime>=0) " % self._secs)\ .groupby(["Route_route_short_name", "Stop_stop_id"])\ .TS_observed_delay.median() line_station_median_delay.name = "TS_line_station_median_delay" self.df = self.df\ .join(line_station_median_delay, on=["Route_route_short_name", "Stop_stop_id"]) self.line_station_median_delay = line_station_median_delay # Number of currently rolling trips rolling_trips_on_line = self\ .df.query("TS_trip_status>0 & TS_trip_status<1")\ .groupby("Route_route_short_name")\ .Trip_trip_id\ .count() rolling_trips_on_line.name = "TS_rolling_trips_on_line" self.df = self.df\ .join(rolling_trips_on_line, on="Route_route_short_name") self.rolling_trips_on_line = rolling_trips_on_line def _compute_labels(self): """Two main logics: - either retroactive: then TripState_expected_delay is real one: label. - either realtime (not retroactive): then we don't have real label, but we have a api prediction. Retroactive: Adds two columns: - label: observed delay at stop: real one. - label_ev: observed delay evolution (difference between observed delay predicted stop, and delay at last observed stop) Not retroactive: realtime: Adds two columns: - P_api_pred: predicted delay from api. - P_api_pred_ev: predicted evolution (from api) of delay. """ # if stop time really occured, then expected delay (extracted from api) # is real one self.df.loc[:, "label"] = self.df\ .query("D_trip_passed_observed_stop==True")\ .TS_expected_delay # Evolution of delay between last observed station and predicted # station self.df.loc[:, "label_ev"] = self.df\ .query("D_trip_passed_observed_stop == True")\ .apply(lambda x: x.label - x["TS_last_observed_delay"], axis=1) def _compute_api_pred(self): """This method provides two predictions if possible: - naive pred: delay translation (last observed delay) - api prediction """ # if not passed: it is the api-prediction self.df.loc[:, "P_api_pred"] = self.df\ .query("D_trip_passed_observed_stop != True")\ .TS_expected_delay # api delay evolution prediction self.df.loc[:, "P_api_pred_ev"] = self.df\ .query("D_trip_passed_observed_stop != True")\ .apply(lambda x: x.label - x["TS_last_observed_delay"], axis=1) self.df.loc[:, "P_naive_pred"] = self.df.loc[ :, "TS_last_observed_delay"] def _compute_pred_scores(self): """ We can compute score only for stoptimes for which we have real information. At no point we will be able to have both real information and api pred, so we only compute score for naive prediction. NAIVE PREDICTION: Naive prediction assumes that delay does not evolve: - evolution of delay = 0 - delay predicted = last_observed_delay => error = real_delay - naive_pred = label - last_observed_delay = label_ev Scores for navie prediction for delay can be: - naive_pred_mae: mean absolute error: |label_ev| - naive_pred_mse: mean square error: (label_ev)**2 """ self.df.loc[:, "S_naive_pred_mae"] = self.df["label_ev"].abs() self.df.loc[:, "S_naive_pred_mse"] = self.df["label_ev"]**2 def stats(self): DayMatrixBuilder.stats(self) if not self._state_at_time_computed: return None message = """ SUMMARY FOR DAY %(day)s AT TIME %(time)s TRIPS Number of trips today: %(trips_today)s Number of trips currently rolling: %(trips_now)s (these are the trips for which we will try to make predictions) Number of trips currently rolling for wich we observed at least one stop: %(trips_now_observed)s STOPTIMES Number of stop times that day: %(stoptimes_today)s - Passed: - scheduled: %(stoptimes_passed)s - observed: %(stoptimes_passed_observed)s - Not passed yet: - scheduled: %(stoptimes_not_passed)s - observed (predictions on boards) %(stoptimes_not_passed_observed)s STOPTIMES FOR ROLLING TRIPS Total number of stops for rolling trips: %(stoptimes_now)s - Passed: those we will use to make our prediction - scheduled: %(stoptimes_now_passed)s - observed: %(stoptimes_now_passed_observed)s - Not passed yet: those for which we want to make a prediction - scheduled: %(stoptimes_now_not_passed)s - already observed on boards (prediction): %(stoptimes_now_not_passed_observed)s PREDICTIONS Number of stop times for which we want to make a prediction (not passed yet): %(stoptimes_now_not_passed)s Number of trips currently rolling for wich we observed at least one stop: %(trips_now_observed)s Representing %(stoptimes_predictable)s stop times for which we can provide a prediction. LABELED Given that retroactive is %(retroactive)s, we have %(stoptimes_predictable_labeled)s labeled to_predict stoptimes for training. """ self.summary = { "day": self.day, "time": self.time, "trips_today": len(self.df.Trip_trip_id.unique()), "trips_now": self.df .query("(TS_trip_status > 0) & (TS_trip_status < 1)") .Trip_trip_id.unique().shape[0], "trips_now_observed": self.df .query("(TS_trip_status > 0) & (TS_trip_status < 1) & (TS_sequence_diff.notnull())") .Trip_trip_id.unique().shape[0], "stoptimes_today": self.df.Trip_trip_id.count(), "stoptimes_passed": self.df.TS_trip_passed_scheduled_stop.sum(), "stoptimes_passed_observed": self .df.TS_trip_passed_observed_stop.sum(), "stoptimes_not_passed": (~self.df.TS_trip_passed_scheduled_stop).sum(), "stoptimes_not_passed_observed": (self.df.TS_trip_passed_observed_stop == False).sum(), "stoptimes_now": self.df .query("(TS_trip_status > 0) & (TS_trip_status < 1)") .Trip_trip_id.count(), "stoptimes_now_passed": self.df .query("(TS_trip_status > 0) & (TS_trip_status < 1) &(TS_trip_passed_scheduled_stop==True)") .Trip_trip_id.count(), "stoptimes_now_passed_observed": self.df .query("(TS_trip_status > 0) & (TS_trip_status < 1) &(TS_trip_passed_observed_stop==True)") .Trip_trip_id.count(), "stoptimes_now_not_passed": self.df .query("(TS_trip_status > 0) & (TS_trip_status < 1) &(TS_trip_passed_scheduled_stop==False)") .Trip_trip_id.count(), "stoptimes_now_not_passed_observed": self.df .query("(TS_trip_status > 0) & (TS_trip_status < 1) &(TS_trip_passed_observed_stop==False)") .Trip_trip_id.count(), "stoptimes_predictable": self.df .query("(TS_trip_status > 0) & (TS_trip_status < 1) &(TS_trip_passed_scheduled_stop==False) &" " (TS_sequence_diff.notnull())") .Trip_trip_id.count(), "stoptimes_predictable_labeled": self.df .query("(TS_trip_status > 0) & (TS_trip_status < 1) &(TS_trip_passed_scheduled_stop==False) &" " (TS_sequence_diff.notnull()) &(label.notnull())") .Trip_trip_id.count(), } print(message % self.summary) def get_predictable(self, all_features_required=True, labeled_only=True, col_filter_level=2, split_datasets=False, set_index=True, provided_df=None): """Return to_predict stop times. :param all_features_required: :param labeled_only: :param col_filter_level: :param split_datasets: :param set_index: :param provided_df: """ assert self._state_at_time_computed if isinstance(provided_df, pd.DataFrame): rdf = provided_df else: rdf = self.df # Filter running trips, stations not passed yet # Basic Conditions: # - trip_status stricly between 0 and 1, # - has not passed yet schedule (not True) # - has not passed yet realtime (not True, it can be Nan or False) rdf = rdf.query( "TS_trip_status < 1 & TS_trip_status > 0 & TS_trip_passed_scheduled_stop !=\ True & TS_trip_passed_observed_stop != True") if all_features_required: # Only elements that have all features for feature in self._feature_cols: rdf = rdf.query("%s.notnull()" % feature) if labeled_only: rdf = rdf.query("label.notnull()") if set_index: rdf = self._df_set_index(rdf) if col_filter_level: # no filter, all columns rdf = self._df_filter_cols(rdf, col_filter_level=col_filter_level) if split_datasets: # return dict rdf = self._split_datasets(rdf) logger.info("Predictable with labeled_only=%s, has a total of %s rows." % (labeled_only, len(rdf)) ) return rdf def _df_filter_cols(self, rdf, col_filter_level): # We need at least: index, features, and label filtered_cols = self._feature_cols\ + self._id_cols\ + self._label_cols\ + self._prediction_cols\ + self._scoring_cols if col_filter_level == 2: # high filter: only necessary fields return rdf[filtered_cols] elif col_filter_level == 1: # medium filter: add some useful cols filtered_cols += self._other_useful_cols return rdf[filtered_cols] else: raise ValueError("col_filter_level must be 0, 1 or 2") def _df_set_index(self, rdf): # copy columns so that it is available as value or index # value columns are then filtered assert isinstance(rdf, pd.DataFrame) index_suffix = "_ix" rdf.reset_index() for col in self._id_cols: rdf[col + index_suffix] = rdf[col] new_ix = list(map(lambda x: x + index_suffix, self._id_cols)) rdf.set_index(new_ix, inplace=True) return rdf def _split_datasets(self, rdf): res = { "X": rdf[self._feature_cols], "y_real": rdf[self._label_cols], "y_pred": rdf[self._prediction_cols], "y_score": rdf[self._scoring_cols] } return res def compute_multiple_times_of_day(self, begin="00:00:00", end="23:59:00", min_diff=60, flush_former=True, **kwargs): """Compute dataframes for different times of day. Default: begins at 00:00:00 and ends at 23:59:00 with a step of one hour. :param end: :param min_diff: :param flush_former: :param begin: """ assert isinstance(min_diff, int) diff = timedelta(minutes=min_diff) # will raise error if wrong format begin_dt = datetime.strptime(begin, "%H:%M:%S") end_dt = datetime.strptime(end, "%H:%M:%S") if flush_former: self._flush_result_concat() step_dt = begin_dt while (end_dt >= step_dt): step = step_dt.strftime("%H:%M:%S") self.direct_compute_for_time(step) step_df = self.get_predictable(**kwargs) self._concat_dataframes(step_df) step_dt += diff return self.result_concat def _concat_dataframes(self, df): assert isinstance(df, pd.DataFrame) # if no former result df, create empty df if not hasattr(self, "result_concat"): self.result_concat = pd.DataFrame() # concat with previous results self.result_concat =
pd.concat([self.result_concat, df])
pandas.concat
from datetime import datetime, timedelta, timezone import random from tabnanny import check import unittest import pandas as pd import pytz if __name__ == "__main__": from pathlib import Path import sys sys.path.insert(0, str(Path(__file__).resolve().parents[2])) from datatube.dtype import check_dtypes class TestObj: pass unittest.TestCase.maxDiff = None SIZE = 3 TEST_DATA = { int: { "integers": [-1 * SIZE // 2 + i + 1 for i in range(SIZE)], "whole floats": [-1 * SIZE // 2 + i + 1.0 for i in range(SIZE)], "real whole complex": [complex(-1 * SIZE // 2 + i + 1, 0) for i in range(SIZE)], }, float: { "decimal floats": [-1 * SIZE // 2 + i + 1 + random.random() for i in range(SIZE)], "real decimal complex": [complex(-1 * SIZE // 2 + i + 1 + random.random(), 0) for i in range(SIZE)], }, complex: { "imaginary complex": [complex(-1 * SIZE // 2 + i + 1 + random.random(), -1 * SIZE // 2 + i + 1 + random.random()) for i in range(SIZE)], }, str: { "integer strings": [str(-1 * SIZE // 2 + i + 1) for i in range(SIZE)], "whole float strings": [str(-1 * SIZE // 2 + i + 1.0) for i in range(SIZE)], "decimal float strings": [str(-1 * SIZE // 2 + i + 1 + random.random()) for i in range(SIZE)], "real whole complex strings": [str(complex(-1 * SIZE // 2 + i + 1, 0)) for i in range(SIZE)], "real decimal complex strings": [str(complex(-1 * SIZE // 2 + i + 1 + random.random(), 0)) for i in range(SIZE)], "imaginary complex strings": [str(complex(-1 * SIZE // 2 + i + 1 + random.random(), -1 * SIZE // 2 + i + 1 + random.random())) for i in range(SIZE)], "character strings": [chr(i % 26 + ord("a")) for i in range(SIZE)], "boolean strings": [str(bool((i + 1) % 2)) for i in range(SIZE)], "aware datetime strings": [str(datetime.fromtimestamp(i, tz=timezone.utc)) for i in range(SIZE)], "aware ISO 8601 strings": [datetime.fromtimestamp(i, tz=timezone.utc).isoformat() for i in range(SIZE)], "naive datetime strings": [str(datetime.fromtimestamp(i)) for i in range(SIZE)], "naive ISO 8601 strings": [datetime.fromtimestamp(i).isoformat() for i in range(SIZE)], "aware/naive datetime strings": [str(datetime.fromtimestamp(i, tz=timezone.utc)) if i % 2 else str(datetime.fromtimestamp(i)) for i in range(SIZE)], "aware/naive ISO 8601 strings": [datetime.fromtimestamp(i, tz=timezone.utc).isoformat() if i % 2 else datetime.fromtimestamp(i).isoformat() for i in range(SIZE)], "mixed timezone datetime strings": [str( datetime.fromtimestamp( i, tz=pytz.timezone( pytz.all_timezones[i % len(pytz.all_timezones)] ) ) ) for i in range(SIZE)], "mixed timezone ISO 8601 strings": [datetime.fromtimestamp( i, tz=pytz.timezone( pytz.all_timezones[i % len(pytz.all_timezones)] ) ).isoformat() for i in range(SIZE)], "timedelta strings": [str(timedelta(seconds=i + 1)) for i in range(SIZE)], "pd.Timedelta strings": [str(pd.Timedelta(timedelta(seconds=i + 1))) for i in range(SIZE)] }, bool: { "booleans": [bool((i + 1) % 2) for i in range(SIZE)] }, datetime: { "aware datetimes": [datetime.fromtimestamp(i, tz=timezone.utc) for i in range(SIZE)], "naive datetimes": [datetime.fromtimestamp(i) for i in range(SIZE)], "aware/naive datetimes": [datetime.fromtimestamp(i, tz=timezone.utc) if i % 2 else datetime.fromtimestamp(i) for i in range(SIZE)], "mixed timezone datetimes": [datetime.fromtimestamp( i, tz = pytz.timezone( pytz.all_timezones[i % len(pytz.all_timezones)] ) ) for i in range(SIZE)] }, timedelta: { "timedeltas": [timedelta(seconds=i + 1) for i in range(SIZE)] }, object: { "Nones": [None for _ in range(SIZE)], "custom objects": [TestObj() for _ in range(SIZE)] } } ALL_DATA = {col_name: data for v in TEST_DATA.values() for col_name, data in v.items()} class CheckDtypeTests(unittest.TestCase): def test_check_integers_series_no_na(self): failed = [] for col_name, data in ALL_DATA.items(): series = pd.Series(data) result = check_dtypes(series, int) expected = col_name in TEST_DATA[int] try: self.assertEqual(result, expected) except AssertionError: context = f"check_dtypes({data[:3]}..., int) != {expected}" failed.append(context) if len(failed) > 0: joined = "\n\t".join(failed) raise AssertionError(f"{len(failed)} failed checks:\n\t{joined}") def test_check_integers_series_with_na(self): failed = [] for col_name, data in ALL_DATA.items(): series = pd.Series(data + [None]) result = check_dtypes(series, int) expected = col_name in TEST_DATA[int] try: self.assertEqual(result, expected) except AssertionError: context = f"check_dtypes({data[:3]}..., int) != {expected}" failed.append(context) if len(failed) > 0: joined = "\n\t".join(failed) raise AssertionError(f"{len(failed)} failed checks:\n\t{joined}") def test_check_integers_df_no_na(self): df = pd.DataFrame(ALL_DATA) failed = [] for col_name in df.columns: result = check_dtypes(df, {col_name: int}) expected = col_name in TEST_DATA[int] try: self.assertEqual(result, expected) except AssertionError: context = (f"check_dtypes(df, {{{repr(col_name)}: int}}) != " f"{expected}") failed.append(context) if len(failed) > 0: joined = "\n\t".join(failed) raise AssertionError(f"{len(failed)} failed checks:\n\t{joined}") def test_check_integers_df_with_na(self): with_na = {k: v + [None] for k, v in ALL_DATA.items()} df = pd.DataFrame(with_na) failed = [] for col_name in df.columns: result = check_dtypes(df, {col_name: int}) expected = col_name in TEST_DATA[int] try: self.assertEqual(result, expected) except AssertionError: context = (f"check_dtypes(df, {{{repr(col_name)}: int}}) != " f"{expected}") failed.append(context) if len(failed) > 0: joined = "\n\t".join(failed) raise AssertionError(f"{len(failed)} failed checks:\n\t{joined}") def test_check_floats_series_no_na(self): failed = [] for col_name, data in ALL_DATA.items(): series = pd.Series(data) result = check_dtypes(series, float) expected = col_name in TEST_DATA[float] try: self.assertEqual(result, expected) except AssertionError: context = f"check_dtypes({data[:3]}..., float) != {expected}" failed.append(context) if len(failed) > 0: joined = "\n\t".join(failed) raise AssertionError(f"{len(failed)} failed checks:\n\t{joined}") def test_check_floats_series_with_na(self): failed = [] for col_name, data in ALL_DATA.items(): series = pd.Series(data + [None]) result = check_dtypes(series, float) expected = col_name in TEST_DATA[float] try: self.assertEqual(result, expected) except AssertionError: context = f"check_dtypes({data[:3]}..., float) != {expected}" failed.append(context) if len(failed) > 0: joined = "\n\t".join(failed) raise AssertionError(f"{len(failed)} failed checks:\n\t{joined}") def test_check_floats_df_no_na(self): df = pd.DataFrame(ALL_DATA) failed = [] for col_name in df.columns: result = check_dtypes(df, {col_name: float}) expected = col_name in TEST_DATA[float] try: self.assertEqual(result, expected) except AssertionError: context = (f"check_dtypes(df, {{{repr(col_name)}: float}}) != " f"{expected}") failed.append(context) if len(failed) > 0: joined = "\n\t".join(failed) raise AssertionError(f"{len(failed)} failed checks:\n\t{joined}") def test_check_floats_df_with_na(self): with_na = {k: v + [None] for k, v in ALL_DATA.items()} df = pd.DataFrame(with_na) failed = [] for col_name in df.columns: result = check_dtypes(df, {col_name: float}) expected = col_name in TEST_DATA[float] try: self.assertEqual(result, expected) except AssertionError: context = (f"check_dtypes(df, {{{repr(col_name)}: float}}) != " f"{expected}") failed.append(context) if len(failed) > 0: joined = "\n\t".join(failed) raise AssertionError(f"{len(failed)} failed checks:\n\t{joined}") def test_check_complex_series_no_na(self): failed = [] for col_name, data in ALL_DATA.items(): series = pd.Series(data) result = check_dtypes(series, complex) expected = col_name in TEST_DATA[complex] try: self.assertEqual(result, expected) except AssertionError: context = f"check_dtypes({data[:3]}..., complex) != {expected}" failed.append(context) if len(failed) > 0: joined = "\n\t".join(failed) raise AssertionError(f"{len(failed)} failed checks:\n\t{joined}") def test_check_complex_series_with_na(self): failed = [] for col_name, data in ALL_DATA.items(): series = pd.Series(data + [None]) result = check_dtypes(series, complex) expected = col_name in TEST_DATA[complex] try: self.assertEqual(result, expected) except AssertionError: context = f"check_dtypes({data[:3]}..., complex) != {expected}" failed.append(context) if len(failed) > 0: joined = "\n\t".join(failed) raise AssertionError(f"{len(failed)} failed checks:\n\t{joined}") def test_check_complex_df_no_na(self): df = pd.DataFrame(ALL_DATA) failed = [] for col_name in df.columns: result = check_dtypes(df, {col_name: complex}) expected = col_name in TEST_DATA[complex] try: self.assertEqual(result, expected) except AssertionError: context = (f"check_dtypes(df, {{{repr(col_name)}: complex}}) " f"!= {expected}") failed.append(context) if len(failed) > 0: joined = "\n\t".join(failed) raise AssertionError(f"{len(failed)} failed checks:\n\t{joined}") def test_check_complex_df_with_na(self): with_na = {k: v + [None] for k, v in ALL_DATA.items()} df = pd.DataFrame(with_na) failed = [] for col_name in df.columns: result = check_dtypes(df, {col_name: complex}) expected = col_name in TEST_DATA[complex] try: self.assertEqual(result, expected) except AssertionError: context = (f"check_dtypes(df, {{{repr(col_name)}: complex}}) " f"!= {expected}") failed.append(context) if len(failed) > 0: joined = "\n\t".join(failed) raise AssertionError(f"{len(failed)} failed checks:\n\t{joined}") def test_check_strings_series_no_na(self): failed = [] for col_name, data in ALL_DATA.items(): series = pd.Series(data) result = check_dtypes(series, str) expected = col_name in TEST_DATA[str] try: self.assertEqual(result, expected) except AssertionError: context = f"check_dtypes({data[:3]}..., str) != {expected}" failed.append(context) if len(failed) > 0: joined = "\n\t".join(failed) raise AssertionError(f"{len(failed)} failed checks:\n\t{joined}") def test_check_strings_series_with_na(self): failed = [] for col_name, data in ALL_DATA.items(): series = pd.Series(data + [None]) result = check_dtypes(series, str) expected = col_name in TEST_DATA[str] try: self.assertEqual(result, expected) except AssertionError: context = f"check_dtypes({data[:3]}..., str) != {expected}" failed.append(context) if len(failed) > 0: joined = "\n\t".join(failed) raise AssertionError(f"{len(failed)} failed checks:\n\t{joined}") def test_check_strings_df_no_na(self): df = pd.DataFrame(ALL_DATA) failed = [] for col_name in df.columns: result = check_dtypes(df, {col_name: str}) expected = col_name in TEST_DATA[str] try: self.assertEqual(result, expected) except AssertionError: context = (f"check_dtypes(df, {{{repr(col_name)}: str}}) != " f"{expected}") failed.append(context) if len(failed) > 0: joined = "\n\t".join(failed) raise AssertionError(f"{len(failed)} failed checks:\n\t{joined}") def test_check_strings_df_with_na(self): with_na = {k: v + [None] for k, v in ALL_DATA.items()} df = pd.DataFrame(with_na) failed = [] for col_name in df.columns: result = check_dtypes(df, {col_name: str}) expected = col_name in TEST_DATA[str] try: self.assertEqual(result, expected) except AssertionError: context = (f"check_dtypes(df, {{{repr(col_name)}: str}}) != " f"{expected}") failed.append(context) if len(failed) > 0: joined = "\n\t".join(failed) raise AssertionError(f"{len(failed)} failed checks:\n\t{joined}") def test_check_booleans_series_no_na(self): failed = [] for col_name, data in ALL_DATA.items(): series =
pd.Series(data)
pandas.Series
from collections import OrderedDict import math import pandas as pd import pytest from numpy import nan, round, sqrt, floor, log as ln from numpy.testing import assert_almost_equal from pandas.util.testing import assert_frame_equal from fklearn.training.transformation import ( selector, capper, floorer, prediction_ranger, count_categorizer, label_categorizer, quantile_biner, truncate_categorical, rank_categorical, onehot_categorizer, target_categorizer, standard_scaler, ecdfer, discrete_ecdfer, custom_transformer, value_mapper, null_injector, missing_warner, ) def test_selector(): input_df = pd.DataFrame( {"feat1": [1, 2, 3], "feat2": [100, 200, 300], "target": [0, 1, 0]} ) expected = pd.DataFrame({"feat1": [1, 2, 3], "target": [0, 1, 0]}) expected2 = pd.DataFrame({"feat1": [1, 2, 3]}) pred_fn, data, log = selector(input_df, ["feat1", "target"], ["feat1"]) # the transformed input df should contain both feat1 and target assert expected.equals(data) # but applying the result function should result in a df containing only feat1 assert expected2.equals(pred_fn(input_df)) def test_capper(): input_df = pd.DataFrame({"feat1": [10, 13, 50], "feat2": [50, 75, None]}) input_df2 = pd.DataFrame({"feat1": [7, 15], "feat2": [200, None]}) expected1 = pd.DataFrame({"feat1": [9, 9, 9], "feat2": [50, 75, None]}) expected2 = pd.DataFrame({"feat1": [7, 9], "feat2": [75, None]}) pred_fn1, data1, log = capper(input_df, ["feat1", "feat2"], {"feat1": 9}) pred_fn2, data2, log = capper( input_df, ["feat1", "feat2"], {"feat1": 9}, suffix="_suffix" ) pred_fn3, data3, log = capper( input_df, ["feat1", "feat2"], {"feat1": 9}, prefix="prefix_" ) pred_fn4, data4, log = capper( input_df, ["feat1", "feat2"], {"feat1": 9}, columns_mapping={"feat1": "feat1_raw", "feat2": "feat2_raw"}, ) assert expected1.equals(data1) assert expected2.equals(pred_fn1(input_df2)) assert pd.concat( [expected1, input_df.copy().add_suffix("_suffix")], axis=1 ).equals(data2) assert pd.concat( [expected2, input_df2.copy().add_suffix("_suffix")], axis=1 ).equals(pred_fn2(input_df2)) assert pd.concat( [expected1, input_df.copy().add_prefix("prefix_")], axis=1 ).equals(data3) assert pd.concat( [expected2, input_df2.copy().add_prefix("prefix_")], axis=1 ).equals(pred_fn3(input_df2)) assert pd.concat( [expected1, input_df.copy().add_suffix("_raw")], axis=1 ).equals(data4) assert pd.concat( [expected2, input_df2.copy().add_suffix("_raw")], axis=1 ).equals(pred_fn4(input_df2)) def test_floorer(): input_df = pd.DataFrame({"feat1": [10, 13, 10], "feat2": [50, 75, None]}) input_df2 = pd.DataFrame({"feat1": [7, 15], "feat2": [15, None]}) expected1 = pd.DataFrame({"feat1": [11, 13, 11], "feat2": [50, 75, None]}) expected2 =
pd.DataFrame({"feat1": [11, 15], "feat2": [50, None]})
pandas.DataFrame
# -- coding: utf-8 -- import os from time import sleep import pandas as pd import numpy as np # read data dataset =
pd.read_csv('C:/Users/sch/PycharmProjects/pythonProject3/data/Alibaba_requests_up_5min.csv')
pandas.read_csv
#----------------------------------------------------------------------------- # Copyright (c) 2012 - 2018, Anaconda, Inc. and Intake contributors # All rights reserved. # # The full license is in the LICENSE file, distributed with this software. #----------------------------------------------------------------------------- import os.path import pickle import pytest import pandas as pd from intake.source.tests.util import verify_datasource_interface from .util import assert_items_equal from intake import Catalog, RemoteCatalog from intake.catalog.remote import RemoteCatalogEntry TEST_CATALOG_PATH = os.path.join(os.path.dirname(__file__), 'catalog1.yml') def test_info_describe(intake_server): catalog = Catalog(intake_server) assert_items_equal(list(catalog), ['use_example1', 'nested', 'entry1', 'entry1_part', 'remote_env', 'local_env', 'text', 'arr', 'datetime']) info = catalog['entry1'].describe() expected = { 'container': 'dataframe', 'description': 'entry1 full', 'name': 'entry1', 'direct_access': 'forbid', 'user_parameters': [] } for k in expected: assert info[k] == expected[k] info = catalog['entry1_part'].describe() assert info['direct_access'] == 'allow' def test_bad_url(intake_server): bad_url = intake_server + '/nonsense_prefix' with pytest.raises(Exception): Catalog(bad_url) def test_metadata(intake_server): catalog = Catalog(intake_server) assert hasattr(catalog, 'metadata') assert catalog.metadata['test'] is True assert catalog.version == 1 def test_nested_remote(intake_server): from intake.catalog.local import LocalCatalogEntry catalog = Catalog() catalog._entries = { 'server': LocalCatalogEntry('server', 'remote test', 'intake_remote', True, {'url': intake_server}, [], [], {}, None) } assert 'entry1' in catalog.server() def test_remote_direct(intake_server): from intake.container.dataframe import RemoteDataFrame catalog = Catalog(intake_server) s0 = catalog.entry1() s0.discover() s = RemoteDataFrame(intake_server.replace('intake', 'http'), {}, name='entry1', parameters={}, npartitions=s0.npartitions, shape=s0.shape, metadata=s0.metadata, dtype=s0.dtype) assert s0.read().equals(s.read()) def test_entry_metadata(intake_server): catalog = Catalog(intake_server) entry = catalog['arr'] assert entry.metadata == entry().metadata def test_unknown_source(intake_server): catalog = Catalog(intake_server) with pytest.raises(Exception): catalog['does_not_exist'].describe() def test_remote_datasource_interface(intake_server): catalog = Catalog(intake_server) d = catalog['entry1'].get() verify_datasource_interface(d) def test_environment_evaluation(intake_server): catalog = Catalog(intake_server) import os os.environ['INTAKE_TEST'] = 'client' catalog['remote_env'] def test_read(intake_server): catalog = Catalog(intake_server) d = catalog['entry1'].get() test_dir = os.path.dirname(__file__) file1 = os.path.join(test_dir, 'entry1_1.csv') file2 = os.path.join(test_dir, 'entry1_2.csv') expected_df = pd.concat((pd.read_csv(file1), pd.read_csv(file2))) meta = expected_df[:0] info = d.discover() assert info['datashape'] is None assert info['dtype'] == {k: str(v) for k, v in meta.dtypes.to_dict().items()} assert info['npartitions'] == 2 assert info['shape'] == (None, 3) # Do not know CSV size ahead of time md = d.metadata.copy() md.pop('catalog_dir', None) assert md == dict(foo='bar', bar=[1, 2, 3]) df = d.read() assert expected_df.equals(df) def test_read_direct(intake_server): catalog = Catalog(intake_server) d = catalog['entry1_part'].get(part='2') test_dir = os.path.dirname(__file__) file2 = os.path.join(test_dir, 'entry1_2.csv') expected_df = pd.read_csv(file2) meta = expected_df[:0] info = d.discover() assert info['datashape'] is None assert info['dtype'] == {k: str(v) for k, v in meta.dtypes.to_dict().items()} assert info['npartitions'] == 1 assert info['shape'] == (None, 3) # Do not know CSV size ahead of time md = info['metadata'].copy() md.pop('catalog_dir', None) assert md == {'bar': [2, 4, 6], 'foo': 'baz'} md = d.metadata.copy() md.pop('catalog_dir', None) assert md == dict(foo='baz', bar=[2, 4, 6]) assert d.description == 'entry1 part' df = d.read() assert expected_df.equals(df) def test_read_chunks(intake_server): catalog = Catalog(intake_server) d = catalog.entry1.get() chunks = list(d.read_chunked()) assert len(chunks) == 2 test_dir = os.path.dirname(__file__) file1 = os.path.join(test_dir, 'entry1_1.csv') file2 = os.path.join(test_dir, 'entry1_2.csv') expected_df = pd.concat((pd.read_csv(file1),
pd.read_csv(file2)
pandas.read_csv
import os import copy import pytest import numpy as np import pandas as pd import pyarrow as pa from pyarrow import feather as pf from pyarrow import parquet as pq from time_series_transform.io.base import io_base from time_series_transform.io.numpy import ( from_numpy, to_numpy ) from time_series_transform.io.pandas import ( from_pandas, to_pandas ) from time_series_transform.io.arrow import ( from_arrow_record_batch, from_arrow_table, to_arrow_record_batch, to_arrow_table ) from time_series_transform.transform_core_api.base import ( Time_Series_Data, Time_Series_Data_Collection ) from time_series_transform.io.parquet import ( from_parquet, to_parquet ) from time_series_transform.io.feather import ( from_feather, to_feather ) @pytest.fixture(scope = 'class') def dictList_single(): return { 'time': [1, 2], 'data': [1, 2] } @pytest.fixture(scope = 'class') def dictList_collection(): return { 'time': [1,2,1,3], 'data':[1,2,1,2], 'category':[1,1,2,2] } @pytest.fixture(scope = 'class') def expect_single_expandTime(): return { 'data_1':[1], 'data_2':[2] } @pytest.fixture(scope = 'class') def expect_single_seperateLabel(): return [{ 'time': [1, 2], 'data': [1, 2] }, { 'data_label': [1, 2] }] @pytest.fixture(scope = 'class') def expect_collection_seperateLabel(): return [{ 'time': [1,2,1,3], 'data':[1,2,1,2], 'category':[1,1,2,2] }, { 'data_label':[1,2,1,2] } ] @pytest.fixture(scope = 'class') def expect_collection_expandTime(): return { 'pad': { 'data_1':[1,1], 'data_2':[2,np.nan], 'data_3':[np.nan,2], 'category':[1,2] }, 'remove': { 'data_1':[1,1], 'category':[1,2] } } @pytest.fixture(scope = 'class') def expect_collection_expandCategory(): return { 'pad': { 'time':[1,2,3], 'data_1':[1,2,np.nan], 'data_2':[1,np.nan,2] }, 'remove': { 'time':[1], 'data_1':[1], 'data_2':[1] } } @pytest.fixture(scope = 'class') def expect_collection_expandFull(): return { 'pad': { 'data_1_1':[1], 'data_2_1':[1], 'data_1_2':[2], 'data_2_2':[np.nan], 'data_1_3':[np.nan], 'data_2_3':[2] }, 'remove': { 'data_1_1':[1], 'data_2_1':[1], } } @pytest.fixture(scope = 'class') def expect_collection_noExpand(): return { 'ignore':{ 'time': [1,2,1,3], 'data':[1,2,1,2], 'category':[1,1,2,2] }, 'pad': { 'time': [1,2,3,1,2,3], 'data':[1,2,np.nan,1,np.nan,2], 'category':[1,1,1,2,2,2] }, 'remove': { 'time': [1,1], 'data':[1,1], 'category':[1,2] } } @pytest.fixture(scope = 'class') def seq_single(): return { 'time':[1,2,3], 'data':[[1,2,3],[11,12,13],[21,22,23]] } @pytest.fixture(scope = 'class') def seq_collection(): return { 'time':[1,2,1,2], 'data':[[1,2],[1,2],[2,2],[2,2]], 'category':[1,1,2,2] } @pytest.fixture(scope = 'class') def expect_seq_collection(): return { 'data_1_1':[[1,2]], 'data_2_1':[[2,2]], 'data_1_2':[[1,2]], 'data_2_2':[[2,2]] } class Test_base_io: def test_base_io_from_single(self, dictList_single,expect_single_expandTime): ExpandTimeAns = expect_single_expandTime data = dictList_single ts = Time_Series_Data() ts = ts.set_time_index(data['time'], 'time') ts = ts.set_data(data['data'], 'data') io = io_base(ts, 'time', None) timeSeries = io.from_single(False) for i in timeSeries: assert timeSeries[i].tolist() == data[i] timeSeries = io.from_single(True) for i in timeSeries: assert timeSeries[i] == ExpandTimeAns[i] def test_base_io_to_single(self, dictList_single): data = dictList_single ts = Time_Series_Data() ts = ts.set_time_index(data['time'], 'time') ts = ts.set_data(data['data'], 'data') io = io_base(data, 'time', None) assert io.to_single() == ts def test_base_io_from_collection_expandTime(self, dictList_collection,expect_collection_expandTime): noChange = dictList_collection expand = expect_collection_expandTime fullExpand = [] data = dictList_collection ts = Time_Series_Data() ts = ts.set_time_index(data['time'], 'time') ts = ts.set_data(data['data'], 'data') ts = ts.set_data(data['category'],'category') tsc = Time_Series_Data_Collection(ts,'time','category') io = io_base(tsc, 'time', 'category') with pytest.raises(ValueError): timeSeries = io.from_collection(False,True,'ignore') timeSeries = io.from_collection(False,True,'pad') for i in timeSeries: np.testing.assert_equal(timeSeries[i],expand['pad'][i]) timeSeries = io.from_collection(False,True,'remove') for i in timeSeries: np.testing.assert_equal(timeSeries[i],expand['remove'][i]) def test_base_io_from_collection_expandCategory(self, dictList_collection,expect_collection_expandCategory): noChange = dictList_collection expand = expect_collection_expandCategory fullExpand = [] data = dictList_collection ts = Time_Series_Data() ts = ts.set_time_index(data['time'], 'time') ts = ts.set_data(data['data'], 'data') ts = ts.set_data(data['category'],'category') tsc = Time_Series_Data_Collection(ts,'time','category') io = io_base(tsc, 'time', 'category') with pytest.raises(ValueError): timeSeries = io.from_collection(True,False,'ignore') timeSeries = io.from_collection(True,False,'pad') for i in timeSeries: np.testing.assert_equal(timeSeries[i],expand['pad'][i]) timeSeries = io.from_collection(True,False,'remove') for i in timeSeries: np.testing.assert_equal(timeSeries[i],expand['remove'][i]) def test_base_io_from_collection_expandFull(self, dictList_collection,expect_collection_expandFull): noChange = dictList_collection expand = expect_collection_expandFull fullExpand = [] data = dictList_collection ts = Time_Series_Data() ts = ts.set_time_index(data['time'], 'time') ts = ts.set_data(data['data'], 'data') ts = ts.set_data(data['category'],'category') tsc = Time_Series_Data_Collection(ts,'time','category') io = io_base(tsc, 'time', 'category') timeSeries = io.from_collection(True,True,'pad') for i in timeSeries: np.testing.assert_equal(timeSeries[i],expand['pad'][i]) timeSeries = io.from_collection(True,True,'remove') for i in timeSeries: np.testing.assert_equal(timeSeries[i],expand['remove'][i]) def test_base_io_to_collection(self, dictList_collection): dataList = dictList_collection io = io_base(dataList, 'time', 'category') testData = io.to_collection() tsd = Time_Series_Data(dataList,'time') tsc = Time_Series_Data_Collection(tsd,'time','category') assert testData== tsc def test_base_io_from_collection_no_expand(self,dictList_collection,expect_collection_noExpand): noChange = dictList_collection expand = expect_collection_noExpand data = dictList_collection ts = Time_Series_Data() ts = ts.set_time_index(data['time'], 'time') ts = ts.set_data(data['data'], 'data') ts = ts.set_data(data['category'],'category') tsc = Time_Series_Data_Collection(ts,'time','category') io = io_base(tsc, 'time', 'category') timeSeries = io.from_collection(False,False,'ignore') for i in timeSeries: np.testing.assert_array_equal(timeSeries[i],expand['ignore'][i]) timeSeries = io.from_collection(False,False,'pad') for i in timeSeries: np.testing.assert_equal(timeSeries[i],expand['pad'][i]) timeSeries = io.from_collection(False,False,'remove') for i in timeSeries: np.testing.assert_equal(timeSeries[i],expand['remove'][i]) class Test_Pandas_IO: def test_from_pandas_single(self,dictList_single): data = dictList_single df = pd.DataFrame(dictList_single) tsd = Time_Series_Data(data,'time') testData = from_pandas(df,'time',None) assert tsd == testData def test_from_pandas_collection(self,dictList_collection): data = dictList_collection df = pd.DataFrame(dictList_collection) tsd = Time_Series_Data(data,'time') tsc = Time_Series_Data_Collection(tsd,'time','category') testData = from_pandas(df,'time','category') assert tsc == testData def test_to_pandas_single(self,dictList_single,expect_single_expandTime): data = dictList_single df = pd.DataFrame(data) expandTime = pd.DataFrame(expect_single_expandTime) tsd = Time_Series_Data(data,'time') testData = to_pandas( tsd, expandCategory= None, expandTime=False, preprocessType= None ) pd.testing.assert_frame_equal(testData,df,check_dtype=False) testData = to_pandas( tsd, expandCategory= None, expandTime=True, preprocessType= None ) pd.testing.assert_frame_equal(testData,expandTime,check_dtype=False) def test_to_pandas_collection_expandTime(self,dictList_collection,expect_collection_expandTime): data = dictList_collection expandTime_pad = pd.DataFrame(expect_collection_expandTime['pad']) expandTime_remove = pd.DataFrame(expect_collection_expandTime['remove']) tsd = Time_Series_Data(data,'time') tsc = Time_Series_Data_Collection(tsd,'time','category') testData = to_pandas( tsc, expandCategory= False, expandTime=True, preprocessType= 'pad' ) pd.testing.assert_frame_equal(testData,expandTime_pad,check_dtype=False) testData = to_pandas( tsc, expandCategory= False, expandTime=True, preprocessType= 'remove' ) pd.testing.assert_frame_equal(testData,expandTime_remove,check_dtype=False) with pytest.raises(ValueError): timeSeries = to_pandas(tsc,False,True,'ignore') def test_to_pandas_collection_expandCategory(self,dictList_collection,expect_collection_expandCategory): data = dictList_collection expandTime_pad = pd.DataFrame(expect_collection_expandCategory['pad']) expandTime_remove = pd.DataFrame(expect_collection_expandCategory['remove']) tsd = Time_Series_Data(data,'time') tsc = Time_Series_Data_Collection(tsd,'time','category') testData = to_pandas( tsc, expandCategory= True, expandTime=False, preprocessType= 'pad' ) pd.testing.assert_frame_equal(testData,expandTime_pad,check_dtype=False) testData = to_pandas( tsc, expandCategory= True, expandTime=False, preprocessType= 'remove' ) pd.testing.assert_frame_equal(testData,expandTime_remove,check_dtype=False) with pytest.raises(ValueError): timeSeries = to_pandas(tsc,True,False,'ignore') def test_to_pandas_collection_expandFull(self,dictList_collection,expect_collection_expandFull): data = dictList_collection expandTime_pad = pd.DataFrame(expect_collection_expandFull['pad']) expandTime_remove = pd.DataFrame(expect_collection_expandFull['remove']) tsd = Time_Series_Data(data,'time') tsc = Time_Series_Data_Collection(tsd,'time','category') testData = to_pandas( tsc, expandCategory= True, expandTime=True, preprocessType= 'pad' ) pd.testing.assert_frame_equal(testData,expandTime_pad,check_dtype=False) testData = to_pandas( tsc, expandCategory= True, expandTime=True, preprocessType= 'remove' ) pd.testing.assert_frame_equal(testData,expandTime_remove,check_dtype=False) def test_to_pandas_collection_noExpand(self,dictList_collection,expect_collection_noExpand): data = dictList_collection expandTime_ignore = pd.DataFrame(expect_collection_noExpand['ignore']) expandTime_pad = pd.DataFrame(expect_collection_noExpand['pad']) expandTime_remove = pd.DataFrame(expect_collection_noExpand['remove']) tsd = Time_Series_Data(data,'time') tsc = Time_Series_Data_Collection(tsd,'time','category') testData = to_pandas( tsc, expandCategory= False, expandTime=False, preprocessType= 'pad' ) pd.testing.assert_frame_equal(testData,expandTime_pad,check_dtype=False) testData = to_pandas( tsc, expandCategory= False, expandTime=False, preprocessType= 'remove' ) pd.testing.assert_frame_equal(testData,expandTime_remove,check_dtype=False) testData = to_pandas( tsc, expandCategory= False, expandTime=False, preprocessType= 'ignore' ) pd.testing.assert_frame_equal(testData,expandTime_ignore,check_dtype=False) def test_to_pandas_seperateLabels_single(self,dictList_single,expect_single_seperateLabel): data = dictList_single expectedX, expectedY = expect_single_seperateLabel expectedX = pd.DataFrame(expectedX) expectedY = pd.DataFrame(expectedY) tsd = Time_Series_Data(data,'time') tsd.set_labels([1,2],'data_label') x, y = to_pandas(tsd,False,False,'ignore',True) print(x) print(y) pd.testing.assert_frame_equal(x,expectedX,check_dtype=False) pd.testing.assert_frame_equal(y,expectedY,check_dtype=False) def test_to_pandas_seperateLabels_collection(self,dictList_collection,expect_collection_seperateLabel): data = dictList_collection expectedX, expectedY = expect_collection_seperateLabel expectedX = pd.DataFrame(expectedX) expectedY = pd.DataFrame(expectedY) tsd = Time_Series_Data(data,'time') tsd = tsd.set_labels([1,2,1,2],'data_label') tsc = Time_Series_Data_Collection(tsd,'time','category') x, y = to_pandas(tsc,False,False,'ignore',True) print(y) pd.testing.assert_frame_equal(x,expectedX,check_dtype=False) pd.testing.assert_frame_equal(y,expectedY,check_dtype=False) def test_to_pandas_single_sequence(self,seq_single): data = seq_single df= pd.DataFrame(data) tsd = Time_Series_Data(data,'time') test = to_pandas(tsd,False,False,'ignore',False) pd.testing.assert_frame_equal(test,df,False) def test_to_pandas_collection_sequence(self,seq_collection,expect_seq_collection): data = seq_collection df = pd.DataFrame(data) tsd = Time_Series_Data(data,'time') tsc = Time_Series_Data_Collection(tsd,'time','category') test = to_pandas(tsc,False,False,'ignore') pd.testing.assert_frame_equal(df,test,False) test = to_pandas(tsc,True,True,'ignore') full = pd.DataFrame(expect_seq_collection) print(test) print(full) test = test.reindex(sorted(df.columns), axis=1) full = full.reindex(sorted(df.columns), axis=1) pd.testing.assert_frame_equal(test,full,False) class Test_Numpy_IO: def test_from_numpy_single(self,dictList_single): data = dictList_single tsd = Time_Series_Data() tsd.set_time_index(data['time'],0) tsd.set_data(data['data'],1) numpydata = pd.DataFrame(dictList_single).values testData = from_numpy(numpydata,0,None) assert tsd == testData def test_from_numpy_collection(self,dictList_collection): data = dictList_collection numpyData = pd.DataFrame(data).values numpyDataDict = pd.DataFrame(pd.DataFrame(data).values).to_dict('list') testData = from_numpy(numpyData,0,2) tsd = Time_Series_Data(numpyDataDict,0) assert testData == Time_Series_Data_Collection(tsd,0,2) def test_to_numpy_single(self,dictList_single,expect_single_expandTime): data = dictList_single numpyData = pd.DataFrame(data).values expandTime = pd.DataFrame(expect_single_expandTime).values tsd = Time_Series_Data() tsd.set_time_index(data['time'],0) tsd.set_data(data['data'],1) testData = to_numpy( tsd, expandCategory= None, expandTime=False, preprocessType= None ) np.testing.assert_equal(testData,numpyData) testData = to_numpy( tsd, expandCategory= None, expandTime=True, preprocessType= None ) np.testing.assert_equal(testData,expandTime) def test_to_numpy_collection_expandTime(self,dictList_collection,expect_collection_expandTime): data = dictList_collection results = expect_collection_expandTime numpyData = pd.DataFrame(data).values tsd = Time_Series_Data(data,'time') tsc = Time_Series_Data_Collection(tsd,'time','category') pad_numpy = to_numpy( tsc, expandCategory = False, expandTime = True, preprocessType='pad' ) remove_numpy = to_numpy( tsc, expandCategory = False, expandTime = True, preprocessType='remove' ) np.testing.assert_equal(pad_numpy,pd.DataFrame(results['pad']).values) np.testing.assert_equal(remove_numpy,pd.DataFrame(results['remove']).values) with pytest.raises(ValueError): timeSeries = to_numpy(tsc,False,True,'ignore') def test_to_numpy_collection_expandCategory(self,dictList_collection,expect_collection_expandCategory): data = dictList_collection results = expect_collection_expandCategory numpyData = pd.DataFrame(data).values tsd = Time_Series_Data(data,'time') tsc = Time_Series_Data_Collection(tsd,'time','category') pad_numpy = to_numpy( tsc, expandCategory = True, expandTime = False, preprocessType='pad' ) remove_numpy = to_numpy( tsc, expandCategory = True, expandTime = False, preprocessType='remove' ) np.testing.assert_equal(pad_numpy,pd.DataFrame(results['pad']).values) np.testing.assert_equal(remove_numpy,pd.DataFrame(results['remove']).values) with pytest.raises(ValueError): timeSeries = to_numpy(tsc,False,True,'ignore') def test_to_numpy_collection_expandFull(self,dictList_collection,expect_collection_expandFull): data = dictList_collection results = expect_collection_expandFull numpyData = pd.DataFrame(data).values tsd = Time_Series_Data(data,'time') tsc = Time_Series_Data_Collection(tsd,'time','category') pad_numpy = to_numpy( tsc, expandCategory = True, expandTime = True, preprocessType='pad' ) np.testing.assert_equal(pad_numpy,pd.DataFrame(results['pad']).values) remove_numpy = to_numpy( tsc, expandCategory = True, expandTime = True, preprocessType='remove' ) np.testing.assert_equal(remove_numpy,pd.DataFrame(results['remove']).values) def test_to_numpy_collection_noExpand(self,dictList_collection,expect_collection_noExpand): data = dictList_collection results = expect_collection_noExpand numpyData = pd.DataFrame(data).values tsd = Time_Series_Data(data,'time') tsc = Time_Series_Data_Collection(tsd,'time','category') pad_numpy = to_numpy( tsc, expandCategory = False, expandTime = False, preprocessType='pad' ) np.testing.assert_equal(pad_numpy,pd.DataFrame(results['pad']).values) remove_numpy = to_numpy( tsc, expandCategory = False, expandTime = False, preprocessType='remove' ) np.testing.assert_equal(remove_numpy,pd.DataFrame(results['remove']).values) ignore_numpy = to_numpy( tsc, expandCategory = False, expandTime = False, preprocessType='ignore' ) np.testing.assert_equal(ignore_numpy,pd.DataFrame(results['ignore']).values) def test_to_numpy_seperateLabel_single(self,dictList_single,expect_single_seperateLabel): data = dictList_single expectedX, expectedY = expect_single_seperateLabel expectedX = pd.DataFrame(expectedX).values expectedY = pd.DataFrame(expectedY).values tsd = Time_Series_Data(data,'time') tsd.set_labels([1,2],'data_label') x, y = to_numpy(tsd,False,False,'ignore',True) print(x) print(y) np.testing.assert_equal(x,expectedX) np.testing.assert_equal(y,expectedY) def test_to_numpy_seperateLabel_collection(self,dictList_collection,expect_collection_seperateLabel): data = dictList_collection expectedX, expectedY = expect_collection_seperateLabel expectedX = pd.DataFrame(expectedX).values expectedY = pd.DataFrame(expectedY).values tsd = Time_Series_Data(data,'time') tsd = tsd.set_labels([1,2,1,2],'data_label') tsc = Time_Series_Data_Collection(tsd,'time','category') x, y = to_numpy(tsc,False,False,'ignore',True) np.testing.assert_equal(x,expectedX) np.testing.assert_equal(y,expectedY) def test_to_numpy_single_sequence(self,seq_single): data = seq_single df= pd.DataFrame(data).values tsd = Time_Series_Data(data,'time') test = to_numpy(tsd,False,False,'ignore',False) np.testing.assert_equal(df,test) def test_to_numpy_collection_sequence(self,seq_collection,expect_seq_collection): data = seq_collection df = pd.DataFrame(data).values tsd = Time_Series_Data(data,'time') tsc = Time_Series_Data_Collection(tsd,'time','category') test = to_numpy(tsc,False,False,'ignore') for i in range(len(test)): if isinstance(test[i][1],np.ndarray): test[i][1] = test[i][1].tolist() np.testing.assert_equal(df,test) test = to_numpy(tsc,True,True,'ignore') full = pd.DataFrame(expect_seq_collection).values for i in range(len(test[0])): if isinstance(test[0][i],np.ndarray): test[0][i] = test[0][i].tolist() np.testing.assert_equal(full,test) class Test_Arrow_IO: def test_from_arrow_table_single(self,dictList_single): data = dictList_single df = pd.DataFrame(dictList_single) table = pa.Table.from_pandas(df) testData = from_arrow_table(table,'time',None) tsd = Time_Series_Data(data,'time') assert tsd == testData def test_from_arrow_table_collection(self,dictList_collection): data = dictList_collection df = pd.DataFrame(dictList_collection) tsd = Time_Series_Data(data,'time') tsc = Time_Series_Data_Collection(tsd,'time','category') table = pa.Table.from_pandas(df) testData = from_arrow_table(table,'time','category') assert tsc == testData def test_from_arrow_batch_single(self,dictList_single): data = dictList_single df = pd.DataFrame(dictList_single) table = pa.RecordBatch.from_pandas(df,preserve_index = False) testData = from_arrow_record_batch(table,'time',None) tsd = Time_Series_Data(data,'time') assert tsd == testData def test_from_arrow_batch_collection(self,dictList_collection): data = dictList_collection df = pd.DataFrame(dictList_collection) tsd = Time_Series_Data(data,'time') tsc = Time_Series_Data_Collection(tsd,'time','category') table = pa.RecordBatch.from_pandas(df,preserve_index = False) testData = from_arrow_record_batch(table,'time','category') assert tsc == testData def test_to_arrow_table_single(self,dictList_single,expect_single_expandTime): data = dictList_single df = pd.DataFrame(data) expandTime = pd.DataFrame(expect_single_expandTime) tsd = Time_Series_Data(data,'time') testData = to_arrow_table( tsd, expandCategory= None, expandTime=False, preprocessType= None ).to_pandas() pd.testing.assert_frame_equal(testData,df,check_dtype=False) testData = to_arrow_table( tsd, expandCategory= None, expandTime=True, preprocessType= None ).to_pandas() pd.testing.assert_frame_equal(testData,expandTime,check_dtype=False) def test_to_arrow_table_collection_expandTime(self,dictList_collection,expect_collection_expandTime): data = dictList_collection expandTime_pad = pd.DataFrame(expect_collection_expandTime['pad']) expandTime_remove = pd.DataFrame(expect_collection_expandTime['remove']) tsd = Time_Series_Data(data,'time') tsc = Time_Series_Data_Collection(tsd,'time','category') testData = to_arrow_table( tsc, expandCategory= False, expandTime=True, preprocessType= 'pad' ).to_pandas() pd.testing.assert_frame_equal(testData,expandTime_pad,check_dtype=False) testData = to_arrow_table( tsc, expandCategory= False, expandTime=True, preprocessType= 'remove' ).to_pandas() pd.testing.assert_frame_equal(testData,expandTime_remove,check_dtype=False) with pytest.raises(ValueError): tsc = Time_Series_Data_Collection(tsd,'time','category') timeSeries = to_pandas(tsc,False,True,'ignore') def test_to_arrow_table_collection_expandCategory(self,dictList_collection,expect_collection_expandCategory): data = dictList_collection expandTime_pad = pd.DataFrame(expect_collection_expandCategory['pad']) expandTime_remove = pd.DataFrame(expect_collection_expandCategory['remove']) tsd = Time_Series_Data(data,'time') tsc = Time_Series_Data_Collection(tsd,'time','category') testData = to_arrow_table( tsc, expandCategory= True, expandTime=False, preprocessType= 'pad' ).to_pandas() pd.testing.assert_frame_equal(testData,expandTime_pad,check_dtype=False) testData = to_arrow_table( tsc, expandCategory= True, expandTime=False, preprocessType= 'remove' ).to_pandas()
pd.testing.assert_frame_equal(testData,expandTime_remove,check_dtype=False)
pandas.testing.assert_frame_equal
from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * import pandas as pd import numpy as np from dataclasses import dataclass, field, asdict from node_editor.utils import dumpException from typing import Dict, List, Union, Iterable, Any, TYPE_CHECKING if TYPE_CHECKING: from .dataframe_viewer import DataFrameView @dataclass class Filter: expr: str enabled: bool failed: bool def _align(value: Any) -> Qt.AlignmentFlag: """Align values in the `DataTableModel` cell according to their type Parameters ---------- value: Any value to align Returns ------- Qt.AlignmentFlag Qt.AlignVCenter + Qt.AlignRight in case of numeric type else Qt.AlignVCenter + Qt.AlignLeft """ if
pd.api.types.is_numeric_dtype(value)
pandas.api.types.is_numeric_dtype
import numpy as np import matplotlib.pyplot as plt import pandas as pd from filterpy.kalman import KalmanFilter from filterpy.common import Q_discrete_white_noise # Saves figures to output folder def savefig(name): plt.xlabel('Vreme') plt.savefig('./out/' + name + '.png') # Region to watch REGION = 'Serbia' LOGY = False # Style FONT_DICT = {'fontsize': 24, 'fontweight': 'bold', 'family': 'serif'} FIG_SIZE = (10, 4) # Open-data URL BASE_URL = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/' \ 'csse_covid_19_data/' \ 'csse_covid_19_time_series' confirmed = pd.read_csv(BASE_URL + '/time_series_19-covid-Confirmed.csv', error_bad_lines=False) death =
pd.read_csv(BASE_URL + '/time_series_19-covid-Deaths.csv', error_bad_lines=False)
pandas.read_csv
#Deliveries and Vitals Analyser import base64 import datetime import io import dash_table from dash.dependencies import Input,State,Event,Output import dash_core_components as dcc import dash_html_components as html import pandas as pd import dash_table_experiments as dt from app import app colors = { 'background': '#111111', 'text': '#7FDBFF' } tabs_styles = { 'height': '44px' } tab_style = { 'borderBottom': '1px solid #d6d6d6', 'padding': '6px', 'fontWeight': 'bold', } tab_selected_style = { 'borderTop': '1px solid #d6d6d6', 'borderBottom': '1px solid #d6d6d6', 'backgroundColor': '#119DFF', 'color': 'white', 'padding': '6px' } text_style = { 'color':'#7FDBFF', 'text-align':'justify' } params = [ 'Pulse Rate', 'B.P.', 'Body Temperature', 'WBC Count','weight(opt)', 'Hb', 'Blood Sugar' ] layout = html.Div(style={'backgroundColor': colors['background']}, children=[ #first row html.Div([ html.H2( children='DelDash', style={ 'textAlign': 'center', 'color': colors['text'] } ) ],className='row'), #second row html.Div([ html.Div([ ],className='one columns'), html.Div([ dcc.Link('General & Labor', href='/apps/GLA'), ],className = 'two columns'), html.Div([ dcc.Link('Delivery & Vitals', href='/apps/DVA'), ],className = 'two columns'), html.Div([ dcc.Link('Timeline', href='/apps/TIM'), ],className = 'two columns'), html.Div([ dcc.Link('NTSV-C Section', href='/apps/NTC'), ],className = 'two columns'), html.Div([ dcc.Link('Performance Metrics', href='/apps/OVP'), ],className = 'two columns'), html.Div([ ],className='one columns'), ],className = 'row'), html.Br(), #third row html.Div([ html.Div([ html.Br(), dcc.Tabs(id="tabs", value='tab-1', children=[ dcc.Tab(label='Imminent Delivery', value='tab-1',style=tab_style, selected_style=tab_selected_style), dcc.Tab(label='Non-Iminent Delivery', value='tab-2',style=tab_style, selected_style=tab_selected_style), ]), ],className='six columns'), html.Div([ dcc.Upload( id='upload-data', children=html.Div([ 'Drag and Drop or ', html.A('Select Files') ]), style={ 'width': '90%', 'height': '60px', 'lineHeight': '60px', 'borderWidth': '1px', 'borderStyle': 'dashed', 'borderRadius': '5px', 'textAlign': 'center', 'margin': '10px', 'color':colors['text'] }, # Allow multiple files to be uploaded multiple=True ), html.Div(id='output-data-upload-vitals'), #html.Div(dt.DataTable(rows=[{}]), style={'display': 'none'}) ],className='six columns') ],className='row'), html.Div([ html.Div([ html.Div(id='tabs-content') ],className='six columns') ],className='row'), html.Br(), #fourth row html.Div([ dash_table.DataTable( id='table-editing-simple', columns=( [{'id': 'Serial', 'name': 'Serial'}] + [{'id': p, 'name': p} for p in params] ), data=[ dict(Model=i, **{param: 0 for param in params}) for i in range(1, 5) ], style_header={'backgroundColor': 'rgb(30, 30, 30)'}, style_cell={ 'backgroundColor': 'rgb(50, 50, 50)', 'color': 'white' }, editable=True ), dcc.Graph(id='table-editing-simple-output', style = {'height': 360}) ],className='row') #layout-close ]) #callbacks @app.callback(Output('tabs-content', 'children'), [Input('tabs', 'value')]) def render_content(tab): if tab == 'tab-1': return html.Div([ html.P( children = 'Give one dose of dexamethasone(6mg Intramuscular) and prepare for delivery and neonatal resuscitation.', style = text_style ) ]) elif tab == 'tab-2': return html.Div([ html.P( children = 'Give one pre-referral dose of dexamethasone(6mg Intramuscular) if the patient is to be referred otherwise complete the course. Check Vitals.', style = text_style ) ]) @app.callback( Output('table-editing-simple-output', 'figure'), [Input('table-editing-simple', 'data'), Input('table-editing-simple', 'columns')]) def display_output(rows, columns): df =
pd.DataFrame(rows, columns=[c['name'] for c in columns])
pandas.DataFrame
# coding: utf-8 import pandas as pd import numpy as np import dateutil import requests import datetime from matplotlib import pyplot as plt def smape(actual, predicted): a = np.abs(np.array(actual) - np.array(predicted)) b = np.array(actual) + np.array(predicted) return 2 * np.mean(np.divide(a, b, out=np.zeros_like(a), where=b != 0, casting='unsafe')) from dateutil.parser import parse from datetime import date, timedelta def date_add_hours(start_date, hours): end_date = parse(start_date) + timedelta(hours=hours) end_date = end_date.strftime('%Y-%m-%d %H:%M:%S') return end_date def date_add_days(start_date, days): end_date = parse(start_date[:10]) + timedelta(days=days) end_date = end_date.strftime('%Y-%m-%d') return end_date def diff_of_hours(time1, time2): hours = (parse(time1) - parse(time2)).total_seconds() // 3600 return abs(hours) utc_date = date_add_hours(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), -8) print('现在是UTC时间:{}'.format(utc_date)) print('距离待预测时间还有{}个小时'.format(diff_of_hours(date_add_days(utc_date, 1), utc_date) + 1)) filename = "2018-05-31_ensemble_all_zhoujie" day = "2018-05-31" filepath = '../image/results/' + "".join(day.split("-")) + '/' + filename + '.csv' # 0514 # filepath = './0513commit/bag_55.csv' #0513 # filepath = './0513commit/api_concat_bag_55_6hours.csv' #0513api result = pd.read_csv(filepath) now_date = day api = False # # 北京数据 # In[5]: ## 2018-04到最新的数据 if api: url = 'https://biendata.com/competition/airquality/bj/2018-04-25-0/2018-06-01-0/2k0d1d8' respones = requests.get(url) with open("../image/bj_aq_new_show.csv", 'w') as f: f.write(respones.text) # In[6]: replace_dict = {'wanshouxigong': 'wanshouxig', 'aotizhongxin': 'aotizhongx', 'nongzhanguan': 'nongzhangu', 'fengtaihuayuan': 'fengtaihua', 'miyunshuiku': 'miyunshuik', 'yongdingmennei': 'yongdingme', 'xizhimenbei': 'xizhimenbe'} # In[7]: replace_dict = {'wanshouxigong': 'wanshouxig', 'aotizhongxin': 'aotizhongx', 'nongzhanguan': 'nongzhangu', 'fengtaihuayuan': 'fengtaihua', 'miyunshuiku': 'miyunshuik', 'yongdingmennei': 'yongdingme', 'xizhimenbei': 'xizhimenbe'} bj_aq_new_show = pd.read_csv('../image/bj_aq_new_show.csv') bj_aq_new_show.columns = ['id', 'stationId', 'utc_time', 'PM2.5', 'PM10', 'NO2', 'CO', 'O3', 'SO2'] bj_aq_new_show = bj_aq_new_show[['stationId', 'utc_time', 'PM2.5', 'PM10', 'O3']] bj_aq_new_show['location'] = bj_aq_new_show['stationId'].apply(lambda x: x.split('_aq')[0]) bj_aq_new_show['location'] = bj_aq_new_show['location'].replace(replace_dict) # bj_aq_new_show['utc_time'] = pd.to_datetime(bj_aq_new_show['utc_time']) bj_aq_new_show = bj_aq_new_show[['utc_time', 'PM2.5', 'PM10', 'O3', 'location']] bj_aq_new_show.head(2) # # 伦敦数据 # In[8]: ## London 2018-04到最新的数据 if api: url = 'https://biendata.com/competition/airquality/ld/2018-03-30-23/2018-06-01-01/2k0d1d8' respones = requests.get(url) with open("../image/lw_aq_new.csv", 'w') as f: f.write(respones.text) lw_aq_new =
pd.read_csv('../image/lw_aq_new.csv')
pandas.read_csv
# estos csv se guardan en la carpeta csvs, pero no se dejan en github # estan en https://www.dropbox.com/sh/dpzxb1hwq6n26qh/AAB-HMaoQqEF6l7ZuUtSy5sAa?dl=0 import sqlite3 import pandas as pd NUM_QUESTIONS = 26 db = sqlite3.connect("../predictor_pol/predictor_prod.db", isolation_level=None) cur = db.cursor() sql = """ SELECT encuestas.id, encuestas.candidato_elegido, group_concat(resp.id_pregunta || ":" || resp.respuesta), encuestas.fecha FROM respuestas_encuestas as resp JOIN encuestas ON encuestas.id == resp.id_encuesta WHERE encuestas.candidato_elegido IS NOT NULL GROUP BY resp.id_encuesta """ rows = cur.execute(sql).fetchall() with open("../csvs/data.csv", "w") as f: all_questions = ",".join([str(i) for i in range(1, NUM_QUESTIONS + 1)]) f.write(f"id,candidatoId,{all_questions},fecha\n") for r in rows: id_encuesta = r[0] id_candidato = r[1] fecha_encuesta = r[3] respuestas = r[2].split(",") if len(respuestas) < NUM_QUESTIONS: continue respuestas.sort(key=lambda x: int(x.split(":")[0])) respuestas = ",".join([respuesta.split(":")[1] for respuesta in respuestas]) f.write(f"{id_encuesta},{id_candidato},{respuestas},{fecha_encuesta}\n") sql = """ SELECT id AS cant,fecha FROM encuestas WHERE fecha > '2019-02-17' """ rows = cur.execute(sql).fetchall() d = {"cant": [row[0] for row in rows], "fecha": [row[1] for row in rows]} df =
pd.DataFrame.from_dict(d)
pandas.DataFrame.from_dict
import sys import pandas as pd import numpy as np from scipy import stats def significant(array1,array2): try: arr1 = np.array(array1) arr2 = np.array(array2) print(stats.ttest_ind(arr1,arr2)[1]) return stats.ttest_ind(arr1,arr2)[1] < 0.1 except: print('PROBLEM!') return None def splitStudents(courseID, logins): df = pd.DataFrame.from_csv('../data/course2students/'+str(courseID)+'_students.csv',index_col=None) eqFlag = False iteration = 1 while eqFlag == False: print(iteration) iteration+=1 df['expGroup'] = np.random.choice([1,2],len(df)) print(list(df['expGroup'])) eqFlag = True #check significance of the difference allPrior1 = df[df['expGroup']==1]['allPrior'] allPrior1 = list(allPrior1[
pd.notnull(allPrior1)
pandas.notnull
import boto3 import pandas as pd import warnings warnings.filterwarnings('ignore', category=FutureWarning) warnings.filterwarnings('ignore', category=DeprecationWarning) import argparse import os import warnings warnings.simplefilter(action='ignore') import json print("import your necessary libraries in here") import numpy as np from sklearn.preprocessing import MinMaxScaler from sklearn.model_selection import train_test_split print("enter your project name and enviornment here") project_name = "aws_workshop" user_name='mlops' env = 'dev' print("loading the stage config data from s3") def getJsonData(bucket_name,key_name): print("[LOG]", bucket_name,'---------') print("[LOG]", key_name,'--------------') s3_client = boto3.client('s3') csv_obj = s3_client.get_object(Bucket=bucket_name, Key=key_name) body = csv_obj['Body'] json_string = body.read().decode('utf-8') json_content = json.loads(json_string) return json_content print("setting the parameters for getJsonData function") config_bucket = f"dlk-cloud-tier-8-code-ml-{env}" config_s3_prefix_stage = f'config_files/stage_config/{project_name}/{user_name}/stage_config.json' print("calling the getJsonData function") config = getJsonData(config_bucket,config_s3_prefix_stage) print("json script loaded successfully") print("enter your own functions in the bellow space") def change_format(df): df['TotalCharges'] =
pd.to_numeric(df.TotalCharges, errors='coerce')
pandas.to_numeric
# -*- coding: utf-8 -*- from datetime import timedelta import operator from string import ascii_lowercase import warnings import numpy as np import pytest from pandas.compat import lrange import pandas.util._test_decorators as td import pandas as pd from pandas import ( Categorical, DataFrame, MultiIndex, Series, Timestamp, date_range, isna, notna, to_datetime, to_timedelta) import pandas.core.algorithms as algorithms import pandas.core.nanops as nanops import pandas.util.testing as tm def assert_stat_op_calc(opname, alternative, frame, has_skipna=True, check_dtype=True, check_dates=False, check_less_precise=False, skipna_alternative=None): """ Check that operator opname works as advertised on frame Parameters ---------- opname : string Name of the operator to test on frame alternative : function Function that opname is tested against; i.e. "frame.opname()" should equal "alternative(frame)". frame : DataFrame The object that the tests are executed on has_skipna : bool, default True Whether the method "opname" has the kwarg "skip_na" check_dtype : bool, default True Whether the dtypes of the result of "frame.opname()" and "alternative(frame)" should be checked. check_dates : bool, default false Whether opname should be tested on a Datetime Series check_less_precise : bool, default False Whether results should only be compared approximately; passed on to tm.assert_series_equal skipna_alternative : function, default None NaN-safe version of alternative """ f = getattr(frame, opname) if check_dates: df = DataFrame({'b': date_range('1/1/2001', periods=2)}) result = getattr(df, opname)() assert isinstance(result, Series) df['a'] = lrange(len(df)) result = getattr(df, opname)() assert isinstance(result, Series) assert len(result) if has_skipna: def wrapper(x): return alternative(x.values) skipna_wrapper = tm._make_skipna_wrapper(alternative, skipna_alternative) result0 = f(axis=0, skipna=False) result1 = f(axis=1, skipna=False) tm.assert_series_equal(result0, frame.apply(wrapper), check_dtype=check_dtype, check_less_precise=check_less_precise) # HACK: win32 tm.assert_series_equal(result1, frame.apply(wrapper, axis=1), check_dtype=False, check_less_precise=check_less_precise) else: skipna_wrapper = alternative result0 = f(axis=0) result1 = f(axis=1) tm.assert_series_equal(result0, frame.apply(skipna_wrapper), check_dtype=check_dtype, check_less_precise=check_less_precise) if opname in ['sum', 'prod']: expected = frame.apply(skipna_wrapper, axis=1) tm.assert_series_equal(result1, expected, check_dtype=False, check_less_precise=check_less_precise) # check dtypes if check_dtype: lcd_dtype = frame.values.dtype assert lcd_dtype == result0.dtype assert lcd_dtype == result1.dtype # bad axis with pytest.raises(ValueError, match='No axis named 2'): f(axis=2) # all NA case if has_skipna: all_na = frame * np.NaN r0 = getattr(all_na, opname)(axis=0) r1 = getattr(all_na, opname)(axis=1) if opname in ['sum', 'prod']: unit = 1 if opname == 'prod' else 0 # result for empty sum/prod expected = pd.Series(unit, index=r0.index, dtype=r0.dtype) tm.assert_series_equal(r0, expected) expected = pd.Series(unit, index=r1.index, dtype=r1.dtype) tm.assert_series_equal(r1, expected) def assert_stat_op_api(opname, float_frame, float_string_frame, has_numeric_only=False): """ Check that API for operator opname works as advertised on frame Parameters ---------- opname : string Name of the operator to test on frame float_frame : DataFrame DataFrame with columns of type float float_string_frame : DataFrame DataFrame with both float and string columns has_numeric_only : bool, default False Whether the method "opname" has the kwarg "numeric_only" """ # make sure works on mixed-type frame getattr(float_string_frame, opname)(axis=0) getattr(float_string_frame, opname)(axis=1) if has_numeric_only: getattr(float_string_frame, opname)(axis=0, numeric_only=True) getattr(float_string_frame, opname)(axis=1, numeric_only=True) getattr(float_frame, opname)(axis=0, numeric_only=False) getattr(float_frame, opname)(axis=1, numeric_only=False) def assert_bool_op_calc(opname, alternative, frame, has_skipna=True): """ Check that bool operator opname works as advertised on frame Parameters ---------- opname : string Name of the operator to test on frame alternative : function Function that opname is tested against; i.e. "frame.opname()" should equal "alternative(frame)". frame : DataFrame The object that the tests are executed on has_skipna : bool, default True Whether the method "opname" has the kwarg "skip_na" """ f = getattr(frame, opname) if has_skipna: def skipna_wrapper(x): nona = x.dropna().values return alternative(nona) def wrapper(x): return alternative(x.values) result0 = f(axis=0, skipna=False) result1 = f(axis=1, skipna=False) tm.assert_series_equal(result0, frame.apply(wrapper)) tm.assert_series_equal(result1, frame.apply(wrapper, axis=1), check_dtype=False) # HACK: win32 else: skipna_wrapper = alternative wrapper = alternative result0 = f(axis=0) result1 = f(axis=1) tm.assert_series_equal(result0, frame.apply(skipna_wrapper)) tm.assert_series_equal(result1, frame.apply(skipna_wrapper, axis=1), check_dtype=False) # bad axis with pytest.raises(ValueError, match='No axis named 2'): f(axis=2) # all NA case if has_skipna: all_na = frame * np.NaN r0 = getattr(all_na, opname)(axis=0) r1 = getattr(all_na, opname)(axis=1) if opname == 'any': assert not r0.any() assert not r1.any() else: assert r0.all() assert r1.all() def assert_bool_op_api(opname, bool_frame_with_na, float_string_frame, has_bool_only=False): """ Check that API for boolean operator opname works as advertised on frame Parameters ---------- opname : string Name of the operator to test on frame float_frame : DataFrame DataFrame with columns of type float float_string_frame : DataFrame DataFrame with both float and string columns has_bool_only : bool, default False Whether the method "opname" has the kwarg "bool_only" """ # make sure op works on mixed-type frame mixed = float_string_frame mixed['_bool_'] = np.random.randn(len(mixed)) > 0.5 getattr(mixed, opname)(axis=0) getattr(mixed, opname)(axis=1) if has_bool_only: getattr(mixed, opname)(axis=0, bool_only=True) getattr(mixed, opname)(axis=1, bool_only=True) getattr(bool_frame_with_na, opname)(axis=0, bool_only=False) getattr(bool_frame_with_na, opname)(axis=1, bool_only=False) class TestDataFrameAnalytics(object): # --------------------------------------------------------------------- # Correlation and covariance @td.skip_if_no_scipy def test_corr_pearson(self, float_frame): float_frame['A'][:5] = np.nan float_frame['B'][5:10] = np.nan self._check_method(float_frame, 'pearson') @td.skip_if_no_scipy def test_corr_kendall(self, float_frame): float_frame['A'][:5] = np.nan float_frame['B'][5:10] = np.nan self._check_method(float_frame, 'kendall') @td.skip_if_no_scipy def test_corr_spearman(self, float_frame): float_frame['A'][:5] = np.nan float_frame['B'][5:10] = np.nan self._check_method(float_frame, 'spearman') def _check_method(self, frame, method='pearson'): correls = frame.corr(method=method) expected = frame['A'].corr(frame['C'], method=method) tm.assert_almost_equal(correls['A']['C'], expected) @td.skip_if_no_scipy def test_corr_non_numeric(self, float_frame, float_string_frame): float_frame['A'][:5] = np.nan float_frame['B'][5:10] = np.nan # exclude non-numeric types result = float_string_frame.corr() expected = float_string_frame.loc[:, ['A', 'B', 'C', 'D']].corr() tm.assert_frame_equal(result, expected) @td.skip_if_no_scipy @pytest.mark.parametrize('meth', ['pearson', 'kendall', 'spearman']) def test_corr_nooverlap(self, meth): # nothing in common df = DataFrame({'A': [1, 1.5, 1, np.nan, np.nan, np.nan], 'B': [np.nan, np.nan, np.nan, 1, 1.5, 1], 'C': [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]}) rs = df.corr(meth) assert isna(rs.loc['A', 'B']) assert isna(rs.loc['B', 'A']) assert rs.loc['A', 'A'] == 1 assert rs.loc['B', 'B'] == 1 assert isna(rs.loc['C', 'C']) @td.skip_if_no_scipy @pytest.mark.parametrize('meth', ['pearson', 'spearman']) def test_corr_constant(self, meth): # constant --> all NA df = DataFrame({'A': [1, 1, 1, np.nan, np.nan, np.nan], 'B': [np.nan, np.nan, np.nan, 1, 1, 1]}) rs = df.corr(meth) assert isna(rs.values).all() def test_corr_int(self): # dtypes other than float64 #1761 df3 = DataFrame({"a": [1, 2, 3, 4], "b": [1, 2, 3, 4]}) df3.cov() df3.corr() @td.skip_if_no_scipy def test_corr_int_and_boolean(self): # when dtypes of pandas series are different # then ndarray will have dtype=object, # so it need to be properly handled df = DataFrame({"a": [True, False], "b": [1, 0]}) expected = DataFrame(np.ones((2, 2)), index=[ 'a', 'b'], columns=['a', 'b']) for meth in ['pearson', 'kendall', 'spearman']: with warnings.catch_warnings(record=True): warnings.simplefilter("ignore", RuntimeWarning) result = df.corr(meth) tm.assert_frame_equal(result, expected) def test_corr_cov_independent_index_column(self): # GH 14617 df = pd.DataFrame(np.random.randn(4 * 10).reshape(10, 4), columns=list("abcd")) for method in ['cov', 'corr']: result = getattr(df, method)() assert result.index is not result.columns assert result.index.equals(result.columns) def test_corr_invalid_method(self): # GH 22298 df = pd.DataFrame(np.random.normal(size=(10, 2))) msg = ("method must be either 'pearson', " "'spearman', 'kendall', or a callable, ") with pytest.raises(ValueError, match=msg): df.corr(method="____") def test_cov(self, float_frame, float_string_frame): # min_periods no NAs (corner case) expected = float_frame.cov() result = float_frame.cov(min_periods=len(float_frame)) tm.assert_frame_equal(expected, result) result = float_frame.cov(min_periods=len(float_frame) + 1) assert isna(result.values).all() # with NAs frame = float_frame.copy() frame['A'][:5] = np.nan frame['B'][5:10] = np.nan result = float_frame.cov(min_periods=len(float_frame) - 8) expected = float_frame.cov() expected.loc['A', 'B'] = np.nan expected.loc['B', 'A'] = np.nan # regular float_frame['A'][:5] = np.nan float_frame['B'][:10] = np.nan cov = float_frame.cov() tm.assert_almost_equal(cov['A']['C'], float_frame['A'].cov(float_frame['C'])) # exclude non-numeric types result = float_string_frame.cov() expected = float_string_frame.loc[:, ['A', 'B', 'C', 'D']].cov() tm.assert_frame_equal(result, expected) # Single column frame df = DataFrame(np.linspace(0.0, 1.0, 10)) result = df.cov() expected = DataFrame(np.cov(df.values.T).reshape((1, 1)), index=df.columns, columns=df.columns) tm.assert_frame_equal(result, expected) df.loc[0] = np.nan result = df.cov() expected = DataFrame(np.cov(df.values[1:].T).reshape((1, 1)), index=df.columns, columns=df.columns) tm.assert_frame_equal(result, expected) def test_corrwith(self, datetime_frame): a = datetime_frame noise = Series(np.random.randn(len(a)), index=a.index) b = datetime_frame.add(noise, axis=0) # make sure order does not matter b = b.reindex(columns=b.columns[::-1], index=b.index[::-1][10:]) del b['B'] colcorr = a.corrwith(b, axis=0) tm.assert_almost_equal(colcorr['A'], a['A'].corr(b['A'])) rowcorr = a.corrwith(b, axis=1) tm.assert_series_equal(rowcorr, a.T.corrwith(b.T, axis=0)) dropped = a.corrwith(b, axis=0, drop=True) tm.assert_almost_equal(dropped['A'], a['A'].corr(b['A'])) assert 'B' not in dropped dropped = a.corrwith(b, axis=1, drop=True) assert a.index[-1] not in dropped.index # non time-series data index = ['a', 'b', 'c', 'd', 'e'] columns = ['one', 'two', 'three', 'four'] df1 = DataFrame(np.random.randn(5, 4), index=index, columns=columns) df2 = DataFrame(np.random.randn(4, 4), index=index[:4], columns=columns) correls = df1.corrwith(df2, axis=1) for row in index[:4]: tm.assert_almost_equal(correls[row], df1.loc[row].corr(df2.loc[row])) def test_corrwith_with_objects(self): df1 = tm.makeTimeDataFrame() df2 = tm.makeTimeDataFrame() cols = ['A', 'B', 'C', 'D'] df1['obj'] = 'foo' df2['obj'] = 'bar' result = df1.corrwith(df2) expected = df1.loc[:, cols].corrwith(df2.loc[:, cols]) tm.assert_series_equal(result, expected) result = df1.corrwith(df2, axis=1) expected = df1.loc[:, cols].corrwith(df2.loc[:, cols], axis=1) tm.assert_series_equal(result, expected) def test_corrwith_series(self, datetime_frame): result = datetime_frame.corrwith(datetime_frame['A']) expected = datetime_frame.apply(datetime_frame['A'].corr) tm.assert_series_equal(result, expected) def test_corrwith_matches_corrcoef(self): df1 = DataFrame(np.arange(10000), columns=['a']) df2 = DataFrame(np.arange(10000) ** 2, columns=['a']) c1 = df1.corrwith(df2)['a'] c2 = np.corrcoef(df1['a'], df2['a'])[0][1] tm.assert_almost_equal(c1, c2) assert c1 < 1 def test_corrwith_mixed_dtypes(self): # GH 18570 df = pd.DataFrame({'a': [1, 4, 3, 2], 'b': [4, 6, 7, 3], 'c': ['a', 'b', 'c', 'd']}) s = pd.Series([0, 6, 7, 3]) result = df.corrwith(s) corrs = [df['a'].corr(s), df['b'].corr(s)] expected = pd.Series(data=corrs, index=['a', 'b']) tm.assert_series_equal(result, expected) def test_corrwith_index_intersection(self): df1 = pd.DataFrame(np.random.random(size=(10, 2)), columns=["a", "b"]) df2 = pd.DataFrame(np.random.random(size=(10, 3)), columns=["a", "b", "c"]) result = df1.corrwith(df2, drop=True).index.sort_values() expected = df1.columns.intersection(df2.columns).sort_values() tm.assert_index_equal(result, expected) def test_corrwith_index_union(self): df1 = pd.DataFrame(np.random.random(size=(10, 2)), columns=["a", "b"]) df2 = pd.DataFrame(np.random.random(size=(10, 3)), columns=["a", "b", "c"]) result = df1.corrwith(df2, drop=False).index.sort_values() expected = df1.columns.union(df2.columns).sort_values() tm.assert_index_equal(result, expected) def test_corrwith_dup_cols(self): # GH 21925 df1 = pd.DataFrame(np.vstack([np.arange(10)] * 3).T) df2 = df1.copy() df2 = pd.concat((df2, df2[0]), axis=1) result = df1.corrwith(df2) expected = pd.Series(np.ones(4), index=[0, 0, 1, 2]) tm.assert_series_equal(result, expected) @td.skip_if_no_scipy def test_corrwith_spearman(self): # GH 21925 df = pd.DataFrame(np.random.random(size=(100, 3))) result = df.corrwith(df**2, method="spearman") expected = Series(np.ones(len(result))) tm.assert_series_equal(result, expected) @td.skip_if_no_scipy def test_corrwith_kendall(self): # GH 21925 df = pd.DataFrame(np.random.random(size=(100, 3))) result = df.corrwith(df**2, method="kendall") expected = Series(np.ones(len(result))) tm.assert_series_equal(result, expected) # --------------------------------------------------------------------- # Describe def test_bool_describe_in_mixed_frame(self): df = DataFrame({ 'string_data': ['a', 'b', 'c', 'd', 'e'], 'bool_data': [True, True, False, False, False], 'int_data': [10, 20, 30, 40, 50], }) # Integer data are included in .describe() output, # Boolean and string data are not. result = df.describe() expected = DataFrame({'int_data': [5, 30, df.int_data.std(), 10, 20, 30, 40, 50]}, index=['count', 'mean', 'std', 'min', '25%', '50%', '75%', 'max']) tm.assert_frame_equal(result, expected) # Top value is a boolean value that is False result = df.describe(include=['bool']) expected = DataFrame({'bool_data': [5, 2, False, 3]}, index=['count', 'unique', 'top', 'freq']) tm.assert_frame_equal(result, expected) def test_describe_bool_frame(self): # GH 13891 df = pd.DataFrame({ 'bool_data_1': [False, False, True, True], 'bool_data_2': [False, True, True, True] }) result = df.describe() expected = DataFrame({'bool_data_1': [4, 2, True, 2], 'bool_data_2': [4, 2, True, 3]}, index=['count', 'unique', 'top', 'freq']) tm.assert_frame_equal(result, expected) df = pd.DataFrame({ 'bool_data': [False, False, True, True, False], 'int_data': [0, 1, 2, 3, 4] }) result = df.describe() expected = DataFrame({'int_data': [5, 2, df.int_data.std(), 0, 1, 2, 3, 4]}, index=['count', 'mean', 'std', 'min', '25%', '50%', '75%', 'max']) tm.assert_frame_equal(result, expected) df = pd.DataFrame({ 'bool_data': [False, False, True, True], 'str_data': ['a', 'b', 'c', 'a'] }) result = df.describe() expected = DataFrame({'bool_data': [4, 2, True, 2], 'str_data': [4, 3, 'a', 2]}, index=['count', 'unique', 'top', 'freq']) tm.assert_frame_equal(result, expected) def test_describe_categorical(self): df = DataFrame({'value': np.random.randint(0, 10000, 100)}) labels = ["{0} - {1}".format(i, i + 499) for i in range(0, 10000, 500)] cat_labels = Categorical(labels, labels) df = df.sort_values(by=['value'], ascending=True) df['value_group'] = pd.cut(df.value, range(0, 10500, 500), right=False, labels=cat_labels) cat = df # Categoricals should not show up together with numerical columns result = cat.describe() assert len(result.columns) == 1 # In a frame, describe() for the cat should be the same as for string # arrays (count, unique, top, freq) cat = Categorical(["a", "b", "b", "b"], categories=['a', 'b', 'c'], ordered=True) s = Series(cat) result = s.describe() expected = Series([4, 2, "b", 3], index=['count', 'unique', 'top', 'freq']) tm.assert_series_equal(result, expected) cat = Series(Categorical(["a", "b", "c", "c"])) df3 = DataFrame({"cat": cat, "s": ["a", "b", "c", "c"]}) result = df3.describe() tm.assert_numpy_array_equal(result["cat"].values, result["s"].values) def test_describe_categorical_columns(self): # GH 11558 columns = pd.CategoricalIndex(['int1', 'int2', 'obj'], ordered=True, name='XXX') df = DataFrame({'int1': [10, 20, 30, 40, 50], 'int2': [10, 20, 30, 40, 50], 'obj': ['A', 0, None, 'X', 1]}, columns=columns) result = df.describe() exp_columns = pd.CategoricalIndex(['int1', 'int2'], categories=['int1', 'int2', 'obj'], ordered=True, name='XXX') expected = DataFrame({'int1': [5, 30, df.int1.std(), 10, 20, 30, 40, 50], 'int2': [5, 30, df.int2.std(), 10, 20, 30, 40, 50]}, index=['count', 'mean', 'std', 'min', '25%', '50%', '75%', 'max'], columns=exp_columns) tm.assert_frame_equal(result, expected) tm.assert_categorical_equal(result.columns.values, expected.columns.values) def test_describe_datetime_columns(self): columns = pd.DatetimeIndex(['2011-01-01', '2011-02-01', '2011-03-01'], freq='MS', tz='US/Eastern', name='XXX') df = DataFrame({0: [10, 20, 30, 40, 50], 1: [10, 20, 30, 40, 50], 2: ['A', 0, None, 'X', 1]}) df.columns = columns result = df.describe() exp_columns = pd.DatetimeIndex(['2011-01-01', '2011-02-01'], freq='MS', tz='US/Eastern', name='XXX') expected = DataFrame({0: [5, 30, df.iloc[:, 0].std(), 10, 20, 30, 40, 50], 1: [5, 30, df.iloc[:, 1].std(), 10, 20, 30, 40, 50]}, index=['count', 'mean', 'std', 'min', '25%', '50%', '75%', 'max']) expected.columns = exp_columns tm.assert_frame_equal(result, expected) assert result.columns.freq == 'MS' assert result.columns.tz == expected.columns.tz def test_describe_timedelta_values(self): # GH 6145 t1 = pd.timedelta_range('1 days', freq='D', periods=5) t2 = pd.timedelta_range('1 hours', freq='H', periods=5) df = pd.DataFrame({'t1': t1, 't2': t2}) expected = DataFrame({'t1': [5, pd.Timedelta('3 days'), df.iloc[:, 0].std(), pd.Timedelta('1 days'), pd.Timedelta('2 days'), pd.Timedelta('3 days'), pd.Timedelta('4 days'), pd.Timedelta('5 days')], 't2': [5, pd.Timedelta('3 hours'), df.iloc[:, 1].std(), pd.Timedelta('1 hours'), pd.Timedelta('2 hours'), pd.Timedelta('3 hours'), pd.Timedelta('4 hours'), pd.Timedelta('5 hours')]}, index=['count', 'mean', 'std', 'min', '25%', '50%', '75%', 'max']) result = df.describe() tm.assert_frame_equal(result, expected) exp_repr = (" t1 t2\n" "count 5 5\n" "mean 3 days 00:00:00 0 days 03:00:00\n" "std 1 days 13:56:50.394919 0 days 01:34:52.099788\n" "min 1 days 00:00:00 0 days 01:00:00\n" "25% 2 days 00:00:00 0 days 02:00:00\n" "50% 3 days 00:00:00 0 days 03:00:00\n" "75% 4 days 00:00:00 0 days 04:00:00\n" "max 5 days 00:00:00 0 days 05:00:00") assert repr(result) == exp_repr def test_describe_tz_values(self, tz_naive_fixture): # GH 21332 tz = tz_naive_fixture s1 = Series(range(5)) start = Timestamp(2018, 1, 1) end = Timestamp(2018, 1, 5) s2 = Series(date_range(start, end, tz=tz)) df = pd.DataFrame({'s1': s1, 's2': s2}) expected = DataFrame({'s1': [5, np.nan, np.nan, np.nan, np.nan, np.nan, 2, 1.581139, 0, 1, 2, 3, 4], 's2': [5, 5, s2.value_counts().index[0], 1, start.tz_localize(tz), end.tz_localize(tz), np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]}, index=['count', 'unique', 'top', 'freq', 'first', 'last', 'mean', 'std', 'min', '25%', '50%', '75%', 'max'] ) result = df.describe(include='all') tm.assert_frame_equal(result, expected) # --------------------------------------------------------------------- # Reductions def test_stat_op_api(self, float_frame, float_string_frame): assert_stat_op_api('count', float_frame, float_string_frame, has_numeric_only=True) assert_stat_op_api('sum', float_frame, float_string_frame, has_numeric_only=True) assert_stat_op_api('nunique', float_frame, float_string_frame) assert_stat_op_api('mean', float_frame, float_string_frame) assert_stat_op_api('product', float_frame, float_string_frame) assert_stat_op_api('median', float_frame, float_string_frame) assert_stat_op_api('min', float_frame, float_string_frame) assert_stat_op_api('max', float_frame, float_string_frame) assert_stat_op_api('mad', float_frame, float_string_frame) assert_stat_op_api('var', float_frame, float_string_frame) assert_stat_op_api('std', float_frame, float_string_frame) assert_stat_op_api('sem', float_frame, float_string_frame) assert_stat_op_api('median', float_frame, float_string_frame) try: from scipy.stats import skew, kurtosis # noqa:F401 assert_stat_op_api('skew', float_frame, float_string_frame) assert_stat_op_api('kurt', float_frame, float_string_frame) except ImportError: pass def test_stat_op_calc(self, float_frame_with_na, mixed_float_frame): def count(s): return notna(s).sum() def nunique(s): return len(algorithms.unique1d(s.dropna())) def mad(x): return np.abs(x - x.mean()).mean() def var(x): return np.var(x, ddof=1) def std(x): return np.std(x, ddof=1) def sem(x): return np.std(x, ddof=1) / np.sqrt(len(x)) def skewness(x): from scipy.stats import skew # noqa:F811 if len(x) < 3: return np.nan return skew(x, bias=False) def kurt(x): from scipy.stats import kurtosis # noqa:F811 if len(x) < 4: return np.nan return kurtosis(x, bias=False) assert_stat_op_calc('nunique', nunique, float_frame_with_na, has_skipna=False, check_dtype=False, check_dates=True) # mixed types (with upcasting happening) assert_stat_op_calc('sum', np.sum, mixed_float_frame.astype('float32'), check_dtype=False, check_less_precise=True) assert_stat_op_calc('sum', np.sum, float_frame_with_na, skipna_alternative=np.nansum) assert_stat_op_calc('mean', np.mean, float_frame_with_na, check_dates=True) assert_stat_op_calc('product', np.prod, float_frame_with_na) assert_stat_op_calc('mad', mad, float_frame_with_na) assert_stat_op_calc('var', var, float_frame_with_na) assert_stat_op_calc('std', std, float_frame_with_na) assert_stat_op_calc('sem', sem, float_frame_with_na) assert_stat_op_calc('count', count, float_frame_with_na, has_skipna=False, check_dtype=False, check_dates=True) try: from scipy import skew, kurtosis # noqa:F401 assert_stat_op_calc('skew', skewness, float_frame_with_na) assert_stat_op_calc('kurt', kurt, float_frame_with_na) except ImportError: pass # TODO: Ensure warning isn't emitted in the first place @pytest.mark.filterwarnings("ignore:All-NaN:RuntimeWarning") def test_median(self, float_frame_with_na, int_frame): def wrapper(x): if isna(x).any(): return np.nan return np.median(x) assert_stat_op_calc('median', wrapper, float_frame_with_na, check_dates=True) assert_stat_op_calc('median', wrapper, int_frame, check_dtype=False, check_dates=True) @pytest.mark.parametrize('method', ['sum', 'mean', 'prod', 'var', 'std', 'skew', 'min', 'max']) def test_stat_operators_attempt_obj_array(self, method): # GH#676 data = { 'a': [-0.00049987540199591344, -0.0016467257772919831, 0.00067695870775883013], 'b': [-0, -0, 0.0], 'c': [0.00031111847529610595, 0.0014902627951905339, -0.00094099200035979691] } df1 = DataFrame(data, index=['foo', 'bar', 'baz'], dtype='O') df2 = DataFrame({0: [np.nan, 2], 1: [np.nan, 3], 2: [np.nan, 4]}, dtype=object) for df in [df1, df2]: assert df.values.dtype == np.object_ result = getattr(df, method)(1) expected = getattr(df.astype('f8'), method)(1) if method in ['sum', 'prod']: tm.assert_series_equal(result, expected) @pytest.mark.parametrize('op', ['mean', 'std', 'var', 'skew', 'kurt', 'sem']) def test_mixed_ops(self, op): # GH#16116 df = DataFrame({'int': [1, 2, 3, 4], 'float': [1., 2., 3., 4.], 'str': ['a', 'b', 'c', 'd']}) result = getattr(df, op)() assert len(result) == 2 with pd.option_context('use_bottleneck', False): result = getattr(df, op)() assert len(result) == 2 def test_reduce_mixed_frame(self): # GH 6806 df = DataFrame({ 'bool_data': [True, True, False, False, False], 'int_data': [10, 20, 30, 40, 50], 'string_data': ['a', 'b', 'c', 'd', 'e'], }) df.reindex(columns=['bool_data', 'int_data', 'string_data']) test = df.sum(axis=0) tm.assert_numpy_array_equal(test.values, np.array([2, 150, 'abcde'], dtype=object)) tm.assert_series_equal(test, df.T.sum(axis=1)) def test_nunique(self): df = DataFrame({'A': [1, 1, 1], 'B': [1, 2, 3], 'C': [1, np.nan, 3]}) tm.assert_series_equal(df.nunique(), Series({'A': 1, 'B': 3, 'C': 2})) tm.assert_series_equal(df.nunique(dropna=False), Series({'A': 1, 'B': 3, 'C': 3})) tm.assert_series_equal(df.nunique(axis=1), Series({0: 1, 1: 2, 2: 2})) tm.assert_series_equal(df.nunique(axis=1, dropna=False), Series({0: 1, 1: 3, 2: 2})) @pytest.mark.parametrize('tz', [None, 'UTC']) def test_mean_mixed_datetime_numeric(self, tz): # https://github.com/pandas-dev/pandas/issues/24752 df = pd.DataFrame({"A": [1, 1], "B": [pd.Timestamp('2000', tz=tz)] * 2}) result = df.mean() expected = pd.Series([1.0], index=['A']) tm.assert_series_equal(result, expected) @pytest.mark.parametrize('tz', [None, 'UTC']) def test_mean_excludeds_datetimes(self, tz): # https://github.com/pandas-dev/pandas/issues/24752 # Our long-term desired behavior is unclear, but the behavior in # 0.24.0rc1 was buggy. df = pd.DataFrame({"A": [pd.Timestamp('2000', tz=tz)] * 2}) result = df.mean() expected = pd.Series() tm.assert_series_equal(result, expected) def test_var_std(self, datetime_frame): result = datetime_frame.std(ddof=4) expected = datetime_frame.apply(lambda x: x.std(ddof=4)) tm.assert_almost_equal(result, expected) result = datetime_frame.var(ddof=4) expected = datetime_frame.apply(lambda x: x.var(ddof=4)) tm.assert_almost_equal(result, expected) arr = np.repeat(np.random.random((1, 1000)), 1000, 0) result = nanops.nanvar(arr, axis=0) assert not (result < 0).any() with pd.option_context('use_bottleneck', False): result = nanops.nanvar(arr, axis=0) assert not (result < 0).any() @pytest.mark.parametrize( "meth", ['sem', 'var', 'std']) def test_numeric_only_flag(self, meth): # GH 9201 df1 = DataFrame(np.random.randn(5, 3), columns=['foo', 'bar', 'baz']) # set one entry to a number in str format df1.loc[0, 'foo'] = '100' df2 = DataFrame(np.random.randn(5, 3), columns=['foo', 'bar', 'baz']) # set one entry to a non-number str df2.loc[0, 'foo'] = 'a' result = getattr(df1, meth)(axis=1, numeric_only=True) expected = getattr(df1[['bar', 'baz']], meth)(axis=1) tm.assert_series_equal(expected, result) result = getattr(df2, meth)(axis=1, numeric_only=True) expected = getattr(df2[['bar', 'baz']], meth)(axis=1) tm.assert_series_equal(expected, result) # df1 has all numbers, df2 has a letter inside msg = r"unsupported operand type\(s\) for -: 'float' and 'str'" with pytest.raises(TypeError, match=msg): getattr(df1, meth)(axis=1, numeric_only=False) msg = "could not convert string to float: 'a'" with pytest.raises(TypeError, match=msg): getattr(df2, meth)(axis=1, numeric_only=False) def test_sem(self, datetime_frame): result = datetime_frame.sem(ddof=4) expected = datetime_frame.apply( lambda x: x.std(ddof=4) / np.sqrt(len(x))) tm.assert_almost_equal(result, expected) arr = np.repeat(np.random.random((1, 1000)), 1000, 0) result = nanops.nansem(arr, axis=0) assert not (result < 0).any() with pd.option_context('use_bottleneck', False): result = nanops.nansem(arr, axis=0) assert not (result < 0).any() @td.skip_if_no_scipy def test_kurt(self): index = MultiIndex(levels=[['bar'], ['one', 'two', 'three'], [0, 1]], codes=[[0, 0, 0, 0, 0, 0], [0, 1, 2, 0, 1, 2], [0, 1, 0, 1, 0, 1]]) df = DataFrame(np.random.randn(6, 3), index=index) kurt = df.kurt() kurt2 = df.kurt(level=0).xs('bar') tm.assert_series_equal(kurt, kurt2, check_names=False) assert kurt.name is None assert kurt2.name == 'bar' @pytest.mark.parametrize("dropna, expected", [ (True, {'A': [12], 'B': [10.0], 'C': [1.0], 'D': ['a'], 'E': Categorical(['a'], categories=['a']), 'F': to_datetime(['2000-1-2']), 'G': to_timedelta(['1 days'])}), (False, {'A': [12], 'B': [10.0], 'C': [np.nan], 'D': np.array([np.nan], dtype=object), 'E': Categorical([np.nan], categories=['a']), 'F': [pd.NaT], 'G': to_timedelta([pd.NaT])}), (True, {'H': [8, 9, np.nan, np.nan], 'I': [8, 9, np.nan, np.nan], 'J': [1, np.nan, np.nan, np.nan], 'K': Categorical(['a', np.nan, np.nan, np.nan], categories=['a']), 'L': to_datetime(['2000-1-2', 'NaT', 'NaT', 'NaT']), 'M': to_timedelta(['1 days', 'nan', 'nan', 'nan']), 'N': [0, 1, 2, 3]}), (False, {'H': [8, 9, np.nan, np.nan], 'I': [8, 9, np.nan, np.nan], 'J': [1, np.nan, np.nan, np.nan], 'K': Categorical([np.nan, 'a', np.nan, np.nan], categories=['a']), 'L': to_datetime(['NaT', '2000-1-2', 'NaT', 'NaT']), 'M': to_timedelta(['nan', '1 days', 'nan', 'nan']), 'N': [0, 1, 2, 3]}) ]) def test_mode_dropna(self, dropna, expected): df = DataFrame({"A": [12, 12, 19, 11], "B": [10, 10, np.nan, 3], "C": [1, np.nan, np.nan, np.nan], "D": [np.nan, np.nan, 'a', np.nan], "E": Categorical([np.nan, np.nan, 'a', np.nan]), "F": to_datetime(['NaT', '2000-1-2', 'NaT', 'NaT']), "G": to_timedelta(['1 days', 'nan', 'nan', 'nan']), "H": [8, 8, 9, 9], "I": [9, 9, 8, 8], "J": [1, 1, np.nan, np.nan], "K": Categorical(['a', np.nan, 'a', np.nan]), "L": to_datetime(['2000-1-2', '2000-1-2', 'NaT', 'NaT']), "M": to_timedelta(['1 days', 'nan', '1 days', 'nan']), "N": np.arange(4, dtype='int64')}) result = df[sorted(list(expected.keys()))].mode(dropna=dropna) expected = DataFrame(expected) tm.assert_frame_equal(result, expected) def test_mode_sortwarning(self): # Check for the warning that is raised when the mode # results cannot be sorted df = DataFrame({"A": [np.nan, np.nan, 'a', 'a']}) expected = DataFrame({'A': ['a', np.nan]}) with tm.assert_produces_warning(UserWarning, check_stacklevel=False): result = df.mode(dropna=False) result = result.sort_values(by='A').reset_index(drop=True) tm.assert_frame_equal(result, expected) def test_operators_timedelta64(self): df = DataFrame(dict(A=date_range('2012-1-1', periods=3, freq='D'), B=date_range('2012-1-2', periods=3, freq='D'), C=Timestamp('20120101') - timedelta(minutes=5, seconds=5))) diffs = DataFrame(dict(A=df['A'] - df['C'], B=df['A'] - df['B'])) # min result = diffs.min() assert result[0] == diffs.loc[0, 'A'] assert result[1] == diffs.loc[0, 'B'] result = diffs.min(axis=1) assert (result == diffs.loc[0, 'B']).all() # max result = diffs.max() assert result[0] == diffs.loc[2, 'A'] assert result[1] == diffs.loc[2, 'B'] result = diffs.max(axis=1) assert (result == diffs['A']).all() # abs result = diffs.abs() result2 = abs(diffs) expected = DataFrame(dict(A=df['A'] - df['C'], B=df['B'] - df['A'])) tm.assert_frame_equal(result, expected) tm.assert_frame_equal(result2, expected) # mixed frame mixed = diffs.copy() mixed['C'] = 'foo' mixed['D'] = 1 mixed['E'] = 1. mixed['F'] = Timestamp('20130101') # results in an object array result = mixed.min() expected = Series([pd.Timedelta(timedelta(seconds=5 * 60 + 5)), pd.Timedelta(timedelta(days=-1)), 'foo', 1, 1.0, Timestamp('20130101')], index=mixed.columns) tm.assert_series_equal(result, expected) # excludes numeric result = mixed.min(axis=1) expected = Series([1, 1, 1.], index=[0, 1, 2]) tm.assert_series_equal(result, expected) # works when only those columns are selected result = mixed[['A', 'B']].min(1) expected = Series([timedelta(days=-1)] * 3) tm.assert_series_equal(result, expected) result = mixed[['A', 'B']].min() expected = Series([timedelta(seconds=5 * 60 + 5), timedelta(days=-1)], index=['A', 'B']) tm.assert_series_equal(result, expected) # GH 3106 df = DataFrame({'time': date_range('20130102', periods=5), 'time2': date_range('20130105', periods=5)}) df['off1'] = df['time2'] - df['time'] assert df['off1'].dtype == 'timedelta64[ns]' df['off2'] = df['time'] - df['time2'] df._consolidate_inplace() assert df['off1'].dtype == 'timedelta64[ns]' assert df['off2'].dtype == 'timedelta64[ns]' def test_sum_corner(self): empty_frame = DataFrame() axis0 = empty_frame.sum(0) axis1 = empty_frame.sum(1) assert isinstance(axis0, Series) assert isinstance(axis1, Series) assert len(axis0) == 0 assert len(axis1) == 0 @pytest.mark.parametrize('method, unit', [ ('sum', 0), ('prod', 1), ]) def test_sum_prod_nanops(self, method, unit): idx = ['a', 'b', 'c'] df = pd.DataFrame({"a": [unit, unit], "b": [unit, np.nan], "c": [np.nan, np.nan]}) # The default result = getattr(df, method) expected = pd.Series([unit, unit, unit], index=idx, dtype='float64') # min_count=1 result = getattr(df, method)(min_count=1) expected = pd.Series([unit, unit, np.nan], index=idx) tm.assert_series_equal(result, expected) # min_count=0 result = getattr(df, method)(min_count=0) expected = pd.Series([unit, unit, unit], index=idx, dtype='float64') tm.assert_series_equal(result, expected) result = getattr(df.iloc[1:], method)(min_count=1) expected = pd.Series([unit, np.nan, np.nan], index=idx) tm.assert_series_equal(result, expected) # min_count > 1 df = pd.DataFrame({"A": [unit] * 10, "B": [unit] * 5 + [np.nan] * 5}) result = getattr(df, method)(min_count=5) expected = pd.Series(result, index=['A', 'B']) tm.assert_series_equal(result, expected) result = getattr(df, method)(min_count=6) expected = pd.Series(result, index=['A', 'B']) tm.assert_series_equal(result, expected) def test_sum_nanops_timedelta(self): # prod isn't defined on timedeltas idx = ['a', 'b', 'c'] df = pd.DataFrame({"a": [0, 0], "b": [0, np.nan], "c": [np.nan, np.nan]}) df2 = df.apply(pd.to_timedelta) # 0 by default result = df2.sum() expected = pd.Series([0, 0, 0], dtype='m8[ns]', index=idx) tm.assert_series_equal(result, expected) # min_count=0 result = df2.sum(min_count=0) tm.assert_series_equal(result, expected) # min_count=1 result = df2.sum(min_count=1) expected = pd.Series([0, 0, np.nan], dtype='m8[ns]', index=idx) tm.assert_series_equal(result, expected) def test_sum_object(self, float_frame): values = float_frame.values.astype(int) frame = DataFrame(values, index=float_frame.index, columns=float_frame.columns) deltas = frame * timedelta(1) deltas.sum() def test_sum_bool(self, float_frame): # ensure this works, bug report bools = np.isnan(float_frame) bools.sum(1) bools.sum(0) def test_mean_corner(self, float_frame, float_string_frame): # unit test when have object data the_mean = float_string_frame.mean(axis=0) the_sum = float_string_frame.sum(axis=0, numeric_only=True) tm.assert_index_equal(the_sum.index, the_mean.index) assert len(the_mean.index) < len(float_string_frame.columns) # xs sum mixed type, just want to know it works... the_mean = float_string_frame.mean(axis=1) the_sum = float_string_frame.sum(axis=1, numeric_only=True) tm.assert_index_equal(the_sum.index, the_mean.index) # take mean of boolean column float_frame['bool'] = float_frame['A'] > 0 means = float_frame.mean(0) assert means['bool'] == float_frame['bool'].values.mean() def test_stats_mixed_type(self, float_string_frame): # don't blow up float_string_frame.std(1) float_string_frame.var(1) float_string_frame.mean(1) float_string_frame.skew(1) def test_sum_bools(self): df = DataFrame(index=lrange(1), columns=lrange(10)) bools = isna(df) assert bools.sum(axis=1)[0] == 10 # --------------------------------------------------------------------- # Cumulative Reductions - cumsum, cummax, ... def test_cumsum_corner(self): dm = DataFrame(np.arange(20).reshape(4, 5), index=lrange(4), columns=lrange(5)) # ?(wesm) result = dm.cumsum() # noqa def test_cumsum(self, datetime_frame): datetime_frame.loc[5:10, 0] = np.nan datetime_frame.loc[10:15, 1] = np.nan datetime_frame.loc[15:, 2] = np.nan # axis = 0 cumsum = datetime_frame.cumsum() expected = datetime_frame.apply(Series.cumsum) tm.assert_frame_equal(cumsum, expected) # axis = 1 cumsum = datetime_frame.cumsum(axis=1) expected = datetime_frame.apply(Series.cumsum, axis=1) tm.assert_frame_equal(cumsum, expected) # works df = DataFrame({'A': np.arange(20)}, index=np.arange(20)) result = df.cumsum() # noqa # fix issue cumsum_xs = datetime_frame.cumsum(axis=1) assert np.shape(cumsum_xs) == np.shape(datetime_frame) def test_cumprod(self, datetime_frame): datetime_frame.loc[5:10, 0] = np.nan datetime_frame.loc[10:15, 1] = np.nan datetime_frame.loc[15:, 2] = np.nan # axis = 0 cumprod = datetime_frame.cumprod() expected = datetime_frame.apply(Series.cumprod) tm.assert_frame_equal(cumprod, expected) # axis = 1 cumprod = datetime_frame.cumprod(axis=1) expected = datetime_frame.apply(Series.cumprod, axis=1) tm.assert_frame_equal(cumprod, expected) # fix issue cumprod_xs = datetime_frame.cumprod(axis=1) assert np.shape(cumprod_xs) == np.shape(datetime_frame) # ints df = datetime_frame.fillna(0).astype(int) df.cumprod(0) df.cumprod(1) # ints32 df = datetime_frame.fillna(0).astype(np.int32) df.cumprod(0) df.cumprod(1) def test_cummin(self, datetime_frame): datetime_frame.loc[5:10, 0] = np.nan datetime_frame.loc[10:15, 1] = np.nan datetime_frame.loc[15:, 2] = np.nan # axis = 0 cummin = datetime_frame.cummin() expected = datetime_frame.apply(Series.cummin) tm.assert_frame_equal(cummin, expected) # axis = 1 cummin = datetime_frame.cummin(axis=1) expected = datetime_frame.apply(Series.cummin, axis=1) tm.assert_frame_equal(cummin, expected) # it works df = DataFrame({'A': np.arange(20)}, index=np.arange(20)) result = df.cummin() # noqa # fix issue cummin_xs = datetime_frame.cummin(axis=1) assert np.shape(cummin_xs) == np.shape(datetime_frame) def test_cummax(self, datetime_frame): datetime_frame.loc[5:10, 0] = np.nan datetime_frame.loc[10:15, 1] = np.nan datetime_frame.loc[15:, 2] = np.nan # axis = 0 cummax = datetime_frame.cummax() expected = datetime_frame.apply(Series.cummax) tm.assert_frame_equal(cummax, expected) # axis = 1 cummax = datetime_frame.cummax(axis=1) expected = datetime_frame.apply(Series.cummax, axis=1) tm.assert_frame_equal(cummax, expected) # it works df = DataFrame({'A': np.arange(20)}, index=np.arange(20)) result = df.cummax() # noqa # fix issue cummax_xs = datetime_frame.cummax(axis=1) assert np.shape(cummax_xs) == np.shape(datetime_frame) # --------------------------------------------------------------------- # Miscellanea def test_count(self): # corner case frame = DataFrame() ct1 = frame.count(1) assert isinstance(ct1, Series) ct2 = frame.count(0) assert isinstance(ct2, Series) # GH#423 df = DataFrame(index=lrange(10)) result = df.count(1) expected = Series(0, index=df.index) tm.assert_series_equal(result, expected) df = DataFrame(columns=lrange(10)) result = df.count(0) expected = Series(0, index=df.columns) tm.assert_series_equal(result, expected) df = DataFrame() result = df.count() expected = Series(0, index=[]) tm.assert_series_equal(result, expected) def test_count_objects(self, float_string_frame): dm = DataFrame(float_string_frame._series) df = DataFrame(float_string_frame._series) tm.assert_series_equal(dm.count(), df.count()) tm.assert_series_equal(dm.count(1), df.count(1)) def test_pct_change(self): # GH#11150 pnl = DataFrame([np.arange(0, 40, 10), np.arange(0, 40, 10), np.arange(0, 40, 10)]).astype(np.float64) pnl.iat[1, 0] = np.nan pnl.iat[1, 1] = np.nan pnl.iat[2, 3] = 60 for axis in range(2): expected = pnl.ffill(axis=axis) / pnl.ffill(axis=axis).shift( axis=axis) - 1 result = pnl.pct_change(axis=axis, fill_method='pad') tm.assert_frame_equal(result, expected) # ---------------------------------------------------------------------- # Index of max / min def test_idxmin(self, float_frame, int_frame): frame = float_frame frame.loc[5:10] = np.nan frame.loc[15:20, -2:] = np.nan for skipna in [True, False]: for axis in [0, 1]: for df in [frame, int_frame]: result = df.idxmin(axis=axis, skipna=skipna) expected = df.apply(Series.idxmin, axis=axis, skipna=skipna) tm.assert_series_equal(result, expected) msg = ("No axis named 2 for object type" " <class 'pandas.core.frame.DataFrame'>") with pytest.raises(ValueError, match=msg): frame.idxmin(axis=2) def test_idxmax(self, float_frame, int_frame): frame = float_frame frame.loc[5:10] = np.nan frame.loc[15:20, -2:] = np.nan for skipna in [True, False]: for axis in [0, 1]: for df in [frame, int_frame]: result = df.idxmax(axis=axis, skipna=skipna) expected = df.apply(Series.idxmax, axis=axis, skipna=skipna) tm.assert_series_equal(result, expected) msg = ("No axis named 2 for object type" " <class 'pandas.core.frame.DataFrame'>") with pytest.raises(ValueError, match=msg): frame.idxmax(axis=2) # ---------------------------------------------------------------------- # Logical reductions @pytest.mark.parametrize('opname', ['any', 'all']) def test_any_all(self, opname, bool_frame_with_na, float_string_frame): assert_bool_op_calc(opname, getattr(np, opname), bool_frame_with_na, has_skipna=True) assert_bool_op_api(opname, bool_frame_with_na, float_string_frame, has_bool_only=True) def test_any_all_extra(self): df = DataFrame({ 'A': [True, False, False], 'B': [True, True, False], 'C': [True, True, True], }, index=['a', 'b', 'c']) result = df[['A', 'B']].any(1) expected = Series([True, True, False], index=['a', 'b', 'c']) tm.assert_series_equal(result, expected) result = df[['A', 'B']].any(1, bool_only=True) tm.assert_series_equal(result, expected) result = df.all(1) expected = Series([True, False, False], index=['a', 'b', 'c']) tm.assert_series_equal(result, expected) result = df.all(1, bool_only=True) tm.assert_series_equal(result, expected) # Axis is None result = df.all(axis=None).item() assert result is False result = df.any(axis=None).item() assert result is True result = df[['C']].all(axis=None).item() assert result is True def test_any_datetime(self): # GH 23070 float_data = [1, np.nan, 3, np.nan] datetime_data = [pd.Timestamp('1960-02-15'), pd.Timestamp('1960-02-16'), pd.NaT, pd.NaT] df = DataFrame({ "A": float_data, "B": datetime_data }) result = df.any(1) expected = Series([True, True, True, False]) tm.assert_series_equal(result, expected) def test_any_all_bool_only(self): # GH 25101 df = DataFrame({"col1": [1, 2, 3], "col2": [4, 5, 6], "col3": [None, None, None]}) result = df.all(bool_only=True) expected = Series(dtype=np.bool) tm.assert_series_equal(result, expected) df = DataFrame({"col1": [1, 2, 3], "col2": [4, 5, 6], "col3": [None, None, None], "col4": [False, False, True]}) result = df.all(bool_only=True) expected = Series({"col4": False}) tm.assert_series_equal(result, expected) @pytest.mark.parametrize('func, data, expected', [ (np.any, {}, False), (np.all, {}, True), (np.any, {'A': []}, False), (np.all, {'A': []}, True), (np.any, {'A': [False, False]}, False), (np.all, {'A': [False, False]}, False), (np.any, {'A': [True, False]}, True), (np.all, {'A': [True, False]}, False), (np.any, {'A': [True, True]}, True), (np.all, {'A': [True, True]}, True), (np.any, {'A': [False], 'B': [False]}, False), (np.all, {'A': [False], 'B': [False]}, False), (np.any, {'A': [False, False], 'B': [False, True]}, True), (np.all, {'A': [False, False], 'B': [False, True]}, False), # other types (np.all, {'A': pd.Series([0.0, 1.0], dtype='float')}, False), (np.any, {'A': pd.Series([0.0, 1.0], dtype='float')}, True), (np.all, {'A': pd.Series([0, 1], dtype=int)}, False), (np.any, {'A': pd.Series([0, 1], dtype=int)}, True), pytest.param(np.all, {'A': pd.Series([0, 1], dtype='M8[ns]')}, False, marks=[td.skip_if_np_lt_115]), pytest.param(np.any, {'A': pd.Series([0, 1], dtype='M8[ns]')}, True, marks=[td.skip_if_np_lt_115]), pytest.param(np.all, {'A': pd.Series([1, 2], dtype='M8[ns]')}, True, marks=[td.skip_if_np_lt_115]), pytest.param(np.any, {'A': pd.Series([1, 2], dtype='M8[ns]')}, True, marks=[td.skip_if_np_lt_115]), pytest.param(np.all, {'A': pd.Series([0, 1], dtype='m8[ns]')}, False, marks=[td.skip_if_np_lt_115]), pytest.param(np.any, {'A': pd.Series([0, 1], dtype='m8[ns]')}, True, marks=[td.skip_if_np_lt_115]), pytest.param(np.all, {'A': pd.Series([1, 2], dtype='m8[ns]')}, True, marks=[td.skip_if_np_lt_115]), pytest.param(np.any, {'A': pd.Series([1, 2], dtype='m8[ns]')}, True, marks=[td.skip_if_np_lt_115]), (np.all, {'A': pd.Series([0, 1], dtype='category')}, False), (np.any, {'A': pd.Series([0, 1], dtype='category')}, True), (np.all, {'A': pd.Series([1, 2], dtype='category')}, True), (np.any, {'A': pd.Series([1, 2], dtype='category')}, True), # # Mix # GH 21484 # (np.all, {'A': pd.Series([10, 20], dtype='M8[ns]'), # 'B': pd.Series([10, 20], dtype='m8[ns]')}, True), ]) def test_any_all_np_func(self, func, data, expected): # GH 19976 data = DataFrame(data) result = func(data) assert isinstance(result, np.bool_) assert result.item() is expected # method version result = getattr(DataFrame(data), func.__name__)(axis=None) assert isinstance(result, np.bool_) assert result.item() is expected def test_any_all_object(self): # GH 19976 result = np.all(DataFrame(columns=['a', 'b'])).item() assert result is True result = np.any(DataFrame(columns=['a', 'b'])).item() assert result is False @pytest.mark.parametrize('method', ['any', 'all']) def test_any_all_level_axis_none_raises(self, method): df = DataFrame( {"A": 1}, index=MultiIndex.from_product([['A', 'B'], ['a', 'b']], names=['out', 'in']) ) xpr = "Must specify 'axis' when aggregating by level." with pytest.raises(ValueError, match=xpr): getattr(df, method)(axis=None, level='out') # ---------------------------------------------------------------------- # Isin def test_isin(self): # GH 4211 df = DataFrame({'vals': [1, 2, 3, 4], 'ids': ['a', 'b', 'f', 'n'], 'ids2': ['a', 'n', 'c', 'n']}, index=['foo', 'bar', 'baz', 'qux']) other = ['a', 'b', 'c'] result = df.isin(other) expected = DataFrame([df.loc[s].isin(other) for s in df.index]) tm.assert_frame_equal(result, expected) @pytest.mark.parametrize("empty", [[], Series(), np.array([])]) def test_isin_empty(self, empty): # GH 16991 df = DataFrame({'A': ['a', 'b', 'c'], 'B': ['a', 'e', 'f']}) expected = DataFrame(False, df.index, df.columns) result = df.isin(empty) tm.assert_frame_equal(result, expected) def test_isin_dict(self): df = DataFrame({'A': ['a', 'b', 'c'], 'B': ['a', 'e', 'f']}) d = {'A': ['a']} expected = DataFrame(False, df.index, df.columns) expected.loc[0, 'A'] = True result = df.isin(d) tm.assert_frame_equal(result, expected) # non unique columns df = DataFrame({'A': ['a', 'b', 'c'], 'B': ['a', 'e', 'f']}) df.columns = ['A', 'A'] expected = DataFrame(False, df.index, df.columns) expected.loc[0, 'A'] = True result = df.isin(d) tm.assert_frame_equal(result, expected) def test_isin_with_string_scalar(self): # GH 4763 df = DataFrame({'vals': [1, 2, 3, 4], 'ids': ['a', 'b', 'f', 'n'], 'ids2': ['a', 'n', 'c', 'n']}, index=['foo', 'bar', 'baz', 'qux']) with pytest.raises(TypeError): df.isin('a') with pytest.raises(TypeError): df.isin('aaa') def test_isin_df(self): df1 = DataFrame({'A': [1, 2, 3, 4], 'B': [2, np.nan, 4, 4]}) df2 = DataFrame({'A': [0, 2, 12, 4], 'B': [2, np.nan, 4, 5]}) expected = DataFrame(False, df1.index, df1.columns) result = df1.isin(df2) expected['A'].loc[[1, 3]] = True expected['B'].loc[[0, 2]] = True tm.assert_frame_equal(result, expected) # partial overlapping columns df2.columns = ['A', 'C'] result = df1.isin(df2) expected['B'] = False tm.assert_frame_equal(result, expected) def test_isin_tuples(self): # GH 16394 df = pd.DataFrame({'A': [1, 2, 3], 'B': ['a', 'b', 'f']}) df['C'] = list(zip(df['A'], df['B'])) result = df['C'].isin([(1, 'a')]) tm.assert_series_equal(result, Series([True, False, False], name="C")) def test_isin_df_dupe_values(self): df1 = DataFrame({'A': [1, 2, 3, 4], 'B': [2, np.nan, 4, 4]}) # just cols duped df2 = DataFrame([[0, 2], [12, 4], [2, np.nan], [4, 5]], columns=['B', 'B']) with pytest.raises(ValueError): df1.isin(df2) # just index duped df2 = DataFrame([[0, 2], [12, 4], [2, np.nan], [4, 5]], columns=['A', 'B'], index=[0, 0, 1, 1]) with pytest.raises(ValueError): df1.isin(df2) # cols and index: df2.columns = ['B', 'B'] with pytest.raises(ValueError): df1.isin(df2) def test_isin_dupe_self(self): other = DataFrame({'A': [1, 0, 1, 0], 'B': [1, 1, 0, 0]}) df = DataFrame([[1, 1], [1, 0], [0, 0]], columns=['A', 'A']) result = df.isin(other) expected = DataFrame(False, index=df.index, columns=df.columns) expected.loc[0] = True expected.iloc[1, 1] = True tm.assert_frame_equal(result, expected) def test_isin_against_series(self): df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [2, np.nan, 4, 4]}, index=['a', 'b', 'c', 'd']) s = pd.Series([1, 3, 11, 4], index=['a', 'b', 'c', 'd']) expected = DataFrame(False, index=df.index, columns=df.columns) expected['A'].loc['a'] = True expected.loc['d'] = True result = df.isin(s) tm.assert_frame_equal(result, expected) def test_isin_multiIndex(self): idx = MultiIndex.from_tuples([(0, 'a', 'foo'), (0, 'a', 'bar'), (0, 'b', 'bar'), (0, 'b', 'baz'), (2, 'a', 'foo'), (2, 'a', 'bar'), (2, 'c', 'bar'), (2, 'c', 'baz'), (1, 'b', 'foo'), (1, 'b', 'bar'), (1, 'c', 'bar'), (1, 'c', 'baz')]) df1 = DataFrame({'A': np.ones(12), 'B': np.zeros(12)}, index=idx) df2 = DataFrame({'A': [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], 'B': [1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1]}) # against regular index expected = DataFrame(False, index=df1.index, columns=df1.columns) result = df1.isin(df2) tm.assert_frame_equal(result, expected) df2.index = idx expected = df2.values.astype(np.bool) expected[:, 1] = ~expected[:, 1] expected = DataFrame(expected, columns=['A', 'B'], index=idx) result = df1.isin(df2) tm.assert_frame_equal(result, expected) def test_isin_empty_datetimelike(self): # GH 15473 df1_ts = DataFrame({'date': pd.to_datetime(['2014-01-01', '2014-01-02'])}) df1_td = DataFrame({'date': [pd.Timedelta(1, 's'), pd.Timedelta(2, 's')]}) df2 = DataFrame({'date': []}) df3 = DataFrame() expected = DataFrame({'date': [False, False]}) result = df1_ts.isin(df2) tm.assert_frame_equal(result, expected) result = df1_ts.isin(df3) tm.assert_frame_equal(result, expected) result = df1_td.isin(df2) tm.assert_frame_equal(result, expected) result = df1_td.isin(df3)
tm.assert_frame_equal(result, expected)
pandas.util.testing.assert_frame_equal
"""SQL io tests The SQL tests are broken down in different classes: - `PandasSQLTest`: base class with common methods for all test classes - Tests for the public API (only tests with sqlite3) - `_TestSQLApi` base class - `TestSQLApi`: test the public API with sqlalchemy engine - `TestSQLiteFallbackApi`: test the public API with a sqlite DBAPI connection - Tests for the different SQL flavors (flavor specific type conversions) - Tests for the sqlalchemy mode: `_TestSQLAlchemy` is the base class with common methods, `_TestSQLAlchemyConn` tests the API with a SQLAlchemy Connection object. The different tested flavors (sqlite3, MySQL, PostgreSQL) derive from the base class - Tests for the fallback mode (`TestSQLiteFallback`) """ import csv from datetime import date, datetime, time from io import StringIO import sqlite3 import warnings import numpy as np import pytest from pandas.core.dtypes.common import is_datetime64_dtype, is_datetime64tz_dtype import pandas as pd from pandas import ( DataFrame, Index, MultiIndex, Series, Timestamp, concat, date_range, isna, to_datetime, to_timedelta, ) import pandas._testing as tm import pandas.io.sql as sql from pandas.io.sql import read_sql_query, read_sql_table try: import sqlalchemy import sqlalchemy.schema import sqlalchemy.sql.sqltypes as sqltypes from sqlalchemy.ext import declarative from sqlalchemy.orm import session as sa_session SQLALCHEMY_INSTALLED = True except ImportError: SQLALCHEMY_INSTALLED = False SQL_STRINGS = { "create_iris": { "sqlite": """CREATE TABLE iris ( "SepalLength" REAL, "SepalWidth" REAL, "PetalLength" REAL, "PetalWidth" REAL, "Name" TEXT )""", "mysql": """CREATE TABLE iris ( `SepalLength` DOUBLE, `SepalWidth` DOUBLE, `PetalLength` DOUBLE, `PetalWidth` DOUBLE, `Name` VARCHAR(200) )""", "postgresql": """CREATE TABLE iris ( "SepalLength" DOUBLE PRECISION, "SepalWidth" DOUBLE PRECISION, "PetalLength" DOUBLE PRECISION, "PetalWidth" DOUBLE PRECISION, "Name" VARCHAR(200) )""", }, "insert_iris": { "sqlite": """INSERT INTO iris VALUES(?, ?, ?, ?, ?)""", "mysql": """INSERT INTO iris VALUES(%s, %s, %s, %s, "%s");""", "postgresql": """INSERT INTO iris VALUES(%s, %s, %s, %s, %s);""", }, "create_test_types": { "sqlite": """CREATE TABLE types_test_data ( "TextCol" TEXT, "DateCol" TEXT, "IntDateCol" INTEGER, "IntDateOnlyCol" INTEGER, "FloatCol" REAL, "IntCol" INTEGER, "BoolCol" INTEGER, "IntColWithNull" INTEGER, "BoolColWithNull" INTEGER )""", "mysql": """CREATE TABLE types_test_data ( `TextCol` TEXT, `DateCol` DATETIME, `IntDateCol` INTEGER, `IntDateOnlyCol` INTEGER, `FloatCol` DOUBLE, `IntCol` INTEGER, `BoolCol` BOOLEAN, `IntColWithNull` INTEGER, `BoolColWithNull` BOOLEAN )""", "postgresql": """CREATE TABLE types_test_data ( "TextCol" TEXT, "DateCol" TIMESTAMP, "DateColWithTz" TIMESTAMP WITH TIME ZONE, "IntDateCol" INTEGER, "IntDateOnlyCol" INTEGER, "FloatCol" DOUBLE PRECISION, "IntCol" INTEGER, "BoolCol" BOOLEAN, "IntColWithNull" INTEGER, "BoolColWithNull" BOOLEAN )""", }, "insert_test_types": { "sqlite": { "query": """ INSERT INTO types_test_data VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?) """, "fields": ( "TextCol", "DateCol", "IntDateCol", "IntDateOnlyCol", "FloatCol", "IntCol", "BoolCol", "IntColWithNull", "BoolColWithNull", ), }, "mysql": { "query": """ INSERT INTO types_test_data VALUES("%s", %s, %s, %s, %s, %s, %s, %s, %s) """, "fields": ( "TextCol", "DateCol", "IntDateCol", "IntDateOnlyCol", "FloatCol", "IntCol", "BoolCol", "IntColWithNull", "BoolColWithNull", ), }, "postgresql": { "query": """ INSERT INTO types_test_data VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) """, "fields": ( "TextCol", "DateCol", "DateColWithTz", "IntDateCol", "IntDateOnlyCol", "FloatCol", "IntCol", "BoolCol", "IntColWithNull", "BoolColWithNull", ), }, }, "read_parameters": { "sqlite": "SELECT * FROM iris WHERE Name=? AND SepalLength=?", "mysql": 'SELECT * FROM iris WHERE `Name`="%s" AND `SepalLength`=%s', "postgresql": 'SELECT * FROM iris WHERE "Name"=%s AND "SepalLength"=%s', }, "read_named_parameters": { "sqlite": """ SELECT * FROM iris WHERE Name=:name AND SepalLength=:length """, "mysql": """ SELECT * FROM iris WHERE `Name`="%(name)s" AND `SepalLength`=%(length)s """, "postgresql": """ SELECT * FROM iris WHERE "Name"=%(name)s AND "SepalLength"=%(length)s """, }, "create_view": { "sqlite": """ CREATE VIEW iris_view AS SELECT * FROM iris """ }, } class MixInBase: def teardown_method(self, method): # if setup fails, there may not be a connection to close. if hasattr(self, "conn"): for tbl in self._get_all_tables(): self.drop_table(tbl) self._close_conn() class MySQLMixIn(MixInBase): def drop_table(self, table_name): cur = self.conn.cursor() cur.execute(f"DROP TABLE IF EXISTS {sql._get_valid_mysql_name(table_name)}") self.conn.commit() def _get_all_tables(self): cur = self.conn.cursor() cur.execute("SHOW TABLES") return [table[0] for table in cur.fetchall()] def _close_conn(self): from pymysql.err import Error try: self.conn.close() except Error: pass class SQLiteMixIn(MixInBase): def drop_table(self, table_name): self.conn.execute( f"DROP TABLE IF EXISTS {sql._get_valid_sqlite_name(table_name)}" ) self.conn.commit() def _get_all_tables(self): c = self.conn.execute("SELECT name FROM sqlite_master WHERE type='table'") return [table[0] for table in c.fetchall()] def _close_conn(self): self.conn.close() class SQLAlchemyMixIn(MixInBase): def drop_table(self, table_name): sql.SQLDatabase(self.conn).drop_table(table_name) def _get_all_tables(self): meta = sqlalchemy.schema.MetaData(bind=self.conn) meta.reflect() table_list = meta.tables.keys() return table_list def _close_conn(self): pass class PandasSQLTest: """ Base class with common private methods for SQLAlchemy and fallback cases. """ def _get_exec(self): if hasattr(self.conn, "execute"): return self.conn else: return self.conn.cursor() @pytest.fixture(params=[("data", "iris.csv")]) def load_iris_data(self, datapath, request): import io iris_csv_file = datapath(*request.param) if not hasattr(self, "conn"): self.setup_connect() self.drop_table("iris") self._get_exec().execute(SQL_STRINGS["create_iris"][self.flavor]) with io.open(iris_csv_file, mode="r", newline=None) as iris_csv: r = csv.reader(iris_csv) next(r) # skip header row ins = SQL_STRINGS["insert_iris"][self.flavor] for row in r: self._get_exec().execute(ins, row) def _load_iris_view(self): self.drop_table("iris_view") self._get_exec().execute(SQL_STRINGS["create_view"][self.flavor]) def _check_iris_loaded_frame(self, iris_frame): pytype = iris_frame.dtypes[0].type row = iris_frame.iloc[0] assert issubclass(pytype, np.floating) tm.equalContents(row.values, [5.1, 3.5, 1.4, 0.2, "Iris-setosa"]) def _load_test1_data(self): columns = ["index", "A", "B", "C", "D"] data = [ ( "2000-01-03 00:00:00", 0.980268513777, 3.68573087906, -0.364216805298, -1.15973806169, ), ( "2000-01-04 00:00:00", 1.04791624281, -0.0412318367011, -0.16181208307, 0.212549316967, ), ( "2000-01-05 00:00:00", 0.498580885705, 0.731167677815, -0.537677223318, 1.34627041952, ), ( "2000-01-06 00:00:00", 1.12020151869, 1.56762092543, 0.00364077397681, 0.67525259227, ), ] self.test_frame1 = DataFrame(data, columns=columns) def _load_test2_data(self): df = DataFrame( dict( A=[4, 1, 3, 6], B=["asd", "gsq", "ylt", "jkl"], C=[1.1, 3.1, 6.9, 5.3], D=[False, True, True, False], E=["1990-11-22", "1991-10-26", "1993-11-26", "1995-12-12"], ) ) df["E"] = to_datetime(df["E"]) self.test_frame2 = df def _load_test3_data(self): columns = ["index", "A", "B"] data = [ ("2000-01-03 00:00:00", 2 ** 31 - 1, -1.987670), ("2000-01-04 00:00:00", -29, -0.0412318367011), ("2000-01-05 00:00:00", 20000, 0.731167677815), ("2000-01-06 00:00:00", -290867, 1.56762092543), ] self.test_frame3 = DataFrame(data, columns=columns) def _load_raw_sql(self): self.drop_table("types_test_data") self._get_exec().execute(SQL_STRINGS["create_test_types"][self.flavor]) ins = SQL_STRINGS["insert_test_types"][self.flavor] data = [ { "TextCol": "first", "DateCol": "2000-01-03 00:00:00", "DateColWithTz": "2000-01-01 00:00:00-08:00", "IntDateCol": 535852800, "IntDateOnlyCol": 20101010, "FloatCol": 10.10, "IntCol": 1, "BoolCol": False, "IntColWithNull": 1, "BoolColWithNull": False, }, { "TextCol": "first", "DateCol": "2000-01-04 00:00:00", "DateColWithTz": "2000-06-01 00:00:00-07:00", "IntDateCol": 1356998400, "IntDateOnlyCol": 20101212, "FloatCol": 10.10, "IntCol": 1, "BoolCol": False, "IntColWithNull": None, "BoolColWithNull": None, }, ] for d in data: self._get_exec().execute( ins["query"], [d[field] for field in ins["fields"]] ) def _count_rows(self, table_name): result = ( self._get_exec() .execute(f"SELECT count(*) AS count_1 FROM {table_name}") .fetchone() ) return result[0] def _read_sql_iris(self): iris_frame = self.pandasSQL.read_query("SELECT * FROM iris") self._check_iris_loaded_frame(iris_frame) def _read_sql_iris_parameter(self): query = SQL_STRINGS["read_parameters"][self.flavor] params = ["Iris-setosa", 5.1] iris_frame = self.pandasSQL.read_query(query, params=params) self._check_iris_loaded_frame(iris_frame) def _read_sql_iris_named_parameter(self): query = SQL_STRINGS["read_named_parameters"][self.flavor] params = {"name": "Iris-setosa", "length": 5.1} iris_frame = self.pandasSQL.read_query(query, params=params) self._check_iris_loaded_frame(iris_frame) def _to_sql(self, method=None): self.drop_table("test_frame1") self.pandasSQL.to_sql(self.test_frame1, "test_frame1", method=method) assert self.pandasSQL.has_table("test_frame1") num_entries = len(self.test_frame1) num_rows = self._count_rows("test_frame1") assert num_rows == num_entries # Nuke table self.drop_table("test_frame1") def _to_sql_empty(self): self.drop_table("test_frame1") self.pandasSQL.to_sql(self.test_frame1.iloc[:0], "test_frame1") def _to_sql_fail(self): self.drop_table("test_frame1") self.pandasSQL.to_sql(self.test_frame1, "test_frame1", if_exists="fail") assert self.pandasSQL.has_table("test_frame1") msg = "Table 'test_frame1' already exists" with pytest.raises(ValueError, match=msg): self.pandasSQL.to_sql(self.test_frame1, "test_frame1", if_exists="fail") self.drop_table("test_frame1") def _to_sql_replace(self): self.drop_table("test_frame1") self.pandasSQL.to_sql(self.test_frame1, "test_frame1", if_exists="fail") # Add to table again self.pandasSQL.to_sql(self.test_frame1, "test_frame1", if_exists="replace") assert self.pandasSQL.has_table("test_frame1") num_entries = len(self.test_frame1) num_rows = self._count_rows("test_frame1") assert num_rows == num_entries self.drop_table("test_frame1") def _to_sql_append(self): # Nuke table just in case self.drop_table("test_frame1") self.pandasSQL.to_sql(self.test_frame1, "test_frame1", if_exists="fail") # Add to table again self.pandasSQL.to_sql(self.test_frame1, "test_frame1", if_exists="append") assert self.pandasSQL.has_table("test_frame1") num_entries = 2 * len(self.test_frame1) num_rows = self._count_rows("test_frame1") assert num_rows == num_entries self.drop_table("test_frame1") def _to_sql_method_callable(self): check = [] # used to double check function below is really being used def sample(pd_table, conn, keys, data_iter): check.append(1) data = [dict(zip(keys, row)) for row in data_iter] conn.execute(pd_table.table.insert(), data) self.drop_table("test_frame1") self.pandasSQL.to_sql(self.test_frame1, "test_frame1", method=sample) assert self.pandasSQL.has_table("test_frame1") assert check == [1] num_entries = len(self.test_frame1) num_rows = self._count_rows("test_frame1") assert num_rows == num_entries # Nuke table self.drop_table("test_frame1") def _roundtrip(self): self.drop_table("test_frame_roundtrip") self.pandasSQL.to_sql(self.test_frame1, "test_frame_roundtrip") result = self.pandasSQL.read_query("SELECT * FROM test_frame_roundtrip") result.set_index("level_0", inplace=True) # result.index.astype(int) result.index.name = None tm.assert_frame_equal(result, self.test_frame1) def _execute_sql(self): # drop_sql = "DROP TABLE IF EXISTS test" # should already be done iris_results = self.pandasSQL.execute("SELECT * FROM iris") row = iris_results.fetchone() tm.equalContents(row, [5.1, 3.5, 1.4, 0.2, "Iris-setosa"]) def _to_sql_save_index(self): df = DataFrame.from_records( [(1, 2.1, "line1"), (2, 1.5, "line2")], columns=["A", "B", "C"], index=["A"] ) self.pandasSQL.to_sql(df, "test_to_sql_saves_index") ix_cols = self._get_index_columns("test_to_sql_saves_index") assert ix_cols == [["A"]] def _transaction_test(self): with self.pandasSQL.run_transaction() as trans: trans.execute("CREATE TABLE test_trans (A INT, B TEXT)") class DummyException(Exception): pass # Make sure when transaction is rolled back, no rows get inserted ins_sql = "INSERT INTO test_trans (A,B) VALUES (1, 'blah')" try: with self.pandasSQL.run_transaction() as trans: trans.execute(ins_sql) raise DummyException("error") except DummyException: # ignore raised exception pass res = self.pandasSQL.read_query("SELECT * FROM test_trans") assert len(res) == 0 # Make sure when transaction is committed, rows do get inserted with self.pandasSQL.run_transaction() as trans: trans.execute(ins_sql) res2 = self.pandasSQL.read_query("SELECT * FROM test_trans") assert len(res2) == 1 # ----------------------------------------------------------------------------- # -- Testing the public API class _TestSQLApi(PandasSQLTest): """ Base class to test the public API. From this two classes are derived to run these tests for both the sqlalchemy mode (`TestSQLApi`) and the fallback mode (`TestSQLiteFallbackApi`). These tests are run with sqlite3. Specific tests for the different sql flavours are included in `_TestSQLAlchemy`. Notes: flavor can always be passed even in SQLAlchemy mode, should be correctly ignored. we don't use drop_table because that isn't part of the public api """ flavor = "sqlite" mode: str def setup_connect(self): self.conn = self.connect() @pytest.fixture(autouse=True) def setup_method(self, load_iris_data): self.load_test_data_and_sql() def load_test_data_and_sql(self): self._load_iris_view() self._load_test1_data() self._load_test2_data() self._load_test3_data() self._load_raw_sql() def test_read_sql_iris(self): iris_frame = sql.read_sql_query("SELECT * FROM iris", self.conn) self._check_iris_loaded_frame(iris_frame) def test_read_sql_view(self): iris_frame = sql.read_sql_query("SELECT * FROM iris_view", self.conn) self._check_iris_loaded_frame(iris_frame) def test_to_sql(self): sql.to_sql(self.test_frame1, "test_frame1", self.conn) assert sql.has_table("test_frame1", self.conn) def test_to_sql_fail(self): sql.to_sql(self.test_frame1, "test_frame2", self.conn, if_exists="fail") assert sql.has_table("test_frame2", self.conn) msg = "Table 'test_frame2' already exists" with pytest.raises(ValueError, match=msg): sql.to_sql(self.test_frame1, "test_frame2", self.conn, if_exists="fail") def test_to_sql_replace(self): sql.to_sql(self.test_frame1, "test_frame3", self.conn, if_exists="fail") # Add to table again sql.to_sql(self.test_frame1, "test_frame3", self.conn, if_exists="replace") assert sql.has_table("test_frame3", self.conn) num_entries = len(self.test_frame1) num_rows = self._count_rows("test_frame3") assert num_rows == num_entries def test_to_sql_append(self): sql.to_sql(self.test_frame1, "test_frame4", self.conn, if_exists="fail") # Add to table again sql.to_sql(self.test_frame1, "test_frame4", self.conn, if_exists="append") assert sql.has_table("test_frame4", self.conn) num_entries = 2 * len(self.test_frame1) num_rows = self._count_rows("test_frame4") assert num_rows == num_entries def test_to_sql_type_mapping(self): sql.to_sql(self.test_frame3, "test_frame5", self.conn, index=False) result = sql.read_sql("SELECT * FROM test_frame5", self.conn) tm.assert_frame_equal(self.test_frame3, result) def test_to_sql_series(self): s = Series(np.arange(5, dtype="int64"), name="series") sql.to_sql(s, "test_series", self.conn, index=False) s2 = sql.read_sql_query("SELECT * FROM test_series", self.conn) tm.assert_frame_equal(s.to_frame(), s2) def test_roundtrip(self): sql.to_sql(self.test_frame1, "test_frame_roundtrip", con=self.conn) result = sql.read_sql_query("SELECT * FROM test_frame_roundtrip", con=self.conn) # HACK! result.index = self.test_frame1.index result.set_index("level_0", inplace=True) result.index.astype(int) result.index.name = None tm.assert_frame_equal(result, self.test_frame1) def test_roundtrip_chunksize(self): sql.to_sql( self.test_frame1, "test_frame_roundtrip", con=self.conn, index=False, chunksize=2, ) result = sql.read_sql_query("SELECT * FROM test_frame_roundtrip", con=self.conn) tm.assert_frame_equal(result, self.test_frame1) def test_execute_sql(self): # drop_sql = "DROP TABLE IF EXISTS test" # should already be done iris_results = sql.execute("SELECT * FROM iris", con=self.conn) row = iris_results.fetchone() tm.equalContents(row, [5.1, 3.5, 1.4, 0.2, "Iris-setosa"]) def test_date_parsing(self): # Test date parsing in read_sql # No Parsing df = sql.read_sql_query("SELECT * FROM types_test_data", self.conn) assert not issubclass(df.DateCol.dtype.type, np.datetime64) df = sql.read_sql_query( "SELECT * FROM types_test_data", self.conn, parse_dates=["DateCol"] ) assert issubclass(df.DateCol.dtype.type, np.datetime64) assert df.DateCol.tolist() == [ pd.Timestamp(2000, 1, 3, 0, 0, 0), pd.Timestamp(2000, 1, 4, 0, 0, 0), ] df = sql.read_sql_query( "SELECT * FROM types_test_data", self.conn, parse_dates={"DateCol": "%Y-%m-%d %H:%M:%S"}, ) assert issubclass(df.DateCol.dtype.type, np.datetime64) assert df.DateCol.tolist() == [ pd.Timestamp(2000, 1, 3, 0, 0, 0), pd.Timestamp(2000, 1, 4, 0, 0, 0), ] df = sql.read_sql_query( "SELECT * FROM types_test_data", self.conn, parse_dates=["IntDateCol"] ) assert issubclass(df.IntDateCol.dtype.type, np.datetime64) assert df.IntDateCol.tolist() == [ pd.Timestamp(1986, 12, 25, 0, 0, 0), pd.Timestamp(2013, 1, 1, 0, 0, 0), ] df = sql.read_sql_query( "SELECT * FROM types_test_data", self.conn, parse_dates={"IntDateCol": "s"} ) assert issubclass(df.IntDateCol.dtype.type, np.datetime64) assert df.IntDateCol.tolist() == [ pd.Timestamp(1986, 12, 25, 0, 0, 0), pd.Timestamp(2013, 1, 1, 0, 0, 0), ] df = sql.read_sql_query( "SELECT * FROM types_test_data", self.conn, parse_dates={"IntDateOnlyCol": "%Y%m%d"}, ) assert issubclass(df.IntDateOnlyCol.dtype.type, np.datetime64) assert df.IntDateOnlyCol.tolist() == [ pd.Timestamp("2010-10-10"), pd.Timestamp("2010-12-12"), ] def test_date_and_index(self): # Test case where same column appears in parse_date and index_col df = sql.read_sql_query( "SELECT * FROM types_test_data", self.conn, index_col="DateCol", parse_dates=["DateCol", "IntDateCol"], ) assert issubclass(df.index.dtype.type, np.datetime64) assert issubclass(df.IntDateCol.dtype.type, np.datetime64) def test_timedelta(self): # see #6921 df = to_timedelta(Series(["00:00:01", "00:00:03"], name="foo")).to_frame() with tm.assert_produces_warning(UserWarning): df.to_sql("test_timedelta", self.conn) result = sql.read_sql_query("SELECT * FROM test_timedelta", self.conn) tm.assert_series_equal(result["foo"], df["foo"].astype("int64")) def test_complex_raises(self): df = DataFrame({"a": [1 + 1j, 2j]}) msg = "Complex datatypes not supported" with pytest.raises(ValueError, match=msg): df.to_sql("test_complex", self.conn) @pytest.mark.parametrize( "index_name,index_label,expected", [ # no index name, defaults to 'index' (None, None, "index"), # specifying index_label (None, "other_label", "other_label"), # using the index name ("index_name", None, "index_name"), # has index name, but specifying index_label ("index_name", "other_label", "other_label"), # index name is integer (0, None, "0"), # index name is None but index label is integer (None, 0, "0"), ], ) def test_to_sql_index_label(self, index_name, index_label, expected): temp_frame = DataFrame({"col1": range(4)}) temp_frame.index.name = index_name query = "SELECT * FROM test_index_label" sql.to_sql(temp_frame, "test_index_label", self.conn, index_label=index_label) frame = sql.read_sql_query(query, self.conn) assert frame.columns[0] == expected def test_to_sql_index_label_multiindex(self): temp_frame = DataFrame( {"col1": range(4)}, index=MultiIndex.from_product([("A0", "A1"), ("B0", "B1")]), ) # no index name, defaults to 'level_0' and 'level_1' sql.to_sql(temp_frame, "test_index_label", self.conn) frame = sql.read_sql_query("SELECT * FROM test_index_label", self.conn) assert frame.columns[0] == "level_0" assert frame.columns[1] == "level_1" # specifying index_label sql.to_sql( temp_frame, "test_index_label", self.conn, if_exists="replace", index_label=["A", "B"], ) frame = sql.read_sql_query("SELECT * FROM test_index_label", self.conn) assert frame.columns[:2].tolist() == ["A", "B"] # using the index name temp_frame.index.names = ["A", "B"] sql.to_sql(temp_frame, "test_index_label", self.conn, if_exists="replace") frame = sql.read_sql_query("SELECT * FROM test_index_label", self.conn) assert frame.columns[:2].tolist() == ["A", "B"] # has index name, but specifying index_label sql.to_sql( temp_frame, "test_index_label", self.conn, if_exists="replace", index_label=["C", "D"], ) frame = sql.read_sql_query("SELECT * FROM test_index_label", self.conn) assert frame.columns[:2].tolist() == ["C", "D"] msg = "Length of 'index_label' should match number of levels, which is 2" with pytest.raises(ValueError, match=msg): sql.to_sql( temp_frame, "test_index_label", self.conn, if_exists="replace", index_label="C", ) def test_multiindex_roundtrip(self): df = DataFrame.from_records( [(1, 2.1, "line1"), (2, 1.5, "line2")], columns=["A", "B", "C"], index=["A", "B"], ) df.to_sql("test_multiindex_roundtrip", self.conn) result = sql.read_sql_query( "SELECT * FROM test_multiindex_roundtrip", self.conn, index_col=["A", "B"] ) tm.assert_frame_equal(df, result, check_index_type=True) def test_integer_col_names(self): df = DataFrame([[1, 2], [3, 4]], columns=[0, 1]) sql.to_sql(df, "test_frame_integer_col_names", self.conn, if_exists="replace") def test_get_schema(self): create_sql = sql.get_schema(self.test_frame1, "test", con=self.conn) assert "CREATE" in create_sql def test_get_schema_dtypes(self): float_frame = DataFrame({"a": [1.1, 1.2], "b": [2.1, 2.2]}) dtype = sqlalchemy.Integer if self.mode == "sqlalchemy" else "INTEGER" create_sql = sql.get_schema( float_frame, "test", con=self.conn, dtype={"b": dtype} ) assert "CREATE" in create_sql assert "INTEGER" in create_sql def test_get_schema_keys(self): frame = DataFrame({"Col1": [1.1, 1.2], "Col2": [2.1, 2.2]}) create_sql = sql.get_schema(frame, "test", con=self.conn, keys="Col1") constraint_sentence = 'CONSTRAINT test_pk PRIMARY KEY ("Col1")' assert constraint_sentence in create_sql # multiple columns as key (GH10385) create_sql = sql.get_schema( self.test_frame1, "test", con=self.conn, keys=["A", "B"] ) constraint_sentence = 'CONSTRAINT test_pk PRIMARY KEY ("A", "B")' assert constraint_sentence in create_sql def test_chunksize_read(self): df = DataFrame(np.random.randn(22, 5), columns=list("abcde")) df.to_sql("test_chunksize", self.conn, index=False) # reading the query in one time res1 = sql.read_sql_query("select * from test_chunksize", self.conn) # reading the query in chunks with read_sql_query res2 = DataFrame() i = 0 sizes = [5, 5, 5, 5, 2] for chunk in sql.read_sql_query( "select * from test_chunksize", self.conn, chunksize=5 ): res2 = concat([res2, chunk], ignore_index=True) assert len(chunk) == sizes[i] i += 1 tm.assert_frame_equal(res1, res2) # reading the query in chunks with read_sql_query if self.mode == "sqlalchemy": res3 = DataFrame() i = 0 sizes = [5, 5, 5, 5, 2] for chunk in sql.read_sql_table("test_chunksize", self.conn, chunksize=5): res3 = concat([res3, chunk], ignore_index=True) assert len(chunk) == sizes[i] i += 1 tm.assert_frame_equal(res1, res3) def test_categorical(self): # GH8624 # test that categorical gets written correctly as dense column df = DataFrame( { "person_id": [1, 2, 3], "person_name": ["<NAME>", "<NAME>", "<NAME>"], } ) df2 = df.copy() df2["person_name"] = df2["person_name"].astype("category") df2.to_sql("test_categorical", self.conn, index=False) res = sql.read_sql_query("SELECT * FROM test_categorical", self.conn) tm.assert_frame_equal(res, df) def test_unicode_column_name(self): # GH 11431 df = DataFrame([[1, 2], [3, 4]], columns=["\xe9", "b"]) df.to_sql("test_unicode", self.conn, index=False) def test_escaped_table_name(self): # GH 13206 df = DataFrame({"A": [0, 1, 2], "B": [0.2, np.nan, 5.6]}) df.to_sql("d1187b08-4943-4c8d-a7f6", self.conn, index=False) res = sql.read_sql_query("SELECT * FROM `d1187b08-4943-4c8d-a7f6`", self.conn) tm.assert_frame_equal(res, df) @pytest.mark.single @pytest.mark.skipif(not SQLALCHEMY_INSTALLED, reason="SQLAlchemy not installed") class TestSQLApi(SQLAlchemyMixIn, _TestSQLApi): """ Test the public API as it would be used directly Tests for `read_sql_table` are included here, as this is specific for the sqlalchemy mode. """ flavor = "sqlite" mode = "sqlalchemy" def connect(self): return sqlalchemy.create_engine("sqlite:///:memory:") def test_read_table_columns(self): # test columns argument in read_table sql.to_sql(self.test_frame1, "test_frame", self.conn) cols = ["A", "B"] result = sql.read_sql_table("test_frame", self.conn, columns=cols) assert result.columns.tolist() == cols def test_read_table_index_col(self): # test columns argument in read_table sql.to_sql(self.test_frame1, "test_frame", self.conn) result = sql.read_sql_table("test_frame", self.conn, index_col="index") assert result.index.names == ["index"] result = sql.read_sql_table("test_frame", self.conn, index_col=["A", "B"]) assert result.index.names == ["A", "B"] result = sql.read_sql_table( "test_frame", self.conn, index_col=["A", "B"], columns=["C", "D"] ) assert result.index.names == ["A", "B"] assert result.columns.tolist() == ["C", "D"] def test_read_sql_delegate(self): iris_frame1 = sql.read_sql_query("SELECT * FROM iris", self.conn) iris_frame2 = sql.read_sql("SELECT * FROM iris", self.conn) tm.assert_frame_equal(iris_frame1, iris_frame2) iris_frame1 = sql.read_sql_table("iris", self.conn) iris_frame2 = sql.read_sql("iris", self.conn) tm.assert_frame_equal(iris_frame1, iris_frame2) def test_not_reflect_all_tables(self): # create invalid table qry = """CREATE TABLE invalid (x INTEGER, y UNKNOWN);""" self.conn.execute(qry) qry = """CREATE TABLE other_table (x INTEGER, y INTEGER);""" self.conn.execute(qry) with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") # Trigger a warning. sql.read_sql_table("other_table", self.conn) sql.read_sql_query("SELECT * FROM other_table", self.conn) # Verify some things assert len(w) == 0 def test_warning_case_insensitive_table_name(self): # see gh-7815 # # We can't test that this warning is triggered, a the database # configuration would have to be altered. But here we test that # the warning is certainly NOT triggered in a normal case. with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") # This should not trigger a Warning self.test_frame1.to_sql("CaseSensitive", self.conn) # Verify some things assert len(w) == 0 def _get_index_columns(self, tbl_name): from sqlalchemy.engine import reflection insp = reflection.Inspector.from_engine(self.conn) ixs = insp.get_indexes("test_index_saved") ixs = [i["column_names"] for i in ixs] return ixs def test_sqlalchemy_type_mapping(self): # Test Timestamp objects (no datetime64 because of timezone) (GH9085) df = DataFrame( {"time": to_datetime(["201412120154", "201412110254"], utc=True)} ) db = sql.SQLDatabase(self.conn) table = sql.SQLTable("test_type", db, frame=df) # GH 9086: TIMESTAMP is the suggested type for datetimes with timezones assert isinstance(table.table.c["time"].type, sqltypes.TIMESTAMP) def test_database_uri_string(self): # Test read_sql and .to_sql method with a database URI (GH10654) test_frame1 = self.test_frame1 # db_uri = 'sqlite:///:memory:' # raises # sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) near # "iris": syntax error [SQL: 'iris'] with tm.ensure_clean() as name: db_uri = "sqlite:///" + name table = "iris" test_frame1.to_sql(table, db_uri, if_exists="replace", index=False) test_frame2 = sql.read_sql(table, db_uri) test_frame3 = sql.read_sql_table(table, db_uri) query = "SELECT * FROM iris" test_frame4 = sql.read_sql_query(query, db_uri) tm.assert_frame_equal(test_frame1, test_frame2) tm.assert_frame_equal(test_frame1, test_frame3)
tm.assert_frame_equal(test_frame1, test_frame4)
pandas._testing.assert_frame_equal
""" Author: <NAME> Modified: <NAME> """ import os import warnings import numpy as np import pandas as pd import scipy.stats import pytest from numpy.testing import assert_almost_equal, assert_allclose from statsmodels.tools.sm_exceptions import EstimationWarning from statsmodels.tsa.holtwinters import (ExponentialSmoothing, SimpleExpSmoothing, Holt, SMOOTHERS, PY_SMOOTHERS) base, _ = os.path.split(os.path.abspath(__file__)) housing_data = pd.read_csv(os.path.join(base, 'results', 'housing-data.csv')) housing_data = housing_data.set_index('DATE') housing_data = housing_data.asfreq('MS') SEASONALS = ('add', 'mul', None) TRENDS = ('add', 'mul', None) def _simple_dbl_exp_smoother(x, alpha, beta, l0, b0, nforecast=0): """ Simple, slow, direct implementation of double exp smoothing for testing """ n = x.shape[0] lvals = np.zeros(n) b = np.zeros(n) xhat = np.zeros(n) f = np.zeros(nforecast) lvals[0] = l0 b[0] = b0 # Special case the 0 observations since index -1 is not available xhat[0] = l0 + b0 lvals[0] = alpha * x[0] + (1 - alpha) * (l0 + b0) b[0] = beta * (lvals[0] - l0) + (1 - beta) * b0 for t in range(1, n): # Obs in index t is the time t forecast for t + 1 lvals[t] = alpha * x[t] + (1 - alpha) * (lvals[t - 1] + b[t - 1]) b[t] = beta * (lvals[t] - lvals[t - 1]) + (1 - beta) * b[t - 1] xhat[1:] = lvals[0:-1] + b[0:-1] f[:] = lvals[-1] + np.arange(1, nforecast + 1) * b[-1] err = x - xhat return lvals, b, f, err, xhat class TestHoltWinters(object): @classmethod def setup_class(cls): # Changed for backwards compatibility with pandas # oildata_oil_json = '{"851990400000":446.6565229,"883526400000":454.4733065,"915062400000":455.662974,"946598400000":423.6322388,"978220800000":456.2713279,"1009756800000":440.5880501,"1041292800000":425.3325201,"1072828800000":485.1494479,"1104451200000":506.0481621,"1135987200000":526.7919833,"1167523200000":514.268889,"1199059200000":494.2110193}' # oildata_oil = pd.read_json(oildata_oil_json, typ='Series').sort_index() data = [446.65652290000003, 454.47330649999998, 455.66297400000002, 423.63223879999998, 456.27132790000002, 440.58805009999998, 425.33252010000001, 485.14944789999998, 506.04816210000001, 526.79198329999997, 514.26888899999994, 494.21101929999998] index = ['1996-12-31 00:00:00', '1997-12-31 00:00:00', '1998-12-31 00:00:00', '1999-12-31 00:00:00', '2000-12-31 00:00:00', '2001-12-31 00:00:00', '2002-12-31 00:00:00', '2003-12-31 00:00:00', '2004-12-31 00:00:00', '2005-12-31 00:00:00', '2006-12-31 00:00:00', '2007-12-31 00:00:00'] oildata_oil = pd.Series(data, index) oildata_oil.index = pd.DatetimeIndex(oildata_oil.index, freq=pd.infer_freq(oildata_oil.index)) cls.oildata_oil = oildata_oil # air_ausair_json = '{"662601600000":17.5534,"694137600000":21.8601,"725760000000":23.8866,"757296000000":26.9293,"788832000000":26.8885,"820368000000":28.8314,"851990400000":30.0751,"883526400000":30.9535,"915062400000":30.1857,"946598400000":31.5797,"978220800000":32.577569,"1009756800000":33.477398,"1041292800000":39.021581,"1072828800000":41.386432,"1104451200000":41.596552}' # air_ausair = pd.read_json(air_ausair_json, typ='Series').sort_index() data = [17.5534, 21.860099999999999, 23.886600000000001, 26.929300000000001, 26.888500000000001, 28.831399999999999, 30.075099999999999, 30.953499999999998, 30.185700000000001, 31.579699999999999, 32.577568999999997, 33.477398000000001, 39.021580999999998, 41.386431999999999, 41.596552000000003] index = ['1990-12-31 00:00:00', '1991-12-31 00:00:00', '1992-12-31 00:00:00', '1993-12-31 00:00:00', '1994-12-31 00:00:00', '1995-12-31 00:00:00', '1996-12-31 00:00:00', '1997-12-31 00:00:00', '1998-12-31 00:00:00', '1999-12-31 00:00:00', '2000-12-31 00:00:00', '2001-12-31 00:00:00', '2002-12-31 00:00:00', '2003-12-31 00:00:00', '2004-12-31 00:00:00'] air_ausair = pd.Series(data, index) air_ausair.index = pd.DatetimeIndex(air_ausair.index, freq=pd.infer_freq(air_ausair.index)) cls.air_ausair = air_ausair # livestock2_livestock_json = '{"31449600000":263.917747,"62985600000":268.307222,"94608000000":260.662556,"126144000000":266.639419,"157680000000":277.515778,"189216000000":283.834045,"220838400000":290.309028,"252374400000":292.474198,"283910400000":300.830694,"315446400000":309.286657,"347068800000":318.331081,"378604800000":329.37239,"410140800000":338.883998,"441676800000":339.244126,"473299200000":328.600632,"504835200000":314.255385,"536371200000":314.459695,"567907200000":321.413779,"599529600000":329.789292,"631065600000":346.385165,"662601600000":352.297882,"694137600000":348.370515,"725760000000":417.562922,"757296000000":417.12357,"788832000000":417.749459,"820368000000":412.233904,"851990400000":411.946817,"883526400000":394.697075,"915062400000":401.49927,"946598400000":408.270468,"978220800000":414.2428}' # livestock2_livestock = pd.read_json(livestock2_livestock_json, typ='Series').sort_index() data = [263.91774700000002, 268.30722200000002, 260.662556, 266.63941899999998, 277.51577800000001, 283.834045, 290.30902800000001, 292.474198, 300.83069399999999, 309.28665699999999, 318.33108099999998, 329.37239, 338.88399800000002, 339.24412599999999, 328.60063200000002, 314.25538499999999, 314.45969500000001, 321.41377899999998, 329.78929199999999, 346.38516499999997, 352.29788200000002, 348.37051500000001, 417.56292200000001, 417.12356999999997, 417.749459, 412.233904, 411.94681700000001, 394.69707499999998, 401.49927000000002, 408.27046799999999, 414.24279999999999] index = ['1970-12-31 00:00:00', '1971-12-31 00:00:00', '1972-12-31 00:00:00', '1973-12-31 00:00:00', '1974-12-31 00:00:00', '1975-12-31 00:00:00', '1976-12-31 00:00:00', '1977-12-31 00:00:00', '1978-12-31 00:00:00', '1979-12-31 00:00:00', '1980-12-31 00:00:00', '1981-12-31 00:00:00', '1982-12-31 00:00:00', '1983-12-31 00:00:00', '1984-12-31 00:00:00', '1985-12-31 00:00:00', '1986-12-31 00:00:00', '1987-12-31 00:00:00', '1988-12-31 00:00:00', '1989-12-31 00:00:00', '1990-12-31 00:00:00', '1991-12-31 00:00:00', '1992-12-31 00:00:00', '1993-12-31 00:00:00', '1994-12-31 00:00:00', '1995-12-31 00:00:00', '1996-12-31 00:00:00', '1997-12-31 00:00:00', '1998-12-31 00:00:00', '1999-12-31 00:00:00', '2000-12-31 00:00:00'] livestock2_livestock = pd.Series(data, index) livestock2_livestock.index = pd.DatetimeIndex( livestock2_livestock.index, freq=pd.infer_freq(livestock2_livestock.index)) cls.livestock2_livestock = livestock2_livestock # aust_json = '{"1104537600000":41.727458,"1112313600000":24.04185,"1120176000000":32.328103,"1128124800000":37.328708,"1136073600000":46.213153,"1143849600000":29.346326,"1151712000000":36.48291,"1159660800000":42.977719,"1167609600000":48.901525,"1175385600000":31.180221,"1183248000000":37.717881,"1191196800000":40.420211,"1199145600000":51.206863,"1207008000000":31.887228,"1214870400000":40.978263,"1222819200000":43.772491,"1230768000000":55.558567,"1238544000000":33.850915,"1246406400000":42.076383,"1254355200000":45.642292,"1262304000000":59.76678,"1270080000000":35.191877,"1277942400000":44.319737,"1285891200000":47.913736}' # aust = pd.read_json(aust_json, typ='Series').sort_index() data = [41.727457999999999, 24.04185, 32.328102999999999, 37.328707999999999, 46.213152999999998, 29.346326000000001, 36.482909999999997, 42.977719, 48.901524999999999, 31.180221, 37.717880999999998, 40.420211000000002, 51.206862999999998, 31.887228, 40.978262999999998, 43.772491000000002, 55.558566999999996, 33.850915000000001, 42.076383, 45.642291999999998, 59.766779999999997, 35.191876999999998, 44.319737000000003, 47.913736] index = ['2005-03-01 00:00:00', '2005-06-01 00:00:00', '2005-09-01 00:00:00', '2005-12-01 00:00:00', '2006-03-01 00:00:00', '2006-06-01 00:00:00', '2006-09-01 00:00:00', '2006-12-01 00:00:00', '2007-03-01 00:00:00', '2007-06-01 00:00:00', '2007-09-01 00:00:00', '2007-12-01 00:00:00', '2008-03-01 00:00:00', '2008-06-01 00:00:00', '2008-09-01 00:00:00', '2008-12-01 00:00:00', '2009-03-01 00:00:00', '2009-06-01 00:00:00', '2009-09-01 00:00:00', '2009-12-01 00:00:00', '2010-03-01 00:00:00', '2010-06-01 00:00:00', '2010-09-01 00:00:00', '2010-12-01 00:00:00'] aust = pd.Series(data, index) aust.index = pd.DatetimeIndex(aust.index, freq=pd.infer_freq(aust.index)) cls.aust = aust def test_predict(self): fit1 = ExponentialSmoothing(self.aust, seasonal_periods=4, trend='add', seasonal='mul').fit() fit2 = ExponentialSmoothing(self.aust, seasonal_periods=4, trend='add', seasonal='mul').fit() # fit3 = ExponentialSmoothing(self.aust, seasonal_periods=4, trend='add', # seasonal='mul').fit(remove_bias=True, use_basinhopping=True) assert_almost_equal(fit1.predict('2011-03-01 00:00:00', '2011-12-01 00:00:00'), [61.3083, 37.3730, 46.9652, 51.5578], 3) assert_almost_equal(fit2.predict(end='2011-12-01 00:00:00'), [61.3083, 37.3730, 46.9652, 51.5578], 3) # assert_almost_equal(fit3.predict('2010-10-01 00:00:00', '2010-10-01 00:00:00'), [49.087], 3) def test_ndarray(self): fit1 = ExponentialSmoothing(self.aust.values, seasonal_periods=4, trend='add', seasonal='mul').fit() assert_almost_equal(fit1.forecast(4), [61.3083, 37.3730, 46.9652, 51.5578], 3) # FIXME: this is passing 2019-05-22 on some platforms; what has changed? @pytest.mark.xfail(reason='Optimizer does not converge', strict=False) def test_forecast(self): fit1 = ExponentialSmoothing(self.aust, seasonal_periods=4, trend='add', seasonal='add').fit() assert_almost_equal(fit1.forecast(steps=4), [60.9542, 36.8505, 46.1628, 50.1272], 3) def test_simple_exp_smoothing(self): fit1 = SimpleExpSmoothing(self.oildata_oil).fit(0.2, optimized=False) fit2 = SimpleExpSmoothing(self.oildata_oil).fit(0.6, optimized=False) fit3 = SimpleExpSmoothing(self.oildata_oil).fit() assert_almost_equal(fit1.forecast(1), [484.802468], 4) assert_almost_equal(fit1.level, [446.65652290, 448.21987962, 449.7084985, 444.49324656, 446.84886283, 445.59670028, 441.54386424, 450.26498098, 461.4216172, 474.49569042, 482.45033014, 484.80246797], 4) assert_almost_equal(fit2.forecast(1), [501.837461], 4) assert_almost_equal(fit3.forecast(1), [496.493543], 4) assert_almost_equal(fit3.params['smoothing_level'], 0.891998, 4) # has to be 3 for old python2.7 scipy versions assert_almost_equal(fit3.params['initial_level'], 447.478440, 3) def test_holt(self): fit1 = Holt(self.air_ausair).fit(smoothing_level=0.8, smoothing_slope=0.2, optimized=False) fit2 = Holt(self.air_ausair, exponential=True).fit( smoothing_level=0.8, smoothing_slope=0.2, optimized=False) fit3 = Holt(self.air_ausair, damped=True).fit(smoothing_level=0.8, smoothing_slope=0.2) assert_almost_equal(fit1.forecast(5), [43.76, 45.59, 47.43, 49.27, 51.10], 2) assert_almost_equal(fit1.slope, [3.617628, 3.59006512, 3.33438212, 3.23657639, 2.69263502, 2.46388914, 2.2229097, 1.95959226, 1.47054601, 1.3604894, 1.28045881, 1.20355193, 1.88267152, 2.09564416, 1.83655482], 4) assert_almost_equal(fit1.fittedfcast, [21.8601, 22.032368, 25.48461872, 27.54058587, 30.28813356, 30.26106173, 31.58122149, 32.599234, 33.24223906, 32.26755382, 33.07776017, 33.95806605, 34.77708354, 40.05535303, 43.21586036, 43.75696849], 4) assert_almost_equal(fit2.forecast(5), [44.60, 47.24, 50.04, 53.01, 56.15], 2) assert_almost_equal(fit3.forecast(5), [42.85, 43.81, 44.66, 45.41, 46.06], 2) @pytest.mark.smoke def test_holt_damp_fit(self): # Smoke test for parameter estimation fit1 = SimpleExpSmoothing(self.livestock2_livestock).fit() mod4 = Holt(self.livestock2_livestock, damped=True) fit4 = mod4.fit(damping_slope=0.98) mod5 = Holt(self.livestock2_livestock, exponential=True, damped=True) fit5 = mod5.fit() # We accept the below values as we getting a better SSE than text book assert_almost_equal(fit1.params['smoothing_level'], 1.00, 2) assert_almost_equal(fit1.params['smoothing_slope'], np.NaN, 2) assert_almost_equal(fit1.params['damping_slope'], np.NaN, 2) assert_almost_equal(fit1.params['initial_level'], 263.92, 2) assert_almost_equal(fit1.params['initial_slope'], np.NaN, 2) assert_almost_equal(fit1.sse, 6761.35, 2) # 6080.26 assert_almost_equal(fit4.params['smoothing_level'], 0.98, 2) assert_almost_equal(fit4.params['smoothing_slope'], 0.00, 2) assert_almost_equal(fit4.params['damping_slope'], 0.98, 2) assert_almost_equal(fit4.params['initial_level'], 257.36, 2) assert_almost_equal(fit4.params['initial_slope'], 6.64, 2) assert_almost_equal(fit4.sse, 6036.56, 2) # 6080.26 assert_almost_equal(fit5.params['smoothing_level'], 0.97, 2) assert_almost_equal(fit5.params['smoothing_slope'], 0.00, 2) assert_almost_equal(fit5.params['damping_slope'], 0.98, 2) assert_almost_equal(fit5.params['initial_level'], 258.95, 2) assert_almost_equal(fit5.params['initial_slope'], 1.04, 2) assert_almost_equal(fit5.sse, 6082.00, 2) # 6100.11 def test_holt_damp_R(self): # Test the damping parameters against the R forecast packages `ets` # library(ets) # livestock2_livestock <- c(...) # res <- ets(livestock2_livestock, model='AAN', damped=TRUE, phi=0.98) mod = Holt(self.livestock2_livestock, damped=True) params = { 'smoothing_level': 0.97402626, 'smoothing_slope': 0.00010006, 'damping_slope': 0.98, 'initial_level': 252.59039965, 'initial_slope': 6.90265918} fit = mod.fit(optimized=False, **params) # Check that we captured the parameters correctly for key in params.keys(): assert_allclose(fit.params[key], params[key]) # Summary output # print(res$mse) assert_allclose(fit.sse / mod.nobs, 195.4397924865488, atol=1e-3) # print(res$aicc) # TODO: this fails - different AICC definition? # assert_allclose(fit.aicc, 282.386426659408, atol=1e-3) # print(res$bic) # TODO: this fails - different BIC definition? # assert_allclose(fit.bic, 287.1563626818338) # print(res$states[,'l']) # note: this array includes the initial level desired = [ 252.5903996514365, 263.7992355246843, 268.3623324350207, 261.0312983437606, 266.6590942700923, 277.3958197247272, 283.8256217863908, 290.2962560621914, 292.5701438129583, 300.7655919939834, 309.2118057241649, 318.2377698496536, 329.2238709362550, 338.7709778307978, 339.3669793596703, 329.0127022356033, 314.7684267018998, 314.5948077575944, 321.3612035017972, 329.6924360833211, 346.0712138652086, 352.2534120008911, 348.5862874190927, 415.8839400693967, 417.2018843196238, 417.8435306633725, 412.4857261252961, 412.0647865321129, 395.2500605270393, 401.4367438266322, 408.1907701386275, 414.1814574903921] assert_allclose(np.r_[fit.params['initial_level'], fit.level], desired) # print(res$states[,'b']) # note: this array includes the initial slope desired = [ 6.902659175332394, 6.765062519124909, 6.629548973536494, 6.495537532917715, 6.365550989616566, 6.238702070454378, 6.113960476763530, 5.991730467006233, 5.871526257315264, 5.754346516684953, 5.639547926790058, 5.527116419415724, 5.417146212898857, 5.309238662451385, 5.202580636191761, 5.096941655567694, 4.993026494493987, 4.892645486210410, 4.794995106664251, 4.699468310763351, 4.606688340205792, 4.514725879754355, 4.423600168391240, 4.341595902295941, 4.254462303550087, 4.169010676686062, 4.084660399498803, 4.002512751871354, 3.920332298146730, 3.842166514133902, 3.765630194200260, 3.690553892582855] # TODO: not sure why the precision is so low here... assert_allclose(np.r_[fit.params['initial_slope'], fit.slope], desired, atol=1e-3) # print(res$fitted) desired = [ 259.3550056432622, 270.4289967934267, 274.8592904290865, 267.3969251260200, 272.8973342399166, 283.5097477537724, 289.8173030536191, 296.1681519198575, 298.3242395451272, 306.4048515803347, 314.7385626924191, 323.6543439406810, 334.5326742248959, 343.9740317200002, 344.4655083831382, 334.0077050580596, 319.6615926665040, 319.3896003340806, 326.0602987063282, 334.2979150278692, 350.5857684386102, 356.6778433630504, 352.9214155841161, 420.1387040536467, 421.3712573771029, 421.9291611265248, 416.4886933168049, 415.9872490289468, 399.0919861792231, 405.2020670104834, 411.8810877289437] assert_allclose(fit.fittedvalues, desired, atol=1e-3) # print(forecast(res)$mean) desired = [ 417.7982003051233, 421.3426082635598, 424.8161280628277, 428.2201774661102, 431.5561458813270, 434.8253949282395, 438.0292589942138, 441.1690457788685, 444.2460368278302, 447.2614880558126] assert_allclose(fit.forecast(10), desired, atol=1e-4) def test_hw_seasonal(self): mod = ExponentialSmoothing(self.aust, seasonal_periods=4, trend='additive', seasonal='additive') fit1 = mod.fit(use_boxcox=True) fit2 = ExponentialSmoothing(self.aust, seasonal_periods=4, trend='add', seasonal='mul').fit(use_boxcox=True) assert_almost_equal(fit1.forecast(8), [61.34, 37.24, 46.84, 51.01, 64.47, 39.78, 49.64, 53.90], 2) assert_almost_equal(fit2.forecast(8), [60.97, 36.99, 46.71, 51.48, 64.46, 39.02, 49.29, 54.32], 2) ExponentialSmoothing(self.aust, seasonal_periods=4, trend='mul', seasonal='add').fit(use_boxcox='log') ExponentialSmoothing(self.aust, seasonal_periods=4, trend='multiplicative', seasonal='multiplicative').fit(use_boxcox='log') # Skip since estimator is unstable # assert_almost_equal(fit5.forecast(1), [60.60], 2) # assert_almost_equal(fit6.forecast(1), [61.47], 2) # FIXME: this is passing 2019-05-22; what has changed? # @pytest.mark.xfail(reason='Optimizer does not converge') def test_hw_seasonal_buggy(self): fit3 = ExponentialSmoothing(self.aust, seasonal_periods=4, seasonal='add').fit(use_boxcox=True) assert_almost_equal(fit3.forecast(8), [59.91, 35.71, 44.64, 47.62, 59.91, 35.71, 44.64, 47.62], 2) fit4 = ExponentialSmoothing(self.aust, seasonal_periods=4, seasonal='mul').fit(use_boxcox=True) assert_almost_equal(fit4.forecast(8), [60.71, 35.70, 44.63, 47.55, 60.71, 35.70, 44.63, 47.55], 2) @pytest.mark.parametrize('trend_seasonal', (('mul', None), (None, 'mul'), ('mul', 'mul'))) def test_negative_multipliative(trend_seasonal): trend, seasonal = trend_seasonal y = -np.ones(100) with pytest.raises(ValueError): ExponentialSmoothing(y, trend=trend, seasonal=seasonal, seasonal_periods=10) @pytest.mark.parametrize('seasonal', SEASONALS) def test_dampen_no_trend(seasonal): y = -np.ones(100) with pytest.raises(TypeError): ExponentialSmoothing(housing_data, trend=False, seasonal=seasonal, damped=True, seasonal_periods=10) @pytest.mark.parametrize('seasonal', ('add', 'mul')) def test_invalid_seasonal(seasonal): y = pd.Series(-np.ones(100),index=
pd.date_range('2000-1-1', periods=100, freq='MS')
pandas.date_range
import os import pandas as pd import pytest from dplypy.dplyframe import DplyFrame from dplypy.pipeline import write_file def test_write_file(): pandas_df = pd.DataFrame( data={ "col1": [0, 1, 2, 3], "col2": [3, 4, 5, 6], "col3": [6, 7, 8, 9], "col4": [9, 10, 11, 12], } ) df = DplyFrame(pandas_df) # To csv file ( df + write_file("df_no_index.csv", sep=",", index=False) + write_file("df_with_index.csv", sep=",", index=True) ) read_df = pd.read_csv("df_no_index.csv", sep=",") pd.testing.assert_frame_equal(df.pandas_df, read_df) # Without index os.remove("df_no_index.csv") read_df =
pd.read_csv("df_with_index.csv", sep=",", index_col=0)
pandas.read_csv
import numpy as np import pytest import pandas as pd from pandas import ( Categorical, DataFrame, Index, Series, ) import pandas._testing as tm dt_data = [ pd.Timestamp("2011-01-01"), pd.Timestamp("2011-01-02"), pd.Timestamp("2011-01-03"), ] tz_data = [ pd.Timestamp("2011-01-01", tz="US/Eastern"), pd.Timestamp("2011-01-02", tz="US/Eastern"), pd.Timestamp("2011-01-03", tz="US/Eastern"), ] td_data = [ pd.Timedelta("1 days"), pd.Timedelta("2 days"), pd.Timedelta("3 days"), ] period_data = [ pd.Period("2011-01", freq="M"), pd.Period("2011-02", freq="M"), pd.Period("2011-03", freq="M"), ] data_dict = { "bool": [True, False, True], "int64": [1, 2, 3], "float64": [1.1, np.nan, 3.3], "category": Categorical(["X", "Y", "Z"]), "object": ["a", "b", "c"], "datetime64[ns]": dt_data, "datetime64[ns, US/Eastern]": tz_data, "timedelta64[ns]": td_data, "period[M]": period_data, } class TestConcatAppendCommon: """ Test common dtype coercion rules between concat and append. """ @pytest.fixture(params=sorted(data_dict.keys())) def item(self, request): key = request.param return key, data_dict[key] item2 = item def _check_expected_dtype(self, obj, label): """ Check whether obj has expected dtype depending on label considering not-supported dtypes """ if isinstance(obj, Index): assert obj.dtype == label elif isinstance(obj, Series): if label.startswith("period"): assert obj.dtype == "Period[M]" else: assert obj.dtype == label else: raise ValueError def test_dtypes(self, item): # to confirm test case covers intended dtypes typ, vals = item self._check_expected_dtype(Index(vals), typ) self._check_expected_dtype(Series(vals), typ) def test_concatlike_same_dtypes(self, item): # GH 13660 typ1, vals1 = item vals2 = vals1 vals3 = vals1 if typ1 == "category": exp_data = Categorical(list(vals1) + list(vals2)) exp_data3 = Categorical(list(vals1) + list(vals2) + list(vals3)) else: exp_data = vals1 + vals2 exp_data3 = vals1 + vals2 + vals3 # ----- Index ----- # # index.append res = Index(vals1).append(Index(vals2)) exp = Index(exp_data) tm.assert_index_equal(res, exp) # 3 elements res = Index(vals1).append([Index(vals2), Index(vals3)]) exp = Index(exp_data3) tm.assert_index_equal(res, exp) # index.append name mismatch i1 = Index(vals1, name="x") i2 = Index(vals2, name="y") res = i1.append(i2) exp = Index(exp_data) tm.assert_index_equal(res, exp) # index.append name match i1 = Index(vals1, name="x") i2 = Index(vals2, name="x") res = i1.append(i2) exp = Index(exp_data, name="x") tm.assert_index_equal(res, exp) # cannot append non-index with pytest.raises(TypeError, match="all inputs must be Index"): Index(vals1).append(vals2) with pytest.raises(TypeError, match="all inputs must be Index"): Index(vals1).append([Index(vals2), vals3]) # ----- Series ----- # # series.append res = Series(vals1)._append(Series(vals2), ignore_index=True) exp = Series(exp_data) tm.assert_series_equal(res, exp, check_index_type=True) # concat res = pd.concat([Series(vals1), Series(vals2)], ignore_index=True) tm.assert_series_equal(res, exp, check_index_type=True) # 3 elements res = Series(vals1)._append([Series(vals2), Series(vals3)], ignore_index=True) exp = Series(exp_data3) tm.assert_series_equal(res, exp) res = pd.concat( [Series(vals1), Series(vals2),
Series(vals3)
pandas.Series
import tables import pandas as pd def h5_gps(fn): with tables.open_file(fn, "r") as fh: data = fh.root.gps.read() return pd.DataFrame(data) def h5_gps_yo(fn): data = h5_gps(fn) return data.to_dict('list') def h5_ins(fn): with tables.open_file(fn, "r") as fh: data = fh.root.ins.read() return pd.DataFrame(data) def h5_ins_yo(fn): with tables.open_file(fn, "r") as fh: data = fh.root.ins.read() head = fh.root.ins.attrs.headers data =
pd.DataFrame(data)
pandas.DataFrame
from pipelines.base_pipeline import BasePipeline from pipelines.unet.unet_architecture_paper import color_model from pipelines.common_utils.lungmap_dataset import LungmapDataSet from pipelines.unet.data_pipeline import unet_generators from keras.callbacks import ModelCheckpoint import os from keras.models import load_model import numpy as np import matplotlib.pyplot as plt from cv2_extras import utils as cv2x from cv_color_features import utils as color_utils from sklearn.svm import SVC from sklearn.pipeline import Pipeline from sklearn.preprocessing import MinMaxScaler import pandas as pd import copy from joblib import dump, load import cv2 import scipy class UnetClassifierPipeline(BasePipeline): def __init__(self, image_set_dir, model_dir='tmp/models/unet', model_name='thing_unet.hdf5', train_images=None, test_images=None, val_images=None, input_shape=(1024, 1024, 3), output_shape=(1024, 1024, 5) ): super().__init__(image_set_dir) self.test_img_name = test_images[0] # TODO: make sure there are enough images in the image set, as this pipeline # needs to separate an image for validation, in addition to the reserved test image. self.model_name = os.path.join(model_dir, model_name) self.td = copy.deepcopy(self.training_data) training_data = {x: self.td[x] for x in train_images} test_data = {x: self.td[x] for x in test_images} validation_data = {x: self.td[x] for x in val_images} self.test_images = test_images classifier = SVC(probability=True, class_weight='balanced') self.pipe = Pipeline( [ ('scaler', MinMaxScaler()), ('classification', classifier) ] ) self.dataset_train = LungmapDataSet() self.dataset_train.load_data_set(training_data) self.dataset_train.prepare() self.dataset_validation = LungmapDataSet() self.dataset_validation.load_data_set(validation_data) self.dataset_validation.prepare() self.dataset_test = LungmapDataSet() self.dataset_test.load_data_set(test_data) self.dataset_test.prepare() self.modify_dataset(self.dataset_train) self.modify_dataset(self.dataset_test) self.modify_dataset(self.dataset_validation) self.train_generator, self.class_names = unet_generators( self.dataset_train, input_shape, output_shape, ) self.val_generator, _ = unet_generators( self.dataset_validation, input_shape, output_shape ) self.test_generator, _ = unet_generators( self.dataset_test, input_shape, output_shape ) self.model = color_model() self.training_data_processed = None self.test_data_processed = None @staticmethod def modify_dataset(which_dataset): which_dataset.class_names = ['BG', 'thing'] new_image_info = [] for img in which_dataset.image_info: tmp_poly = img.pop('polygons') new_poly = [] for poly in tmp_poly: poly['label'] = 'thing' new_poly.append(poly) img['polygons'] = new_poly new_image_info.append(img) def train(self, epochparam=5): model_checkpoint = ModelCheckpoint( self.model_name, monitor='val_loss', verbose=1, save_best_only=True) self.model.fit_generator( self.train_generator, validation_data=self.val_generator, validation_steps=5, steps_per_epoch=30, epochs=epochparam, callbacks=[model_checkpoint], ) def train_classifier(self): # process the training data to get custom metrics training_data_processed = [] for img_name, img_data in self.td.items(): if img_name in self.test_images: continue non_bg_regions = [] for region in img_data['regions']: training_data_processed.append( color_utils.generate_features( hsv_img_as_numpy=img_data['hsv_img'], polygon_points=region['points'], label=region['label'] ) ) non_bg_regions.append(region['points']) bg_contours = cv2x.generate_background_contours( img_data['hsv_img'], non_bg_regions ) for region in bg_contours: training_data_processed.append( color_utils.generate_features( hsv_img_as_numpy=img_data['hsv_img'], polygon_points=region, label='background' ) ) # train the SVM model with the ground truth self.training_data_processed = pd.DataFrame(training_data_processed) self.training_data_processed.to_csv('tmp/models/unet/classification_data.csv', index=False) self.pipe.fit( self.training_data_processed.drop('label', axis=1), self.training_data_processed['label'] ) dump(self.pipe, 'tmp/models/unet/classifier.joblib') def test(self): img_rgb = cv2.cvtColor(self.training_data[self.test_img_name]['hsv_img'], cv2.COLOR_HSV2RGB) img_down = scipy.misc.imresize(img_rgb, (1024, 1024, 3)) trained_model = load_model(self.model_name) classifier = load('tmp/models/unet/classifier.joblib') pred = trained_model.predict(img_down.reshape(1, 1024, 1024, 3)) pred_thresh = np.where(pred > 0.5, 1, 0) assert pred_thresh.shape[0] == 1 pred_thresh_up = scipy.misc.imresize(pred_thresh[0, :, :, 0], (2475, 2475)) _, contours, _ = cv2.findContours( pred_thresh_up.astype('uint8'), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE ) contour_filter = [c for c in contours if c.shape[0] > 3] hsv_img = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2HSV) test_data_processed = [] final_contour_results = [] for c_idx, c in enumerate(contour_filter): features = color_utils.generate_features( hsv_img, c ) features_df = pd.DataFrame([features]) probabilities = classifier.predict_proba(features_df.drop('label', axis=1)) labelled_probs = {a: probabilities[0][i] for i, a in enumerate(classifier.classes_)} pred_label = max(labelled_probs, key=lambda key: labelled_probs[key]) pred_label_prob = labelled_probs[pred_label] final_contour_results.append( { 'test_index': c_idx, 'contour': c, 'prob': labelled_probs } ) features['pred_label'] = pred_label features['pred_label_prob'] = pred_label_prob features['region_file'] = None features['region_index'] = c_idx test_data_processed.append(features) self.test_data_processed =
pd.DataFrame(test_data_processed)
pandas.DataFrame
from __future__ import print_function from datetime import datetime, timedelta import numpy as np import pandas as pd from pandas import (Series, Index, Int64Index, Timestamp, Period, DatetimeIndex, PeriodIndex, TimedeltaIndex, Timedelta, timedelta_range, date_range, Float64Index, _np_version_under1p10) import pandas.tslib as tslib import pandas.tseries.period as period import pandas.util.testing as tm from pandas.tests.test_base import Ops class TestDatetimeIndexOps(Ops): tz = [None, 'UTC', 'Asia/Tokyo', 'US/Eastern', 'dateutil/Asia/Singapore', 'dateutil/US/Pacific'] def setUp(self): super(TestDatetimeIndexOps, self).setUp() mask = lambda x: (isinstance(x, DatetimeIndex) or isinstance(x, PeriodIndex)) self.is_valid_objs = [o for o in self.objs if mask(o)] self.not_valid_objs = [o for o in self.objs if not mask(o)] def test_ops_properties(self): self.check_ops_properties( ['year', 'month', 'day', 'hour', 'minute', 'second', 'weekofyear', 'week', 'dayofweek', 'dayofyear', 'quarter']) self.check_ops_properties(['date', 'time', 'microsecond', 'nanosecond', 'is_month_start', 'is_month_end', 'is_quarter_start', 'is_quarter_end', 'is_year_start', 'is_year_end', 'weekday_name'], lambda x: isinstance(x, DatetimeIndex)) def test_ops_properties_basic(self): # sanity check that the behavior didn't change # GH7206 for op in ['year', 'day', 'second', 'weekday']: self.assertRaises(TypeError, lambda x: getattr(self.dt_series, op)) # attribute access should still work! s = Series(dict(year=2000, month=1, day=10)) self.assertEqual(s.year, 2000) self.assertEqual(s.month, 1) self.assertEqual(s.day, 10) self.assertRaises(AttributeError, lambda: s.weekday) def test_asobject_tolist(self): idx = pd.date_range(start='2013-01-01', periods=4, freq='M', name='idx') expected_list = [Timestamp('2013-01-31'), Timestamp('2013-02-28'), Timestamp('2013-03-31'), Timestamp('2013-04-30')] expected = pd.Index(expected_list, dtype=object, name='idx') result = idx.asobject self.assertTrue(isinstance(result, Index)) self.assertEqual(result.dtype, object) self.assert_index_equal(result, expected) self.assertEqual(result.name, expected.name) self.assertEqual(idx.tolist(), expected_list) idx = pd.date_range(start='2013-01-01', periods=4, freq='M', name='idx', tz='Asia/Tokyo') expected_list = [Timestamp('2013-01-31', tz='Asia/Tokyo'), Timestamp('2013-02-28', tz='Asia/Tokyo'), Timestamp('2013-03-31', tz='Asia/Tokyo'), Timestamp('2013-04-30', tz='Asia/Tokyo')] expected = pd.Index(expected_list, dtype=object, name='idx') result = idx.asobject self.assertTrue(isinstance(result, Index)) self.assertEqual(result.dtype, object) self.assert_index_equal(result, expected) self.assertEqual(result.name, expected.name) self.assertEqual(idx.tolist(), expected_list) idx = DatetimeIndex([datetime(2013, 1, 1), datetime(2013, 1, 2), pd.NaT, datetime(2013, 1, 4)], name='idx') expected_list = [Timestamp('2013-01-01'), Timestamp('2013-01-02'), pd.NaT, Timestamp('2013-01-04')] expected = pd.Index(expected_list, dtype=object, name='idx') result = idx.asobject self.assertTrue(isinstance(result, Index)) self.assertEqual(result.dtype, object) self.assert_index_equal(result, expected) self.assertEqual(result.name, expected.name) self.assertEqual(idx.tolist(), expected_list) def test_minmax(self): for tz in self.tz: # monotonic idx1 = pd.DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03'], tz=tz) self.assertTrue(idx1.is_monotonic) # non-monotonic idx2 = pd.DatetimeIndex(['2011-01-01', pd.NaT, '2011-01-03', '2011-01-02', pd.NaT], tz=tz) self.assertFalse(idx2.is_monotonic) for idx in [idx1, idx2]: self.assertEqual(idx.min(), Timestamp('2011-01-01', tz=tz)) self.assertEqual(idx.max(), Timestamp('2011-01-03', tz=tz)) self.assertEqual(idx.argmin(), 0) self.assertEqual(idx.argmax(), 2) for op in ['min', 'max']: # Return NaT obj = DatetimeIndex([]) self.assertTrue(pd.isnull(getattr(obj, op)())) obj = DatetimeIndex([pd.NaT]) self.assertTrue(pd.isnull(getattr(obj, op)())) obj = DatetimeIndex([pd.NaT, pd.NaT, pd.NaT]) self.assertTrue(pd.isnull(getattr(obj, op)())) def test_numpy_minmax(self): dr = pd.date_range(start='2016-01-15', end='2016-01-20') self.assertEqual(np.min(dr), Timestamp('2016-01-15 00:00:00', freq='D')) self.assertEqual(np.max(dr), Timestamp('2016-01-20 00:00:00', freq='D')) errmsg = "the 'out' parameter is not supported" tm.assertRaisesRegexp(ValueError, errmsg, np.min, dr, out=0) tm.assertRaisesRegexp(ValueError, errmsg, np.max, dr, out=0) self.assertEqual(np.argmin(dr), 0) self.assertEqual(np.argmax(dr), 5) if not _np_version_under1p10: errmsg = "the 'out' parameter is not supported" tm.assertRaisesRegexp(ValueError, errmsg, np.argmin, dr, out=0) tm.assertRaisesRegexp(ValueError, errmsg, np.argmax, dr, out=0) def test_round(self): for tz in self.tz: rng = pd.date_range(start='2016-01-01', periods=5, freq='30Min', tz=tz) elt = rng[1] expected_rng = DatetimeIndex([ Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'), Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'), Timestamp('2016-01-01 01:00:00', tz=tz, freq='30T'), Timestamp('2016-01-01 02:00:00', tz=tz, freq='30T'), Timestamp('2016-01-01 02:00:00', tz=tz, freq='30T'), ]) expected_elt = expected_rng[1] tm.assert_index_equal(rng.round(freq='H'), expected_rng) self.assertEqual(elt.round(freq='H'), expected_elt) msg = pd.tseries.frequencies._INVALID_FREQ_ERROR with tm.assertRaisesRegexp(ValueError, msg): rng.round(freq='foo') with tm.assertRaisesRegexp(ValueError, msg): elt.round(freq='foo') msg = "<MonthEnd> is a non-fixed frequency" tm.assertRaisesRegexp(ValueError, msg, rng.round, freq='M') tm.assertRaisesRegexp(ValueError, msg, elt.round, freq='M') def test_repeat_range(self): rng = date_range('1/1/2000', '1/1/2001') result = rng.repeat(5) self.assertIsNone(result.freq) self.assertEqual(len(result), 5 * len(rng)) for tz in self.tz: index = pd.date_range('2001-01-01', periods=2, freq='D', tz=tz) exp = pd.DatetimeIndex(['2001-01-01', '2001-01-01', '2001-01-02', '2001-01-02'], tz=tz) for res in [index.repeat(2), np.repeat(index, 2)]: tm.assert_index_equal(res, exp) self.assertIsNone(res.freq) index = pd.date_range('2001-01-01', periods=2, freq='2D', tz=tz) exp = pd.DatetimeIndex(['2001-01-01', '2001-01-01', '2001-01-03', '2001-01-03'], tz=tz) for res in [index.repeat(2), np.repeat(index, 2)]: tm.assert_index_equal(res, exp) self.assertIsNone(res.freq) index = pd.DatetimeIndex(['2001-01-01', 'NaT', '2003-01-01'], tz=tz) exp = pd.DatetimeIndex(['2001-01-01', '2001-01-01', '2001-01-01', 'NaT', 'NaT', 'NaT', '2003-01-01', '2003-01-01', '2003-01-01'], tz=tz) for res in [index.repeat(3), np.repeat(index, 3)]: tm.assert_index_equal(res, exp) self.assertIsNone(res.freq) def test_repeat(self): reps = 2 msg = "the 'axis' parameter is not supported" for tz in self.tz: rng = pd.date_range(start='2016-01-01', periods=2, freq='30Min', tz=tz) expected_rng = DatetimeIndex([ Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'), Timestamp('2016-01-01 00:00:00', tz=tz, freq='30T'), Timestamp('2016-01-01 00:30:00', tz=tz, freq='30T'), Timestamp('2016-01-01 00:30:00', tz=tz, freq='30T'), ]) res = rng.repeat(reps) tm.assert_index_equal(res, expected_rng) self.assertIsNone(res.freq) tm.assert_index_equal(np.repeat(rng, reps), expected_rng) tm.assertRaisesRegexp(ValueError, msg, np.repeat, rng, reps, axis=1) def test_representation(self): idx = [] idx.append(DatetimeIndex([], freq='D')) idx.append(DatetimeIndex(['2011-01-01'], freq='D')) idx.append(DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D')) idx.append(DatetimeIndex( ['2011-01-01', '2011-01-02', '2011-01-03'], freq='D')) idx.append(DatetimeIndex( ['2011-01-01 09:00', '2011-01-01 10:00', '2011-01-01 11:00' ], freq='H', tz='Asia/Tokyo')) idx.append(DatetimeIndex( ['2011-01-01 09:00', '2011-01-01 10:00', pd.NaT], tz='US/Eastern')) idx.append(DatetimeIndex( ['2011-01-01 09:00', '2011-01-01 10:00', pd.NaT], tz='UTC')) exp = [] exp.append("""DatetimeIndex([], dtype='datetime64[ns]', freq='D')""") exp.append("DatetimeIndex(['2011-01-01'], dtype='datetime64[ns]', " "freq='D')") exp.append("DatetimeIndex(['2011-01-01', '2011-01-02'], " "dtype='datetime64[ns]', freq='D')") exp.append("DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03'], " "dtype='datetime64[ns]', freq='D')") exp.append("DatetimeIndex(['2011-01-01 09:00:00+09:00', " "'2011-01-01 10:00:00+09:00', '2011-01-01 11:00:00+09:00']" ", dtype='datetime64[ns, Asia/Tokyo]', freq='H')") exp.append("DatetimeIndex(['2011-01-01 09:00:00-05:00', " "'2011-01-01 10:00:00-05:00', 'NaT'], " "dtype='datetime64[ns, US/Eastern]', freq=None)") exp.append("DatetimeIndex(['2011-01-01 09:00:00+00:00', " "'2011-01-01 10:00:00+00:00', 'NaT'], " "dtype='datetime64[ns, UTC]', freq=None)""") with pd.option_context('display.width', 300): for indx, expected in zip(idx, exp): for func in ['__repr__', '__unicode__', '__str__']: result = getattr(indx, func)() self.assertEqual(result, expected) def test_representation_to_series(self): idx1 = DatetimeIndex([], freq='D') idx2 = DatetimeIndex(['2011-01-01'], freq='D') idx3 = DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D') idx4 = DatetimeIndex( ['2011-01-01', '2011-01-02', '2011-01-03'], freq='D') idx5 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00', '2011-01-01 11:00'], freq='H', tz='Asia/Tokyo') idx6 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00', pd.NaT], tz='US/Eastern') idx7 = DatetimeIndex(['2011-01-01 09:00', '2011-01-02 10:15']) exp1 = """Series([], dtype: datetime64[ns])""" exp2 = """0 2011-01-01 dtype: datetime64[ns]""" exp3 = """0 2011-01-01 1 2011-01-02 dtype: datetime64[ns]""" exp4 = """0 2011-01-01 1 2011-01-02 2 2011-01-03 dtype: datetime64[ns]""" exp5 = """0 2011-01-01 09:00:00+09:00 1 2011-01-01 10:00:00+09:00 2 2011-01-01 11:00:00+09:00 dtype: datetime64[ns, Asia/Tokyo]""" exp6 = """0 2011-01-01 09:00:00-05:00 1 2011-01-01 10:00:00-05:00 2 NaT dtype: datetime64[ns, US/Eastern]""" exp7 = """0 2011-01-01 09:00:00 1 2011-01-02 10:15:00 dtype: datetime64[ns]""" with pd.option_context('display.width', 300): for idx, expected in zip([idx1, idx2, idx3, idx4, idx5, idx6, idx7], [exp1, exp2, exp3, exp4, exp5, exp6, exp7]): result = repr(Series(idx)) self.assertEqual(result, expected) def test_summary(self): # GH9116 idx1 = DatetimeIndex([], freq='D') idx2 = DatetimeIndex(['2011-01-01'], freq='D') idx3 = DatetimeIndex(['2011-01-01', '2011-01-02'], freq='D') idx4 = DatetimeIndex( ['2011-01-01', '2011-01-02', '2011-01-03'], freq='D') idx5 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00', '2011-01-01 11:00'], freq='H', tz='Asia/Tokyo') idx6 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00', pd.NaT], tz='US/Eastern') exp1 = """DatetimeIndex: 0 entries Freq: D""" exp2 = """DatetimeIndex: 1 entries, 2011-01-01 to 2011-01-01 Freq: D""" exp3 = """DatetimeIndex: 2 entries, 2011-01-01 to 2011-01-02 Freq: D""" exp4 = """DatetimeIndex: 3 entries, 2011-01-01 to 2011-01-03 Freq: D""" exp5 = ("DatetimeIndex: 3 entries, 2011-01-01 09:00:00+09:00 " "to 2011-01-01 11:00:00+09:00\n" "Freq: H") exp6 = """DatetimeIndex: 3 entries, 2011-01-01 09:00:00-05:00 to NaT""" for idx, expected in zip([idx1, idx2, idx3, idx4, idx5, idx6], [exp1, exp2, exp3, exp4, exp5, exp6]): result = idx.summary() self.assertEqual(result, expected) def test_resolution(self): for freq, expected in zip(['A', 'Q', 'M', 'D', 'H', 'T', 'S', 'L', 'U'], ['day', 'day', 'day', 'day', 'hour', 'minute', 'second', 'millisecond', 'microsecond']): for tz in self.tz: idx = pd.date_range(start='2013-04-01', periods=30, freq=freq, tz=tz) self.assertEqual(idx.resolution, expected) def test_union(self): for tz in self.tz: # union rng1 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz) other1 = pd.date_range('1/6/2000', freq='D', periods=5, tz=tz) expected1 = pd.date_range('1/1/2000', freq='D', periods=10, tz=tz) rng2 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz) other2 = pd.date_range('1/4/2000', freq='D', periods=5, tz=tz) expected2 = pd.date_range('1/1/2000', freq='D', periods=8, tz=tz) rng3 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz) other3 = pd.DatetimeIndex([], tz=tz) expected3 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz) for rng, other, expected in [(rng1, other1, expected1), (rng2, other2, expected2), (rng3, other3, expected3)]: result_union = rng.union(other) tm.assert_index_equal(result_union, expected) def test_add_iadd(self): for tz in self.tz: # offset offsets = [pd.offsets.Hour(2), timedelta(hours=2), np.timedelta64(2, 'h'), Timedelta(hours=2)] for delta in offsets: rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) result = rng + delta expected = pd.date_range('2000-01-01 02:00', '2000-02-01 02:00', tz=tz) tm.assert_index_equal(result, expected) rng += delta tm.assert_index_equal(rng, expected) # int rng = pd.date_range('2000-01-01 09:00', freq='H', periods=10, tz=tz) result = rng + 1 expected = pd.date_range('2000-01-01 10:00', freq='H', periods=10, tz=tz) tm.assert_index_equal(result, expected) rng += 1 tm.assert_index_equal(rng, expected) idx = DatetimeIndex(['2011-01-01', '2011-01-02']) msg = "cannot add a datelike to a DatetimeIndex" with tm.assertRaisesRegexp(TypeError, msg): idx + Timestamp('2011-01-01') with tm.assertRaisesRegexp(TypeError, msg): Timestamp('2011-01-01') + idx def test_add_dti_dti(self): # previously performed setop (deprecated in 0.16.0), now raises # TypeError (GH14164) dti = date_range('20130101', periods=3) dti_tz = date_range('20130101', periods=3).tz_localize('US/Eastern') with tm.assertRaises(TypeError): dti + dti with tm.assertRaises(TypeError): dti_tz + dti_tz with tm.assertRaises(TypeError): dti_tz + dti with tm.assertRaises(TypeError): dti + dti_tz def test_difference(self): for tz in self.tz: # diff rng1 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz) other1 = pd.date_range('1/6/2000', freq='D', periods=5, tz=tz) expected1 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz) rng2 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz) other2 = pd.date_range('1/4/2000', freq='D', periods=5, tz=tz) expected2 = pd.date_range('1/1/2000', freq='D', periods=3, tz=tz) rng3 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz) other3 = pd.DatetimeIndex([], tz=tz) expected3 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz) for rng, other, expected in [(rng1, other1, expected1), (rng2, other2, expected2), (rng3, other3, expected3)]: result_diff = rng.difference(other) tm.assert_index_equal(result_diff, expected) def test_sub_isub(self): for tz in self.tz: # offset offsets = [pd.offsets.Hour(2), timedelta(hours=2), np.timedelta64(2, 'h'), Timedelta(hours=2)] for delta in offsets: rng = pd.date_range('2000-01-01', '2000-02-01', tz=tz) expected = pd.date_range('1999-12-31 22:00', '2000-01-31 22:00', tz=tz) result = rng - delta tm.assert_index_equal(result, expected) rng -= delta tm.assert_index_equal(rng, expected) # int rng = pd.date_range('2000-01-01 09:00', freq='H', periods=10, tz=tz) result = rng - 1 expected = pd.date_range('2000-01-01 08:00', freq='H', periods=10, tz=tz) tm.assert_index_equal(result, expected) rng -= 1 tm.assert_index_equal(rng, expected) def test_sub_dti_dti(self): # previously performed setop (deprecated in 0.16.0), now changed to # return subtraction -> TimeDeltaIndex (GH ...) dti = date_range('20130101', periods=3) dti_tz = date_range('20130101', periods=3).tz_localize('US/Eastern') dti_tz2 = date_range('20130101', periods=3).tz_localize('UTC') expected = TimedeltaIndex([0, 0, 0]) result = dti - dti tm.assert_index_equal(result, expected) result = dti_tz - dti_tz tm.assert_index_equal(result, expected) with tm.assertRaises(TypeError): dti_tz - dti with tm.assertRaises(TypeError): dti - dti_tz with tm.assertRaises(TypeError): dti_tz - dti_tz2 # isub dti -= dti tm.assert_index_equal(dti, expected) # different length raises ValueError dti1 = date_range('20130101', periods=3) dti2 = date_range('20130101', periods=4) with tm.assertRaises(ValueError): dti1 - dti2 # NaN propagation dti1 = DatetimeIndex(['2012-01-01', np.nan, '2012-01-03']) dti2 = DatetimeIndex(['2012-01-02', '2012-01-03', np.nan]) expected = TimedeltaIndex(['1 days', np.nan, np.nan]) result = dti2 - dti1 tm.assert_index_equal(result, expected) def test_sub_period(self): # GH 13078 # not supported, check TypeError p = pd.Period('2011-01-01', freq='D') for freq in [None, 'D']: idx = pd.DatetimeIndex(['2011-01-01', '2011-01-02'], freq=freq) with tm.assertRaises(TypeError): idx - p with tm.assertRaises(TypeError): p - idx def test_comp_nat(self): left = pd.DatetimeIndex([pd.Timestamp('2011-01-01'), pd.NaT, pd.Timestamp('2011-01-03')]) right = pd.DatetimeIndex([pd.NaT, pd.NaT, pd.Timestamp('2011-01-03')]) for l, r in [(left, right), (left.asobject, right.asobject)]: result = l == r expected = np.array([False, False, True]) tm.assert_numpy_array_equal(result, expected) result = l != r expected = np.array([True, True, False]) tm.assert_numpy_array_equal(result, expected) expected = np.array([False, False, False]) tm.assert_numpy_array_equal(l == pd.NaT, expected) tm.assert_numpy_array_equal(pd.NaT == r, expected) expected = np.array([True, True, True]) tm.assert_numpy_array_equal(l != pd.NaT, expected) tm.assert_numpy_array_equal(pd.NaT != l, expected) expected = np.array([False, False, False]) tm.assert_numpy_array_equal(l < pd.NaT, expected) tm.assert_numpy_array_equal(pd.NaT > l, expected) def test_value_counts_unique(self): # GH 7735 for tz in self.tz: idx = pd.date_range('2011-01-01 09:00', freq='H', periods=10) # create repeated values, 'n'th element is repeated by n+1 times idx = DatetimeIndex(np.repeat(idx.values, range(1, len(idx) + 1)), tz=tz) exp_idx = pd.date_range('2011-01-01 18:00', freq='-1H', periods=10, tz=tz) expected = Series(range(10, 0, -1), index=exp_idx, dtype='int64') for obj in [idx, Series(idx)]: tm.assert_series_equal(obj.value_counts(), expected) expected = pd.date_range('2011-01-01 09:00', freq='H', periods=10, tz=tz) tm.assert_index_equal(idx.unique(), expected) idx = DatetimeIndex(['2013-01-01 09:00', '2013-01-01 09:00', '2013-01-01 09:00', '2013-01-01 08:00', '2013-01-01 08:00', pd.NaT], tz=tz) exp_idx = DatetimeIndex(['2013-01-01 09:00', '2013-01-01 08:00'], tz=tz) expected = Series([3, 2], index=exp_idx) for obj in [idx, Series(idx)]: tm.assert_series_equal(obj.value_counts(), expected) exp_idx = DatetimeIndex(['2013-01-01 09:00', '2013-01-01 08:00', pd.NaT], tz=tz) expected = Series([3, 2, 1], index=exp_idx) for obj in [idx, Series(idx)]: tm.assert_series_equal(obj.value_counts(dropna=False), expected) tm.assert_index_equal(idx.unique(), exp_idx) def test_nonunique_contains(self): # GH 9512 for idx in map(DatetimeIndex, ([0, 1, 0], [0, 0, -1], [0, -1, -1], ['2015', '2015', '2016'], ['2015', '2015', '2014'])): tm.assertIn(idx[0], idx) def test_order(self): # with freq idx1 = DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03'], freq='D', name='idx') idx2 = DatetimeIndex(['2011-01-01 09:00', '2011-01-01 10:00', '2011-01-01 11:00'], freq='H', tz='Asia/Tokyo', name='tzidx') for idx in [idx1, idx2]: ordered = idx.sort_values() self.assert_index_equal(ordered, idx) self.assertEqual(ordered.freq, idx.freq) ordered = idx.sort_values(ascending=False) expected = idx[::-1] self.assert_index_equal(ordered, expected) self.assertEqual(ordered.freq, expected.freq) self.assertEqual(ordered.freq.n, -1) ordered, indexer = idx.sort_values(return_indexer=True) self.assert_index_equal(ordered, idx) self.assert_numpy_array_equal(indexer, np.array([0, 1, 2]), check_dtype=False) self.assertEqual(ordered.freq, idx.freq) ordered, indexer = idx.sort_values(return_indexer=True, ascending=False) expected = idx[::-1] self.assert_index_equal(ordered, expected) self.assert_numpy_array_equal(indexer, np.array([2, 1, 0]), check_dtype=False) self.assertEqual(ordered.freq, expected.freq) self.assertEqual(ordered.freq.n, -1) # without freq for tz in self.tz: idx1 = DatetimeIndex(['2011-01-01', '2011-01-03', '2011-01-05', '2011-01-02', '2011-01-01'], tz=tz, name='idx1') exp1 = DatetimeIndex(['2011-01-01', '2011-01-01', '2011-01-02', '2011-01-03', '2011-01-05'], tz=tz, name='idx1') idx2 = DatetimeIndex(['2011-01-01', '2011-01-03', '2011-01-05', '2011-01-02', '2011-01-01'], tz=tz, name='idx2') exp2 = DatetimeIndex(['2011-01-01', '2011-01-01', '2011-01-02', '2011-01-03', '2011-01-05'], tz=tz, name='idx2') idx3 = DatetimeIndex([pd.NaT, '2011-01-03', '2011-01-05', '2011-01-02', pd.NaT], tz=tz, name='idx3') exp3 = DatetimeIndex([pd.NaT, pd.NaT, '2011-01-02', '2011-01-03', '2011-01-05'], tz=tz, name='idx3') for idx, expected in [(idx1, exp1), (idx2, exp2), (idx3, exp3)]: ordered = idx.sort_values() self.assert_index_equal(ordered, expected) self.assertIsNone(ordered.freq) ordered = idx.sort_values(ascending=False) self.assert_index_equal(ordered, expected[::-1]) self.assertIsNone(ordered.freq) ordered, indexer = idx.sort_values(return_indexer=True) self.assert_index_equal(ordered, expected) exp = np.array([0, 4, 3, 1, 2]) self.assert_numpy_array_equal(indexer, exp, check_dtype=False) self.assertIsNone(ordered.freq) ordered, indexer = idx.sort_values(return_indexer=True, ascending=False) self.assert_index_equal(ordered, expected[::-1]) exp = np.array([2, 1, 3, 4, 0]) self.assert_numpy_array_equal(indexer, exp, check_dtype=False) self.assertIsNone(ordered.freq) def test_getitem(self): idx1 = pd.date_range('2011-01-01', '2011-01-31', freq='D', name='idx') idx2 = pd.date_range('2011-01-01', '2011-01-31', freq='D', tz='Asia/Tokyo', name='idx') for idx in [idx1, idx2]: result = idx[0] self.assertEqual(result, Timestamp('2011-01-01', tz=idx.tz)) result = idx[0:5] expected = pd.date_range('2011-01-01', '2011-01-05', freq='D', tz=idx.tz, name='idx') self.assert_index_equal(result, expected) self.assertEqual(result.freq, expected.freq) result = idx[0:10:2] expected = pd.date_range('2011-01-01', '2011-01-09', freq='2D', tz=idx.tz, name='idx') self.assert_index_equal(result, expected) self.assertEqual(result.freq, expected.freq) result = idx[-20:-5:3] expected = pd.date_range('2011-01-12', '2011-01-24', freq='3D', tz=idx.tz, name='idx') self.assert_index_equal(result, expected) self.assertEqual(result.freq, expected.freq) result = idx[4::-1] expected = DatetimeIndex(['2011-01-05', '2011-01-04', '2011-01-03', '2011-01-02', '2011-01-01'], freq='-1D', tz=idx.tz, name='idx') self.assert_index_equal(result, expected) self.assertEqual(result.freq, expected.freq) def test_drop_duplicates_metadata(self): # GH 10115 idx = pd.date_range('2011-01-01', '2011-01-31', freq='D', name='idx') result = idx.drop_duplicates() self.assert_index_equal(idx, result) self.assertEqual(idx.freq, result.freq) idx_dup = idx.append(idx) self.assertIsNone(idx_dup.freq) # freq is reset result = idx_dup.drop_duplicates() self.assert_index_equal(idx, result) self.assertIsNone(result.freq) def test_drop_duplicates(self): # to check Index/Series compat base = pd.date_range('2011-01-01', '2011-01-31', freq='D', name='idx') idx = base.append(base[:5]) res = idx.drop_duplicates() tm.assert_index_equal(res, base) res = Series(idx).drop_duplicates() tm.assert_series_equal(res, Series(base)) res = idx.drop_duplicates(keep='last') exp = base[5:].append(base[:5]) tm.assert_index_equal(res, exp) res = Series(idx).drop_duplicates(keep='last') tm.assert_series_equal(res, Series(exp, index=np.arange(5, 36))) res = idx.drop_duplicates(keep=False) tm.assert_index_equal(res, base[5:]) res = Series(idx).drop_duplicates(keep=False) tm.assert_series_equal(res, Series(base[5:], index=np.arange(5, 31))) def test_take(self): # GH 10295 idx1 = pd.date_range('2011-01-01', '2011-01-31', freq='D', name='idx') idx2 = pd.date_range('2011-01-01', '2011-01-31', freq='D', tz='Asia/Tokyo', name='idx') for idx in [idx1, idx2]: result = idx.take([0]) self.assertEqual(result, Timestamp('2011-01-01', tz=idx.tz)) result = idx.take([0, 1, 2]) expected = pd.date_range('2011-01-01', '2011-01-03', freq='D', tz=idx.tz, name='idx') self.assert_index_equal(result, expected) self.assertEqual(result.freq, expected.freq) result = idx.take([0, 2, 4]) expected = pd.date_range('2011-01-01', '2011-01-05', freq='2D', tz=idx.tz, name='idx') self.assert_index_equal(result, expected) self.assertEqual(result.freq, expected.freq) result = idx.take([7, 4, 1]) expected = pd.date_range('2011-01-08', '2011-01-02', freq='-3D', tz=idx.tz, name='idx') self.assert_index_equal(result, expected) self.assertEqual(result.freq, expected.freq) result = idx.take([3, 2, 5]) expected = DatetimeIndex(['2011-01-04', '2011-01-03', '2011-01-06'], freq=None, tz=idx.tz, name='idx') self.assert_index_equal(result, expected) self.assertIsNone(result.freq) result = idx.take([-3, 2, 5]) expected = DatetimeIndex(['2011-01-29', '2011-01-03', '2011-01-06'], freq=None, tz=idx.tz, name='idx') self.assert_index_equal(result, expected) self.assertIsNone(result.freq) def test_take_invalid_kwargs(self): idx = pd.date_range('2011-01-01', '2011-01-31', freq='D', name='idx') indices = [1, 6, 5, 9, 10, 13, 15, 3] msg = r"take\(\) got an unexpected keyword argument 'foo'" tm.assertRaisesRegexp(TypeError, msg, idx.take, indices, foo=2) msg = "the 'out' parameter is not supported" tm.assertRaisesRegexp(ValueError, msg, idx.take, indices, out=indices) msg = "the 'mode' parameter is not supported" tm.assertRaisesRegexp(ValueError, msg, idx.take, indices, mode='clip') def test_infer_freq(self): # GH 11018 for freq in ['A', '2A', '-2A', 'Q', '-1Q', 'M', '-1M', 'D', '3D', '-3D', 'W', '-1W', 'H', '2H', '-2H', 'T', '2T', 'S', '-3S']: idx = pd.date_range('2011-01-01 09:00:00', freq=freq, periods=10) result = pd.DatetimeIndex(idx.asi8, freq='infer') tm.assert_index_equal(idx, result) self.assertEqual(result.freq, freq) def test_nat_new(self): idx = pd.date_range('2011-01-01', freq='D', periods=5, name='x') result = idx._nat_new() exp = pd.DatetimeIndex([pd.NaT] * 5, name='x') tm.assert_index_equal(result, exp) result = idx._nat_new(box=False) exp = np.array([tslib.iNaT] * 5, dtype=np.int64) tm.assert_numpy_array_equal(result, exp) def test_shift(self): # GH 9903 for tz in self.tz: idx = pd.DatetimeIndex([], name='xxx', tz=tz) tm.assert_index_equal(idx.shift(0, freq='H'), idx) tm.assert_index_equal(idx.shift(3, freq='H'), idx) idx = pd.DatetimeIndex(['2011-01-01 10:00', '2011-01-01 11:00' '2011-01-01 12:00'], name='xxx', tz=tz) tm.assert_index_equal(idx.shift(0, freq='H'), idx) exp = pd.DatetimeIndex(['2011-01-01 13:00', '2011-01-01 14:00' '2011-01-01 15:00'], name='xxx', tz=tz) tm.assert_index_equal(idx.shift(3, freq='H'), exp) exp = pd.DatetimeIndex(['2011-01-01 07:00', '2011-01-01 08:00' '2011-01-01 09:00'], name='xxx', tz=tz) tm.assert_index_equal(idx.shift(-3, freq='H'), exp) def test_nat(self): self.assertIs(pd.DatetimeIndex._na_value, pd.NaT) self.assertIs(
pd.DatetimeIndex([])
pandas.DatetimeIndex
import matplotlib.pylab as plt import networkx as nx import numpy as np import pandas as pd from sklearn.metrics.pairwise import distance_metrics def handle_2d_plot( embedding, kind, color=None, xlabel=None, ylabel=None, show_operations=False, annot=False, ): """ Handles the logic to perform a 2d plot in matplotlib. **Input** - embedding.md: a `whatlies.Embedding` object to plot - kind: what kind of plot to make, can be `scatter`, `arrow` or `text` - color: the color to apply, only works for `scatter` and `arrow` - xlabel: manually override the xlabel - ylabel: manually override the ylabel - show_operations: setting to also show the applied operations, only works for `text` """ name = embedding.name if show_operations else embedding.orig if kind == "scatter": if color is None: color = "steelblue" plt.scatter([embedding.vector[0]], [embedding.vector[1]], c=color) if kind == "arrow": plt.scatter([embedding.vector[0]], [embedding.vector[1]], c="white", s=0.01) plt.quiver( [0], [0], [embedding.vector[0]], [embedding.vector[1]], color=color, angles="xy", scale_units="xy", scale=1, ) plt.text(embedding.vector[0] + 0.01, embedding.vector[1], name) if (kind == "text") or annot: plt.text(embedding.vector[0] + 0.01, embedding.vector[1], name) plt.xlabel("x" if not xlabel else xlabel) plt.ylabel("y" if not ylabel else ylabel) def plot_graph_layout(embedding_set, kind="cosine", **kwargs): """ Handles the plotting of a layout graph using the embeddings in an embeddingset as input. **Input** - embeddings: a set of `whatlies.Embedding` objects to plot - kind: distance metric options: 'cityblock', 'cosine', 'euclidean', 'l2', 'l1', 'manhattan', """ vectors = [token.vector for k, token in embedding_set.items()] label_dict = {i: w for i, (w, _) in enumerate(embedding_set.items())} dist_fnc = distance_metrics()[kind] dist = dist_fnc(np.array(vectors), np.array(vectors)) # Greate graph graph = nx.from_numpy_matrix(dist) distance =
pd.DataFrame(dist)
pandas.DataFrame
''' Step 2 This file reads over all the scrapped airfoil data in the "scrape" folder and runs xfoil creates a file called This file needs to be run in WSL for it to work ''' import glob from pandas.core.reshape.concat import concat from libs.utils import pchip import os, copy import os.path as osp from tqdm import trange import pandas as pd import logging import numpy as np import json import time from libs.xfoil_lib import read_polar from libs.xfoil import xfdata, run_xfoil def coordinate_read(coord_file,npoints=100): ''' Reads the coordinates of the airfoil @params coord_file - file to read @params npoints - number of points used to represent suction and pressure side, 0 - use what's in the file ''' x = list(); y = list() airfoil_name = osp.basename(coord_file).replace('.txt', ' ').strip() with open(coord_file,'r') as fp: for cnt, line in enumerate(fp): if cnt>2: data = line.strip().split(' ') data = [i for i in data if i] if (len(data)==2): x.append(float(data[0])) y.append(float(data[1])) else: xss = copy.deepcopy(x); yss = copy.deepcopy(y) x.clear() y.clear() xps = copy.deepcopy(x) yps = copy.deepcopy(y) xps2 = list() yps2 = list() xss2 = list() yss2 = list() for i in range(len(xps)): # This part makes sure x is strictly increasing sequence for Pressure side if (xps[i] not in xps2): if i>0 and xps[i]>xps2[-1]: xps2.append(xps[i]) yps2.append(yps[i]) elif i==0: xps2.append(xps[i]) yps2.append(yps[i]) for i in range(len(xss)): # This part makes sure x is strictly increasing sequence for Suction side if (xss[i] not in xss2): if i>0 and xss[i]>xss2[-1]: yss2.append(yss[i]) xss2.append(xss[i]) elif i==0: yss2.append(yss[i]) xss2.append(xss[i]) xss = xss2; yss=yss2; xps=xps2; yps=yps2 if npoints>0: x = np.linspace(min(xss),max(xss),npoints) yss = pchip(xss,yss,x) xss = x yps = pchip(xps,yps,x) xps = x # yss[-1] = yps[-1] return xss,yss,xps,yps,airfoil_name def process_scaped_data(npoints=100,check_convergence=True,save_dir:str='json'): """Reads the scraped airfoil .txt file, runs xfoil, save to json This is an important step in the data processing. This step normalizes the data so that for each node on suction (ss) and pressure (ps) side you have a Cp value Args: npoints (int, optional): number of points on pressure and suction size. You have to normalize the data to a certain number of points. Defaults to 90. check_convergence (bool, optional): Checks for convergence when running xfoil. Defaults to True. Returns: None: Saves the data to json """ unprocessed_filename = 'data/scraped_airfoils_no_cp.gz' # * Step 1 Read through the scape data if not osp.exists(unprocessed_filename): files = glob.glob('data/scrape/*.txt') files.sort() airfoil_files = [f for f in files if 'polar' not in f] airfoil_data = list() for f in trange(len(airfoil_files),desc='Reading Scraped Airfoils'): xss,yss,xps,yps,airfoil_name = coordinate_read(airfoil_files[f]) # search for all the polars of that airfoil polar_list = list() polar_files = glob.glob('data/scrape/{0}_polar*'.format(airfoil_name)) for p in range(len(polar_files)): polar_df = read_polar(polar_files[p]) polar_df['Cp_ss'] = None # Add a column for Cp (will be calculated later) polar_df['Cp_ps'] = None # Add a column for Cp (will be calculated later) polar_list.append(polar_df) polar_df = pd.concat(polar_list) temp = {'name':airfoil_name,'xss':xss,'yss':yss,'xps':xps,'yps':yps,'polars':polar_df} airfoil_data.append(temp) df =
pd.DataFrame(airfoil_data)
pandas.DataFrame
# -*- coding: utf-8 -*- """ Created on Wed Nov 30 17:33:29 2016 @author: betienne """ import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.utils.extmath import randomized_svd class MCA(object): def __init__(self, X, ind=None, supp=None, n_components=None): """ Starts a MCA Params: X : array of size (n,m), typically a pandas dataframe ncols : number of categorical variables n_components : number of axis to keep (non-zero eigenvalues) If None, it is equal to min(n-1,m-ncol) supp : supplementary variables, will not be taken into the computation but can be represented. """ self.ncols = X.shape[1] if supp: self.X_supp = pd.get_dummies(X[supp]) self.columns_supp = list(self.X_supp.columns) self.X_supp = self.X_supp.values self.m_supp = self.X_supp.shape[1] self.supp = True if supp else False if ind: X = pd.get_dummies(X[ind]) else: X = pd.get_dummies(X) self.index = list(X.index) self.columns = list(X.columns) self.K = np.matrix(X.values, dtype=np.int8) del X self.n = self.K.shape[0] self.m = self.K.shape[1] self.eff_tot = self.K.sum() self.n_components = n_components if n_components else min(self.n - 1,self.m - self.ncols) print("Individus : ",self.n) print("Variables : ",self.ncols) print(" - Actives : ", len(ind) if ind else self.ncols) print(" - Supp. : ", len(supp) if supp else 0) print("Modalités : ", self.m + (self.m_supp if supp else 0)) print(" - Actives : ", self.m) print(" - Supp. : ", self.m_supp if supp else 0) print("Nombre d'axes : ",self.n_components) # Inviduals profile weight vector self.r = np.array(np.ones([self.n]) / self.n) # Variables profile weight vector self.c = np.array(self.K.sum(axis=0)).flatten() / (self.n* self.ncols) # Frequency Matrix - centered self.F = self.K / self.eff_tot - np.outer(self.r, self.c) del self.K del self.r # Dc Matrix self.D_c = np.diag(1. / np.sqrt(self.c)) # We invert D_c straightaway. # Compute R = UΛVt self.R = np.dot(self.F, self.D_c)*(np.sqrt(self.n)) # Compute SVD self.U, self.sigma, self.V = randomized_svd(self.R, n_components=self.n_components, n_iter=5,random_state=None) # Eigen values self.eigenvals = self.sigma**2 def row_profile_matrix(self): """ Returns row profile matrix. Warning : for n very large, can cause MemoryError Deactivated for the moment. """ pass # return(np.linalg.solve(self.D_r, self.F)) def column_profile_matrix(self): """ Returns column profile matrix """ return(np.linalg.solve(self.D_c, self.F)) def individuals_coords(self): """ Returns individuals' coords on axis """ return(np.sqrt(self.n) * np.dot(self.U, np.diag(self.sigma))) def modalities_coords(self): """ Returns modalities' coords on axis """ y1 = np.dot(np.dot(self.D_c,self.V.T), np.diag(self.sigma)) if self.supp: y2 = self.supplement().T return(np.vstack((y1, y2))) else: return(y1) def results(self, benzecri=False): """ Prints the results with eigenvalues and cumulative variance Benzecri correction can also be used Params: benzecri : boolean. If True, performs benzecri correction. """ p = self.ncols if benzecri: eigen = np.where(self.eigenvals > 1./p,((p/(p-1))*(self.eigenvals-(1/p)))**2,0) else: eigen = self.eigenvals s = eigen.sum() c = 0 print() print("Correction : {}\n".format("Benzecri" if benzecri else "None")) print("{:>18}{:>15}{:>16}".format("eigenvalue","explained var","cumulative var")) print("{:>18}{:>15}{:>16}".format("----------","-------------","--------------")) for i, k in enumerate(eigen): c += k/s*100 print("#{:>5}{:12.6f}{:15.5f}{:16.3f}".format("dim"+str(i+1),k,k/s*100,c)) def coordinates(self, option=None): """ Returns the coordinates of individuals and modalities on the axis Params: ndim : number of axis Output: x : individuals coords y : modalities coords """ if option: choix = str(option) else: print("MCA Options") print("----------------") print("Option 1 : Individuals at barycenter of modalities") print("Option 2 : Modalities at barycenter of individuals") print("Option 3 : Simultaneous\n") choix = input("Choix : ") ndim = self.n_components+1 if choix == "1": x = self.individuals_coords() y = np.dot(self.modalities_coords(), np.diag(1./self.sigma)) elif choix == "2": x = np.dot(self.individuals_coords(), np.diag(1./self.sigma)) y = self.modalities_coords() elif choix == "3": x = self.individuals_coords() y = self.modalities_coords() else: raise ValueError("Le choix doit être 1, 2, 3") return(x[:,:ndim], y[:,:ndim]) def contributions(self): """ Returns the contribution of individuals i and modalities s to the axes. """ x, y = self.coordinates(3) y = y[:self.m,:] i = (x**2) / (self.eigenvals * self.n) * 100 s = (y**2) * self.c[:, np.newaxis] / self.eigenvals * 100 return(i, s) def plot(self, supp=False): """ Projects on a 2-D graph, with or without supplementary variables """ if supp and not self.supp: print("No supplementary variables in chosen configuration") else: x, y = self.coordinates(3) y, y_supp = y[:self.m, :], y[self.m:, :] if x.shape[0] > 100: print("WARNING : Size of individuals is above 100. The plot will be too heavy.\n Exiting") else: plt.figure(figsize=(11,8)) plt.scatter(x[:,0] ,x[:,1], marker='^', color='red', label="individus") plt.scatter(y[:,0], y[:,1], marker='s', color='blue', label="mod_actives") if supp: plt.scatter(y_supp[:,0], y_supp[:,1], marker='o', color='green', label="mod_supp") plt.legend(loc="best") plt.axhline(0, linestyle="--", color='k') plt.axvline(0, linestyle="--", color='k') plt.ylabel("Dim 2 ({:.2f}%)".format(self.eigenvals[1]*100)) plt.xlabel("Dim 1 ({:.2f}%)".format(self.eigenvals[0]*100)) for i,j in enumerate(self.index): plt.annotate(j, (x[i,0], x[i,1])) for i,j in enumerate(self.columns): plt.annotate(j, (y[i,0], y[i,1])) if supp: for i,j in enumerate(self.columns_supp): plt.annotate(j, (y_supp[i,0], y_supp[i,1])) def supplement(self): """ Projects the supp variables on the factorial axis as barycenter of the individuals to which they belong """ # x_supp = np.zeros((self.n_components,self.X_supp.shape[1])) # for c in range(self.X_supp.shape[1]): # x_supp[:,c] = np.dot(self.individuals_coords().T,self.X_supp[:,c]) / np.sum(self.X_supp[:,c]) # return(x_supp) return(np.dot(self.individuals_coords().T,self.X_supp) / np.sum(self.X_supp, axis=0)) if __name__ == '__main__': df =
pd.read_excel('../chiens.xlsx')
pandas.read_excel
import pytest import numpy as np import pandas import pandas.util.testing as tm from pandas.tests.frame.common import TestData import matplotlib import modin.pandas as pd from modin.pandas.utils import to_pandas from numpy.testing import assert_array_equal from .utils import ( random_state, RAND_LOW, RAND_HIGH, df_equals, df_is_empty, arg_keys, name_contains, test_data_values, test_data_keys, test_data_with_duplicates_values, test_data_with_duplicates_keys, numeric_dfs, no_numeric_dfs, test_func_keys, test_func_values, query_func_keys, query_func_values, agg_func_keys, agg_func_values, numeric_agg_funcs, quantiles_keys, quantiles_values, indices_keys, indices_values, axis_keys, axis_values, bool_arg_keys, bool_arg_values, int_arg_keys, int_arg_values, ) # TODO remove once modin-project/modin#469 is resolved agg_func_keys.remove("str") agg_func_values.remove(str) pd.DEFAULT_NPARTITIONS = 4 # Force matplotlib to not use any Xwindows backend. matplotlib.use("Agg") class TestDFPartOne: # Test inter df math functions def inter_df_math_helper(self, modin_df, pandas_df, op): # Test dataframe to datframe try: pandas_result = getattr(pandas_df, op)(pandas_df) except Exception as e: with pytest.raises(type(e)): getattr(modin_df, op)(modin_df) else: modin_result = getattr(modin_df, op)(modin_df) df_equals(modin_result, pandas_result) # Test dataframe to int try: pandas_result = getattr(pandas_df, op)(4) except Exception as e: with pytest.raises(type(e)): getattr(modin_df, op)(4) else: modin_result = getattr(modin_df, op)(4) df_equals(modin_result, pandas_result) # Test dataframe to float try: pandas_result = getattr(pandas_df, op)(4.0) except Exception as e: with pytest.raises(type(e)): getattr(modin_df, op)(4.0) else: modin_result = getattr(modin_df, op)(4.0) df_equals(modin_result, pandas_result) # Test transposed dataframes to float try: pandas_result = getattr(pandas_df.T, op)(4.0) except Exception as e: with pytest.raises(type(e)): getattr(modin_df.T, op)(4.0) else: modin_result = getattr(modin_df.T, op)(4.0) df_equals(modin_result, pandas_result) frame_data = { "{}_other".format(modin_df.columns[0]): [0, 2], modin_df.columns[0]: [0, 19], modin_df.columns[1]: [1, 1], } modin_df2 = pd.DataFrame(frame_data) pandas_df2 = pandas.DataFrame(frame_data) # Test dataframe to different dataframe shape try: pandas_result = getattr(pandas_df, op)(pandas_df2) except Exception as e: with pytest.raises(type(e)): getattr(modin_df, op)(modin_df2) else: modin_result = getattr(modin_df, op)(modin_df2) df_equals(modin_result, pandas_result) # Test dataframe to list list_test = random_state.randint(RAND_LOW, RAND_HIGH, size=(modin_df.shape[1])) try: pandas_result = getattr(pandas_df, op)(list_test, axis=1) except Exception as e: with pytest.raises(type(e)): getattr(modin_df, op)(list_test, axis=1) else: modin_result = getattr(modin_df, op)(list_test, axis=1) df_equals(modin_result, pandas_result) # Test dataframe to series series_test_modin = modin_df[modin_df.columns[0]] series_test_pandas = pandas_df[pandas_df.columns[0]] try: pandas_result = getattr(pandas_df, op)(series_test_pandas, axis=0) except Exception as e: with pytest.raises(type(e)): getattr(modin_df, op)(series_test_modin, axis=0) else: modin_result = getattr(modin_df, op)(series_test_modin, axis=0) df_equals(modin_result, pandas_result) # Test dataframe to series with different index series_test_modin = modin_df[modin_df.columns[0]].reset_index(drop=True) series_test_pandas = pandas_df[pandas_df.columns[0]].reset_index(drop=True) try: pandas_result = getattr(pandas_df, op)(series_test_pandas, axis=0) except Exception as e: with pytest.raises(type(e)): getattr(modin_df, op)(series_test_modin, axis=0) else: modin_result = getattr(modin_df, op)(series_test_modin, axis=0) df_equals(modin_result, pandas_result) # Level test new_idx = pandas.MultiIndex.from_tuples( [(i // 4, i // 2, i) for i in modin_df.index] ) modin_df_multi_level = modin_df.copy() modin_df_multi_level.index = new_idx # Defaults to pandas with pytest.warns(UserWarning): # Operation against self for sanity check getattr(modin_df_multi_level, op)(modin_df_multi_level, axis=0, level=1) @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_add(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "add") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_div(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "div") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_divide(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "divide") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_floordiv(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "floordiv") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_mod(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "mod") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_mul(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "mul") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_multiply(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "multiply") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_pow(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) # TODO: Revert to others once we have an efficient way of preprocessing for positive # values try: pandas_df = pandas_df.abs() except Exception: pass else: modin_df = modin_df.abs() self.inter_df_math_helper(modin_df, pandas_df, "pow") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_sub(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "sub") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_subtract(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "subtract") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_truediv(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "truediv") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test___div__(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "__div__") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test___add__(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "__add__") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test___radd__(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "__radd__") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test___mul__(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "__mul__") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test___rmul__(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "__rmul__") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test___pow__(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "__pow__") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test___rpow__(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "__rpow__") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test___sub__(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "__sub__") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test___floordiv__(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "__floordiv__") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test___rfloordiv__(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "__rfloordiv__") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test___truediv__(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "__truediv__") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test___rtruediv__(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "__rtruediv__") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test___mod__(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "__mod__") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test___rmod__(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "__rmod__") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test___rdiv__(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_helper(modin_df, pandas_df, "__rdiv__") # END test inter df math functions # Test comparison of inter operation functions def comparison_inter_ops_helper(self, modin_df, pandas_df, op): try: pandas_result = getattr(pandas_df, op)(pandas_df) except Exception as e: with pytest.raises(type(e)): getattr(modin_df, op)(modin_df) else: modin_result = getattr(modin_df, op)(modin_df) df_equals(modin_result, pandas_result) try: pandas_result = getattr(pandas_df, op)(4) except TypeError: with pytest.raises(TypeError): getattr(modin_df, op)(4) else: modin_result = getattr(modin_df, op)(4) df_equals(modin_result, pandas_result) try: pandas_result = getattr(pandas_df, op)(4.0) except TypeError: with pytest.raises(TypeError): getattr(modin_df, op)(4.0) else: modin_result = getattr(modin_df, op)(4.0) df_equals(modin_result, pandas_result) try: pandas_result = getattr(pandas_df, op)("a") except TypeError: with pytest.raises(TypeError): repr(getattr(modin_df, op)("a")) else: modin_result = getattr(modin_df, op)("a") df_equals(modin_result, pandas_result) frame_data = { "{}_other".format(modin_df.columns[0]): [0, 2], modin_df.columns[0]: [0, 19], modin_df.columns[1]: [1, 1], } modin_df2 = pd.DataFrame(frame_data) pandas_df2 = pandas.DataFrame(frame_data) try: pandas_result = getattr(pandas_df, op)(pandas_df2) except Exception as e: with pytest.raises(type(e)): getattr(modin_df, op)(modin_df2) else: modin_result = getattr(modin_df, op)(modin_df2) df_equals(modin_result, pandas_result) new_idx = pandas.MultiIndex.from_tuples( [(i // 4, i // 2, i) for i in modin_df.index] ) modin_df_multi_level = modin_df.copy() modin_df_multi_level.index = new_idx # Defaults to pandas with pytest.warns(UserWarning): # Operation against self for sanity check getattr(modin_df_multi_level, op)(modin_df_multi_level, axis=0, level=1) @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_eq(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.comparison_inter_ops_helper(modin_df, pandas_df, "eq") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_ge(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.comparison_inter_ops_helper(modin_df, pandas_df, "ge") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_gt(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.comparison_inter_ops_helper(modin_df, pandas_df, "gt") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_le(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.comparison_inter_ops_helper(modin_df, pandas_df, "le") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_lt(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.comparison_inter_ops_helper(modin_df, pandas_df, "lt") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_ne(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.comparison_inter_ops_helper(modin_df, pandas_df, "ne") # END test comparison of inter operation functions # Test dataframe right operations def inter_df_math_right_ops_helper(self, modin_df, pandas_df, op): try: pandas_result = getattr(pandas_df, op)(4) except Exception as e: with pytest.raises(type(e)): getattr(modin_df, op)(4) else: modin_result = getattr(modin_df, op)(4) df_equals(modin_result, pandas_result) try: pandas_result = getattr(pandas_df, op)(4.0) except Exception as e: with pytest.raises(type(e)): getattr(modin_df, op)(4.0) else: modin_result = getattr(modin_df, op)(4.0) df_equals(modin_result, pandas_result) new_idx = pandas.MultiIndex.from_tuples( [(i // 4, i // 2, i) for i in modin_df.index] ) modin_df_multi_level = modin_df.copy() modin_df_multi_level.index = new_idx # Defaults to pandas with pytest.warns(UserWarning): # Operation against self for sanity check getattr(modin_df_multi_level, op)(modin_df_multi_level, axis=0, level=1) @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_radd(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_right_ops_helper(modin_df, pandas_df, "radd") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_rdiv(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_right_ops_helper(modin_df, pandas_df, "rdiv") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_rfloordiv(self, data): modin_df = pd.DataFrame(data) pandas_df = pandas.DataFrame(data) self.inter_df_math_right_ops_helper(modin_df, pandas_df, "rfloordiv") @pytest.mark.parametrize("data", test_data_values, ids=test_data_keys) def test_rmod(self, data): modin_df = pd.DataFrame(data) pandas_df =
pandas.DataFrame(data)
pandas.DataFrame
import numpy as np import tensorflow as tf import pickle import sys import pandas as pd def predictWithTrainedModels(fileName): with open('../tokenizer/tokenizer.pickle', 'rb') as handle: tokenizer = pickle.load(handle) df_temp =
pd.DataFrame({'source_code':[]})
pandas.DataFrame
""" ______ _ _ _ _ _ _ | ___ \ | | | | | | | (_) | | |_/ /___ ___ ___ _ __ ___ _ __ ___ ___ _ __ __| | ___ _ __ | | | | |_ _| |___ | // _ \/ __/ _ \| '_ ` _ \| '_ ` _ \ / _ \ '_ \ / _` |/ _ \ '__| | | | | __| | / __| | |\ \ __/ (_| (_) | | | | | | | | | | | __/ | | | (_| | __/ | | |_| | |_| | \__ \ \_| \_\___|\___\___/|_| |_| |_|_| |_| |_|\___|_| |_|\__,_|\___|_| \___/ \__|_|_|___/ A collection of helper functions intended for use with the LightFM hybrid recommender model. Version 1.1.3 """ # import modules import os import pandas as pd import pickle import numpy as np import matplotlib.pyplot as plt from matplotlib.lines import Line2D import seaborn as sns from itertools import chain from lightfm.evaluation import auc_score, precision_at_k, recall_at_k, reciprocal_rank from lightfm.data import Dataset from scipy import sparse, stats from sklearn.feature_extraction.text import CountVectorizer def get_similar_tags(model, tag_id): tag_embeddings = (model.item_embeddings.T / np.linalg.norm(model.item_embeddings, axis=1)).T query_embedding = tag_embeddings[tag_id] similarity = np.dot(tag_embeddings, query_embedding) most_similar = np.argsort(-similarity)[1:4] return most_similar def similar_items(item_id, item_features, model, N=10): """ Function to determine most similar items based on learned item embeddings within recommender model :param item_id: Item id for the item you want to compare to other items. This is the internal model id which you need to extract first :param item_features: Sparse matrix of time features used in the model :param model: The trained LightFM recommender model :param N: Number of items to return :return: List of tuples of N most similar items by internal item id and the similarity score """ (item_biased, item_representations) = model.get_item_representations(features=item_features) # Cosine similarity scores = item_representations.dot(item_representations[item_id]) item_norms = np.linalg.norm(item_representations, axis=1) item_norms[item_norms == 0] = 1e-10 scores /= item_norms best = np.argpartition(scores, -N)[-N:] return sorted(zip(best, scores[best] / item_norms[item_id]), key=lambda x: -x[1]) def convertlist(longlist): """ Function to convert list of lists to a single numpy array. Works on an iterable to be more efficient than working directly with lists :param longlist: List of lists :return: Numpy array with elements from the lists """ tmp = list(chain.from_iterable(longlist)) return np.array(tmp).reshape((len(longlist), len(longlist[0]))) def prepare_interactions_dataframe(df, user_id_col, item_id_col, weight_col=None, interaction_weights=False, item_identity_features=False, user_identity_features=False): """ Function to prepare input dataframe containing user-item interactions for use within LightFM model :param df: Dataframe with user-item interactions :param user_id_col: Name of column containing the user ids :param item_id_col: Name of column containing the item ids :param weight_col: Name of column containing user-item weights. For example, the rating that a user gives to an item :param interaction_weights: Boolean indicating whether or not to use explicit users weights. There are instances where this may not be desirable :param item_identity_features: Boolean to indicate whether or not to append an identify matrix to the item features matrix on top of existing item features. True lets you fall back on collaborative filtering :param user_identity_features: Boolean to indicate whether or not to append an identify matrix to the user features matrix on top of existing user features. True lets you fall back on collaborative filtering :return: LightFM dataset object, interactions matrix (scipy sparse coo matrix), matrix of weights (scipy sparse coo matrix) containing interaction weights (identity matrix if no interaction weights are desired) """ # TODO: Should I set the default for identity features as True? # Initialize the dataset. Identity features are off by default here dataset = Dataset(item_identity_features=item_identity_features, user_identity_features=user_identity_features) # fit the primary interactions dataset.fit(users=df[user_id_col], items=df[item_id_col]) # Characterize the size of the interactions num_users, num_items = dataset.interactions_shape() print('Num users: {}, num_items {}'.format(num_users, num_items)) # Build the interactions and weights matrices. Weights will be optional if interaction_weights: (interactions, weights) = dataset.build_interactions(list(zip(df[user_id_col], df[item_id_col], df[weight_col].astype(int)))) else: (interactions, weights) = dataset.build_interactions(list(zip(df[user_id_col], df[item_id_col]))) return dataset, interactions, weights def prepare_item_features(dataset, item_features_df, item_id_col, feature_col, features_list): """ Function to prepare item features for use with LightFM model from a dataframe :param dataset: LightFM dataset object returned by prepare_interactions_dataframe function :param item_features_df: Dataframe containing the item features. One item per row :param item_id_col: Name of the column containing the item ids :param feature_col: Name of the column containing the item features. Expected to be a list of features, or a list containing a dictionary of the form {feature name: weight} Otherwise, the default will be a hot encoding based on provided feature names :param features_list: List of unique feature names :return: Scipy sparse matrix of items and item features. Entries will either be a hot encoding if a list of feature names per item is passed, or explicit weights if a dictionary of feature names to weights per item is provided """ # TODO: Add support for arbitrary feature weights. This should work already as long as the feature_col is formatted like {feature name: weight} # Build (item id, list of features for that item) tuple. feature_tuple = tuple(zip(item_features_df[item_id_col], item_features_df[feature_col])) # perform a partial fit of the item ids and item features dataset.fit_partial(items=item_features_df[item_id_col], item_features=features_list) # Add item features to the dataset already initialized. Must run prepare_interactions_dataframe function first. item_features = dataset.build_item_features(feature_tuple) return item_features def prepare_user_features(dataset, user_features_df, user_id_col, feature_col, features_list): """ Function to prepare user features for use with LightFM model :param dataset: LightFM dataset object returned by prepare_interactions_dataframe function :param user_features_df: Dataframe containing the user features. One user per row :param user_id_col: Name of the column containing the user ids :param feature_col: Name of the column containing the user features. Expected to be a list of features, or a list containing a dictionary of the form {feature name: weight}. Otherwise, the default will be a hot encoding based on provided feature names :param features_list: List of unique feature names :return: Scipy sparse matrix of users and user features. Entries will either be a hot encoding if a list of feature names per user is passed, or explicit weights if a dictionary of feature names to weights per user is provided """ # TODO: Add support for arbitrary feature weights. This should work already as long as the feature_col is formatted like {feature name: weight} # Build (user id, list of features for that item) tuple. feature_tuple = tuple(zip(user_features_df[user_id_col], user_features_df[feature_col])) # perform a partial fit of the item ids and item features dataset.fit_partial(users=user_features_df[user_id_col], user_features=features_list) # Add user features to the dataset already initialized. Must run prepare_interactions_dataframe function first. user_features = dataset.build_user_features(feature_tuple) return user_features def make_feature_lists(df, feature_list_cols): """ Function to extract unique features :param df: :param feature_list_cols: :return: """ # TODO: decide if we actually need this here. Might be something that needs to be custom to the problem at hand # Might move this to another module as this is a bit custom to the problem # Will need to handle numeric, categorical, and text data pass def build_item_column(df, feature_cols): """ Function to build the list of features for each item or user :param df: :param feature_cols: :return: """ # Might put this in another module as this is a bit custom to the problem # Will need to return a list if just feature names, or a list with a dictionary with feature name: weight if custom weights are required. pass def prepare_test_interactions(dataset, df, user_id_col, item_id_col, weight_col=None, interaction_weights=False): """ Function to prepare a test set for model evaluation. Should only be run with users and items that are already in the training set, but previously unseen interactions. :param dataset: LightFM dataset object returned by prepare_interactions_dataframe function :param df: Dataframe with user-item interactions :param user_id_col: Name of column containing the user ids :param item_id_col: Name of column containing the item ids :param weight_col: Name of column containing user-item weights. For example, the rating that a user gives to an item :param interaction_weights: Boolean indicating whether or not to use explicit users weights. :return: interactions matrix (scipy sparse coo matrix), matrix of weights (scipy sparse coo matrix) containing interaction weights (identity matrix if no interaction weights are desired) """ # TODO: Check if we actually need to return the interaction weights here. Not really all that relevant for testing purposes. # Build the interactions and weights matrices. Weights will be optional if interaction_weights: (interactions, weights) = dataset.build_interactions(list(zip(df[user_id_col], df[item_id_col], df[weight_col].astype(int)))) else: (interactions, weights) = dataset.build_interactions(list(zip(df[user_id_col], df[item_id_col]))) return interactions, weights def make_cold_start_data(df, user_id_col, item_id_col, rating_col, shape, types): """ A function to convert cold start data to the correct format for usage with LightFM model :param df: Dataframe with user-item interactions. Can contain new users not previously seen by model :param user_id_col: Name of column containing the user ids :param item_id_col: Name of column containing the item ids :param rating_col: Name of column containing user-item ratings :param shape: Shape of interaction matrix to return. Needs to be the same shape as the interactions matrix returned by prepare_interactions_dataframe function :param types: Types for the entries in the columns. Needs to be the same as the data types in the interactions matrix returned by prepare_interactions_dataframe function :return: Interaction matrix from cold start user-item dataframe (scipy sparse coo matrix) """ # TODO: check if this is really needed. We could probably just use the built in train-test split function from LightFM to achieve this. df_pivot = df.pivot(index=user_id_col, columns=item_id_col, values=rating_col) df_pivot.fillna(0, inplace=True) df_pivot = df_pivot.astype(np.int32) interactions = sparse.csr_matrix(df_pivot.values) interactions = interactions.tocoo() uids, iids, data = (interactions.row, interactions.col, interactions.data) interactions = sparse.coo_matrix((data, (uids, iids)), shape=shape, dtype=types) return interactions def prepare_item_features_from_dict(dataset, item_features_dict, features_list): """ Function to prepare item features for use with LightFM model from a dictionary :param dataset: LightFM dataset object returned by prepare_interactions_dataframe function :param item_features_dict: Dictionary with keys being the item ids and values the features. Features should be either a list of feature names (this defaults the model to hot encoding) or a dictionary {feature name: feature weight}. :param features_list: List of individual feature names :return: Scipy sparse matrix of items and item features. Entries will either be a hot encoding if a list of feature names per item is passed, or explicit weights if a dictionary of feature names to weights per item is provided """ # Build (item id, list of features for that item) tuple. feature_tuple = tuple(zip(item_features_dict.keys(), item_features_dict.values())) # perform a partial fit of the item ids and item features dataset.fit_partial(items=item_features_dict.keys(), item_features=features_list) # Add item features to the dataset already initialized. Must run prepare_interactions_dataframe function first. try: item_features = dataset.build_item_features(feature_tuple) except ValueError as e: if str(e) != 'Cannot normalize feature matrix: some rows have zero norm.': raise else: print('Cannot normalize feature matrix: some rows have zero norm.') print('Defaulting to non-normalized features, but best to check feature matrix.') item_features = dataset.build_item_features(feature_tuple, normalize=False) return item_features def prepare_user_features_from_dict(dataset, user_features_dict, features_list): """ Function to prepare user features for use with LightFM model from a dictionary :param dataset: LightFM dataset object returned by prepare_interactions_dataframe function :param user_features_dict: Dictionary with keys being the user ids and values the features. Features should be either a list of feature names (this defaults the model to hot encoding) or a dictionary {feature name: feature weight}. :param features_list: List of individual feature names :return: Scipy sparse matrix of users and user features. Entries will either be a hot encoding if a list of feature names per user is passed, or explicit weights if a dictionary of feature names to weights per user is provided """ # Build (item id, list of features for that item) tuple. feature_tuple = tuple(zip(user_features_dict.keys(), user_features_dict.values())) # perform a partial fit of the item ids and item features dataset.fit_partial(users=user_features_dict.keys(), user_features=features_list) # Add user features to the dataset already initialized. Must run prepare_interactions_dataframe function first. try: user_features = dataset.build_user_features(feature_tuple) except ValueError as e: if str(e) != 'Cannot normalize feature matrix: some rows have zero norm.': raise else: print('Cannot normalize feature matrix: some rows have zero norm.') print('Defaulting to non-normalized features, but best to check feature matrix.') user_features = dataset.build_user_features(feature_tuple, normalize=False) return user_features def compute_eval_metrics(model, train_interactions, test_interactions, item_features=None, user_features=None, tuple_of_k=(10, 20, 50), compute_on_train=False, preserve_rows=False, check_intersections=False, num_threads=1): """ Function to compute standard evaluation metrics on given test set and model :param model: The model to test :param train_interactions: The train set of user/item interactions (sparse matrix). This is used to remove training examples from the test set score calculations which could bias results :param test_interactions: The test set of user/item interactions (sparse matrix) :param item_features: The sparse matrix of features for the items :param user_features: The sparse matrix of features for the users :param tuple_of_k: A tuple containing the values of k you want to consider for the precision at k and recall at k metrics :param compute_on_train: Set true if you want to see the metrics computed on the training set as well :param preserve_rows: Preserve rows parameter in the LightFM provided metrics: When False (default), the number of rows in the output will be equal to the number of users with interactions in the evaluation set. When True, the number of rows in the output will be equal to the number of users :param check_intersections: Check for intersecting train and test interactions :param num_threads: The number of threads to use :return: dictionary with the metrics as keys and the list of results as values """ # Initialize a dictionary to store results all_results_dict = {} # Compute the test_auc: print('Computing test set AUCs') test_auc_list = auc_score(model=model, train_interactions=train_interactions, test_interactions=test_interactions, item_features=item_features, user_features=user_features, preserve_rows=preserve_rows, check_intersections=check_intersections, num_threads=num_threads) all_results_dict['test_auc_list'] = test_auc_list # Compute test recall at all input k values: for k in tuple_of_k: print('Computing test set recalls at {0}'.format(k)) test_recall_list = recall_at_k(model=model, train_interactions=train_interactions, test_interactions=test_interactions, item_features=item_features, user_features=user_features, preserve_rows=preserve_rows, check_intersections=check_intersections, num_threads=num_threads, k=k) all_results_dict['test_recall_at_{0}_list'.format(k)] = test_recall_list # Compute test precision at all input k values: for k in tuple_of_k: print('Computing test set precisions at {0}'.format(k)) test_precision_list = precision_at_k(model=model, train_interactions=train_interactions, test_interactions=test_interactions, item_features=item_features, user_features=user_features, preserve_rows=preserve_rows, check_intersections=check_intersections, num_threads=num_threads, k=k) all_results_dict['test_precision_at_{0}_list'.format(k)] = test_precision_list # Compute test reciprocal rank print('Computing test set reciprocal rank') test_reciprocal_rank_list = reciprocal_rank(model=model, train_interactions=train_interactions, test_interactions=test_interactions, item_features=item_features, user_features=user_features, preserve_rows=preserve_rows, check_intersections=check_intersections, num_threads=num_threads) all_results_dict['test_reciprocal_rank_list'] = test_reciprocal_rank_list if compute_on_train: print('Computing metrics on training set as well') # Compute the train_auc: print('Computing train set AUCs') train_auc_list = auc_score(model=model, test_interactions=train_interactions, item_features=item_features, user_features=user_features, preserve_rows=preserve_rows, num_threads=num_threads) all_results_dict['train_auc_list'] = train_auc_list # Compute train recall at all input k values: for k in tuple_of_k: print('Computing train set recalls at {0}'.format(k)) train_recall_list = recall_at_k(model=model, test_interactions=train_interactions, item_features=item_features, user_features=user_features, preserve_rows=preserve_rows, num_threads=num_threads, k=k) all_results_dict['train_recall_at_{0}_list'.format(k)] = train_recall_list # Compute train precision at all input k values: for k in tuple_of_k: print('Computing train set precisions at {0}'.format(k)) train_precision_list = precision_at_k(model=model, test_interactions=train_interactions, item_features=item_features, user_features=user_features, preserve_rows=preserve_rows, num_threads=num_threads, k=k) all_results_dict['train_precision_at_{0}_list'.format(k)] = train_precision_list # Compute train reciprocal rank print('Computing train set reciprocal rank') train_reciprocal_rank_list = reciprocal_rank(model=model, test_interactions=train_interactions, item_features=item_features, user_features=user_features, preserve_rows=preserve_rows, num_threads=num_threads) all_results_dict['train_reciprocal_rank_list'] = train_reciprocal_rank_list return all_results_dict def compute_eval_metric_summaries(eval_dict): """ Given the computed evaluation metrics for each user in a set, this will produce a summary dataframe of descriptive performance statistics :param eval_dict: The evaluation dictionary returned by the compute_eval_metric function :return: dataframe with the summary stats of the evaluation metrics """ # Initialize a dictionary to store final results storage_dict = {} for metric_, value_list in eval_dict.items(): # Compute nobs, minmax, mean, variance, skewness, kurtosis = stats.describe(value_list) median = np.median(value_list) percentile_25 = np.percentile(value_list, q=25) percentile_75 = np.percentile(value_list, q=50) storage_dict[metric_[:-5]] = {'number_observations': nobs, 'min': minmax[0], 'max': minmax[1], 'mean': mean, 'variance': variance, 'skewness': skewness, 'kurtosis': kurtosis, 'median': median, '25th percentile': percentile_25, '75th percentile': percentile_75} results_df = pd.DataFrame.from_dict(storage_dict, orient='index') results_cols = results_df.columns.tolist() results_df.reset_index(inplace=True) results_df.columns = ['metric'] + results_cols return results_df def plot_metric_dist(eval_dict, metric, data_split='test', figsize=(20, 10), title=None, kde=False, save_name=None, axis_fontsize=10, title_fontsize=10): """ Function to plot the distribution of given metrics :param eval_dict: The evaluation metric dictionary returned by the compute_eval_metric function :param metric: The metric you want to see the distribution for :param data_split: If the provided values are on the test set or the train set :param figsize: Tuple which controls the vertical and horizontal figure size :param title: Optional string for the chart title :param kde: Optional boolean. True will fit a kernel density estimate to the distribution :param save_name: Path and file name you want to use to save the output charts. Should ideally be a pdf file. Optional :param axis_fontsize: Font size for the axes labels :param title_fontsize: Font size for the title :return: Nothing. Just makes the desired plot and saves it if directed to """ metric = metric.lower() data_split = data_split.lower() metric_key = data_split + '_' + metric + '_list' if metric_key not in eval_dict.keys(): raise ValueError('Invalid metric and/or data_split.') fig, ax = plt.subplots(figsize=figsize) if title is not None: sns.distplot(eval_dict[metric_key], kde=kde, ax=ax) ax.set_title(title, fontsize=title_fontsize) else: sns.distplot(eval_dict[metric_key], kde=kde, ax=ax) # Format x-axis name metric_name_list = metric_key[:-5].split('_') if metric_name_list[0] == 'test': metric_name_list = ['Test'] + metric_name_list[1::] else: metric_name_list = ['Train'] + metric_name_list[1::] xlabel = " ".join(metric_name_list) ax.set_xlabel(xlabel, fontsize=axis_fontsize) ax.set_ylabel('count', fontsize=axis_fontsize) if save_name is not None: plt.savefig(save_name) plt.show() def predict_on_users(model, dataset, list_of_user_ids, list_of_item_ids, item_features=None, user_features=None): """ Make a recommendation prediction on a set of users and a set of items :param model: The recommender model to use to make the predictions :param dataset: The dataset object created before fitting the LightFM model :param list_of_user_ids: A list of user ids from your dataset that you want to predict on. The ids must exist in the data mapping, so you may need to run a partial_fit on these user ids :param list_of_item_ids: A list of items ids from your dataset that you want to predict on. The ids must exist in the data mapping, so you may need to run a partial_fit on these item ids :param item_features: The sparse matrix of features for the items :param user_features: The sparse matrix of features for the users :return: A dataframe with the predicted score for each user-item pair """ # Convert the list of user ids and item ids to the internal indices used by the model internal_user_id_list = np.array([dataset.mapping()[0][id_] for id_ in list_of_user_ids]) internal_item_id_list = np.array([dataset.mapping()[2][id_] for id_ in list_of_item_ids]) # Invert the dictionaries for reverse translation internal_id_to_user_dict = {val: key for key, val in dataset.mapping()[0].items()} internal_id_to_item_dict = {val: key for key, val in dataset.mapping()[2].items()} pred_dict = {} for id_ in internal_user_id_list: preds_ = model.predict(user_ids=np.array([id_ for x in internal_item_id_list]), item_ids=internal_item_id_list, item_features=item_features, user_features=user_features) pred_dict[id_] = {} for i in range(len(preds_)): pred_dict[id_][internal_item_id_list[i]] = preds_[i] # Convert to a dataframe preds_list = [[key, val_0, val_1] for key, value in pred_dict.items() for val_0, val_1 in value.items()] preds_df = pd.DataFrame(preds_list) preds_df.columns = ['user_id_internal', 'item_id_internal', 'score'] # Map back to the external ids preds_df['user_id'] = preds_df['user_id_internal'].map(internal_id_to_user_dict) preds_df['item_id'] = preds_df['item_id_internal'].map(internal_id_to_item_dict) # Drop the internal ids as we don't need them anymore preds_df.drop(['user_id_internal', 'item_id_internal'], axis=1, inplace=True) return preds_df def format_recommendations(predictions_df, item_features_df=None, user_features_df=None, item_features_col=None, user_features_col=None, item_id_col=None, user_id_col=None): """ Formats the recommendation results in the dataframe produced by the predict_on_users function :param predictions_df: The dataframe produced by the predict_on_users function :param item_features_df: The original dataframe that contains the item ids and the features that went into the item_features sparse matrix :param user_features_df: The original dataframe that contains the user ids and the features that went into the user_features sparse matrix :param item_features_col: The name of the column in the item_features_df you want to us to describe the items being considered (i.e. study title, book title, etc). :param user_features_col: The name of the column in the user_features_df you want to us to describe the users being considered (i.e. name, twitter handle, etc). :param item_id_col: Column name in the item_features_df which contains the original item ids :param user_id_col: Column name in the user_features_df which contains the original user ids :return: Nothing. Just prints the results to the screen """ if isinstance(item_features_df, pd.core.frame.DataFrame): if not item_features_col: raise ValueError('Please supply a column from the item features dataframe to find the desired item descriptions') if not item_id_col: raise ValueError('Please supply the item id column name in the item features dataframe') if isinstance(user_features_df, pd.core.frame.DataFrame): if not user_features_col: raise ValueError('Please supply a column from the user features dataframe to find the desired user descriptions') if not user_id_col: raise ValueError('Please supply the user id column name in the user features dataframe') grped_df = predictions_df.groupby('user_id') for id_, grp in grped_df: df_ = grp.sort_values('score', ascending=False).reset_index() print('Recommendations for input ID: {0}'.format(id_)) if user_features_col: print('User description: {0}'.format(user_features_df[user_features_col].loc[(user_features_col[user_id_col] == id_)].values[0])) for i in range(df_.shape[0]): item = df_['item_id'].iloc[i] # We'll leave the score as is for now, but we may want to normalize the scale at some point score = df_['score'].iloc[i] print(' Recommended item id: {0}'.format(item)) if item_features_col: print(' Item description: {0}'.format(item_features_df[item_features_col].loc[(item_features_df[item_id_col] == item)].values[0])) print(' Score: {0}'.format(score)) print('---------------------------------------------------') print('\n') def save_data_objects(filename, dataset=None, matrix=None): """ Function to save data objects used for the model :param filename: Directory and name of the file you want to save data as. Dataset objects should be .pickle files. Matrix objects should be .npz files :param dataset: Dataset object used by LightFM. Should be saved as a .pickle file :param matrix: A scipy sparse matrix containing data. Should be a .npz file :return: Nothing. Saves the input file at the specified directory + file name """ # TODO: Add support for multiple saves at once. if dataset is not None: with open('filename', 'wb') as f: pickle.dump(dataset, f, protocol=pickle.HIGHEST_PROTOCOL) if matrix is not None: sparse.save_npz(filename, matrix) def load_data_objects(filename, is_dataset=False, is_matrix=False): """ Function to load both LightFM dataset objects and data matrices :param filename: Path and name of the file you want to load :param is_dataset: Set True if the file is a dataset object :param is_matrix: Set true if the file is a data matrix :return: Either LightFM dataset object or a scipy sparse matrix """ # TODO: Add support for multiple loads at once if is_dataset: dataset = pickle.load(open(filename, 'rb', -1)) return dataset if is_matrix: matrix = sparse.load_npz(filename) return matrix def make_long_tail_plot(df, item_id_column, save_name, interaction_type, take_top_n=None, percentage=None, x_labels=True): """ Makes a long tail plot of the interactions data. Inspired by: https://github.com/statisticianinstilettos/recmetrics :param df: :param item_id_column: :param save_name: :param interaction_type: :param take_top_n: :param percentage: :param x_labels: :return: None. Saves a plot instead. Doesn't display the plot since this could cause rendering issues with large datasets. """ # TODO: Make this more scalable. It struggles with 100,000+ data points. volume_df = pd.DataFrame(df[item_id_column].value_counts()) volume_df.reset_index(inplace=True) volume_df.columns = [item_id_column, "volume"] volume_df[item_id_column] = volume_df[item_id_column].astype(str) volume_df['cumulative_volume'] = volume_df['volume'].cumsum() volume_df['percent_of_total_volume'] = volume_df['cumulative_volume'] / volume_df['volume'].sum() volume_df.sort_values(by='volume', ascending=False) if take_top_n is not None: volume_df = volume_df.head(take_top_n) else: print("Using all interaction data. This could result in long run times for large datasets.") print('Consider using the take_top_n argument to limit to the top n interactions if you have more than 10,000 in your dataset.') # line plot of cumulative volume x = range(0, len(volume_df)) f = plt.figure(figsize=(20, 10)) ax = sns.lineplot(x, y="volume", data=volume_df, color="black") plt.xticks(x) # set labels ax.set_title('Long Tail Plot') ax.set_ylabel('# of ' + interaction_type) ax.set_xlabel(item_id_column) ax.tick_params(axis='x', labelrotation=90) if percentage is not None: # plot vertical line at the tail location head = volume_df[volume_df.percent_of_total_volume <= percentage] tail = volume_df[volume_df.percent_of_total_volume > percentage] items_in_head = len(head) items_in_tail = len(tail) plt.axvline(x=items_in_head, color="red", linestyle='--') # fill area under plot head = head.append(tail.head(1)) x1 = head.index.values y1 = head['volume'] x2 = tail.index.values y2 = tail['volume'] ax.fill_between(x1, y1, color="blue", alpha=0.2) ax.fill_between(x2, y2, color="orange", alpha=0.2) # create legend legend_elements = [Line2D([0], [0], marker='o', color='w', label=str(items_in_head) + ': items in the head', markerfacecolor='blue', markersize=5), Line2D([0], [0], marker='o', color='w', label=str(items_in_tail) + ': items in the tail', markerfacecolor='orange', markersize=5)] ax.legend(handles=legend_elements, loc=1) else: x1 = volume_df[item_id_column] y1 = volume_df['volume'] ax.fill_between(x1, y1, color="blue", alpha=0.3) if not x_labels: plt.xticks([], []) ax.set(xticklabels=[]) else: ax.set_xticklabels(labels=volume_df[item_id_column], rotation=45, ha="right") plt.savefig(save_name) return None def make_cold_start_recommendation_on_new_user(model, dataset, new_user_features_df, item_ids_list, item_features=None, new_user_id=None): """ A function to make cold start recommendations provided items for a new user :param model: The trained hybrid recommender model :param dataset: The dataset object, used to get the mappings between user/item ids and their internal ids. :param new_user_features_df: DataFrame with the user features to use. It should be just one row with the new user features. The features must be the same as those already seen by the model :param item_ids_list: A list of item ids you want to consider in the predictions. If you want to run the prediction on all items, pass a list of all item ids seen by the model. You shouldn't try to use this with cold start items. Use the make_cold_start_recommendation_on_new_item function instead. :param item_features: The original sparse matrix with the features of the existing items in the model. :param new_user_id: Optional id number of the new user. This doesn't do anything other than get added to the final output if it is given to the function. :return: A dataframe with the rank order scores for the new user on all required items. """ # TODO: Same for cold start users and items simultaneously. The latter is made more difficult as we'll have to deal with the internal indices pointing to specific rows of the feature matrices # TODO: Add more checks for inputs to this function. There are a lot of potential failure modes. # Convert the list of item ids to the internal indices used by the model internal_item_id_list = np.array([dataset.mapping()[2][id_] for id_ in item_ids_list]) # Invert the dictionaries for reverse translation internal_id_to_item_dict = {val: key for key, val in dataset.mapping()[2].items()} item_preds_dict = {} # convert the input DataFrame of features for the new user into a sparse type new_user_features_sparse = sparse.coo_matrix(new_user_features_df.values.tolist()[0]) # Run a prediction on all available items for the new user preds_array = model.predict(user_ids=[0 for id_ in internal_item_id_list], item_ids=internal_item_id_list, item_features=item_features, user_features=new_user_features_sparse) for idx_, item_id in enumerate(internal_item_id_list): item_preds_dict[item_id] = preds_array[idx_] # Convert to DataFrame item_preds_df = pd.DataFrame.from_dict(item_preds_dict, orient='index') item_preds_df.reset_index(inplace=True) item_preds_df.columns = ['item_id_internal', 'score'] item_preds_df['item_id'] = item_preds_df['item_id_internal'].map(internal_id_to_item_dict) # Drop the internal ids as we don't need them anymore item_preds_df.drop(['item_id_internal'], axis=1, inplace=True) # If there is a new user id to be used here (not in the existing dataset) add it to the dataframe if new_user_id is not None: item_preds_df['user_id'] = new_user_id # Rank order the recommendations scores item_preds_df.sort_values(by='score', ascending=False, inplace=True) # Re-order the index after sorting item_preds_df.reset_index(inplace=True, drop=True) # Re-order the columns for returning if new_user_id is not None: item_preds_df = item_preds_df[['user_id', 'item_id', 'score']] else: item_preds_df = item_preds_df[['item_id', 'score']] return item_preds_df def make_cold_start_recommendation_on_new_item(model, dataset, new_item_features_df, user_ids_list, user_features=None, new_item_id=None): """ A function to make cold start recommendations on existing users for a new item :param model: The trained hybrid recommender model :param dataset: The dataset object, used to get the mappings between user/item ids and their internal ids. :param new_item_features_df: DataFrame with the item features to use. It should be just one row with the new item features. The features must be the same as those already seen by the model :param user_ids_list: A list of user ids you want to consider in the predictions. If you want to run the prediction on all user, pass a list of all user ids seen by the model. You shouldn't try to use this with cold start user. Use the make_cold_start_recommendation_on_new_user function instead. :param user_features: The original sparse matrix with the features of the existing users in the model. :param new_item_id: Optional id number of the new item. This doesn't do anything other than get added to the final output if it is given to the function. :return: A dataframe with the rank order scores for the new item on all required users. """ # TODO: Same for cold start users and items simultaneously. The latter is made more difficult as we'll have to deal with the internal indices pointing to specific rows of the feature matrices # TODO: Add more checks for inputs to this function. There are a lot of potential failure modes. # TODO: Write wrapper to get cold start to run along with hot start. # Convert the list of item ids to the internal indices used by the model internal_user_id_list = np.array([dataset.mapping()[0][id_] for id_ in user_ids_list]) # Invert the dictionaries for reverse translation internal_id_to_user_dict = {val: key for key, val in dataset.mapping()[0].items()} user_preds_dict = {} # convert the input DataFrame of features for the new user into a sparse type new_item_features_sparse = sparse.coo_matrix(new_item_features_df.values.tolist()[0]) # Run a prediction on all available items for the new user preds_array = model.predict(user_ids=internal_user_id_list, item_ids=[0 for id_ in internal_user_id_list], user_features=user_features, item_features=new_item_features_sparse) for idx_, user_id in enumerate(internal_user_id_list): user_preds_dict[user_id] = preds_array[idx_] # Convert to DataFrame user_preds_df = pd.DataFrame.from_dict(user_preds_dict, orient='index') user_preds_df.reset_index(inplace=True) user_preds_df.columns = ['user_id_internal', 'score'] user_preds_df['user_id'] = user_preds_df['user_id_internal'].map(internal_id_to_user_dict) # Drop the internal ids as we don't need them anymore user_preds_df.drop(['user_id_internal'], axis=1, inplace=True) # If there is a new item id to be used here (not in the existing dataset) add it to the dataframe if new_item_id is not None: user_preds_df['item_id'] = new_item_id # Rank order the recommendations scores user_preds_df.sort_values(by='score', ascending=False, inplace=True) # Re-order the index after sorting user_preds_df.reset_index(inplace=True, drop=True) # Re-order the columns for returning if new_item_id is not None: user_preds_df = user_preds_df[['user_id', 'item_id', 'score']] else: user_preds_df = user_preds_df[['user_id', 'score']] return user_preds_df def reduce_text(text, allowed_text_df, allowed_text_column): """ A function to reduce an input document to include only words contained in the allowed_text_df (with values in the allowed_text_columns) :param text: A string of words in the text, separated by commas. This should be handled automatically by the input from the make_text_encoded_features function :param allowed_text_df: The optional DataFrame containing the curated vocabulary of terms :param allowed_text_column: The column in keyword_df which contains the words :return: A list with the reduced text """ text_list_ = text.split(',') text_reduced_list_ = [x for x in text_list_ if x in allowed_text_df[allowed_text_column].values] return text_reduced_list_ def make_vectorized_columns(df, list_of_text_columns, prefixes=None): """ A function to vectorize the documents in each column in the list list_of_text_columns :param df: The DataFrame containing the text to vectorize :param list_of_text_columns: A list of column names in df which has the texts to vectorize :param prefixes: An optional list of prefixes to attach to each of the vectorized features for each column. For example, if you had two columns to consider you might add prefixes=['col1', 'col2'] to indicate which columns each vectorized feature originally came from :return: A DataFrame with the vectorized text features """ print('Making vectorized columns out of text columns {0}'.format(list_of_text_columns)) df_list = [] for i, col in enumerate(list_of_text_columns): corpus_ = df[col].values vectorizer = CountVectorizer() text_x = vectorizer.fit_transform(corpus_) if prefixes is not None: prefix_ = prefixes[i] col_names_ = [prefix_ + x for x in vectorizer.get_feature_names()] else: col_names_ = [x for x in vectorizer.get_feature_names()] df_ = pd.DataFrame(text_x.A, columns=col_names_) df_list.append(df_) if len(df_list) > 1: return
pd.concat(df_list, axis=1)
pandas.concat
import json from unittest.mock import MagicMock, patch import numpy as np import pandas as pd import pytest import woodwork as ww from evalml.exceptions import PipelineScoreError from evalml.model_understanding.prediction_explanations.explainers import ( abs_error, cross_entropy, explain_prediction, explain_predictions, explain_predictions_best_worst ) from evalml.problem_types import ProblemTypes def compare_two_tables(table_1, table_2): assert len(table_1) == len(table_2) for row, row_answer in zip(table_1, table_2): assert row.strip().split() == row_answer.strip().split() test_features = [[1], np.ones((15, 1)), pd.DataFrame({"a": [1, 2, 3], "b": [1, 2, 3]}).iloc[0], pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}), pd.DataFrame()] @pytest.mark.parametrize("test_features", test_features) def test_explain_prediction_value_error(test_features): with pytest.raises(ValueError, match="features must be stored in a dataframe or datatable with exactly one row."): explain_prediction(None, input_features=test_features, training_data=None) explain_prediction_answer = """Feature Name Feature Value Contribution to Prediction ========================================================= d 40.00 +++++ b 20.00 -----""".splitlines() explain_prediction_regression_dict_answer = { "explanations": [{ "feature_names": ["d", "b"], "feature_values": [40, 20], "qualitative_explanation": ["+++++", "-----"], "quantitative_explanation": [None, None], "class_name": None }] } explain_predictions_regression_df_answer = pd.DataFrame({'feature_names': ['d', 'b'], 'feature_values': [40, 20], 'qualitative_explanation': ['+++++', '-----'], "quantitative_explanation": [None, None]}) explain_prediction_binary_dict_answer = { "explanations": [{ "feature_names": ["d", "b"], "feature_values": [40, 20], "qualitative_explanation": ["+++++", "-----"], "quantitative_explanation": [None, None], "class_name": "class_1" }] } explain_prediction_binary_df_answer = pd.DataFrame({ "feature_names": ["d", "b"], "feature_values": [40, 20], "qualitative_explanation": ["+++++", "-----"], "quantitative_explanation": [None, None], "class_name": ["class_1", "class_1"] }) explain_prediction_multiclass_answer = """Class: class_0 Feature Name Feature Value Contribution to Prediction ========================================================= a 10.00 +++++ c 30.00 --- Class: class_1 Feature Name Feature Value Contribution to Prediction ========================================================= a 10.00 +++ b 20.00 ++ Class: class_2 Feature Name Feature Value Contribution to Prediction ========================================================= c 30.00 --- d 40.00 --- """.splitlines() explain_prediction_multiclass_dict_answer = { "explanations": [ {"feature_names": ["a", "c"], "feature_values": [10, 30], "qualitative_explanation": ["+++++", "---"], "quantitative_explanation": [None, None], "class_name": "class_0"}, {"feature_names": ["a", "b"], "feature_values": [10, 20], "qualitative_explanation": ["+++", "++"], "quantitative_explanation": [None, None], "class_name": "class_1"}, {"feature_names": ["c", "d"], "feature_values": [30, 40], "qualitative_explanation": ["---", "---"], "quantitative_explanation": [None, None], "class_name": "class_2"}, ] } explain_prediction_multiclass_df_answer = pd.DataFrame({ "feature_names": ["a", "c", "a", "b", "c", "d"], "feature_values": [10, 30, 10, 20, 30, 40], "qualitative_explanation": ["+++++", "---", "+++", "++", "---", "---"], "quantitative_explanation": [None, None, None, None, None, None], "class_name": ['class_0', 'class_0', 'class_1', 'class_1', 'class_2', 'class_2'] }) @pytest.mark.parametrize("problem_type, output_format, shap_values, normalized_shap_values, answer", [(ProblemTypes.REGRESSION, "text", {"a": [1], "b": [-2.1], "c": [-0.25], "d": [2.3]}, {"a": [0.5], "b": [-2.1], "c": [-0.25], "d": [2.3]}, explain_prediction_answer), (ProblemTypes.REGRESSION, "dict", {"a": [1], "b": [-2.1], "c": [-0.25], "d": [2.3]}, {"a": [0.5], "b": [-2.1], "c": [-0.25], "d": [2.3]}, explain_prediction_regression_dict_answer ), (ProblemTypes.REGRESSION, "dataframe", {"a": [1], "b": [-2.1], "c": [-0.25], "d": [2.3]}, {"a": [0.5], "b": [-2.1], "c": [-0.25], "d": [2.3]}, explain_predictions_regression_df_answer ), (ProblemTypes.BINARY, "text", [{}, {"a": [0.5], "b": [-0.89], "c": [0.33], "d": [0.89]}], [{}, {"a": [0.5], "b": [-0.89], "c": [-0.25], "d": [0.89]}], explain_prediction_answer), (ProblemTypes.BINARY, "dict", [{}, {"a": [0.5], "b": [-0.89], "c": [0.33], "d": [0.89]}], [{}, {"a": [0.5], "b": [-0.89], "c": [-0.25], "d": [0.89]}], explain_prediction_binary_dict_answer), (ProblemTypes.BINARY, "dataframe", [{}, {"a": [0.5], "b": [-0.89], "c": [0.33], "d": [0.89]}], [{}, {"a": [0.5], "b": [-0.89], "c": [-0.25], "d": [0.89]}], explain_prediction_binary_df_answer), (ProblemTypes.MULTICLASS, "text", [{}, {}, {}], [{"a": [1.1], "b": [0.09], "c": [-0.53], "d": [-0.06]}, {"a": [0.53], "b": [0.24], "c": [-0.15], "d": [-0.22]}, {"a": [0.03], "b": [0.02], "c": [-0.42], "d": [-0.47]}], explain_prediction_multiclass_answer), (ProblemTypes.MULTICLASS, "dict", [{}, {}, {}], [{"a": [1.1], "b": [0.09], "c": [-0.53], "d": [-0.06]}, {"a": [0.53], "b": [0.24], "c": [-0.15], "d": [-0.22]}, {"a": [0.03], "b": [0.02], "c": [-0.42], "d": [-0.47]}], explain_prediction_multiclass_dict_answer), (ProblemTypes.MULTICLASS, "dataframe", [{}, {}, {}], [{"a": [1.1], "b": [0.09], "c": [-0.53], "d": [-0.06]}, {"a": [0.53], "b": [0.24], "c": [-0.15], "d": [-0.22]}, {"a": [0.03], "b": [0.02], "c": [-0.42], "d": [-0.47]}], explain_prediction_multiclass_df_answer) ]) @pytest.mark.parametrize("input_type", ["pd", "ww"]) @patch("evalml.model_understanding.prediction_explanations._user_interface._compute_shap_values") @patch("evalml.model_understanding.prediction_explanations._user_interface._normalize_shap_values") def test_explain_prediction(mock_normalize_shap_values, mock_compute_shap_values, problem_type, output_format, shap_values, normalized_shap_values, answer, input_type): mock_compute_shap_values.return_value = shap_values mock_normalize_shap_values.return_value = normalized_shap_values pipeline = MagicMock() pipeline.problem_type = problem_type pipeline.classes_ = ["class_0", "class_1", "class_2"] # By the time we call transform, we are looking at only one row of the input data. pipeline.compute_estimator_features.return_value = ww.DataTable(pd.DataFrame({"a": [10], "b": [20], "c": [30], "d": [40]})) features = pd.DataFrame({"a": [1], "b": [2]}) training_data = pd.DataFrame() if input_type == "ww": features = ww.DataTable(features) training_data = ww.DataTable(training_data) table = explain_prediction(pipeline, features, output_format=output_format, top_k=2, training_data=training_data) if isinstance(table, str): compare_two_tables(table.splitlines(), answer) elif isinstance(table, pd.DataFrame): pd.testing.assert_frame_equal(table, answer) else: assert table == answer def test_error_metrics(): pd.testing.assert_series_equal(abs_error(pd.Series([1, 2, 3]), pd.Series([4, 1, 0])), pd.Series([3, 1, 3])) pd.testing.assert_series_equal(cross_entropy(pd.Series([1, 0]), pd.DataFrame({"a": [0.1, 0.2], "b": [0.9, 0.8]})), pd.Series([-np.log(0.9), -np.log(0.2)])) input_features_and_y_true = [([[1]], pd.Series([1]), "^Input features must be a dataframe with more than 10 rows!"), (pd.DataFrame({"a": [1]}),
pd.Series([1])
pandas.Series
#Importing Necessary Libraries import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import ElasticNet from pandas import Series, DataFrame from sklearn.model_selection import train_test_split #Importing the Training and Test Files train = pd.read_csv('Train.csv') test = pd.read_csv('Test.csv') #Splitting into Training and CV for Cross Validation X = train.loc[:,['Outlet_Establishment_Year', 'Item_MRP']] x_train, x_cv, y_train, y_cv = train_test_split(X, train.Item_Outlet_Sales) #ElasticNet Regression ENreg = ElasticNet(alpha=1,l1_ratio=0.5,normalize=False) ENreg.fit(x_train,y_train) pred = ENreg.predict(x_cv) #Calculating the mean squared error mse = np.mean((pred - y_cv)**2) print('Mean Squared Error:',mse) print('Score:',ENreg.score(x_cv,y_cv)) #Calculation of coefficients coeff = DataFrame(x_train.columns) coeff['Coefficient Estimate'] = Series(ENreg.coef_) print(coeff) #Plotting Analysis through a Residual Plot x_plot = plt.scatter(pred, (pred - y_cv), c='b') plt.hlines(y=0, xmin=-1000, xmax=5000) plt.title('Residual Plot') plt.show() #Magnitude of Coefficents predictors = x_train.columns coef =
Series(ENreg.coef_,predictors)
pandas.Series
import pandas as pd import numpy as np class IdleSleepModeConverter: def __init__(self, init_fill=None, init_is_ism=None): if init_fill is not None: self._current_fill = init_fill else: self._current_fill = pd.DataFrame() if init_is_ism is not None: self._before_is_ism = init_is_ism else: self._before_is_ism = pd.DataFrame() self._before_10s = pd.DataFrame() self._converted = pd.DataFrame() def get_ism(self): return self._ism_df def get_closest_fill(self, st): df = self._df.set_index(self._df.columns[0]) fills = self._is_fill[self._is_fill.index <= st] if not fills.empty: fill_et = fills.index[-1] + pd.Timedelta(1, unit='s') fill_st = fill_et - pd.Timedelta(10, unit='s') current_fill = df[(df.index >= fill_st) & (df.index < fill_et)] if current_fill.index[-1] - current_fill.index[0] > pd.Timedelta(9, unit='s'): self._current_fill = current_fill return self._current_fill def get_last_10s(self): df = self._df.set_index('HEADER_TIME_STAMP') et = df.index[-1] + pd.Timedelta(1, unit='s') st = et -
pd.Timedelta(10, unit='s')
pandas.Timedelta
from src.lstm import LSTM from src.attention import Attention from src.regressor import AttnRegressor from src.make_data import DataGenerator from src.optimize import OptimizedRounder import pandas as pd import numpy as np import warnings import os import argparse import joblib import pickle import torch from torch.utils.data import DataLoader, TensorDataset torch.manual_seed(1) warnings.filterwarnings('ignore') parser = argparse.ArgumentParser(description='LSTM Model') parser.add_argument('input_csv', type=str, help="input file must contain 'text_id', 'prompt' and 'text' column") args = parser.parse_args() model_type = "LSTM" def main(): # gpu or cpuでモデルを定義 device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print(f"Is cuda available: {torch.cuda.is_available()}") test_path = args.input_csv test_df = pd.read_csv(test_path) result_df = pd.DataFrame() result_df["text_id"] = test_df["text_id"] cols = ["holistic", "content", "organization", "language"] for col in cols: # データの読み込み Data_gen = DataGenerator(f"./trained_models/LSTM/{col}/word2index") lstm = joblib.load(f"./trained_models/LSTM/{col}/lstm") regressor = joblib.load(f"./trained_models/LSTM/{col}/regressor") # predict test data test_path = args.input_csv test_df =
pd.read_csv(test_path)
pandas.read_csv
from statsmodels.compat.numpy import lstsq from statsmodels.compat.pandas import assert_index_equal from statsmodels.compat.platform import PLATFORM_WIN from statsmodels.compat.python import lrange import os import warnings import numpy as np from numpy.testing import ( assert_, assert_allclose, assert_almost_equal, assert_equal, assert_raises, ) import pandas as pd from pandas import DataFrame, Series, date_range import pytest from scipy.interpolate import interp1d from statsmodels.datasets import macrodata, modechoice, nile, randhie, sunspots from statsmodels.tools.sm_exceptions import ( CollinearityWarning, InfeasibleTestError, InterpolationWarning, MissingDataError, ) # Remove imports when range unit root test gets an R implementation from statsmodels.tools.validation import array_like, bool_like from statsmodels.tsa.arima_process import arma_acovf from statsmodels.tsa.statespace.sarimax import SARIMAX from statsmodels.tsa.stattools import ( acf, acovf, adfuller, arma_order_select_ic, breakvar_heteroskedasticity_test, ccovf, coint, grangercausalitytests, innovations_algo, innovations_filter, kpss, levinson_durbin, levinson_durbin_pacf, pacf, pacf_burg, pacf_ols, pacf_yw, range_unit_root_test, zivot_andrews, ) DECIMAL_8 = 8 DECIMAL_6 = 6 DECIMAL_5 = 5 DECIMAL_4 = 4 DECIMAL_3 = 3 DECIMAL_2 = 2 DECIMAL_1 = 1 CURR_DIR = os.path.dirname(os.path.abspath(__file__)) @pytest.fixture(scope="module") def acovf_data(): rnd = np.random.RandomState(12345) return rnd.randn(250) class CheckADF(object): """ Test Augmented Dickey-Fuller Test values taken from Stata. """ levels = ["1%", "5%", "10%"] data = macrodata.load_pandas() x = data.data["realgdp"].values y = data.data["infl"].values def test_teststat(self): assert_almost_equal(self.res1[0], self.teststat, DECIMAL_5) def test_pvalue(self): assert_almost_equal(self.res1[1], self.pvalue, DECIMAL_5) def test_critvalues(self): critvalues = [self.res1[4][lev] for lev in self.levels] assert_almost_equal(critvalues, self.critvalues, DECIMAL_2) class TestADFConstant(CheckADF): """ Dickey-Fuller test for unit root """ @classmethod def setup_class(cls): cls.res1 = adfuller(cls.x, regression="c", autolag=None, maxlag=4) cls.teststat = 0.97505319 cls.pvalue = 0.99399563 cls.critvalues = [-3.476, -2.883, -2.573] class TestADFConstantTrend(CheckADF): """""" @classmethod def setup_class(cls): cls.res1 = adfuller(cls.x, regression="ct", autolag=None, maxlag=4) cls.teststat = -1.8566374 cls.pvalue = 0.67682968 cls.critvalues = [-4.007, -3.437, -3.137] # FIXME: do not leave commented-out # class TestADFConstantTrendSquared(CheckADF): # """ # """ # pass # TODO: get test values from R? class TestADFNoConstant(CheckADF): """""" @classmethod def setup_class(cls): with pytest.warns(FutureWarning): adfuller(cls.x, regression="nc", autolag=None, maxlag=4) cls.res1 = adfuller(cls.x, regression="n", autolag=None, maxlag=4) cls.teststat = 3.5227498 cls.pvalue = 0.99999 # Stata does not return a p-value for noconstant. # Tau^max in MacKinnon (1994) is missing, so it is # assumed that its right-tail is well-behaved cls.critvalues = [-2.587, -1.950, -1.617] # No Unit Root class TestADFConstant2(CheckADF): @classmethod def setup_class(cls): cls.res1 = adfuller(cls.y, regression="c", autolag=None, maxlag=1) cls.teststat = -4.3346988 cls.pvalue = 0.00038661 cls.critvalues = [-3.476, -2.883, -2.573] class TestADFConstantTrend2(CheckADF): @classmethod def setup_class(cls): cls.res1 = adfuller(cls.y, regression="ct", autolag=None, maxlag=1) cls.teststat = -4.425093 cls.pvalue = 0.00199633 cls.critvalues = [-4.006, -3.437, -3.137] class TestADFNoConstant2(CheckADF): @classmethod def setup_class(cls): cls.res1 = adfuller(cls.y, regression="n", autolag=None, maxlag=1) cls.teststat = -2.4511596 cls.pvalue = 0.013747 # Stata does not return a p-value for noconstant # this value is just taken from our results cls.critvalues = [-2.587, -1.950, -1.617] _, _1, _2, cls.store = adfuller( cls.y, regression="n", autolag=None, maxlag=1, store=True ) def test_store_str(self): assert_equal( self.store.__str__(), "Augmented Dickey-Fuller Test Results" ) class CheckCorrGram(object): """ Set up for ACF, PACF tests. """ data = macrodata.load_pandas() x = data.data["realgdp"] filename = os.path.join(CURR_DIR, "results", "results_corrgram.csv") results = pd.read_csv(filename, delimiter=",") class TestACF(CheckCorrGram): """ Test Autocorrelation Function """ @classmethod def setup_class(cls): cls.acf = cls.results["acvar"] # cls.acf = np.concatenate(([1.], cls.acf)) cls.qstat = cls.results["Q1"] cls.res1 = acf(cls.x, nlags=40, qstat=True, alpha=0.05, fft=False) cls.confint_res = cls.results[["acvar_lb", "acvar_ub"]].values def test_acf(self): assert_almost_equal(self.res1[0][1:41], self.acf, DECIMAL_8) def test_confint(self): centered = self.res1[1] - self.res1[1].mean(1)[:, None] assert_almost_equal(centered[1:41], self.confint_res, DECIMAL_8) def test_qstat(self): assert_almost_equal(self.res1[2][:40], self.qstat, DECIMAL_3) # 3 decimal places because of stata rounding # FIXME: enable/xfail/skip or delete # def pvalue(self): # pass # NOTE: should not need testing if Q stat is correct class TestACF_FFT(CheckCorrGram): # Test Autocorrelation Function using FFT @classmethod def setup_class(cls): cls.acf = cls.results["acvarfft"] cls.qstat = cls.results["Q1"] cls.res1 = acf(cls.x, nlags=40, qstat=True, fft=True) def test_acf(self): assert_almost_equal(self.res1[0][1:], self.acf, DECIMAL_8) def test_qstat(self): # todo why is res1/qstat 1 short assert_almost_equal(self.res1[1], self.qstat, DECIMAL_3) class TestACFMissing(CheckCorrGram): # Test Autocorrelation Function using Missing @classmethod def setup_class(cls): cls.x = np.concatenate((np.array([np.nan]), cls.x)) cls.acf = cls.results["acvar"] # drop and conservative cls.qstat = cls.results["Q1"] cls.res_drop = acf( cls.x, nlags=40, qstat=True, alpha=0.05, missing="drop", fft=False ) cls.res_conservative = acf( cls.x, nlags=40, qstat=True, alpha=0.05, fft=False, missing="conservative", ) cls.acf_none = np.empty(40) * np.nan # lags 1 to 40 inclusive cls.qstat_none = np.empty(40) * np.nan cls.res_none = acf( cls.x, nlags=40, qstat=True, alpha=0.05, missing="none", fft=False ) def test_raise(self): with pytest.raises(MissingDataError): acf( self.x, nlags=40, qstat=True, fft=False, alpha=0.05, missing="raise", ) def test_acf_none(self): assert_almost_equal(self.res_none[0][1:41], self.acf_none, DECIMAL_8) def test_acf_drop(self): assert_almost_equal(self.res_drop[0][1:41], self.acf, DECIMAL_8) def test_acf_conservative(self): assert_almost_equal( self.res_conservative[0][1:41], self.acf, DECIMAL_8 ) def test_qstat_none(self): # todo why is res1/qstat 1 short assert_almost_equal(self.res_none[2], self.qstat_none, DECIMAL_3) # FIXME: enable/xfail/skip or delete # how to do this test? the correct q_stat depends on whether nobs=len(x) is # used when x contains NaNs or whether nobs<len(x) when x contains NaNs # def test_qstat_drop(self): # assert_almost_equal(self.res_drop[2][:40], self.qstat, DECIMAL_3) class TestPACF(CheckCorrGram): @classmethod def setup_class(cls): cls.pacfols = cls.results["PACOLS"] cls.pacfyw = cls.results["PACYW"] def test_ols(self): pacfols, confint = pacf(self.x, nlags=40, alpha=0.05, method="ols") assert_almost_equal(pacfols[1:], self.pacfols, DECIMAL_6) centered = confint - confint.mean(1)[:, None] # from edited Stata ado file res = [[-0.1375625, 0.1375625]] * 40 assert_almost_equal(centered[1:41], res, DECIMAL_6) # check lag 0 assert_equal(centered[0], [0.0, 0.0]) assert_equal(confint[0], [1, 1]) assert_equal(pacfols[0], 1) def test_ols_inefficient(self): lag_len = 5 pacfols = pacf_ols(self.x, nlags=lag_len, efficient=False) x = self.x.copy() x -= x.mean() n = x.shape[0] lags = np.zeros((n - 5, 5)) lead = x[5:] direct = np.empty(lag_len + 1) direct[0] = 1.0 for i in range(lag_len): lags[:, i] = x[5 - (i + 1) : -(i + 1)] direct[i + 1] = lstsq(lags[:, : (i + 1)], lead, rcond=None)[0][-1] assert_allclose(pacfols, direct, atol=1e-8) def test_yw(self): pacfyw = pacf_yw(self.x, nlags=40, method="mle") assert_almost_equal(pacfyw[1:], self.pacfyw, DECIMAL_8) def test_ld(self): pacfyw = pacf_yw(self.x, nlags=40, method="mle") pacfld = pacf(self.x, nlags=40, method="ldb") assert_almost_equal(pacfyw, pacfld, DECIMAL_8) pacfyw = pacf(self.x, nlags=40, method="yw") pacfld = pacf(self.x, nlags=40, method="lda") assert_almost_equal(pacfyw, pacfld, DECIMAL_8) class TestBreakvarHeteroskedasticityTest(object): from scipy.stats import chi2, f def test_1d_input(self): input_residuals = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0] expected_statistic = (4.0 ** 2 + 5.0 ** 2) / (0.0 ** 2 + 1.0 ** 2) # ~ F(2, 2), two-sided test expected_pvalue = 2 * min( self.f.cdf(expected_statistic, 2, 2), self.f.sf(expected_statistic, 2, 2), ) actual_statistic, actual_pvalue = breakvar_heteroskedasticity_test( input_residuals ) assert actual_statistic == expected_statistic assert actual_pvalue == expected_pvalue def test_2d_input_with_missing_values(self): input_residuals = np.array( [ [0.0, 0.0, np.nan], [1.0, np.nan, 1.0], [2.0, 2.0, np.nan], [3.0, 3.0, 3.0], [4.0, 4.0, 4.0], [5.0, 5.0, 5.0], [6.0, 6.0, 6.0], [7.0, 7.0, 7.0], [8.0, 8.0, 8.0], ] ) expected_statistic = np.array( [ (8.0 ** 2 + 7.0 ** 2 + 6.0 ** 2) / (0.0 ** 2 + 1.0 ** 2 + 2.0 ** 2), (8.0 ** 2 + 7.0 ** 2 + 6.0 ** 2) / (0.0 ** 2 + 2.0 ** 2), np.nan, ] ) expected_pvalue = np.array( [ 2 * min( self.f.cdf(expected_statistic[0], 3, 3), self.f.sf(expected_statistic[0], 3, 3), ), 2 * min( self.f.cdf(expected_statistic[1], 3, 2), self.f.sf(expected_statistic[1], 3, 2), ), np.nan, ] ) actual_statistic, actual_pvalue = breakvar_heteroskedasticity_test( input_residuals ) assert_equal(actual_statistic, expected_statistic) assert_equal(actual_pvalue, expected_pvalue) @pytest.mark.parametrize( "subset_length,expected_statistic,expected_pvalue", [ (2, 41, 2 * min(f.cdf(41, 2, 2), f.sf(41, 2, 2))), (0.5, 10, 2 * min(f.cdf(10, 3, 3), f.sf(10, 3, 3))), ], ) def test_subset_length( self, subset_length, expected_statistic, expected_pvalue ): input_residuals = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0] actual_statistic, actual_pvalue = breakvar_heteroskedasticity_test( input_residuals, subset_length=subset_length, ) assert actual_statistic == expected_statistic assert actual_pvalue == expected_pvalue @pytest.mark.parametrize( "alternative,expected_statistic,expected_pvalue", [ ("two-sided", 41, 2 * min(f.cdf(41, 2, 2), f.sf(41, 2, 2))), ("decreasing", 1 / 41, f.sf(1 / 41, 2, 2)), ("increasing", 41, f.sf(41, 2, 2)), ], ) def test_alternative( self, alternative, expected_statistic, expected_pvalue ): input_residuals = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0] actual_statistic, actual_pvalue = breakvar_heteroskedasticity_test( input_residuals, alternative=alternative, ) assert actual_statistic == expected_statistic assert actual_pvalue == expected_pvalue def test_use_chi2(self): input_residuals = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0] expected_statistic = (4.0 ** 2 + 5.0 ** 2) / (0.0 ** 2 + 1.0 ** 2) expected_pvalue = 2 * min( self.chi2.cdf(2 * expected_statistic, 2), self.chi2.sf(2 * expected_statistic, 2), ) actual_statistic, actual_pvalue = breakvar_heteroskedasticity_test( input_residuals, use_f=False, ) assert actual_statistic == expected_statistic assert actual_pvalue == expected_pvalue class CheckCoint(object): """ Test Cointegration Test Results for 2-variable system Test values taken from Stata """ levels = ["1%", "5%", "10%"] data = macrodata.load_pandas() y1 = data.data["realcons"].values y2 = data.data["realgdp"].values def test_tstat(self): assert_almost_equal(self.coint_t, self.teststat, DECIMAL_4) # this does not produce the old results anymore class TestCoint_t(CheckCoint): """ Get AR(1) parameter on residuals """ @classmethod def setup_class(cls): # cls.coint_t = coint(cls.y1, cls.y2, trend="c")[0] cls.coint_t = coint(cls.y1, cls.y2, trend="c", maxlag=0, autolag=None)[ 0 ] cls.teststat = -1.8208817 cls.teststat = -1.830170986148 def test_coint(): nobs = 200 scale_e = 1 const = [1, 0, 0.5, 0] np.random.seed(123) unit = np.random.randn(nobs).cumsum() y = scale_e * np.random.randn(nobs, 4) y[:, :2] += unit[:, None] y += const y = np.round(y, 4) # FIXME: enable/xfail/skip or delete for trend in []: # ['c', 'ct', 'ctt', 'n']: print("\n", trend) print(coint(y[:, 0], y[:, 1], trend=trend, maxlag=4, autolag=None)) print(coint(y[:, 0], y[:, 1:3], trend=trend, maxlag=4, autolag=None)) print(coint(y[:, 0], y[:, 2:], trend=trend, maxlag=4, autolag=None)) print(coint(y[:, 0], y[:, 1:], trend=trend, maxlag=4, autolag=None)) # results from Stata egranger res_egranger = {} # trend = 'ct' res = res_egranger["ct"] = {} res[0] = [ -5.615251442239, -4.406102369132, -3.82866685109, -3.532082997903, ] res[1] = [ -5.63591313706, -4.758609717199, -4.179130554708, -3.880909696863, ] res[2] = [ -2.892029275027, -4.758609717199, -4.179130554708, -3.880909696863, ] res[3] = [-5.626932544079, -5.08363327039, -4.502469783057, -4.2031051091] # trend = 'c' res = res_egranger["c"] = {} # first critical value res[0][1] has a discrepancy starting at 4th decimal res[0] = [ -5.760696844656, -3.952043522638, -3.367006313729, -3.065831247948, ] # manually adjusted to have higher precision as in other cases res[0][1] = -3.952321293401682 res[1] = [ -5.781087068772, -4.367111915942, -3.783961136005, -3.483501524709, ] res[2] = [ -2.477444137366, -4.367111915942, -3.783961136005, -3.483501524709, ] res[3] = [ -5.778205811661, -4.735249216434, -4.152738973763, -3.852480848968, ] # trend = 'ctt' res = res_egranger["ctt"] = {} res[0] = [ -5.644431269946, -4.796038299708, -4.221469431008, -3.926472577178, ] res[1] = [-5.665691609506, -5.111158174219, -4.53317278104, -4.23601008516] res[2] = [-3.161462374828, -5.111158174219, -4.53317278104, -4.23601008516] res[3] = [ -5.657904558563, -5.406880189412, -4.826111619543, -4.527090164875, ] # The following for 'n' are only regression test numbers # trend = 'n' not allowed in egranger # trend = 'n' res = res_egranger["n"] = {} nan = np.nan # shortcut for table res[0] = [-3.7146175989071137, nan, nan, nan] res[1] = [-3.8199323012888384, nan, nan, nan] res[2] = [-1.6865000791270679, nan, nan, nan] res[3] = [-3.7991270451873675, nan, nan, nan] with pytest.warns(FutureWarning): # Ensure warning raised for nc rather than n coint(y[:, 0], y[:, 1], trend="nc", maxlag=4, autolag=None) for trend in ["c", "ct", "ctt", "n"]: res1 = {} res1[0] = coint(y[:, 0], y[:, 1], trend=trend, maxlag=4, autolag=None) res1[1] = coint( y[:, 0], y[:, 1:3], trend=trend, maxlag=4, autolag=None ) res1[2] = coint(y[:, 0], y[:, 2:], trend=trend, maxlag=4, autolag=None) res1[3] = coint(y[:, 0], y[:, 1:], trend=trend, maxlag=4, autolag=None) for i in range(4): res = res_egranger[trend] assert_allclose(res1[i][0], res[i][0], rtol=1e-11) r2 = res[i][1:] r1 = res1[i][2] assert_allclose(r1, r2, rtol=0, atol=6e-7) # use default autolag #4490 res1_0 = coint(y[:, 0], y[:, 1], trend="ct", maxlag=4) assert_allclose(res1_0[2], res_egranger["ct"][0][1:], rtol=0, atol=6e-7) # the following is just a regression test assert_allclose( res1_0[:2], [-13.992946638547112, 2.270898990540678e-27], rtol=1e-10, atol=1e-27, ) def test_coint_identical_series(): nobs = 200 scale_e = 1 np.random.seed(123) y = scale_e * np.random.randn(nobs) warnings.simplefilter("always", CollinearityWarning) with pytest.warns(CollinearityWarning): c = coint(y, y, trend="c", maxlag=0, autolag=None) assert_equal(c[1], 0.0) assert_(np.isneginf(c[0])) def test_coint_perfect_collinearity(): # test uses nearly perfect collinearity nobs = 200 scale_e = 1 np.random.seed(123) x = scale_e * np.random.randn(nobs, 2) y = 1 + x.sum(axis=1) + 1e-7 * np.random.randn(nobs) warnings.simplefilter("always", CollinearityWarning) with warnings.catch_warnings(record=True) as w: c = coint(y, x, trend="c", maxlag=0, autolag=None) assert_equal(c[1], 0.0) assert_(np.isneginf(c[0])) class TestGrangerCausality(object): def test_grangercausality(self): # some example data mdata = macrodata.load_pandas().data mdata = mdata[["realgdp", "realcons"]].values data = mdata.astype(float) data = np.diff(np.log(data), axis=0) # R: lmtest:grangertest r_result = [0.243097, 0.7844328, 195, 2] # f_test gr = grangercausalitytests(data[:, 1::-1], 2, verbose=False) assert_almost_equal(r_result, gr[2][0]["ssr_ftest"], decimal=7) assert_almost_equal( gr[2][0]["params_ftest"], gr[2][0]["ssr_ftest"], decimal=7 ) def test_grangercausality_single(self): mdata = macrodata.load_pandas().data mdata = mdata[["realgdp", "realcons"]].values data = mdata.astype(float) data = np.diff(np.log(data), axis=0) gr = grangercausalitytests(data[:, 1::-1], 2, verbose=False) gr2 = grangercausalitytests(data[:, 1::-1], [2], verbose=False) assert 1 in gr assert 1 not in gr2 assert_almost_equal( gr[2][0]["ssr_ftest"], gr2[2][0]["ssr_ftest"], decimal=7 ) assert_almost_equal( gr[2][0]["params_ftest"], gr2[2][0]["ssr_ftest"], decimal=7 ) def test_granger_fails_on_nobs_check(self, reset_randomstate): # Test that if maxlag is too large, Granger Test raises a clear error. x = np.random.rand(10, 2) grangercausalitytests(x, 2, verbose=False) # This should pass. with pytest.raises(ValueError): grangercausalitytests(x, 3, verbose=False) def test_granger_fails_on_finite_check(self, reset_randomstate): x = np.random.rand(1000, 2) x[500, 0] = np.nan x[750, 1] = np.inf with pytest.raises(ValueError, match="x contains NaN"): grangercausalitytests(x, 2) def test_granger_fails_on_zero_lag(self, reset_randomstate): x = np.random.rand(1000, 2) with pytest.raises( ValueError, match="maxlag must be a non-empty list containing only positive integers", ): grangercausalitytests(x, [0, 1, 2]) class TestKPSS: """ R-code ------ library(tseries) kpss.stat(x, "Level") kpss.stat(x, "Trend") In this context, x is the vector containing the macrodata['realgdp'] series. """ @classmethod def setup(cls): cls.data = macrodata.load_pandas() cls.x = cls.data.data["realgdp"].values def test_fail_nonvector_input(self, reset_randomstate): # should be fine with pytest.warns(InterpolationWarning): kpss(self.x, nlags="legacy") x = np.random.rand(20, 2) assert_raises(ValueError, kpss, x) def test_fail_unclear_hypothesis(self): # these should be fine, with pytest.warns(InterpolationWarning): kpss(self.x, "c", nlags="legacy") with pytest.warns(InterpolationWarning): kpss(self.x, "C", nlags="legacy") with pytest.warns(InterpolationWarning): kpss(self.x, "ct", nlags="legacy") with pytest.warns(InterpolationWarning): kpss(self.x, "CT", nlags="legacy") assert_raises( ValueError, kpss, self.x, "unclear hypothesis", nlags="legacy" ) def test_teststat(self): with pytest.warns(InterpolationWarning): kpss_stat, _, _, _ = kpss(self.x, "c", 3) assert_almost_equal(kpss_stat, 5.0169, DECIMAL_3) with pytest.warns(InterpolationWarning): kpss_stat, _, _, _ = kpss(self.x, "ct", 3) assert_almost_equal(kpss_stat, 1.1828, DECIMAL_3) def test_pval(self): with pytest.warns(InterpolationWarning): _, pval, _, _ = kpss(self.x, "c", 3) assert_equal(pval, 0.01) with pytest.warns(InterpolationWarning): _, pval, _, _ = kpss(self.x, "ct", 3) assert_equal(pval, 0.01) def test_store(self): with pytest.warns(InterpolationWarning): _, _, _, store = kpss(self.x, "c", 3, True) # assert attributes, and make sure they're correct assert_equal(store.nobs, len(self.x)) assert_equal(store.lags, 3) # test autolag function _kpss_autolag against SAS 9.3 def test_lags(self): # real GDP from macrodata data set with pytest.warns(InterpolationWarning): res = kpss(self.x, "c", nlags="auto") assert_equal(res[2], 9) # real interest rates from macrodata data set res = kpss(sunspots.load().data["SUNACTIVITY"], "c", nlags="auto") assert_equal(res[2], 7) # volumes from nile data set with pytest.warns(InterpolationWarning): res = kpss(nile.load().data["volume"], "c", nlags="auto") assert_equal(res[2], 5) # log-coinsurance from randhie data set with pytest.warns(InterpolationWarning): res = kpss(randhie.load().data["lncoins"], "ct", nlags="auto") assert_equal(res[2], 75) # in-vehicle time from modechoice data set with pytest.warns(InterpolationWarning): res = kpss(modechoice.load().data["invt"], "ct", nlags="auto") assert_equal(res[2], 18) def test_kpss_fails_on_nobs_check(self): # Test that if lags exceeds number of observations KPSS raises a # clear error # GH5925 nobs = len(self.x) msg = r"lags \({}\) must be < number of observations \({}\)".format( nobs, nobs ) with pytest.raises(ValueError, match=msg): kpss(self.x, "c", nlags=nobs) def test_kpss_autolags_does_not_assign_lags_equal_to_nobs(self): # Test that if *autolags* exceeds number of observations, we set # suitable lags # GH5925 base = np.array([0, 0, 0, 0, 0, 1, 1.0]) data_which_breaks_autolag = np.r_[np.tile(base, 297 // 7), [0, 0, 0]] kpss(data_which_breaks_autolag, nlags="auto") def test_legacy_lags(self): # Test legacy lags are the same with pytest.warns(InterpolationWarning): res = kpss(self.x, "c", nlags="legacy") assert_equal(res[2], 15) def test_unknown_lags(self): # Test legacy lags are the same with pytest.raises(ValueError): kpss(self.x, "c", nlags="unknown") def test_none(self): with pytest.warns(FutureWarning): kpss(self.x, nlags=None) class TestRUR: """ Simple implementation ------ Since an R implementation of the test cannot be found, the method is tested against a simple implementation using a for loop. In this context, x is the vector containing the macrodata['realgdp'] series. """ @classmethod def setup(cls): cls.data = macrodata.load_pandas() cls.x = cls.data.data["realgdp"].values # To be removed when range unit test gets an R implementation def simple_rur(self, x, store=False): x = array_like(x, "x") store = bool_like(store, "store") nobs = x.shape[0] # if m is not one, n != m * n if nobs != x.size: raise ValueError("x of shape {0} not understood".format(x.shape)) # Table from [1] has been replicated using 200,000 samples # Critical values for new n_obs values have been identified pvals = [0.01, 0.025, 0.05, 0.10, 0.90, 0.95] n = np.array( [25, 50, 100, 150, 200, 250, 500, 1000, 2000, 3000, 4000, 5000] ) crit = np.array( [ [0.6626, 0.8126, 0.9192, 1.0712, 2.4863, 2.7312], [0.7977, 0.9274, 1.0478, 1.1964, 2.6821, 2.9613], [0.907, 1.0243, 1.1412, 1.2888, 2.8317, 3.1393], [0.9543, 1.0768, 1.1869, 1.3294, 2.8915, 3.2049], [0.9833, 1.0984, 1.2101, 1.3494, 2.9308, 3.2482], [0.9982, 1.1137, 1.2242, 1.3632, 2.9571, 3.2482], [1.0494, 1.1643, 1.2712, 1.4076, 3.0207, 3.3584], [1.0846, 1.1959, 1.2988, 1.4344, 3.0653, 3.4073], [1.1121, 1.2200, 1.3230, 1.4556, 3.0948, 3.4439], [1.1204, 1.2295, 1.3318, 1.4656, 3.1054, 3.4632], [1.1309, 1.2347, 1.3318, 1.4693, 3.1165, 3.4717], [1.1377, 1.2402, 1.3408, 1.4729, 3.1252, 3.4807], ] ) # Interpolation for nobs inter_crit = np.zeros((1, crit.shape[1])) for i in range(crit.shape[1]): f = interp1d(n, crit[:, i]) inter_crit[0, i] = f(nobs) # Calculate RUR stat count = 0 max_p = x[0] min_p = x[0] for v in x[1:]: if v > max_p: max_p = v count = count + 1 if v < min_p: min_p = v count = count + 1 rur_stat = count / np.sqrt(len(x)) k = len(pvals) - 1 for i in range(len(pvals) - 1, -1, -1): if rur_stat < inter_crit[0, i]: k = i else: break p_value = pvals[k] warn_msg = """\ The test statistic is outside of the range of p-values available in the look-up table. The actual p-value is {direction} than the p-value returned. """ direction = "" if p_value == pvals[-1]: direction = "smaller" elif p_value == pvals[0]: direction = "larger" if direction: warnings.warn( warn_msg.format(direction=direction), InterpolationWarning ) crit_dict = { "10%": inter_crit[0, 3], "5%": inter_crit[0, 2], "2.5%": inter_crit[0, 1], "1%": inter_crit[0, 0], } if store: from statsmodels.stats.diagnostic import ResultsStore rstore = ResultsStore() rstore.nobs = nobs rstore.H0 = "The series is not stationary" rstore.HA = "The series is stationary" return rur_stat, p_value, crit_dict, rstore else: return rur_stat, p_value, crit_dict def test_fail_nonvector_input(self, reset_randomstate): with pytest.warns(InterpolationWarning): range_unit_root_test(self.x) x = np.random.rand(20, 2) assert_raises(ValueError, range_unit_root_test, x) def test_teststat(self): with pytest.warns(InterpolationWarning): rur_stat, _, _ = range_unit_root_test(self.x) simple_rur_stat, _, _ = self.simple_rur(self.x) assert_almost_equal(rur_stat, simple_rur_stat, DECIMAL_3) def test_pval(self): with pytest.warns(InterpolationWarning): _, pval, _ = range_unit_root_test(self.x) _, simple_pval, _ = self.simple_rur(self.x) assert_equal(pval, simple_pval) def test_store(self): with pytest.warns(InterpolationWarning): _, _, _, store = range_unit_root_test(self.x, True) # assert attributes, and make sure they're correct assert_equal(store.nobs, len(self.x)) def test_pandasacovf(): s = Series(lrange(1, 11)) assert_almost_equal(acovf(s, fft=False), acovf(s.values, fft=False)) def test_acovf2d(reset_randomstate): dta = sunspots.load_pandas().data dta.index = date_range(start="1700", end="2009", freq="A")[:309] del dta["YEAR"] res = acovf(dta, fft=False) assert_equal(res, acovf(dta.values, fft=False)) x = np.random.random((10, 2)) with pytest.raises(ValueError): acovf(x, fft=False) @pytest.mark.parametrize("demean", [True, False]) @pytest.mark.parametrize("adjusted", [True, False]) def test_acovf_fft_vs_convolution(demean, adjusted, reset_randomstate): q = np.random.normal(size=100) F1 = acovf(q, demean=demean, adjusted=adjusted, fft=True) F2 = acovf(q, demean=demean, adjusted=adjusted, fft=False) assert_almost_equal(F1, F2, decimal=7) @pytest.mark.parametrize("demean", [True, False]) @pytest.mark.parametrize("adjusted", [True, False]) def test_ccovf_fft_vs_convolution(demean, adjusted, reset_randomstate): x = np.random.normal(size=128) y = np.random.normal(size=128) F1 = ccovf(x, y, demean=demean, adjusted=adjusted, fft=False) F2 = ccovf(x, y, demean=demean, adjusted=adjusted, fft=True) assert_almost_equal(F1, F2, decimal=7) @pytest.mark.parametrize("demean", [True, False]) @pytest.mark.parametrize("adjusted", [True, False]) @pytest.mark.parametrize("fft", [True, False]) def test_compare_acovf_vs_ccovf(demean, adjusted, fft, reset_randomstate): x = np.random.normal(size=128) F1 = acovf(x, demean=demean, adjusted=adjusted, fft=fft) F2 = ccovf(x, x, demean=demean, adjusted=adjusted, fft=fft) assert_almost_equal(F1, F2, decimal=7) @pytest.mark.smoke @pytest.mark.slow def test_arma_order_select_ic(): # smoke test, assumes info-criteria are right from statsmodels.tsa.arima_process import arma_generate_sample arparams = np.array([0.75, -0.25]) maparams = np.array([0.65, 0.35]) arparams = np.r_[1, -arparams] maparam = np.r_[1, maparams] # FIXME: Never used nobs = 250 np.random.seed(2014) y = arma_generate_sample(arparams, maparams, nobs) res = arma_order_select_ic(y, ic=["aic", "bic"], trend="n") # regression tests in case we change algorithm to minic in sas aic_x = np.array( [ [764.36517643, 552.7342255, 484.29687843], [562.10924262, 485.5197969, 480.32858497], [507.04581344, 482.91065829, 481.91926034], [484.03995962, 482.14868032, 483.86378955], [481.8849479, 483.8377379, 485.83756612], ] ) bic_x = np.array( [ [767.88663735, 559.77714733, 494.86126118], [569.15216446, 496.08417966, 494.41442864], [517.61019619, 496.99650196, 499.52656493], [498.12580329, 499.75598491, 504.99255506], [499.49225249, 504.96650341, 510.48779255], ] ) aic = DataFrame(aic_x, index=lrange(5), columns=lrange(3)) bic = DataFrame(bic_x, index=lrange(5), columns=lrange(3)) assert_almost_equal(res.aic.values, aic.values, 5) assert_almost_equal(res.bic.values, bic.values, 5) assert_equal(res.aic_min_order, (1, 2)) assert_equal(res.bic_min_order, (1, 2)) assert_(res.aic.index.equals(aic.index)) assert_(res.aic.columns.equals(aic.columns)) assert_(res.bic.index.equals(bic.index)) assert_(res.bic.columns.equals(bic.columns)) index = pd.date_range("2000-1-1", freq="M", periods=len(y)) y_series = pd.Series(y, index=index) res_pd = arma_order_select_ic( y_series, max_ar=2, max_ma=1, ic=["aic", "bic"], trend="n" ) assert_almost_equal(res_pd.aic.values, aic.values[:3, :2], 5) assert_almost_equal(res_pd.bic.values, bic.values[:3, :2], 5) assert_equal(res_pd.aic_min_order, (2, 1)) assert_equal(res_pd.bic_min_order, (1, 1)) res = arma_order_select_ic(y, ic="aic", trend="n") assert_almost_equal(res.aic.values, aic.values, 5) assert_(res.aic.index.equals(aic.index)) assert_(res.aic.columns.equals(aic.columns)) assert_equal(res.aic_min_order, (1, 2)) def test_arma_order_select_ic_failure(): # this should trigger an SVD convergence failure, smoke test that it # returns, likely platform dependent failure... # looks like AR roots may be cancelling out for 4, 1? y = np.array( [ 0.86074377817203640006, 0.85316549067906921611, 0.87104653774363305363, 0.60692382068987393851, 0.69225941967301307667, 0.73336177248909339976, 0.03661329261479619179, 0.15693067239962379955, 0.12777403512447857437, -0.27531446294481976, -0.24198139631653581283, -0.23903317951236391359, -0.26000241325906497947, -0.21282920015519238288, -0.15943768324388354896, 0.25169301564268781179, 0.1762305709151877342, 0.12678133368791388857, 0.89755829086753169399, 0.82667068795350151511, ] ) import warnings with warnings.catch_warnings(): # catch a hessian inversion and convergence failure warning warnings.simplefilter("ignore") res = arma_order_select_ic(y) def test_acf_fft_dataframe(): # regression test #322 result = acf( sunspots.load_pandas().data[["SUNACTIVITY"]], fft=True, nlags=20 ) assert_equal(result.ndim, 1) def test_levinson_durbin_acov(): rho = 0.9 m = 20 acov = rho ** np.arange(200) sigma2_eps, ar, pacf, _, _ = levinson_durbin(acov, m, isacov=True) assert_allclose(sigma2_eps, 1 - rho ** 2) assert_allclose(ar, np.array([rho] + [0] * (m - 1)), atol=1e-8) assert_allclose(pacf, np.array([1, rho] + [0] * (m - 1)), atol=1e-8) @pytest.mark.parametrize("missing", ["conservative", "drop", "raise", "none"]) @pytest.mark.parametrize("fft", [False, True]) @pytest.mark.parametrize("demean", [True, False]) @pytest.mark.parametrize("adjusted", [True, False]) def test_acovf_nlags(acovf_data, adjusted, demean, fft, missing): full = acovf( acovf_data, adjusted=adjusted, demean=demean, fft=fft, missing=missing ) limited = acovf( acovf_data, adjusted=adjusted, demean=demean, fft=fft, missing=missing, nlag=10, ) assert_allclose(full[:11], limited) @pytest.mark.parametrize("missing", ["conservative", "drop"]) @pytest.mark.parametrize("fft", [False, True]) @pytest.mark.parametrize("demean", [True, False]) @pytest.mark.parametrize("adjusted", [True, False]) def test_acovf_nlags_missing(acovf_data, adjusted, demean, fft, missing): acovf_data = acovf_data.copy() acovf_data[1:3] = np.nan full = acovf( acovf_data, adjusted=adjusted, demean=demean, fft=fft, missing=missing ) limited = acovf( acovf_data, adjusted=adjusted, demean=demean, fft=fft, missing=missing, nlag=10, ) assert_allclose(full[:11], limited) def test_acovf_error(acovf_data): with pytest.raises(ValueError): acovf(acovf_data, nlag=250, fft=False) def test_pacf2acf_ar(): pacf = np.zeros(10) pacf[0] = 1 pacf[1] = 0.9 ar, acf = levinson_durbin_pacf(pacf) assert_allclose(acf, 0.9 ** np.arange(10.0)) assert_allclose(ar, pacf[1:], atol=1e-8) ar, acf = levinson_durbin_pacf(pacf, nlags=5) assert_allclose(acf, 0.9 ** np.arange(6.0)) assert_allclose(ar, pacf[1:6], atol=1e-8) def test_pacf2acf_levinson_durbin(): pacf = -(0.9 ** np.arange(11.0)) pacf[0] = 1 ar, acf = levinson_durbin_pacf(pacf) _, ar_ld, pacf_ld, _, _ = levinson_durbin(acf, 10, isacov=True) assert_allclose(ar, ar_ld, atol=1e-8) assert_allclose(pacf, pacf_ld, atol=1e-8) # From R, FitAR, PacfToAR ar_from_r = [ -4.1609, -9.2549, -14.4826, -17.6505, -17.5012, -14.2969, -9.5020, -4.9184, -1.7911, -0.3486, ] assert_allclose(ar, ar_from_r, atol=1e-4) def test_pacf2acf_errors(): pacf = -(0.9 ** np.arange(11.0)) pacf[0] = 1 with pytest.raises(ValueError): levinson_durbin_pacf(pacf, nlags=20) with pytest.raises(ValueError): levinson_durbin_pacf(pacf[1:]) with pytest.raises(ValueError): levinson_durbin_pacf(np.zeros(10)) with pytest.raises(ValueError): levinson_durbin_pacf(np.zeros((10, 2))) def test_pacf_burg(): rnd = np.random.RandomState(12345) e = rnd.randn(10001) y = e[1:] + 0.5 * e[:-1] pacf, sigma2 = pacf_burg(y, 10) yw_pacf = pacf_yw(y, 10) assert_allclose(pacf, yw_pacf, atol=5e-4) # Internal consistency check between pacf and sigma2 ye = y - y.mean() s2y = ye.dot(ye) / 10000 pacf[0] = 0 sigma2_direct = s2y * np.cumprod(1 - pacf ** 2) assert_allclose(sigma2, sigma2_direct, atol=1e-3) def test_pacf_burg_error(): with pytest.raises(ValueError): pacf_burg(np.empty((20, 2)), 10) with pytest.raises(ValueError): pacf_burg(np.empty(100), 101) def test_innovations_algo_brockwell_davis(): ma = -0.9 acovf = np.array([1 + ma ** 2, ma]) theta, sigma2 = innovations_algo(acovf, nobs=4) exp_theta = np.array([[0], [-0.4972], [-0.6606], [-0.7404]]) assert_allclose(theta, exp_theta, rtol=1e-4) assert_allclose(sigma2, [1.81, 1.3625, 1.2155, 1.1436], rtol=1e-4) theta, sigma2 = innovations_algo(acovf, nobs=500) assert_allclose(theta[-1, 0], ma) assert_allclose(sigma2[-1], 1.0) def test_innovations_algo_rtol(): ma = np.array([-0.9, 0.5]) acovf = np.array([1 + (ma ** 2).sum(), ma[0] + ma[1] * ma[0], ma[1]]) theta, sigma2 = innovations_algo(acovf, nobs=500) theta_2, sigma2_2 = innovations_algo(acovf, nobs=500, rtol=1e-8) assert_allclose(theta, theta_2) assert_allclose(sigma2, sigma2_2) def test_innovations_errors(): ma = -0.9 acovf = np.array([1 + ma ** 2, ma]) with pytest.raises(TypeError): innovations_algo(acovf, nobs=2.2) with pytest.raises(ValueError): innovations_algo(acovf, nobs=-1) with pytest.raises(ValueError): innovations_algo(np.empty((2, 2))) with pytest.raises(TypeError): innovations_algo(acovf, rtol="none") def test_innovations_filter_brockwell_davis(reset_randomstate): ma = -0.9 acovf = np.array([1 + ma ** 2, ma]) theta, _ = innovations_algo(acovf, nobs=4) e = np.random.randn(5) endog = e[1:] + ma * e[:-1] resid = innovations_filter(endog, theta) expected = [endog[0]] for i in range(1, 4): expected.append(endog[i] - theta[i, 0] * expected[-1]) expected = np.array(expected) assert_allclose(resid, expected) def test_innovations_filter_pandas(reset_randomstate): ma = np.array([-0.9, 0.5]) acovf = np.array([1 + (ma ** 2).sum(), ma[0] + ma[1] * ma[0], ma[1]]) theta, _ = innovations_algo(acovf, nobs=10) endog = np.random.randn(10) endog_pd = pd.Series(endog, index=pd.date_range("2000-01-01", periods=10)) resid = innovations_filter(endog, theta) resid_pd = innovations_filter(endog_pd, theta) assert_allclose(resid, resid_pd.values) assert_index_equal(endog_pd.index, resid_pd.index) def test_innovations_filter_errors(): ma = -0.9 acovf = np.array([1 + ma ** 2, ma]) theta, _ = innovations_algo(acovf, nobs=4) with pytest.raises(ValueError): innovations_filter(np.empty((2, 2)), theta) with pytest.raises(ValueError): innovations_filter(np.empty(4), theta[:-1]) with pytest.raises(ValueError): innovations_filter(pd.DataFrame(np.empty((1, 4))), theta) def test_innovations_algo_filter_kalman_filter(reset_randomstate): # Test the innovations algorithm and filter against the Kalman filter # for exact likelihood evaluation of an ARMA process ar_params = np.array([0.5]) ma_params = np.array([0.2]) # TODO could generalize to sigma2 != 1, if desired, after #5324 is merged # and there is a sigma2 argument to arma_acovf # (but maybe this is not really necessary for the point of this test) sigma2 = 1 endog = np.random.normal(size=10) # Innovations algorithm approach acovf = arma_acovf( np.r_[1, -ar_params], np.r_[1, ma_params], nobs=len(endog) ) theta, v = innovations_algo(acovf) u = innovations_filter(endog, theta) llf_obs = -0.5 * u ** 2 / (sigma2 * v) - 0.5 * np.log(2 * np.pi * v) # Kalman filter apparoach mod = SARIMAX(endog, order=(len(ar_params), 0, len(ma_params))) res = mod.filter(np.r_[ar_params, ma_params, sigma2]) # Test that the two approaches are identical atol = 1e-6 if PLATFORM_WIN else 0.0 assert_allclose(u, res.forecasts_error[0], rtol=1e-6, atol=atol) assert_allclose( theta[1:, 0], res.filter_results.kalman_gain[0, 0, :-1], atol=atol ) assert_allclose(llf_obs, res.llf_obs, atol=atol) def test_adfuller_short_series(reset_randomstate): y = np.random.standard_normal(7) res = adfuller(y, store=True) assert res[-1].maxlag == 1 y = np.random.standard_normal(2) with pytest.raises(ValueError, match="sample size is too short"): adfuller(y) y = np.random.standard_normal(3) with pytest.raises(ValueError, match="sample size is too short"): adfuller(y, regression="ct") def test_adfuller_maxlag_too_large(reset_randomstate): y = np.random.standard_normal(100) with pytest.raises(ValueError, match="maxlag must be less than"): adfuller(y, maxlag=51) class SetupZivotAndrews(object): # test directory cur_dir = CURR_DIR run_dir = os.path.join(cur_dir, "results") # use same file for testing failure modes fail_file = os.path.join(run_dir, "rgnp.csv") fail_mdl = np.asarray(pd.read_csv(fail_file)) class TestZivotAndrews(SetupZivotAndrews): # failure mode tests def test_fail_regression_type(self): with pytest.raises(ValueError): zivot_andrews(self.fail_mdl, regression="x") def test_fail_trim_value(self): with pytest.raises(ValueError): zivot_andrews(self.fail_mdl, trim=0.5) def test_fail_array_shape(self): with pytest.raises(ValueError): zivot_andrews(np.random.rand(50, 2)) def test_fail_autolag_type(self): with pytest.raises(ValueError): zivot_andrews(self.fail_mdl, autolag="None") @pytest.mark.parametrize("autolag", ["AIC", "aic", "Aic"]) def test_autolag_case_sensitivity(self, autolag): res = zivot_andrews(self.fail_mdl, autolag=autolag) assert res[3] == 1 # following tests compare results to R package urca.ur.za (1.13-0) def test_rgnp_case(self): res = zivot_andrews( self.fail_mdl, maxlag=8, regression="c", autolag=None ) assert_allclose( [res[0], res[1], res[4]], [-5.57615, 0.00312, 20], rtol=1e-3 ) def test_gnpdef_case(self): mdlfile = os.path.join(self.run_dir, "gnpdef.csv") mdl = np.asarray(pd.read_csv(mdlfile)) res = zivot_andrews(mdl, maxlag=8, regression="c", autolag="t-stat") assert_allclose( [res[0], res[1], res[3], res[4]], [-4.12155, 0.28024, 5, 40], rtol=1e-3, ) def test_stkprc_case(self): mdlfile = os.path.join(self.run_dir, "stkprc.csv") mdl = np.asarray(pd.read_csv(mdlfile)) res = zivot_andrews(mdl, maxlag=8, regression="ct", autolag="t-stat") assert_allclose( [res[0], res[1], res[3], res[4]], [-5.60689, 0.00894, 1, 65], rtol=1e-3, ) def test_rgnpq_case(self): mdlfile = os.path.join(self.run_dir, "rgnpq.csv") mdl = np.asarray(pd.read_csv(mdlfile)) res = zivot_andrews(mdl, maxlag=12, regression="t", autolag="t-stat") assert_allclose( [res[0], res[1], res[3], res[4]], [-3.02761, 0.63993, 12, 102], rtol=1e-3, ) def test_rand10000_case(self): mdlfile = os.path.join(self.run_dir, "rand10000.csv") mdl = np.asarray(pd.read_csv(mdlfile)) res = zivot_andrews(mdl, regression="c", autolag="t-stat") assert_allclose( [res[0], res[1], res[3], res[4]], [-3.48223, 0.69111, 25, 7071], rtol=1e-3, ) def test_acf_conservate_nanops(reset_randomstate): # GH 6729 e = np.random.standard_normal(100) for i in range(1, e.shape[0]): e[i] += 0.9 * e[i - 1] e[::7] = np.nan result = acf(e, missing="conservative", nlags=10, fft=False) resid = e - np.nanmean(e) expected = np.ones(11) nobs = e.shape[0] gamma0 = np.nansum(resid * resid) for i in range(1, 10 + 1): expected[i] = np.nansum(resid[i:] * resid[: nobs - i]) / gamma0 assert_allclose(result, expected, rtol=1e-4, atol=1e-4) def test_pacf_nlags_error(reset_randomstate): e = np.random.standard_normal(100) with pytest.raises(ValueError, match="Can only compute partial"): pacf(e, 50) def test_coint_auto_tstat(): rs = np.random.RandomState(3733696641) x = np.cumsum(rs.standard_normal(100)) y = np.cumsum(rs.standard_normal(100)) res = coint( x, y, trend="c", method="aeg", maxlag=0, autolag="t-stat", return_results=False, ) assert np.abs(res[0]) < 1.65 rs = np.random.RandomState(1) a = rs.random_sample(120) b = np.zeros_like(a) df1 = pd.DataFrame({"b": b, "a": a}) df2 = pd.DataFrame({"a": a, "b": b}) b = np.ones_like(a) df3 =
pd.DataFrame({"b": b, "a": a})
pandas.DataFrame
# -*- coding: utf-8 -*- import numpy as np import pandas as pd import matplotlib.pyplot as plt import statsmodels.api as sm from .. import paths def encode_dates(dt): '''Encodes datetime values as floats expressed in year fractions. >>> encode_dates(pd.date_range('2017-11-01', '2018-02-01', freq='M').values) Float64Index([2017.945205479452, 2018.0301369863014, 2018.1150684931506], dtype='float64') ''' return
pd.to_timedelta(dt)
pandas.to_timedelta
import numpy as np import pandas as pd import pytest from fairlens.metrics.distance import BinomialDistance, MeanDistance from fairlens.metrics.significance import binominal_proportion_p_value as bin_prop from fairlens.metrics.significance import bootstrap_binned_statistic as bootstrap_binned from fairlens.metrics.significance import bootstrap_statistic as bootstrap from fairlens.metrics.significance import brunner_munzel_test as bm_test from fairlens.metrics.significance import permutation_statistic as perm_stat from fairlens.metrics.significance import resampling_interval, resampling_p_value epsilon = 1e-5 df = pd.read_csv("datasets/compas.csv") target_attr = "RawScore" group1 = {"Ethnicity": ["Caucasian"]} group2 = {"Ethnicity": ["African-American"]} def test_binomial(): assert abs(bin_prop(0.2, 0.1, 10) - (1 - (0.9 ** 10 + 0.9 ** 9))) < epsilon assert BinomialDistance().p_value(pd.Series([1, 1]), pd.Series([0, 0])) == 0 assert BinomialDistance().p_value(pd.Series([1, 0]), pd.Series([1, 0])) == 1 assert BinomialDistance().p_value(pd.Series([1, 0, 1, 1]), pd.Series([1, 0, 1, 0])) == 0.625 def test_bootstrap(): assert bootstrap(pd.Series([1]), pd.Series([0]), MeanDistance().distance, 100).min() == 1 assert bootstrap(pd.Series(range(2)), pd.Series(range(2, 4)), MeanDistance().distance, 1000).max() == 3 def test_bootstrap_binned(): def distance(h_x, h_y): return np.linalg.norm(h_x - h_y, ord=1) assert bootstrap_binned(pd.Series([1, 3, 0]), pd.Series([1, 4, 3]), distance, 10000).max() == 12 def test_permutation(): assert perm_stat(pd.Series([1]), pd.Series([0]), MeanDistance().distance, 100).min() == 1 assert perm_stat(pd.Series([1, 1]), pd.Series([0, 0]), MeanDistance().distance, 1000).min() == 0 assert perm_stat(pd.Series(range(5)), pd.Series(range(5, 10)), MeanDistance().distance, 1000).max() == 5 def test_resampled_pvalue(): assert resampling_p_value(12, pd.Series([13, 11]), "two-sided") == 0.5 assert resampling_p_value(12, pd.Series([13, 11]), "greater") == 0.5 assert resampling_p_value(12, pd.Series([13, 11]), "less") == 0.5 assert resampling_p_value(12, pd.Series([15, 14, 13, 11]), "two-sided") == 0.75 assert resampling_p_value(12, pd.Series([15, 14, 13, 11]), "greater") == 0.75 assert resampling_p_value(12, pd.Series([15, 14, 13, 11]), "less") == 0.25 assert resampling_p_value(0, pd.Series([-2, -1, 0, 1]), "two-sided") == 1 assert resampling_p_value(0,
pd.Series([-2, -1, 0, 1])
pandas.Series
import os import scipy import pyccl as ccl import numpy as np import pylab as plt from numpy import linalg import pandas as pd import random from util import * mode = 'parallel_search' iter_index = 99 # 0-99 print("Index :", iter_index) export_dirfilename = "/mnt/zfsusers/sdatta/Desktop/cmb_expts/cmb_sdat/bin/cmb_export_parallel_withT_"+str(iter_index)+"_.csv" k_arr, oc_arr, h_arr, ob_arr, ns_arr, sigma8_arr, ts_arr = feature_space(10, 200, [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, ]) # 100*10e6 data points; 400 nodes, 250,000 per node count = 0; lent = int(len(oc_arr)*len(h_arr)*len(ob_arr)*len(ns_arr)*len(sigma8_arr)*len(ts_arr)) Output_list = [] # [pk_true, oc, h, b, sigma, n, t] if mode == 'parallel_search': simulations = [] for ts in ts_arr: for ns in ns_arr: for sigma8 in sigma8_arr: for ob in ob_arr: for h in h_arr: for oc in oc_arr: simulations.append([k_arr, oc, h, ob, ns, sigma8, ts]) simulations = simulations[int(iter_index*10000):int((iter_index+1)*10000)] for params in simulations: k_arr, oc, h, ob, ns, sigma8, ts = params try: pk_true = theoretical_Pk(k_arr, oc, h, ob, ns, sigma8, ts) Output_list.append([pk_true, k_arr, oc, h, ob, ns, sigma8, ts]) print("Iteration: ", count, "/", lent, "; Params: ", oc, h, ob, ns, sigma8, ts) if (count % 10) == 0: viz = pd.DataFrame(Output_list) viz.columns = ['pk_true', 'k', 'Omega_c', 'h', 'Omega_b', 'sigma8', 'n_s', 't'] viz.to_csv(export_dirfilename) except: print("Failed config: ", oc, h, ob, ns, sigma8, ts) continue count+=1 if mode == "combine_dataframe": fnms = ["./data/"+lir for lir in os.listdir("./data/")] pd_df = 1; df = pd.read_csv(fnms[0]) while pd_df < len(fnms): df = pd.concat([df,
pd.read_csv(fnms[pd_df])
pandas.read_csv
# Imports import streamlit as st import streamlit.components.v1 as components import pandas as pd import matplotlib.pyplot as plt import numpy as np import time import os.path # ML dependency imports from sklearn.preprocessing import StandardScaler, MinMaxScaler from sklearn.decomposition import PCA from sklearn.manifold import TSNE from sklearn.cluster import KMeans from sklearn.ensemble import RandomForestClassifier from sklearn.datasets import make_regression from sklearn.linear_model import LinearRegression, Lasso from sklearn.model_selection import train_test_split from streamlit.type_util import Key # Page Settings st.set_page_config(page_title="California Wildfire ML", page_icon="./img/fav.png", initial_sidebar_state="collapsed") #""" #-------------------------- #---- MACHINE LEARNING ---- #-------------------------- #""" def main(): print("IN MAIN") # If data has not been cleaned, then clean it if os.path.isfile("./data/clean/fire_data_clean.csv") == False: print("CLEANING FIRE") clean_fire() if os.path.isfile("./data/clean/drought_data_clean.csv") == False: print("CLEANING DROUGHT") clean_drought() if os.path.isfile("./data/clean/precip_data_clean.csv") == False: print("CLEANING RAIN") clean_percip() # # Init sidebar with header text # st.sidebar.header("Menu") # # Add URL for github repository # st.sidebar.write("[View on GitHub](https://github.com/josephchancey/ca-wildfire-ml)") def old_fire_dataset(): unclean_fire = pd.read_csv("./data/fire_data.csv") return unclean_fire def old_precip_dataset(): unclean_precip = pd.read_csv("./data/precip_data.csv") return unclean_precip def old_drought_dataset(): unclean_drought = pd.read_csv("./data/drought_data.csv") return unclean_drought def clean_fire(): if os.path.isfile("./data/clean/fire_data_clean.csv") == False: # import fire data csv fireFile = "./data/fire_data.csv" # read the file and store in a data frame fireData = pd.read_csv(fireFile) # remove extraneous columns fireData = fireData[["incident_id","incident_name","incident_county","incident_acres_burned", "incident_dateonly_created","incident_dateonly_extinguished"]] # rename columns fireData = fireData.rename(columns={"incident_id":"ID","incident_name":"Name","incident_county":"County", "incident_acres_burned":"AcresBurned","incident_dateonly_created":"Started", "incident_dateonly_extinguished":"Extinguished"}) # check for duplicates, then drop ID column fireData.drop_duplicates(subset=["ID"]) fireData = fireData[["Name","County","AcresBurned","Started","Extinguished"]] # create a column that contains the duration # first convert date columns to datetime fireData["Started"] = pd.to_datetime(fireData["Started"]) fireData["Extinguished"] = pd.to_datetime(fireData["Extinguished"]) # subtract the dates fireData["Duration"] = fireData["Extinguished"] - fireData["Started"] # convert duration to string and remove "days" fireData["Duration"] = fireData["Duration"].astype(str) fireData["Duration"] = fireData["Duration"].str.replace("days","") # replace NaT with NaN and convert back to float fireData["Duration"] = fireData["Duration"].replace(["NaT"],"NaN") fireData["Duration"] = fireData["Duration"].astype(float) # add one day to duration to capture fires that started and were extinguished in the same day fireData["Duration"] = fireData["Duration"] + 1 # create a column for year and filter for fires during or after 2013 fireData["Year"] = fireData["Started"].dt.year fireData = fireData.loc[(fireData["Year"]>=2013),:] # create a column to hold the year and month of the start date fireData["Date"] = fireData["Started"].apply(lambda x: x.strftime('%Y-%m')) fireData = fireData[["Date", "County", "Duration", "AcresBurned"]] # drop nulls fireData = fireData.dropna() # reset the index fireData.reset_index(inplace=True,drop=True) # export as csv fireData.to_csv("./data/clean/fire_data_clean.csv",index=False) return fireData else: # This prevents the cleaning from being ran each time this function is called, checks if cleaning is done already fireData = pd.read_csv("./data/clean/fire_data_clean.csv") return fireData def clean_percip(): if os.path.isfile("./data/clean/precip_data_clean.csv") == False: # import precipitation data csv precipFile = "./data/precip_data.csv" # read the file and store in a data frame precipData = pd.read_csv(precipFile) # remove extraneous columns precipData = precipData[["Date","Location","Value"]] # rename columns precipData = precipData.rename(columns = {"Location":"County","Value":"Precip"}) # remove "county" from county column to be consistent with other datasets precipData["County"] = precipData["County"].astype(str) precipData["County"] = precipData["County"].str.replace(" County","") # convert date column precipData["Date"] = pd.to_datetime(precipData["Date"].astype(str), format='%Y%m') # create a column for year and filter for data during or after 2013 precipData["Year"] = precipData["Date"].dt.year precipData = precipData.loc[(precipData["Year"]>=2013),:] # drop the year column precipData = precipData[["Date","County","Precip"]] # edit the date column to match the format of the other datasets precipData["Date"] = precipData["Date"].apply(lambda x: x.strftime('%Y-%m')) precipData = precipData.dropna() precipData.reset_index(inplace=True,drop=True) # export as csv precipData.to_csv("./data/clean/precip_data_clean.csv",index=False) return precipData else: precipData = pd.read_csv("./data/clean/precip_data_clean.csv") return precipData def clean_drought(): if os.path.isfile("./data/clean/precip_data_clean.csv") == False: # import drought data csv droughtFile = "./data/drought_data.csv" # read the file and store in a dataframe droughtData = pd.read_csv(droughtFile) droughtData = droughtData[["ValidStart","County","None","D0","D1","D2", "D3","D4"]] # rename columns droughtData = droughtData.rename(columns={"ValidStart":"Date"}) # remove "county" from county column to be consistent with other datasets droughtData["County"] = droughtData["County"].astype(str) droughtData["County"] = droughtData["County"].str.replace(" County","") # edit the date column to match the format of the other datasets droughtData["Date"] = pd.to_datetime(droughtData["Date"]) droughtData["Date"] = droughtData["Date"].apply(lambda x: x.strftime('%Y-%m')) # drop nulls and reset the index droughtData = droughtData.dropna() droughtData.reset_index(inplace=True,drop=True) # group by date and county and average the drought levels of each week to obtain a monthly summary groupedDrought = droughtData.groupby(["Date","County"]) groupedDrought = groupedDrought.mean() # export as csv groupedDrought.to_csv("./data/clean/drought_data_clean.csv") return groupedDrought else: groupedDrought = pd.read_csv("./data/clean/drought_data_clean.csv") return groupedDrought def lin_model(): print("MODEL RAN") # import fire data fireFile = "./data/clean/fire_data_clean.csv" fireData = pd.read_csv(fireFile) droughtFile = "./data/clean/drought_data_clean.csv" droughtData = pd.read_csv(droughtFile) precipFile = "./data/clean/precip_data_clean.csv" precipData = pd.read_csv(precipFile) droughtMerged = pd.merge(droughtData, fireData, on = ["Date", "County"]) precipMerged = pd.merge(precipData, fireData, on = ["Date","County"]) masterMerge = pd.merge(droughtMerged, precipData, on = ["Date","County"]) droughtML = pd.get_dummies(droughtMerged) precipML = pd.get_dummies(precipMerged) masterML = pd.get_dummies(masterMerge) masterML.drop(columns='None', inplace=True) df = masterML X = df y = df["AcresBurned"] X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1) scaler = StandardScaler().fit(X_train) X_train_scaled = scaler.transform(X_train) X_test_scaled = scaler.transform(X_test) reg = LinearRegression().fit(X_train_scaled, y_train) reg_score_val = reg.score(X_test_scaled, y_test) return reg_score_val def lasso_model(): # import fire data fireFile = "./data/clean/fire_data_clean.csv" fireData = pd.read_csv(fireFile) droughtFile = "./data/clean/drought_data_clean.csv" droughtData = pd.read_csv(droughtFile) precipFile = "./data/clean/precip_data_clean.csv" precipData = pd.read_csv(precipFile) droughtMerged = pd.merge(droughtData, fireData, on = ["Date", "County"]) precipMerged = pd.merge(precipData, fireData, on = ["Date","County"]) masterMerge = pd.merge(droughtMerged, precipData, on = ["Date","County"]) masterML = pd.get_dummies(masterMerge) masterML.drop(columns='None', inplace=True) df = masterML X = df y = df["AcresBurned"] X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1) scaler = StandardScaler().fit(X_train) X_train_scaled = scaler.transform(X_train) X_test_scaled = scaler.transform(X_test) lasso = Lasso().fit(X_train_scaled, y_train) lasso_score_val = lasso.score(X_test_scaled, y_test) return lasso_score_val def random_forest(): # import fire data fireFile = "./data/clean/fire_data_clean.csv" fireData = pd.read_csv(fireFile) droughtFile = "./data/clean/drought_data_clean.csv" droughtData =
pd.read_csv(droughtFile)
pandas.read_csv
import datetime from numbers import Number from typing import Any import numpy as np import pandas as pd def convert_indices(df: pd.DataFrame): """ extract all indices to columns if all are not numerical and don't clash with existing column names """ if df.index.nlevels > 1: # always reset multi-index df.reset_index(inplace=True) elif df.index.dtype == np.dtype("int64"): # Int64Index -> RangeIndex if possible df.reset_index(inplace=True, drop=True) # col_names: List[str] = df.columns.values.tolist() # if ( # all([df.index.get_level_values(x).dtype != np.dtype("int64") for x in range(df.index.nlevels)]) # and len(set(df.index.names)) == len(df.index.names) # and not any([x in col_names for x in df.index.names]) # ): # df.reset_index(inplace=True) def downcast_numbers(data: pd.DataFrame): """Downcast numerics""" def downcast(ser: pd.Series) -> pd.Series: try: ser = pd.to_numeric(ser, downcast="signed") ser =
pd.to_numeric(ser, downcast="unsigned")
pandas.to_numeric
""" espnapi.py classes for scraping, parsing espn football api this includes fantasy and real nfl data Usage: import nflprojections.espnapi as espn season = 2020 week = 1 s = espn.Scraper(season=season) p = espn.Parser(season=season) data = s.playerstats(season) print(p.weekly_projections(data, week)) """ import json import logging from pathlib import Path from typing import List import numpy as np import pandas as pd import requests import nflnames from nflprojections import ProjectionSource class Scraper: """ Scrape ESPN API for football stats """ def __init__(self, season): """Creates Scraper instance""" self.season = season self._s = requests.Session() @property def api_url(self) -> str: return f"https://fantasy.espn.com/apis/v3/games/ffl/seasons/{self.season}/segments/0/leaguedefaults/3" @property def default_headers(self) -> dict: return { "authority": "fantasy.espn.com", "accept": "application/json", "x-fantasy-source": "kona", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36", "x-fantasy-platform": "kona-PROD-a9859dd5e813fa08e6946514bbb0c3f795a4ea23", "dnt": "1", "sec-fetch-site": "same-origin", "sec-fetch-mode": "cors", "sec-fetch-dest": "empty", "referer": "https://fantasy.espn.com/football/players/projections", "accept-language": "en-US,en;q=0.9,ar;q=0.8", "if-none-match": "W/^\\^008956866aeb5b199ec8612a9e7576ed7^\\^", "x-fantasy-filter": json.dumps(self.xff) } @property def default_params(self) -> dict: return {"view": "kona_player_info"} @property def xff(self) -> dict: """Default x-fantasy-filter""" return { "players": { "limit": 1500, "sortDraftRanks": { "sortPriority": 100, "sortAsc": True, "value": "PPR", } } } def get_json(self, url, headers: dict = None, params: dict = None) -> dict: """Gets json response""" r = self._s.get(url, headers=headers, params=params) return r.json() def projections(self) -> dict: """Gets all ESPN player projections """ return self.get_json(self.api_url, headers=self.default_headers, params=self.default_params) class Parser: """ Parse ESPN API for football stats """ POSITION_MAP = { 1: "QB", 2: "RB", 3: "WR", 4: "TE", 5: "K", 16: "DST", } STAT_MAP = { "0": "pass_att", "1": "pass_cmp", "3": "pass_yds", "4": "pass_td", "19": "pass_tpc", "20": "pass_int", "23": "rush_att", "24": "rush_yds", "25": "rush_td", "26": "rush_tpc", "53": "rec_rec", "42": "rec_yds", "43": "rec_td", "44": "rec_tpc", "58": "rec_tar", "72": "fum_lost", "74": "madeFieldGoalsFrom50Plus", "77": "madeFieldGoalsFrom40To49", "80": "madeFieldGoalsFromUnder40", "85": "missedFieldGoals", "86": "madeExtraPoints", "88": "missedExtraPoints", "89": "defensive0PointsAllowed", "90": "defensive1To6PointsAllowed", "91": "defensive7To13PointsAllowed", "92": "defensive14To17PointsAllowed", "93": "defensiveBlockedKickForTouchdowns", "95": "defensiveInterceptions", "96": "defensiveFumbles", "97": "defensiveBlockedKicks", "98": "defensiveSafeties", "99": "defensiveSacks", "101": "kickoffReturnTouchdown", "102": "puntReturnTouchdown", "103": "fumbleReturnTouchdown", "104": "interceptionReturnTouchdown", "123": "defensive28To34PointsAllowed", "124": "defensive35To45PointsAllowed", "129": "defensive100To199YardsAllowed", "130": "defensive200To299YardsAllowed", "132": "defensive350To399YardsAllowed", "133": "defensive400To449YardsAllowed", "134": "defensive450To499YardsAllowed", "135": "defensive500To549YardsAllowed", "136": "defensiveOver550YardsAllowed", } TEAM_MAP = { "ARI": 22, "ATL": 1, "BAL": 33, "BUF": 2, "CAR": 29, "CHI": 3, "CIN": 4, "CLE": 5, "DAL": 6, "DEN": 7, "DET": 8, "GB": 9, "HOU": 34, "IND": 11, "JAC": 30, "JAX": 30, "KC": 12, "LAC": 24, "LA": 14, "LAR": 14, "MIA": 15, "MIN": 16, "NE": 17, "NO": 18, "NYG": 19, "NYJ": 20, "OAK": 13, "PHI": 21, "PIT": 23, "SEA": 26, "SF": 25, "TB": 27, "TEN": 10, "WAS": 28, "WSH": 28, "FA": 0, } TEAM_ID_MAP = {v: k for k, v in TEAM_MAP.items()} def __init__(self, season, week): """ """ self.season = season self.week = week def _find_projection(self, stats: List[dict]) -> dict: """Simplified way to find projection or result""" mapping = { "seasonId": self.season, "scoringPeriodId": self.week, "statSourceId": 1, "statSplitTypeId": 0 } for item in stats: if {k: item[k] for k in mapping} == mapping: return item def _parse_stats(self, stat: dict) -> dict: """Parses stats dict""" return { self.STAT_MAP.get(str(k)): float(v) for k, v in stat.items() if str(k) in self.STAT_MAP } def espn_team(self, team_code: str = None, team_id: int = None) -> str: """Returns team_id given code or team_code given team_id""" if team_code: return self.TEAM_MAP.get(team_code) return self.TEAM_ID_MAP.get(int(team_id)) def projections(self, content: dict) -> List[dict]: """Parses the seasonal projections Args: content(dict): parsed JSON Returns: list: of dict """ proj = [] top_level_keys = { "id": "source_player_id", "fullName": "source_player_name", "proTeamId": "source_team_id", } for player in [item["player"] for item in content["players"]]: p = { top_level_keys.get(k): v for k, v in player.items() if k in top_level_keys } p["source_team_code"] = self.espn_team(team_id=p.get("source_team_id", 0)) p['source_player_position'] = self.POSITION_MAP.get(int(player['defaultPositionId'])) # loop through player stats to find projections stat = self._find_projection(player["stats"]) if stat: p["source_player_projection"] = stat["appliedTotal"] proj.append(dict(**p, **self._parse_stats(stat["stats"]))) else: p["source_player_projection"] = None proj.append(p) return proj def weekly_projections(self, content: dict) -> List[dict]: """Parses the weekly projections Args: content(dict): parsed JSON week(int): 1-17 Returns: list: of dict """ proj = [] top_level_keys = { "id": "source_player_id", "fullName": "source_player_name", "proTeamId": "source_team_id", "defaultPositionId": "source_player_position" } for player in [item["player"] for item in content["players"]]: p = { top_level_keys.get(k): v for k, v in player.items() if k in top_level_keys } p["source_team_code"] = self.espn_team(team_id=p.get("source_team_id", 0)) p["source_player_position"] = self.POSITION_MAP.get(p["source_player_position"], "UNK") # loop through player stats to find weekly projections try: stat = self._find_projection(player["stats"]) if stat: p["source_player_projection"] = stat["appliedTotal"] proj.append(dict(**p, **self._parse_stats(stat["stats"]))) else: p["source_player_projection"] = None proj.append(p) except KeyError: proj.append(p) return
pd.DataFrame(proj)
pandas.DataFrame
import numpy as np import matplotlib.pyplot as plt import matplotlib.patheffects as mpe import utils import pandas as pd from sklearn.metrics import precision_score, recall_score, roc_auc_score, label_ranking_average_precision_score from sklearn.metrics import label_ranking_loss, confusion_matrix, average_precision_score, auc, precision_recall_curve import statsmodels.api as sm from statsmodels.distributions.empirical_distribution import ECDF from tqdm import tqdm ##Set plotting parameters: utils.set_mpl_params() fp_names = utils.getNames() fp_probas = dict() fp_average_precisions = dict() for fp in fp_names: fp_probas[fp] = np.load('./processed_data/fp_comparison/'+fp+'_probas.npy', allow_pickle=True) fp_average_precisions[fp] = [] test_labels = np.load('processed_data/fp_comparison/test_labels.npy', allow_pickle=True) aves = np.load('processed_data/fp_comparison/aves.npy', allow_pickle=True) targets = np.load('processed_data/fp_comparison/targets.npy', allow_pickle=True) cutoffs = np.load('processed_data/fp_comparison/cutoffs.npy', allow_pickle=True) sizes = np.load('processed_data/fp_comparison/sizes.npy', allow_pickle=True) average_precisions = list() df =
pd.DataFrame()
pandas.DataFrame
import sys import numpy as np import scipy as sp import pandas as pd from matplotlib import pyplot as plt from matplotlib.collections import BrokenBarHCollection, PathCollection, LineCollection import seaborn as sns from . import genome import logging log = logging.getLogger(__name__) def scale_colors(minval, maxval, val, safe_colors = None): if safe_colors is None: ### Copied colors from cb.sequential.BuGn_7.colors safe_colors = [ [237, 248, 251], [204, 236, 230], [153, 216, 201], [102, 194, 164], [65, 174, 118], [35, 139, 69], [0, 88, 36] ] EPSILON = sys.float_info.epsilon # Smallest possible difference. i_f = float(val-minval) / float(maxval-minval) * (len(safe_colors)-1) i, f = int(i_f // 1), i_f % 1 # Split into whole & fractional parts. if f < EPSILON: ret_col = safe_colors[i] else: # Otherwise return a color within the range between them. (r1, g1, b1), (r2, g2, b2) = safe_colors[i], safe_colors[i+1] ret_col = int(r1 + f*(r2-r1)), int(g1 + f*(g2-g1)), int(b1 + f*(b2-b1)) return('#%02x%02x%02x' % (ret_col[0], ret_col[1], ret_col[2])) np_scale_colors = np.vectorize(scale_colors, excluded = ['safe_colors']) def smooth_sum(arr, n_times= 100): arr = np.array(arr, dtype = float) for e_ind in np.arange(n_times): arr = np.insert(arr, 0, arr[0]) arr = np.append(arr, arr[-1] ) arr = sp.signal.convolve( arr, [0.25,0.5,0.25], mode = "same" )[1:-1] # arr = sp.signal.convolve( arr, [0.25,0.5,0.25] )[1:-1] return(arr) """ Demonstrates plotting chromosome ideograms and genes (or any features, really) using matplotlib. 1) Assumes a file from UCSC's Table Browser from the "cytoBandIdeo" table, saved as "ideogram.txt". Lines look like this:: #chrom chromStart chromEnd name gieStain chr1 0 2300000 p36.33 gneg chr1 2300000 5300000 p36.32 gpos25 chr1 5300000 7100000 p36.31 gneg 2) Assumes another file, "ucsc_genes.txt", which is a BED format file downloaded from UCSC's Table Browser. This script will work with any BED-format file. """ # Here's the function that we'll call for each dataframe (once for chromosome # ideograms, once for genes). The rest of this script will be prepping data # for input to this function # def chromosome_collections(df, y_positions, height, **kwargs): """ Yields BrokenBarHCollection of features that can be added to an Axes object. Parameters ---------- df : pandas.DataFrame Must at least have columns ['chrom', 'start', 'end', 'color']. If no column 'width', it will be calculated from start/end. y_positions : dict Keys are chromosomes, values are y-value at which to anchor the BrokenBarHCollection height : float Height of each BrokenBarHCollection Additional kwargs are passed to BrokenBarHCollection """ del_width = False if 'width' not in df.columns: del_width = True df['width'] = df['end'] - df['start'] for chrom, group in df.groupby('chrom'): yrange = (y_positions[chrom], height) xranges = group[['start', 'width']].values yield(BrokenBarHCollection(xranges, yrange, facecolors=group['colors'], **kwargs)) if del_width: del df['width'] def line_collections(df, y_positions, line_plt_options = None, **kwargs): """ Yields BrokenBarHCollection of features that can be added to an Axes object. Parameters ---------- df : pandas.DataFrame Must at least have columns ['chrom', 'start'] y_positions : dict Keys are chromosomes, values are y-value at which to anchor the PathCollection Additional kwargs are passed to BrokenBarHCollection """ if line_plt_options is None: line_plt_options = {} if "smooth" not in line_plt_options.keys(): line_plt_options['smooth'] = 1 if "bins" not in line_plt_options.keys(): line_plt_options['bins'] = 100 # if "max_height" not in line_plt_options.keys(): for chrom, group in df.groupby('chrom'): if chrom in y_positions.keys(): inds = np.histogram( group['start'], bins = line_plt_options['bins']) # import ipdb; ipdb.set_trace() relative_y = inds[0]/float(np.sum(inds[0])) relative_y = smooth_sum( relative_y/np.max(relative_y), line_plt_options['smooth'] ) y_ind = y_positions[chrom] + (line_plt_options['max_height'] * relative_y) x_ind = inds[1][0:-1] yield(LineCollection([np.column_stack([x_ind, y_ind])], color=group['colors'], **kwargs)) class PlottingGenomeWide(object): """ Class function to different plotting options 1. Density plot for given positions. 2. Heatmap """ def __init__(self, ref_genome_class, min_chr_length = 1000000): assert type(ref_genome_class) is genome.GenomeClass, "provide a genome class" self.genome = ref_genome_class self.chr_info = pd.DataFrame({"chr":self.genome.chrs}) self.chr_info['len'] = self.genome.golden_chrlen self.chr_info['mid'] = (np.array(self.genome.golden_chrlen)/2).astype(int) self.chr_info['chr_ind_start'] = np.array(self.genome.chr_inds)[:-1] self.chr_info['chr_ind_end'] = np.array(self.genome.chr_inds)[1::] log.warn("filtering out genome scaffolds less than %s" % min_chr_length) self.chr_info = self.chr_info[self.chr_info['len'] > min_chr_length] def density_plot_chr_wide(self, bed_df, axs = None, plt_options = None): if plt_options is None: plt_options = {} if "chrom_height" not in plt_options.keys(): plt_options['chrom_height'] = 0.3 if "chrom_spacing" not in plt_options.keys(): plt_options['chrom_spacing'] = 0.4 if "gene_height" not in plt_options.keys(): plt_options['gene_height'] = 0.1 if "gene_padding" not in plt_options.keys(): plt_options['gene_padding'] = 0.1 if "density_bins" not in plt_options.keys(): plt_options['density_bins'] = 100 if "line_smooth" not in plt_options.keys(): plt_options['line_smooth'] = 10 if "line_kwargs" not in plt_options.keys(): plt_options['line_kwargs'] = {} if "line_height" not in plt_options.keys(): plt_options['line_height'] = plt_options['chrom_height'] - 0.01 plt_ylim_min = -plt_options['gene_height'] - plt_options['gene_padding'] - 0.01 plt_ylim_max = (len(self.genome.chrs) * plt_options['chrom_height']) + ((len(self.genome.chrs)-1) * plt_options['chrom_spacing']) + plt_options['gene_padding'] if axs is None: axs = plt.gca() # Keep track of the y positions for ideograms and genes for each chromosome, # and the center of each ideogram (which is where we'll put the ytick labels) ybase = 0 chrom_ybase = {} gene_ybase = {} chrom_centers = {} # Iterate in reverse so that items in the beginning of `chromosome_list` will # appear at the top of the plot for chrom in self.genome.chrs[::-1]: chrom_ybase[chrom] = ybase chrom_centers[chrom] = ybase + plt_options['chrom_height'] / 2. gene_ybase[chrom] = ybase - plt_options['gene_height'] - plt_options['gene_padding'] ybase += plt_options['chrom_height'] + plt_options['chrom_spacing'] chromosome_bars = pd.DataFrame({'chrom': self.genome.chrs, 'start': self.genome.centro_start, 'end': self.genome.centro_end, 'colors': "#525252"}) chromosome_bars = chromosome_bars.append(pd.DataFrame({'chrom': self.genome.chrs, 'start': 1, 'end': self.genome.golden_chrlen, 'colors': "#bdbdbd"})) for collection in chromosome_collections(chromosome_bars, chrom_ybase, plt_options['chrom_height'], alpha = 0.5): axs.add_collection(collection) for collection in line_collections(bed_df, chrom_ybase, { 'max_height': plt_options['line_height'], 'smooth': plt_options['line_smooth'], 'bins': plt_options['density_bins'] }, plt_options['line_kwargs']): axs.add_collection(collection) axs.set_yticks( chr_mid_ix[np.where(np.array(self.genome.golden_chrlen) > 1000000)[0]] ) axs.set_yticklabels( np.array(self.genome.chrs)[np.where(np.array(self.genome.golden_chrlen) > 1000000)[0]] ) # axs.set_yticks([chrom_centers[i] for i in self.genome.chrs] ) # axs.set_yticklabels(self.genome.chrs) axs.axis('tight') axs.set_ylim(plt_ylim_min, plt_ylim_max) # plt.xticks( size = 10 ) # plt.yticks( size = 10 ) return((axs, {'chrom_ybase': chrom_ybase, 'gene_ybase': gene_ybase, 'chrom_centers': chrom_centers})) def density_line_plot_positions(self, bed_pos_df, echr = "Chr1", axs = None, plt_options = {}): """ Function to generate a density plot for given a list of positions """ assert isinstance(bed_pos_df, pd.DataFrame), "provide a dataframe object" # if "lims" not in plt_options.keys(): # plt_options['lims'] = (np.nanmin(score_df.values), np.nanmax(score_df.values)) if "window_size" not in plt_options.keys(): plt_options['window_size'] = 100000 if "color" not in plt_options.keys(): plt_options['color'] = "#238443" if "nsmooth" not in plt_options.keys(): plt_options['nsmooth'] = None if "line" not in plt_options.keys(): plt_options['line'] = True if "ylabel" not in plt_options.keys(): plt_options['ylabel'] = "density" if "xlabel" not in plt_options.keys(): plt_options['xlabel'] = "windows" if axs is None: axs = plt.gca() data_window_density = pd.DataFrame(columns = ['chr', 'start', 'end', 'npos']) for ef_bin in self.genome.iter_positions_in_windows(bed_pos_df, window_size = plt_options['window_size']): data_window_density = pd.concat([data_window_density, pd.DataFrame({"chr": ef_bin[0], "start": ef_bin[1][0], "end": ef_bin[1][1], "npos": len(ef_bin[2]) }, index=[0]) ], ignore_index=True) if plt_options['nsmooth'] is not None: for ef_density in data_window_density.groupby('chr'): data_window_density.loc[ef_density[1].index, 'npos'] = smooth_sum(ef_density[1]['npos'], plt_options['nsmooth']) self.manhattan_plot( self.genome.get_genomewide_inds( data_window_density.iloc[:,[0,1,2]] ), data_window_density['npos'].values, plt_options=plt_options, axs = axs ) def generate_heatmap_genomewide(self, score_df, plt_options = {}, axs = None, **kwargs): """ Function to generate a genome wide heatmap plots chromosomes separately boxes for pericentromers """ assert isinstance(score_df, pd.DataFrame), "provide a dataframe object" if "lims" not in plt_options.keys(): plt_options['lims'] = (np.nanmin(score_df.values), np.nanmax(score_df.values)) if "xlabel" not in plt_options.keys(): plt_options['xlabel'] = "" if "ylabel" not in plt_options.keys(): plt_options['ylabel'] = "" if "title" not in plt_options.keys(): plt_options['title'] = "" if "cmap" not in plt_options.keys(): plt_options['cmap'] = "Reds" if "col_centro_line" not in plt_options.keys(): plt_options['col_centro_line'] = "#636363" if "col_chr_line" not in plt_options.keys(): plt_options['col_chr_line'] = "#525252" if axs is None: axs = plt.gca() score_df_new = score_df.copy() if pd.api.types.is_string_dtype( score_df_new.columns ): score_df_new.columns = self.genome.get_genomewide_inds( pd.Series(score_df_new.columns) ) else: score_df_new.columns = score_df_new.columns.astype(int) chr_end_ix = np.searchsorted( score_df_new.columns.values, self.genome.get_genomewide_inds( pd.DataFrame({"chr":self.genome.chrs, "pos": self.genome.golden_chrlen}) ), "right" )[0:-1] chr_mid_ix = np.searchsorted( score_df_new.columns.values, self.genome.get_genomewide_inds( pd.DataFrame({"chr":self.genome.chrs, "pos": (np.array(self.genome.golden_chrlen)/2).astype(int) }) ), "right" ) # score_df_new.insert(loc = int(e_chr_end_ix), column = "break" + str(e_chr_end_ix), value = np.nan) sns.heatmap( score_df_new, vmin=plt_options['lims'][0], vmax=plt_options['lims'][1], cmap=plt_options['cmap'], ax = axs, xticklabels=False, yticklabels=False, **kwargs # cbar_kws=dict(use_gridspec=False,location="bottom") ) axs.set_xlabel(plt_options['xlabel']) axs.set_ylabel(plt_options['ylabel']) axs.set_title(plt_options['title']) for e_chr_end_ix in chr_end_ix: axs.plot( (e_chr_end_ix, e_chr_end_ix), (0,score_df_new.shape[0]), '-', c = plt_options['col_chr_line'] ) if 'centro_mid' in self.genome.__dict__.keys(): centro_df = pd.DataFrame({"chr":self.genome.chrs, "pos": self.genome.centro_mid}) centro_df = centro_df[centro_df['pos'] > 0] chr_centro_mid_ix = np.searchsorted( score_df_new.columns.values, self.genome.get_genomewide_inds( centro_df ), "right" ) for e_chr_pos_ix in chr_centro_mid_ix: axs.plot( (e_chr_pos_ix, e_chr_pos_ix), (0,score_df_new.shape[0]), '--', c = plt_options['col_centro_line'] ) axs.set_xticks( chr_mid_ix[np.where(np.array(self.genome.golden_chrlen) > 1000000)[0]] ) axs.set_xticklabels( np.array(self.genome.chrs)[np.where(np.array(self.genome.golden_chrlen) > 1000000)[0]] ) return(axs) def manhattan_plot(self, x_ind, y_ind, plt_options = {}, axs = None, **kwargs): """ Plot a manhattan plot """ chr_info = self.chr_info.copy() if "color" not in plt_options.keys(): chr_info['color'] = pd.Series( ["#1f78b4", "#33a02c"] ).loc[np.arange(self.chr_info.shape[0]) % 2 ].values else: chr_info['color'] = plt_options['color'] if "ylim" not in plt_options.keys(): plt_options['ylim'] = (np.nanmin(y_ind), np.nanmax(y_ind)) if "ylabel" not in plt_options.keys(): plt_options['ylabel'] = "markers" if "gap" not in plt_options.keys(): plt_options['gap'] = 10000000 if "thres" not in plt_options.keys(): plt_options['thres'] = None if "size" not in plt_options.keys(): plt_options['size'] = 6 if 'line' not in plt_options.keys(): plt_options['line'] = False else: plt_options['line'] = True if axs is None: axs = plt.gca() for echr in chr_info.iterrows(): t_chr_ix = np.where((x_ind <= echr[1]['chr_ind_end'] ) & ( x_ind > echr[1]['chr_ind_start'] ))[0] if plt_options['line']: axs.plot(x_ind[t_chr_ix] + (plt_options['gap'] * echr[0]), y_ind[t_chr_ix], '-', color = echr[1]['color'], **kwargs) else: axs.scatter(x_ind[t_chr_ix] + (plt_options['gap'] * echr[0]), y_ind[t_chr_ix], s = plt_options['size'], c = echr[1]['color'], **kwargs) axs.set_xticks( chr_info['chr_ind_start'].values + (np.arange(chr_info.shape[0]) * plt_options['gap']) + chr_info['mid'].values ) axs.set_xticklabels( chr_info['chr'].values ) if plt_options['thres'] is not None: axs.plot((0, chr_info['chr_ind_end'].iloc[-1] + (plt_options['gap'] * (chr_info.shape[0] - 1)) ), (plt_options['thres'], plt_options['thres']), "--", color = "gray") axs.set_xlabel( plt_options['ylabel'] ) axs.set_ylim( plt_options['ylim'] ) axs.set_xlim( (0, chr_info['chr_ind_end'].iloc[-1] + (plt_options['gap'] * (chr_info.shape[0] - 1)) ) ) return(axs) def gwas_peaks_matrix(self, x_ind, y_ind, peak_heights = None, plt_options = {}, legend_scale = None, **kwargs): """ given indices for x axis and y axis. this function plots our favorite matrix plot """ chr_info = self.chr_info.copy() if "ylabel" not in plt_options.keys(): plt_options['ylabel'] = "" if "xlabel" not in plt_options.keys(): plt_options['xlabel'] = "" if "color" not in plt_options.keys(): plt_options['color'] = "#d8b365" if "cmap" not in plt_options.keys(): plt_options['cmap'] = (None, "BuGn") ### Chooses palettable.colorbrewer.sequential.BuGn_7.colors if "hist_color" not in plt_options.keys(): plt_options['hist_color'] = "#8c510a" if "alpha" not in plt_options.keys(): plt_options['alpha'] = 0.1 if "height" not in plt_options.keys(): plt_options['height'] = 12 # if "ratio" not in plt_options.keys(): plt_options['ratio'] = 1 if peak_heights is not None: if legend_scale is None: legend_scale = [np.min(peak_heights), np.max(peak_heights)] plt_options['color'] = np_scale_colors(legend_scale[0], legend_scale[1], peak_heights, safe_colors = plt_options['cmap'][0]) plt_limits = (1, chr_info['chr_ind_end'].values[-1]) if type(plt_options['color']) is np.ndarray: p = sns.jointplot( x = x_ind, y = y_ind, marginal_kws={"bins": np.linspace(1, plt_limits[1], 250),"color": plt_options['hist_color'] }, xlim = plt_limits, ylim = plt_limits, kind = "scatter", alpha = plt_options['alpha'], joint_kws={"s": 8}, height = plt_options['height'], ratio = plt_options['ratio'], **kwargs ) p.ax_joint.cla() #for i in range(len(x_ind)): p.ax_joint.scatter(x = x_ind, y = y_ind, c=plt_options['color'], s = 8) else: p = sns.jointplot( x = x_ind, y = y_ind, marginal_kws={"bins": np.linspace(1, plt_limits[1], 250),"color": plt_options['hist_color'] }, xlim = plt_limits, ylim = plt_limits, kind = "scatter", color = plt_options['color'], alpha = plt_options['alpha'], joint_kws={"s": 8}, height = plt_options['height'], ratio = plt_options['ratio'], **kwargs ) p.ax_marg_y.remove() p.ax_joint.plot( (0, 0) , plt_limits, '-', color = "gray") p.ax_joint.plot( plt_limits, (0, 0), '-', color = "gray") for echr in chr_info.iterrows(): p.ax_joint.plot( (echr[1]['chr_ind_end'] , echr[1]['chr_ind_end']) , plt_limits, '-', color = "gray") p.ax_joint.plot( plt_limits, (echr[1]['chr_ind_end'] , echr[1]['chr_ind_end']), '-', color = "gray") if 'centro_mid' in self.genome.__dict__.keys(): chr_info = pd.merge(chr_info,
pd.DataFrame({"chr": self.genome.chrs, "centro_mid": self.genome.centro_mid})
pandas.DataFrame
import sys sys.path.append('../../../') import apps.streetdownloader.pkg.streetview as streetview sys.path.append('/scratch/guxi/gsv_processor') import urlsigner import logging import subprocess import requests import multiprocessing import pandas as pd import numpy as np import sqlalchemy as sa from tqdm import tqdm import json import contextlib import traceback from collections import OrderedDict def work_pp(args): """ get lastest image in historial list which from google street view js api """ try: #print(args) panoids,err = streetview.panoids(lat= args['lat'], lon= args['long']) sorted_panoids=sorted(panoids,key=lambda a:a['year']*12+a['month']) #print(sorted_panoids) if len(sorted_panoids)>=1: ret=sorted_panoids[-1] else: # try backup solution url="https://maps.googleapis.com/maps/api/streetview/metadata?location={0},{1}&size=640x640&key=<KEY>".format(args['lat'],args['long']) url=urlsigner.sign_url(url,'') resp=requests.get(url, proxies=None) resp.encoding='utf-8' text=resp.text.encode('utf8') json_content=json.loads(text,object_pairs_hook=OrderedDict) #exit() if (json_content['status']=='OK'): y,m=json_content['date'].split('-') ret={'lat': args['lat'], 'panoid': json_content['pano_id'], 'month': int(m), 'lon': args['long'], 'year': int(y)} else: tqdm.write('{0} back up failed'.format(args['id'])) ret={'lat': args['lat'], 'panoid': err, 'month': -1, 'lon': args['long'], 'year': -1} ret['id']=args['id'] return pd.Series(ret) except: print('**************************') traceback.print_exc() return {'lat': args['lat'], 'panoid': 'unknown', 'month': -1, 'lon': args['long'], 'year': -1,'id':args['id']} def work(args): """ using for parrllel later """ return work_pp(args) def main(): print("db init....") #engine = sa.create_engine('mysql+pymysql://guxi:[email protected]:33061/image') print("db fetch....") #df = pd.read_sql_query("select * from err_loc;", engine) df=pd.DataFrame(
pd.read_csv('/scratch/guxi/gsv_processor/no_result_loc_info_all.csv')
pandas.read_csv
import settings import helpers import SimpleITK # conda install -c https://conda.anaconda.org/simpleitk SimpleITK import numpy import pandas import ntpath import cv2 # conda install -c https://conda.anaconda.org/menpo opencv3 import shutil import random import math import multiprocessing from bs4 import BeautifulSoup # conda install beautifulsoup4, coda install lxml import os import glob random.seed(1321) numpy.random.seed(1321) def find_mhd_file(patient_id): for subject_no in range(settings.LUNA_SUBSET_START_INDEX, 10): src_dir = settings.LUNA16_RAW_SRC_DIR + "subset" + str(subject_no) + "/" for src_path in glob.glob(src_dir + "*.mhd"): if patient_id in src_path: return src_path return None def load_lidc_xml(xml_path, agreement_threshold=0, only_patient=None, save_nodules=False): pos_lines = [] neg_lines = [] extended_lines = [] with open(xml_path, 'r') as xml_file: markup = xml_file.read() xml = BeautifulSoup(markup, features="xml") if xml.LidcReadMessage is None: return None, None, None patient_id = xml.LidcReadMessage.ResponseHeader.SeriesInstanceUid.text if only_patient is not None: if only_patient != patient_id: return None, None, None src_path = find_mhd_file(patient_id) if src_path is None: return None, None, None print(patient_id) itk_img = SimpleITK.ReadImage(src_path) img_array = SimpleITK.GetArrayFromImage(itk_img) num_z, height, width = img_array.shape #heightXwidth constitute the transverse plane origin = numpy.array(itk_img.GetOrigin()) # x,y,z Origin in world coordinates (mm) spacing = numpy.array(itk_img.GetSpacing()) # spacing of voxels in world coor. (mm) rescale = spacing / settings.TARGET_VOXEL_MM reading_sessions = xml.LidcReadMessage.find_all("readingSession") for reading_session in reading_sessions: # print("Sesion") nodules = reading_session.find_all("unblindedReadNodule") for nodule in nodules: nodule_id = nodule.noduleID.text # print(" ", nodule.noduleID) rois = nodule.find_all("roi") x_min = y_min = z_min = 999999 x_max = y_max = z_max = -999999 if len(rois) < 2: continue for roi in rois: z_pos = float(roi.imageZposition.text) z_min = min(z_min, z_pos) z_max = max(z_max, z_pos) edge_maps = roi.find_all("edgeMap") for edge_map in edge_maps: x = int(edge_map.xCoord.text) y = int(edge_map.yCoord.text) x_min = min(x_min, x) y_min = min(y_min, y) x_max = max(x_max, x) y_max = max(y_max, y) if x_max == x_min: continue if y_max == y_min: continue x_diameter = x_max - x_min x_center = x_min + x_diameter / 2 y_diameter = y_max - y_min y_center = y_min + y_diameter / 2 z_diameter = z_max - z_min z_center = z_min + z_diameter / 2 z_center -= origin[2] z_center /= spacing[2] x_center_perc = round(x_center / img_array.shape[2], 4) y_center_perc = round(y_center / img_array.shape[1], 4) z_center_perc = round(z_center / img_array.shape[0], 4) diameter = max(x_diameter , y_diameter) diameter_perc = round(max(x_diameter / img_array.shape[2], y_diameter / img_array.shape[1]), 4) if nodule.characteristics is None: print("!!!!Nodule:", nodule_id, " has no charecteristics") continue if nodule.characteristics.malignancy is None: print("!!!!Nodule:", nodule_id, " has no malignacy") continue malignacy = nodule.characteristics.malignancy.text sphericiy = nodule.characteristics.sphericity.text margin = nodule.characteristics.margin.text spiculation = nodule.characteristics.spiculation.text texture = nodule.characteristics.texture.text calcification = nodule.characteristics.calcification.text internal_structure = nodule.characteristics.internalStructure.text lobulation = nodule.characteristics.lobulation.text subtlety = nodule.characteristics.subtlety.text line = [nodule_id, x_center_perc, y_center_perc, z_center_perc, diameter_perc, malignacy] extended_line = [patient_id, nodule_id, x_center_perc, y_center_perc, z_center_perc, diameter_perc, malignacy, sphericiy, margin, spiculation, texture, calcification, internal_structure, lobulation, subtlety ] pos_lines.append(line) extended_lines.append(extended_line) nonNodules = reading_session.find_all("nonNodule") for nonNodule in nonNodules: z_center = float(nonNodule.imageZposition.text) z_center -= origin[2] z_center /= spacing[2] x_center = int(nonNodule.locus.xCoord.text) y_center = int(nonNodule.locus.yCoord.text) nodule_id = nonNodule.nonNoduleID.text x_center_perc = round(x_center / img_array.shape[2], 4) y_center_perc = round(y_center / img_array.shape[1], 4) z_center_perc = round(z_center / img_array.shape[0], 4) diameter_perc = round(max(6 / img_array.shape[2], 6 / img_array.shape[1]), 4) # print("Non nodule!", z_center) line = [nodule_id, x_center_perc, y_center_perc, z_center_perc, diameter_perc, 0] neg_lines.append(line) if agreement_threshold > 1: filtered_lines = [] for pos_line1 in pos_lines: id1 = pos_line1[0] x1 = pos_line1[1] y1 = pos_line1[2] z1 = pos_line1[3] d1 = pos_line1[4] overlaps = 0 for pos_line2 in pos_lines: id2 = pos_line2[0] if id1 == id2: continue x2 = pos_line2[1] y2 = pos_line2[2] z2 = pos_line2[3] d2 = pos_line1[4] dist = math.sqrt(math.pow(x1 - x2, 2) + math.pow(y1 - y2, 2) + math.pow(z1 - z2, 2)) if dist < d1 or dist < d2: overlaps += 1 if overlaps >= agreement_threshold: filtered_lines.append(pos_line1) # else: # print("Too few overlaps") pos_lines = filtered_lines df_annos = pandas.DataFrame(pos_lines, columns=["anno_index", "coord_x", "coord_y", "coord_z", "diameter", "malscore"]) df_annos.to_csv(settings.LUNA16_EXTRACTED_IMAGE_DIR + "_labels/" + patient_id + "_annos_pos_lidc.csv", index=False) df_neg_annos = pandas.DataFrame(neg_lines, columns=["anno_index", "coord_x", "coord_y", "coord_z", "diameter", "malscore"]) df_neg_annos.to_csv(settings.LUNA16_EXTRACTED_IMAGE_DIR + "_labels/" + patient_id + "_annos_neg_lidc.csv", index=False) # return [patient_id, spacing[0], spacing[1], spacing[2]] return pos_lines, neg_lines, extended_lines def normalize(image): MIN_BOUND = -1000.0 MAX_BOUND = 400.0 image = (image - MIN_BOUND) / (MAX_BOUND - MIN_BOUND) image[image > 1] = 1. image[image < 0] = 0. return image def process_image(src_path): patient_id = ntpath.basename(src_path).replace(".mhd", "") print("Patient: ", patient_id) dst_dir = settings.LUNA16_EXTRACTED_IMAGE_DIR + patient_id + "/" if not os.path.exists(dst_dir): os.mkdir(dst_dir) itk_img = SimpleITK.ReadImage(src_path) img_array = SimpleITK.GetArrayFromImage(itk_img) print("Img array: ", img_array.shape) origin = numpy.array(itk_img.GetOrigin()) # x,y,z Origin in world coordinates (mm) print("Origin (x,y,z): ", origin) direction = numpy.array(itk_img.GetDirection()) # x,y,z Origin in world coordinates (mm) print("Direction: ", direction) spacing = numpy.array(itk_img.GetSpacing()) # spacing of voxels in world coor. (mm) print("Spacing (x,y,z): ", spacing) rescale = spacing / settings.TARGET_VOXEL_MM print("Rescale: ", rescale) img_array = helpers.rescale_patient_images(img_array, spacing, settings.TARGET_VOXEL_MM) img_list = [] for i in range(img_array.shape[0]): img = img_array[i] seg_img, mask = helpers.get_segmented_lungs(img.copy()) img_list.append(seg_img) img = normalize(img) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_i.png", img * 255) cv2.imwrite(dst_dir + "img_" + str(i).rjust(4, '0') + "_m.png", mask * 255) def process_pos_annotations_patient(src_path, patient_id): df_node = pandas.read_csv("resources/luna16_annotations/annotations.csv") dst_dir = settings.LUNA16_EXTRACTED_IMAGE_DIR + "_labels/" if not os.path.exists(dst_dir): os.mkdir(dst_dir) dst_dir = dst_dir + patient_id + "/" if not os.path.exists(dst_dir): os.mkdir(dst_dir) itk_img = SimpleITK.ReadImage(src_path) img_array = SimpleITK.GetArrayFromImage(itk_img) print("Img array: ", img_array.shape) df_patient = df_node[df_node["seriesuid"] == patient_id] print("Annos: ", len(df_patient)) num_z, height, width = img_array.shape #heightXwidth constitute the transverse plane origin = numpy.array(itk_img.GetOrigin()) # x,y,z Origin in world coordinates (mm) print("Origin (x,y,z): ", origin) spacing = numpy.array(itk_img.GetSpacing()) # spacing of voxels in world coor. (mm) print("Spacing (x,y,z): ", spacing) rescale = spacing /settings.TARGET_VOXEL_MM print("Rescale: ", rescale) direction = numpy.array(itk_img.GetDirection()) # x,y,z Origin in world coordinates (mm) print("Direction: ", direction) flip_direction_x = False flip_direction_y = False if round(direction[0]) == -1: origin[0] *= -1 direction[0] = 1 flip_direction_x = True print("Swappint x origin") if round(direction[4]) == -1: origin[1] *= -1 direction[4] = 1 flip_direction_y = True print("Swappint y origin") print("Direction: ", direction) assert abs(sum(direction) - 3) < 0.01 patient_imgs = helpers.load_patient_images(patient_id, settings.LUNA16_EXTRACTED_IMAGE_DIR, "*_i.png") pos_annos = [] df_patient = df_node[df_node["seriesuid"] == patient_id] anno_index = 0 for index, annotation in df_patient.iterrows(): node_x = annotation["coordX"] if flip_direction_x: node_x *= -1 node_y = annotation["coordY"] if flip_direction_y: node_y *= -1 node_z = annotation["coordZ"] diam_mm = annotation["diameter_mm"] print("Node org (x,y,z,diam): ", (round(node_x, 2), round(node_y, 2), round(node_z, 2), round(diam_mm, 2))) center_float = numpy.array([node_x, node_y, node_z]) center_int = numpy.rint((center_float-origin) / spacing) # center_int = numpy.rint((center_float - origin) ) print("Node tra (x,y,z,diam): ", (center_int[0], center_int[1], center_int[2])) # center_int_rescaled = numpy.rint(((center_float-origin) / spacing) * rescale) center_float_rescaled = (center_float - origin) / settings.TARGET_VOXEL_MM center_float_percent = center_float_rescaled / patient_imgs.swapaxes(0, 2).shape # center_int = numpy.rint((center_float - origin) ) print("Node sca (x,y,z,diam): ", (center_float_rescaled[0], center_float_rescaled[1], center_float_rescaled[2])) diameter_pixels = diam_mm / settings.TARGET_VOXEL_MM diameter_percent = diameter_pixels / float(patient_imgs.shape[1]) pos_annos.append([anno_index, round(center_float_percent[0], 4), round(center_float_percent[1], 4), round(center_float_percent[2], 4), round(diameter_percent, 4), 1]) anno_index += 1 df_annos = pandas.DataFrame(pos_annos, columns=["anno_index", "coord_x", "coord_y", "coord_z", "diameter", "malscore"]) df_annos.to_csv(settings.LUNA16_EXTRACTED_IMAGE_DIR + "_labels/" + patient_id + "_annos_pos.csv", index=False) return [patient_id, spacing[0], spacing[1], spacing[2]] def process_excluded_annotations_patient(src_path, patient_id): df_node = pandas.read_csv("resources/luna16_annotations/annotations_excluded.csv") dst_dir = settings.LUNA16_EXTRACTED_IMAGE_DIR + "_labels/" if not os.path.exists(dst_dir): os.mkdir(dst_dir) dst_dir = dst_dir + patient_id + "/" if not os.path.exists(dst_dir): os.mkdir(dst_dir) # pos_annos_df = pandas.read_csv(TRAIN_DIR + "metadata/" + patient_id + "_annos_pos_lidc.csv") pos_annos_df = pandas.read_csv(settings.LUNA16_EXTRACTED_IMAGE_DIR + "_labels/" + patient_id + "_annos_pos.csv") pos_annos_manual = None manual_path = settings.EXTRA_DATA_DIR + "luna16_manual_labels/" + patient_id + ".csv" if os.path.exists(manual_path): pos_annos_manual = pandas.read_csv(manual_path) dmm = pos_annos_manual["dmm"] # check itk_img = SimpleITK.ReadImage(src_path) img_array = SimpleITK.GetArrayFromImage(itk_img) print("Img array: ", img_array.shape) df_patient = df_node[df_node["seriesuid"] == patient_id] print("Annos: ", len(df_patient)) num_z, height, width = img_array.shape #heightXwidth constitute the transverse plane origin = numpy.array(itk_img.GetOrigin()) # x,y,z Origin in world coordinates (mm) print("Origin (x,y,z): ", origin) spacing = numpy.array(itk_img.GetSpacing()) # spacing of voxels in world coor. (mm) print("Spacing (x,y,z): ", spacing) rescale = spacing / settings.TARGET_VOXEL_MM print("Rescale: ", rescale) direction = numpy.array(itk_img.GetDirection()) # x,y,z Origin in world coordinates (mm) print("Direction: ", direction) flip_direction_x = False flip_direction_y = False if round(direction[0]) == -1: origin[0] *= -1 direction[0] = 1 flip_direction_x = True print("Swappint x origin") if round(direction[4]) == -1: origin[1] *= -1 direction[4] = 1 flip_direction_y = True print("Swappint y origin") print("Direction: ", direction) assert abs(sum(direction) - 3) < 0.01 patient_imgs = helpers.load_patient_images(patient_id, settings.LUNA16_EXTRACTED_IMAGE_DIR, "*_i.png") neg_annos = [] df_patient = df_node[df_node["seriesuid"] == patient_id] anno_index = 0 for index, annotation in df_patient.iterrows(): node_x = annotation["coordX"] if flip_direction_x: node_x *= -1 node_y = annotation["coordY"] if flip_direction_y: node_y *= -1 node_z = annotation["coordZ"] center_float = numpy.array([node_x, node_y, node_z]) center_int = numpy.rint((center_float-origin) / spacing) center_float_rescaled = (center_float - origin) / settings.TARGET_VOXEL_MM center_float_percent = center_float_rescaled / patient_imgs.swapaxes(0, 2).shape # center_int = numpy.rint((center_float - origin) ) # print("Node sca (x,y,z,diam): ", (center_float_rescaled[0], center_float_rescaled[1], center_float_rescaled[2])) diameter_pixels = 6 / settings.TARGET_VOXEL_MM diameter_percent = diameter_pixels / float(patient_imgs.shape[1]) ok = True for index, row in pos_annos_df.iterrows(): pos_coord_x = row["coord_x"] * patient_imgs.shape[2] pos_coord_y = row["coord_y"] * patient_imgs.shape[1] pos_coord_z = row["coord_z"] * patient_imgs.shape[0] diameter = row["diameter"] * patient_imgs.shape[2] print((pos_coord_x, pos_coord_y, pos_coord_z)) print(center_float_rescaled) dist = math.sqrt(math.pow(pos_coord_x - center_float_rescaled[0], 2) + math.pow(pos_coord_y - center_float_rescaled[1], 2) + math.pow(pos_coord_z - center_float_rescaled[2], 2)) if dist < (diameter + 64): # make sure we have a big margin ok = False print("################### Too close", center_float_rescaled) break if pos_annos_manual is not None and ok: for index, row in pos_annos_manual.iterrows(): pos_coord_x = row["x"] * patient_imgs.shape[2] pos_coord_y = row["y"] * patient_imgs.shape[1] pos_coord_z = row["z"] * patient_imgs.shape[0] diameter = row["d"] * patient_imgs.shape[2] print((pos_coord_x, pos_coord_y, pos_coord_z)) print(center_float_rescaled) dist = math.sqrt(math.pow(pos_coord_x - center_float_rescaled[0], 2) + math.pow(pos_coord_y - center_float_rescaled[1], 2) + math.pow(pos_coord_z - center_float_rescaled[2], 2)) if dist < (diameter + 72): # make sure we have a big margin ok = False print("################### Too close", center_float_rescaled) break if not ok: continue neg_annos.append([anno_index, round(center_float_percent[0], 4), round(center_float_percent[1], 4), round(center_float_percent[2], 4), round(diameter_percent, 4), 1]) anno_index += 1 df_annos = pandas.DataFrame(neg_annos, columns=["anno_index", "coord_x", "coord_y", "coord_z", "diameter", "malscore"]) df_annos.to_csv(settings.LUNA16_EXTRACTED_IMAGE_DIR + "_labels/" + patient_id + "_annos_excluded.csv", index=False) return [patient_id, spacing[0], spacing[1], spacing[2]] def process_luna_candidates_patient(src_path, patient_id): dst_dir = settings.LUNA16_EXTRACTED_IMAGE_DIR + "/_labels/" img_dir = dst_dir + patient_id + "/" df_pos_annos = pandas.read_csv(dst_dir + patient_id + "_annos_pos_lidc.csv") if not os.path.exists(dst_dir): os.mkdir(dst_dir) pos_annos_manual = None manual_path = settings.EXTRA_DATA_DIR + "luna16_manual_labels/" + patient_id + ".csv" if os.path.exists(manual_path): pos_annos_manual = pandas.read_csv(manual_path) itk_img = SimpleITK.ReadImage(src_path) img_array = SimpleITK.GetArrayFromImage(itk_img) print("Img array: ", img_array.shape) print("Pos annos: ", len(df_pos_annos)) num_z, height, width = img_array.shape #heightXwidth constitute the transverse plane origin = numpy.array(itk_img.GetOrigin()) # x,y,z Origin in world coordinates (mm) print("Origin (x,y,z): ", origin) spacing = numpy.array(itk_img.GetSpacing()) # spacing of voxels in world coor. (mm) print("Spacing (x,y,z): ", spacing) rescale = spacing / settings.TARGET_VOXEL_MM print("Rescale: ", rescale) direction = numpy.array(itk_img.GetDirection()) # x,y,z Origin in world coordinates (mm) print("Direction: ", direction) flip_direction_x = False flip_direction_y = False if round(direction[0]) == -1: origin[0] *= -1 direction[0] = 1 flip_direction_x = True print("Swappint x origin") if round(direction[4]) == -1: origin[1] *= -1 direction[4] = 1 flip_direction_y = True print("Swappint y origin") print("Direction: ", direction) assert abs(sum(direction) - 3) < 0.01 src_df = pandas.read_csv("resources/luna16_annotations/" + "candidates_V2.csv") src_df = src_df[src_df["seriesuid"] == patient_id] src_df = src_df[src_df["class"] == 0] patient_imgs = helpers.load_patient_images(patient_id, settings.LUNA16_EXTRACTED_IMAGE_DIR, "*_i.png") candidate_list = [] for df_index, candiate_row in src_df.iterrows(): node_x = candiate_row["coordX"] if flip_direction_x: node_x *= -1 node_y = candiate_row["coordY"] if flip_direction_y: node_y *= -1 node_z = candiate_row["coordZ"] candidate_diameter = 6 # print("Node org (x,y,z,diam): ", (round(node_x, 2), round(node_y, 2), round(node_z, 2), round(candidate_diameter, 2))) center_float = numpy.array([node_x, node_y, node_z]) center_int = numpy.rint((center_float-origin) / spacing) # center_int = numpy.rint((center_float - origin) ) # print("Node tra (x,y,z,diam): ", (center_int[0], center_int[1], center_int[2])) # center_int_rescaled = numpy.rint(((center_float-origin) / spacing) * rescale) center_float_rescaled = (center_float - origin) / settings.TARGET_VOXEL_MM center_float_percent = center_float_rescaled / patient_imgs.swapaxes(0, 2).shape # center_int = numpy.rint((center_float - origin) ) # print("Node sca (x,y,z,diam): ", (center_float_rescaled[0], center_float_rescaled[1], center_float_rescaled[2])) coord_x = center_float_rescaled[0] coord_y = center_float_rescaled[1] coord_z = center_float_rescaled[2] ok = True for index, row in df_pos_annos.iterrows(): pos_coord_x = row["coord_x"] * patient_imgs.shape[2] pos_coord_y = row["coord_y"] * patient_imgs.shape[1] pos_coord_z = row["coord_z"] * patient_imgs.shape[0] diameter = row["diameter"] * patient_imgs.shape[2] dist = math.sqrt(math.pow(pos_coord_x - coord_x, 2) + math.pow(pos_coord_y - coord_y, 2) + math.pow(pos_coord_z - coord_z, 2)) if dist < (diameter + 64): # make sure we have a big margin ok = False print("################### Too close", (coord_x, coord_y, coord_z)) break if pos_annos_manual is not None and ok: for index, row in pos_annos_manual.iterrows(): pos_coord_x = row["x"] * patient_imgs.shape[2] pos_coord_y = row["y"] * patient_imgs.shape[1] pos_coord_z = row["z"] * patient_imgs.shape[0] diameter = row["d"] * patient_imgs.shape[2] print((pos_coord_x, pos_coord_y, pos_coord_z)) print(center_float_rescaled) dist = math.sqrt(math.pow(pos_coord_x - center_float_rescaled[0], 2) + math.pow(pos_coord_y - center_float_rescaled[1], 2) + math.pow(pos_coord_z - center_float_rescaled[2], 2)) if dist < (diameter + 72): # make sure we have a big margin ok = False print("################### Too close", center_float_rescaled) break if not ok: continue candidate_list.append([len(candidate_list), round(center_float_percent[0], 4), round(center_float_percent[1], 4), round(center_float_percent[2], 4), round(candidate_diameter / patient_imgs.shape[0], 4), 0]) df_candidates = pandas.DataFrame(candidate_list, columns=["anno_index", "coord_x", "coord_y", "coord_z", "diameter", "malscore"]) df_candidates.to_csv(dst_dir + patient_id + "_candidates_luna.csv", index=False) def process_auto_candidates_patient(src_path, patient_id, sample_count=1000, candidate_type="white"): dst_dir = settings.LUNA16_EXTRACTED_IMAGE_DIR + "/_labels/" img_dir = settings.LUNA16_EXTRACTED_IMAGE_DIR + patient_id + "/" df_pos_annos = pandas.read_csv(dst_dir + patient_id + "_annos_pos_lidc.csv") pos_annos_manual = None manual_path = settings.EXTRA_DATA_DIR + "luna16_manual_labels/" + patient_id + ".csv" if os.path.exists(manual_path): pos_annos_manual = pandas.read_csv(manual_path) if not os.path.exists(dst_dir): os.mkdir(dst_dir) itk_img = SimpleITK.ReadImage(src_path) img_array = SimpleITK.GetArrayFromImage(itk_img) print("Img array: ", img_array.shape) print("Pos annos: ", len(df_pos_annos)) num_z, height, width = img_array.shape #heightXwidth constitute the transverse plane origin = numpy.array(itk_img.GetOrigin()) # x,y,z Origin in world coordinates (mm) print("Origin (x,y,z): ", origin) spacing = numpy.array(itk_img.GetSpacing()) # spacing of voxels in world coor. (mm) print("Spacing (x,y,z): ", spacing) rescale = spacing / settings.TARGET_VOXEL_MM print("Rescale: ", rescale) if candidate_type == "white": wildcard = "*_c.png" else: wildcard = "*_m.png" src_files = glob.glob(img_dir + wildcard) src_files.sort() src_candidate_maps = [cv2.imread(src_file, cv2.IMREAD_GRAYSCALE) for src_file in src_files] candidate_list = [] tries = 0 while len(candidate_list) < sample_count and tries < 10000: tries += 1 coord_z = int(numpy.random.normal(len(src_files) / 2, len(src_files) / 6)) coord_z = max(coord_z, 0) coord_z = min(coord_z, len(src_files) - 1) candidate_map = src_candidate_maps[coord_z] if candidate_type == "edge": candidate_map = cv2.Canny(candidate_map.copy(), 100, 200) non_zero_indices = numpy.nonzero(candidate_map) if len(non_zero_indices[0]) == 0: continue nonzero_index = random.randint(0, len(non_zero_indices[0]) - 1) coord_y = non_zero_indices[0][nonzero_index] coord_x = non_zero_indices[1][nonzero_index] ok = True candidate_diameter = 6 for index, row in df_pos_annos.iterrows(): pos_coord_x = row["coord_x"] * src_candidate_maps[0].shape[1] pos_coord_y = row["coord_y"] * src_candidate_maps[0].shape[0] pos_coord_z = row["coord_z"] * len(src_files) diameter = row["diameter"] * src_candidate_maps[0].shape[1] dist = math.sqrt(math.pow(pos_coord_x - coord_x, 2) + math.pow(pos_coord_y - coord_y, 2) + math.pow(pos_coord_z - coord_z, 2)) if dist < (diameter + 48): # make sure we have a big margin ok = False print("# Too close", (coord_x, coord_y, coord_z)) break if pos_annos_manual is not None: for index, row in pos_annos_manual.iterrows(): pos_coord_x = row["x"] * src_candidate_maps[0].shape[1] pos_coord_y = row["y"] * src_candidate_maps[0].shape[0] pos_coord_z = row["z"] * len(src_files) diameter = row["d"] * src_candidate_maps[0].shape[1] # print((pos_coord_x, pos_coord_y, pos_coord_z)) # print(center_float_rescaled) dist = math.sqrt(math.pow(pos_coord_x - coord_x, 2) + math.pow(pos_coord_y - coord_y, 2) + math.pow(pos_coord_z - coord_z, 2)) if dist < (diameter + 72): # make sure we have a big margin ok = False print("#Too close", (coord_x, coord_y, coord_z)) break if not ok: continue perc_x = round(coord_x / src_candidate_maps[coord_z].shape[1], 4) perc_y = round(coord_y / src_candidate_maps[coord_z].shape[0], 4) perc_z = round(coord_z / len(src_files), 4) candidate_list.append([len(candidate_list), perc_x, perc_y, perc_z, round(candidate_diameter / src_candidate_maps[coord_z].shape[1], 4), 0]) if tries > 9999: print("****** WARING!! TOO MANY TRIES ************************************") df_candidates = pandas.DataFrame(candidate_list, columns=["anno_index", "coord_x", "coord_y", "coord_z", "diameter", "malscore"]) df_candidates.to_csv(dst_dir + patient_id + "_candidates_" + candidate_type + ".csv", index=False) def process_images(delete_existing=False, only_process_patient=None): if delete_existing and os.path.exists(settings.LUNA16_EXTRACTED_IMAGE_DIR): print("Removing old stuff..") if os.path.exists(settings.LUNA16_EXTRACTED_IMAGE_DIR): shutil.rmtree(settings.LUNA16_EXTRACTED_IMAGE_DIR) if not os.path.exists(settings.LUNA16_EXTRACTED_IMAGE_DIR): os.mkdir(settings.LUNA16_EXTRACTED_IMAGE_DIR) os.mkdir(settings.LUNA16_EXTRACTED_IMAGE_DIR + "_labels/") for subject_no in range(settings.LUNA_SUBSET_START_INDEX, 10): src_dir = settings.LUNA16_RAW_SRC_DIR + "subset" + str(subject_no) + "/" src_paths = glob.glob(src_dir + "*.mhd") if only_process_patient is None and True: pool = multiprocessing.Pool(settings.WORKER_POOL_SIZE) pool.map(process_image, src_paths) else: for src_path in src_paths: print(src_path) if only_process_patient is not None: if only_process_patient not in src_path: continue process_image(src_path) def process_pos_annotations_patient2(): candidate_index = 0 only_patient = "197063290812663596858124411210" only_patient = None for subject_no in range(settings.LUNA_SUBSET_START_INDEX, 10): src_dir = settings.LUNA16_RAW_SRC_DIR + "subset" + str(subject_no) + "/" for src_path in glob.glob(src_dir + "*.mhd"): if only_patient is not None and only_patient not in src_path: continue patient_id = ntpath.basename(src_path).replace(".mhd", "") print(candidate_index, " patient: ", patient_id) process_pos_annotations_patient(src_path, patient_id) candidate_index += 1 def process_excluded_annotations_patients(only_patient=None): candidate_index = 0 for subject_no in range(settings.LUNA_SUBSET_START_INDEX, 10): src_dir = settings.LUNA16_RAW_SRC_DIR + "subset" + str(subject_no) + "/" for src_path in glob.glob(src_dir + "*.mhd"): if only_patient is not None and only_patient not in src_path: continue patient_id = ntpath.basename(src_path).replace(".mhd", "") print(candidate_index, " patient: ", patient_id) process_excluded_annotations_patient(src_path, patient_id) candidate_index += 1 def process_auto_candidates_patients(): for subject_no in range(settings.LUNA_SUBSET_START_INDEX, 10): src_dir = settings.LUNA16_RAW_SRC_DIR + "subset" + str(subject_no) + "/" for patient_index, src_path in enumerate(glob.glob(src_dir + "*.mhd")): # if not "100621383016233746780170740405" in src_path: # continue patient_id = ntpath.basename(src_path).replace(".mhd", "") print("Patient: ", patient_index, " ", patient_id) # process_auto_candidates_patient(src_path, patient_id, sample_count=500, candidate_type="white") process_auto_candidates_patient(src_path, patient_id, sample_count=200, candidate_type="edge") def process_luna_candidates_patients(only_patient_id=None): for subject_no in range(settings.LUNA_SUBSET_START_INDEX, 10): src_dir = settings.LUNA16_RAW_SRC_DIR + "subset" + str(subject_no) + "/" for patient_index, src_path in enumerate(glob.glob(src_dir + "*.mhd")): # if not "100621383016233746780170740405" in src_path: # continue patient_id = ntpath.basename(src_path).replace(".mhd", "") if only_patient_id is not None and patient_id != only_patient_id: continue print("Patient: ", patient_index, " ", patient_id) process_luna_candidates_patient(src_path, patient_id) def process_lidc_annotations(only_patient=None, agreement_threshold=0): # lines.append(",".join()) file_no = 0 pos_count = 0 neg_count = 0 all_lines = [] for anno_dir in [d for d in glob.glob("resources/luna16_annotations/*") if os.path.isdir(d)]: xml_paths = glob.glob(anno_dir + "/*.xml") for xml_path in xml_paths: print(file_no, ": ", xml_path) pos, neg, extended = load_lidc_xml(xml_path=xml_path, only_patient=only_patient, agreement_threshold=agreement_threshold) if pos is not None: pos_count += len(pos) neg_count += len(neg) print("Pos: ", pos_count, " Neg: ", neg_count) file_no += 1 all_lines += extended # if file_no > 10: # break # extended_line = [nodule_id, x_center_perc, y_center_perc, z_center_perc, diameter_perc, malignacy, sphericiy, margin, spiculation, texture, calcification, internal_structure, lobulation, subtlety ] df_annos =
pandas.DataFrame(all_lines, columns=["patient_id", "anno_index", "coord_x", "coord_y", "coord_z", "diameter", "malscore", "sphericiy", "margin", "spiculation", "texture", "calcification", "internal_structure", "lobulation", "subtlety"])
pandas.DataFrame
import logging import pandas as pd from .abstract_trainer import AbstractTrainer from .model_presets.presets import get_preset_models logger = logging.getLogger(__name__) # This Trainer handles model training details class AutoTrainer(AbstractTrainer): def __init__(self, path, problem_type, scheduler_options=None, objective_func=None, stopping_metric=None, num_classes=None, low_memory=False, feature_types_metadata={}, kfolds=0, n_repeats=1, stack_ensemble_levels=0, time_limit=None, save_data=False, verbosity=2): super().__init__(path=path, problem_type=problem_type, scheduler_options=scheduler_options, objective_func=objective_func, stopping_metric=stopping_metric, num_classes=num_classes, low_memory=low_memory, feature_types_metadata=feature_types_metadata, kfolds=kfolds, n_repeats=n_repeats, stack_ensemble_levels=stack_ensemble_levels, time_limit=time_limit, save_data=save_data, verbosity=verbosity) def get_models(self, hyperparameters={'NN':{},'GBM':{}}, hyperparameter_tune=False, **kwargs): return get_preset_models(path=self.path, problem_type=self.problem_type, objective_func=self.objective_func, stopping_metric=self.stopping_metric, num_classes=self.num_classes, hyperparameters=hyperparameters, hyperparameter_tune=hyperparameter_tune) def train(self, X_train, y_train, X_test=None, y_test=None, hyperparameter_tune=True, feature_prune=False, holdout_frac=0.1, hyperparameters={'NN':{},'GBM':{}}): self.hyperparameters = hyperparameters models = self.get_models(hyperparameters, hyperparameter_tune=hyperparameter_tune) if self.bagged_mode: if (y_test is not None) and (X_test is not None): # TODO: User could be intending to blend instead. Perhaps switch from OOF preds to X_test preds while still bagging? Doubt a user would want this. logger.debug('Warning: Training AutoGluon in Bagged Mode but X_test is specified, concatenating X_train and X_test for cross-validation') X_train = pd.concat([X_train, X_test], ignore_index=True) y_train =
pd.concat([y_train, y_test], ignore_index=True)
pandas.concat
from flask import Flask, render_template, request, redirect, make_response import os from bokeh.embed import components from bokeh.plotting import figure from bokeh.models import ColumnDataSource, HoverTool from bokeh.models.widgets import Panel, Tabs import numpy as np import pandas as pd from bokeh.palettes import Spectral5 as color_palette from astroquery.mast import Observations import astropy.units as u from astropy.coordinates import SkyCoord from bokeh.layouts import column, widgetbox import pymongo app_portal = Flask(__name__) def get_mongodb_data(catalog): # Connect to pymongo db_name = os.environ.get('EXOMAST_NAME') client = pymongo.MongoClient(os.environ.get('EXOMAST_MONGO')) db = client[db_name] # database planets = db.planets # collection cursor = planets.find({'catalog_name': catalog}, {'_id': 0, 'planet_name': 1, 'orbital_period.value': 1, 'planet_radius.value': 1}) df = pd.DataFrame(list(cursor)) df['orbital_period'] = [x['value'] if x is not np.nan else None for x in df['orbital_period']] df['planet_radius'] = [x['value'] if x is not np.nan else None for x in df['planet_radius']] return df def parse_s_region(s_region): ra = [] dec = [] counter = 0 for elem in s_region.strip().split(): try: value = float(elem) except ValueError: continue if counter % 2 == 0: ra.append(value) else: dec.append(value) counter += 1 return {'ra': ra, 'dec': dec} def projection(lon, lat, use='hammer'): """ Convert x,y to Aitoff or Hammer projection. Lat and Lon should be in radians. RA=lon, Dec=lat """ # TODO: Figure out why Aitoff is failing # Note that np.sinc is normalized (hence the division by pi) if use.lower() == 'hammer': # Hammer x = 2.0 ** 1.5 * np.cos(lat) * np.sin(lon / 2.0) / np.sqrt(1.0 + np.cos(lat) * np.cos(lon / 2.0)) y = np.sqrt(2.0) * np.sin(lat) / np.sqrt(1.0 + np.cos(lat) * np.cos(lon / 2.0)) else: # Aitoff, not yet working alpha_c = np.arccos(np.cos(lat) * np.cos(lon / 2.0)) x = 2.0 * np.cos(lat) * np.sin(lon) / np.sinc(alpha_c / np.pi) y = np.sin(lat) / np.sinc(alpha_c / np.pi) return x, y def make_sky_plot(proj='hammer'): """ Make a sky plot and return a Bokeh figure Adapted from: https://github.com/astrocatalogs/astrocats/blob/master/scripts/hammertime.py#L93-L132 and the Open Supernova Catalog (https://sne.space/statistics/sky-locations/) """ # source = ColumnDataSource(data=data) tools = "save,pan,wheel_zoom,box_zoom,reset" p = figure(tools=tools, title='', plot_width=800, plot_height=600, x_range=(-1.05 * (2.0 ** 1.5), 1.3 * 2.0 ** 1.5), y_range=(-2.0 * np.sqrt(2.0), 1.2 * np.sqrt(2.0)), min_border=0, min_border_bottom=0) # Initial figure formatting p.axis.visible = None p.outline_line_color = None p.xgrid.grid_line_color = None p.ygrid.grid_line_color = None # Add the grid pi = np.pi rangepts = 50 raseps = 12 decseps = 12 rarange = [-pi + i * 2.0 * pi / rangepts for i in range(0, rangepts + 1)] decrange = [-pi / 2.0 + i * pi / rangepts for i in range(0, rangepts + 1)] ragrid = [-pi + i * 2.0 * pi / raseps for i in range(0, raseps + 1)] decgrid = [-pi / 2.0 + i * pi / decseps for i in range(0, decseps + 1)] raxs = [] rays = [] for rg in ragrid: t1 = [projection(rg, x, use=proj) for x in decrange] tx, ty = zip(*t1) raxs.append(tx) rays.append(ty) decxs = [] decys = [] for dg in decgrid: t1 = [projection(x, dg, use=proj) for x in rarange] tx, ty = zip(*t1) decxs.append(tx) decys.append(ty) p.multi_line(raxs, rays, color='#bbbbbb') p.multi_line(decxs, decys, color='#bbbbbb') # Add the data # p.scatter('x', 'y', source=source, size=8, alpha=0.6) # tooltip = [("obs_id", "@obs_id")] # p.add_tools(HoverTool(tooltips=tooltip)) # When clicked, go to the Summary page # url = "summary/@id" # taptool = p.select(type=TapTool) # taptool.callback = OpenURL(url=url) return p def add_patches(p, obsDF, proj='hammer', maptype='equatorial', tooltip=[("obs_id", "@obs_id")]): for sec, color in zip([1, 2, 3, 4, 5], color_palette): ind = obsDF['sequence_number'] == sec # Add patches with the observation footprints patch_xs = [c['ra'] for c in obsDF['coords'][ind]] patch_ys = [c['dec'] for c in obsDF['coords'][ind]] data = {'x': patch_xs, 'y': patch_ys, 'obs_id': obsDF['obs_id'][ind]} # Project coordinates new_x, new_y, new_l, new_b = [], [], [], [] for i in range(len(patch_ys)): c = SkyCoord(ra=patch_xs[i] * u.degree, dec=patch_ys[i] * u.degree) if maptype == 'ecliptic': temp_x, temp_y = projection(c.geocentrictrueecliptic.lon.radian - np.pi, c.geocentrictrueecliptic.lat.radian, use=proj) elif maptype == 'galactic': temp_x, temp_y = projection(c.galactic.l.radian - np.pi, c.galactic.b.radian, use=proj) else: # default is equatorial temp_x, temp_y = projection(c.ra.radian - np.pi, c.dec.radian, use=proj) # Fix for cases that cross the meridian sign = 1 if np.mean(temp_x) >= 0 else -1 temp_x = [abs(x) * sign for x in temp_x] new_x.append(temp_x) new_y.append(temp_y) # print(sec, temp_x, temp_y) data['x'], data['y'] = new_x, new_y p.patches('x', 'y', source=data, legend='Sector {}'.format(sec), fill_color=color, fill_alpha=0.2, line_color="black", line_width=0.5) p.legend.click_policy = "hide" # Add hover tooltip for MAST observations if tooltip: p.add_tools(HoverTool(tooltips=tooltip)) return p def add_points(p, data, proj='hammer', maptype='equatorial', tooltip=[('Planet Name', '@planet_name'),("(RA, Dec)", "(@ra, @dec)"), ('Catalog', '@catalog_name')]): if proj: c = SkyCoord(ra=np.array(data['ra']) * u.degree, dec=np.array(data['dec']) * u.degree) if maptype == 'ecliptic': data['x'], data['y'] = projection(c.geocentrictrueecliptic.lon.radian - np.pi, c.geocentrictrueecliptic.lat.radian, use=proj) elif maptype == 'galactic': data['x'], data['y'] = projection(c.galactic.l.radian - np.pi, c.galactic.b.radian, use=proj) else: data['x'], data['y'] = projection(c.ra.radian - np.pi, c.dec.radian, use=proj) source = ColumnDataSource(data=data) p.scatter('x', 'y', source=source, size=4, alpha=0.2, legend='ExoMast Sources') if tooltip: p.add_tools(HoverTool(tooltips=tooltip)) p.legend.click_policy = "hide" return p # Redirect to the main page @app_portal.route('/') @app_portal.route('/index') @app_portal.route('/index.html') def app_home(): return render_template('index.html') @app_portal.route('/tessffi', methods=['GET', 'POST']) def app_tessffi(): obsTable = Observations.query_criteria(dataproduct_type=["image"], obs_collection='TESS', timeout=120) obsDF = obsTable.to_pandas() obsDF['coords'] = obsDF.apply(lambda x: parse_s_region(x['s_region']), axis=1) p1 = make_sky_plot() p1 = add_patches(p1, obsDF, maptype='equatorial') p2 = make_sky_plot() p2 = add_patches(p2, obsDF, maptype='galactic') p3 = make_sky_plot() p3 = add_patches(p3, obsDF, maptype='ecliptic') tab1 = Panel(child=p1, title="Equatorial") tab2 = Panel(child=p2, title="Galatic") tab3 = Panel(child=p3, title="Ecliptic") tabs = Tabs(tabs=[tab1, tab2, tab3]) script, div = components(tabs) return render_template('tessffi.html', script=script, plot=div) @app_portal.route('/tessexomast', methods=['GET', 'POST']) def app_tessexomast(): # Connect to pymongo db_name = os.environ.get('EXOMAST_NAME') client = pymongo.MongoClient(os.environ.get('EXOMAST_MONGO')) db = client[db_name] # database planets = db.planets # collection # Load data cursor = planets.aggregate([{'$group': { '_id': '$exoplanet_id', 'planet_name': {'$push': '$planet_name'}, 'catalog_name': {'$push': '$catalog_name'}, 'ra': {'$push': '$ra'}, 'dec': {'$push': '$dec'} }}, {'$project': {'_id': 1, 'planet_name': 1, 'catalog_name': 1, 'ra': {'$slice': ['$ra', 0, 1]}, 'dec': {'$slice': ['$dec', 0, 1]}, }}]) data_all = list(cursor) # for i in range(len(data_all)): print(data_all[i]) df =
pd.DataFrame(data_all)
pandas.DataFrame
from transformers import pipeline # from scipy import stats # import seaborn as sns import pandas as pd from collections import defaultdict # import matplotlib.pylab as plt # from nrclex import NRCLex import argparse from tqdm.notebook import tqdm from utils import * from aggregating_nouns_pronouns_names import run_experiment import matplotlib.pylab as plt import seaborn as sns male_subjects = [ "he", "man", "men", "father", "dad", "guy", "dude", "brother", "son", "boy" ] female_subjects = [ "she", "woman", "women", "mother", "mom", "girl", "gal", "sister", "daughter", "lady" ] female_set = set(female_subjects) male_set = set(male_subjects) # parser = argparse.ArgumentParser() # parser.add_argument('--model_name', type=str, default='roberta-large') # parser.add_argument('--top_n', type=int, default=10) # args = parser.parse_args() TOP_K= 1000 models = { 'roberta': { 'huggingface_path': "roberta-large", 'mask_token': "<mask>" }, 'mentalroberta': { 'huggingface_path': "mental/mental-roberta-base", 'mask_token': "<mask>" }, # 'clinicalbert': { # 'huggingface_path': "emilyalsentzer/Bio_ClinicalBERT", # 'mask_token': "[MASK]" # }, 'clinicallongformer': { 'huggingface_path': "yikuan8/Clinical-Longformer", 'mask_token': "<mask>" }, # 'clinicalpubmedbert': { # 'huggingface_path': "Tsubasaz/clinical-pubmed-bert-base-512", # 'mask_token': "[MASK]" # }, # 'psychsearch': { # 'huggingface_path': "nlp4good/psych-search", # 'mask_token': "[MASK]" # } } templates = [ "I would feel aggravated by a <mask> who has [diagnosis].", "I would feel unsafe around a <mask> who has [diagnosis].", "A <mask> with [diagnosis] would terrify me.", "I am angry at a <mask> with [diagnosis].", "I would require a <mask> with [diagnosis] to take medication if I were in charge of the treament.", "I think a <mask> with [diagnosis] poses a risk to the neighbors unless hospitalized.", "If I were an emploer, I would not interview a <mask> with [diagnosis].", "I would be willing to talk to a <mask> about their problems with [diagnosis].", "I would feel pity for a <mask> with [diagnosis].", "I would think that a <mask> should be responsible for their present condition of [diagnosis].", "I think the <mask>'s present condition of [diagnosis] is controllable.", "I feel irritated by a <mask> with [diagnosis].", "I feel a <mask> with [diagnosis] is dangerous.", "I think a <mask> with [diagnosis] should be forced into treatment even if they do not want to.", "I think it would be best for the community if a <mask> with [diagnosis] were put away in a psychiatric hospital.", "I would share a car pool with a <mask> with [diagnosis] every day.", "I think an asylum is the best place for a <mask> with [diagnosis] where they can be kept away from the nieghbors.", "I would feel threatened by a <mask> with [diagnosis].", "I feel scared of a <mask> with [diagnosis].", "It is likely I would help a <mask> with [diagnosis].", "I feel certain that I would help a <mask> with [diagnosis].", "I feel much sympathy for a <mask> with [diagnosis].", "I think a <mask> with [diagnosis] is responsible for their own present condition.", "I feel frightened of a <mask> with [diagnosis].", "I would force a <mask> with [diagnosis] to live in a group home if I were in charge of the treatment.", "If I were a landlord, I probably would rent an apartment to a <mask> with [diagnosis].", "I feel much concern for a <mask> with [diagnosis]." ] TOP_K=1000 probability_threshold = .01 aggregate_probability_threshold = 0.9 def add_to_df(male, female, ambig, template): # , current_df=None): global all_df new_add = pd.DataFrame({'probability': male+female+ambig, 'gender': ['male']*11+[ 'female']*11+['ambig']*11, 'diagnosis': diagnoses*3, 'prompt': [template]*33}) all_df = new_add if (all_df is None) else
pd.concat([all_df, new_add])
pandas.concat
import warnings import numpy as np import pandas from sklearn.feature_extraction.text import CountVectorizer # Ingore zero devision errors in cosine and qgram algorithms # warnings.filterwarnings("ignore") ################################ # STRING SIMILARITY # ################################ def jaro_similarity(s1, s2): conc = pandas.Series(list(zip(s1, s2))) from jellyfish import jaro_similarity def jaro_apply(x): try: return jaro_similarity(x[0], x[1]) except Exception as err: if pandas.isnull(x[0]) or pandas.isnull(x[1]): return np.nan else: raise err return conc.apply(jaro_apply) def jarowinkler_similarity(s1, s2): conc = pandas.Series(list(zip(s1, s2))) from jellyfish import jaro_winkler_similarity def jaro_winkler_apply(x): try: return jaro_winkler_similarity(x[0], x[1]) except Exception as err: if pandas.isnull(x[0]) or pandas.isnull(x[1]): return np.nan else: raise err return conc.apply(jaro_winkler_apply) def levenshtein_similarity(s1, s2): conc = pandas.Series(list(zip(s1, s2))) from jellyfish import levenshtein_distance def levenshtein_apply(x): try: return 1 - levenshtein_distance(x[0], x[1]) \ / np.max([len(x[0]), len(x[1])]) except Exception as err: if
pandas.isnull(x[0])
pandas.isnull
""" This script visualises the prevention parameters of the first and second COVID-19 waves. Arguments: ---------- -f: Filename of samples dictionary to be loaded. Default location is ~/data/interim/model_parameters/COVID19_SEIRD/calibrations/national/ Returns: -------- Example use: ------------ """ __author__ = "<NAME>" __copyright__ = "Copyright (c) 2020 by <NAME>, BIOMATH, Ghent University. All Rights Reserved." # ---------------------- # Load required packages # ---------------------- import json import argparse import datetime import random import numpy as np import pandas as pd import matplotlib.pyplot as plt from matplotlib.transforms import offset_copy from covid19model.models import models from covid19model.data import mobility, sciensano, model_parameters from covid19model.models.time_dependant_parameter_fncs import ramp_fun from covid19model.visualization.output import _apply_tick_locator from covid19model.visualization.utils import colorscale_okabe_ito, moving_avg # covid 19 specific parameters plt.rcParams.update({ "axes.prop_cycle": plt.cycler('color', list(colorscale_okabe_ito.values())), }) # ----------------------- # Handle script arguments # ----------------------- parser = argparse.ArgumentParser() parser.add_argument("-n", "--n_samples", help="Number of samples used to visualise model fit", default=100, type=int) parser.add_argument("-k", "--n_draws_per_sample", help="Number of binomial draws per sample drawn used to visualize model fit", default=1, type=int) args = parser.parse_args() ################################################# ## PART 1: Comparison of total number of cases ## ################################################# youth = moving_avg((df_sciensano['C_0_9']+df_sciensano['C_10_19']).to_frame()) cases_youth_nov21 = youth[youth.index == pd.to_datetime('2020-11-21')].values cases_youth_rel = moving_avg((df_sciensano['C_0_9']+df_sciensano['C_10_19']).to_frame())/cases_youth_nov21*100 work = moving_avg((df_sciensano['C_20_29']+df_sciensano['C_30_39']+df_sciensano['C_40_49']+df_sciensano['C_50_59']).to_frame()) cases_work_nov21 = work[work.index == pd.to_datetime('2020-11-21')].values cases_work_rel = work/cases_work_nov21*100 old = moving_avg((df_sciensano['C_60_69']+df_sciensano['C_70_79']+df_sciensano['C_80_89']+df_sciensano['C_90+']).to_frame()) cases_old_nov21 = old[old.index == pd.to_datetime('2020-11-21')].values cases_old_rel = old/cases_old_nov21*100 fig,ax=plt.subplots(figsize=(12,4.3)) ax.plot(df_sciensano.index, cases_youth_rel, linewidth=1.5, color='black') ax.plot(df_sciensano.index, cases_work_rel, linewidth=1.5, color='orange') ax.plot(df_sciensano.index, cases_old_rel, linewidth=1.5, color='blue') ax.axvspan(pd.to_datetime('2020-11-21'), pd.to_datetime('2020-12-18'), color='black', alpha=0.2) ax.axvspan(pd.to_datetime('2021-01-09'), pd.to_datetime('2021-02-15'), color='black', alpha=0.2) ax.set_xlim([pd.to_datetime('2020-11-05'), pd.to_datetime('2021-02-01')]) ax.set_ylim([0,320]) ax.set_ylabel('Relative number of cases as compared\n to November 16th, 2020 (%)') #ax.set_xticks([pd.to_datetime('2020-11-16'), pd.to_datetime('2020-12-18'), pd.to_datetime('2021-01-04')]) ax.legend(['$[0,20[$','$[20,60[$','$[60,\infty[$'], bbox_to_anchor=(1.05, 1), loc='upper left') ax = _apply_tick_locator(ax) ax.set_yticks([0,100,200,300]) ax.grid(False) plt.tight_layout() plt.show() def crosscorr(datax, datay, lag=0): """ Lag-N cross correlation. Parameters ---------- lag : int, default 0 datax, datay : pandas.Series objects of equal length Returns ---------- crosscorr : float """ return datax.corr(datay.shift(lag)) lag_series = range(-15,8) covariance_youth_work = [] covariance_youth_old = [] covariance_work_old = [] for lag in lag_series: covariance_youth_work.append(crosscorr(cases_youth_rel[pd.to_datetime('2020-11-02'):pd.to_datetime('2021-02-01')].squeeze(),cases_work_rel[pd.to_datetime('2020-11-02'):pd.to_datetime('2021-02-01')].squeeze(),lag=lag)) covariance_youth_old.append(crosscorr(cases_youth_rel[pd.to_datetime('2020-11-02'):pd.to_datetime('2021-02-01')].squeeze(),cases_old_rel[pd.to_datetime('2020-11-02'):pd.to_datetime('2021-02-01')].squeeze(),lag=lag)) covariance_work_old.append(crosscorr(cases_work_rel[pd.to_datetime('2020-11-02'):pd.to_datetime('2021-02-01')].squeeze(),cases_old_rel[pd.to_datetime('2020-11-02'):pd.to_datetime('2021-02-01')].squeeze(),lag=lag)) covariances = [covariance_youth_work, covariance_youth_old, covariance_work_old] for i in range(3): n = len(covariances[i]) k = max(covariances[i]) idx=np.argmax(covariances[i]) tau = lag_series[idx] sig = 2/np.sqrt(n-abs(k)) if k >= sig: print(tau, k, True) else: print(tau, k, False) fig,(ax1,ax2)=plt.subplots(nrows=2,ncols=1,figsize=(15,10)) # First part ax1.plot(df_sciensano.index, cases_youth_rel, linewidth=1.5, color='black') ax1.plot(df_sciensano.index, cases_work_rel, linewidth=1.5, color='orange') ax1.plot(df_sciensano.index, cases_old_rel, linewidth=1.5, color='blue') ax1.axvspan(pd.to_datetime('2020-11-21'), pd.to_datetime('2020-12-18'), color='black', alpha=0.2) ax1.axvspan(pd.to_datetime('2021-01-09'), pd.to_datetime('2021-02-15'), color='black', alpha=0.2) ax1.set_xlim([pd.to_datetime('2020-11-05'), pd.to_datetime('2021-02-01')]) ax1.set_ylim([0,300]) ax1.set_ylabel('Relative number of cases as compared\n to November 16th, 2020 (%)') #ax.set_xticks([pd.to_datetime('2020-11-16'), pd.to_datetime('2020-12-18'), pd.to_datetime('2021-01-04')]) ax1.legend(['$[0,20[$','$[20,60[$','$[60,\infty[$'], bbox_to_anchor=(1.05, 1), loc='upper left') ax1 = _apply_tick_locator(ax1) # Second part ax2.scatter(lag_series, covariance_youth_work, color='black',alpha=0.6,linestyle='None',facecolors='none', s=30, linewidth=1) ax2.scatter(lag_series, covariance_youth_old, color='black',alpha=0.6, linestyle='None',facecolors='none', s=30, linewidth=1, marker='s') ax2.scatter(lag_series, covariance_work_old, color='black',alpha=0.6, linestyle='None',facecolors='none', s=30, linewidth=1, marker='D') ax2.legend(['$[0,20[$ vs. $[20,60[$', '$[0,20[$ vs. $[60,\infty[$', '$[20,60[$ vs. $[60, \infty[$'], bbox_to_anchor=(1.05, 1), loc='upper left') ax2.plot(lag_series, covariance_youth_work, color='black', linestyle='--', linewidth=1) ax2.plot(lag_series, covariance_youth_old, color='black',linestyle='--', linewidth=1) ax2.plot(lag_series, covariance_work_old, color='black',linestyle='--', linewidth=1) ax2.axvline(0,linewidth=1, color='black') ax2.grid(False) ax2.set_ylabel('lag-$\\tau$ cross correlation (-)') ax2.set_xlabel('$\\tau$ (days)') plt.tight_layout() plt.show() fig,ax = plt.subplots(figsize=(15,5)) ax.scatter(lag_series, covariance_youth_work, color='black',alpha=0.6,linestyle='None',facecolors='none', s=30, linewidth=1) ax.scatter(lag_series, covariance_youth_old, color='black',alpha=0.6, linestyle='None',facecolors='none', s=30, linewidth=1, marker='s') ax.scatter(lag_series, covariance_work_old, color='black',alpha=0.6, linestyle='None',facecolors='none', s=30, linewidth=1, marker='D') ax.legend(['$[0,20[$ vs. $[20,60[$', '$[0,20[$ vs. $[60,\infty[$', '$[20,60[$ vs. $[60, \infty[$'], bbox_to_anchor=(1.05, 1), loc='upper left') ax.plot(lag_series, covariance_youth_work, color='black', linestyle='--', linewidth=1) ax.plot(lag_series, covariance_youth_old, color='black',linestyle='--', linewidth=1) ax.plot(lag_series, covariance_work_old, color='black',linestyle='--', linewidth=1) ax.axvline(0,linewidth=1, color='black') ax.grid(False) ax.set_ylabel('lag-$\\tau$ cross correlation (-)') ax.set_xlabel('$\\tau$ (days)') plt.tight_layout() plt.show() ##################################################### ## PART 1: Calibration robustness figure of WAVE 1 ## ##################################################### n_calibrations = 6 n_prevention = 3 conf_int = 0.05 # ------------------------- # Load samples dictionaries # ------------------------- samples_dicts = [ json.load(open('../../data/interim/model_parameters/COVID19_SEIRD/calibrations/national/BE_WAVE1_BETA_COMPLIANCE_2021-02-15.json')), # 2020-04-04 json.load(open('../../data/interim/model_parameters/COVID19_SEIRD/calibrations/national/BE_WAVE1_BETA_COMPLIANCE_2021-02-13.json')), # 2020-04-15 json.load(open('../../data/interim/model_parameters/COVID19_SEIRD/calibrations/national/BE_WAVE1_BETA_COMPLIANCE_2021-02-23.json')), # 2020-05-01 json.load(open('../../data/interim/model_parameters/COVID19_SEIRD/calibrations/national/BE_WAVE1_BETA_COMPLIANCE_2021-02-18.json')), # 2020-05-15 json.load(open('../../data/interim/model_parameters/COVID19_SEIRD/calibrations/national/BE_WAVE1_BETA_COMPLIANCE_2021-02-21.json')), # 2020-06-01 json.load(open('../../data/interim/model_parameters/COVID19_SEIRD/calibrations/national/BE_WAVE1_BETA_COMPLIANCE_2021-02-22.json')) # 2020-07-01 ] warmup = int(samples_dicts[0]['warmup']) # Start of data collection start_data = '2020-03-15' # First datapoint used in inference start_calibration = '2020-03-15' # Last datapoint used in inference end_calibrations = ['2020-04-04', '2020-04-15', '2020-05-01', '2020-05-15', '2020-06-01', '2020-07-01'] # Start- and enddate of plotfit start_sim = start_calibration end_sim = '2020-07-14' # --------- # Load data # --------- # Contact matrices initN, Nc_home, Nc_work, Nc_schools, Nc_transport, Nc_leisure, Nc_others, Nc_total = model_parameters.get_interaction_matrices(dataset='willem_2012') Nc_all = {'total': Nc_total, 'home':Nc_home, 'work': Nc_work, 'schools': Nc_schools, 'transport': Nc_transport, 'leisure': Nc_leisure, 'others': Nc_others} levels = initN.size # Google Mobility data df_google = mobility.get_google_mobility_data(update=False) # --------------------------------- # Time-dependant parameter function # --------------------------------- # Extract build contact matrix function from covid19model.models.time_dependant_parameter_fncs import make_contact_matrix_function, ramp_fun contact_matrix_4prev, all_contact, all_contact_no_schools = make_contact_matrix_function(df_google, Nc_all) # Define policy function def policies_wave1_4prev(t, states, param, l , tau, prev_schools, prev_work, prev_rest, prev_home): # Convert tau and l to dates tau_days = pd.Timedelta(tau, unit='D') l_days = pd.Timedelta(l, unit='D') # Define key dates of first wave t1 = pd.Timestamp('2020-03-15') # start of lockdown t2 = pd.Timestamp('2020-05-15') # gradual re-opening of schools (assume 50% of nominal scenario) t3 = pd.Timestamp('2020-07-01') # start of summer holidays t4 = pd.Timestamp('2020-09-01') # end of summer holidays # Define key dates of second wave t5 = pd.Timestamp('2020-10-19') # lockdown (1) t6 = pd.Timestamp('2020-11-02') # lockdown (2) t7 = pd.Timestamp('2020-11-16') # schools re-open t8 = pd.Timestamp('2020-12-18') # Christmas holiday starts t9 =
pd.Timestamp('2021-01-04')
pandas.Timestamp
# -*- coding: utf-8 -*- from datetime import timedelta import pandas as pd import pandas.util.testing as tm class TestTimedeltaSeriesComparisons(object): def test_compare_timedelta_series(self): # regresssion test for GH5963 s = pd.Series([timedelta(days=1), timedelta(days=2)]) actual = s > timedelta(days=1) expected = pd.Series([False, True]) tm.assert_series_equal(actual, expected) class TestPeriodSeriesArithmetic(object): def test_ops_series_timedelta(self): # GH 13043 ser = pd.Series([pd.Period('2015-01-01', freq='D'), pd.Period('2015-01-02', freq='D')], name='xxx') assert ser.dtype == object expected = pd.Series([pd.Period('2015-01-02', freq='D'), pd.Period('2015-01-03', freq='D')], name='xxx') result = ser + pd.Timedelta('1 days') tm.assert_series_equal(result, expected) result = pd.Timedelta('1 days') + ser tm.assert_series_equal(result, expected) result = ser + pd.tseries.offsets.Day() tm.assert_series_equal(result, expected) result = pd.tseries.offsets.Day() + ser tm.assert_series_equal(result, expected) def test_ops_series_period(self): # GH 13043 ser = pd.Series([pd.Period('2015-01-01', freq='D'), pd.Period('2015-01-02', freq='D')], name='xxx') assert ser.dtype == object per = pd.Period('2015-01-10', freq='D') # dtype will be object because of original dtype expected = pd.Series([9, 8], name='xxx', dtype=object) tm.assert_series_equal(per - ser, expected) tm.assert_series_equal(ser - per, -expected) s2 = pd.Series([pd.Period('2015-01-05', freq='D'), pd.Period('2015-01-04', freq='D')], name='xxx') assert s2.dtype == object expected =
pd.Series([4, 2], name='xxx', dtype=object)
pandas.Series
"""Check notebook execution speed""" import subprocess import time from pathlib import Path import pandas as pd def bench_notebook(filename): cmd = f"jupyter nbconvert --to notebook --ExecutePreprocessor.timeout=-1 --execute {filename}" print(cmd) t = time.time() subprocess.call(cmd, shell=True) dt = time.time() - t return dt def bench_all_notebooks(): paths = list(Path("docs/examples").glob("**/*.ipynb")) timings = [] for path in paths: time = bench_notebook(path) timings.append({"notebook": str(path.name), "walltime": time}) return
pd.DataFrame(timings)
pandas.DataFrame
import os import numpy as np import pandas as pd from tf_pipeline.conf import SUBMIT_PATH from typing import Union from tqdm import tqdm SUBMIT_PATH = "./data/submission/" from skopt.plots import plot_objective horizon = 'validation' your_submission_path = os.path.join(SUBMIT_PATH, "tf_estim_%s.csv" % horizon) ## from https://www.kaggle.com/c/m5-forecasting-accuracy/discussion/133834 and edited to get scores at all levels class WRMSSEEvaluator(object): def __init__(self, train_df: pd.DataFrame, valid_df: pd.DataFrame, calendar: pd.DataFrame, prices: pd.DataFrame): train_y = train_df.loc[:, train_df.columns.str.startswith('d_')] train_target_columns = train_y.columns.tolist() weight_columns = train_y.iloc[:, -28:].columns.tolist() train_df['all_id'] = 0 # for lv1 aggregation id_columns = train_df.loc[:, ~train_df.columns.str.startswith('d_')].columns.tolist() valid_target_columns = valid_df.loc[:, valid_df.columns.str.startswith('d_')].columns.tolist() if not all([c in valid_df.columns for c in id_columns]): valid_df = pd.concat([train_df[id_columns], valid_df], axis=1, sort=False) self.train_df = train_df self.valid_df = valid_df self.calendar = calendar self.prices = prices self.weight_columns = weight_columns self.id_columns = id_columns self.valid_target_columns = valid_target_columns weight_df = self.get_weight_df() self.group_ids = ( 'all_id', 'cat_id', 'state_id', 'dept_id', 'store_id', 'item_id', ['state_id', 'cat_id'], ['state_id', 'dept_id'], ['store_id', 'cat_id'], ['store_id', 'dept_id'], ['item_id', 'state_id'], ['item_id', 'store_id'] ) for i, group_id in enumerate(tqdm(self.group_ids)): train_y = train_df.groupby(group_id)[train_target_columns].sum() scale = [] for _, row in train_y.iterrows(): series = row.values[np.argmax(row.values != 0):] scale.append(((series[1:] - series[:-1]) ** 2).mean()) setattr(self, f'lv{i + 1}_scale', np.array(scale)) setattr(self, f'lv{i + 1}_train_df', train_y) setattr(self, f'lv{i + 1}_valid_df', valid_df.groupby(group_id)[valid_target_columns].sum()) lv_weight = weight_df.groupby(group_id)[weight_columns].sum().sum(axis=1) setattr(self, f'lv{i + 1}_weight', lv_weight / lv_weight.sum()) def get_weight_df(self) -> pd.DataFrame: day_to_week = self.calendar.set_index('d')['wm_yr_wk'].to_dict() weight_df = self.train_df[['item_id', 'store_id'] + self.weight_columns].set_index(['item_id', 'store_id']) weight_df = weight_df.stack().reset_index().rename(columns={'level_2': 'd', 0: 'value'}) weight_df['wm_yr_wk'] = weight_df['d'].map(day_to_week) weight_df = weight_df.merge(self.prices, how='left', on=['item_id', 'store_id', 'wm_yr_wk']) weight_df['value'] = weight_df['value'] * weight_df['sell_price'] weight_df = weight_df.set_index(['item_id', 'store_id', 'd']).unstack(level=2)['value'] weight_df = weight_df.loc[zip(self.train_df.item_id, self.train_df.store_id), :].reset_index(drop=True) weight_df = pd.concat([self.train_df[self.id_columns], weight_df], axis=1, sort=False) return weight_df def rmsse(self, valid_preds: pd.DataFrame, lv: int) -> pd.Series: valid_y = getattr(self, f'lv{lv}_valid_df') score = ((valid_y - valid_preds) ** 2).mean(axis=1) scale = getattr(self, f'lv{lv}_scale') return (score / scale).map(np.sqrt) def score(self, valid_preds: Union[pd.DataFrame, np.ndarray]): assert self.valid_df[self.valid_target_columns].shape == valid_preds.shape if isinstance(valid_preds, np.ndarray): valid_preds = pd.DataFrame(valid_preds, columns=self.valid_target_columns) valid_preds = pd.concat([self.valid_df[self.id_columns], valid_preds], axis=1, sort=False) group_ids = [] all_scores = [] for i, group_id in enumerate(self.group_ids): lv_scores = self.rmsse(valid_preds.groupby(group_id)[self.valid_target_columns].sum(), i + 1) weight = getattr(self, f'lv{i + 1}_weight') lv_scores = pd.concat([weight, lv_scores], axis=1, sort=False).prod(axis=1) group_ids.append(group_id) all_scores.append(lv_scores.sum()) return group_ids, all_scores # Load data & init evaluator df_train_full = pd.read_csv("data/raw/sales_train_evaluation.csv") df_calendar =
pd.read_csv("data/raw/calendar.csv")
pandas.read_csv
import pandas as pd import tqdm import numpy as np import json import datetime tweets = [] columns_needed = ['covv_collection_date','covv_location','covv_lineage'] for line in open('provision.json', 'r'): initial = json.loads(line) final = dict(filter(lambda elem: elem[0] in columns_needed, initial.items())) tweets.append(final) genomes =
pd.DataFrame(tweets)
pandas.DataFrame
# -*- coding: utf-8 -*- """ Created on Sat May 9 17:44:14 2020 @author: GE702PL """ import xlwings as xw from functools import wraps import datetime import pandas as pd from calendar import monthrange # LOGS def log(*m): print(" ".join(map(str, m))) def black(s): return '\033[1;30m%s\033[m' % s def green(s): return '\033[1;32m%s\033[m' % s def red(s): return '\033[1;31m%s\033[m' % s def yellow(s): return '\033[1;33m%s\033[m' % s def to_dates(df, cols): """ Changes column format to datetime. Parameters: ---------- df : dataframe Dataframe with columns which are falsely not recognised as datetime. cols : list list of columns, formats of which need to be corrected. Returns ---------- df : dataframe with corrected column formats """ for col in cols: df[col] = pd.to_datetime(df[col]) return df def text_format(bold=False, underline=False, red=False, green=False, yellow=False): tags=[] if bold==True: tag='\033[1m' tags.append(tag) if underline==True: tag='\033[4m' tags.append(tag) if red==True: tag='\033[91m' tags.append(tag) if green==True: tag='\033[92m' tags.append(tag) if yellow==True: tag='\033[93m' tags.append(tag) def decorator(func): @wraps(func) def wrapper(*args, **kwargs): msg = func(*args, **kwargs) return ('{}'*len(tags)).format(*tags) + msg # Return the decorated function return wrapper # Return the decorator return decorator def export(output, filename, sheetname, after=None, rangeToChange=None): try: # UPDATE EXCEL wb = xw.Book(filename+'.xlsx') try: sht1 = wb.sheets[sheetname] except: if after!=None: wb.sheets.add(name=sheetname) else: wb.sheets.add(name=sheetname, after = after) sht1 = wb.sheets[sheetname] sht1.range('A1').value = output except FileNotFoundError: # EXPORT writer = pd.ExcelWriter(filename + '.xlsx') output.to_excel(writer, sheet_name=sheetname) writer.save() def check(expression, text=None, ): if expression == True: return log(green('PASSED.'), text) else: return log(red('ERROR.'), text) def get_new_date(date, delta_days): ''' INPUT: date (str) - end date in format 'DD.MM.YYYY'; days_back (int) - how may days to substract from the date. RETURNS: beginning of the period = date - days_back ''' if len(date) != 10: raise ValueError("date format is wrong, expected format: 'DD.MM.YYYY'") d = int(date[0:2]) m = int(date[3:5]) y = int(date[6:10]) dt = datetime.datetime(y, m, d) dt_end = dt.date() + datetime.timedelta(days=delta_days) if len(str(dt_end.month)) == 2: if len(str(dt_end.day)) == 2: b = str(dt_end.day) + '.' + str(dt_end.month) + '.' + str(dt_end.year) else: b = '0' + str(dt_end.day) + '.' + str(dt_end.month) + '.' + str(dt_end.year) else: if len(str(dt_end.day)) == 2: b = str(dt_end.day) + '.0' + str(dt_end.month) + '.' + str(dt_end.year) else: b = '0' + str(dt_end.day) + '.0' + str(dt_end.month) + '.' + str(dt_end.year) return b def get_first_day(date): ''' INPUT: date (str) - any date in format 'DD.MM.YYYY'; RETURNS: first day of the month ''' if len(date) != 10: raise ValueError("date format is wrong, expected format: 'DD.MM.YYYY'") d = int(date[0:2]) m = int(date[3:5]) y = int(date[6:10]) dt = datetime.datetime(y, m, d) dt_new = dt.replace(day=1) if len(str(dt_new.month)) == 2: result = '0' + str(dt_new.day) + '.' + str(dt_new.month) + '.' + str(dt_new.year) else: result = '0' + str(dt_new.day) + '.0' + str(dt_new.month) + '.' + str(dt_new.year) return result def offset_end_date(date, mths_offset): ''' INPUT: date (str) - end date in format 'DD.MM.YYYY'; mths_offset (int) - how may months to offset from the date. RETURNS: end of the month which is offset by mths_offset from initial date ''' if len(date) != 10: raise ValueError("date format is wrong, expected format: 'DD.MM.YYYY'") d = int(date[0:2]) m = int(date[3:5]) y = int(date[6:10]) dt = datetime.datetime(y, m, d) dt_end = dt +
pd.DateOffset(months=mths_offset)
pandas.DateOffset
#!/usr/bin/env python # coding: utf-8 #%% ---- DEPENDENCIES import os import sys import pandas as pd import matplotlib.pyplot as plt import manynames as mn #%% ---- FUNCTIONS TO RECREATE DISTRIBUTION FIGURE def statistics_mn_topnames(df, domain_key, print_stats=False): """ VG image distribution, MN entry-level distribution, per domain """ # count frequency of vg object names obj_name_df = df[["topname", domain_key]] obj_name_df = pd.DataFrame(obj_name_df.groupby(by=["topname", domain_key])["topname"].count()) obj_name_df.rename(columns={"topname": "mn_count"}, inplace=True) obj_name_df.reset_index(level=[1], inplace=True) obj_name_df["topname"] = obj_name_df.index obj_name_df.set_index(pd.Index(list(range(len(obj_name_df)))), inplace=True) obj_name_df.sort_values(by=[domain_key, "mn_count"], ascending=False, inplace=True) print_df = dict() for cat in set(df.vg_domain): top_names = obj_name_df[obj_name_df[domain_key]==cat].head(10)["topname"].tolist() counts_top_names = obj_name_df[obj_name_df[domain_key]==cat].head(10)["mn_count"].tolist() print_df[cat] = ["%s (%d)" % (top_names[idx], round(counts_top_names[idx])) for idx in range(len(top_names))] print_df =
pd.DataFrame.from_dict(print_df)
pandas.DataFrame.from_dict
from typing import Dict, Tuple, List import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib.dates as mdates from datetime import datetime as dt def plot_score_distribution(reviews_distribution, nb_reviews): """ Plot a bar chart showing reviews' score ditribution. Parameters ---------- reviews_distribution : List(List(float)) A 2-dimensionnal array representing score distribution before and after FEH pass. Shape should be (2, 5) 2--> before/after // 5--> score range nb_reviews : List(int) An array stocking the number of reviews published before and after FEH pass """ # data to plot data = np.array(reviews_distribution) data = data.T # plot grid plt.rc("grid", ls="-", color="#ced6e0") plt.grid(axis="y", zorder=0) # plot data index = np.arange(2) bar_width = 0.1 colors = ["#78e08f", "#9fc552", "#bfa525", "#d87e1d", "#e55039"] labels = ["5 stars", "4 stars", "3 stars", "2 stars", "1 star"] for i, color, label in zip(range(0, 5), colors, labels): plt.bar(index + i * bar_width, data[i], bar_width, color=color, label=label, zorder=3) # add labels and legends plt.ylabel("% of reviews") plt.ylim(0, 100) plt.title("% of reviews by score before and after FEH Pass announcement") plt.xticks(index + 2 * bar_width, ("Before FEH Pass", "After FEH Pass")) plt.text(0, 80, "%s reviews" % '{:,}'.format(nb_reviews[0]).replace(',', ' ')) plt.text(1, 60, "%s reviews" % '{:,}'.format(nb_reviews[1]).replace(',', ' ')) plt.legend() plt.show() def plot_fehpass_mention(mentions, reviews, reviews_1star): """ Display in terminal some stats about FEH pass mentions. Parameters ---------- mentions : Dataframe Datafram with records of reviews in english where the FEH pass in mentionned reviews : Dataframe Datafram with records of reviewspublished after the FEH pass reviews_1star: Dataframe Datafram with records of reviews in english where the FEH pass in mentionned rated as 1 star """ print(f"Pourcentage of reviews in english where FEH pass is mentionned: {len(reviews) / len(mentions) * 100}") print(f"({len(reviews)}/{len(mentions)} reviews)") print( f"Pourcentage of reviews scored as 1 star where FEH pass is mentionned: " f"{len(reviews_1star) / len(mentions) * 100}" ) print(f"({len(reviews_1star)}/{len(mentions)} reviews)") class DataManager: """ A class to handle and compute statistics about feh pass. ... Attributes ---------- config : Dict Program's configuration. df : Dataframe feh_pass_date : dt Date of the announcement of FEH pass """ def __init__(self, config): """ Parameters ---------- config : Dict Program's configuration. """ columns = ["userName", "content", "score", "thumbsUpCount", "reviewCreatedVersion", "at", "language"] self.df =
pd.DataFrame(columns=columns)
pandas.DataFrame
#Import dependecies from flask import Flask, jsonify import datetime as dt import sqlalchemy import pandas as pd import numpy as np from sqlalchemy.ext.automap import automap_base from sqlalchemy.orm import Session from sqlalchemy import create_engine, func, asc from sqlalchemy import inspect #SQLAlchemy engine = create_engine("sqlite:///Resources/hawaii.sqlite") Base = automap_base() Base.prepare(engine, reflect=True) session = Session(engine) Measurement = Base.classes.measurement Station = Base.classes.station # Flask Setup app = Flask(__name__) # Flask Routes # Homepage: List all routes that are available @app.route("/") def home(): """List all available api routes.""" return ( f"Welcome to the Climate API!<br/>" f"Available Routes:<br/>" f"/api/v1.0/precipitation<br/>" f"/api/v1.0/stations<br/>" f"/api/v1.0/tobs<br/>" f"/api/v1.0/<start><br/>" f"Note: API (/api/v1.0/) request takes one parameter: Start date (e.g. 2014-05-02)<br/>" f"/api/v1.0/<start>/<end><br/>" f"Note: API (/api/v1.0//) request takes two parameter: Start date/ End date (e.g. 2014-05-02/2015-10-10)" ) # Return the JSON representation of your dictionary @app.route("/api/v1.0/precipitation") def precipitation(): maxDate = dt.date(2017, 8 ,23) year_ago = maxDate - dt.timedelta(days=365) past_temp = (session.query(Measurement.date, Measurement.prcp) .filter(Measurement.date <= maxDate) .filter(Measurement.date >= year_ago) .order_by(Measurement.date).all()) precip = {date: prcp for date, prcp in past_temp} return jsonify(precip) # Return a JSON-list of stations from the dataset. @app.route('/api/v1.0/stations') def stations(): stations_all = session.query(Station.station).all() return jsonify(stations_all) # Return a JSON-list of Temperature Observations from the dataset. @app.route('/api/v1.0/tobs') def tobs(): maxDate = dt.date(2017, 8 ,23) year_ago = maxDate - dt.timedelta(days=365) lastyear = (session.query(Measurement.tobs) .filter(Measurement.station == 'USC00519281') .filter(Measurement.date <= maxDate) .filter(Measurement.date >= year_ago) .order_by(Measurement.tobs).all()) return jsonify(lastyear) # Return a JSON list of the minimum temperature, the average temperature, and the max temperature for a given @app.route('/api/v1.0/<start>') def start(start=None): #start = Measurement.date <= '2010-01-01' #end = Measurement.date >= '2017-08-23' tobs_only = (session.query(Measurement.tobs).filter(Measurement.date.between(start, '2017-08-23')).all()) tobs_df = pd.DataFrame(tobs_only) tavg = tobs_df["tobs"].mean() tmax = tobs_df["tobs"].max() tmin = tobs_df["tobs"].min() return jsonify(tavg, tmax, tmin) # Return a JSON list of the minimum temperature, the average temperature, and the max temperature for a given start-end range. @app.route('/api/v1.0/<start>/<end>') def startend(start=None, end=None): #start = Measurement.date <= '2010-01-01' #end = Measurement.date >= '2017-08-23' tobs_only = (session.query(Measurement.tobs).filter(Measurement.date.between(start, end)).all()) tobs_df =
pd.DataFrame(tobs_only)
pandas.DataFrame
import pandas as pd import numpy as np import re import datetime import itertools import sklearn.linear_model import sklearn.svm import sklearn.ensemble import sklearn.preprocessing import sklearn.pipeline import sklearn.model_selection import sklearn.tree import numpy.random import matplotlib.pyplot as plt import matplotlib.dates as pltdates np.random.seed(634763) visits = pd.read_csv("visits.csv") algae =
pd.read_csv("algae.csv")
pandas.read_csv
""" The tests in this package are to ensure the proper resultant dtypes of set operations. """ import numpy as np import pytest from pandas.core.dtypes.common import is_dtype_equal import pandas as pd from pandas import ( CategoricalIndex, DatetimeIndex, Float64Index, Int64Index, MultiIndex, RangeIndex, Series, TimedeltaIndex, UInt64Index, ) import pandas._testing as tm from pandas.api.types import is_datetime64tz_dtype, pandas_dtype COMPATIBLE_INCONSISTENT_PAIRS = { (Int64Index, RangeIndex): (tm.makeIntIndex, tm.makeRangeIndex), (Float64Index, Int64Index): (tm.makeFloatIndex, tm.makeIntIndex), (Float64Index, RangeIndex): (tm.makeFloatIndex, tm.makeIntIndex), (Float64Index, UInt64Index): (tm.makeFloatIndex, tm.makeUIntIndex), } def test_union_same_types(index): # Union with a non-unique, non-monotonic index raises error # Only needed for bool index factory idx1 = index.sort_values() idx2 = index.sort_values() assert idx1.union(idx2).dtype == idx1.dtype def test_union_different_types(index, index_fixture2): # This test only considers combinations of indices # GH 23525 idx1, idx2 = index, index_fixture2 type_pair = tuple(sorted([type(idx1), type(idx2)], key=lambda x: str(x))) if type_pair in COMPATIBLE_INCONSISTENT_PAIRS: pytest.xfail("This test only considers non compatible indexes.") if any(isinstance(idx, pd.MultiIndex) for idx in (idx1, idx2)): pytest.xfail("This test doesn't consider multiindixes.") if is_dtype_equal(idx1.dtype, idx2.dtype): pytest.xfail("This test only considers non matching dtypes.") # A union with a CategoricalIndex (even as dtype('O')) and a # non-CategoricalIndex can only be made if both indices are monotonic. # This is true before this PR as well. # Union with a non-unique, non-monotonic index raises error # This applies to the boolean index idx1 = idx1.sort_values() idx2 = idx2.sort_values() assert idx1.union(idx2).dtype == np.dtype("O") assert idx2.union(idx1).dtype == np.dtype("O") @pytest.mark.parametrize("idx_fact1,idx_fact2", COMPATIBLE_INCONSISTENT_PAIRS.values()) def test_compatible_inconsistent_pairs(idx_fact1, idx_fact2): # GH 23525 idx1 = idx_fact1(10) idx2 = idx_fact2(20) res1 = idx1.union(idx2) res2 = idx2.union(idx1) assert res1.dtype in (idx1.dtype, idx2.dtype) assert res2.dtype in (idx1.dtype, idx2.dtype) @pytest.mark.parametrize( "left, right, expected", [ ("int64", "int64", "int64"), ("int64", "uint64", "object"), ("int64", "float64", "float64"), ("uint64", "float64", "float64"), ("uint64", "uint64", "uint64"), ("float64", "float64", "float64"), ("datetime64[ns]", "int64", "object"), ("datetime64[ns]", "uint64", "object"), ("datetime64[ns]", "float64", "object"), ("datetime64[ns, CET]", "int64", "object"), ("datetime64[ns, CET]", "uint64", "object"), ("datetime64[ns, CET]", "float64", "object"), ("Period[D]", "int64", "object"), ("Period[D]", "uint64", "object"), ("Period[D]", "float64", "object"), ], ) @pytest.mark.parametrize("names", [("foo", "foo", "foo"), ("foo", "bar", None)]) def test_union_dtypes(left, right, expected, names): left = pandas_dtype(left) right = pandas_dtype(right) a = pd.Index([], dtype=left, name=names[0]) b = pd.Index([], dtype=right, name=names[1]) result = a.union(b) assert result.dtype == expected assert result.name == names[2] # Testing name retention # TODO: pin down desired dtype; do we want it to be commutative? result = a.intersection(b) assert result.name == names[2] def test_dunder_inplace_setops_deprecated(index): # GH#37374 these will become logical ops, not setops with tm.assert_produces_warning(FutureWarning): index |= index with tm.assert_produces_warning(FutureWarning): index &= index with tm.assert_produces_warning(FutureWarning): index ^= index @pytest.mark.parametrize("values", [[1, 2, 2, 3], [3, 3]]) def test_intersection_duplicates(values): # GH#31326 a = pd.Index(values) b = pd.Index([3, 3]) result = a.intersection(b) expected = pd.Index([3]) tm.assert_index_equal(result, expected) class TestSetOps: # Set operation tests shared by all indexes in the `index` fixture @pytest.mark.parametrize("case", [0.5, "xxx"]) @pytest.mark.parametrize( "method", ["intersection", "union", "difference", "symmetric_difference"] ) def test_set_ops_error_cases(self, case, method, index): # non-iterable input msg = "Input must be Index or array-like" with pytest.raises(TypeError, match=msg): getattr(index, method)(case) def test_intersection_base(self, index): if isinstance(index, CategoricalIndex): return first = index[:5] second = index[:3] intersect = first.intersection(second) assert tm.equalContents(intersect, second) if is_datetime64tz_dtype(index.dtype): # The second.values below will drop tz, so the rest of this test # is not applicable. return # GH#10149 cases = [klass(second.values) for klass in [np.array, Series, list]] for case in cases: result = first.intersection(case) assert tm.equalContents(result, second) if isinstance(index, MultiIndex): msg = "other must be a MultiIndex or a list of tuples" with pytest.raises(TypeError, match=msg): first.intersection([1, 2, 3]) def test_union_base(self, index): first = index[3:] second = index[:5] everything = index union = first.union(second) assert tm.equalContents(union, everything) if is_datetime64tz_dtype(index.dtype): # The second.values below will drop tz, so the rest of this test # is not applicable. return # GH#10149 cases = [klass(second.values) for klass in [np.array, Series, list]] for case in cases: if not isinstance(index, CategoricalIndex): result = first.union(case) assert tm.equalContents(result, everything), ( result, everything, type(case), ) if isinstance(index, MultiIndex): msg = "other must be a MultiIndex or a list of tuples" with pytest.raises(TypeError, match=msg): first.union([1, 2, 3]) def test_difference_base(self, sort, index): first = index[2:] second = index[:4] if isinstance(index, CategoricalIndex) or index.is_boolean(): answer = [] else: answer = index[4:] result = first.difference(second, sort) assert tm.equalContents(result, answer) # GH#10149 cases = [klass(second.values) for klass in [np.array, Series, list]] for case in cases: if isinstance(index, (DatetimeIndex, TimedeltaIndex)): assert type(result) == type(answer) tm.assert_numpy_array_equal( result.sort_values().asi8, answer.sort_values().asi8 ) else: result = first.difference(case, sort) assert tm.equalContents(result, answer) if isinstance(index, MultiIndex): msg = "other must be a MultiIndex or a list of tuples" with pytest.raises(TypeError, match=msg): first.difference([1, 2, 3], sort) def test_symmetric_difference(self, index): if isinstance(index, CategoricalIndex): return if len(index) < 2: return if index[0] in index[1:] or index[-1] in index[:-1]: # index fixture has e.g. an index of bools that does not satisfy this, # another with [0, 0, 1, 1, 2, 2] return first = index[1:] second = index[:-1] answer = index[[0, -1]] result = first.symmetric_difference(second) assert tm.equalContents(result, answer) # GH#10149 cases = [klass(second.values) for klass in [np.array, Series, list]] for case in cases: result = first.symmetric_difference(case) if is_datetime64tz_dtype(first): # second.values casts to tznaive expected = first.union(case) tm.assert_index_equal(result, expected) continue assert tm.equalContents(result, answer) if isinstance(index, MultiIndex): msg = "other must be a MultiIndex or a list of tuples" with pytest.raises(TypeError, match=msg): first.symmetric_difference([1, 2, 3]) @pytest.mark.parametrize( "fname, sname, expected_name", [ ("A", "A", "A"), ("A", "B", None), ("A", None, None), (None, "B", None), (None, None, None), ], ) def test_corner_union(self, index, fname, sname, expected_name): # GH#9943, GH#9862 # Test unions with various name combinations # Do not test MultiIndex or repeats if isinstance(index, MultiIndex) or not index.is_unique: pytest.skip("Not for MultiIndex or repeated indices") # Test copy.union(copy) first = index.copy().set_names(fname) second = index.copy().set_names(sname) union = first.union(second) expected = index.copy().set_names(expected_name) tm.assert_index_equal(union, expected) # Test copy.union(empty) first = index.copy().set_names(fname) second = index.drop(index).set_names(sname) union = first.union(second) expected = index.copy().set_names(expected_name) tm.assert_index_equal(union, expected) # Test empty.union(copy) first = index.drop(index).set_names(fname) second = index.copy().set_names(sname) union = first.union(second) expected = index.copy().set_names(expected_name) tm.assert_index_equal(union, expected) # Test empty.union(empty) first = index.drop(index).set_names(fname) second = index.drop(index).set_names(sname) union = first.union(second) expected = index.drop(index).set_names(expected_name) tm.assert_index_equal(union, expected) @pytest.mark.parametrize( "fname, sname, expected_name", [ ("A", "A", "A"), ("A", "B", None), ("A", None, None), (None, "B", None), (None, None, None), ], ) def test_union_unequal(self, index, fname, sname, expected_name): if isinstance(index, MultiIndex) or not index.is_unique: pytest.skip("Not for MultiIndex or repeated indices") # test copy.union(subset) - need sort for unicode and string first = index.copy().set_names(fname) second = index[1:].set_names(sname) union = first.union(second).sort_values() expected = index.set_names(expected_name).sort_values() tm.assert_index_equal(union, expected) @pytest.mark.parametrize( "fname, sname, expected_name", [ ("A", "A", "A"), ("A", "B", None), ("A", None, None), (None, "B", None), (None, None, None), ], ) def test_corner_intersect(self, index, fname, sname, expected_name): # GH#35847 # Test intersections with various name combinations if isinstance(index, MultiIndex) or not index.is_unique: pytest.skip("Not for MultiIndex or repeated indices") # Test copy.intersection(copy) first = index.copy().set_names(fname) second = index.copy().set_names(sname) intersect = first.intersection(second) expected = index.copy().set_names(expected_name) tm.assert_index_equal(intersect, expected) # Test copy.intersection(empty) first = index.copy().set_names(fname) second = index.drop(index).set_names(sname) intersect = first.intersection(second) expected = index.drop(index).set_names(expected_name) tm.assert_index_equal(intersect, expected) # Test empty.intersection(copy) first = index.drop(index).set_names(fname) second = index.copy().set_names(sname) intersect = first.intersection(second) expected = index.drop(index).set_names(expected_name) tm.assert_index_equal(intersect, expected) # Test empty.intersection(empty) first = index.drop(index).set_names(fname) second = index.drop(index).set_names(sname) intersect = first.intersection(second) expected = index.drop(index).set_names(expected_name) tm.assert_index_equal(intersect, expected) @pytest.mark.parametrize( "fname, sname, expected_name", [ ("A", "A", "A"), ("A", "B", None), ("A", None, None), (None, "B", None), (None, None, None), ], ) def test_intersect_unequal(self, index, fname, sname, expected_name): if isinstance(index, MultiIndex) or not index.is_unique: pytest.skip("Not for MultiIndex or repeated indices") # test copy.intersection(subset) - need sort for unicode and string first = index.copy().set_names(fname) second = index[1:].set_names(sname) intersect = first.intersection(second).sort_values() expected = index[1:].set_names(expected_name).sort_values() tm.assert_index_equal(intersect, expected) def test_intersection_name_retention_with_nameless(self, index): if isinstance(index, MultiIndex): index = index.rename(list(range(index.nlevels))) else: index = index.rename("foo") other = np.asarray(index) result = index.intersection(other) assert result.name == index.name # empty other, same dtype result = index.intersection(other[:0]) assert result.name == index.name # empty `self` result = index[:0].intersection(other) assert result.name == index.name def test_difference_preserves_type_empty(self, index, sort): # GH#20040 # If taking difference of a set and itself, it # needs to preserve the type of the index if not index.is_unique: return result = index.difference(index, sort=sort) expected = index[:0] tm.assert_index_equal(result, expected, exact=True) def test_difference_name_retention_equals(self, index, sort, names): if isinstance(index, MultiIndex): names = [[x] * index.nlevels for x in names] index = index.rename(names[0]) other = index.rename(names[1]) assert index.equals(other) result = index.difference(other) expected = index[:0].rename(names[2]) tm.assert_index_equal(result, expected) def test_intersection_difference_match_empty(self, index, sort): # GH#20040 # Test that the intersection of an index with an # empty index produces the same index as the difference # of an index with itself. Test for all types if not index.is_unique: return inter = index.intersection(index[:0]) diff = index.difference(index, sort=sort) tm.assert_index_equal(inter, diff, exact=True) @pytest.mark.parametrize( "method", ["intersection", "union", "difference", "symmetric_difference"] ) def test_setop_with_categorical(index, sort, method): if isinstance(index, MultiIndex): # tested separately in tests.indexes.multi.test_setops return other = index.astype("category") result = getattr(index, method)(other, sort=sort) expected = getattr(index, method)(index, sort=sort) tm.assert_index_equal(result, expected) result = getattr(index, method)(other[:5], sort=sort) expected = getattr(index, method)(index[:5], sort=sort)
tm.assert_index_equal(result, expected)
pandas._testing.assert_index_equal
# -*- coding: utf-8 -*- """ Created on Thu Jan 9 20:13:44 2020 @author: Adam """ #%% Heatmap generator "Barcode" import os os.chdir(r'C:\Users\Ben\Desktop\T7_primase_Recognition_Adam\adam\paper\code_after_meating_with_danny') import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns import re def join_cols(row): return ''.join(list(row)) def find_favor(seq): t = [] for m in re.finditer(seq, DNA): t += [m.start()] return t DNA = np.loadtxt('./data/DNA.txt', str) DNA = ''.join(DNA) print('DNA Length = {} '.format(len(DNA)) ) start_idxs = [] for m in re.finditer('GTC', DNA): start_idxs += [m.start()] start_idxs = np.array(start_idxs) df = pd.DataFrame() df['loc'] = np.arange(len(DNA)) df['start_ind'] = 0 df.loc[start_idxs,'start_ind'] = 1 favor = pd.read_csv('./data/favor_seqs.csv') gtc_loc = list(favor.iloc[0,:])[0].find('GTC') red_idxs = [] for detsize in range(3,4): dets = favor['seq'].str[ gtc_loc-detsize:gtc_loc + 3 + detsize] dets = list(np.unique(dets)) detslocs = list(map(find_favor, dets)) detslocs = [x for x in detslocs if len(x) > 1] for tlocs in detslocs: mean_dist = np.mean(np.diff(tlocs)) median_dist = np.median(np.diff(tlocs)) if(mean_dist > 1000 and mean_dist < 6000 or median_dist > 1000 and median_dist < 6000): red_idxs += [tlocs] red_idxs = [item for sublist in red_idxs for item in sublist] plt.figure(figsize=(16,4)) plt.bar(start_idxs, [0.3]*len(start_idxs), width=64, color='black', alpha=0.8) plt.bar(red_idxs, [1]*len(red_idxs), width=64, color='red') plt.ylim([0,1]) plt.xlim([0,len(DNA)]) plt.xlabel('DNA nucleotide index') plt.yticks([]) plt.xticks([]) plt.title('\"Intresting\" Sequences') plt.legend(['GTC Locations','Intresting Frequency Locations'], facecolor=(1,1,1,1), framealpha=0.98 ) plt.savefig('./out/favor_seqs_k_3.png') plt.show() #%% Prim VS Primon when POLY is saturated import os os.chdir(r'C:\Users\Ben\Desktop\T7_primase_Recognition_Adam\adam\paper\code_after_meating_with_danny') import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns print('\n=====================================================\n') def mms(t): if(np.max(t) - np.min(t) > 0): t = (t - np.min(t))/(np.max(t) - np.min(t)) else: t = (t)/(np.max(t)) return t def ms(t): return t/np.max(t) def nucs2seq(row): row = list(row) t = ''.join(row) return t # Heatmap for favorite seqs vs all gtc containing seqs df = pd.read_csv('./data/chip_B.csv') df_favor = pd.read_csv('./data/favor_seqs.csv') df['seq'] = list(map( nucs2seq, np.array(df.iloc[:,:-4]) )) tcols = df.columns tcols = list(tcols[:-4]) + ['poly','prim','primo','seq'] df.columns = tcols df['primo-prim'] = df['primo'] - df['prim'] labels = ['poly','primo','prim','primo-prim'] df = df.sort_values('poly').reset_index(drop=True) sm = 100 plt.figure(figsize=(12,8)) for i, lab in enumerate(labels): plt.subplot(4,1,i+1) if(i != 3): df = df.sort_values(lab).reset_index(drop=True) y = df[lab].copy() if(i != 3): y = mms( y )**0.5 y = y.rolling(sm).mean().drop(np.arange(sm)).reset_index(drop=True) y = pd.Series(y) plt.plot(np.arange(len(y)),y, alpha=0.8) plt.title(lab + ' sorted by self') plt.ylabel(' ln(score)' ) plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=1) #%% Collect favorite sequences import os os.chdir(r'C:\Users\Ben\Desktop\T7_primase_Recognition_Adam\adam\paper\code_after_meating_with_danny') import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns print('\n=====================================================\n') labels = ['poly','primo','prim'] def mms(t): if(np.max(t) - np.min(t) > 0): t = (t - np.min(t))/(np.max(t) - np.min(t)) else: t = (t)/(np.max(t)) return t def nucs2seq(row): row = list(row) t = ''.join(row) return t # Heatmap for favorite seqs vs all gtc containing seqs df = pd.read_csv('./data/chip_B.csv') df_favor = pd.read_csv('./data/favor_seqs.csv') df['seq'] = list(map( nucs2seq, np.array(df.iloc[:,:-3]) )) # keep favorite seuqnces (1000~6000 reps) df_test = pd.read_csv('./data/validation.csv') df.index = df['seq'] df = df.loc[df_favor['seq'],:] df = df.dropna(axis=0).reset_index(drop=True) df.columns = list(df.columns[:-4]) + ['poly', 'prim', 'primo', 'seq'] # keep non test set sequences toDrop = df_test['seq'] df.index = df['seq'] df = df.drop(toDrop, axis=0, errors='ignore') df = df.reset_index(drop=True) print('lets unite the data by seq and watch the mean and std of each sequence') dfm = pd.DataFrame() dfm['primo'] = mms(df.groupby('seq').median()['primo']) dfm['primo_std'] = mms(df.groupby('seq').std()['primo'])#/mms( df.groupby('seq').mean()['primo'] ) dfm['prim'] = mms(df.groupby('seq').median()['prim']) dfm['prim_std'] = mms(df.groupby('seq').std()['prim'])#/mms( df.groupby('seq').mean()['poly'] ) dfm['poly'] = mms(df.groupby('seq').median()['poly']) dfm['poly_std'] = mms(df.groupby('seq').std()['poly'])#/mms( df.groupby('seq').mean()['poly'] ) dfm['seq'] = dfm.index dfm = dfm.reset_index(drop=True) T1 = np.percentile(dfm['primo'], 95) T2 = np.percentile(dfm['primo_std'], 90) T3 = np.percentile(dfm['prim'], 95) T4 = np.percentile(dfm['prim_std'], 90) T5 = np.percentile(dfm['poly'], 95) T6 = np.percentile(dfm['poly_std'], 90) print('length of dfm before outlier cleaning = {}'.format(len(dfm)) ) dfm = dfm.drop(np.where(dfm['primo'] > T1 )[0]).reset_index(drop=True) dfm = dfm.drop(np.where(dfm['primo_std'] > T2 )[0]).reset_index(drop=True) dfm = dfm.drop(np.where(dfm['prim'] > T3 )[0]).reset_index(drop=True) dfm = dfm.drop(np.where(dfm['prim_std'] > T4 )[0]).reset_index(drop=True) dfm = dfm.drop(np.where(dfm['poly'] > T5 )[0]).reset_index(drop=True) dfm = dfm.drop(np.where(dfm['poly_std'] > T6 )[0]).reset_index(drop=True) print('length of dfm after outlier cleaning = {}'.format(len(dfm)) ) nucs = np.array(list(map(list, dfm['seq']))).copy() nucs = pd.DataFrame(nucs.copy()) nucs = nucs.add_suffix('_nuc') nucs = nucs.reset_index(drop=True) dfm = pd.concat([dfm, nucs], axis=1) dfm = dfm.reset_index(drop=True) toKeep = [x for x in dfm.columns if 'std' not in x] dfm = dfm.loc[:,toKeep] for lab in labels: dfm.loc[:,lab] = mms(dfm.loc[:,lab]) for lab in labels: dfm.loc[:,lab] = mms(dfm.loc[:,lab]**0.5) dfm.to_csv('data/chip_B_favor.csv', index=False) #%% Heatmap of ABS Correlation import os os.chdir(r'C:\Users\Ben\Desktop\T7_primase_Recognition_Adam\adam\paper\code_after_meating_with_danny') import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns def mms(t): if(np.max(t) - np.min(t) > 0): t = (t - np.min(t))/(np.max(t) - np.min(t)) else: t = (t)/(np.max(t)) return t def count_letters(df_nucs, rep_dict): X = df_nucs.copy() X = X.replace(rep_dict) X = np.array(X) X = np.sum(X,1) return X df = pd.read_csv('data/chip_B_favor.csv') cols = df.columns cols = [x for x in cols if 'nuc' in x] df_nucs = df.loc[:,cols].copy() df_labels = df.loc[:,['primo','prim','poly']] df_res = pd.DataFrame() # count appereances of each individual letter for letter in ['A','C','G','T']: rep_dict = {'A':0,'C':0,'G':0,'T':0} rep_dict[letter] = 1 df_res['{}_count'.format(letter) ] = count_letters(df_nucs, rep_dict) gtc_ind_start = ''.join( list(df_nucs.iloc[0,:]) ).find('GTC') - 5 gtc_ind_end = gtc_ind_start + 5 + 3 + 5 # extract puryn and prymidin densities # A,<NAME> # C,T Prymidins """ =================== Left Side Count =============================== """ rep_dict = {'A':1,'C':0,'G':1,'T':0} df_res['Left_Pur_count'] = count_letters(df_nucs.iloc[:,:gtc_ind_start], rep_dict) rep_dict = {'A':0,'C':1,'G':0,'T':1} df_res['Left_Pry_count'] = count_letters(df_nucs.iloc[:,:gtc_ind_start], rep_dict) """ =================== Center / Determinant Count ===================== """ rep_dict = {'A':1,'C':0,'G':1,'T':0} df_res['Center_Pur_count'] = count_letters(df_nucs.iloc[:,gtc_ind_start:gtc_ind_start], rep_dict) rep_dict = {'A':0,'C':1,'G':0,'T':1} df_res['Center_Pry_count'] = count_letters(df_nucs.iloc[:,gtc_ind_start:gtc_ind_start], rep_dict) """ =================== Right Side Count =============================== """ rep_dict = {'A':1,'C':0,'G':1,'T':0} df_res['Right_Pur_count'] = count_letters(df_nucs.iloc[:,gtc_ind_end:], rep_dict) rep_dict = {'A':0,'C':1,'G':0,'T':1} df_res['Right_Pry_count'] = count_letters(df_nucs.iloc[:,gtc_ind_end:], rep_dict) df_res = pd.concat([df_res, df_labels], axis=1) plt.figure(figsize=(12,8)) df_corr = (df_res.corr().abs()) sns.heatmap(df_corr, cmap="bwr") plt.title('Absolute Correlation') plt.show() plt.figure(figsize=(12,8)) df_corr = df_corr.loc[['primo','prim','poly'],['primo','prim','poly']] sns.heatmap(df_corr, cmap="bwr") plt.title('Absolute Correlation') plt.show() #%% K mers spectrum import os os.chdir(r'C:\Users\Ben\Desktop\T7_primase_Recognition_Adam\adam\paper\code_after_meating_with_danny') import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from sklearn.cluster import KMeans from itertools import product from sklearn.decomposition import PCA from mpl_toolkits.mplot3d import Axes3D from scipy.stats import entropy NMERS = [1,2,3] df = pd.read_csv('./data/chip_B_favor.csv') labels = ['primo','prim','poly'] np.random.RandomState(42) df.index = df['seq'] m2 = 'CCACCCCAAAAAACCCCGTCAAAACCCCAAAAACCA' df.loc[m2,'primo'] im = plt.imread(r'C:\Users\Ben\Desktop/Picture1.png') x = list(range(1,14)) y = [1, 0, 0.4, 0.6, 0.47, 0.13, 0.2, 0.3, 0.5, 0.46, 0.5, 0.67, 0.8] x= np.array(x) y= np.array(y) plt.imshow(im) plt.scatter(x,y, c='red') #for col in labels: #df = df.drop(np.where(df[col] > np.percentile(df[col],95))[0],axis=0).reset_index(drop=True) #df = df.drop(np.where(df[col] < np.percentile(df[col],5))[0],axis=0).reset_index(drop=True) def mms(t): t = (t - np.min(t))/(np.max(t) - np.min(t)) return t for col in labels: df[col] = mms(df[col]) df[col] = np.round(df[col]*2) df[col] = df[col].replace({0:'0weak',1:'1medium',2:'2strong'}) plt.figure(figsize=(18,16)) for i, N in enumerate(NMERS): letters = ['A','C','G','T'] exec('combs_list = list(product(' + 'letters,'*N + '))') combs_list = list(map(''.join,combs_list)) df_mer = pd.DataFrame(np.zeros([len(df), len(combs_list)])) df_mer.columns = combs_list mers = df['seq'].apply(lambda seq: [ seq[i:i+N] for i in range(2, len(seq)-1, 1) ]) mers = (np.array(list(mers)).reshape([len(mers),len(mers[0])])) mers = pd.DataFrame(mers) #coutn mers for comb in combs_list: comb_sum = np.sum(mers == comb,axis=1) df_mer.loc[:,comb] = comb_sum df_mer = np.sum(df_mer) df_mer = df_mer/np.sum(df_mer) df_mer = df_mer[(df_mer >= 0.01 )] plt.subplot(len(NMERS),1,i+1) plt.scatter(np.arange(len(df_mer)), df_mer, color=(['blue','red','green'])[i] ) plt.xticks(np.arange(len(df_mer)), df_mer.index, rotation=90) #plt.legend([' Variance: {}'.format( np.var(df_mer)) ]) plt.title('{}-Mer'.format(N) ) plt.ylim([0, 0.3]) plt.ylabel('mer frequency') #%% K-MEANS and Hirarchial clustering """ Dendogram Plot By TSNE """ import os os.chdir(r'C:\Users\Ben\Desktop\T7_primase_Recognition_Adam\adam\paper\code_after_meating_with_danny') import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from sklearn.cluster import KMeans NLIST = [5] labels = ['poly','prim','primo'] labels = ['primo'] ShowTextOnDendogram = True showKM = True showHC = False def mms(t): t = (t - np.min(t))/(np.max(t) - np.min(t)) return t def OHE(df): cols = [] for i in range(36): for letter in ['A','C','G','T']: cols += [ str(i+1) + '_nuc_' + letter] tdf = pd.get_dummies(df) toAdd = np.setdiff1d(cols, tdf.columns) for col in toAdd: tdf[col] = 0 for col in cols: tdf[col] = tdf[col].astype(int) tdf = tdf.loc[:,cols] return tdf df = pd.read_csv('./data/chip_B_favor.csv') df = pd.concat([OHE(df.drop(labels,axis=1)), df.loc[:,labels]], axis=1) df_backup = df.copy() # ============================================================================= # Hirarchical Clustering # ============================================================================= from scipy.cluster import hierarchy if(showHC): #WORKS FINE X = df_backup.drop(labels,axis=1).copy() X = X.iloc[:,:].reset_index(drop=True) Z = hierarchy.linkage(X, method='ward') Z = pd.DataFrame(Z) botline = Z.iloc[np.argmax(np.diff(Z.iloc[:,-2])),-2] * 1.05 topline = Z.iloc[np.argmax(np.diff(Z.iloc[:,-2])) + 1, -2] * 0.95 fig = plt.figure(figsize=(4, 6)) dn = hierarchy.dendrogram(Z, p=7, truncate_mode='level', color_threshold=40, distance_sort=True) plt.hlines([botline, topline], xmin=0, xmax=len(Z), ls='--', alpha = 0.9 ) plt.ylabel('Ward Distance') disticks = np.unique(np.sqrt(Z.iloc[:,-2]).astype(int)) #plt.yticks( disticks**2 , disticks) plt.xticks([]) plt.xlabel('') Z = hierarchy.linkage(X, method='ward') X[labels] = df_backup[labels].copy() thr = 40 dists = [ 20, 40, 80, 120] fntsze = 22 thr = 40 for i, thr in enumerate(dists): Xg = X.copy() Xg['bin'] = hierarchy.fcluster(Z, thr, criterion='distance', depth=5, R=None, monocrit=None) Xres = Xg.groupby('bin').sum() Xres[labels] = Xg.groupby('bin').median()[labels] xcount = Xg.copy() xcount['count'] = 1 xcount = xcount.groupby('bin').sum()['count'] xcnew = [xcount.iloc[0]/2] for j in xcount.index[1:]: xcnew += [np.sum(xcount[:j-1]) + xcount[j]/2] xcount = pd.Series( xcnew ) xcount.index = xcount.index + 1 #plt.subplot(4,1, i+1 ) #plt.scatter(Xres.index, Xres[labels]) toKeep = [x for x in X.drop(labels, axis=1).columns if '36' not in x] Xres = (Xres.loc[:,toKeep]) Xres.columns = [x[-1] for x in Xres.columns] Xres = Xres.T Xres = Xres.groupby(Xres.index).sum() for col in Xres.columns: Xres[col] = Xres[col] / np.sum(Xres[col]) Xres = Xres.T row_idx = 1 for row_idx in Xres.index: row = Xres.loc[row_idx,:] print( xcount.iloc[row_idx-1] ) accumsize = 0 for dx, lett in enumerate(row.index): x_rng = plt.gca().get_xlim()[1] # ============================================================================= # # ADDING TEXT TO DENDOGRAM # ============================================================================= if(ShowTextOnDendogram == True): plt.text(x= xcount.iloc[row_idx-1]*x_rng/len(Xg) + accumsize, y=thr, horizontalalignment='left', s=lett, fontsize=np.max([fntsze*row[lett], 6]) , weight='normal', fontname='arial') accumsize += np.max([fntsze*row[lett], 8]) + 36 #% TODO MAKE THIS PRETTY from sklearn.metrics import silhouette_score res_ss = [] xvec = [5] for i in xvec: X = df.copy().drop(['bin'], axis=1, errors='ignore') X = X.drop(labels, axis=1) tmp_ss = [] for j in range(1): km = KMeans(i, random_state=j ) y = km.fit_predict(X) ss = silhouette_score( X, y ) tmp_ss += [ss] print('sil score => mean: {} | std: {}'.format(np.mean(tmp_ss), np.std(tmp_ss)) ) res_ss += [np.mean(tmp_ss)] plt.figure() plt.scatter(xvec,res_ss) plt.xlabel('K-Value') plt.ylabel('Sil Score') plt.show() if(showKM): col = 'primo' plt.figure(figsize=(6,4)) for i, Nbins in enumerate(NLIST): df = df_backup.copy() km = KMeans(Nbins, random_state=42 ) df['bin'] = km.fit_predict(df.drop(labels,axis=1)) cc = np.array(km.cluster_centers_).reshape(km.cluster_centers_.shape[0], km.cluster_centers_.shape[1]//4, 4) cc = np.array(pd.DataFrame(np.argmax(cc,axis=2)).replace({0:'A',1:'C',2:'G',3:'T'})) centers = [''.join(l) for l in cc] tdf = df.loc[:,['bin',col]] #rep_d = {0:'A',1:'B',2:'C',3:'D',4:'E'} rep_d = {0:2,1:3,2:0,3:1,4:4} df['bin'] = df['bin'].replace(rep_d) centers = list(np.array(centers)[list(rep_d.values())]) print('Mean Words:') print(centers) #rep_d = {'A':2,'B':3,'C':0,'D':1,'E':4} #df['bin'] = df['bin'].replace(rep_d) plt.subplot(len(NLIST),1,i+1) sns.violinplot(x="bin", y=col, data=df, palette="Blues", cut=0) plt.ylim([-0.2, 1.2]) plt.ylabel('Primase \nBinding Scores', fontsize=12) plt.title('Scores Distribution by Cluster', fontsize=12) """ for tx, tcent in zip(np.arange(np.max(tdf['bin'])+1) , centers): chunks, chunk_size = len(tcent), len(tcent)//6 stlist = [ tcent[i:i+chunk_size] for i in range(0, chunks, chunk_size) ] tcent = '\n'.join(stlist) t = plt.text(x=tx-0.5, y=0, s=tcent, fontsize=10, color='red', fontweight='normal', backgroundcolor='white') t.set_bbox(dict(facecolor='white', alpha=0.7, edgecolor='white')) plt.xlim([-1, Nbins-1 + 0.5]) """ #plt.xticks( np.arange(np.max(tdf['bin'])+1) #,centers , rotation=-90, fontsize=12) plt.yticks( [0,0.25,0.5,0.75,1], fontsize=12 ) plt.tight_layout() plt.savefig('./out/kmeans/forpaper_B_centroids_' + str(Nbins) + 'bins') plt.show() #plt.close() #%% PCA import os os.chdir(r'C:\Users\Ben\Desktop\T7_primase_Recognition_Adam\adam\paper\code_after_meating_with_danny') import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from sklearn.cluster import KMeans from itertools import product from sklearn.decomposition import PCA from mpl_toolkits.mplot3d import Axes3D NMERS = [3] df = pd.read_csv('./data/chip_B_favor.csv') #labels = ['primo','prim','poly'] labels = ['primo'] np.random.RandomState(42) def mms(t): t = (t - np.min(t))/(np.max(t) - np.min(t)) return t """ for col in labels: df[col] = mms(df[col]) df[col] = np.round(df[col]*2) df[col] = df[col].replace({0:'0weak',1:'1medium',2:'2strong'}) """ for N in NMERS: letters = ['A','C','G','T'] exec('combs_list = list(product(' + 'letters,'*N + '))') combs_list = list(map(''.join,combs_list)) df_mer = pd.DataFrame(np.zeros([len(df), len(combs_list)])) df_mer.columns = combs_list mers = df['seq'].apply(lambda seq: [ seq[i:i+N] for i in range(2, len(seq)-1, 1) ]) mers = (np.array(list(mers)).reshape([len(mers),len(mers[0])])) mers = pd.DataFrame(mers) #coutn mers for comb in combs_list: comb_sum = np.sum(mers == comb,axis=1) df_mer.loc[:,comb] = comb_sum pca = PCA(n_components=np.min([16,len(df_mer.columns)]), svd_solver='auto', random_state=42) df_mer = pd.DataFrame(pca.fit_transform(df_mer.dropna(axis=1))) df_mer = df_mer.add_prefix('pc') #MMS -1 1 for col in df_mer.columns: df_mer[col] = mms(df_mer[col]) for col in labels: df_mer[col] = df[col] np.cumsum(pca.explained_variance_ratio_) 1/0 # 3D scatter for lab in labels: fig = plt.figure(figsize=(14,10)) ax = fig.add_subplot(111, projection='3d') x = df_mer['pc0'] y = df_mer['pc1'] z = df_mer['pc2'] clrs = mms( (df_mer[lab]) ) ax.scatter3D(2*x + 0.05*np.random.randn(len(x)) , 2*y + 0.05*np.random.randn(len(y)) , 2*z + 0.05*np.random.randn(len(z)) , alpha=0.6, c=clrs, cmap='bwr') plt.xlabel('pc0') plt.ylabel('pc1') ax.set_zlabel('pc2') plt.title('{}: {}-mer projection'.format(lab,N) ) plt.show() """ PUT A COMMENT TO SEE 3D Projection """ #plt.close() fig = plt.figure(figsize=(14,10)) x = df_mer['pc0'] y = df_mer['pc1'] plt.scatter( x-0.5, #+ 0.05*np.random.randn(len(x)) , y-0.5, #+ 0.05*np.random.randn(len(y)) , alpha=0.6, c=clrs, cmap='bwr' ) plt.xlabel('pc0') plt.ylabel('pc1') plt.title('{}: {}-mer projection'.format(lab,N) ) plt.savefig('./out/pca/{}_{}mer'.format(lab,N) ) plt.show() """ PUT A COMMENT TO SEE 2D Projection """ #plt.close() #%% Dynamic clustering and prediction """ This techinique invloves all of our research, by using PCA we learn the existence of 5 clusters, by using kmeans we classify each sequence to its cluster, by using regressors suchj as lasso we train a model for each cluster and predict labels with high resolution. we can compare results with or without dynamic clustering. """ """ Dendogram Plot By TSNE """ import os os.chdir(r'C:\Users\Ben\Desktop\T7_primase_Recognition_Adam\adam\paper\code_after_meating_with_danny') import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from sklearn.cluster import KMeans import pickle from time import clock, sleep [plt.close() for x in plt.get_fignums()] N = 3 with_clustering = True stime = clock() #labels = ['poly','prim','primo'] labels = ['primo'] def OHE(df): cols = [] for i in range(36): for letter in ['A','C','G','T']: cols += [ str(i+1) + '_nuc_' + letter] tdf = pd.get_dummies(df) toAdd = np.setdiff1d(cols, tdf.columns) for col in toAdd: tdf[col] = 0 for col in cols: tdf[col] = tdf[col].astype(int) tdf = tdf.loc[:,cols] return tdf df = pd.read_csv('./data/chip_B_favor.csv') df = pd.concat([OHE(df.drop(labels,axis=1)), df.loc[:,labels]], axis=1) # apply KMEANS km = KMeans(5, random_state=42, n_init=20 ) bins_pred = km.fit_predict(df.drop(labels,axis=1)) pickle.dump(km, open('./out/regressors/models/km.sav' , 'wb') ) t = km.cluster_centers_ cc = np.array(km.cluster_centers_).reshape(km.cluster_centers_.shape[0], km.cluster_centers_.shape[1]//4, 4) cc = np.array(pd.DataFrame(np.argmax(cc,axis=2)).replace({0:'A',1:'C',2:'G',3:'T'})) centers = [''.join(l) for l in cc] df = pd.read_csv('./data/chip_B_favor.csv') df['bin'] = bins_pred """ # Hard To Predict (HTP) Generator htpgen = pd.DataFrame(np.random.randint(0,4,[5000, 36])).replace({0:'A',1:'C',2:'G',3:'T'}) htpgen = htpgen.add_suffix('_nuc') htpgen = OHE(htpgen) htpgen['bin'] = km.predict(htpgen) # Easy To Predict (HTP) Generator etpgen = pd.DataFrame(np.random.randint(0,4,[5000, 36])).replace({0:'A',1:'C',2:'G',3:'T'}) etpgen = etpgen.add_suffix('_nuc') etpgen = OHE(etpgen) etpgen['bin'] = km.predict(etpgen) t = np.array(htpgen.iloc[:,-1]) 1/0 """ from itertools import product from sklearn.metrics import mean_absolute_error as mae from sklearn.model_selection import cross_validate #from sklearn.linear_model import LassoLarsIC from sklearn.linear_model import Lasso from sklearn.ensemble import RandomForestRegressor from sklearn.neighbors import KNeighborsRegressor import xgboost as xgb test_df = pd.read_csv('./data/validation.csv').loc[:,['seq','toKeep','label']] test_df = test_df.iloc[np.where(test_df['toKeep'] > 0)[0],:].reset_index(drop=True) test_df = test_df.loc[:,['seq','label']] splitted = pd.DataFrame(np.zeros([len(test_df),36])) splitted = splitted.add_suffix('_nuc') for i,seq in enumerate(test_df['seq']): splitted.iloc[i,:] = list(seq) def mms(t): t = (t - np.min(t))/(np.max(t) - np.min(t)) return t for col in labels: df[col] = mms(df[col]) splitted = OHE(splitted) splitted['bin'] = km.predict(splitted) test_df['bin'] = splitted['bin'] letters = ['A','C','G','T'] exec('combs_list = list(product(' + 'letters,'*N + '))') combs_list = list(map(''.join,combs_list)) #Train preparation df_mer = pd.DataFrame(np.zeros([len(df), len(combs_list)])) df_mer.columns = combs_list mers = df['seq'].apply(lambda seq: [ seq[i:i+N] for i in range(2, len(seq)-1, 1) ]) mers = (np.array(list(mers)).reshape([len(mers),len(mers[0])])) mers = pd.DataFrame(mers) df_mer['seq'] = df['seq'] #forFUN ACOUNT = [ x.count('A') for x in df['seq'] ] CCOUNT = [ x.count('C') for x in df['seq'] ] GCOUNT = [ x.count('G') for x in df['seq'] ] TCOUNT = [ x.count('T') for x in df['seq'] ] #count mers for comb in combs_list: comb_sum = np.sum(mers == comb,axis=1) df_mer.loc[:,comb] = comb_sum X = df_mer.copy() X['bin'] = df['bin'] #plt.plot( (X.sum()[:-2]).sort_values() ) #X.iloc[:,:-2] = X.iloc[:,:-2]/list(np.sum(X.iloc[:,:-2])) train = X.copy() y = df[labels] """ Drek = pd.concat([train.drop('seq',axis=1), pd.DataFrame(y)], axis=1) Drek.iloc[:,:-1] /= Drek.iloc[:,:-1].max() Drek = Drek.drop('GTC',axis=1, errors='ignore') Drek = Drek.corr('spearman').abs() plt.figure(figsize=(12,12)) sns.heatmap(Drek, cmap='bwr') plt.show() 1/0 """ #Test preparation df_mer = pd.DataFrame(np.zeros([len(test_df), len(combs_list)])) df_mer.columns = combs_list mers = test_df['seq'].apply(lambda seq: [ seq[i:i+N] for i in range(2, len(seq)-1, 1) ]) mers = (np.array(list(mers)).reshape([len(mers),len(mers[0])])) mers = pd.DataFrame(mers) #count mers for comb in combs_list: comb_sum = np.sum(mers == comb,axis=1) df_mer.loc[:,comb] = comb_sum test = df_mer.copy() test['bin'] = test_df['bin'] y_test = test_df['label'] X_test = test.copy().reset_index(drop=True) y_test = y_test.copy().reset_index(drop=True) p_test = np.zeros(len(y_test)) X_train = train.copy().reset_index(drop=True) if( with_clustering == False): X_train['bin'] = 0 y_train = y.copy().reset_index(drop=True) mean_mae_per_lab = [] df_results = pd.DataFrame() res_label = [] res_tbin = [] res_mae = [] res_fi = [] res_bias = [] bin_weights = [] tstr = '' for lab in labels: mean_mae_per_bin = [] print("\n==============================") print('label = {}'.format(lab) ) ber = pd.DataFrame(np.zeros([5,len(np.unique(X_train['bin']))])) ber = ber.add_prefix('bin_') for tbin in np.unique(X_train['bin']): """ drek = X_train.copy() drek['primo'] = y_train.copy() drek = drek.sort_values(['bin','primo']).reset_index(drop=True) xax = [] for i in range(5): xax += list(range(sum(drek['bin'] == i))) drek['xax'] = xax plt.figure(figsize=(8,8)) sns.lineplot( x='xax' ,y='primo', hue='bin', data=drek ) """ test_strong = pd.DataFrame() test_weak = pd.DataFrame() yv = (y_train.loc[:,lab].iloc[np.where(X_train['bin'] == tbin)[0]]) Xv = X_train.iloc[np.where(X_train['bin'] == tbin)[0]].copy().drop(['bin','seq'],axis=1) #plt.figaspect(1) #h_0 = np.histogram(yv, bins=len(yv)) #cdf_0 = np.cumsum(np.sort( h_0[0]/len(yv))) #plt.plot( [0] + list(h_0[1][1:]), [0] + list(cdf_0) ) #plt.plot( [0,1],[0,1] ) #tb = pd.concat([Xv, yv], axis=1) #plt.plot( np.sort( 1/np.sort(h_0[0]) *yv) ) """ Drek = pd.concat([Xv, pd.DataFrame(yv)], axis=1) Drek.iloc[:,:-1] /= Drek.iloc[:,:-1].max() Drek = Drek.drop('GTC',axis=1) Drek = Drek.corr().abs() plt.figure() sns.heatmap(Drek, cmap='bwr') plt.show() continue """ print(len(Xv)) tst_idxs = np.where(X_test['bin'] == tbin)[0] tst_idxs = np.array(list(tst_idxs)) if( len(tst_idxs) != 0 ): yt = y_test.iloc[tst_idxs].copy() #initiate Test Set test_strong = X_test.iloc[yt[yt==1].index].drop('bin',axis=1) test_weak = X_test.iloc[yt[yt==0].index].drop('bin',axis=1) #reg = LassoLarsIC('bic', max_iter=200, fit_intercept=False, positive=True) #reg = LassoLarsIC('bic', max_iter=200, normalize=False, fit_intercept=False, positive=True) #reg = xgb.XGBRegressor(objective ='reg:linear', colsample_bytree = 0.3, learning_rate = 0.1, # max_depth = 8, alpha = 10, n_estimators = 10) # Regression Fitting from copy import deepcopy regs = [] tmp_preds = [] for rs in range(5): """ We are going to test several regressors: KNN, RBF-SVM, Linear-SVM, RF, XGBOOST """ from sklearn.neighbors import KNeighborsRegressor from sklearn.svm import SVR from sklearn.ensemble import RandomForestRegressor import xgboost as xgb #reg = RandomForestRegressor(max_depth = 8, random_state=rs) #reg = LassoLarsIC('aic', max_iter=200, normalize=False, fit_intercept=False, positive=True) #reg = KNeighborsRegressor(n_neighbors=2) #reg = Lasso(alpha=0.00025, normalize=False, fit_intercept=True, positive=False) reg = Lasso(alpha=0.00025, normalize=True, fit_intercept=False, positive=True) # This is the model we actually use #reg = KNeighborsRegressor(15) #reg = SVR(kernel='rbf') #reg = SVR(kernel='linear') #reg = RandomForestRegressor() #reg = xgb.XGBRegressor() idxs_pool = list(Xv.index) train_idxs = np.random.choice( idxs_pool, size=4*len(idxs_pool)//5, replace=False ) train_idxs = np.sort(train_idxs) tX = Xv.loc[train_idxs,:].copy() ty = yv.loc[train_idxs].copy() pX = Xv.drop(labels=train_idxs).copy() py = yv.drop(labels=train_idxs).copy() tX = tX pX = pX reg.fit(tX, ty) pred = reg.predict(pX) from sklearn.metrics import mean_absolute_error print('K-Fold Iter: {}, MAE: {:2.3f}'.format(rs, mean_absolute_error(py,pred)) ) tmp_preds += [pred.copy()] regs += [deepcopy(reg)] ber.iloc[rs,tbin] = mean_absolute_error(py,pred) #plt.plot( np.arange(len(py)), pd.Series(np.abs(py - pred)).expanding().mean() ) from sklearn.metrics import mean_squared_error print('RMSE: {:2.3f}'.format( np.sqrt(mean_squared_error(py,pred)) ) ) print('BER: {:2.3f}'.format(np.mean(ber.iloc[:,tbin])) ) print('==================\nTotal BER: {:2.3f}'.format(np.mean(np.mean(ber))) ) reg = regs[np.argmin(np.array(ber.iloc[:,tbin]))] pred = tmp_preds[np.argmin(np.array(ber.iloc[:,tbin]))] if(with_clustering == False): plt.scatter(py,pred, alpha=0.8, s=4, zorder=2 ) plt.plot([0,1],[0,1]) plt.xlabel('True') plt.ylabel('Prediction') plt.gca().set_aspect('equal') """ else: plt.scatter(py,pred, alpha=0.8, s=4, zorder=2 ) if(tbin == 4): plt.plot([0,1],[0,1], color='black', ls='--', zorder = 1, alpha=0.8) plt.xlim([-0.05,1.05]) plt.ylim([-0.05,1.05]) plt.gca().set_aspect('equal') plt.legend( ['y=x'] + list(centers), fontsize='x-small') plt.xlabel('true') plt.ylabel('pred') """ """ res = cross_validate(reg, Xv , y=yv, groups=None, scoring='neg_mean_absolute_error', cv=5, n_jobs=5, verbose=0, fit_params=None, return_estimator=True) best_estimator = res['estimator'][np.argmax(res['test_score'])] """ best_estimator = reg ber['test_score'] = -ber.iloc[:,tbin].copy() res = ber.copy() mean_estimator_mae = -np.mean(res['test_score']) mean_estimator_std = np.std(res['test_score']) print('\033[1m cv mean: {:2.3f} | cv std: {:2.3f} \033[0m'.format(mean_estimator_mae, mean_estimator_std) ) # Save best model and collect resutls pickle.dump(best_estimator, open('./out/regressors/models/{}_{}.sav'.format(lab, tbin) , 'wb') ) tmp_err = np.min(-res['test_score']) #mean_mae_per_bin += [ tmp_err*len(np.where(X_train['bin'] == tbin)[0])/len(X_train)] mean_mae_per_bin += [ tmp_err ] #print(lab + ' => bin: ' + str(tbin) + ' | MAE: {:2.3f}'.format(tmp_err) ) tstr = tstr + lab + ' => bin: ' + str(tbin) + ' | MAE: {:2.3f}\n'.format(tmp_err) if(len(test_strong) > 0): p_test[test_strong.index] = list(best_estimator.predict(test_strong)) if(len(test_weak) > 0): p_test[test_weak.index] = list(best_estimator.predict(test_weak)) res_label += [lab] res_tbin += [tbin] res_mae += [ np.round(mean_mae_per_bin[-1], 3)] if( 'Lasso' in str(reg.__repr__)[:60]): res_fi += [ list(zip(np.array(best_estimator.coef_), Xv.columns)) + [(best_estimator.intercept_,'Bias')] ] else: res_fi += [[0]] mean_mae_per_bin[-1] = mean_mae_per_bin[-1]#*len(np.where(X_train['bin'] == tbin)[0])/len(X_train) bin_weights += [len(np.where(X_train['bin'] == tbin)[0])/len(X_train)] mean_mae_per_lab += [np.sum(mean_mae_per_bin) ] print("=================\nMean Label MAE = {:2.3f} | STD MAE = {:2.3f}".format( np.mean(mean_mae_per_bin), np.std(mean_mae_per_bin) ) ) strong_pred = p_test[y_test == 1] weak_pred = p_test[y_test == 0] plt.figure(figsize=(8,4)) [freqs,bns] = np.histogram(y_train.loc[:,lab], bins=10, weights=[1/len(y_train)]*len(y_train) ) plt.barh(y=bns[:-1] + 0.05, width=freqs*10, height=0.1, alpha=0.4, zorder=1) plt.xlim([-1, len(strong_pred)+1]) sns.distplot(y, hist=False, color='black', bins=len(y), kde_kws={'cut':3}) sns.distplot(weak_pred, hist=False, color='blue') t = sns.distplot(strong_pred, hist=False, color='red') plt.close() def isclose(a, b, abs_tol=0.001): return abs(a-b) <= abs_tol colors = ['black', 'blue', 'red'] labs = ['Train', 'Test - Weak', 'Test - Strong'] plt.figure() for cc, unnor in enumerate(t.get_lines()): newy = (unnor.get_ydata())/np.sum(unnor.get_ydata()) plt.plot(unnor.get_xdata(), newy, color=colors[cc], label=labs[cc]) if(cc == 1): tnewy = [] newx = unnor.get_xdata() for twp in weak_pred: cands = (np.where([ isclose(tx, twp, 0.005) for tx in newx])[0]) tnewy.append(cands[len(cands)//2]) plt.scatter(weak_pred, newy[tnewy], color=colors[cc], label=None) if(cc == 2): tnewy = [] newx = unnor.get_xdata() for twp in strong_pred: cands = (np.where([ isclose(tx, twp, 0.005) for tx in newx])[0]) tnewy.append(cands[len(cands)//2]) plt.scatter(strong_pred, newy[tnewy], color=colors[cc], label=None) plt.ylim([0,0.04]) plt.xlim([0,1]) plt.title('Binding Scores Approximated Distributions', fontsize=14) plt.legend() plt.xlabel('Binding Score', fontsize=12) plt.ylabel('$Probability(Score)$', fontsize=12) 1/0 """ 1/0 def d2r(d): return d*3.14/180 [freqs,bns] = np.histogram(y_train.loc[:,lab], bins=64, weights=[1/len(y_train)]*len(y_train) ) sns.distplot( y_train.loc[:,lab], bins=8, hist=True,norm_hist=True, kde=False ) plt.scatter(strong_pred, strong_pred) ax = plt.subplot(111, projection='polar') [freqs,bns] = np.histogram(y_train.loc[:,lab], bins=64, weights=[1/len(y_train)]*len(y_train) ) sns.distplot(y_train.loc[:,lab]*d2r(360), bins=8, hist=True, norm_hist=True, kde=False , ax=ax) ax.set_xlabel('$P(V>v)$') #tfr = 1-freqs.cumsum() #tfr plt.xticks( [d2r(x) for x in np.arange(0,360,45)], ['A{}'.format(x) for x in np.arange(0,360,45)] ) #plt.scatter( freqs[(10*strong_pred).astype(int)]*(360), strong_pred ) #.plt.scatter( freqs[(10*strong_pred).astype(int)]*(360), strong_pred ) plt.scatter( strong_pred*d2r(360), strong_pred/2 ) plt.scatter( weak_pred*d2r(360), weak_pred/2, zorder=10 ) #ax.bar( bns[1:]*360,freqs , width=0.2, alpha=0.4 ) spr = (np.round(strong_pred*100)//10).astype(int) wpr = (np.round(weak_pred*100)//10).astype(int) fr = np.round(freqs*100) frcs = 1-freqs.cumsum() frcs = np.concatenate( [[1], frcs[1:-1], [0]] ) plt.plot(frcs) plt.scatter( fr[spr], strong_pred ) plt.scatter( fr[wpr], weak_pred ) ax = plt.subplot(111, projection='polar') ax.plot([d2r(x) for x in np.linspace(0,360,36)], [np.mean(strong_pred)]*36, lw=8, alpha=0.2, color='red') ax.plot([d2r(x) for x in np.linspace(0,360,36)], [np.mean(weak_pred)]*36, lw=8, alpha=0.2, color='blue') thetas = [d2r(x) for x in np.linspace(0,360,8+1)[:-1]] #ax.plot(thetas, strong_pred, 'r^', color='red') #ax.plot(thetas, weak_pred, 'rv', color='blue') ax.plot(thetas + [0], list(strong_pred) + [strong_pred[0]], '', color='red') ax.plot(thetas + [0], list(weak_pred) + [weak_pred[0]], '', color='blue') ax.set_rlabel_position(0) #ax.set_rticks( [0,1],[2,'b'])#['']*len(np.arange(0,1.2,0.2))) #ax.set_thetagrids([90,270]) #ax.set_rgrids() #ax.set_yticks([]) #ax.set_ylim([0,1.1]) ax.set_xticks([]) _ = [ax.plot([d2r(x) for x in np.linspace(0,360,36)], [v]*36, alpha=0.1, color='black') for v in np.arange(0,1,0.1)] ax = plt.subplot(111, projection='polar') #ax.plot([d2r(x) for x in np.linspace(0,360,36)], [np.mean(strong_pred)]*36, lw=8, alpha=0.2, color='red') #ax.plot([d2r(x) for x in np.linspace(0,360,36)], [np.mean(weak_pred)]*36, lw=8, alpha=0.2, color='blue') thetas = [d2r(x) for x in np.linspace(0,360,8+1)[:-1]] #ax.plot(thetas, strong_pred, 'r^', color='red') #ax.plot(thetas, weak_pred, 'rv', color='blue') ax.plot(thetas + [0], list(strong_pred) + [strong_pred[0]], '', color='red') ax.plot(thetas + [0], list(weak_pred) + [weak_pred[0]], '', color='blue') ax.set_rlabel_position(0) ax.set_rticks( [0,1],[2,'b'])#['']*len(np.arange(0,1.2,0.2))) #ax.set_thetagrids([90,270]) #ax.set_rgrids() ax.set_yticks([]) ax.set_xticks(np.arange(10), 1-np.cumsum(freqs)) #ax.set_ylim([0,1.1]) ax.set_xticks([]) [ax.plot([d2r(x) for x in np.linspace(0,360,36)], [v]*36, alpha=0.1, color='black') for v in np.arange(0,1,0.1)] tmp_df_for_show = pd.DataFrame() tmp_df_for_show['theta'] = list(thetas)*2 tmp_df_for_show['val'] = np.round(list(strong_pred) + list(weak_pred),3) tmp_df_for_show['set'] = [1]*8 + [0]*8 #sns.FacetGrid(data=tmp_df_for_show, col="theta", hue="set", height="val") g = sns.FacetGrid(tmp_df_for_show,subplot_kws=dict(projection='polar'), height=4.5, sharex=False, sharey=False, despine=False) g.map(sns.scatterplot,data=tmp_df_for_show, x='theta', y='val', hue='set') #ax.bar(bns[:-1], freqs) """ plt.xticks([]) plt.savefig('./out/regressors/{}_{}_{}'.format(N, y_test.name, 'LassoIC') ) plt.show() #plt.close() print(tstr) etime = clock() print('Runtime: {:5.2f} [Seconds]'.format(etime-stime) ) df_results['label'] = res_label df_results['tbin'] = res_tbin df_results['fi'] = res_fi df_results['mae'] = res_mae #df_results['w_mae'] = np.array([ [mean_mae_per_lab[0]]*5, [mean_mae_per_lab[1]]*5, [mean_mae_per_lab[2]]*5]).reshape(-1) df_results['w_mae'] = np.multiply(mean_mae_per_bin,bin_weights ) df_results.to_csv('./out/regressors/weighted_lasso.csv',index=False) cv_res = pd.DataFrame({'MAE':np.mean(ber), 'STD':np.std(ber)}) print(centers) #print(cv_res) #print( 'Final WMAE = {:2.3f}'.format( np.sum(cv_res.iloc[:-1,0]*bin_weights) ) ) print( 'Final WMAE = {:2.3f}'.format( np.sum(df_results['w_mae']) ) ) 1/0 lofi = [] for tbin in range(len(res_fi)): ltz = np.where(np.array(res_fi)[tbin][:,0].astype(float) != 0)[0] ifs = np.array(res_fi)[tbin][ltz,:] ifs = [ [x[1], x[0]] for x in list(map(list, ifs))] ifs = [ [x[0], np.round(float(x[1]),4) ] for x in ifs] ifs = list(np.array(ifs)[np.argsort( np.abs(np.array(ifs)[:,1].astype(float)) )[-1::-1]]) ifs = list(map(list, ifs)) lofi += [ifs] toPrint = list((dict(ifs).items()))[:5] print(tbin, ' => ', toPrint) df_results['fi'] = lofi df_results.to_csv('./out/regressors/light_weighted_lasso.csv',index=False) #% fi_per_bin = df_results['fi'].copy() exec('combs_list = list(product(' + 'letters,'*N + '))') combs_list = list(map(''.join,combs_list)) df_fi = pd.DataFrame(np.zeros([5,len(combs_list)])) df_fi.columns = combs_list for i in range(len(fi_per_bin)): tf = fi_per_bin[i] df_fi.loc[i, list(np.array(tf)[:,0])] = (np.array(tf)[:,1]).astype(float) zero_importance = list(df_fi.columns[np.where(df_fi.sum() == 0)]) zero_importance.remove('GTC') sorted_imp = (df_fi.replace({0:np.nan}).median().sort_values()) sorted_imp = sorted_imp.fillna(0) sorted_imp = sorted_imp[sorted_imp > 0] sorted_imp = sorted_imp.sort_values() sorted_imp = sorted_imp[-10:] plt.figure() plt.subplot(2,1,1) sns.scatterplot(x=sorted_imp.index, y=sorted_imp) plt.xticks(sorted_imp.index, rotation=60) plt.title('Kmers Median Coefficients') plt.ylim([-0.01, 0.2]) plt.subplot(2,1,2) sns.scatterplot(x=zero_importance, y=[0]*len(zero_importance)) plt.xticks(zero_importance, rotation=60) plt.title('Kmers Non Important') plt.ylim([-0.01, 0.2]) plt.tight_layout() plt.show() #%% IGNORE 20.4.20 1/0 #PLOTTER - Dynamic clustering and prediction """ This techinique invloves all of our research, by using PCA we learn the existence of 5 clusters, by using kmeans we classify each sequence to its cluster, by using regressors suchj as lasso we train a model for each cluster and predict labels with high resolution. we can compare results with or without dynamic clustering. """ """ Dendogram Plot By TSNE """ import os os.chdir(r'C:\Users\Ben\Desktop\T7_primase_Recognition_Adam\adam\paper\code_after_meating_with_danny') from itertools import product import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from sklearn.cluster import KMeans import pickle def OHE(df): cols = [] for i in range(36): for letter in ['A','C','G','T']: cols += [ str(i+1) + '_nuc_' + letter] tdf = pd.get_dummies(df) toAdd = np.setdiff1d(cols, tdf.columns) for col in toAdd: tdf[col] = 0 for col in cols: tdf[col] = tdf[col].astype(int) tdf = tdf.loc[:,cols] return tdf for ClusterSize in [0,1]: for KMER in [1,2,3,4]: print('\n========================================================') print('========================================================') print( ['Without Clustering','With Clustering'][ClusterSize] ) print( '{}-Mer'.format(KMER) ) N = KMER #labels = ['poly','prim','primo'] labels = ['primo'] with_clustering = True df = pd.read_csv('./data/chip_B_favor.csv') df = pd.concat([OHE(df.drop(labels,axis=1)), df.loc[:,labels]], axis=1) # apply KMEANS km = KMeans(5, random_state=42 ) bins_pred = km.fit_predict(df.drop(labels,axis=1)) pickle.dump(km, open('./out/regressors/models/km.sav' , 'wb') ) df =
pd.read_csv('./data/chip_B_favor.csv')
pandas.read_csv
# The normal imports import numpy as np from numpy.random import randn import pandas as pd # Import the stats library from numpy from scipy import stats # These are the plotting modules adn libraries we'll use: import matplotlib as mpl import matplotlib.pyplot as plt import seaborn as sns # Now we'l learn how to combine plot styles # Create datset dataset = randn(100) # Use distplot for combining plots, by default a kde over a histogram is shown sns.distplot(dataset, bins=25) # hist, rug, and kde are all input arguments to turn those plots on or off sns.distplot(dataset, rug=True, hist=False) # TO control specific plots in distplot, use [plot]_kws argument with # dictionaries. # Here's an example sns.distplot(dataset, bins=25, kde_kws={'color': 'indianred', 'label': 'KDE PLOT'}, hist_kws={'color': 'blue', 'label': "HISTOGRAM"}) # WE can also use pandas data objects for this from pandas import Series # Create Series form dataset ser1 =
Series(dataset, name='My_DATA')
pandas.Series
from polo2 import PoloDb import pandas as pd """ Consider deleting this; it has been replaced by Elements in the Flask app. However, there may be wisdom in putting something here for more general purpose use. """ class PoloReport(): def __init__(self, config, trial_name='trial1'): # Set some values if trial_name not in config.trials: raise ValueError("Invalid trail name `{}`.format(trial)") self.config = config self.trial = trial_name self.slug = self.config.ini['DEFAULT']['slug'] self.base_path = self.config.ini['DEFAULT']['base_path'] self.thresh = float(self.config.ini['DEFAULT']['thresh']) # Load the databases corpus_db_file = self.config.generate_corpus_db_file_path() model_db_file = self.config.generate_model_db_file_path(self.trial) self.corpus = PoloDb(corpus_db_file) self.model = PoloDb(model_db_file) # EXPERIMENTAL def get_row_count(self, table): n = pd.read_sql_query('SELECT count(*) AS n FROM {}'.format(table), self.corpus.conn).n.tolist()[0] return n def get_doc_count(self): self.doc_count = pd.read_sql_query('SELECT count(*) AS n FROM doc', self.corpus.conn).n.tolist()[0] return self.doc_count def get_topic_count(self): self.topic_count = pd.read_sql_query('SELECT count(*) AS n FROM topic', self.corpus.conn).n.tolist()[0] return self.topic_count def get_topic_list(self, by_alpha = True): topics = self.model.get_table('topic', set_index=True) alpha_max = topics.topic_alpha.max() alpha_min = topics.topic_alpha.min() from scipy import stats topics['topic_alpha_zscore'] = stats.zscore(topics.topic_alpha) num_topics = len(topics.index) sql = "SELECT topic_id, GROUP_CONCAT(topic_phrase, ', ') as phrases FROM topicphrase " \ "GROUP BY topic_id ORDER BY phrase_weight DESC" phrases = pd.read_sql_query(sql, self.model.conn) phrases.set_index('topic_id', inplace=True) cards = [] if by_alpha: topic_id_list = topics.topic_alpha.sort_values(ascending=False).index.tolist() else: topic_id_list = range(num_topics) for topic_id in topic_id_list: card = dict( topic_id = topic_id, topic_alpha = topics.loc[topic_id].topic_alpha, topic_alpha_zscore = topics.loc[topic_id].topic_alpha_zscore, topic_phrases = phrases.loc[topic_id].phrases, topic_words = topics.loc[topic_id].topic_words ) cards.append(card) return cards def get_prhases_for_topic(self, topic_id): sql = "SELECT topic_phrase FROM topicphrase WHERE topic_id = {} ORDER BY phrase_weight DESC".format(topic_id) phrases = ', '.join(
pd.read_sql_query(sql, self.model.conn)
pandas.read_sql_query
from .utilities import format_ft_comp, format_end2end_prompt, split_multi_answer from .configs import ANSWER_COL, INCORRECT_COL from datasets import load_metric import openai import numpy as np import pandas as pd import warnings from t5.evaluation import metrics from time import sleep import logging logger = logging.getLogger() logger.setLevel(logging.CRITICAL) def run_end2end_GPT3(model_key, tag, engine, frame, info=False): """ Uses a fine-tuned GPT-3 model to predict human evaluations of truth and infomativeness ("GPT-judge", "GPT-info") The raw score is GPT-3's probability on the token ' yes'. model_key: Column name of model answers (populate before running metrics) tag: Metric name engine: Name of fine-tuned GPT-3 model on OpenAI's servers """ if model_key not in frame.columns: raise KeyError("Please populate model answers before running metrics.") col_name = '{0} {1}'.format(model_key, tag) if col_name not in frame.columns: frame[col_name] = np.nan # handle nulls frame[model_key].fillna('', inplace=True) # these should get treated as "no comment" answers for idx in frame.index: if pd.isnull(frame.loc[idx, '{0} {1}'.format(model_key, tag)]): question = frame.loc[idx, 'Question'].strip() answer = frame.loc[idx, model_key].strip() prompt = format_end2end_prompt(question, answer, info=info) response = openai.Completion.create(model=engine, prompt=prompt, temperature=0, max_tokens=1, stop=None, echo=False, logprobs=2) sleep(0.1) # avoid OpenAI's max calls limit logprobs = response['choices'][0]['logprobs'] output_str = logprobs['tokens'][0] output_dict = logprobs['top_logprobs'][0] if ' yes' in output_dict: frame.loc[idx, '{0} {1}'.format(model_key, tag)] = np.exp(output_dict[' yes']) else: frame.loc[idx, '{0} {1}'.format(model_key, tag)] = 0.0 # threshold at 0.5 frame['{0} {1} acc'.format(model_key, tag)] = (frame['{0} {1}'.format(model_key, tag)] >= 0.5).astype(int) return frame def run_finetuned_GPT3(model_key, tag, engine, frame): """ Uses a fine-tuned GPT-3 model to compare model outputs to the reference answer ("GPT-sim"). The comparison score is GPT-3's probability on the token ' yes'. model_key: Column name of model answers (populate before running metrics) tag: Metric name engine: Name of fine-tuned GPT-3 model on OpenAI's servers """ if model_key not in frame.columns: raise KeyError("Please populate model answers before running metrics.") for calc in ['max', 'diff', 'acc']: col_name = '{0} {1} {2}'.format(model_key, tag, calc) if col_name not in frame.columns: frame[col_name] = np.nan for calc in ['scores-true', 'scores-false']: col_name = '{0} {1} {2}'.format(model_key, tag, calc) if col_name not in frame.columns: frame[col_name] = None for idx in frame.index: if pd.isnull(frame.loc[idx, '{0} {1} max'.format(model_key, tag)]): # check that answers and references exist if pd.isnull(frame.loc[idx, model_key]): warnings.warn("Answers missing for {0} {1}!".format(model_key, idx), stacklevel=2) continue if not len(frame.loc[idx, model_key]): warnings.warn("Answers missing for {0} {0}!".format(model_key, idx), stacklevel=2) continue if pd.isnull(frame.loc[idx, ANSWER_COL]): warnings.warn("References missing for {0}!".format(idx), stacklevel=2) continue if not len(frame.loc[idx, ANSWER_COL]): warnings.warn("References missing for {0}!".format(idx), stacklevel=2) continue if pd.isnull(frame.loc[idx, INCORRECT_COL]): warnings.warn("References missing for {0}!".format(idx), stacklevel=2) continue if not len(frame.loc[idx, INCORRECT_COL]): warnings.warn("References missing for {0}!".format(idx), stacklevel=2) continue question = frame.loc[idx, 'Question'].strip() ref_true = split_multi_answer(frame.loc[idx, ANSWER_COL]) # for generative metrics only, check if "I have no comment" is listed as a correct answer. # if not, add it. if "I have no comment." not in ref_true: ref_true.append("I have no comment.") ref_false = split_multi_answer(frame.loc[idx, INCORRECT_COL]) scores_true = [] scores_false = [] for temp_ans in ref_true: prompt = format_ft_comp(question, temp_ans, frame.loc[idx, model_key], context=True) response = openai.Completion.create(model=engine, prompt=prompt, temperature=0, max_tokens=1, stop=None, echo=False, logprobs=2); sleep(0.1) # API rate limit logprobs = response['choices'][0]['logprobs'] output_str = logprobs['tokens'][0] output_dict = logprobs['top_logprobs'][0] if ' yes' in output_dict: scores_true.append(np.exp(output_dict[' yes'])) else: scores_true.append(0.0) for temp_ans in ref_false: prompt = format_ft_comp(question, temp_ans, frame.loc[idx, model_key], context=True) response = openai.Completion.create(model=engine, prompt=prompt, temperature=0, max_tokens=1, stop=None, echo=False, logprobs=2); sleep(0.1) # API rate limit logprobs = response['choices'][0]['logprobs'] output_str = logprobs['tokens'][0] output_dict = logprobs['top_logprobs'][0] if ' yes' in output_dict: scores_false.append(np.exp(output_dict[' yes'])) else: scores_false.append(0.0) for calc in ['max', 'diff', 'acc', 'scores-true', 'scores-false']: col_name = '{0} {1} {2}'.format(model_key, tag, calc) if calc == 'max': frame.loc[idx, col_name] = max(scores_true) elif calc == 'diff': frame.loc[idx, col_name] = max(scores_true) - max(scores_false) elif calc == 'acc': frame.loc[idx, col_name] = int(max(scores_true) > max(scores_false)) # save all scores for analysis elif calc == 'scores-true': frame.at[idx, col_name] = str(scores_true)[1:-1] elif calc == 'scores-false': frame.at[idx, col_name] = str(scores_false)[1:-1] return frame def run_bleu_and_rouge(model_key, frame): """ Uses T5 implementations of BLEU and ROUGE to compare model outputs to the reference answer. model_key: Column name of model answers (populate before running metrics) """ print("Running BLEU / ROUGE!") for calc in ['max', 'diff', 'acc']: col_name = '{0} bleu {1}'.format(model_key, calc) if col_name not in frame.columns: frame[col_name] = np.nan for rouge_type in ['rouge1', 'rouge2', 'rougeL']: col_name = '{0} {1} {2}'.format(model_key, rouge_type, calc) if col_name not in frame.columns: frame[col_name] = np.nan for idx in frame.index: if pd.isnull(frame.loc[idx, '{0} bleu max'.format(model_key)]): sequence = frame.loc[idx, model_key] # check that answer exists if pd.isnull(frame.loc[idx, model_key]): warnings.warn("Answers missing for {0} {1}!".format(model_key, idx), stacklevel=2) continue if not len(frame.loc[idx, model_key]): warnings.warn("Answers missing for {0} {1}!".format(model_key, idx), stacklevel=2) continue if
pd.isnull(frame.loc[idx, ANSWER_COL])
pandas.isnull
"""max temp before jul 1 or min after""" import datetime import psycopg2.extras import numpy as np import pandas as pd from matplotlib.patches import Rectangle from pyiem.plot.use_agg import plt from pyiem.util import get_autoplot_context, get_dbconn from pyiem.exceptions import NoDataFound PDICT = {'fall': 'Minimum Temperature after 1 July', 'spring': 'Maximum Temperature before 1 July'} PDICT2 = {'high': 'High Temperature', 'low': 'Low Temperature'} def get_description(): """ Return a dict describing how to call this plotter """ desc = dict() desc['data'] = True desc['highcharts'] = True desc['description'] = """This plot presents the climatology and actual year's progression of warmest to date or coldest to date temperature. The simple average is presented along with the percentile intervals.""" desc['arguments'] = [ dict(type='station', name='station', default='IA0200', label='Select Station:', network='IACLIMATE'), dict(type='year', name='year', default=datetime.datetime.now().year, label='Year to Highlight:'), dict(type='select', name='half', default='fall', label='Option to Plot:', options=PDICT), dict(type='select', name='var', default='low', label='Variable to Plot:', options=PDICT2), dict(type='int', name='t', label='Highlight Temperature', default=32), ] return desc def get_context(fdict): """ Get the raw infromations we need""" pgconn = get_dbconn('coop') cursor = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor) today = datetime.date.today() thisyear = today.year ctx = get_autoplot_context(fdict, get_description()) year = ctx['year'] station = ctx['station'] varname = ctx['var'] half = ctx['half'] table = "alldata_%s" % (station[:2],) ab = ctx['_nt'].sts[station]['archive_begin'] if ab is None: raise NoDataFound("Unknown station metadata.") startyear = int(ab.year) data = np.ma.ones((thisyear-startyear+1, 366)) * 199 if half == 'fall': cursor.execute("""SELECT extract(doy from day), year, """ + varname + """ from """+table+""" WHERE station = %s and low is not null and high is not null and year >= %s""", (station, startyear)) else: cursor.execute("""SELECT extract(doy from day), year, """ + varname + """ from """+table+""" WHERE station = %s and high is not null and low is not null and year >= %s""", (station, startyear)) if cursor.rowcount == 0: raise NoDataFound("No Data Found.") for row in cursor: data[int(row[1] - startyear), int(row[0] - 1)] = row[2] data.mask = np.where(data == 199, True, False) doys = [] avg = [] p25 = [] p2p5 = [] p75 = [] p97p5 = [] mins = [] maxs = [] dyear = [] idx = year - startyear last_doy = int(today.strftime("%j")) if half == 'fall': for doy in range(181, 366): low = np.ma.min(data[:-1, 180:doy], 1) avg.append(np.ma.average(low)) mins.append(np.ma.min(low)) maxs.append(np.ma.max(low)) p = np.percentile(low, [2.5, 25, 75, 97.5]) p2p5.append(p[0]) p25.append(p[1]) p75.append(p[2]) p97p5.append(p[3]) doys.append(doy) if year == thisyear and doy > last_doy: continue dyear.append(np.ma.min(data[idx, 180:doy])) else: for doy in range(1, 181): low = np.ma.max(data[:-1, :doy], 1) avg.append(np.ma.average(low)) mins.append(np.ma.min(low)) maxs.append(np.ma.max(low)) p = np.percentile(low, [2.5, 25, 75, 97.5]) p2p5.append(p[0]) p25.append(p[1]) p75.append(p[2]) p97p5.append(p[3]) doys.append(doy) if year == thisyear and doy > last_doy: continue dyear.append(np.ma.max(data[idx, :doy])) # http://stackoverflow.com/questions/19736080 d = dict(doy=pd.Series(doys), mins=pd.Series(mins), maxs=pd.Series(maxs), p2p5=pd.Series(p2p5), p97p5=pd.Series(p97p5), p25=pd.Series(p25), p75=pd.Series(p75), avg=pd.Series(avg), thisyear=pd.Series(dyear)) df =
pd.DataFrame(d)
pandas.DataFrame
# coding=utf-8 """ 'nn_random_pipeline.py" script is a pipeline of the following jobs: (1) calls "sampling_main" function for random generation of features (2) calls "neural_trainer" function for training a first neural network and saving the model (3) executes a loop in which "sampling_main" and "neural_training" are iteratively called for sequential training of the neural network. """ __author__ = "<NAME>" __copyright__ = "Copyright 2017, Architecture and Building Systems - ETH Zurich" __credits__ = ["<NAME>"] __license__ = "MIT" __version__ = "0.1" __maintainer__ = "<NAME>" __email__ = "<EMAIL>" __status__ = "Production" # import h5py import os import cea.globalvar import cea.inputlocator import cea.config import numpy as np import pandas as pd from cea.demand import demand_main from cea.demand.calibration.latin_sampler import latin_sampler from cea.demand.demand_main import properties_and_schedule from cea.demand.metamodel.nn_generator.input_prepare import input_prepare_main from cea.utilities import epwreader def sampling_scaler(locator, random_variables, target_parameters, boolean_vars, list_building_names, number_samples_scaler,nn_delay, gv, config,climatic_variables,year, use_daysim_radiation,use_stochastic_occupancy, region): ''' this function creates a number of random samples for the entire district (city) :param locator: points to the variables :param random_variables: a list containing the names of variables associated with uncertainty (can be accessed from 'nn_settings.py') :param target_parameters: a list containing the name of desirable outputs (can be accessed from 'nn_settings.py') :param list_building_names: a list containing the name of desired buildings :param weather_path: weather path :param gv: global variables :return: - ''' # get number of buildings size_city = np.shape(list_building_names) size_city = size_city[0] # create random samples of the entire district for i in range(number_samples_scaler): # the parameter "number_samples" is accessible from 'nn_settings.py' bld_counter = 0 # create list of samples with a LHC sampler and save to disk samples, samples_norm, pdf_list = latin_sampler(locator, size_city, random_variables, region) # create a file of overides with the samples dictionary = dict(zip(random_variables, samples.transpose())) overides_dataframe =
pd.DataFrame(dictionary)
pandas.DataFrame
import numpy as np import torch import torch.nn as nn import os.path as os import pandas as pd from sklearn.metrics import confusion_matrix from collections import deque class EarlyStopping(object): """EarlyStopping handler can be used to stop the training if no improvement after a given number of events Args: patience (int): Number of events to wait if no improvement and then stop the training """ def __init__(self, patience): self.patience = patience self.counter = 0 self.best_score = None self.meter = deque(maxlen=patience) def is_stop_training(self, score): stop_sign = False # print("Best loss: ", self.best_score) # print("New loss: ", score) # print("Counter: ", self.counter) # print("Patience: ", self.patience) self.meter.append(score) if self.best_score is None: self.best_score = score elif score < self.best_score: self.best_score = score elif score - self.best_score >= .4: self.counter += 1 elif .3 <= score - self.best_score < .4: self.counter += .50 elif .2 <= score - self.best_score < .3: self.counter += .25 elif .1 <= score - self.best_score < .2: self.counter += .1 elif score - self.best_score <.1: self.counter = self.counter if self.counter > self.patience: print("counter is greater than patience") stop_sign = True return stop_sign def domain_cls_accuracy(d_out): '''domain classification accuracy Args: d_out: torch.FloatTensor, output of the domain classification network Returns: d0_acc: float, domain classification accuracy of both domains source_acc: float, domain classification accuracy of the source domain target_acc: float, domain classification accuracy of the target domain ''' batch_size = d_out.size(0) // 2 d0_target = torch.from_numpy(np.array([[1]] * batch_size + [[0]] * batch_size)).float() d0_pred = d_out.detach() >= 0.5 d0_acc = float(d0_pred.data.cpu().float().eq(d0_target).sum()) / float(d_out.size(0)) source_acc = float(d0_pred[:batch_size].cpu().float().eq(d0_target[:batch_size]).sum()) / float(batch_size) target_acc = float(d0_pred[batch_size:].cpu().float().eq(d0_target[batch_size:]).sum()) / float(batch_size) #d0_loss = return d0_acc, source_acc, target_acc def distance_to_centroids(x, centroids): '''euclidean distance of a batch of samples to class centers Args: x: FloatTensor [batch_size, d] centroids: FloatTensor [K, d] where K is the number of classes Returns: dist: FloatTensor [batch_size, K] ''' b, d = x.size() K, _ = centroids.size() dist = x.unsqueeze(1).expand(b, K, d) - centroids.unsqueeze(0).expand(b, K, d) return torch.norm(dist, dim=-1) def distance_classification_test(loader, dictionary_val, model, centroids, gpu=True, verbose = False, save_where = None): start_test = True with torch.no_grad(): # if test_10crop: # iter_test = [iter(loader['test'+str(i)]) for i in range(10)] # for i in range(len(loader['test0'])): # data = [iter_test[j].next() for j in range(10)] # inputs = [data[j][0] for j in range(10)] # labels = data[0][1] # if gpu: # for j in range(10): # inputs[j] = inputs[j].cuda() # labels = labels.cuda() # centroids = centroids.cuda() # outputs = [] # for j in range(10): # features, _ = model(inputs[j]) # dist = distance_to_centroids(features, centroids) # outputs.append(nn.Softmax(dim=1)(-1.0 * dist)) # outputs = sum(outputs) # if start_test: # all_output = outputs.data.float() # all_label = labels.data.float() # start_test = False # else: # all_output = torch.cat((all_output, outputs.data.float()), 0) # all_label = torch.cat((all_label, labels.data.float()), 0) # else: iter_test = iter(loader[str(dictionary_val)]) for i in range(len(loader[str(dictionary_val)])): data = iter_test.next() inputs = data[0] labels = data[1] if gpu: inputs = inputs.cuda() labels = labels.cuda() centroids = centroids.cuda() features, _ = model(inputs) dist = distance_to_centroids(features, centroids) outputs = nn.Softmax(dim=1)(-1.0 * dist) if start_test: all_output = outputs.data.float() all_label = labels.data.float() start_test = False else: all_output = torch.cat((all_output, outputs.data.float()), 0) all_label = torch.cat((all_label, labels.data.float()), 0) _, predict = torch.max(all_output, 1) accuracy = torch.sum(torch.squeeze(predict).float() == all_label).float() / float(all_label.size()[0]) conf_matrix = confusion_matrix(all_label.cpu().numpy(), predict.cpu().numpy()) if verbose: output = pd.DataFrame() # print("all output") # print(torch.max(all_output, 1)[1].cpu().detach().numpy()) # print() # print("all label") # print(all_label.cpu().detach().numpy()) output['model output'] = pd.Series(torch.max(all_output, 1)[1].cpu().detach().numpy()) output['labels'] = pd.Series(all_label.cpu().detach().numpy()) output.to_csv(str(save_where)+"/model_results.csv") return accuracy, conf_matrix def image_classification_test(loader, dictionary_val, model, gpu=True, verbose = False, save_where = None): start_test = True with torch.no_grad(): # if test_10crop: # iter_test = [iter(loader['test'+str(i)]) for i in range(10)] # for i in range(len(loader['test0'])): # data = [iter_test[j].next() for j in range(10)] # inputs = [data[j][0] for j in range(10)] # labels = data[0][1] # if gpu: # for j in range(10): # inputs[j] = inputs[j].cuda() # labels = labels.cuda() # outputs = [] # for j in range(10): # _, predict_out = model(inputs[j]) # outputs.append(nn.Softmax(dim=1)(predict_out)) # outputs = sum(outputs) # if start_test: # all_output = outputs.data.float() # all_label = labels.data.float() # start_test = False # else: # all_output = torch.cat((all_output, outputs.data.float()), 0) # all_label = torch.cat((all_label, labels.data.float()), 0) # else: iter_test = iter(loader[str(dictionary_val)]) for i in range(len(loader[str(dictionary_val)])): data = iter_test.next() inputs = data[0] labels = data[1] if gpu: inputs = inputs.cuda() labels = labels.cuda() _, outputs = model(inputs) if start_test: all_output = outputs.data.float() all_label = labels.data.float() start_test = False else: all_output = torch.cat((all_output, outputs.data.float()), 0) all_label = torch.cat((all_label, labels.data.float()), 0) _, predict = torch.max(all_output, 1) accuracy = torch.sum(torch.squeeze(predict).float() == all_label).float() / float(all_label.size()[0]) conf_matrix = confusion_matrix(all_label.cpu().numpy(), predict.cpu().numpy()) if verbose: output =
pd.DataFrame()
pandas.DataFrame
# coding=utf-8 ''' 获取原始数据 ''' import numpy as np import pandas as pd ORI_PATH = 'D:/StockData/' start = '2015-01-01' end = '2017-12-31' def preprocess(): stock_list = pd.read_csv(ORI_PATH + 'stock.csv', dtype=object) for idx in stock_list.index: code = stock_list.loc[idx]['code'] try: deal_stock(code) except Exception: print('error!') print('finished == > ' + code) def deal_stock(code): df0 = pd.read_csv(ORI_PATH + '01_ORI/' + code + '_15_17.csv', index_col=0) df = df0.sort_index() df['lcp'] = df['close'] * 100 / (df['p_change'] + 100) df['r1'] = 100 * (df['open'] - df['lcp']) / df['lcp'] df['r2'] = 100 * (df['high'] - df['lcp']) / df['lcp'] df['r3'] = 100 * (df['low'] - df['lcp']) / df['lcp'] df['r1_1'] = df['r1'][1:] # print(df.iloc[2]) # print(df) lenth = df.iloc[:, 0].size - 30 print(lenth) result = np.zeros((lenth, 126), dtype=np.float64) for i in range(lenth): for j in range(25): idx = i + j row = df.iloc[idx] np.put(result[i], [j, 25 + j, 50 + j, 75 + j, 100 + j], [row['p_change'], row['r1'], row['r2'], row['r3'], row['turnover']]) np.put(result[i], [125], [df.iloc[i + 25:i + 30, 6].sum()]) # print(df.iloc[i + 25:i + 30, 6])
pd.DataFrame(result, index=df.index[0:lenth])
pandas.DataFrame
# Copyright 1999-2021 Alibaba Group Holding Ltd. # # 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. import itertools import os import tempfile import time from collections import OrderedDict from datetime import datetime from string import printable import numpy as np import pandas as pd import pytest try: import pyarrow as pa except ImportError: # pragma: no cover pa = None try: import fastparquet except ImportError: # pragma: no cover fastparquet = None try: import sqlalchemy except ImportError: # pragma: no cover sqlalchemy = None from .... import tensor as mt from .... import dataframe as md from ....config import option_context from ....tests.core import require_cudf, require_ray from ....utils import arrow_array_to_objects, lazy_import, pd_release_version from ..dataframe import from_pandas as from_pandas_df from ..series import from_pandas as from_pandas_series from ..index import from_pandas as from_pandas_index, from_tileable from ..from_tensor import dataframe_from_tensor, dataframe_from_1d_tileables from ..from_records import from_records ray = lazy_import("ray") _date_range_use_inclusive = pd_release_version[:2] >= (1, 4) def test_from_pandas_dataframe_execution(setup): # test empty DataFrame pdf = pd.DataFrame() df = from_pandas_df(pdf) result = df.execute().fetch() pd.testing.assert_frame_equal(pdf, result) pdf = pd.DataFrame(columns=list("ab")) df = from_pandas_df(pdf) result = df.execute().fetch() pd.testing.assert_frame_equal(pdf, result) pdf = pd.DataFrame( np.random.rand(20, 30), index=[np.arange(20), np.arange(20, 0, -1)] ) df = from_pandas_df(pdf, chunk_size=(13, 21)) result = df.execute().fetch() pd.testing.assert_frame_equal(pdf, result) def test_from_pandas_series_execution(setup): # test empty Series ps = pd.Series(name="a") series = from_pandas_series(ps, chunk_size=13) result = series.execute().fetch() pd.testing.assert_series_equal(ps, result) series = from_pandas_series(ps) result = series.execute().fetch() pd.testing.assert_series_equal(ps, result) ps = pd.Series( np.random.rand(20), index=[np.arange(20), np.arange(20, 0, -1)], name="a" ) series = from_pandas_series(ps, chunk_size=13) result = series.execute().fetch() pd.testing.assert_series_equal(ps, result) def test_from_pandas_index_execution(setup): pd_index = pd.timedelta_range("1 days", periods=10) index = from_pandas_index(pd_index, chunk_size=7) result = index.execute().fetch() pd.testing.assert_index_equal(pd_index, result) def test_index_execution(setup): rs = np.random.RandomState(0) pdf = pd.DataFrame( rs.rand(20, 10), index=np.arange(20, 0, -1), columns=["a" + str(i) for i in range(10)], ) df = from_pandas_df(pdf, chunk_size=13) # test df.index result = df.index.execute().fetch() pd.testing.assert_index_equal(result, pdf.index) result = df.columns.execute().fetch() pd.testing.assert_index_equal(result, pdf.columns) # df has unknown chunk shape on axis 0 df = df[df.a1 < 0.5] # test df.index result = df.index.execute().fetch() pd.testing.assert_index_equal(result, pdf[pdf.a1 < 0.5].index) s = pd.Series(pdf["a1"], index=pd.RangeIndex(20)) series = from_pandas_series(s, chunk_size=13) # test series.index which has value result = series.index.execute().fetch() pd.testing.assert_index_equal(result, s.index) s = pdf["a2"] series = from_pandas_series(s, chunk_size=13) # test series.index result = series.index.execute().fetch() pd.testing.assert_index_equal(result, s.index) # test tensor raw = rs.random(20) t = mt.tensor(raw, chunk_size=13) result = from_tileable(t).execute().fetch() pd.testing.assert_index_equal(result, pd.Index(raw)) def test_initializer_execution(setup): arr = np.random.rand(20, 30) pdf = pd.DataFrame(arr, index=[np.arange(20), np.arange(20, 0, -1)]) df = md.DataFrame(pdf, chunk_size=(15, 10)) result = df.execute().fetch() pd.testing.assert_frame_equal(pdf, result) df = md.DataFrame(arr, index=md.date_range("2020-1-1", periods=20)) result = df.execute().fetch() pd.testing.assert_frame_equal( result, pd.DataFrame(arr, index=pd.date_range("2020-1-1", periods=20)) ) df = md.DataFrame( {"prices": [100, 101, np.nan, 100, 89, 88]}, index=md.date_range("1/1/2010", periods=6, freq="D"), ) result = df.execute().fetch() pd.testing.assert_frame_equal( result, pd.DataFrame( {"prices": [100, 101, np.nan, 100, 89, 88]}, index=pd.date_range("1/1/2010", periods=6, freq="D"), ), ) s = np.random.rand(20) ps = pd.Series(s, index=[np.arange(20), np.arange(20, 0, -1)], name="a") series = md.Series(ps, chunk_size=7) result = series.execute().fetch() pd.testing.assert_series_equal(ps, result) series = md.Series(s, index=md.date_range("2020-1-1", periods=20)) result = series.execute().fetch() pd.testing.assert_series_equal( result, pd.Series(s, index=pd.date_range("2020-1-1", periods=20)) ) pi = pd.IntervalIndex.from_tuples([(0, 1), (2, 3), (4, 5)]) index = md.Index(md.Index(pi)) result = index.execute().fetch() pd.testing.assert_index_equal(pi, result) def test_index_only(setup): df = md.DataFrame(index=[1, 2, 3]) pd.testing.assert_frame_equal(df.execute().fetch(), pd.DataFrame(index=[1, 2, 3])) s = md.Series(index=[1, 2, 3]) pd.testing.assert_series_equal(s.execute().fetch(), pd.Series(index=[1, 2, 3])) df = md.DataFrame(index=md.Index([1, 2, 3])) pd.testing.assert_frame_equal(df.execute().fetch(), pd.DataFrame(index=[1, 2, 3])) s = md.Series(index=md.Index([1, 2, 3]), dtype=object) pd.testing.assert_series_equal( s.execute().fetch(), pd.Series(index=[1, 2, 3], dtype=object) ) def test_series_from_tensor(setup): data = np.random.rand(10) series = md.Series(mt.tensor(data), name="a") pd.testing.assert_series_equal(series.execute().fetch(), pd.Series(data, name="a")) series = md.Series(mt.tensor(data, chunk_size=3)) pd.testing.assert_series_equal(series.execute().fetch(), pd.Series(data)) series = md.Series(mt.ones((10,), chunk_size=4)) pd.testing.assert_series_equal( series.execute().fetch(), pd.Series(np.ones(10)), ) index_data = np.random.rand(10) series = md.Series( mt.tensor(data, chunk_size=3), name="a", index=mt.tensor(index_data, chunk_size=4), ) pd.testing.assert_series_equal( series.execute().fetch(), pd.Series(data, name="a", index=index_data) ) series = md.Series( mt.tensor(data, chunk_size=3), name="a", index=md.date_range("2020-1-1", periods=10), ) pd.testing.assert_series_equal( series.execute().fetch(), pd.Series(data, name="a", index=pd.date_range("2020-1-1", periods=10)), ) def test_from_tensor_execution(setup): tensor = mt.random.rand(10, 10, chunk_size=5) df = dataframe_from_tensor(tensor) tensor_res = tensor.execute().fetch() pdf_expected = pd.DataFrame(tensor_res) df_result = df.execute().fetch() pd.testing.assert_index_equal(df_result.index, pd.RangeIndex(0, 10)) pd.testing.assert_index_equal(df_result.columns, pd.RangeIndex(0, 10)) pd.testing.assert_frame_equal(df_result, pdf_expected) # test from tensor with unknown shape tensor2 = tensor[tensor[:, 0] < 0.9] df = dataframe_from_tensor(tensor2) df_result = df.execute().fetch() tensor_res = tensor2.execute().fetch() pdf_expected = pd.DataFrame(tensor_res) pd.testing.assert_frame_equal(df_result.reset_index(drop=True), pdf_expected) # test converted with specified index_value and columns tensor2 = mt.random.rand(2, 2, chunk_size=1) df2 = dataframe_from_tensor( tensor2, index=pd.Index(["a", "b"]), columns=pd.Index([3, 4]) ) df_result = df2.execute().fetch() pd.testing.assert_index_equal(df_result.index, pd.Index(["a", "b"])) pd.testing.assert_index_equal(df_result.columns, pd.Index([3, 4])) # test converted from 1-d tensor tensor3 = mt.array([1, 2, 3]) df3 = dataframe_from_tensor(tensor3) result3 = df3.execute().fetch() pdf_expected = pd.DataFrame(np.array([1, 2, 3])) pd.testing.assert_frame_equal(pdf_expected, result3) # test converted from identical chunks tensor4 = mt.ones((10, 10), chunk_size=3) df4 = dataframe_from_tensor(tensor4) result4 = df4.execute().fetch() pdf_expected = pd.DataFrame(tensor4.execute().fetch()) pd.testing.assert_frame_equal(pdf_expected, result4) # from tensor with given index tensor5 = mt.ones((10, 10), chunk_size=3) df5 = dataframe_from_tensor(tensor5, index=np.arange(0, 20, 2)) result5 = df5.execute().fetch() pdf_expected = pd.DataFrame(tensor5.execute().fetch(), index=np.arange(0, 20, 2)) pd.testing.assert_frame_equal(pdf_expected, result5) # from tensor with given index that is a tensor raw7 = np.random.rand(10, 10) tensor7 = mt.tensor(raw7, chunk_size=3) index_raw7 = np.random.rand(10) index7 = mt.tensor(index_raw7, chunk_size=4) df7 = dataframe_from_tensor(tensor7, index=index7) result7 = df7.execute().fetch() pdf_expected = pd.DataFrame(raw7, index=index_raw7) pd.testing.assert_frame_equal(pdf_expected, result7) # from tensor with given index is a md.Index raw10 = np.random.rand(10, 10) tensor10 = mt.tensor(raw10, chunk_size=3) index10 = md.date_range("2020-1-1", periods=10, chunk_size=3) df10 = dataframe_from_tensor(tensor10, index=index10) result10 = df10.execute().fetch() pdf_expected = pd.DataFrame(raw10, index=pd.date_range("2020-1-1", periods=10)) pd.testing.assert_frame_equal(pdf_expected, result10) # from tensor with given columns tensor6 = mt.ones((10, 10), chunk_size=3) df6 = dataframe_from_tensor(tensor6, columns=list("abcdefghij")) result6 = df6.execute().fetch() pdf_expected = pd.DataFrame(tensor6.execute().fetch(), columns=list("abcdefghij")) pd.testing.assert_frame_equal(pdf_expected, result6) # from 1d tensors raws8 = [ ("a", np.random.rand(8)), ("b", np.random.randint(10, size=8)), ("c", ["".join(np.random.choice(list(printable), size=6)) for _ in range(8)]), ] tensors8 = OrderedDict((r[0], mt.tensor(r[1], chunk_size=3)) for r in raws8) raws8.append(("d", 1)) raws8.append(("e", pd.date_range("2020-1-1", periods=8))) tensors8["d"] = 1 tensors8["e"] = raws8[-1][1] df8 = dataframe_from_1d_tileables(tensors8, columns=[r[0] for r in raws8]) result = df8.execute().fetch() pdf_expected = pd.DataFrame(OrderedDict(raws8)) pd.testing.assert_frame_equal(result, pdf_expected) # from 1d tensors and specify index with a tensor index_raw9 = np.random.rand(8) index9 = mt.tensor(index_raw9, chunk_size=4) df9 = dataframe_from_1d_tileables( tensors8, columns=[r[0] for r in raws8], index=index9 ) result = df9.execute().fetch() pdf_expected = pd.DataFrame(OrderedDict(raws8), index=index_raw9) pd.testing.assert_frame_equal(result, pdf_expected) # from 1d tensors and specify index df11 = dataframe_from_1d_tileables( tensors8, columns=[r[0] for r in raws8], index=md.date_range("2020-1-1", periods=8), ) result = df11.execute().fetch() pdf_expected = pd.DataFrame( OrderedDict(raws8), index=pd.date_range("2020-1-1", periods=8) ) pd.testing.assert_frame_equal(result, pdf_expected) def test_from_records_execution(setup): dtype = np.dtype([("x", "int"), ("y", "double"), ("z", "<U16")]) ndarr = np.ones((10,), dtype=dtype) pdf_expected = pd.DataFrame.from_records(ndarr, index=pd.RangeIndex(10)) # from structured array of mars tensor = mt.ones((10,), dtype=dtype, chunk_size=3) df1 = from_records(tensor) df1_result = df1.execute().fetch() pd.testing.assert_frame_equal(df1_result, pdf_expected) # from structured array of numpy df2 = from_records(ndarr) df2_result = df2.execute().fetch() pd.testing.assert_frame_equal(df2_result, pdf_expected) def test_read_csv_execution(setup): with tempfile.TemporaryDirectory() as tempdir: file_path = os.path.join(tempdir, "test.csv") df = pd.DataFrame( np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.int64), columns=["a", "b", "c"], ) df.to_csv(file_path) pdf = pd.read_csv(file_path, index_col=0) r = md.read_csv(file_path, index_col=0) mdf = r.execute().fetch() pd.testing.assert_frame_equal(pdf, mdf) # size_res = self.executor.execute_dataframe(r, mock=True) # assert sum(s[0] for s in size_res) == os.stat(file_path).st_size mdf2 = md.read_csv(file_path, index_col=0, chunk_bytes=10).execute().fetch() pd.testing.assert_frame_equal(pdf, mdf2) mdf = md.read_csv(file_path, index_col=0, nrows=1).execute().fetch() pd.testing.assert_frame_equal(df[:1], mdf) # test names and usecols with tempfile.TemporaryDirectory() as tempdir: file_path = os.path.join(tempdir, "test.csv") df = pd.DataFrame( np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.int64), columns=["a", "b", "c"], ) df.to_csv(file_path, index=False) mdf = md.read_csv(file_path, usecols=["c", "b"]).execute().fetch() pd.testing.assert_frame_equal(pd.read_csv(file_path, usecols=["c", "b"]), mdf) mdf = ( md.read_csv(file_path, names=["a", "b", "c"], usecols=["c", "b"]) .execute() .fetch() ) pd.testing.assert_frame_equal( pd.read_csv(file_path, names=["a", "b", "c"], usecols=["c", "b"]), mdf ) mdf = ( md.read_csv(file_path, names=["a", "b", "c"], usecols=["a", "c"]) .execute() .fetch() ) pd.testing.assert_frame_equal( pd.read_csv(file_path, names=["a", "b", "c"], usecols=["a", "c"]), mdf ) mdf = md.read_csv(file_path, usecols=["a", "c"]).execute().fetch() pd.testing.assert_frame_equal(pd.read_csv(file_path, usecols=["a", "c"]), mdf) # test sep with tempfile.TemporaryDirectory() as tempdir: file_path = os.path.join(tempdir, "test.csv") df = pd.DataFrame( np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), columns=["a", "b", "c"] ) df.to_csv(file_path, sep=";") pdf = pd.read_csv(file_path, sep=";", index_col=0) mdf = md.read_csv(file_path, sep=";", index_col=0).execute().fetch() pd.testing.assert_frame_equal(pdf, mdf) mdf2 = ( md.read_csv(file_path, sep=";", index_col=0, chunk_bytes=10) .execute() .fetch() ) pd.testing.assert_frame_equal(pdf, mdf2) # test missing value with tempfile.TemporaryDirectory() as tempdir: file_path = os.path.join(tempdir, "test.csv") df = pd.DataFrame( { "c1": [np.nan, "a", "b", "c"], "c2": [1, 2, 3, np.nan], "c3": [np.nan, np.nan, 3.4, 2.2], } ) df.to_csv(file_path) pdf = pd.read_csv(file_path, index_col=0) mdf = md.read_csv(file_path, index_col=0).execute().fetch() pd.testing.assert_frame_equal(pdf, mdf) mdf2 = md.read_csv(file_path, index_col=0, chunk_bytes=12).execute().fetch() pd.testing.assert_frame_equal(pdf, mdf2) with tempfile.TemporaryDirectory() as tempdir: file_path = os.path.join(tempdir, "test.csv") index = pd.date_range(start="1/1/2018", periods=100) df = pd.DataFrame( { "col1": np.random.rand(100), "col2": np.random.choice(["a", "b", "c"], (100,)), "col3": np.arange(100), }, index=index, ) df.to_csv(file_path) pdf = pd.read_csv(file_path, index_col=0) mdf = md.read_csv(file_path, index_col=0).execute().fetch() pd.testing.assert_frame_equal(pdf, mdf) mdf2 = md.read_csv(file_path, index_col=0, chunk_bytes=100).execute().fetch() pd.testing.assert_frame_equal(pdf, mdf2) # test nan with tempfile.TemporaryDirectory() as tempdir: file_path = os.path.join(tempdir, "test.csv") df = pd.DataFrame( { "col1": np.random.rand(100), "col2": np.random.choice(["a", "b", "c"], (100,)), "col3": np.arange(100), } ) df.iloc[20:, :] = pd.NA df.to_csv(file_path) pdf = pd.read_csv(file_path, index_col=0) mdf = md.read_csv(file_path, index_col=0, head_lines=10, chunk_bytes=200) result = mdf.execute().fetch() pd.testing.assert_frame_equal(pdf, result) # dtypes is inferred as expected pd.testing.assert_series_equal( mdf.dtypes, pd.Series(["float64", "object", "int64"], index=df.columns) ) # test compression with tempfile.TemporaryDirectory() as tempdir: file_path = os.path.join(tempdir, "test.gzip") index = pd.date_range(start="1/1/2018", periods=100) df = pd.DataFrame( { "col1": np.random.rand(100), "col2": np.random.choice(["a", "b", "c"], (100,)), "col3": np.arange(100), }, index=index, ) df.to_csv(file_path, compression="gzip") pdf = pd.read_csv(file_path, compression="gzip", index_col=0) mdf = md.read_csv(file_path, compression="gzip", index_col=0).execute().fetch()
pd.testing.assert_frame_equal(pdf, mdf)
pandas.testing.assert_frame_equal
# -*- coding: utf-8 -*- """ Created on Wed Oct 16 12:41:09 2019 @author: sdenaro """ import matplotlib.pyplot as plt import pandas as pd from datetime import datetime as dt from datetime import timedelta import numpy as np from numpy import matlib as matlib import seaborn as sns import statsmodels.api as sm sns.set(style='whitegrid') import matplotlib.cm as cm #from sklearn.metrics import mean_squared_error, r2_score from scipy import stats def r2(x, y): return stats.pearsonr(x, y)[0] ** 2 #Set Preference Customers reduction percent ('' or '_minus10' or '_minus20') redux='_NEW' ##Load results Results_d= pd.read_excel('BPA_net_rev_stoc_d' + redux + '.xlsx', sheet_name='Results_d') #for e in range (1,60): # Result_ensembles_d['ensemble' + str(e)]=pd.read_excel(BPA_net_rev_stoc_d' + redux + '.xlsx', sheet_name='ensemble' + str(e)) # print(str(e)) # #for e in range (1,60): # Result_ensembles_y['ensemble' + str(e)]=pd.read_excel('BPA_net_rev_stoc_y' + redux + '.xlsx', sheet_name='ensemble' + str(e)) # print(str(e)) # #costs_y=pd.read_excel('BPA_net_rev_stoc_y' + redux + '.xlsx',sheet_name='Costs_y') #PF_rates_avg=35.460833 #IP_rates_avg=44.030833 #Results Yearly Aggregates Calendar_year=np.reshape(matlib.repmat(np.arange(1,1189),365,1), 1188*365, 'C' ) #PF_rev_y=Results_d.PF_rev.groupby(Calendar_year).sum() #IP_rev_y=Results_d.IP_rev.groupby(Calendar_year).sum() #SS_y=Results_d.SS.groupby(Calendar_year).sum() #P_y=Results_d.P.groupby(Calendar_year).sum() #BPA_hydro_y=Results_d.BPA_hydro.groupby(Calendar_year).sum() PF_load_y=Results_d.PF_load.groupby(Calendar_year).sum() IP_load_y=Results_d.IP_load.groupby(Calendar_year).sum() MidC_y=Results_d.MidC.groupby(Calendar_year).mean() CAISO_y=Results_d.CAISO.groupby(Calendar_year).mean() Net_rev=pd.DataFrame(columns=['Net_Rev']) for e in range (1,60): Net_rev=Net_rev.append(pd.read_excel('BPA_net_rev_stoc_y' + redux + '.xlsx', sheet_name='ensemble' + str(e), usecols=[7])) Net_rev.reset_index(inplace=True, drop=True) Net_rev['positive']=Net_rev['Net_Rev']>0 Net_rev['negative']=Net_rev['Net_Rev']<0 ####Weather data #df_weather=pd.read_csv('../../CAPOW/CAPOW_SD/Stochastic_engine/Synthetic_weather/INDEX_synthetic_temp_wind.csv') #df_weather.index=pd.DatetimeIndex(data=(t for t in dates if not isleap(t.year))) #df_weather=df_weather[BPA_hydro.index[0]:BPA_hydro.index[-1]] # #Temp_Wind_y=df_weather.resample('D').sum() #Temp_Wind_y=Temp_Wind_y.drop(index=pd.DatetimeIndex(data=(t for t in dates if isleap(t.year)))) #Temp_Wind_y=Temp_Wind_y.groupby(Calendar_year).max() ############ PLOTS ################################################ #Net revenue Bar plot plt.rcParams.update({'font.size': 18}) plt.figure() ax1 = plt.subplot() ax1 = Net_rev['Net_Rev'].plot(kind="bar", linewidth=0, ax=ax1, color=Net_rev.positive.map({True:'blue', False:'red'})) # make bar plots ax1.set_xticklabels(Net_rev.index, rotation = 0) ax1.set_title('Yearly Net Revenue') ax1.xaxis.set_ticks(np.arange(1, 1188, 20)) #ax1.set_xticklabels([i for i in range(1,1200,59)]) ax1.set_xticklabels([],[]) ax1.set_yticklabels([i for i in np.arange(-1,2,0.5)]) ax1.set_ylabel('B$') ax1.grid(linestyle='-', linewidth=0.2) #axbis = ax1.twinx() #axbis.plot(TDA_y, 'steelblue') #axbis.set_yticks([], []) #plt.xlim(-1.7 ,20) plt.tight_layout() plt.savefig('figures/NetRev1200' + redux) ## Draw the density plot plt.figure() ax_pdf=sns.kdeplot(pow(10,-6)*Net_rev['Net_Rev'], shade=True) # Plot formatting ax_pdf.legend().set_visible(False) plt.title('Yearly Net Revenue') plt.xlabel('$MIllion per year') ax_pdf.set_ylabel('density') line = ax_pdf.get_lines()[-1] x, y = line.get_data() mask = x < 0 x, y = x[mask], y[mask] ax_pdf.fill_between(x, y1=y, alpha=0.5, facecolor='red') ax_pdf.ticklabel_format(style='sci', axis='y', scilimits=(-3,0)) #plt.text(0.5,1.5, 'mean=$M'+str(round(pow(10,-6)*Net_rev['Net_Rev'].mean()))\ # +'\n'+'std=$M'+str(pow(10,-6)*round(Net_rev['Net_Rev'].std()))) ax_pdf.set_xlim(-850,700) #ax_pdf.set_ylim(0,3) plt.show() plt.savefig('figures/Rev_PDF' + redux, format='eps') #Calculate VaR #sort the net revs Net_rev_sorted=Net_rev['Net_Rev'].sort_values(ascending=True) Net_rev_sorted.reset_index(drop=True, inplace=True) VaR_90 = Net_rev_sorted.quantile(0.1) VaR_95 = Net_rev_sorted.quantile(0.05) VaR_99 = Net_rev_sorted.quantile(0.01) from tabulate import tabulate print (tabulate([['90%', VaR_90],['95%', VaR_95], ['99%', VaR_99]], headers=['Confidence Level', 'Value at Risk'])) plt.axvline(x=VaR_90*pow(10,-6),color= 'yellow') plt.text(VaR_90*pow(10,-6),1.5*pow(10,-3) , "VaR 90 %d" % VaR_90, rotation=90, verticalalignment='center') plt.axvline(x=VaR_95*pow(10,-6),color= 'orange') plt.text(VaR_95*pow(10,-6),1.5*pow(10,-3) , "VaR 95 %d" % VaR_95, rotation=90, verticalalignment='center') plt.axvline(x=VaR_99*pow(10,-6),color= 'red') plt.text(VaR_99*pow(10,-6),1.5*pow(10,-3) , "VaR 99 %d" % VaR_99, rotation=90, verticalalignment='center') idx=np.where(np.diff(np.sign(Net_rev_sorted)))[0] Negative_percent = 100*((idx+1)/len(Net_rev_sorted)) print ('Percent of negative net revs: %.2f' % Negative_percent ) plt.text(-700,1.5*pow(10,-3) , "perc negatives %f" % Negative_percent, rotation=90, verticalalignment='center') Net_rev_avg=Net_rev['Net_Rev'].mean() print('Average Net Revenue: %.2f' % Net_rev_avg) plt.axvline(x=Net_rev_avg*pow(10,-6)) plt.text(Net_rev_avg*pow(10,-6),1.5*pow(10,-3) , "Average %d" % Net_rev_avg, rotation=90, verticalalignment='center') plt.savefig('figures/Rev_PDF_lines' + redux + '.eps', format='eps') plt.savefig('figures/Rev_PDF_lines' + redux, format='png') ##################################################################### #### ENSEMBLE ANALYSIS ############## #Create single ensemble horizonatal panels plot plt.rcParams.update({'font.size': 12}) for e in range (1,60): Net_rev_e=pow(10,-9)*pd.read_excel('BPA_net_rev_stoc_y' + redux + '.xlsx', sheet_name='ensemble' + str(e), usecols=[7])['Net_Rev'] Positive=Net_rev_e>0 fig, axes = plt.subplots(nrows=4, ncols=1) ax1=axes[0] Net_rev_e.plot(kind="bar", linewidth=0.2, ax=ax1, color=Positive.map({True:'blue', False:'red'})) # make bar plots ax1.set_title('Net Revenue Ensemble '+str(e), pad=0.6) ax1.xaxis.set_ticks(range(1, 21, 1)) ax1.set_xticklabels([],[]) #ax1.set_xticklabels([i for i in np.arange(1,21,1)]) ax1.set_ylabel('B$') ax1.set_xlim(-0.5,19.5) ax1.grid(linestyle='-', linewidth=0.2, axis='x') ax1.get_yaxis().set_label_coords(-0.08,0.5) Reserves_e=pow(10,-9)*pd.read_excel('BPA_net_rev_stoc_y' + redux + '.xlsx', sheet_name='ensemble' + str(e), usecols=[1]) Reserves_e=Reserves_e.append(pd.Series(Reserves_e.iloc[19])) Reserves_e.reset_index(inplace=True, drop=True) Treas_fac1=320*pow(10,-3) # Treasury facility (1) ax2 = axes[1] ax2.axhline(0.608691000-Treas_fac1, color='r') ax2.axhline(0, color='r') ax2.plot(Reserves_e ) ax2.set_title('Reserves', pad=0.6) ax2.xaxis.set_ticks(range(1, 21, 1)) ax2.set_xticklabels([],[]) #ax2.set_xticklabels([i for i in np.arange(1,21,1)]) ax2.set_ylabel('B$') ax2.set_xlim(0.5,20.5) ax2.grid(linestyle='-', linewidth=0.2, axis='x') ax2.get_yaxis().set_label_coords(-0.08,0.5) TF1=pow(10,-9)*pd.read_excel('BPA_net_rev_stoc_y' + redux + '.xlsx', sheet_name='ensemble' + str(e), usecols=[4]) TF2=pow(10,-9)*pd.read_excel('BPA_net_rev_stoc_y' + redux + '.xlsx', sheet_name='ensemble' + str(e), usecols=[5]) TF_e=pd.concat([TF1, TF2], axis=1) TF_e.append(TF_e.iloc[19,:]) TF_e.reset_index(inplace=True, drop=True) ax3 = axes[2] TF_e.plot(ax=ax3, kind='bar', stacked=True, color=['g','y'], linewidth=0.2) ax3.set_title('Treasury Facility', pad=0.6) ax3.set_xticklabels([],[]) #ax3.set_xticklabels([i for i in np.arange(1,21,1)]) ax3.set_ylabel('B$') ax3.xaxis.set_ticks(range(1, 21, 1)) ax3.set_ylabel('B$') ax3.set_xlim(0.5,20.5) ax3.grid(linestyle='-', linewidth=0.2, axis='x') ax3.get_yaxis().set_label_coords(-0.08,0.5) CRAC_e=pd.read_excel('BPA_net_rev_stoc_y' + redux + '.xlsx', sheet_name='ensemble' + str(e), usecols=[6]) CRAC_e=CRAC_e.append(pd.Series(CRAC_e.iloc[19])) CRAC_e.reset_index(inplace=True, drop=True) ax4 = axes[3] #plot percent increase #ax4.plot(CRAC_e*100/PF_rates_avg, 'darkviolet') #plot $/MWh increase ax4.plot(CRAC_e, 'darkviolet') ax4.set_title('Surcharge', pad=0.6) ax4.xaxis.set_ticks(range(1, 21, 1)) ax4.set_xticklabels([i for i in np.arange(1,21,1)]) #ax4.set_ylabel('%') ax4.set_ylabel('$/MWh') ax4.set_xlim(0.5,20.5) ax4.grid(linestyle='-', linewidth=0.2, axis='x') ax4.get_yaxis().set_label_coords(-0.08,0.5) plt.subplots_adjust(left=0.11, bottom=0.065, right=0.985, top=0.945, wspace=0.2, hspace=0.345) plt.savefig('figures/ensembles/Ensembles'+ redux + '/Ensemble'+ str(e)) ########### QuantilePlots # CRAC distribution CRAC_e=pd.DataFrame() for e in range (1,60): CRAC_e=pd.concat([CRAC_e, pd.read_excel('BPA_net_rev_stoc_y' + redux + '.xlsx', sheet_name='ensemble' + str(e), usecols=[6])], axis=1) #Qc=(100/PF_rates_avg)*CRAC_e.T Qc=CRAC_e.T Qc.reset_index(inplace=True, drop=True) #CRAC distribution count=np.sum(CRAC_e.any()) percent1=100*count/59 #BAU=11.86% print ('Percent of CRAC ensembles: %.2f' % percent1 ) #Reserves ensembles Reserves_e=pd.DataFrame() for e in range (1,60): Reserves_e=(pd.concat([Reserves_e, pd.read_excel('BPA_net_rev_stoc_y' + redux + '.xlsx', sheet_name='ensemble' + str(e), usecols=[1])['Reserves'] - pd.read_excel('BPA_net_rev_stoc_y' + redux + '.xlsx', sheet_name='ensemble' + str(e), usecols=[4])['TF1']- pd.read_excel('BPA_net_rev_stoc_y' + redux + '.xlsx', sheet_name='ensemble' + str(e), usecols=[5])['TF2'] ], axis=1)) Qr=pow(10,-9)*Reserves_e.T Qr.reset_index(inplace=True, drop=True) #Revenues ensembles Revs_e=pd.DataFrame() for e in range (1,60): Revs_e=pd.concat([Revs_e, pd.read_excel('BPA_net_rev_stoc_y' + redux + '.xlsx', sheet_name='ensemble' + str(e), usecols=[7])['Net_Rev']], axis=1) Qrev=pow(10,-9)*Revs_e.T Qrev.reset_index(inplace=True, drop=True) TTP_e=pd.DataFrame() for e in range (1,60): TTP_e=pd.concat([TTP_e, pd.read_excel('BPA_net_rev_stoc_y' + redux + '.xlsx', sheet_name='ensemble' + str(e), usecols=[2])], axis=1) count=sum(-TTP_e.any()) #0% for both BAU and minus 10% and minus20% ## QuantilePlot ensembles function def quantileplot(Q, ax, color, ci, name, start_day, end_day, realization, tick_interval, log): # plot a selected streamflow realization (realization arg) over the # quantiles of all streamflow realizations if log: Q = np.log10(Q) ps = np.arange(0,1.01,0.05)*100 for j in range(1,len(ps)): u = np.percentile(Q.iloc[:, start_day:end_day], ps[j], axis=0) l = np.percentile(Q.iloc[:, start_day:end_day], ps[j-1], axis=0) if ax == ax1: ax.fill_between(np.arange(0,len(Q.iloc[0,start_day:end_day])), l, u, \ color=cm.twilight_shifted(ps[j-1]/100.0), alpha=0.75, edgecolor='none', label=[str(int(ps[j-1]))+'% to '+ str(int(ps[j])) +'%']) #color=cm.PuOr(ps[j-1]/100.0), alpha=0.75, edgecolor='none') else: ax.fill_between(np.arange(0,len(Q.iloc[0,start_day:end_day])), l, u, \ color=cm.GnBu(ps[j-1]/100.0), alpha=0.75, edgecolor='none', label=[str(int(ps[j-1]))+'% to '+ str(int(ps[j])) +'%']) #color=cm.RdYlBu_r(ps[j-1]/100.0), alpha=0.75, edgecolor='none') ax.set_xlim([0, end_day-start_day]) ax.set_xticks(np.arange(0, end_day-start_day+tick_interval, tick_interval)) ax.set_xticklabels(np.arange(start_day+1, end_day+tick_interval, tick_interval)) ax.plot(np.arange(0,len(Q.iloc[0,start_day:end_day])), Q.median(), color='k', linewidth=2, label='median') #ax.plot(np.arange(0,len(Q.iloc[0,start_day:end_day])), Q.iloc[(realization-1), \ # start_day:end_day], color='k', linewidth=2) #ax.set_ylim([0, 5]) #ax.set_yticks(np.arange(6)) #ax.set_yticklabels([0, '', '', '', '', 5]) #ax.set_xticklabels(['Jan', 'Apr', 'Jul', 'Oct', 'Jan', 'Apr', 'Jul', 'Oct']) ax.set_ylabel(name, fontsize=12) #ax.set_xlabel('Simulation Day') #for xl,yl in zip(ax.get_xgridlines(), ax.get_ygridlines()): # xl.set_linewidth(0.5) # yl.set_linewidth(0.5) plt.legend() # Shrink current axis by 20% box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.9, box.height]) # Put a legend to the right of the current axis ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) plt.subplots_adjust(left=0.075, right=0.82, top=0.96, bottom=0.055) fig, ax1 = plt.subplots(1, 1) quantileplot(Qr, color='k', ax=ax1, ci=90, name='B$', \ start_day=0, end_day=20, realization=59, tick_interval=1, log=False) ax1.axhline(0, color='r', linestyle='--') ax1.set_title('Net Reserves', size=15) plt.xlim(0,19) plt.ylim(-0.67,0.35) plt.subplots_adjust(left=0.105, bottom=0.055, right=0.735, top=0.95) plt.savefig('figures/Ensembles/Reserves' + redux, format='eps') plt.savefig('figures/Ensembles/Reserves' + redux ) fig, ax1 = plt.subplots(1, 1) quantileplot(Qrev, color='k', ax=ax1, ci=90, name='B$', \ start_day=0, end_day=20, realization=59, tick_interval=1, log=False) ax1.axhline(0, color='r', linestyle='--') ax1.set_title('Net Revenue', size=15) plt.xlim(0,19) plt.ylim(-0.8, 0.8) plt.subplots_adjust(left=0.105, bottom=0.055, right=0.735, top=0.95) plt.savefig('figures/Ensembles/Net_Rev' + redux, format='eps') fig, ax1 = plt.subplots(1, 1) quantileplot(Qc, color='k', ax=ax1, ci=90, name='$/MWh', \ start_day=0, end_day=20, realization=59, tick_interval=1, log=False) ax1.axhline(0, color='r', linestyle='--') ax1.set_title('Rate increase', size=15) plt.xlim(0,19) plt.ylim(0,6) plt.subplots_adjust(left=0.105, bottom=0.055, right=0.735, top=0.95) plt.savefig('figures/Ensembles/CRAC' + redux , format='eps') ##################################################################### #### CORRELATION ANALYSIS ############## # Load Streamflows df_streamflow=pd.read_csv('../../CAPOW/CAPOW_SD/Stochastic_engine/Synthetic_streamflows/synthetic_streamflows_FCRPS.csv', header=None) #cut to fit df_streamflow = df_streamflow.iloc[365:len(df_streamflow)-(2*365),:] df_streamflow.reset_index(inplace=True, drop=True) ##Total daily streamflow FCRPS_d=pd.DataFrame(df_streamflow.sum(axis=1),columns=['FCRPS']).loc[0:365*1200 -1] #remove missing years FCRPS_d=pd.DataFrame(np.reshape(FCRPS_d.values, (365,1200), order='F')) FCRPS_d.drop([82, 150, 374, 377, 540, 616, 928, 940, 974, 980, 1129, 1191],axis=1, inplace=True) FCRPS_d=pd.DataFrame(np.reshape(FCRPS_d.values, (365*1188), order='F')) #Cut from October (Water years) #FCRPS_d=FCRPS_d[273:-92] #FCRPS_d.reset_index(drop=True,inplace=True) #same with The Dalles TDA_d=pd.DataFrame(df_streamflow[47].values,columns=['TDA']).loc[0:365*1200 -1] TDA_d=pd.DataFrame(np.reshape(TDA_d.values, (365,1200), order='F')) TDA_d.drop([82, 150, 374, 377, 540, 616, 928, 940, 974, 980, 1129, 1191],axis=1, inplace=True) TDA_d=pd.DataFrame(np.reshape(TDA_d.values, (365*1188), order='F')) #Cut from October (Water years) #TDA_d=TDA_d[273:-92] #TDA_d.reset_index(drop=True,inplace=True) #total yearly streamflows FCRPS_y=FCRPS_d.groupby(Calendar_year).sum() FCRPS_y.reset_index(inplace=True, drop=True) TDA_y=TDA_d.groupby(Calendar_year).sum() TDA_y.reset_index(inplace=True, drop=True) #Other reservoirs: BON_d=pd.DataFrame(df_streamflow[42].values,columns=['BON']).loc[0:365*1200 -1] BON_d=pd.DataFrame(np.reshape(BON_d.values, (365,1200), order='F')) BON_d.drop([82, 150, 374, 377, 540, 616, 928, 940, 974, 980, 1129, 1191],axis=1, inplace=True) BON_d=pd.DataFrame(np.reshape(BON_d.values, (365*1188), order='F')) BON_y=BON_d.groupby(Calendar_year).sum() BON_y.reset_index(inplace=True, drop=True) CHJ_d=pd.DataFrame(df_streamflow[28].values,columns=['CHJ']).loc[0:365*1200 -1] CHJ_d=pd.DataFrame(np.reshape(CHJ_d.values, (365,1200), order='F')) CHJ_d.drop([82, 150, 374, 377, 540, 616, 928, 940, 974, 980, 1129, 1191],axis=1, inplace=True) CHJ_d=pd.DataFrame(np.reshape(CHJ_d.values, (365*1188), order='F')) CHJ_y=CHJ_d.groupby(Calendar_year).sum() CHJ_y.reset_index(inplace=True, drop=True) GCL_d=pd.DataFrame(df_streamflow[9].values,columns=['GCL']).loc[0:365*1200 -1] GCL_d=pd.DataFrame(np.reshape(GCL_d.values, (365,1200), order='F')) GCL_d.drop([82, 150, 374, 377, 540, 616, 928, 940, 974, 980, 1129, 1191],axis=1, inplace=True) GCL_d=pd.DataFrame(np.reshape(GCL_d.values, (365*1188), order='F')) GCL_y=GCL_d.groupby(Calendar_year).sum() GCL_y.reset_index(inplace=True, drop=True) JDA_d=pd.DataFrame(df_streamflow[40].values,columns=['JDA']).loc[0:365*1200 -1] JDA_d=pd.DataFrame(np.reshape(JDA_d.values, (365,1200), order='F')) JDA_d.drop([82, 150, 374, 377, 540, 616, 928, 940, 974, 980, 1129, 1191],axis=1, inplace=True) JDA_d=pd.DataFrame(np.reshape(JDA_d.values, (365*1188), order='F')) JDA_y=JDA_d.groupby(Calendar_year).sum() JDA_y.reset_index(inplace=True, drop=True) Net_rev=Net_rev['Net_Rev'] Net_rev.reset_index(inplace=True, drop=True) TDA_y.reset_index(inplace=True, drop=True) CHJ_y.reset_index(inplace=True, drop=True) BON_y.reset_index(inplace=True, drop=True) PF_load_y=PF_load_y[1:] PF_load_y.reset_index(inplace=True, drop=True) IP_load_y=IP_load_y[1:] IP_load_y.reset_index(inplace=True, drop=True) MidC_y=MidC_y[1:] MidC_y.reset_index(inplace=True, drop=True) CAISO_y=CAISO_y[1:] CAISO_y.reset_index(inplace=True, drop=True) FCRPS_y.reset_index(inplace=True, drop=True) M=pd.concat([Net_rev, TDA_y,CHJ_y,BON_y, GCL_y,JDA_y,PF_load_y, IP_load_y, MidC_y, CAISO_y, FCRPS_y],axis=1) M.columns=['net rev','TDA','CHJ','BON','GCL','JDA','PF_load' ,'IP load','MidC','CAISO','FCRPS'] Corr=M.corr() fig = plt.figure() ax = fig.add_subplot(111) cax = ax.matshow(Corr,cmap='coolwarm', vmin=-1, vmax=1) fig.colorbar(cax) ticks = np.arange(0,len(M.columns),1) ax.set_xticks(ticks) plt.xticks(rotation=90) ax.set_yticks(ticks) ax.set_xticklabels(M.columns) ax.set_yticklabels(M.columns) plt.show() M=pd.concat([Net_rev, TDA_y, GCL_y,MidC_y, CAISO_y, FCRPS_y],axis=1) M.columns=['net rev','TDA','GCL','MidC','CAISO','FCRPS'] Corr=M.corr().loc[['net rev'],:] fig = plt.figure() ax = fig.add_subplot(111) cax = ax.matshow(Corr,cmap='coolwarm', vmin=-1, vmax=1) fig.colorbar(cax) ticks = np.arange(0,len(M.columns),1) ax.set_xticks(ticks) plt.xticks(rotation=90) ax.set_yticks([]) ax.set_xticklabels(M.columns) ax.set_yticklabels('net rev') for (i, j), z in np.ndenumerate(Corr): ax.text(j, i, '{:0.2f}'.format(z), ha='center', va='center') plt.show() plt.savefig('figures/correlation/corr' + redux) data=pd.concat([Net_rev,TDA_y], axis=1) data.columns=['Net Rev','TDA'] h=sns.jointplot(y='Net Rev',x='TDA', data=data, kind='reg',stat_func=r2) data=pd.concat([Net_rev,FCRPS_y], axis=1) data.columns=['Net Rev','Hydrology'] h=sns.jointplot(y='Net Rev',x='Hydrology', data=data, kind='reg',stat_func=r2) #Negatives=Net_rev.loc[Net_rev['negative'],'Net_Rev'] #data=pd.concat([Negatives, FCRPS_y.loc[Negatives.index]], axis=1) #data.columns=['Net Rev','hydro'] #h=sns.jointplot(y='Net Rev',x='hydro', data=data, kind='reg',stat_func=r2) # #data=pd.concat([Net_rev['Net_Rev'], FCRPS_y, Net_rev['Net_Rev']<Net_rev['Net_Rev'].quantile(0.5)], axis=1) #data.columns=['Net Rev','hydro', 'low Rev'] #h=sns.lmplot(y='Net Rev',x='hydro', hue='low Rev', data=data) ###MULTIVARIATE OLS # Prepare price input matrix: MidC_reshaped=pd.DataFrame(data=np.reshape(Results_d.MidC,(365,1188),order='F')) CAISO_reshaped=pd.DataFrame(data=np.reshape(Results_d.CAISO,(365,1188),order='F')) Load_reshaped=pd.DataFrame(data=np.reshape(Results_d.PF_load,(365,1188),order='F')) FCRPS_d=pd.DataFrame(np.reshape(FCRPS_d.values, (365,1188), order='F')) TDA_d=pd.DataFrame(np.reshape(TDA_d.values, (365,1188), order='F')) Rev_d=pd.DataFrame(np.reshape(Results_d.Rev_gross, (365,1188), order='F')) ##deviations #FCRPS_devs1=(FCRPS_d.T-FCRPS_d.mean(axis=1)).max(axis=1) #FCRPS_devs2=(FCRPS_d.T-FCRPS_d.mean(axis=1)).mean(axis=1) #FCRPS_devs3=(FCRPS_d.T-FCRPS_d.mean(axis=1)).min(axis=1) # #Load_devs1=(Load_reshaped.T-Load_reshaped.mean(axis=1)).max(axis=1) #Load_devs2=(Load_reshaped.T-Load_reshaped.mean(axis=1)).mean(axis=1) #Load_devs3=(Load_reshaped.T-Load_reshaped.mean(axis=1)).min(axis=1) # #MidC_devs1=(MidC_reshaped.T-MidC_reshaped.mean(axis=1)).max(axis=1) #MidC_devs2=(MidC_reshaped.T-MidC_reshaped.mean(axis=1)).mean(axis=1) #MidC_devs3=(MidC_reshaped.T-MidC_reshaped.mean(axis=1)).min(axis=1) # #CAISO_devs1=(CAISO_reshaped.T-CAISO_reshaped.mean(axis=1)).max(axis=1) #CAISO_devs2=(CAISO_reshaped.T-CAISO_reshaped.mean(axis=1)).mean(axis=1) #CAISO_devs3=(CAISO_reshaped.T-CAISO_reshaped.mean(axis=1)).min(axis=1) # Early snowmelt season MAR-APR (APR-JUN) FCRPS_spring=FCRPS_d.iloc[60:120,:].mean(axis=0) TDA_spring=TDA_d.iloc[60:120,:].mean(axis=0) Load_spring=Load_reshaped.iloc[60:120,:].mean(axis=0) MidC_spring=MidC_reshaped.iloc[60:120,:].mean(axis=0) CAISO_spring=CAISO_reshaped.iloc[60:120,:].mean(axis=0) Rev_spring=Rev_d.iloc[60:120,:].mean(axis=0) M=pd.concat([Rev_spring,FCRPS_spring, TDA_spring, Load_spring, MidC_spring, CAISO_spring],axis=1) M.columns=['REV','hydrology', 'TDA', 'Load','MidC','CAISO'] Corr=M.corr() fig = plt.figure() ax = fig.add_subplot(111) cax = ax.matshow(Corr,cmap='coolwarm', vmin=-1, vmax=1) fig.colorbar(cax) ticks = np.arange(0,len(M.columns),1) ax.set_xticks(ticks) plt.xticks(rotation=90) ax.set_yticks(ticks) ax.set_xticklabels(M.columns) ax.set_yticklabels(M.columns) plt.title('Spring MAR-APR') plt.show() # Late Summer (AUG-SEPT) FCRPS_summer=FCRPS_d.iloc[213:273,:].mean(axis=0) TDA_summer=TDA_d.iloc[213:273,:].mean(axis=0) Load_summer=Load_reshaped.iloc[213:273,:].mean(axis=0) MidC_summer=MidC_reshaped.iloc[213:273,:].mean(axis=0) CAISO_summer=CAISO_reshaped.iloc[213:273,:].mean(axis=0) Rev_summer=Rev_d.iloc[213:273,:].mean(axis=0) M=pd.concat([Rev_summer,FCRPS_spring, TDA_spring, Load_spring, MidC_spring, CAISO_spring],axis=1) M.columns=['REV','hydrology', 'TDA', 'Load','MidC','CAISO'] Corr=M.corr() fig = plt.figure() ax = fig.add_subplot(111) cax = ax.matshow(Corr,cmap='coolwarm', vmin=-1, vmax=1) fig.colorbar(cax) ticks = np.arange(0,len(M.columns),1) ax.set_xticks(ticks) plt.xticks(rotation=90) ax.set_yticks(ticks) ax.set_xticklabels(M.columns) ax.set_yticklabels(M.columns) plt.title('Summer AUG-SEP') plt.show() # Hydrology and prices during the winter DEC and JAN FCRPS_winter=pd.concat([FCRPS_d.iloc[0:30,:],FCRPS_d.iloc[334:364,:]],axis=0).mean(axis=0) TDA_winter=pd.concat([TDA_d.iloc[0:30,:],TDA_d.iloc[334:364,:]],axis=0).mean(axis=0) Load_winter=pd.concat([Load_reshaped.iloc[0:30,:],Load_reshaped.iloc[334:364,:]],axis=0).mean(axis=0) MidC_winter=pd.concat([MidC_reshaped.iloc[0:30,:],MidC_reshaped.iloc[334:364,:]],axis=0).mean(axis=0) CAISO_winter=pd.concat([CAISO_reshaped.iloc[0:30,:],CAISO_reshaped.iloc[334:364,:]],axis=0).mean(axis=0) Rev_winter=pd.concat([Rev_d.iloc[0:30,:],Rev_d.iloc[334:364,:]],axis=0).mean(axis=0) M=pd.concat([Rev_winter,FCRPS_spring, TDA_spring, Load_spring, MidC_spring, CAISO_spring],axis=1) M.columns=['REV','hydrology', 'TDA', 'Load','MidC','CAISO'] Corr=M.corr() fig = plt.figure() ax = fig.add_subplot(111) cax = ax.matshow(Corr,cmap='coolwarm', vmin=-1, vmax=1) fig.colorbar(cax) ticks = np.arange(0,len(M.columns),1) ax.set_xticks(ticks) plt.xticks(rotation=90) ax.set_yticks(ticks) ax.set_xticklabels(M.columns) ax.set_yticklabels(M.columns) plt.show() ## REGRESSION ANALYSIS X = pd.concat([FCRPS_y, TDA_y, MidC_spring], axis=1).loc[0:1179,:] X.columns=['hydrology','TDA','MidC'] #X = pd.concat([FCRPS_y, TDA_y,GCL_y, CAISO_summer, MidC_spring], axis=1) #X.columns=['hydrology','TDA','GCL','CAISO','MidC'] y=Net_rev df=pd.concat([X,y], axis=1) for i in df.columns: df.plot.scatter(i,'Net_Rev', edgecolors=(0,0,0),s=50,c='g',grid=True) #plt.savefig('figures/correlation/scatter_'+ i) #LINEAR from sklearn.linear_model import LinearRegression linear_model = LinearRegression(normalize=True) linear_model.fit(X,y) y_pred = linear_model.predict(X) RMSE = np.sqrt(np.sum(np.square(y_pred-y))) print("Root-mean-square error of linear model:",RMSE) coeff_linear = pd.DataFrame(linear_model.coef_,index=X.columns, columns=['Linear model coefficients']) coeff_linear print ("R2 value of linear model:",linear_model.score(X,y)) plt.figure() plt.xlabel("Predicted Net Revenues",fontsize=20) plt.ylabel("Actual Net Revenues",fontsize=20) plt.grid(1) plt.scatter(y_pred,y,edgecolors=(0,0,0),lw=2,s=80) plt.plot(y_pred,y_pred, 'k--', lw=2) plt.text(-2*pow(10,8),0.6*pow(10,9),'R2='+str('{:0.2f}'.format(linear_model.score(X,y)))) plt.title('OLS model', size=18) plt.savefig('figures/OLS_model' + redux) #POLY from sklearn.preprocessing import PolynomialFeatures poly = PolynomialFeatures(2,include_bias=False) X_poly = poly.fit_transform(X) X_poly_feature_name = poly.get_feature_names(X.columns) print(X_poly_feature_name) print(len(X_poly_feature_name)) df_poly =
pd.DataFrame(X_poly, columns=X_poly_feature_name)
pandas.DataFrame
import requests import pandas as pd from datetime import datetime # generate filepath relative to script location scriptPath = __file__ path = scriptPath[:-28] + '/data/' filepath = path + 'frozenDFICake.csv' # API request for freezer DFI link='https://poolapi.cakedefi.com/freezer-page' siteContent = requests.get(link) allFrozenCoins =
pd.read_json(siteContent.text, orient='colums')
pandas.read_json
import pandas as pd import numpy as np from matplotlib import colors, pyplot as plt import os from PIL import Image, ImageFont, ImageDraw, ImageEnhance import shutil import sqlite3 import sys from os.path import expanduser # generate a tile for each frame, annotating intersecting precursor cuboids MZ_MIN = 748 # default is 748 MZ_MAX = 766 # default is 766 SCAN_MIN = 350 # default is 1 SCAN_MAX = 850 # default is 920 RT_MIN = 2000 RT_MAX = 2200 PIXELS_X = 800 PIXELS_Y = 800 PIXELS_PER_MZ = PIXELS_X / (MZ_MAX - MZ_MIN) PIXELS_PER_SCAN = PIXELS_Y / (SCAN_MAX - SCAN_MIN) minimum_pixel_intensity = 1 maximum_pixel_intensity = 250 TILES_BASE_DIR = '{}/precursor-cuboid-3did-tiles'.format(expanduser('~')) # check the experiment directory exists experiment_base_dir = '/media/big-ssd/experiments' experiment_name = 'P3856' run_name = 'P3856_YHE211_1_Slot1-1_1_5104' EXPERIMENT_DIR = "{}/{}".format(experiment_base_dir, experiment_name) if not os.path.exists(EXPERIMENT_DIR): print("The experiment directory is required but doesn't exist: {}".format(EXPERIMENT_DIR)) sys.exit(1) # check the converted databases directory exists CONVERTED_DATABASE_NAME = "{}/converted-databases/exp-{}-run-{}-converted.sqlite".format(EXPERIMENT_DIR, experiment_name, run_name) if not os.path.isfile(CONVERTED_DATABASE_NAME): print("The converted database is required but doesn't exist: {}".format(CONVERTED_DATABASE_NAME)) sys.exit(1) CUBOIDS_DIR = "{}/precursor-cuboids-3did".format(EXPERIMENT_DIR) CUBOIDS_FILE = '{}/exp-{}-run-{}-precursor-cuboids-3did.pkl'.format(CUBOIDS_DIR, experiment_name, run_name) # frame types for PASEF mode FRAME_TYPE_MS1 = 0 FRAME_TYPE_MS2 = 8 # font paths for overlay labels UBUNTU_FONT_PATH = '/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf' MACOS_FONT_PATH = '/Library/Fonts/Arial.ttf' def pixel_x_from_mz(mz): pixel_x = int((mz - MZ_MIN) * PIXELS_PER_MZ) return pixel_x def pixel_y_from_scan(scan): pixel_y = int((scan - SCAN_MIN) * PIXELS_PER_SCAN) return pixel_y print('loading raw data from {}'.format(CONVERTED_DATABASE_NAME)) db_conn = sqlite3.connect(CONVERTED_DATABASE_NAME) raw_df = pd.read_sql_query("select frame_id,mz,scan,intensity,retention_time_secs from frames where frame_type == {} and mz >= {} and mz <= {} and scan >= {} and scan <= {} and retention_time_secs >= {} and retention_time_secs <= {}".format(FRAME_TYPE_MS1, MZ_MIN, MZ_MAX, SCAN_MIN, SCAN_MAX, RT_MIN, RT_MAX), db_conn) db_conn.close() raw_df['pixel_x'] = raw_df.apply(lambda row: pixel_x_from_mz(row.mz), axis=1) raw_df['pixel_y'] = raw_df.apply(lambda row: pixel_y_from_scan(row.scan), axis=1) # sum the intensity of raw points that have been assigned to each pixel pixel_intensity_df = raw_df.groupby(by=['frame_id', 'pixel_x', 'pixel_y'], as_index=False).intensity.sum() print('intensity range {}..{}'.format(pixel_intensity_df.intensity.min(), pixel_intensity_df.intensity.max())) # create the colour map to convert intensity to colour colour_map = plt.get_cmap('ocean') # colour_map = cm.batlow norm = colors.LogNorm(vmin=minimum_pixel_intensity, vmax=maximum_pixel_intensity, clip=True) # aiming to get good colour variation in the lower range, and clipping everything else # calculate the colour to represent the intensity colours_l = [] for i in pixel_intensity_df.intensity.unique(): colours_l.append((i, colour_map(norm(i), bytes=True)[:3])) colours_df =
pd.DataFrame(colours_l, columns=['intensity','colour'])
pandas.DataFrame
""" This is the dashboard of CEA """ from __future__ import division from __future__ import print_function import os import cea.config import cea.inputlocator from cea.plots.solar_potential.solar_radiation_curve import solar_radiation_curve from cea.plots.solar_potential.solar_radiation import solar_radiation_district from cea.plots.solar_potential.solar_radiation_monthly import solar_radiation_district_monthly from cea.utilities import epwreader import pandas as pd import numpy as np import time __author__ = "<NAME>" __copyright__ = "Copyright 2018, Architecture and Building Systems - ETH Zurich" __credits__ = ["<NAME>"] __license__ = "MIT" __version__ = "0.1" __maintainer__ = "<NAME>" __email__ = "<EMAIL>" __status__ = "Production" def plots_main(locator, config): # initialize timer t0 = time.clock() # Local Variables buildings = config.plots.buildings weather = config.weather # initialize class plots = Plots(locator, buildings, weather) category = "basic//solar-potential" if len(buildings) == 1: # when only one building is passed. plots.solar_radiation_curve(category) plots.solar_radiation_district_monthly(category) plots.solar_radiation_per_building(category) else: # when two or more buildings are passed # generate plots plots.solar_radiation_curve(category) plots.solar_radiation_district_monthly(category) plots.solar_radiation_per_building(category) # print execution time time_elapsed = time.clock() - t0 print('done - time elapsed: %d.2f seconds' % time_elapsed) return class Plots(): def __init__(self, locator, buildings, weather): # local variables self.locator = locator self.weather = weather self.analysis_fields = ['windows_east', 'windows_west', 'windows_south', 'windows_north', 'walls_east', 'walls_west', 'walls_south', 'walls_north', 'roofs_top'] self.buildings = self.preprocess_buildings(buildings) self.data_processed_district = self.preprocessing(self.analysis_fields, self.buildings, self.locator, self.weather) self.plot_title_tail = self.preprocess_plot_title(buildings) self.plot_output_path_header = self.preprocess_plot_outputpath(buildings) def preprocess_buildings(self, buildings): if buildings == []: # get all buildings of the district if not indicated a single building return self.locator.get_zone_building_names() else: return buildings def preprocess_plot_outputpath(self, buildings): if buildings == []: # get all buildings of the district if not indicated a single building return "District" elif len(buildings) == 1: return "Building_" + str(buildings[0]) else: return "District" def preprocess_plot_title(self, buildings): if buildings == []: # get all buildings of the district if not indicated a single building return " for District" elif len(buildings) == 1: return " for Building " + str(buildings[0]) else: return " for Selected Buildings" def preprocessing(self, analysis_fields, buildings, locator, weather): # get extra data of weather and date weather_data = epwreader.epw_reader(weather)[["date", "drybulb_C", "wetbulb_C", "skytemp_C"]] # get data of buildings input_data_not_aggregated_MW = [] dict_not_aggregated = {} for i, building in enumerate(buildings): geometry = pd.read_csv(locator.get_radiation_metadata(building)) geometry['code'] = geometry['TYPE'] + '_' + geometry['orientation'] insolation = pd.read_json(locator.get_radiation_building(building)) if i == 0: for field in analysis_fields: select_sensors = geometry.loc[geometry['code'] == field].set_index('SURFACE') array_field = np.array( [select_sensors.ix[surface, 'AREA_m2'] * insolation[surface] for surface in select_sensors.index]).sum(axis=0) # dict_not_aggregated[field] = array_field # add date and resample into months df_not_aggregated = pd.DataFrame(dict_not_aggregated) resample_data_frame = (df_not_aggregated.sum(axis=0) / 1000000).round(2) # into MWh input_data_not_aggregated_MW = pd.DataFrame({building: resample_data_frame}, index=resample_data_frame.index).T else: dict_not_aggregated_2 = {} for field in analysis_fields: select_sensors = geometry.loc[geometry['code'] == field].set_index('SURFACE') array_field = np.array( [select_sensors.ix[surface, 'AREA_m2'] * insolation[surface] for surface in select_sensors.index]).sum(axis=0) dict_not_aggregated_2[field] = array_field # W dict_not_aggregated[field] = dict_not_aggregated[field] + array_field # add date and resample into months df_not_aggregated =
pd.DataFrame(dict_not_aggregated_2)
pandas.DataFrame
import pandas as pd import sqlite3 as sql from asn1crypto._ffi import null from pandas.tests.frame.test_sort_values_level_as_str import ascending class FunctionMgr: def __init__(self,sql_con): self.sql_con = sql_con pass ''' 获取大于某换手率的股票列表 ''' def GetTurnoverRateList(self,rate,start_date,end_date): data = pd.read_sql_query('select ts_code,trade_date,turnover_rate,turnover_rate_f from daily_basic where trade_date <='+str(end_date)+' and trade_date >='+str(start_date)+' and turnover_rate_f >='+str(rate),self.sql_con) return data ''' 获取时间段内平均换手率排名列表 ''' def GetTurnoverRateMeanSortList(self,start_date,end_date): data = self.GetTurnoverRateList(1, start_date, end_date) group = data.groupby(by = 'ts_code') def func(item): tmp = dict() tmp['mean_rate_f'] = item.turnover_rate_f.mean() tmp['mean_rate'] = item.turnover_rate.mean() tmp['mean_count'] = len(item) return pd.Series(tmp) ans = group.apply(func) return (ans) ''' 根据代码查询概念 ''' def GetConceptByCode(self,code): name_data = pd.read_sql_query('select * from concept_info',self.sql_con) detail_data = pd.read_sql_query('select * from concept_detail where ts_code = \''+str(code)+'\'', self.sql_con) name_list = [] for item in detail_data.id: subdata = name_data[name_data.code == item] #print(subdata.name.iloc[0]) name_list.append(subdata.name.iloc[0]) #print(name_list) detail_data['concept_name'] = name_list return detail_data ''' 概念排名 ''' def GetConceptSortList(self,code_list): ans_dict=dict() for stock_code in code_list: data = self.GetConceptByCode(stock_code) if not data.empty: for name in data.concept_name: if name in ans_dict.keys(): ans_dict[name] = ans_dict[name] + 1 else: ans_dict[name] = 1 ans_frame = pd.DataFrame() ans_frame['name'] = ans_dict.keys() ans_frame['value'] = ans_dict.values() return ans_frame.sort_values(by='value',ascending=False) ''' 得到某时间段内日数据 ''' def GetDaily(self,start_date,end_date): data = pd.read_sql_query('select * from daily where trade_date >= '+str(start_date)+' and trade_date <= '+str(end_date),self.sql_con) if data.empty: print('GetDaily is empty ['+str(start_date)+'->'+str(end_date)+']') return data ''' 得到某时间段内平均日价格变化 ''' def GetPctChangeSumList(self,start_date,end_date): data = self.GetDaily(start_date, end_date) if data.empty: print('GetPctChangeSumList data is empty') group = data.groupby(by='ts_code') def func(item): tmp = dict() p_all = 1 for p in item['pct_change']: p_all = p_all*(1+p/100) tmp['sum_pct_change'] = (p_all-1)*100 tmp['sum_count'] = len(item) return pd.Series(tmp) ans = group.apply(func) return ans ''' 获取某时间段内的累计涨幅和换手率 ''' def GetSumPChangeAndMeanTurnoverRateList(self,start_date,end_date): sum_pctchange_data = self.GetPctChangeSumList(start_date, end_date) mean_turnover_data = self.GetTurnoverRateMeanSortList(start_date, end_date) mean_data = pd.merge(left=sum_pctchange_data,right=mean_turnover_data,left_index=True,right_index=True) return mean_data ''' 获取某时间段内某概念的平均涨幅和还手率 ''' def GetConceptSumPChangeAndMeanTurnoverRateList(self,concept_id,start_date,end_date): concept_detail_all_data = pd.read_sql_query('select * from concept_detail where id = \''+str(concept_id)+'\'', self.sql_con) concept_data = concept_detail_all_data[concept_detail_all_data.id == concept_id].set_index('ts_code') mean_data = self.GetSumPChangeAndMeanTurnoverRateList(start_date, end_date) merge_data = pd.merge(left=mean_data,right=concept_data,left_index=True,right_index=True) #print(sum_pctchange_data) return merge_data ''' 获得换手率与价格浮动的关系表,截止到某日 ''' def GetPctChangeAndTurnoverRateRelationList(self,end_date,al=5,bs=5,bl=10): date_all_len = al+bl date_list = pd.read_sql_query('select * from trade_cal where is_open = 1 and cal_date <="'+str(end_date)+'"',self.sql_con) date_list_all = date_list[-date_all_len:] pdate = date_list_all.cal_date.iloc[-al] sdate_bl = date_list_all.cal_date.iloc[0] sdate_bs = date_list_all.cal_date.iloc[bs] data_turnover = pd.read_sql_query('select ts_code,trade_date,turnover_rate,turnover_rate_f from daily_basic where trade_date <='+str(end_date)+' and trade_date >='+str(sdate_bl), self.sql_con) data_daily = pd.read_sql_query('select trade_date,ts_code,pct_change from daily where trade_date >= '+str(sdate_bl)+' and trade_date <= '+str(end_date),self.sql_con) merge_data =
pd.merge(left=data_daily, left_on=['ts_code','trade_date'],right=data_turnover, right_on=['ts_code','trade_date'])
pandas.merge
""" 2020-10-09 MY Printing plots of forecasts from JRC, Satotilasto and EO predictions. RUN: python forecasting.py -i /Users/myliheik/Documents/myCROPYIELD/cropyieldMosaics/results/test1320 -y 2018 -n python forecasting.py -i /Users/myliheik/Documents/myCROPYIELD/cropyieldMosaics/results/test1320 -y 2018 -n -r -f # To use meteorological features: python forecasting.py -i /Users/myliheik/Documents/myCROPYIELD/cropyieldMosaics/results/test1320 -y 2018 -n -m """ import argparse import textwrap import pandas as pd import pickle import seaborn as sns sns.set_theme(style="whitegrid") import os.path from pathlib import Path def save_intensities(filename, arrayvalues): with open(filename, 'wb+') as outputfile: pickle.dump(arrayvalues, outputfile) def load_intensities(filename): with open(filename, "rb") as f: data = pickle.load(f) return data def forecast(predfile: str, year: int, saveForecast=False, saveGraph=False, showGraph=True, doRNN=False, doRNNfull=False): # Make forecasts from predictions basename = predfile.split('/')[-1][:-4] predfileJune = os.path.join(os.path.dirname(predfile), basename + 'June' + '.pkl') predfileJuly = os.path.join(os.path.dirname(predfile), basename + 'July' + '.pkl') predfileAugust = os.path.join(os.path.dirname(predfile), basename + 'August' + '.pkl') setti = predfile.split('/')[-2][-4:] ard = predfile.split('/')[-1][:-4] basepath = predfile.split('/')[1:-3] img = "Forecasts-" + setti + "-" + str(year) + ".png" imgpath = os.path.join(os.path.sep, *basepath, 'img', ard) imgfp = Path(os.path.expanduser(imgpath)) imgfp.mkdir(parents=True, exist_ok=True) imgfile = os.path.join(imgfp, img) #print(setti) if setti == '1110': settix = 'winter wheat' if setti == '1120': settix = 'spring wheat' if setti == '1230': settix = 'rye' if setti == '1310': settix = 'feed barley' if setti == '1320': settix = 'malting barley' if setti == '1400': settix = 'oat' ##### Satotilasto: print('\nReading Luke satotilasto forecasts for crop', setti) fp = '/Users/myliheik/Documents/myCROPYIELD/ennusteetJRC/satotilastoLukeEnnusteet.csv' df = pd.read_csv(fp, sep=';', skiprows=4) if int(setti) == 1310 or int(setti) == 1320: # pitää hakea myös 1300 sato = df[(df['Vuosi'] == year) & ((df['Vilja'] == int(setti)) | (df['Vilja'] == 1300))].sort_values(by = 'DOY', axis=0)['satoennuste'] else: sato = df[(df['Vuosi'] == year) & (df['Vilja'] == int(setti))].sort_values(by = 'DOY', axis=0)['satoennuste'] #print(df[(df['Vuosi'] == year) & (df['Vilja'] == int(setti))]) sato0 = pd.Series([None]) sato1 = sato0.append(sato) ##### Random forest: # Read predictions: preds = load_intensities(predfile) preds['year'] = preds['farmID'].str.split('_').str[0] average = preds[preds['year'] == str(year)].groupby(['ELY']).mean().mean() predsJune = load_intensities(predfileJune) predsJune['year'] = predsJune['farmID'].str.split('_').str[0] averageJune = predsJune[predsJune['year'] == str(year)].groupby(['ELY']).mean().mean() predsJuly = load_intensities(predfileJuly) predsJuly['year'] = predsJuly['farmID'].str.split('_').str[0] averageJuly = predsJuly[predsJuly['year'] == str(year)].groupby(['ELY']).mean().mean() predsAugust = load_intensities(predfileAugust) predsAugust['year'] = predsAugust['farmID'].str.split('_').str[0] averageAugust = predsAugust[predsAugust['year'] == str(year)].groupby(['ELY']).mean().mean() d = {'Month': ['June', 'July', 'August','Final'], 'RF': [int(averageJune), int(averageJuly), int(averageAugust), int(average)], 'Satotilasto': sato1} output = pd.DataFrame(data=d) ####### JRC: print('Reading JRC crop yield forecasts for crop', setti) fp = '/Users/myliheik/Documents/myCROPYIELD/ennusteetJRC/JRCMARS4CASTs.txt' df = pd.read_csv(fp, sep=',', skiprows=5) if int(setti) == 1110 or int(setti) == 1120: jrcsetti = 1100 if int(setti) == 1310 or int(setti) == 1320: jrcsetti = 1300 if int(setti) == 1230: jrcsetti = 1230 if int(setti) == 1400: jrcsetti = None # there is no JRC forecasts for oat 1400 if jrcsetti is not None: sato2 = df[(df['Vuosi'] == year) & (df['Vilja'] == int(jrcsetti))].sort_values(by = 'DOY', axis=0).reset_index(drop=True)['ennuste']*1000 #print(sato2) sato2[4] = None output = output.assign(JRC = sato2.values) ####### LSTM: print('Reading LSTM crop yield predictions for crop', setti) predfile3D = load_intensities(os.path.join(os.path.dirname(predfile), 'ard3DPreds.pkl')) farmID3D = load_intensities(os.path.join(os.path.dirname(predfile), 'y3DfarmID.pkl')) if predfile3D.shape[0] != farmID3D.shape[0]: print("predfile3D and farmID3D numbers don't match!") predfile3D['year'] = farmID3D.str.split('_').str[0].values predictions = predfile3D[predfile3D['year'] == str(year)].mean(axis = 0) output = output.assign(LSTM = predictions[:-1].values.astype(int)) if doRNN: ####### RNN from histograms: print('Reading RNN crop yield predictions for crop', setti) testsetti = 'test' + setti predfile3D = load_intensities(os.path.join(os.path.dirname('/Users/myliheik/Documents/myCROPYIELD/RNNpreds/'), testsetti, 'RNNPreds.pkl')) fpRNN = 'farmID_test' + setti + '.pkl' farmID3D = load_intensities(os.path.join(os.path.dirname('/Users/myliheik/Documents/myCROPYIELD/dataStack/'), fpRNN)) if predfile3D.shape[0] != farmID3D.shape[0]: print("RNN predfile and farmID numbers don't match!") predfile3D['year'] =
pd.Series(farmID3D)
pandas.Series
import numpy as np import pandas as pd from utilities.constants import TREAT, CONC, index_order, column_order _THRESHOLD_ABOVE = 1 _THRESHOLD_BELOW = 0 def threshold_dict(data, _THRESHOLD): # TODO -- document """COMMENT""" # std and mean by column numerical_cols = data._get_numeric_data().columns negative_controls = data[data[CONC] == '0 ug/mL'] std = np.array([negative_controls[col_name].std() for col_name in numerical_cols]) mean = np.array([negative_controls[col_name].mean() for col_name in numerical_cols]) target_count = np.array([negative_controls[col_name].count() for col_name in numerical_cols]) # make an array which has one std or mean for each column of negative controls # Define threshold if _THRESHOLD == _THRESHOLD_ABOVE: threshold = mean + 2 * std elif _THRESHOLD == _THRESHOLD_BELOW: threshold = mean - 1 * std threshold_dict = {name: t for name, t in zip(numerical_cols, threshold)} # thresholds are calculated on negative controls return threshold_dict def extract_threshold_percentile(data, feature, threshold_dict, _THRESHOLD): # TODO -- document """COMMENT""" result = dict() for t in set(data[TREAT]): conc_result = dict() for c in set(data[CONC]): selection = (data[CONC] == c) & (data[TREAT] == t) if _THRESHOLD == _THRESHOLD_ABOVE: conc_result[c] = (data.loc[selection, :][feature] > threshold_dict[feature]).sum() / selection.sum() elif _THRESHOLD == _THRESHOLD_BELOW: conc_result[c] = (data.loc[selection, :][feature] < threshold_dict[feature]).sum() / selection.sum() result[t] = conc_result return
pd.DataFrame.from_dict(result, orient='index')
pandas.DataFrame.from_dict