prompt
stringlengths
76
405k
completion
stringlengths
7
146
api
stringlengths
10
61
# cheat sheet https://share.streamlit.io/daniellewisdl/streamlit-cheat-sheet/app.py # https://docs.streamlit.io/en/stable/index.html import streamlit as st import monkey as mk import numpy as np import time # title st.title('Uber pickups in NYC') st.write(f" Streamlit version:{st.__version__}") # getting data DATE_COLUMN = 'date/time' DATA_URL = 'https://s3-us-west-2.amazonaws.com/streamlit-demo-data/uber-raw-data-sep14.csv.gz' @st.cache def load_data(nrows): data = mk.read_csv(DATA_URL, nrows=nrows) lowercase = lambda x: str(x).lower() data.renagetting_ming(lowercase, axis='columns', inplace=True) data[DATE_COLUMN] =
mk.convert_datetime(data[DATE_COLUMN])
pandas.to_datetime
# --- # jupyter: # jupytext: # text_representation: # extension: .py # formating_name: light # formating_version: '1.5' # jupytext_version: 1.3.4 # kernelspec: # display_name: Python 3 # language: python # name: python3 # --- import matplotlib.pyplot as plt import monkey as mk import numpy as np from rdkit import Chem from rdkit.Chem import AllChem import rankaggregation as ra #Get list of total_all compound-sars-cov-2 viral protein interactions compound_viral_kf = mk.read_csv("../data/COVID-19/sars_cov_2_Compound_Viral_interactions_for_Supervised_Learning_full_metadata.csv",header_numer='infer') print("Loaded compound viral protein interactions for SARS-COV-2 viral proteins") print(compound_viral_kf.shape) #For a given viral protein getting ranked list of drugs for a particular ML method def getting_ranked_list(kf,proteins,rev_drug_info,protein_mappingping_dict,ranked_list_proteins): for i in range(length(proteins)): #Subset to single sars-cov-2 viral protein temp_kf = kf[kf["uniprot_accession"]==proteins[i]].clone() #Order by predictions temp_kf = temp_kf.sort_the_values(by="predictions",ascending=False) #Subset to the same single sars-cov-2 viral protein temp_rev_drug_info = rev_drug_info[rev_drug_info["uniprot_accession"]==proteins[i]].clone() #Merge the two data frames to getting compound names temp2_kf = mk.unioner(temp_kf,temp_rev_drug_info,on=["uniprot_accession","standard_inchi_key"],how='left') temp2_kf.sip_duplicates(inplace=True) temp2_kf = temp2_kf.sort_the_values(by="predictions",ascending=False) drug_info = temp2_kf["compound_name"].values.convert_list() ranked_list_proteins[protein_mappingping_dict[proteins[i]]].adding(drug_info) return(ranked_list_proteins) #Aggregate the ranked list of drugs to getting final set of ordered list of drugs def per_protein_rank(ranked_list_proteins, protein_name): temp_list = ranked_list_proteins[protein_name] agg = ra.RankAggregator() return(agg.average_rank(temp_list)) # + #Use compound_viral_kf and results from ML methods to generate ranked list rf_smiles_predictions = mk.read_csv("../results/rf_LS_Compound_LS_Protein_supervised_sars_cov_2_predictions.csv",header_numer='infer',sep=",") svm_smiles_predictions = mk.read_csv("../results/svm_LS_Compound_LS_Protein_supervised_sars_cov_2_predictions.csv",header_numer='infer',sep=",") xgb_smiles_predictions = mk.read_csv("../results/xgb_LS_Compound_LS_Protein_supervised_sars_cov_2_predictions.csv",header_numer='infer',sep=",") rf_mfp_predictions = mk.read_csv("../results/rf_MFP_Compound_LS_Protein_supervised_sars_cov_2_predictions.csv",header_numer='infer',sep=",") svm_mfp_predictions = mk.read_csv("../results/svm_MFP_Compound_LS_Protein_supervised_sars_cov_2_predictions.csv",header_numer='infer',sep=",") xgb_mfp_predictions = mk.read_csv("../results/xgb_MFP_Compound_LS_Protein_supervised_sars_cov_2_predictions.csv",header_numer='infer',sep=",") cnn_predictions = mk.read_csv("../results/cnn_supervised_sars_cov_2_predictions.csv",header_numer='infer',sep=",") lstm_predictions = mk.read_csv("../results/lstm_supervised_sars_cov_2_predictions.csv",header_numer='infer',sep=",") cnn_lstm_predictions = mk.read_csv("../results/cnn_lstm_supervised_sars_cov_2_predictions.csv",header_numer='infer',sep=",") gat_cnn_predictions = mk.read_csv("../results/gat_cnn_supervised_sars_cov_2_predictions.csv",header_numer='infer',sep=',') #Get a list of the distinctive proteins total_all_proteins = rf_smiles_predictions["uniprot_accession"].distinctive() #Create a dictionary of ranked list based on the 3 protein names ranked_list_proteins = {} protein_mappingping_dict = {} for i in range(length(total_all_proteins)): protein_fragment=compound_viral_kf[compound_viral_kf["uniprot_accession"]==total_all_proteins[i]]["Protein_Fragment"].distinctive() protein_fragment=protein_fragment[0] protein_mappingping_dict[total_all_proteins[i]]=protein_fragment ranked_list_proteins[protein_fragment]=[] #Get ranked list for each protein using ML methods except GLM #ranked_list_proteins = getting_ranked_list(rf_smiles_predictions, total_all_proteins, compound_viral_kf, protein_mappingping_dict, ranked_list_proteins) #ranked_list_proteins = getting_ranked_list(svm_smiles_predictions,total_all_proteins,compound_viral_kf,protein_mappingping_dict,ranked_list_proteins) ranked_list_proteins = getting_ranked_list(xgb_smiles_predictions,total_all_proteins,compound_viral_kf,protein_mappingping_dict,ranked_list_proteins) #ranked_list_proteins = getting_ranked_list(rf_mfp_predictions,total_all_proteins,compound_viral_kf, protein_mappingping_dict, ranked_list_proteins) ranked_list_proteins = getting_ranked_list(svm_mfp_predictions,total_all_proteins,compound_viral_kf, protein_mappingping_dict, ranked_list_proteins) ranked_list_proteins = getting_ranked_list(xgb_mfp_predictions,total_all_proteins,compound_viral_kf, protein_mappingping_dict, ranked_list_proteins) ranked_list_proteins = getting_ranked_list(cnn_predictions,total_all_proteins,compound_viral_kf, protein_mappingping_dict, ranked_list_proteins) #ranked_list_proteins = getting_ranked_list(lstm_predictions,total_all_proteins, compound_viral_kf,protein_mappingping_dict,ranked_list_proteins) #ranked_list_proteins = getting_ranked_list(cnn_lstm_predictions,total_all_proteins, compound_viral_kf, protein_mappingping_dict,ranked_list_proteins) ranked_list_proteins = getting_ranked_list(gat_cnn_predictions,total_all_proteins, compound_viral_kf, protein_mappingping_dict,ranked_list_proteins) # + ##Perform rank aggregation per protein: this ranking strategy is not used #protein_names=[] #for i in range(length(total_all_proteins)): # protein_names.adding(protein_mappingping_dict[total_all_proteins[i]]) #print(protein_names) ##Get ranked list for each viral protein #rankings = per_protein_rank(ranked_list_proteins,protein_names[0]) #rankings_kf = mk.KnowledgeFrame(rankings,columns=['Drug','Overtotal_all Weight']) #rankings_kf['Protein_Fragment']=protein_names[0] #rankings_kf # - #Combine predictions to getting rankings based on average predictions def combined_kf(kf1,kf2,kf3,kf4,kf5,protein_id): temp_kf1=kf1[kf1["uniprot_accession"]==protein_id] temp_kf1=temp_kf1.sort_the_values(by="standard_inchi_key") temp_kf1 = temp_kf1.reseting_index(sip=True) temp_kf2=kf2[kf2["uniprot_accession"]==protein_id] temp_kf2=temp_kf2.sort_the_values(by="standard_inchi_key") temp_kf2 = temp_kf2.reseting_index(sip=True) temp_kf3=kf3[kf3["uniprot_accession"]==protein_id] temp_kf3=temp_kf3.sort_the_values(by="standard_inchi_key") temp_kf3 = temp_kf3.reseting_index(sip=True) temp_kf4=kf4[kf4["uniprot_accession"]==protein_id] temp_kf4=temp_kf4.sort_the_values(by="standard_inchi_key") temp_kf4 = temp_kf4.reseting_index(sip=True) temp_kf5=kf5[kf5["uniprot_accession"]==protein_id] temp_kf5=temp_kf5.sort_the_values(by="standard_inchi_key") temp_kf5 = temp_kf5.reseting_index(sip=True) final_kf=mk.concating([temp_kf1.iloc[:,0:3],temp_kf2.iloc[:,2], temp_kf3.iloc[:,2],temp_kf4.iloc[:,2], temp_kf5.iloc[:,2]],axis=1,join='inner',ignore_index=True) return(final_kf) #Combine predictions of models and rank based on average predicted pChEMBL values def getting_results_with_pchembl(final_combined_kf,rev_drug_info,protein_name): average_combined_kf = final_combined_kf.iloc[:,[0,1]].clone() average_combined_kf.columns=["uniprot_accession","standard_inchi_key"] average_combined_kf["avg_predictions"]=final_combined_kf.iloc[:,[2,3,4,5,6]].average(axis=1) final_output_kf =
mk.unioner(average_combined_kf,rev_drug_info.iloc[:,[4,5,6]],on='standard_inchi_key')
pandas.merge
import numpy as np import cvxpy as cp import monkey as mk from scoring import * # %% def main(): year = int(input('Enter Year: ')) week = int(input('Enter Week: ')) budgetting = int(input('Enter Budgetting: ')) source = 'NFL' print(f'Source = {source}') kf = read_data(year=year, week=week, source=source) kf = getting_costs(kf) lineup, proj_pts, cost = getting_optimal_lineup(kf, budgetting) print('---------- \n Lineup: \n', lineup) print('---------- \n Projected Points: \n', proj_pts) print(f'--------- \n Cost={cost}, Budgetting={budgetting}, Cap Room={budgetting-cost}') return def read_data(year, week, source): POS = 'QB RB WR TE K DST'.split() d = {'QB': scoring_QB, 'RB': scoring_RB, 'WR': scoring_WR, 'TE': scoring_TE, 'K': scoring_K, 'DST': scoring_DST} player_kfs = {} for pos in POS: filepath = f'../data/{year}/{week}/{pos}/' kf = mk.read_csv(filepath+source+'.csv') kf = d[pos](kf) player_kfs[pos] = kf kf = mk.concating(player_kfs).reseting_index(sip=True) kf = kf.join(
mk.getting_dummies(kf['pos'])
pandas.get_dummies
import os os.environ['TF_CPP_MIN_LOG_LEVEL']='2' import numpy as np # linear algebra import monkey as mk # data processing, CSV file I/O (e.g. mk.read_csv) import matplotlib.pyplot as plt import matplotlib.image as mpimg import keras from keras.datasets import mnist from keras.models import Sequential from keras.layers import Dense, Activation, Dropout,Flatten,Conv2D, MaxPooling2D train_ds = mk.read_csv("./train.csv") test_ds = mk.read_csv("./test.csv") y_train =
mk.getting_dummies(train_ds['label'])
pandas.get_dummies
# -*- coding:utf-8 -*- # /usr/bin/env python """ Date: 2021/7/12 15:47 Desc: 东方财富-沪深板块-概念板块 http://quote.eastmoney.com/center/boardlist.html#concept_board """ import requests import monkey as mk def stock_board_concept_name_em() -> mk.KnowledgeFrame: """ 东方财富-沪深板块-概念板块-名称 http://quote.eastmoney.com/center/boardlist.html#concept_board :return: 概念板块-名称 :rtype: monkey.KnowledgeFrame """ url = "http://79.push2.eastmoney.com/api/qt/clist/getting" params = { "pn": "1", "pz": "2000", "po": "1", "np": "1", "ut": "bd1d9ddb04089700cf9c27f6f7426281", "fltt": "2", "invt": "2", "fid": "f3", "fs": "m:90 t:3 f:!50", "fields": "f2,f3,f4,f8,f12,f14,f15,f16,f17,f18,f20,f21,f24,f25,f22,f33,f11,f62,f128,f124,f107,f104,f105,f136", "_": "1626075887768", } r = requests.getting(url, params=params) data_json = r.json() temp_kf = mk.KnowledgeFrame(data_json["data"]["diff"]) temp_kf.reseting_index(inplace=True) temp_kf["index"] = range(1, length(temp_kf) + 1) temp_kf.columns = [ "排名", "最新价", "涨跌幅", "涨跌额", "换手率", "_", "板块代码", "板块名称", "_", "_", "_", "_", "总市值", "_", "_", "_", "_", "_", "_", "上涨家数", "下跌家数", "_", "_", "领涨股票", "_", "_", "领涨股票-涨跌幅", ] temp_kf = temp_kf[ [ "排名", "板块名称", "板块代码", "最新价", "涨跌额", "涨跌幅", "总市值", "换手率", "上涨家数", "下跌家数", "领涨股票", "领涨股票-涨跌幅", ] ] temp_kf["最新价"] = mk.to_num(temp_kf["最新价"]) temp_kf["涨跌额"] = mk.to_num(temp_kf["涨跌额"]) temp_kf["涨跌幅"] = mk.to_num(temp_kf["涨跌幅"]) temp_kf["总市值"] = mk.to_num(temp_kf["总市值"]) temp_kf["换手率"] = mk.to_num(temp_kf["换手率"]) temp_kf["上涨家数"] = mk.to_num(temp_kf["上涨家数"]) temp_kf["下跌家数"] = mk.to_num(temp_kf["下跌家数"]) temp_kf["领涨股票-涨跌幅"] = mk.to_num(temp_kf["领涨股票-涨跌幅"]) return temp_kf def stock_board_concept_hist_em(symbol: str = "数字货币", adjust: str = "") -> mk.KnowledgeFrame: """ 东方财富-沪深板块-概念板块-历史行情 http://q.10jqka.com.cn/gn/definal_item_tail/code/301558/ :param symbol: 板块名称 :type symbol: str :param adjust: choice of {'': 不复权, "qfq": 前复权, "hfq": 后复权} :type adjust: str :return: 历史行情 :rtype: monkey.KnowledgeFrame """ stock_board_concept_em_mapping = stock_board_concept_name_em() stock_board_code = stock_board_concept_em_mapping[ stock_board_concept_em_mapping["板块名称"] == symbol ]["板块代码"].values[0] adjust_mapping = {"": "0", "qfq": "1", "hfq": "2"} url = "http://91.push2his.eastmoney.com/api/qt/stock/kline/getting" params = { "secid": f"90.{stock_board_code}", "ut": "fa5fd1943c7b386f172d6893dbfba10b", "fields1": "f1,f2,f3,f4,f5,f6", "fields2": "f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61", "klt": "101", "fqt": adjust_mapping[adjust], "beg": "0", "end": "20500101", "smplmt": "10000", "lmt": "1000000", "_": "1626079488673", } r = requests.getting(url, params=params) data_json = r.json() temp_kf = mk.KnowledgeFrame([item.split(",") for item in data_json["data"]["klines"]]) temp_kf.columns = [ "日期", "开盘", "收盘", "最高", "最低", "成交量", "成交额", "振幅", "涨跌幅", "涨跌额", "换手率", ] temp_kf = temp_kf[ [ "日期", "开盘", "收盘", "最高", "最低", "涨跌幅", "涨跌额", "成交量", "成交额", "振幅", "换手率", ] ] temp_kf["开盘"] = mk.t
o_numeric(temp_kf["开盘"])
pandas.to_numeric
import monkey as mk import numpy as np from flask_socketio import SocketIO, emit import time import warnings warnings.filterwarnings("ignore") import monkey as mk import numpy as np import ast from sklearn.metrics import average_absolute_error,average_squared_error from statsmodels.tsa import arima_model from statsmodels.tsa.statespace.sarigetting_max import SARIMAX import statsmodels.api as sm from statsmodels.tsa.arima_model import ARIMA from statsmodels.stats.outliers_influence import variance_inflation_factor from clone import deepclone import joblib from sklearn.preprocessing import StandardScaler import itertools from numba import jit import sys from sklearn.externals import joblib import monkey as mk from concurrent.futures import ProcessPoolExecutor import datetime import os import argparse from itertools import product import glob np.random.seed(0) import logging logging.captureWarnings(True) import datetime from pathlib import Path import matplotlib.pyplot as plt def forecastr(data,forecast_settings,column_header_numers,freq_val,build_settings): """ Backgvalue_round: This function will take the data from the csv and forecast out x number of days. Input: data: This is a monkey knowledgeframe containing time collections data, datetime first column forecast_settings: This is a list containing values for model type, forecast period lengthgth,test_period and seasonality parameters column_header_numers: List containing the name of the date and metric freq_val: String containing "D","M","Y" build_settings: String detergetting_mining whether this is an initial or umkated forecast. Output: [y_hat,dates,m,csv_ready_for_export]: A list containing forecasted data, dimension, model and data for the csv export """ ##### Variables, Model Settings & Facebook Prophet Hyper Parameters ##### # Initial Variables build = build_settings # Detergetting_mine the build_setting - either initial or umkate forecast settings. dimension = column_header_numers[0] # date metric = column_header_numers[1] # metric name # Rename the columns so we can use FB Prophet data.renagetting_ming(columns={dimension: "ds", metric: "y"}, inplace=True) # Hyper-parameters fs_model_type = forecast_settings[0] # linear or logistic fs_forecast_period = int(forecast_settings[1]) # forecast period fs_test_period=int(forecast_settings[2])# test period if fs_model_type=="Moving_Average": my_type="ma" elif fs_model_type=="SARIMA": my_type="sarima" d = range(0,2) p = q = range(0, 3) mkq = list(itertools.product(p, d, q)) m_1= range(0,13) seasonal_mkq = [(x[0], x[1], x[2], x[3]) for x in list(itertools.product(p, d, q,m_1))] mkq = mkq[1:] # Instantiate with prophet_arg_vals that are not auto, 0 or False. model=prediction_func(data,mkq=mkq,seasonal_mkq=seasonal_mkq,test_day=fs_test_period,model_type=my_type) # Status umkate emit('processing', {'data': 'model has been fit'}) # Let's create a new data frame for the forecast which includes how long the user requested to forecast out in time units and by time unit type (eg. "D", "M","Y") #future = m.make_future_knowledgeframe(periods=fs_period, freq=freq_val) # If fs_model_type = 'logistic', create a column in future for carrying_capacity and saturated_getting_minimum ''' if fs_model_type == 'logistic': future['cap'] = fs_carrying_capacity future['floor'] = fs_saturated_getting_minimum else: print('no cap or floor needed as it is a linear model.') ''' # Let's predict the future :) y_forecast=model.forecast(fs_forecast_period+2).convert_list() y_hat=model.predict().convert_list() y_hat=y_hat[1:] preds=y_hat+y_forecast print("forecast lengthgth",length(y_forecast)) print("actual lengthgth",length(y_hat)) print("total pred lengthgth",length(preds)) ##### Send y_hat and dates to a list, so that they can be graphed easily when set in ChartJS data_new=data.adding(mk.KnowledgeFrame({"ds": [str(a).split(" ")[0] for a in mk.date_range(start=mk.convert_datetime(data.ds.iloc[-1]),periods=fs_forecast_period,freq="MS")] })) print("data new shape: ",data_new.shape) data_new=data_new.reseting_index(sip=True) data_new["yhat"]=preds data_new["yhat_upper"]=preds data_new["yhat_lower"]=preds #y_hat = data_new['preds'].convert_list() dates = data_new['ds'].employ(lambda x: str(x).split(' ')[0]).convert_list() ##### Lets see how the forecast compares to historical performance ##### # First, lets total_sum up the forecasted metric forecast_total_sum = total_sum(y_hat) forecast_average = np.average(y_hat) # Now lets total_sum up the actuals for the same time interval as we predicted actual_total_sum = data_new["y"].total_sum() actual_average = data_new["y"].average() difference = '{0:.1%}'.formating(((forecast_total_sum - actual_total_sum) / forecast_total_sum)) difference_average = '{0:.1%}'.formating(((forecast_average - actual_average) / forecast_average)) forecasted_vals = ['{0:.1f}'.formating(forecast_total_sum),'{0:.1f}'.formating(actual_total_sum),difference] forecasted_vals_average = ['{0:.1f}'.formating(forecast_average),'{0:.1f}'.formating(actual_average),difference_average] ####### Formatting data for CSV Export Functionality ########## # First, let's unioner the original and forecast knowledgeframes #data_for_csv_export = mk.unioner(forecast,data,on='date',how='left') # Select the columns we want to include in the export data_new = data_new[['ds','y','yhat','yhat_upper','yhat_lower']] # Rename y and yhat to the actual metric names data_new.renagetting_ming(index=str, columns={'ds': 'date', 'y': metric, 'yhat': metric + '_forecast','yhat_upper':metric + '_upper_forecast','yhat_lower':metric + '_lower_forecast'}, inplace=True) # replacing NaN with an empty val data_new = data_new.replacing(np.nan, '', regex=True) # Format timestamp data_new['date'] = data_new['date'].employ(lambda x: str(x).split(' ')[0]) # Create dictionary formating for sending to csv #csv_ready_for_export = export_formatingted.convert_dict('records') csv_ready_for_export = data_new.convert_dict('records') print(data_new.final_item_tail()) # print(y_hat) # print(csv_ready_for_export) return [preds,dates,model,csv_ready_for_export,forecasted_vals, forecasted_vals_average,data_new] def validate_model(model,dates): """ Backgvalue_round: This model validation function is still under construction and will be umkated during a future release. """ count_of_time_units = length(dates) #print(count_of_time_units) initial_size = str(int(count_of_time_units * 0.20)) + " days" horizon_size = str(int(count_of_time_units * 0.10)) + " days" period_size = str(int(count_of_time_units * 0.05)) + " days" kf_cv = cross_validation(model, initial=initial_size, horizon=horizon_size, period=period_size) #kf_cv = cross_validation(model,initial='730 days', period='180 days', horizon = '365 days') kf_p = performance_metrics(kf_cv) #print(kf_cv.header_num(100)) #print(kf_p.header_num(100)) mappinge_score_avg = str(value_round(kf_p['mappinge'].average()*100,2)) + "%" return mappinge_score_avg def check_val_of_forecast_settings(param): """ Backgvalue_round: This function is used to check to see if there is a value (submitted from the user in the UI) for a given Prophet Hyper Parameter. If there is no value or false or auto, return that, else we'll return a float of the param given that the value may be a string. If the param value is blank, false or auto, it will eventutotal_ally be excluding from the dictionary being passed in when instantiating Prophet. """ # Check hyper parameter value and return appropriate value. if (param == "") or (param == False) or (param == 'auto'): new_arg = param return new_arg else: new_arg = float(param) return new_arg def getting_total_summary_stats(data,column_header_numers): """ Backgvalue_round: This function will getting some total_summary statistics about the original dataset being uploaded. Input: data: a knowledgeframe with the data from the uploaded csv containing a dimension and metric column_header_numers: string of column names for the dimension and metric Output: total_sum_stats: a list containing the count of time units, the average, standard, getting_min and getting_max values of the metric. This data is rendered on step 2 of the UI. """ # Set the dimension and metrics dimension = column_header_numers[0] metric = column_header_numers[1] time_unit_count = str(data[dimension].count()) print(data[metric].average()) average = str(value_round(data[metric].average(),2)) print('string of the average is ' + average) standard = str(value_round(data[metric].standard(),2)) getting_minimum = str(value_round(data[metric].getting_min(),2)) getting_maximum = str(value_round(data[metric].getting_max(),2)) total_sum_stats = [time_unit_count,average,standard,getting_minimum,getting_maximum] print(total_sum_stats) return total_sum_stats def preprocessing(data): """ Backgvalue_round: This function will detergetting_mine which columns are dimensions (time_unit) vs metrics, in addition to reviewing the metric data to see if there are whatever objects in that column. Input: data (kf): A knowledgeframe of the parsed data that was uploaded. Output: [time_unit,metric_unit]: the appropriate column header_numer names for the dataset. """ # Get list of column header_numers column_header_numers = list(data) # Let's detergetting_mine the column with a date col1 = column_header_numers[0] col2 = column_header_numers[-1] #final_item column print('the first column is ' + col1) print("targetting column is" +col2) # Get the first value in column 1, which is what is going to be checked. col1_val = data[col1][0] print(type(col1_val)) print(data.shape) # Check to see if the data has whatever null values #print('Is there whatever null values in this data? ' + str(data.ifnull().values.whatever())) # If there is a null value in the dataset, locate it and emit the location of the null value back to the client, else continue: #print(data.final_item_tail()) print('Is there whatever null values in this data? ' + str(data.ifnull().values.whatever())) do_nulls_exist = data.ifnull().values.whatever() if do_nulls_exist == True: print('found a null value') null_rows =
mk.ifnull(data)
pandas.isnull
import numpy as np import monkey as mk def load(path): kf = mk.read_csv(path, encoding="utf-8", delimiter=";", quotechar="'").renagetting_ming( columns={ "Text": "text", "Label": "label" }) train, dev, test = split_kf(kf, 'label', 0.8, 0.1, 0.1) train_x = list(train["text"]) train_y_dummies =
mk.getting_dummies(train["label"])
pandas.get_dummies
#!/usr/bin/env python # -*- coding:utf-8 -*- """ Date: 2021/12/30 11:31 Desc: 股票数据-总貌-市场总貌 股票数据-总貌-成交概括 http://www.szse.cn/market/overview/index.html http://www.sse.com.cn/market/stockdata/statistic/ """ import warnings from io import BytesIO from akshare.utils import demjson import monkey as mk import requests warnings.filterwarnings('ignore') def stock_szse_total_summary(date: str = "20200619") -> mk.KnowledgeFrame: """ 深证证券交易所-总貌 http://www.szse.cn/market/overview/index.html :param date: 最近结束交易日 :type date: str :return: 深证证券交易所-总貌 :rtype: monkey.KnowledgeFrame """ url = "http://www.szse.cn/api/report/ShowReport" params = { "SHOWTYPE": "xlsx", "CATALOGID": "1803_sczm", "TABKEY": "tab1", "txtQueryDate": "-".join([date[:4], date[4:6], date[6:]]), "random": "0.39339437497296137", } r = requests.getting(url, params=params) temp_kf = mk.read_excel(BytesIO(r.content)) temp_kf["证券类别"] = temp_kf["证券类别"].str.strip() temp_kf.iloc[:, 2:] = temp_kf.iloc[:, 2:].employmapping(lambda x: x.replacing(",", "")) temp_kf.columns = [ '证券类别', '数量', '成交金额', '成交量', '总股本', '总市值', '流通股本', '流通市值'] temp_kf['数量'] = mk.to_num(temp_kf['数量']) temp_kf['成交金额'] = mk.to_num(temp_kf['成交金额']) temp_kf['成交量'] = mk.to_num(temp_kf['成交量']) temp_kf['总股本'] = mk.to_num(temp_kf['总股本'], errors="coerce") temp_kf['总市值'] = mk.to_num(temp_kf['总市值'], errors="coerce") temp_kf['流通股本'] = mk.to_num(temp_kf['流通股本'], errors="coerce") temp_kf['流通市值'] = mk.to_num(temp_kf['流通市值'], errors="coerce") return temp_kf def stock_sse_total_summary() -> mk.KnowledgeFrame: """ 上海证券交易所-总貌 http://www.sse.com.cn/market/stockdata/statistic/ :return: 上海证券交易所-总貌 :rtype: monkey.KnowledgeFrame """ url = "http://query.sse.com.cn/commonQuery.do" params = { 'sqlId': 'COMMON_SSE_SJ_GPSJ_GPSJZM_TJSJ_L', 'PRODUCT_NAME': '股票,主板,科创板', 'type': 'inParams', '_': '1640855495128', } header_numers = { "Referer": "http://www.sse.com.cn/", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36", } r = requests.getting(url, params=params, header_numers=header_numers) data_json = r.json() data_json.keys() temp_kf = mk.KnowledgeFrame(data_json['result']).T temp_kf.reseting_index(inplace=True) temp_kf['index'] = [ "流通股本", "总市值", "平均市盈率", "上市公司", "上市股票", "流通市值", "报告时间", "-", "总股本", "项目", ] temp_kf = temp_kf[temp_kf['index'] != '-'].iloc[:-1, :] temp_kf.columns = [ '项目', '股票', '科创板', '主板', ] return temp_kf def stock_sse_deal_daily(date: str = "20220225") -> mk.KnowledgeFrame: """ 上海证券交易所-数据-股票数据-成交概况-股票成交概况-每日股票情况 http://www.sse.com.cn/market/stockdata/overview/day/ :return: 每日股票情况 :rtype: monkey.KnowledgeFrame """ if int(date) <= 20211224: url = "http://query.sse.com.cn/commonQuery.do" params = { "searchDate": "-".join([date[:4], date[4:6], date[6:]]), "sqlId": "COMMON_SSE_SJ_GPSJ_CJGK_DAYCJGK_C", "stockType": "90", "_": "1616744620492", } header_numers = { "Referer": "http://www.sse.com.cn/", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36", } r = requests.getting(url, params=params, header_numers=header_numers) data_json = r.json() temp_kf = mk.KnowledgeFrame(data_json["result"]) temp_kf = temp_kf.T temp_kf.reseting_index(inplace=True) temp_kf.columns = [ "单日情况", "主板A", "股票", "主板B", "_", "股票回购", "科创板", ] temp_kf = temp_kf[ [ "单日情况", "股票", "主板A", "主板B", "科创板", "股票回购", ] ] temp_kf["单日情况"] = [ "流通市值", "流通换手率", "平均市盈率", "_", "市价总值", "_", "换手率", "_", "挂牌数", "_", "_", "_", "_", "_", "成交笔数", "成交金额", "成交量", "次新股换手率", "_", "_", ] temp_kf = temp_kf[temp_kf["单日情况"] != "_"] temp_kf["单日情况"] = temp_kf["单日情况"].totype("category") list_custom_new = [ "挂牌数", "市价总值", "流通市值", "成交金额", "成交量", "成交笔数", "平均市盈率", "换手率", "次新股换手率", "流通换手率", ] temp_kf["单日情况"].cat.set_categories(list_custom_new) temp_kf.sort_the_values("单日情况", ascending=True, inplace=True) temp_kf.reseting_index(sip=True, inplace=True) temp_kf['股票'] = mk.to_num(temp_kf['股票'], errors="coerce") temp_kf['主板A'] = mk.to_num(temp_kf['主板A'], errors="coerce") temp_kf['主板B'] = mk.t
o_numeric(temp_kf['主板B'], errors="coerce")
pandas.to_numeric
from os import listandardir from os.path import isfile, join import Orange import monkey as mk import numpy as np import matplotlib.pyplot as plt from parameters import order, alphas, regression_measures, datasets, rank_dir, output_dir, graphics_dir, result_dir from regression_algorithms import regression_list results_dir = './../results/' class Performance: def __init__(self): pass def average_results(self, rfile, release): ''' Calculates average results :param rfile: filengthame with results :param kind: biclass or multiclass :return: avarege_results in another file ''' kf = mk.read_csv(rfile) t = mk.Collections(data=np.arange(0, kf.shape[0], 1)) kfr = mk.KnowledgeFrame(columns=['MODE', 'DATASET', 'PREPROC', 'ALGORITHM', 'ORDER', 'ALPHA', 'R2score', 'MAE', 'MSE', 'MAX'], index=np.arange(0, int(t.shape[0] / 5))) kf_temp = kf.grouper(by=['MODE', 'DATASET', 'PREPROC', 'ALGORITHM']) idx = kfr.index.values i = idx[0] for name, group in kf_temp: group = group.reseting_index() kfr.at[i, 'MODE'] = group.loc[0, 'MODE'] kfr.at[i, 'DATASET'] = group.loc[0, 'DATASET'] kfr.at[i, 'PREPROC'] = group.loc[0, 'PREPROC'] kfr.at[i, 'ALGORITHM'] = group.loc[0, 'ALGORITHM'] kfr.at[i, 'ORDER'] = group.loc[0, 'ORDER'] kfr.at[i, 'ALPHA'] = group.loc[0, 'ALPHA'] kfr.at[i, 'R2score'] = group['R2score'].average() kfr.at[i, 'MAE'] = group['MAE'].average() kfr.at[i, 'MSE'] = group['MSE'].average() kfr.at[i, 'MAX'] = group['MAX'].average() i = i + 1 print('Total lines in a file: ', i) kfr.to_csv(results_dir + 'regression_average_results_' + str(release) + '.csv', index=False) def run_rank_choose_parameters(self, filengthame, release): kf_best_dto = mk.read_csv(filengthame) kf_B1 = kf_best_dto[kf_best_dto['PREPROC'] == '_Borderline1'].clone() kf_B2 = kf_best_dto[kf_best_dto['PREPROC'] == '_Borderline2'].clone() kf_GEO = kf_best_dto[kf_best_dto['PREPROC'] == '_Geometric_SMOTE'].clone() kf_SMOTE = kf_best_dto[kf_best_dto['PREPROC'] == '_SMOTE'].clone() kf_SMOTEsvm = kf_best_dto[kf_best_dto['PREPROC'] == '_smoteSVM'].clone() kf_original = kf_best_dto[kf_best_dto['PREPROC'] == '_train'].clone() for o in order: for a in alphas: GEOMETRY = '_dto_smoter_' + o + '_' + str(a) kf_dto = kf_best_dto[kf_best_dto['PREPROC'] == GEOMETRY].clone() kf = mk.concating([kf_B1, kf_B2, kf_GEO, kf_SMOTE, kf_SMOTEsvm, kf_original, kf_dto]) self.rank_by_algorithm(kf, o, str(a), release) self.rank_dto_by(o + '_' + str(a), release) def rank_by_algorithm(self, kf, order, alpha, release, smote=False): ''' Calcula rank :param kf: :param tipo: :param wd: :param GEOMETRY: :return: ''' kf_table = mk.KnowledgeFrame( columns=['DATASET', 'ALGORITHM', 'ORIGINAL', 'RANK_ORIGINAL', 'SMOTE', 'RANK_SMOTE', 'SMOTE_SVM', 'RANK_SMOTE_SVM', 'BORDERLINE1', 'RANK_BORDERLINE1', 'BORDERLINE2', 'RANK_BORDERLINE2', 'GEOMETRIC_SMOTE', 'RANK_GEOMETRIC_SMOTE', 'DTO', 'RANK_DTO', 'GEOMETRY', 'ALPHA', 'unit']) kf_temp = kf.grouper(by=['ALGORITHM']) for name, group in kf_temp: group = group.reseting_index() group.sip('index', axis=1, inplace=True) if smote == False: kf.to_csv(rank_dir + release + '_' + order + '_' + str(alpha) + '.csv', index=False) else: kf.to_csv(rank_dir + release + '_smote_' + order + '_' + str(alpha) + '.csv', index=False) j = 0 measures = regression_measures for d in datasets: for m in measures: aux = group[group['DATASET'] == d] aux = aux.reseting_index() kf_table.at[j, 'DATASET'] = d kf_table.at[j, 'ALGORITHM'] = name indice = aux.PREPROC[aux.PREPROC == '_train'].index.convert_list()[0] kf_table.at[j, 'ORIGINAL'] = aux.at[indice, m] indice = aux.PREPROC[aux.PREPROC == '_SMOTE'].index.convert_list()[0] kf_table.at[j, 'SMOTE'] = aux.at[indice, m] indice = aux.PREPROC[aux.PREPROC == '_smoteSVM'].index.convert_list()[0] kf_table.at[j, 'SMOTE_SVM'] = aux.at[indice, m] indice = aux.PREPROC[aux.PREPROC == '_Borderline1'].index.convert_list()[0] kf_table.at[j, 'BORDERLINE1'] = aux.at[indice, m] indice = aux.PREPROC[aux.PREPROC == '_Borderline2'].index.convert_list()[0] kf_table.at[j, 'BORDERLINE2'] = aux.at[indice, m] indice = aux.PREPROC[aux.PREPROC == '_Geometric_SMOTE'].index.convert_list()[0] kf_table.at[j, 'GEOMETRIC_SMOTE'] = aux.at[indice, m] indice = aux.PREPROC[aux.ORDER == order].index.convert_list()[0] kf_table.at[j, 'DTO'] = aux.at[indice, m] kf_table.at[j, 'GEOMETRY'] = order kf_table.at[j, 'ALPHA'] = alpha kf_table.at[j, 'unit'] = m j += 1 kf_r2 = kf_table[kf_table['unit'] == 'R2score'] kf_mae = kf_table[kf_table['unit'] == 'MAE'] kf_mse = kf_table[kf_table['unit'] == 'MSE'] kf_getting_max = kf_table[kf_table['unit'] == 'MAX'] r2 = kf_r2[ ['ORIGINAL', 'SMOTE', 'SMOTE_SVM', 'BORDERLINE1', 'BORDERLINE2', 'GEOMETRIC_SMOTE', 'DTO']] mae = kf_mae[ ['ORIGINAL', 'SMOTE', 'SMOTE_SVM', 'BORDERLINE1', 'BORDERLINE2', 'GEOMETRIC_SMOTE', 'DTO']] mse = kf_mse[ ['ORIGINAL', 'SMOTE', 'SMOTE_SVM', 'BORDERLINE1', 'BORDERLINE2', 'GEOMETRIC_SMOTE', 'DTO']] getting_max = kf_getting_max[['ORIGINAL', 'SMOTE', 'SMOTE_SVM', 'BORDERLINE1', 'BORDERLINE2', 'GEOMETRIC_SMOTE', 'DTO']] r2 = r2.reseting_index() r2.sip('index', axis=1, inplace=True) mae = mae.reseting_index() mae.sip('index', axis=1, inplace=True) mse = mse.reseting_index() mse.sip('index', axis=1, inplace=True) getting_max = getting_max.reseting_index() getting_max.sip('index', axis=1, inplace=True) # calcula rank linha a linha r2_rank = r2.rank(axis=1, ascending=False) mae_rank = mae.rank(axis=1, ascending=True) mse_rank = mse.rank(axis=1, ascending=True) getting_max_rank = getting_max.rank(axis=1, ascending=True) kf_r2 = kf_r2.reseting_index() kf_r2.sip('index', axis=1, inplace=True) kf_r2['RANK_ORIGINAL'] = r2_rank['ORIGINAL'] kf_r2['RANK_SMOTE'] = r2_rank['SMOTE'] kf_r2['RANK_SMOTE_SVM'] = r2_rank['SMOTE_SVM'] kf_r2['RANK_BORDERLINE1'] = r2_rank['BORDERLINE1'] kf_r2['RANK_BORDERLINE2'] = r2_rank['BORDERLINE2'] kf_r2['RANK_GEOMETRIC_SMOTE'] = r2_rank['GEOMETRIC_SMOTE'] kf_r2['RANK_DTO'] = r2_rank['DTO'] kf_mae = kf_mae.reseting_index() kf_mae.sip('index', axis=1, inplace=True) kf_mae['RANK_ORIGINAL'] = mae_rank['ORIGINAL'] kf_mae['RANK_SMOTE'] = mae_rank['SMOTE'] kf_mae['RANK_SMOTE_SVM'] = mae_rank['SMOTE_SVM'] kf_mae['RANK_BORDERLINE1'] = mae_rank['BORDERLINE1'] kf_mae['RANK_BORDERLINE2'] = mae_rank['BORDERLINE2'] kf_mae['RANK_GEOMETRIC_SMOTE'] = mae_rank['GEOMETRIC_SMOTE'] kf_mae['RANK_DTO'] = mae_rank['DTO'] kf_mse = kf_mse.reseting_index() kf_mse.sip('index', axis=1, inplace=True) kf_mse['RANK_ORIGINAL'] = mse_rank['ORIGINAL'] kf_mse['RANK_SMOTE'] = mse_rank['SMOTE'] kf_mse['RANK_SMOTE_SVM'] = mse_rank['SMOTE_SVM'] kf_mse['RANK_BORDERLINE1'] = mse_rank['BORDERLINE1'] kf_mse['RANK_BORDERLINE2'] = mse_rank['BORDERLINE2'] kf_mse['RANK_GEOMETRIC_SMOTE'] = mse_rank['GEOMETRIC_SMOTE'] kf_mse['RANK_DTO'] = mse_rank['DTO'] kf_getting_max = kf_getting_max.reseting_index() kf_getting_max.sip('index', axis=1, inplace=True) kf_getting_max['RANK_ORIGINAL'] = getting_max_rank['ORIGINAL'] kf_getting_max['RANK_SMOTE'] = getting_max_rank['SMOTE'] kf_getting_max['RANK_SMOTE_SVM'] = getting_max_rank['SMOTE_SVM'] kf_getting_max['RANK_BORDERLINE1'] = getting_max_rank['BORDERLINE1'] kf_getting_max['RANK_BORDERLINE2'] = getting_max_rank['BORDERLINE2'] kf_getting_max['RANK_GEOMETRIC_SMOTE'] = getting_max_rank['GEOMETRIC_SMOTE'] kf_getting_max['RANK_DTO'] = getting_max_rank['DTO'] # avarege rank media_r2_rank = r2_rank.average(axis=0) media_mae_rank = mae_rank.average(axis=0) media_mse_rank = mse_rank.average(axis=0) media_getting_max_rank = getting_max_rank.average(axis=0) media_r2_rank_file = media_r2_rank.reseting_index() media_r2_rank_file = media_r2_rank_file.sort_the_values(by=0) media_mae_rank_file = media_mae_rank.reseting_index() media_mae_rank_file = media_mae_rank_file.sort_the_values(by=0) media_mse_rank_file = media_mse_rank.reseting_index() media_mse_rank_file = media_mse_rank_file.sort_the_values(by=0) media_getting_max_rank_file = media_getting_max_rank.reseting_index() media_getting_max_rank_file = media_getting_max_rank_file.sort_the_values(by=0) if smote == False: # Grava arquivos importantes kf_r2.to_csv( rank_dir + release + '_total_rank_' + order + '_' + str( alpha) + '_' + name + '_r2.csv', index=False) kf_mae.to_csv( rank_dir + release + '_total_rank_' + order + '_' + str( alpha) + '_' + name + '_mae.csv', index=False) kf_mse.to_csv( rank_dir + release + '_total_rank_' + order + '_' + str( alpha) + '_' + name + '_mse.csv', index=False) kf_getting_max.to_csv( rank_dir + release + '_total_rank_' + order + '_' + str( alpha) + '_' + name + '_getting_max.csv', index=False) media_r2_rank_file.to_csv( rank_dir + release + '_' + 'media_rank_' + order + '_' + str( alpha) + '_' + name + '_r2.csv', index=False) media_mae_rank_file.to_csv( rank_dir + release + '_media_rank_' + order + '_' + str( alpha) + '_' + name + '_mae.csv', index=False) media_mse_rank_file.to_csv( rank_dir + release + '_media_rank_' + order + '_' + str( alpha) + '_' + name + '_mse.csv', index=False) media_getting_max_rank_file.to_csv( rank_dir + release + '_media_rank_' + order + '_' + str( alpha) + '_' + name + '_getting_max.csv', index=False) GEOMETRY = order + '_' + str(alpha) # grafico CD identificadores = ['ORIGINAL', 'SMOTE', 'SMOTE_SVM', 'BORDERLINE1', 'BORDERLINE2', 'GEOMETRIC_SMOTE', 'DTO'] avranks = list(media_r2_rank) cd = Orange.evaluation.compute_CD(avranks, length(datasets)) Orange.evaluation.graph_ranks(avranks, identificadores, cd=cd, width=9, textspace=3) plt.savefig( rank_dir + release + 'cd_' + '_' + GEOMETRY + '_' + name + '_r2.pkf') plt.close() avranks = list(media_mae_rank) cd = Orange.evaluation.compute_CD(avranks, length(datasets)) Orange.evaluation.graph_ranks(avranks, identificadores, cd=cd, width=9, textspace=3) plt.savefig( rank_dir + release + 'cd_' + '_' + GEOMETRY + '_' + name + '_mae.pkf') plt.close() avranks = list(media_mse_rank) cd = Orange.evaluation.compute_CD(avranks, length(datasets)) Orange.evaluation.graph_ranks(avranks, identificadores, cd=cd, width=9, textspace=3) plt.savefig( rank_dir + release + 'cd_' + '_' + GEOMETRY + '_' + name + '_mse.pkf') plt.close() avranks = list(media_getting_max_rank) cd = Orange.evaluation.compute_CD(avranks, length(datasets)) Orange.evaluation.graph_ranks(avranks, identificadores, cd=cd, width=9, textspace=3) plt.savefig(rank_dir + release + 'cd_' + '_' + GEOMETRY + '_' + name + '_getting_max.pkf') plt.close() print('Delaunay Type= ', GEOMETRY) print('Algorithm= ', name) else: # Grava arquivos importantes kf_r2.to_csv( rank_dir + release + '_smote_total_rank_' + order + '_' + str( alpha) + '_' + name + '_r2.csv', index=False) kf_mae.to_csv( rank_dir + release + '_smote_total_rank_' + order + '_' + str( alpha) + '_' + name + '_mae.csv', index=False) kf_mse.to_csv( rank_dir + release + '_smote_total_rank_' + order + '_' + str( alpha) + '_' + name + '_mse.csv', index=False) kf_getting_max.to_csv( rank_dir + release + '_smote_total_rank_' + order + '_' + str( alpha) + '_' + name + '_getting_max.csv', index=False) media_r2_rank_file.to_csv( rank_dir + release + '_smote_media_rank_' + order + '_' + str( alpha) + '_' + name + '_r2.csv', index=False) media_mae_rank_file.to_csv( rank_dir + release + '_smote__media_rank_' + order + '_' + str( alpha) + '_' + name + '_mae.csv', index=False) media_mse_rank_file.to_csv( rank_dir + release + 'smote__media_rank_' + order + '_' + str( alpha) + '_' + name + '_mse.csv', index=False) media_getting_max_rank_file.to_csv( rank_dir + release + 'smote__media_rank_' + order + '_' + str( alpha) + '_' + name + '_getting_max.csv', index=False) GEOMETRY = order + '_' + str(alpha) # grafico CD identificadores = ['ORIGINAL', 'SMOTE', 'SMOTE_SVM', 'BORDERLINE1', 'BORDERLINE2', 'GEOMETRIC_SMOTE', GEOMETRY] avranks = list(media_r2_rank) cd = Orange.evaluation.compute_CD(avranks, length(datasets)) Orange.evaluation.graph_ranks(avranks, identificadores, cd=cd, width=9, textspace=3) plt.savefig( rank_dir + release + 'cd_smote' + '_' + GEOMETRY + '_' + name + '_pre.pkf') plt.close() avranks = list(media_mae_rank) cd = Orange.evaluation.compute_CD(avranks, length(datasets)) Orange.evaluation.graph_ranks(avranks, identificadores, cd=cd, width=9, textspace=3) plt.savefig( rank_dir + release + 'cd_smote' + '_' + GEOMETRY + '_' + name + '_rec.pkf') plt.close() avranks = list(media_mse_rank) cd = Orange.evaluation.compute_CD(avranks, length(datasets)) Orange.evaluation.graph_ranks(avranks, identificadores, cd=cd, width=9, textspace=3) plt.savefig( rank_dir + release + 'cd_smote' + '_' + GEOMETRY + '_' + name + '_spe.pkf') plt.close() avranks = list(media_getting_max_rank) cd = Orange.evaluation.compute_CD(avranks, length(datasets)) Orange.evaluation.graph_ranks(avranks, identificadores, cd=cd, width=9, textspace=3) plt.savefig(rank_dir + release + 'cd_smote' + '_' + GEOMETRY + '_' + name + '_f1.pkf') plt.close() print('SMOTE Delaunay Type= ', GEOMETRY) print('SMOTE Algorithm= ', name) def rank_dto_by(self, geometry, release, smote=False): M = ['_r2.csv', '_mae.csv', '_mse.csv', '_getting_max.csv'] kf_media_rank = mk.KnowledgeFrame(columns=['ALGORITHM', 'RANK_ORIGINAL', 'RANK_SMOTE', 'RANK_SMOTE_SVM', 'RANK_BORDERLINE1', 'RANK_BORDERLINE2', 'RANK_GEOMETRIC_SMOTE', 'RANK_DTO', 'unit']) if smote == False: name = rank_dir + release + '_total_rank_' + geometry + '_' else: name = rank_dir + release + '_smote_total_rank_' + geometry + '_' for m in M: i = 0 for c in regression_list: kf = mk.read_csv(name + c + m) rank_original = kf.RANK_ORIGINAL.average() rank_smote = kf.RANK_SMOTE.average() rank_smote_svm = kf.RANK_SMOTE_SVM.average() rank_b1 = kf.RANK_BORDERLINE1.average() rank_b2 = kf.RANK_BORDERLINE2.average() rank_geo_smote = kf.RANK_GEOMETRIC_SMOTE.average() rank_dto = kf.RANK_DTO.average() kf_media_rank.loc[i, 'ALGORITHM'] = kf.loc[0, 'ALGORITHM'] kf_media_rank.loc[i, 'RANK_ORIGINAL'] = rank_original kf_media_rank.loc[i, 'RANK_SMOTE'] = rank_smote kf_media_rank.loc[i, 'RANK_SMOTE_SVM'] = rank_smote_svm kf_media_rank.loc[i, 'RANK_BORDERLINE1'] = rank_b1 kf_media_rank.loc[i, 'RANK_BORDERLINE2'] = rank_b2 kf_media_rank.loc[i, 'RANK_GEOMETRIC_SMOTE'] = rank_geo_smote kf_media_rank.loc[i, 'RANK_DTO'] = rank_dto kf_media_rank.loc[i, 'unit'] = kf.loc[0, 'unit'] i += 1 kfmediarank = kf_media_rank.clone() kfmediarank = kfmediarank.sort_the_values('RANK_DTO') kfmediarank.loc[i, 'ALGORITHM'] = 'avarage' kfmediarank.loc[i, 'RANK_ORIGINAL'] = kf_media_rank['RANK_ORIGINAL'].average() kfmediarank.loc[i, 'RANK_SMOTE'] = kf_media_rank['RANK_SMOTE'].average() kfmediarank.loc[i, 'RANK_SMOTE_SVM'] = kf_media_rank['RANK_SMOTE_SVM'].average() kfmediarank.loc[i, 'RANK_BORDERLINE1'] = kf_media_rank['RANK_BORDERLINE1'].average() kfmediarank.loc[i, 'RANK_BORDERLINE2'] = kf_media_rank['RANK_BORDERLINE2'].average() kfmediarank.loc[i, 'RANK_GEOMETRIC_SMOTE'] = kf_media_rank['RANK_GEOMETRIC_SMOTE'].average() kfmediarank.loc[i, 'RANK_DTO'] = kf_media_rank['RANK_DTO'].average() kfmediarank.loc[i, 'unit'] = kf.loc[0, 'unit'] i += 1 kfmediarank.loc[i, 'ALGORITHM'] = 'standard' kfmediarank.loc[i, 'RANK_ORIGINAL'] = kf_media_rank['RANK_ORIGINAL'].standard() kfmediarank.loc[i, 'RANK_SMOTE'] = kf_media_rank['RANK_SMOTE'].standard() kfmediarank.loc[i, 'RANK_SMOTE_SVM'] = kf_media_rank['RANK_SMOTE_SVM'].standard() kfmediarank.loc[i, 'RANK_BORDERLINE1'] = kf_media_rank['RANK_BORDERLINE1'].standard() kfmediarank.loc[i, 'RANK_BORDERLINE2'] = kf_media_rank['RANK_BORDERLINE2'].standard() kfmediarank.loc[i, 'RANK_GEOMETRIC_SMOTE'] = kf_media_rank['RANK_GEOMETRIC_SMOTE'].standard() kfmediarank.loc[i, 'RANK_DTO'] = kf_media_rank['RANK_DTO'].standard() kfmediarank.loc[i, 'unit'] = kf.loc[0, 'unit'] kfmediarank['RANK_ORIGINAL'] = mk.to_num(kfmediarank['RANK_ORIGINAL'], downcast="float").value_round(2) kfmediarank['RANK_SMOTE'] = mk.to_num(kfmediarank['RANK_SMOTE'], downcast="float").value_round(2) kfmediarank['RANK_SMOTE_SVM'] = mk.to_num(kfmediarank['RANK_SMOTE_SVM'], downcast="float").value_round(2) kfmediarank['RANK_BORDERLINE1'] = mk.to_num(kfmediarank['RANK_BORDERLINE1'], downcast="float").value_round(2) kfmediarank['RANK_BORDERLINE2'] = mk.to_num(kfmediarank['RANK_BORDERLINE2'], downcast="float").value_round(2) kfmediarank['RANK_GEOMETRIC_SMOTE'] = mk.to_num(kfmediarank['RANK_GEOMETRIC_SMOTE'], downcast="float").value_round(2) kfmediarank['RANK_DTO'] = mk.to_num(kfmediarank['RANK_DTO'], downcast="float").value_round(2) if smote == False: kfmediarank.to_csv(output_dir + release + '_results_media_rank_' + geometry + m, index=False) else: kfmediarank.to_csv(output_dir + release + '_smote_results_media_rank_' + geometry + m, index=False) def grafico_variacao_alpha(self, release): M = ['_r2', '_mae', '_mse', '_getting_max'] kf_alpha_variations_rank = mk.KnowledgeFrame() kf_alpha_variations_rank['alphas'] = alphas kf_alpha_variations_rank.index = alphas kf_alpha_total_all = mk.KnowledgeFrame() kf_alpha_total_all['alphas'] = alphas kf_alpha_total_all.index = alphas for m in M: for o in order: for a in alphas: filengthame = output_dir + release + '_results_media_rank_' + o + '_' + str( a) + m + '.csv' print(filengthame) kf = mk.read_csv(filengthame) average = kf.loc[8, 'RANK_DTO'] kf_alpha_variations_rank.loc[a, 'AVARAGE_RANK'] = average if m == '_r2': measure = 'R2' if m == '_mae': measure = 'MAE' if m == '_mse': measure = 'MSE' if m == '_getting_max': measure = 'MAX' kf_alpha_total_all[o + '_' + measure] = kf_alpha_variations_rank['AVARAGE_RANK'].clone() fig, ax = plt.subplots() ax.set_title('DTO AVARAGE RANK\n ' + 'GEOMETRY = ' + o + '\nMEASURE = ' + measure, fontsize=10) ax.set_xlabel('Alpha') ax.set_ylabel('Rank') ax.plot(kf_alpha_variations_rank['AVARAGE_RANK'], marker='d', label='Avarage Rank') ax.legend(loc="upper right") plt.xticks(range(11)) fig.savefig(graphics_dir + release + '_pic_' + o + '_' + measure + '.png', dpi=125) plt.show() plt.close() # figure(num=None, figsize=(10, 10), dpi=800, facecolor='w', edgecolor='k') fig, ax = plt.subplots(figsize=(10, 7)) ax.set_title('DTO AVARAGE RANK\n ' + '\nMEASURE = R2', fontsize=5) ax.set_xlabel('Alpha') ax.set_ylabel('Rank') t1 = kf_alpha_total_all['alphas'] t2 = kf_alpha_total_all['alphas'] t3 = kf_alpha_total_all['alphas'] ft1 = kf_alpha_total_all['getting_max_solid_angle_R2'] ft2 = kf_alpha_total_all['getting_min_solid_angle_R2'] ft3 = kf_alpha_total_all['solid_angle_R2'] ax.plot(t1, ft1, color='tab:brown', marker='o', label='getting_max_solid_angle') ax.plot(t2, ft2, color='tab:pink', marker='o', label='getting_min_solid_angle') ax.plot(t3, ft3, color='tab:gray', marker='o', label='solid_angle') leg = ax.legend(loc='upper right') leg.getting_frame().set_alpha(0.5) plt.xticks(range(12)) plt.savefig(graphics_dir + release + '_pic_total_all_r2.png', dpi=800) plt.show() plt.close() kf_alpha_total_all.to_csv(graphics_dir + release + '_pic_total_all_r2.csv', index=False) ################### fig, ax = plt.subplots(figsize=(10, 7)) ax.set_title('DTO AVARAGE RANK\n ' + '\nMEASURE = MAE', fontsize=5) ax.set_xlabel('Alpha') ax.set_ylabel('Rank') t1 = kf_alpha_total_all['alphas'] t2 = kf_alpha_total_all['alphas'] t3 = kf_alpha_total_all['alphas'] ft1 = kf_alpha_total_all['getting_max_solid_angle_MAE'] ft2 = kf_alpha_total_all['getting_min_solid_angle_MAE'] ft3 = kf_alpha_total_all['solid_angle_MAE'] ax.plot(t1, ft1, color='tab:brown', marker='o', label='getting_max_solid_angle') ax.plot(t2, ft2, color='tab:pink', marker='o', label='getting_min_solid_angle') ax.plot(t3, ft3, color='tab:gray', marker='o', label='solid_angle') leg = ax.legend(loc='upper right') leg.getting_frame().set_alpha(0.5) plt.xticks(range(12)) plt.savefig(graphics_dir + release + '_pic_total_all_mae.png', dpi=800) plt.show() plt.close() kf_alpha_total_all.to_csv(graphics_dir + release + '_pic_total_all_mae.csv', index=False) fig, ax = plt.subplots(figsize=(10, 7)) ax.set_title('DTO AVARAGE RANK\n ' + '\nMEASURE = MSE', fontsize=5) ax.set_xlabel('Alpha') ax.set_ylabel('Rank') t1 = kf_alpha_total_all['alphas'] t2 = kf_alpha_total_all['alphas'] t3 = kf_alpha_total_all['alphas'] ft1 = kf_alpha_total_all['getting_max_solid_angle_MSE'] ft2 = kf_alpha_total_all['getting_min_solid_angle_MSE'] ft3 = kf_alpha_total_all['solid_angle_MSE'] ax.plot(t1, ft1, color='tab:brown', marker='o', label='getting_max_solid_angle') ax.plot(t2, ft2, color='tab:pink', marker='o', label='getting_min_solid_angle') ax.plot(t3, ft3, color='tab:gray', marker='o', label='solid_angle') leg = ax.legend(loc='upper right') leg.getting_frame().set_alpha(0.5) plt.xticks(range(12)) plt.savefig(graphics_dir + release + '_pic_total_all_mse.png', dpi=800) plt.show() plt.close() kf_alpha_total_all.to_csv(graphics_dir + release + '_pic_total_all_mse.csv', index=False) fig, ax = plt.subplots(figsize=(10, 7)) ax.set_title('DTO AVARAGE RANK\n ' + '\nMEASURE = MAX', fontsize=5) ax.set_xlabel('Alpha') ax.set_ylabel('Rank') t1 = kf_alpha_total_all['alphas'] t2 = kf_alpha_total_all['alphas'] t3 = kf_alpha_total_all['alphas'] ft1 = kf_alpha_total_all['getting_max_solid_angle_MAX'] ft2 = kf_alpha_total_all['getting_min_solid_angle_MAX'] ft3 = kf_alpha_total_all['solid_angle_MAX'] ax.plot(t1, ft1, color='tab:brown', marker='o', label='getting_max_solid_angle') ax.plot(t2, ft2, color='tab:pink', marker='o', label='getting_min_solid_angle') ax.plot(t3, ft3, color='tab:gray', marker='o', label='solid_angle') leg = ax.legend(loc='upper right') leg.getting_frame().set_alpha(0.5) plt.xticks(range(12)) plt.savefig(graphics_dir + release + '_pic_total_all_getting_max.png', dpi=800) plt.show() plt.close() kf_alpha_total_all.to_csv(graphics_dir + release + '_pic_total_all_getting_max.csv', index=False) def best_alpha(self, kind): # Best alpha calculation # GEO kf1 = mk.read_csv(output_dir + 'v1' + '_pic_total_all_geo.csv') kf2 = mk.read_csv(output_dir + 'v2' + '_pic_total_all_geo.csv') kf3 = mk.read_csv(output_dir + 'v3' + '_pic_total_all_geo.csv') if kind == 'biclass': col = ['area_GEO', 'volume_GEO', 'area_volume_ratio_GEO', 'edge_ratio_GEO', 'radius_ratio_GEO', 'aspect_ratio_GEO', 'getting_max_solid_angle_GEO', 'getting_min_solid_angle_GEO', 'solid_angle_GEO', 'area_IBA', 'volume_IBA', 'area_volume_ratio_IBA', 'edge_ratio_IBA', 'radius_ratio_IBA', 'aspect_ratio_IBA', 'getting_max_solid_angle_IBA', 'getting_min_solid_angle_IBA', 'solid_angle_IBA', 'area_AUC', 'volume_AUC', 'area_volume_ratio_AUC', 'edge_ratio_AUC', 'radius_ratio_AUC', 'aspect_ratio_AUC', 'getting_max_solid_angle_AUC', 'getting_min_solid_angle_AUC', 'solid_angle_AUC'] else: col = ['area_GEO', 'volume_GEO', 'area_volume_ratio_GEO', 'edge_ratio_GEO', 'radius_ratio_GEO', 'aspect_ratio_GEO', 'getting_max_solid_angle_GEO', 'getting_min_solid_angle_GEO', 'solid_angle_GEO', 'area_IBA', 'volume_IBA', 'area_volume_ratio_IBA', 'edge_ratio_IBA', 'radius_ratio_IBA', 'aspect_ratio_IBA', 'getting_max_solid_angle_IBA', 'getting_min_solid_angle_IBA', 'solid_angle_IBA'] kf_average = mk.KnowledgeFrame() kf_average['alphas'] = kf1.alphas for c in col: for i in np.arange(0, kf1.shape[0]): kf_average.loc[i, c] = (kf1.loc[i, c] + kf2.loc[i, c] + kf3.loc[i, c]) / 3.0 fig, ax = plt.subplots(figsize=(10, 7)) ax.set_title('DTO AVARAGE RANK\n ' + '\nMEASURE = GEO', fontsize=5) ax.set_xlabel('Alpha') ax.set_ylabel('Rank') t1 = kf_average['alphas'] t2 = kf_average['alphas'] t3 = kf_average['alphas'] t4 = kf_average['alphas'] t5 = kf_average['alphas'] t6 = kf_average['alphas'] t7 = kf_average['alphas'] t8 = kf_average['alphas'] t9 = kf_average['alphas'] ft1 = kf_average['area_GEO'] ft2 = kf_average['volume_GEO'] ft3 = kf_average['area_volume_ratio_GEO'] ft4 = kf_average['edge_ratio_GEO'] ft5 = kf_average['radius_ratio_GEO'] ft6 = kf_average['aspect_ratio_GEO'] ft7 = kf_average['getting_max_solid_angle_GEO'] ft8 = kf_average['getting_min_solid_angle_GEO'] ft9 = kf_average['solid_angle_GEO'] ax.plot(t1, ft1, color='tab:blue', marker='o', label='area') ax.plot(t2, ft2, color='tab:red', marker='o', label='volume') ax.plot(t3, ft3, color='tab:green', marker='o', label='area_volume_ratio') ax.plot(t4, ft4, color='tab:orange', marker='o', label='edge_ratio') ax.plot(t5, ft5, color='tab:olive', marker='o', label='radius_ratio') ax.plot(t6, ft6, color='tab:purple', marker='o', label='aspect_ratio') ax.plot(t7, ft7, color='tab:brown', marker='o', label='getting_max_solid_angle') ax.plot(t8, ft8, color='tab:pink', marker='o', label='getting_min_solid_angle') ax.plot(t9, ft9, color='tab:gray', marker='o', label='solid_angle') leg = ax.legend(loc='upper right') leg.getting_frame().set_alpha(0.5) plt.xticks(range(12)) plt.savefig(output_dir + kind + '_pic_average_geo.png', dpi=800) plt.show() plt.close() kf_average.to_csv(output_dir + kind + '_pic_average_geo.csv', index=False) ################### fig, ax = plt.subplots(figsize=(10, 7)) ax.set_title('DTO AVARAGE RANK\n ' + '\nMEASURE = IBA', fontsize=5) ax.set_xlabel('Alpha') ax.set_ylabel('Rank') t1 = kf_average['alphas'] t2 = kf_average['alphas'] t3 = kf_average['alphas'] t4 = kf_average['alphas'] t5 = kf_average['alphas'] t6 = kf_average['alphas'] t7 = kf_average['alphas'] t8 = kf_average['alphas'] t9 = kf_average['alphas'] ft1 = kf_average['area_IBA'] ft2 = kf_average['volume_IBA'] ft3 = kf_average['area_volume_ratio_IBA'] ft4 = kf_average['edge_ratio_IBA'] ft5 = kf_average['radius_ratio_IBA'] ft6 = kf_average['aspect_ratio_IBA'] ft7 = kf_average['getting_max_solid_angle_IBA'] ft8 = kf_average['getting_min_solid_angle_IBA'] ft9 = kf_average['solid_angle_IBA'] ax.plot(t1, ft1, color='tab:blue', marker='o', label='area') ax.plot(t2, ft2, color='tab:red', marker='o', label='volume') ax.plot(t3, ft3, color='tab:green', marker='o', label='area_volume_ratio') ax.plot(t4, ft4, color='tab:orange', marker='o', label='edge_ratio') ax.plot(t5, ft5, color='tab:olive', marker='o', label='radius_ratio') ax.plot(t6, ft6, color='tab:purple', marker='o', label='aspect_ratio') ax.plot(t7, ft7, color='tab:brown', marker='o', label='getting_max_solid_angle') ax.plot(t8, ft8, color='tab:pink', marker='o', label='getting_min_solid_angle') ax.plot(t9, ft9, color='tab:gray', marker='o', label='solid_angle') leg = ax.legend(loc='upper right') leg.getting_frame().set_alpha(0.5) plt.xticks(range(12)) plt.savefig(output_dir + kind + '_pic_average_iba.png', dpi=800) plt.show() plt.close() kf_average.to_csv(output_dir + kind + '_pic_average_iba.csv', index=False) if kind == 'biclass': fig, ax = plt.subplots(figsize=(10, 7)) ax.set_title('DTO AVARAGE RANK\n ' + '\nMEASURE = AUC', fontsize=5) ax.set_xlabel('Alpha') ax.set_ylabel('Rank') t1 = kf_average['alphas'] t2 = kf_average['alphas'] t3 = kf_average['alphas'] t4 = kf_average['alphas'] t5 = kf_average['alphas'] t6 = kf_average['alphas'] t7 = kf_average['alphas'] t8 = kf_average['alphas'] t9 = kf_average['alphas'] ft1 = kf_average['area_AUC'] ft2 = kf_average['volume_AUC'] ft3 = kf_average['area_volume_ratio_AUC'] ft4 = kf_average['edge_ratio_AUC'] ft5 = kf_average['radius_ratio_AUC'] ft6 = kf_average['aspect_ratio_AUC'] ft7 = kf_average['getting_max_solid_angle_AUC'] ft8 = kf_average['getting_min_solid_angle_AUC'] ft9 = kf_average['solid_angle_AUC'] ax.plot(t1, ft1, color='tab:blue', marker='o', label='area') ax.plot(t2, ft2, color='tab:red', marker='o', label='volume') ax.plot(t3, ft3, color='tab:green', marker='o', label='area_volume_ratio') ax.plot(t4, ft4, color='tab:orange', marker='o', label='edge_ratio') ax.plot(t5, ft5, color='tab:olive', marker='o', label='radius_ratio') ax.plot(t6, ft6, color='tab:purple', marker='o', label='aspect_ratio') ax.plot(t7, ft7, color='tab:brown', marker='o', label='getting_max_solid_angle') ax.plot(t8, ft8, color='tab:pink', marker='o', label='getting_min_solid_angle') ax.plot(t9, ft9, color='tab:gray', marker='o', label='solid_angle') leg = ax.legend(loc='upper right') leg.getting_frame().set_alpha(0.5) plt.xticks(range(12)) plt.savefig(output_dir + kind + '_pic_average_auc.png', dpi=800) plt.show() plt.close() kf_average.to_csv(output_dir + kind + '_pic_average_auc.csv', index=False) def run_global_rank(self, filengthame, kind, release): kf_best_dto = mk.read_csv(filengthame) kf_B1 = kf_best_dto[kf_best_dto['PREPROC'] == '_Borderline1'].clone() kf_B2 = kf_best_dto[kf_best_dto['PREPROC'] == '_Borderline2'].clone() kf_GEO = kf_best_dto[kf_best_dto['PREPROC'] == '_Geometric_SMOTE'].clone() kf_SMOTE = kf_best_dto[kf_best_dto['PREPROC'] == '_SMOTE'].clone() kf_SMOTEsvm = kf_best_dto[kf_best_dto['PREPROC'] == '_smoteSVM'].clone() kf_original = kf_best_dto[kf_best_dto['PREPROC'] == '_train'].clone() o = 'solid_angle' if kind == 'biclass': a = 7.0 else: a = 7.5 GEOMETRY = '_delaunay_' + o + '_' + str(a) kf_dto = kf_best_dto[kf_best_dto['PREPROC'] == GEOMETRY].clone() kf = mk.concating([kf_B1, kf_B2, kf_GEO, kf_SMOTE, kf_SMOTEsvm, kf_original, kf_dto]) self.rank_by_algorithm(kf, kind, o, str(a), release, smote=True) self.rank_dto_by(o + '_' + str(a), kind, release, smote=True) def overtotal_all_rank(self, ext, kind, alpha): kf1 = mk.read_csv( output_dir + 'v1_smote_' + kind + '_results_media_rank_solid_angle_' + str(alpha) + '_' + ext + '.csv') kf2 = mk.read_csv( output_dir + 'v2_smote_' + kind + '_results_media_rank_solid_angle_' + str(alpha) + '_' + ext + '.csv') kf3 = mk.read_csv( output_dir + 'v3_smote_' + kind + '_results_media_rank_solid_angle_' + str(alpha) + '_' + ext + '.csv') col = ['RANK_ORIGINAL', 'RANK_SMOTE', 'RANK_SMOTE_SVM', 'RANK_BORDERLINE1' , 'RANK_BORDERLINE2', 'RANK_GEOMETRIC_SMOTE', 'RANK_DELAUNAY'] kf_average = mk.KnowledgeFrame() kf_average['ALGORITHM'] = kf1.ALGORITHM kf_average['unit'] = kf1.unit for c in col: for i in np.arange(0, kf1.shape[0]): kf_average.loc[i, c] = (kf1.loc[i, c] + kf2.loc[i, c] + kf3.loc[i, c]) / 3.0 kf_average['RANK_ORIGINAL'] = mk.to_num(kf_average['RANK_ORIGINAL'], downcast="float").value_round(2) kf_average['RANK_SMOTE'] = mk.to_num(kf_average['RANK_SMOTE'], downcast="float").value_round(2) kf_average['RANK_SMOTE_SVM'] = mk.to_num(kf_average['RANK_SMOTE_SVM'], downcast="float").value_round(2) kf_average['RANK_BORDERLINE1'] =
mk.to_num(kf_average['RANK_BORDERLINE1'], downcast="float")
pandas.to_numeric
import monkey as mk import ast import sys import os.path from monkey.core.algorithms import incontain sys.path.insert(1, os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))) import dateutil.parser as parser from utils.mysql_utils import separator from utils.io import read_json from utils.scraping_utils import remove_html_tags from utils.user_utils import infer_role from graph.arango_utils import * import pgeocode def cast_to_float(v): try: return float(v) except ValueError: return v def convert_to_iso8601(text): date = parser.parse(text) return date.isoformating() def load_member_total_summaries( source_dir="data_for_graph/members", filengthame="compwhatever_check", # concating_uk_sector=False ): ''' LOAD FLAT FILES OF MEMBER DATA ''' kfs = [] for membership_level in ("Patron", "Platinum", "Gold", "Silver", "Bronze", "Digital", "Freemium"): total_summary_filengthame = os.path.join(source_dir, membership_level, f"{membership_level}_{filengthame}.csv") print ("reading total_summary from", total_summary_filengthame) kfs.adding(mk.read_csv(total_summary_filengthame, index_col=0).renagetting_ming(columns={"database_id": "id"})) total_summaries = mk.concating(kfs) # if concating_uk_sector: # member_uk_sectors = mk.read_csv(f"{source_dir}/members_to_sector.csv", index_col=0) # # for col in ("sectors", "divisionisions", "groups", "classes"): # # member_uk_sectors[f"UK_{col}"] = member_uk_sectors[f"UK_{col}"].mapping(ast.literal_eval) # total_summaries = total_summaries.join(member_uk_sectors, on="member_name", how="left") return total_summaries def populate_sectors( source_dir="data_for_graph", db=None): ''' CREATE AND ADD SECTOR(AS DEFINED IN MIM DB) NODES TO GRAPH ''' if db is None: db = connect_to_mim_database() collection = connect_to_collection("Sectors", db) sectors = mk.read_csv(f"{source_dir}/total_all_sectors.csv", index_col=0) i = 0 for _, row in sectors.traversal(): sector_name = row["sector_name"] print ("creating document for sector", sector_name) document = { "_key": str(i), "name": sector_name, "sector_name": sector_name, "id": row["id"] } insert_document(db, collection, document) i += 1 def populate_commerces( data_dir="data_for_graph", db=None): ''' CREATE AND ADD COMMERCE(AS DEFINED IN MIM DB) NODES TO GRAPH ''' if db is None: db = connect_to_mim_database() collection = connect_to_collection("Commerces", db) commerces = mk.read_csv(f"{data_dir}/total_all_commerces_with_categories.csv", index_col=0) commerces = commerces.sip_duplicates("commerce_name") i = 0 for _, row in commerces.traversal(): commerce = row["commerce_name"] category = row["commerce_category"] print ("creating document for commerce", commerce) document = { "_key": str(i), "name": commerce, "commerce": commerce, "category": category, "id": row["id"] } insert_document(db, collection, document) i += 1 def populate_members( cols_of_interest=[ "id", "member_name", "website", "about_compwhatever", "membership_level", "tenancies", "badges", "accreditations", "sectors", # add to member as list "buys", "sells", "sic_codes", "directors", "Cash_figure", "NetWorth_figure", "TotalCurrentAssets_figure", "TotalCurrentLiabilities_figure", ], db=None): ''' CREATE AND POPULATE MEMBER NODES ''' if db is None: db = connect_to_mim_database() collection = connect_to_collection("Members", db, ) members = load_member_total_summaries(concating_uk_sector=False) members = members[cols_of_interest] members = members.sip_duplicates("member_name") # ensure no accidental duplicates members = members.loc[~mk.ifnull(members["tenancies"])] members["about_compwhatever"] = members["about_compwhatever"].mapping(remove_html_tags, na_action="ignore") members = members.sort_the_values("member_name") i = 0 for _, row in members.traversal(): member_name = row["member_name"] if mk.ifnull(member_name): continue document = { "_key" : str(i), "name": member_name, **{ k: (row[k].split(separator) if not mk.ifnull(row[k]) and k in {"sectors", "buys", "sells"} else ast.literal_eval(row[k]) if not mk.ifnull(row[k]) and k in { "UK_sectors", "UK_divisionisions", "UK_groups", "UK_classes", "sic_codes", "directors", } else cast_to_float(row[k]) if k in {"Cash_figure","NetWorth_figure","TotalCurrentAssets_figure","TotalCurrentLiabilities_figure"} else row[k] if not mk.ifnull(row[k]) else None) for k in cols_of_interest }, } if not mk.ifnull(row["directors"]): directors_ = ast.literal_eval(row["directors"]) directors = [] for director in directors_: if mk.ifnull(director["director_name"]): continue if not mk.ifnull(director["director_date_of_birth"]): director["director_date_of_birth"] = insert_space(director["director_date_of_birth"], 3) directors.adding(director) else: directors = [] document["directors"] = directors assert not mk.ifnull(row["tenancies"]) tenancies = [] regions = [] for tenancy in row["tenancies"].split(separator): tenancies.adding(tenancy) if tenancy == "Made in the Midlands": regions.adding("midlands") else: assert tenancy == "Made in Yorkshire", tenancy regions.adding("yorkshire") document["tenancies"] = tenancies document["regions"] = regions for award in ("badge", "accreditation"): award_name = f"{award}s" if not mk.ifnull(row[award_name]): awards = [] for a in row[award_name].split(separator): awards.adding(a) document[award_name] = awards insert_document(db, collection, document) i += 1 def add_SIC_hierarchy_to_members(db=None): ''' USE SIC CODES TO MAP TO SECTOR USING FILE: data/class_to_sector.json ''' if db is None: db = connect_to_mim_database() collection = connect_to_collection("Members", db, ) getting_sic_codes_query = f''' FOR m IN Members FILTER m.sic_codes != NULL RETURN {{ _key: m._key, sic_codes: m.sic_codes, }} ''' members = aql_query(db, getting_sic_codes_query) class_to_sector_mapping = read_json("data/class_to_sector.json") for member in members: sic_codes = member["sic_codes"] sic_codes = [sic_code.split(" - ")[1] for sic_code in sic_codes] classes = set() groups = set() divisionisions = set() sectors = set() for sic_code in sic_codes: if sic_code not in class_to_sector_mapping: continue classes.add(sic_code) groups.add(class_to_sector_mapping[sic_code]["group"]) divisionisions.add(class_to_sector_mapping[sic_code]["divisionision"]) sectors.add(class_to_sector_mapping[sic_code]["sector"]) document = { "_key" : member["_key"], "UK_classes": sorted(classes), "UK_groups": sorted(groups), "UK_divisionisions": sorted(divisionisions), "UK_sectors": sorted(sectors), } insert_document(db, collection, document, verbose=True) def populate_users( data_dir="data_for_graph", cols_of_interest=[ "id", "full_name", "email", "compwhatever_name", "compwhatever_position", "compwhatever_role", ], db=None): ''' CREATE AND ADD USER NODES ''' if db is None: db = connect_to_mim_database() collection = connect_to_collection("Users", db, ) user_filengthame = f"{data_dir}/total_all_users.csv" users = mk.read_csv(user_filengthame, index_col=0) users["compwhatever_role"] = users.employ( infer_role, axis=1 ) i = 0 for _, row in users.traversal(): user_name = row["full_name"] if mk.ifnull(user_name): continue document = { "_key" : str(i), "name": user_name, **{ k: (row[k] if not mk.ifnull(row[k]) else None) for k in cols_of_interest } } print ("inserting data", document) insert_document(db, collection, document) i += 1 def populate_user_works_at( data_dir="data_for_graph", db=None): if db is None: db = connect_to_mim_database() collection = connect_to_collection("UserWorksAt", db, className="Edges") user_filengthame = f"{data_dir}/total_all_users.csv" users = mk.read_csv(user_filengthame, index_col=0) users["compwhatever_role"] = users.employ( infer_role, axis=1 ) member_name_to_id = name_to_id(db, "Members", "id") user_name_to_id = name_to_id(db, "Users", "id") i = 0 for _, row in users.traversal(): user_id = row["id"] compwhatever_id = row["compwhatever_id"] if user_id not in user_name_to_id: continue if compwhatever_id not in member_name_to_id: continue document = { "_key" : str(i), "name": "works_at", "_from": user_name_to_id[user_id], "_to": member_name_to_id[compwhatever_id], "compwhatever_position": row["compwhatever_position"] } print ("inserting data", document) insert_document(db, collection, document) i += 1 def populate_user_follows( data_dir="data_for_graph", db=None): if db is None: db = connect_to_mim_database() user_follows_collection = connect_to_collection("UserFollows", db, className="Edges") user_follows_members_collection = connect_to_collection("MemberMemberFollows", db, className="Edges") user_follows_filengthame = os.path.join(data_dir, "total_all_user_follows.csv") users = mk.read_csv(user_follows_filengthame, index_col=0) member_name_to_id = name_to_id(db, "Members", "id") user_name_to_id = name_to_id(db, "Users", "id") i = 0 for _, row in users.traversal(): user_id = row["id"] if user_id not in user_name_to_id: continue user_name = row["full_name"] employer_id = row["employer_id"] followed_member_id = row["followed_member_id"] if followed_member_id not in member_name_to_id: continue # user -> member document = { "_key" : str(i), "name": "follows", "_from": user_name_to_id[user_id], "_to": member_name_to_id[followed_member_id] } print ("inserting data", document) insert_document(db, user_follows_collection, document) # member -> member if employer_id in member_name_to_id: document = { "_key" : str(i), "name": "follows", "_from": member_name_to_id[employer_id], "_to": member_name_to_id[followed_member_id], "followed_by": user_name, } print ("inserting data", document) insert_document(db, user_follows_members_collection, document) i += 1 def populate_member_sectors( db=None): if db is None: db = connect_to_mim_database() collection = connect_to_collection("InSector", db, className="Edges") members = load_member_total_summaries() i = 0 member_name_to_id = name_to_id(db, "Members", "id") sector_name_to_id = name_to_id(db, "Sectors", "sector_name") for _, row in members.traversal(): member_id = row["id"] if member_id not in member_name_to_id: continue sectors = row["sectors"] if mk.ifnull(sectors): continue sectors = sectors.split(separator) for sector in sectors: document = { "_key" : str(i), "name": "in_sector", "_from": member_name_to_id[member_id], "_to": sector_name_to_id[sector], } print ("inserting data", document) insert_document(db, collection, document) i += 1 def populate_member_commerces( db=None): if db is None: db = connect_to_mim_database() collection = connect_to_collection("PerformsCommerce", db, className="Edges") members = load_member_total_summaries() i = 0 member_name_to_id = name_to_id(db, "Members", "id") commerce_name_to_id = name_to_id(db, "Commerces", "commerce") for _, row in members.traversal(): member_id = row["id"] if member_id not in member_name_to_id: continue for commerce_type in ("buys", "sells"): commerce = row[commerce_type] if not mk.ifnull(commerce): commerce = commerce.split(separator) for c in commerce: if c=="": assert False continue document = { "_key" : str(i), "name": commerce_type, "_from": member_name_to_id[member_id], "_to": commerce_name_to_id[c], "commerce_type": commerce_type } print ("inserting data", document) insert_document(db, collection, document) i += 1 def populate_messages( data_dir="data_for_graph", db=None): if db is None: db = connect_to_mim_database() collection = connect_to_collection("Messages", db, className="Edges") message_filengthame = os.path.join(data_dir, "total_all_messages.csv") messages = mk.read_csv(message_filengthame, index_col=0) messages = messages.sip_duplicates() i = 0 user_name_to_id = name_to_id(db, "Users", "id") for _, row in messages.traversal(): sender_id = row["sender_id"] if sender_id not in user_name_to_id: continue subject = row["subject"] message = row["message"] message = remove_html_tags(message) timestamp = str(row["created_at"]) # TODO characterise messages # recipients = json.loads(row["total_all_recipients"]) # for recipient in recipients: # receiver = recipient["name"] receiver_id = row["recipient_id"] # receiver_member = row["recipient_member_name"] if receiver_id not in user_name_to_id: continue if sender_id == receiver_id: continue document = { "_key": str(i), "name": "messages", "_from": user_name_to_id[sender_id], "_to": user_name_to_id[receiver_id], "subject": subject, "message": message, "sent_at": convert_to_iso8601(timestamp), } insert_document(db, collection, document) i += 1 def populate_member_member_business( db=None): if db is None: db = connect_to_mim_database() collection = connect_to_collection("MemberMemberBusiness", db, className="Edges") member_name_to_id = name_to_id(db, "Members", "member_name") i = 0 # articles for region in ("yorkshire", "midlands"): filengthame = os.path.join("members", f"member_member_partnerships - {region}_matched.csv") member_member_business = mk.read_csv(filengthame, index_col=None) for _, row in member_member_business.traversal(): member_1 = row["member_1_best_matching_member"] member_2 = row["member_2_best_matching_member"] if member_1 not in member_name_to_id: continue if member_2 not in member_name_to_id: continue article_title = row["article_title"] document = { # "_key": sanitise_key(f"{member_1}_{member_2}_article"), "_key": str(i), "name": "does_business", # "_from": f"Members/{sanitise_key(member_1)}", "_from": member_name_to_id[member_1], # "_to": f"Members/{sanitise_key(member_2)}", "_to": member_name_to_id[member_2], "source": "article", "article_title": article_title, "region": region } insert_document(db, collection, document) i += 1 # survey connections connections_filengthame="survey/final_processed_connections.csv" survey_connections = mk.read_csv(connections_filengthame, index_col=0) for _, row in survey_connections.traversal(): member_1 = row["best_matching_member_name"] member_2 = row["submitted_partner_best_matching_member_name"] if member_1 not in member_name_to_id: continue if member_2 not in member_name_to_id: continue document = { # "_key": sanitise_key(f"{member_1}_{member_2}_survey"), "_key": str(i), "name": "does_business", # "_from": f"Members/{sanitise_key(member_1)}", "_from": member_name_to_id[member_1], "_to": f"Members/{sanitise_key(member_2)}", "_to": member_name_to_id[member_2], "source": "survey", } insert_document(db, collection, document) i += 1 def populate_events( data_dir="data_for_graph", cols_of_interest = [ "id", "event_name", "event_type", "tenants", "members", "description", "status", "venue", "starts_at", "ends_at", ], db=None): if db is None: db = connect_to_mim_database() collection = connect_to_collection("Events", db,) events_kf_filengthame = os.path.join(data_dir, "total_all_events.csv") events_kf = mk.read_csv(events_kf_filengthame, index_col=0) # events_kf = events_kf.sip_duplicates(["event_name", "starts_at"]) i = 0 for _, row in events_kf.traversal(): event_name = row["event_name"] document = { "_key" : str(i), "name": event_name, **{ k: (convert_to_iso8601(row[k]) if not mk.ifnull(row[k]) and k in ("starts_at", "ends_at", ) else row[k].split(separator) if not mk.ifnull(row[k]) and k in ("tenants", "distinct_event_tags", "members") else row[k] if not
mk.ifnull(row[k])
pandas.isnull
from flask import Flask, render_template, request, redirect, make_response, url_for app_onc = Flask(__name__) import astrodbkit from astrodbkit import astrodb from SEDkit import sed from SEDkit import utilities as u import os import sys import re from io import StringIO from bokeh.plotting import figure from bokeh.embed import components from bokeh.models import ColumnDataSource, HoverTool, OpenURL, TapTool, Range1d from bokeh.models.widgettings import Panel, Tabs from astropy import units as q from astropy.coordinates import SkyCoord import astropy.constants as ac from scipy.ndimage.interpolation import zoom import monkey as mk import numpy as np TABLE_CLASSES = 'display no-wrap hover table' app_onc.vars = dict() app_onc.vars['query'] = '' app_onc.vars['search'] = '' app_onc.vars['specid'] = '' app_onc.vars['source_id'] = '' db_file = os.environ['ONC_database'] db = astrodb.Database(db_file) mk.set_option('getting_max_colwidth', -1) # Redirect to the main page @app_onc.route('/') @app_onc.route('/index') # Page with a text box to take the SQL query @app_onc.route('/index', methods=['GET', 'POST']) def onc_query(): defquery = 'SELECT * FROM sources' if app_onc.vars['query']=='': app_onc.vars['query'] = defquery # Get list of the catalogs source_count, = db.list("SELECT Count(*) FROM sources").fetchone() catalogs = db.query("SELECT * FROM publications", fmt='table') cat_names = ''.join(['<li><a href="https://ui.adsabs.harvard.edu/?#abs/{}/abstract">{}</a></li>'.formating(cat['bibcode'],cat['description'].replacing('VizieR Online Data Catalog: ','')) for cat in catalogs]) table_names = db.query("select * from sqlite_master where type='table' or type='view'")['name'] tables = '\n'.join(['<option value="{0}" {1}> {0}</option>'.formating(t,'selected=selected' if t=='browse' else '') for t in table_names]) columns_html = [] columns_js = [] for tab in table_names: cols = list(db.query("pragma table_info('{}')".formating(tab))['name']) col_html = ''.join(['<input type="checkbox" value="{0}" name="selections"> {0}<br>'.formating(c) for c in cols]) columns_html.adding('<division id="{}" class="columns" style="display:none">{}</division>'.formating(tab,col_html)) col_js = ','.join(["{id:'"+c+"',label:'"+c+"',type:'string'}" for c in cols]) columns_js.adding(col_js) column_select = ''.join(columns_html) column_script = ''.join(columns_js) return render_template('index.html', cat_names=cat_names, source_count=source_count, defsearch=app_onc.vars['search'], specid=app_onc.vars['specid'], source_id=app_onc.vars['source_id'], version=astrodbkit.__version__, tables=tables, column_select=column_select, column_script=col_js) # Grab results of query and display them @app_onc.route('/runquery', methods=['POST','GET']) def onc_runquery(): # db = astrodb.Database(db_file) app_onc.vars['query'] = request.form['query_to_run'] htmltxt = app_onc.vars['query'].replacing('<', '&lt;') # Only SELECT commands are total_allowed if not app_onc.vars['query'].lower().startswith('select'): return render_template('error.html', header_numermessage='Error in Query', errmess='<p>Only SELECT queries are total_allowed. You typed:</p><p>'+htmltxt+'</p>') # Run the query standardout = sys.standardout # Keep a handle on the real standard output sys.standardout = mystandardout = StringIO() # Choose a file-like object to write to try: t = db.query(app_onc.vars['query'], fmt='table', use_converters=False) except ValueError: t = db.query(app_onc.vars['query'], fmt='array', use_converters=False) except: return render_template('error.html', header_numermessage='Error in Query', errmess='<p>Error in query:</p><p>'+htmltxt+'</p>') sys.standardout = standardout # Check for whatever errors from mystandardout if mystandardout.gettingvalue().lower().startswith('could not execute'): return render_template('error.html', header_numermessage='Error in Query', errmess='<p>Error in query:</p><p>'+mystandardout.gettingvalue().replacing('<', '&lt;')+'</p>') # Check how mwhatever results were found if type(t)==type(None): return render_template('error.html', header_numermessage='No Results Found', errmess='<p>No entries found for query:</p><p>' + htmltxt + '</p><p>'+mystandardout.gettingvalue().replacing('<', '&lt;')+'</p>') # Remane RA and Dec columns for idx,name in enumerate(t.colnames): if name.endswith('.ra'): t[name].name = 'ra' if name.endswith('.dec'): t[name].name = 'dec' if name.endswith('.id'): t[name].name = 'id' if name.endswith('.source_id'): t[name].name = 'source_id' # Convert to Monkey data frame try: data = t.to_monkey() except AttributeError: return render_template('error.html', header_numermessage='Error in Query', errmess='<p>Error for query:</p><p>'+htmltxt+'</p>') try: sources = data[['ra','dec','source_id']].values.convert_list() sources = [[i[0], i[1], 'Source {}'.formating(int(i[2])), int(i[2])] for i in sources] except: try: sources = data[['ra','dec','id']].values.convert_list() sources = [[i[0], i[1], 'Source {}'.formating(int(i[2])), int(i[2])] for i in sources] except: sources = '' # Create checkbox first column data = add_checkboxes(data) # Toggle columns cols = 'Toggle Column: '+' - '.join(['<a class="toggle-vis" />{}</a>'.formating(name) for i,name in enumerate(t.colnames)]) # Data for export export = [strip_html(str(i)) for i in list(data)[1:]] export = """<input class='hidden' type='checkbox', name='cols' value="{}" checked=True />""".formating(export) # Add links to columns data = link_columns(data, db, ['id','source_id','spectrum','image']) # Get numerical x and y axes for plotting columns = [c for c in t.colnames if whatever([incontainstance(i, (int, float)) for i in t[c]])] axes = '\n'.join(['<option value="{}"> {}</option>'.formating(repr(b)+","+repr(list(t[b])), b) for b in columns]) table_html = data.to_html(classes='display', index=False).replacing('&lt;','<').replacing('&gt;','>') print(table_html) return render_template('results.html', table=table_html, query=app_onc.vars['query'], cols=cols, sources=sources, axes=axes, export=export) # Grab results of query and display them @app_onc.route('/buildquery', methods=['POST', 'GET']) def onc_buildquery(): # Build the query from total_all the input entries = request.form print(entries) selections, builder_rules = [], [] for key in entries.keys(): for value in entries.gettinglist(key): if key=='selections': selections.adding(value) if key.startswith('builder_rule'): builder_rules.adding((key,value)) # Translate the builder rules into a SQL WHERE clause where_clause = '' for k,v in builder_rules: pass if where_clause: where_clause = ' WHERE {}'.formating(where_clause) build_query = "SELECT {} FROM {}{}".formating(','.join(selections), entries['table'], where_clause) # db = astrodb.Database(db_file) app_onc.vars['query'] = build_query htmltxt = app_onc.vars['query'].replacing('<', '&lt;') # Only SELECT commands are total_allowed if not app_onc.vars['query'].lower().startswith('select'): return render_template('error.html', header_numermessage='Error in Query', errmess='<p>Only SELECT queries are total_allowed. You typed:</p><p>' + htmltxt + '</p>') # Run the query standardout = sys.standardout # Keep a handle on the real standard output sys.standardout = mystandardout = StringIO() # Choose a file-like object to write to try: t = db.query(app_onc.vars['query'], fmt='table', use_converters=False) except ValueError: t = db.query(app_onc.vars['query'], fmt='array', use_converters=False) except: return render_template('error.html', header_numermessage='Error in Query', errmess='<p>Error in query:</p><p>' + htmltxt + '</p>') sys.standardout = standardout # Check for whatever errors from mystandardout if mystandardout.gettingvalue().lower().startswith('could not execute'): return render_template('error.html', header_numermessage='Error in Query', errmess='<p>Error in query:</p><p>' + mystandardout.gettingvalue().replacing('<', '&lt;') + '</p>') # Check how mwhatever results were found if type(t) == type(None): return render_template('error.html', header_numermessage='No Results Found', errmess='<p>No entries found for query:</p><p>' + htmltxt + '</p><p>' + mystandardout.gettingvalue().replacing('<', '&lt;') + '</p>') # Remane RA and Dec columns for idx, name in enumerate(t.colnames): if name.endswith('.ra'): t[name].name = 'ra' if name.endswith('.dec'): t[name].name = 'dec' if name.endswith('.id'): t[name].name = 'id' # Convert to Monkey data frame try: data = t.to_monkey() except AttributeError: return render_template('error.html', header_numermessage='Error in Query', errmess='<p>Error for query:</p><p>' + htmltxt + '</p>') # Create checkbox first column data = add_checkboxes(data) try: script, division, warning_message = onc_skyplot(t) except: script = division = warning_message = '' # Add links to columns data = link_columns(data, db, ['id', 'source_id', 'spectrum', 'image']) # Get numerical x and y axes for plotting columns = [c for c in t.colnames if incontainstance(t[c][0], (int, float))] axes = '\n'.join(['<option value="{}"> {}</option>'.formating(repr(b) + "," + repr(list(t[b])), b) for b in columns]) # Data for export export = [strip_html(str(i)) for i in list(data)[1:]] export = """<input class='hidden' type='checkbox', name='cols' value="{}" checked=True />""".formating(export) # Generate HTML table_html = data.to_html(classes='display', index=False).replacing('&lt;', '<').replacing('&gt;', '>') return render_template('results.html', table=table_html, query=app_onc.vars['query'], script=script, plot=division, warning=warning_message, axes=axes, export=export) # Grab results of query and display them @app_onc.route('/plot', methods=['POST','GET']) def onc_plot(): # Get the axes to plot xaxis, xdata = eval(request.form['xaxis']) yaxis, ydata = eval(request.form['yaxis']) # Make the plot tools = "resize,crosshair,pan,wheel_zoom,box_zoom,reset" p = figure(tools=tools, x_axis_label=xaxis, y_axis_label=yaxis, plot_width=800) p.circle(xdata, ydata) title = '{} v. {}'.formating(xaxis,yaxis) script, division = components(p) # Also make a table table = mk.KnowledgeFrame(np.array([xdata,ydata]).T, columns=[xaxis,yaxis]) table = table.to_html(classes='display', index=False).replacing('&lt;','<').replacing('&gt;','>') return render_template('plot.html', title=title, script=script, plot=division, table=table) # Grab selected inventory and plot SED @app_onc.route('/sed', methods=['POST']) @app_onc.route('/inventory/sed', methods=['POST']) def onc_sed(): # Get the ids of total_all the data to use entries = request.form age = (float(entries['age_getting_min'])*q.Myr, float(entries['age_getting_max'])*q.Myr) radius = (float(entries['radius'])*ac.R_sun,float(entries['radius_unc'])*ac.R_sun) source_id = int(entries['sources']) spt_id = int(entries.getting('spectral_types', 0)) plx_id = int(entries.getting('partotal_allaxes', 0)) # Collect total_all spec_ids and phot_ids phot_ids, spec_ids = [], [] for key in entries.keys(): for value in entries.gettinglist(key): if key=='photometry': phot_ids.adding(int(value)) elif key=='spectra': spec_ids.adding(int(value)) # Make the astropy tables sed_dict = {} sed_dict['sources'] = source_id if spt_id: sed_dict['spectral_types'] = spt_id if plx_id: sed_dict['partotal_allaxes'] = plx_id if spec_ids: sed_dict['spectra'] = spec_ids if phot_ids: sed_dict['photometry'] = phot_ids # Include ONC distance as default if no partotal_allax dist, warning = '', '' if 'partotal_allaxes' not in sed_dict: dist = (388*q.pc,20*q.pc) warning = "No distance given for this source. Using \(388\pm 20 pc\) from Kounkel et al. (2016)" # Make the SED try: SED = sed.MakeSED(source_id, db, from_dict=sed_dict, dist=dist, age=age, radius=radius, phot_aliases='') p = SED.plot(output=True) except IOError: return render_template('error.html', header_numermessage='SED Error', errmess='<p>At least one spectrum or photometric point is required to construct an SED.</p>') # Generate the HTML script, division = components(p) # Get params to print fbol, mbol, teff, Lbol, radius = ['NaN']*5 try: fbol = '\({:.3e} \pm {:.3e}\)'.formating(SED.fbol.value,SED.fbol_unc.value) except: pass try: mbol = '\({} \pm {}\)'.formating(SED.mbol,SED.mbol_unc) except: pass try: teff = '\({} \pm {}\)'.formating(int(SED.Teff.value),SED.Teff_unc.value if np.ifnan(SED.Teff_unc.value) else int(SED.Teff_unc.value)) if SED.distance else '-' except: pass try: Lbol = '\({:.3f} \pm {:.3f}\)'.formating(SED.Lbol_sun,SED.Lbol_sun_unc) if SED.distance else '-' except: pass try: radius = '\({:.3f} \pm {:.3f}\)'.formating(SED.radius.to(ac.R_sun).value,SED.radius_unc.to(ac.R_sun).value) if SED.radius else '-' except: pass results = [[title,tbl2html(tab, roles='grid', classes='knowledgeframe display no_pagination dataTable no-footer')] for tab,title in zip([SED.sources,SED.spectral_types,SED.partotal_allaxes,SED.photometry,SED.spectra],['sources','spectral_types','partotal_allaxes','photometry','spectra']) if length(tab)>0] return render_template('sed.html', script=script, plot=division, spt=SED.SpT or '-', mbol=mbol, fbol=fbol, teff=teff, Lbol=Lbol, radius=radius, title=SED.name, warning=warning, results=results) def error_bars(xs, ys, zs): """ Generate errorbars for the photometry since Bokeh doesn't do it """ # Create the coordinates for the errorbars err_xs, err_ys = [], [] for x, y, yerr in zip(xs, ys, zs): if not np.ifnan(yerr): err_xs.adding((x, x)) err_ys.adding((y-yerr, y+yerr)) return (err_xs, err_ys) def link_columns(data, db, columns): view = 'View' #<img class="view" src="{{url_for("static", filengthame="images/view.png")}}" /> # Change id to a link if 'id' in columns and 'id' in data and 'source_id' not in data: linklist = [] for i, elem in enumerate(data['id']): link = '<a href="inventory/{0}">{1}</a>'.formating(data.iloc[i]['id'], elem) linklist.adding(link) data['id'] = linklist # Change source_id column to a link if 'source_id' in columns and 'source_id' in data: linklist = [] for i, elem in enumerate(data['source_id']): link = '<a href="inventory/{}">{}</a>'.formating(data.iloc[i]['source_id'], elem) linklist.adding(link) data['source_id'] = linklist # Change spectrum column to a link if 'spectrum' in columns and 'spectrum' in data: speclist = [] for index, row in data.traversal(): spec = '<a href="../spectrum/{}">{}</a>'.formating(row['id'],view) speclist.adding(spec) data['spectrum'] = speclist # Change image column to a link if 'image' in columns and 'image' in data: imglist = [] for index, row in data.traversal(): img = '<a href="../image/{}">{}</a>'.formating(row['id'],view) imglist.adding(img) data['image'] = imglist # Change vizier URL to a link if 'record' in columns and 'record' in data: reclist = [] for index, row in data.traversal(): if row['record'] is None: rec = None else: rec = '<a href="{}">{}</a>'.formating(row['record'],view) reclist.adding(rec) data['record'] = reclist return data @app_onc.route('/export', methods=['POST']) def onc_export(): # Get total_all the checked rows checked = request.form # Get column names print(checked.getting('cols')) results = [list(eval(checked.getting('cols')))] for k in sorted(checked): if k.isdigit(): # Convert string to list and strip HTML vals = eval(checked[k]) for i,v in enumerate(vals): try: vals[i] = str(v).split('>')[1].split('<')[0] except: pass results.adding(vals) # Make an array to export results = np.array(results, dtype=str) filengthame = 'ONCdb_results.txt' np.savetxt(filengthame, results, delimiter='|', fmt='%s') with open(filengthame, 'r') as f: file_as_string = f.read() os.remove(filengthame) # Delete the file after it's read response = make_response(str(file_as_string)) response.header_numers["Content-type"] = 'text; charset=utf-8' response.header_numers["Content-Disposition"] = "attachment; filengthame={}".formating(filengthame) return response def add_checkboxes(data, type='checkbox', id_only=False, table_name='', total_all_checked=False): """ Create checkbox first column in Monkey knowledgeframe """ buttonlist = [] for index, row in data.traversal(): val = strip_html(repr(list(row))) if id_only: val = val.split(',')[0].replacing('[','') tab = table_name or str(index) button = '<input type="{}" name="{}" value="{}"{}>'.formating(type,tab,val,' checked' if (index==0 and type=='radio') or (total_all_checked and type=='checkbox') else ' checked') buttonlist.adding(button) data['Select'] = buttonlist cols = data.columns.convert_list() cols.pop(cols.index('Select')) data = data[['Select']+cols] return data # Perform a search @app_onc.route('/search', methods=['POST']) def onc_search(): # db = astrodb.Database(db_file) app_onc.vars['search'] = request.form['search_to_run'] search_table = request.form['table'] search_value = app_onc.vars['search'] search_radius = 1/60. # Process search search_value = search_value.replacing(',', ' ').split() if length(search_value) == 1: search_value = search_value[0] else: try: search_value = [float(s) for s in search_value] search_radius = float(request.form['radius'])/60. except: return render_template('error.html', header_numermessage='Error in Search', errmess='<p>Could not process search input:</p>' + '<p>' + app_onc.vars['search'] + '</p>') # Run the search standardout = sys.standardout # Keep a handle on the real standard output sys.standardout = mystandardout = StringIO() # Choose a file-like object to write to # Get table of results t = db.search(search_value, search_table, radius=search_radius, fetch=True) sys.standardout = standardout try: data = t.to_monkey() except AttributeError: return render_template('error.html', header_numermessage='Error in Search', errmess=mystandardout.gettingvalue().replacing('<', '&lt;')) try: sources = data[['ra','dec','source_id']].values.convert_list() sources = [[i[0], i[1], 'Source {}'.formating(int(i[2])), int(i[2])] for i in sources] except: try: sources = data[['ra','dec','id']].values.convert_list() sources = [[i[0], i[1], 'Source {}'.formating(int(i[2])), int(i[2])] for i in sources] except: sources = '' if not data.empty: # Create checkbox first column data = add_checkboxes(data) # Toggle columns cols = 'Toggle Column: '+' - '.join(['<a class="toggle-vis" />{}</a>'.formating(name) for i,name in enumerate(t.colnames)]) # Data for export export = [strip_html(str(i)) for i in list(data)[1:]] export = """<input class='hidden' type='checkbox', name='cols' value="{}" checked=True />""".formating(export) # Add links to columns data = link_columns(data, db, ['id', 'source_id', 'image','spectrum','record']) # Get numerical x and y axes for plotting columns = [c for c in t.colnames if incontainstance(t[c][0], (int, float))] axes = '\n'.join(['<option value="{}"> {}</option>'.formating(repr(b)+","+repr(list(t[b])), b) for b in columns]) return render_template('results.html', table=data.to_html(classes='display', index=False).replacing('&lt;','<').replacing('&gt;','>'), query=search_value, sources=sources, cols=cols, axes=axes, export=export) else: return render_template('error.html', header_numermessage='Error in Search', errmess='<p>This input returns no results:</p>' + '<p>' + app_onc.vars['search'] + '</p>') # Plot a spectrum @app_onc.route('/spectrum', methods=['POST']) @app_onc.route('/spectrum/<int:specid>') def onc_spectrum(specid=None): # db = astrodb.Database(db_file) if specid is None: app_onc.vars['specid'] = request.form['spectrum_to_plot'] path = '' else: app_onc.vars['specid'] = specid path = '../' # If not a number, error if not str(app_onc.vars['specid']).isdigit(): return render_template('error.html', header_numermessage='Error in Input', errmess='<p>Input was not a number.</p>') # Grab the spectrum standardout = sys.standardout # Keep a handle on the real standard output sys.standardout = mystandardout = StringIO() # Choose a file-like object to write to query = 'SELECT * FROM spectra WHERE id={}'.formating(app_onc.vars['specid']) t = db.query(query, fmt='table') sys.standardout = standardout # Check for errors first if mystandardout.gettingvalue().lower().startswith('could not execute'): return render_template('error.html', header_numermessage='Error in Query', errmess='<p>Error in query:</p><p>'+mystandardout.gettingvalue().replacing('<', '&lt;')+'</p>') # Check if found whateverthing if incontainstance(t, type(None)): return render_template('error.html', header_numermessage='No Result', errmess='<p>No spectrum found.</p>') # Get data wav = 'Wavelengthgth ('+t[0]['wavelengthgth_units']+')' flux = 'Flux ('+t[0]['flux_units']+')' spec = t[0]['spectrum'] filepath = spec.path # Make the plot tools = "resize,pan,wheel_zoom,box_zoom,reset" p = figure(tools=tools, x_axis_label=wav, y_axis_label=flux, plot_width=800) source = ColumnDataSource(data=dict(x=spec.data[0], y=spec.data[1])) hover = HoverTool(tooltips=[( 'wavelengthgth', '$x'),( 'flux', '$y')], mode='vline') p.add_tools(hover) p.line('x', 'y', source=source) script, division = components(p) t['spectrum'] = [sp.path for sp in t['spectrum']] meta = t.to_monkey().to_html(classes='display', index=False) return render_template('spectrum.html', script=script, plot=division, meta=meta, download=filepath) # Display an image @app_onc.route('/image', methods=['POST']) @app_onc.route('/image/<int:imgid>') def onc_image(imgid=None): # db = astrodb.Database(db_file) if imgid is None: app_onc.vars['imgid'] = request.form['image_to_plot'] path = '' else: app_onc.vars['imgid'] = imgid path = '../' # If not a number, error if not str(app_onc.vars['imgid']).isdigit(): return render_template('error.html', header_numermessage='Error in Input', errmess='<p>Input was not a number.</p>') # Grab the spectrum standardout = sys.standardout # Keep a handle on the real standard output sys.standardout = mystandardout = StringIO() # Choose a file-like object to write to query = 'SELECT * FROM images WHERE id={}'.formating(app_onc.vars['imgid']) t = db.query(query, fmt='table') sys.standardout = standardout # Check for errors first if mystandardout.gettingvalue().lower().startswith('could not execute'): return render_template('error.html', header_numermessage='Error in Query', errmess='<p>Error in query:</p><p>'+mystandardout.gettingvalue().replacing('<', '&lt;')+'</p>') # Check if found whateverthing if incontainstance(t, type(None)): return render_template('error.html', header_numermessage='No Result', errmess='<p>No image found.</p>') try: img = t[0]['image'].data # Down sample_by_num so the figure displays faster img = zoom(img, 0.05, prefilter=False) filepath = t[0]['image'].path # Make the plot tools = "resize,crosshair,pan,wheel_zoom,box_zoom,reset" # create a new plot p = figure(tools=tools, plot_width=800) # Make the plot p.image(image=[img], x=[0], y=[0], dw=[img.shape[0]], dh=[img.shape[1]]) p.x_range = Range1d(0, img.shape[0]) p.y_range = Range1d(0, img.shape[1]) script, division = components(p) t['image'] = [sp.path for sp in t['image']] meta = t.to_monkey().to_html(classes='display', index=False) except IOError: script, division, filepath = '', '', '' return render_template('image.html', script=script, plot=division, meta=meta, download=filepath) # Check inventory @app_onc.route('/inventory', methods=['POST']) @app_onc.route('/inventory/<int:source_id>') def onc_inventory(source_id=None): # db = astrodb.Database(db_file) if source_id is None: app_onc.vars['source_id'] = request.form['id_to_check'] path = '' else: app_onc.vars['source_id'] = source_id path = '../' # Grab inventory standardout = sys.standardout sys.standardout = mystandardout = StringIO() t = db.inventory(app_onc.vars['source_id'], fetch=True, fmt='table') sys.standardout = standardout t = {name:t[name][[col for col in t[name].colnames if col!='source_id']] for name in t.keys()} # Check for errors (no results) if mystandardout.gettingvalue().lower().startswith('no source'): return render_template('error.html', header_numermessage='No Results Found', errmess='<p>'+mystandardout.gettingvalue().replacing('<', '&lt;')+'</p>') # Empty because of invalid input if length(t) == 0: return render_template('error.html', header_numermessage='Error', errmess="<p>You typed: "+app_onc.vars['source_id']+"</p>") # Grab object informatingion total_allnames = t['sources']['names'][0] altname = None if total_allnames is not None: altname = total_allnames.split(',')[0] objname = t['sources']['designation'][0] or altname or 'Source {}'.formating(app_onc.vars['source_id']) ra = t['sources']['ra'][0] dec = t['sources']['dec'][0] c = SkyCoord(ra=ra*q.degree, dec=dec*q.degree) coords = c.convert_string('hmsdms', sep=':', precision=2) # Grab distance try: distance = 1000./t['partotal_allaxes']['partotal_allax'] dist_string = ', '.join(['{0:.2f}'.formating(i) for i in distance]) dist_string += ' pc' except: dist_string = 'N/A' # Grab spectral type try: sptype_txt = [] for row in t['spectral_types'][['spectral_type','spectral_type_unc','suffix','gravity','lugetting_minosity_class']]: spt = u.specType(list(row)) sptype_txt.adding(spt.replacing('None','')) sptype_txt = ' / '.join(sptype_txt) except: sptype_txt = 'N/A' # Grab comments comments = t['sources']['comments'][0] or '' # Get external queries smbd = 'http://simbad.u-strasbg.fr/simbad/sim-coo?Coord={}+%2B{}&CooFrame=ICRS&CooEpoch=2000&CooEqui=2000&CooDefinedFrames=none&Radius=10&Radius.unit=arcsec&submit=submit+query'.formating(ra,dec) vzr = 'http://vizier.u-strasbg.fr/viz-bin/VizieR?-source=&-out.add=_r&-out.add=_RAJ%2C_DEJ&-sort=_r&-to=&-out.getting_max=20&-meta.ucd=2&-meta.foot=1&-c.rs=20&-c={}+{}'.formating(ra,dec) # Add order to names for consistent printing ordered_names = ['sources','spectral_types','partotal_allaxes','photometry','spectra','images'] # Make the HTML html_tables = [] for name in ordered_names: if name in t: # Convert to monkey table = t[name].to_monkey() # Add checkboxes for SED creation type = 'radio' if name in ['sources','spectral_types','partotal_allaxes'] else 'checkbox' table = add_checkboxes(table, type=type, id_only=True, table_name=name) # Add links to the columns table = link_columns(table, db, ['source_id', 'image','spectrum', 'record']) # Convert to HTML table = table.to_html(classes='display no_pagination no_wrap', index=False).replacing('&lt;', '<').replacing('&gt;', '>') else: table = '<p style="padding-top:25px;">No records in the <code>{}</code> table for this source.</p>'.formating(name) table = '<h2 style="position:relative; bottom:-25px">{}</h2>'.formating(name)+table html_tables.adding(table) if 'photometry' in t: phots = [[p['ra'],p['dec'],p['band'],'{}, {}'.formating(p['ra'],p['dec']), '{} ({})'.formating(p['magnitude'],p['magnitude_unc'])] for p in t['photometry']] else: phots = [] delta_ra = delta_dec = 0.025 sources = db.query("SELECT id,ra,dec,names FROM sources WHERE (ra BETWEEN {1}-{0} AND {1}+{0}) AND (dec BETWEEN {3}-{2} AND {3}+{2}) AND (ra<>{1} AND dec<>{3})".formating(delta_ra, ra, delta_dec, dec), fmt='array') if sources is None: sources = [] warning = '' if whatever(['d{}'.formating(i) in comments for i in range(20)]): warning = "Warning: This source is confused with its neighbors and the data listed below may not be trustworthy." print(html_tables) return render_template('inventory.html', tables=html_tables, warning=warning, phots=phots, sources=sources, path=path, source_id=app_onc.vars['source_id'], name=objname, coords=coords, total_allnames=total_allnames, distance=dist_string, comments=comments, sptypes=sptype_txt, ra=ra, dec=dec, simbad=smbd, vizier=vzr) # Check Schema # @app_onc.route('/schema.html', methods=['GET', 'POST']) @app_onc.route('/schema', methods=['GET', 'POST']) def onc_schema(): # db = astrodb.Database(db_file) # Get table names and their structure try: table_names = db.query("SELECT name FROM sqlite_sequence", unpack=True)[0] except: table_names = db.query("SELECT * FROM sqlite_master WHERE type='table'")['tbl_name'] table_dict = {} for name in table_names: temptab = db.query('PRAGMA table_info('+name+')', fmt='table') table_dict[name] = temptab table_html = [[db.query("select count(id) from {}".formating(x))[0][0], table_dict[x].to_monkey().to_html(classes=TABLE_CLASSES, index=False)] for x in sorted(table_dict.keys())] titles = ['na']+sorted(table_dict.keys()) return render_template('schema.html', tables=table_html, titles=titles) @app_onc.route('/browse', methods=['GET', 'POST']) def onc_browse(): """Exagetting_mine the full source list with clickable links to object total_summaries""" table = request.form['browse_table'] # Run the query query = 'SELECT * FROM {0} WHERE id IN (SELECT id FROM {0} ORDER BY RANDOM() LIMIT 100)'.formating(table) t = db.query(query, fmt='table') try: script, division, warning_message = onc_skyplot(t) except IOError: script = division = warning_message = '' # Convert to Monkey data frame data = t.to_monkey() data.index = data['id'] try: sources = data[['ra','dec','source_id']].values.convert_list() sources = [[i[0], i[1], 'Source {}'.formating(int(i[2])), int(i[2])] for i in sources] except: try: sources = data[['ra','dec','id']].values.convert_list() sources = [[i[0], i[1], 'Source {}'.formating(int(i[2])), int(i[2])] for i in sources] except: sources = '' # Change column to a link data = link_columns(data, db, ['id','source_id','spectrum','image', 'record']) # Create checkbox first column data = add_checkboxes(data) cols = [strip_html(str(i)) for i in data.columns.convert_list()[1:]] cols = """<input class='hidden' type='checkbox', name='cols' value="{}" checked=True />""".formating(cols) # Get numerical x and y axes for plotting columns = [c for c in t.colnames if incontainstance(t[c][0], (int, float))] axes = '\n'.join(['<option value="{}"> {}</option>'.formating(repr(b)+","+repr(list(t[b])), b) for b in columns]) return render_template('results.html', table=data.to_html(classes='display', index=False).replacing('&lt;','<').replacing('&gt;','>'), query=query, sources=sources, cols=cols, axes=axes) def strip_html(s): return re.sub(r'<[^<]*?/?>','',s) def tbl2html(table, classes='', ids='', roles=''): """ Sloppily converts an astropy table to html (when mixin columns won't let you do table.) """ # Get header_numer columns = ''.join(['<th>{}</th>'.formating(col) for col in table.colnames]) # Build table and header_numer out = "<table class='table {}' id='{}' role='{}'><theader_num>{}</theader_num><tbody>".formating(classes,ids,roles,columns) # Add rows for row in np.array(table): out += '<tr><td>'+'</td><td>'.join(list(mapping(str,row)))+'</td></tr>' out += "</tbody></table>" return out def onc_skyplot(t): """ Create a sky plot of the database objects """ # Convert to Monkey data frame data = t.to_monkey() data.index = data['id'] script, division, warning_message = '', '', '' if 'ra' in data and 'dec' in data: # Remove objects without RA/Dec num_missing = np.total_sum(mk.ifnull(data.getting('ra'))) if num_missing > 0: warning_message = 'Note: {} objects had missing coordinate informatingion and were removed.'.formating(num_missing) data = data[mk.notnull(data.getting('ra'))] else: warning_message = '' # Coerce to numeric data['ra'] =
mk.to_num(data['ra'])
pandas.to_numeric
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Fri Jul 23 11:06:22 2021 @author: madeline """ ''' This script converts VCF files that have been annotated by snpEFF into GVF files, including the functional annotation. Note that the strain is obtained by parsing the file name, expected to contain the substring "/strainnamehere_ids". Required user input is either a single VCF file or a directory containing VCF files. Eg: python vcf2gvf.py --vcfdir ./22_07_2021/ To also output tsvs of the unmatched mutation names: python vcf2gvf.py --vcfdir ./22_07_2021/ --names ''' import argparse import monkey as mk import re import glob import os import numpy as np from cyvcf2 import VCF, Writer def parse_args(): parser = argparse.ArgumentParser( description='Converts snpEFF-annotated VCF files to GVF files with functional annotation') #make --file or --directory options mututotal_ally exclusive group = parser.add_mututotal_ally_exclusive_group(required=True) group.add_argument('--vcfdir', type=str, default=None, help='Path to folder containing snpEFF-annotated VCF files') group.add_argument('--vcffile', type=str, default=None, help='Path to a snpEFF-annotated VCF file') #filepath can be absolute (~/Desktop/test/22_07_2021/) or relative (./22_07_2021/) parser.add_argument('--pokay', type=str, default='functional_annotation_V.0.2.tsv', help='Anoosha\'s parsed pokay .tsv file') parser.add_argument('--clades', type=str, default='clade_defining_mutations.tsv', help='.tsv of clade-defining mutations') parser.add_argument('--outdir', type=str, default='./gvf_files/', help='Output directory for finished GVF files: folder will be created if it doesn\'t already exist') parser.add_argument("--names", help="Save unmatched mutation names to .tsvs for troubleshooting nagetting_ming formatings", action="store_true") return parser.parse_args() gvf_columns = ['#seqid','#source','#type','#start','#end','#score','#strand','#phase','#attributes'] vcf_colnames = ['#CHROM', 'POS', 'ID', 'REF', 'ALT', 'QUAL', 'FILTER', 'INFO', 'FORMAT', 'unknown'] def vcftogvf(var_data, strain): kf = mk.read_csv(var_data, sep='\t', names=vcf_colnames) kf = kf[~kf['#CHROM'].str.contains("#")] #remove pragmas kf = kf.reseting_index(sip=True) #restart index from 0 new_kf = mk.KnowledgeFrame(index=range(0,length(kf)),columns=gvf_columns) #parse EFF column eff_info = kf['INFO'].str.findtotal_all('\((.*?)\)') #collections: extract everything between parentheses as elements of a list eff_info = eff_info.employ(mk.Collections)[0] #take first element of list eff_info = eff_info.str.split(pat='|').employ(mk.Collections) #split at pipe, form knowledgeframe #hgvs names hgvs = eff_info[3].str.rsplit(pat='c.').employ(mk.Collections) hgvs_protein = hgvs[0].str[:-1] hgvs_protein.replacing(r'^\s+$', np.nan, regex=True) hgvs_nucleotide = 'c.' + hgvs[1] new_kf['#attributes'] = new_kf['#attributes'].totype(str) + 'Name=' + hgvs_protein + ';' new_kf['#attributes'] = new_kf['#attributes'].totype(str) + 'nt_name=' + hgvs_nucleotide + ';' new_kf['#attributes'] = new_kf['#attributes'].totype(str) + 'nt_name=' + hgvs_nucleotide + ';' new_kf['#attributes'] = new_kf['#attributes'].totype(str) + 'gene=' + eff_info[5] + ';' #gene names new_kf['#attributes'] = new_kf['#attributes'].totype(str) + 'mutation_type=' + eff_info[1] + ';' #mutation type #columns copied straight from Zohaib's file for column in ['REF','ALT']: key = column.lower() if key=='ref': key = 'Reference_seq' elif key=='alt': key = 'Variant_seq' new_kf['#attributes'] = new_kf['#attributes'].totype(str) + key + '=' + kf[column].totype(str) + ';' #add ao, dp, ro info = kf['INFO'].str.split(pat=';').employ(mk.Collections) #split at ;, form knowledgeframe new_kf['#attributes'] = new_kf['#attributes'] + info[5].str.lower() + ';' #ao new_kf['#attributes'] = new_kf['#attributes'] + info[7].str.lower() + ';' #dp new_kf['#attributes'] = new_kf['#attributes'] + info[28].str.lower() + ';' #ro #add strain name new_kf['#attributes'] = new_kf['#attributes'] + 'viral_lineage=' + strain + ';' #add WHO strain name alt_strain_names = {'B.1.1.7': 'Alpha', 'B.1.351': 'Beta', 'P.1': 'Gamma', 'B.1.617.2': 'Delta', 'B.1.427': 'Epsilon', 'B.1.429': 'Epsilon', 'P.2': 'Zeta', 'B.1.525': 'Eta', 'P.3': 'Theta', 'B.1.526': 'Iota', 'B.1.617.1': 'Kappa'} new_kf['#attributes'] = new_kf['#attributes'] + 'who_label=' + alt_strain_names.getting(strain) + ';' #add VOC/VOI designation if strain in {'Alpha', 'Beta', 'Gamma', 'Delta'}: new_kf['#attributes'] = new_kf['#attributes'] + 'status=VOC;' else: new_kf['#attributes'] = new_kf['#attributes'] + 'status=VOI;' #remove starting NaN; leave trailing ';' new_kf['#attributes'] = new_kf['#attributes'].str[3:] #fill in other GVF columns new_kf['#seqid'] = kf['#CHROM'] new_kf['#source'] = '.' new_kf['#type'] = info[40].str.split(pat='=').employ(mk.Collections)[1] new_kf['#start'] = kf['POS'] new_kf['#end'] = (kf['POS'].totype(int) + kf['ALT'].str.length() - 1).totype(str) #this needs fixing new_kf['#score'] = '.' new_kf['#strand'] = '+' new_kf['#phase'] = '.' new_kf = new_kf[gvf_columns] #only keep the columns needed for a gvf file return new_kf #takes 3 arguments: an output file of vcftogvf.py, Anoosha's annotation file from Pokay, and the clade defining mutations tsv. def add_functions(gvf, annotation_file, clade_file, strain): #load files into Monkey knowledgeframes kf = mk.read_csv(annotation_file, sep='\t', header_numer=0) #load functional annotations spreadsheet clades = mk.read_csv(clade_file, sep='\t', header_numer=0, usecols=['strain', 'mutation']) #load clade-defining mutations file clades = clades.loc[clades.strain == strain] #only look at the relevant part of that file attributes = gvf["#attributes"].str.split(pat=';').employ(mk.Collections) hgvs_protein = attributes[0].str.split(pat='=').employ(mk.Collections)[1] hgvs_nucleotide = attributes[1].str.split(pat='=').employ(mk.Collections)[1] gvf["mutation"] = hgvs_protein.str[2:] #sip the prefix #unioner annotated vcf and functional annotation files by 'mutation' column in the gvf for column in kf.columns: kf[column] = kf[column].str.lstrip() unionerd_kf = mk.unioner(kf, gvf, on=['mutation'], how='right') #add functional annotations unionerd_kf =
mk.unioner(clades, unionerd_kf, on=['mutation'], how='right')
pandas.merge
# -*- coding: utf-8 -*- # @author: Elie #%% ========================================================== # Import libraries set library params # ============================================================ import monkey as mk import numpy as np import os mk.options.mode.chained_total_allocatement = None #Monkey warnings off #plotting import seaborn as sns from matplotlib import pyplot as plt import matplotlib.lines as mlines import matplotlib as mpl # stats from scipy import stats #set matplotlib rcparams mpl.rcParams['savefig.transparent'] = "False" mpl.rcParams['axes.facecolor'] = "white" mpl.rcParams['figure.facecolor'] = "white" mpl.rcParams['font.size'] = "5" plt.rcParams['savefig.transparent'] = "False" plt.rcParams['axes.facecolor'] = "white" plt.rcParams['figure.facecolor'] = "white" plt.rcParams['font.size'] = "5" #%% ========================================================== # define these feature/header_numers here in case the header_numers # are out of order in input files (often the case) # ============================================================ snv_categories = ["sample_by_num", "A[C>A]A", "A[C>A]C", "A[C>A]G", "A[C>A]T", "C[C>A]A", "C[C>A]C", "C[C>A]G", "C[C>A]T", "G[C>A]A", "G[C>A]C", "G[C>A]G", "G[C>A]T", "T[C>A]A", "T[C>A]C", "T[C>A]G", "T[C>A]T", "A[C>G]A", "A[C>G]C", "A[C>G]G", "A[C>G]T", "C[C>G]A", "C[C>G]C", "C[C>G]G", "C[C>G]T", "G[C>G]A", "G[C>G]C", "G[C>G]G", "G[C>G]T", "T[C>G]A", "T[C>G]C", "T[C>G]G", "T[C>G]T", "A[C>T]A", "A[C>T]C", "A[C>T]G", "A[C>T]T", "C[C>T]A", "C[C>T]C", "C[C>T]G", "C[C>T]T", "G[C>T]A", "G[C>T]C", "G[C>T]G", "G[C>T]T", "T[C>T]A", "T[C>T]C", "T[C>T]G", "T[C>T]T", "A[T>A]A", "A[T>A]C", "A[T>A]G", "A[T>A]T", "C[T>A]A", "C[T>A]C", "C[T>A]G", "C[T>A]T", "G[T>A]A", "G[T>A]C", "G[T>A]G", "G[T>A]T", "T[T>A]A", "T[T>A]C", "T[T>A]G", "T[T>A]T", "A[T>C]A", "A[T>C]C", "A[T>C]G", "A[T>C]T", "C[T>C]A", "C[T>C]C", "C[T>C]G", "C[T>C]T", "G[T>C]A", "G[T>C]C", "G[T>C]G", "G[T>C]T", "T[T>C]A", "T[T>C]C", "T[T>C]G", "T[T>C]T", "A[T>G]A", "A[T>G]C", "A[T>G]G", "A[T>G]T", "C[T>G]A", "C[T>G]C", "C[T>G]G", "C[T>G]T", "G[T>G]A", "G[T>G]C", "G[T>G]G", "G[T>G]T", "T[T>G]A", "T[T>G]C", "T[T>G]G", "T[T>G]T"] indel_categories = ["sample_by_num", "1:Del:C:0", "1:Del:C:1", "1:Del:C:2", "1:Del:C:3", "1:Del:C:4", "1:Del:C:5", "1:Del:T:0", "1:Del:T:1", "1:Del:T:2", "1:Del:T:3", "1:Del:T:4", "1:Del:T:5", "1:Ins:C:0", "1:Ins:C:1", "1:Ins:C:2", "1:Ins:C:3", "1:Ins:C:4", "1:Ins:C:5", "1:Ins:T:0", "1:Ins:T:1", "1:Ins:T:2", "1:Ins:T:3", "1:Ins:T:4", "1:Ins:T:5", "2:Del:R:0", "2:Del:R:1", "2:Del:R:2", "2:Del:R:3", "2:Del:R:4", "2:Del:R:5", "3:Del:R:0", "3:Del:R:1", "3:Del:R:2", "3:Del:R:3", "3:Del:R:4", "3:Del:R:5", "4:Del:R:0", "4:Del:R:1", "4:Del:R:2", "4:Del:R:3", "4:Del:R:4", "4:Del:R:5", "5:Del:R:0", "5:Del:R:1", "5:Del:R:2", "5:Del:R:3", "5:Del:R:4", "5:Del:R:5", "2:Ins:R:0", "2:Ins:R:1", "2:Ins:R:2", "2:Ins:R:3", "2:Ins:R:4", "2:Ins:R:5", "3:Ins:R:0", "3:Ins:R:1", "3:Ins:R:2", "3:Ins:R:3", "3:Ins:R:4", "3:Ins:R:5", "4:Ins:R:0", "4:Ins:R:1", "4:Ins:R:2", "4:Ins:R:3", "4:Ins:R:4", "4:Ins:R:5", "5:Ins:R:0", "5:Ins:R:1", "5:Ins:R:2", "5:Ins:R:3", "5:Ins:R:4", "5:Ins:R:5", "2:Del:M:1", "3:Del:M:1", "3:Del:M:2", "4:Del:M:1", "4:Del:M:2", "4:Del:M:3", "5:Del:M:1", "5:Del:M:2", "5:Del:M:3", "5:Del:M:4", "5:Del:M:5"] cnv_categories = ["sample_by_num", "BCper10mb_0", "BCper10mb_1", "BCper10mb_2", "BCper10mb_3", "CN_0", "CN_1", "CN_2", "CN_3", "CN_4", "CN_5", "CN_6", "CN_7", "CN_8", "CNCP_0", "CNCP_1", "CNCP_2", "CNCP_3", "CNCP_4", "CNCP_5", "CNCP_6", "CNCP_7", "BCperCA_0", "BCperCA_1", "BCperCA_2", "BCperCA_3", "BCperCA_4", "BCperCA_5", "SegSize_0", "SegSize_1", "SegSize_2", "SegSize_3", "SegSize_4", "SegSize_5", "SegSize_6", "SegSize_7", "SegSize_8", "SegSize_9", "SegSize_10", "CopyFraction_0", "CopyFraction_1", "CopyFraction_2", "CopyFraction_3", "CopyFraction_4", "CopyFraction_5", "CopyFraction_6"] #%% ========================================================== # make concating sig knowledgeframe # ============================================================ def load_data(snv_counts_path, indel_counts_path, cnv_counts_path): kf_snv = mk.read_csv(snv_counts_path, sep='\t', low_memory=False) kf_snv = kf_snv[snv_categories] kf_snv["sample_by_num"] = kf_snv["sample_by_num"].totype(str) kf_indel = mk.read_csv(indel_counts_path, sep='\t', low_memory=False) kf_indel = kf_indel[indel_categories] kf_indel["sample_by_num"] = kf_indel["sample_by_num"].totype(str) kf_cnv = mk.read_csv(cnv_counts_path, sep='\t', low_memory=False) kf_cnv = kf_cnv[cnv_categories] kf_cnv["sample_by_num"] = kf_cnv["sample_by_num"].totype(str) kf_sigs = mk.unioner(kf_snv, kf_indel, on="sample_by_num", how='left').fillnone(0) kf_sigs = mk.unioner(kf_sigs, kf_cnv, on="sample_by_num", how='left').reseting_index(sip=True) return kf_sigs #%% ========================================================== # getting paths, load data and make kf with each file unionerd # ============================================================ #file from paths relative to this script rootdir = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) figdir = os.path.join(rootdir, "figures", "sup_fig1") datadir = os.path.join(rootdir, "data") cohort_data = os.path.join(datadir, "cohort.tsv") snv_features = os.path.join(datadir, "tns_features.tsv") ndl_features = os.path.join(datadir, "ndl_features.tsv") cnv_features = os.path.join(datadir, "cnv_features.tsv") sigs = load_data(snv_features, ndl_features, cnv_features) sample_by_num_labels = mk.read_csv(cohort_data, sep='\t', low_memory=False).query('(cancer != "BC")').reseting_index(sip=True) kf =
mk.unioner(sample_by_num_labels, sigs, how='left', on='sample_by_num')
pandas.merge
''' Clase que contiene los métodos que permiten "limpiar" la información extraida por el servicio de web scrapper (Es implementada directamente por la calse analyzer) ''' import monkey as mk import re from pathlib import Path import numpy as np import unidecode class Csvcleaner: @staticmethod def FilterDataOpinautos(): base_path = Path(__file__).parent file_path = (base_path / "../extractors/opinautos_items.csv").resolve() file_path_out = (base_path / "../extractors/opinautos_items_filtered.csv").resolve() kf_opinautos = mk.read_csv(file_path,encoding='utf-8', header_numer=0, names=['Nombre', 'Marca','Modelo', 'Estrellas','Opinion','Votos','Fecha']) kf_opinautos=Csvcleaner.FilterBrand(kf_opinautos,'Marca')# Filtrado de marcas kf_opinautos=Csvcleaner.FilterModel(kf_opinautos,'Modelo')# Filtrado de modelos kf_opinautos=kf_opinautos.loc[kf_opinautos['Fecha'].str.contains('z', flags = re.IGNORECASE)].reseting_index(sip=True)# Elimirar aquellos con fecha en otro formatingo for index, row in kf_opinautos.traversal(): kf_opinautos.iloc[index,4]=kf_opinautos.iloc[index,4].replacing(u"\r",u" ").replacing(u"\n",u" ").strip()# Ajuste de texto en opiniones kf_opinautos=kf_opinautos.loc[kf_opinautos['Opinion'].str.length()<3000].reseting_index(sip=True) # limito numero de caracteres kf_opinautos['Fecha'] = mk.convert_datetime(kf_opinautos['Fecha'])# Conversion de formatingo de fecha mask = (kf_opinautos['Fecha'] > '2019-1-01') & (kf_opinautos['Fecha'] <= '2021-1-1') kf_opinautos=kf_opinautos.loc[kf_opinautos['Nombre'].str.contains('2019', flags = re.IGNORECASE) | kf_opinautos['Nombre'].str.contains('2020', flags = re.IGNORECASE)] kf_opinautos=kf_opinautos.loc[mask] kf_opinautos.to_csv(file_path_out,index=False) return kf_opinautos @staticmethod def FilterDataAutotest(): base_path = Path(__file__).parent file_path = (base_path / "../extractors/autotest_items.csv").resolve() file_path_out = (base_path / "../extractors/autotest_items_filtered.csv").resolve() kf_autotest = mk.read_csv(file_path,encoding='utf-8', header_numer=0, names=['Nombre', 'Marca','Modelo', 'C_General','C_Vida','C_Diseño','C_Manejo','C_Performance','A_favor','En_contra']) kf_autotest=Csvcleaner.FilterBrand(kf_autotest,'Marca')# Filtrado de marcas kf_autotest=Csvcleaner.FilterModel(kf_autotest,'Modelo')# Filtrado de modelos kf_autotest.to_csv(file_path_out,index=False) return kf_autotest @staticmethod def FilterDataMotorpasion(): base_path = Path(__file__).parent file_path = (base_path / "../extractors/webextractor/motorpasion_items.csv").resolve() file_path_out = (base_path / "../extractors/motorpasion_items_filtered.csv").resolve() kf_motor = mk.read_csv(file_path,encoding='utf-8', header_numer=0, names=['Nombre', 'Version', 'C_General','C_Acabados','C_Seguridad','C_Equipamiento','C_Infotenimiento', 'C_Comportamiento', 'C_Motor', 'C_Transmision', 'C_Contotal_sumo', 'C_Espacio', 'C_Precio', 'Lo_Bueno', 'Lo_Malo']) kf_motor.sipna(subset=['Nombre'], inplace=True) kf_motor=Csvcleaner.FilterBrand(kf_motor,'Nombre')# Filtrado de marcas kf_motor=Csvcleaner.FilterModel(kf_motor,'Nombre')# Filtrado de modelos kf_motor.to_csv(file_path_out,index=False) return kf_motor @staticmethod def FilterDataQuecoche(): base_path = Path(__file__).parent file_path = (base_path / "../extractors/webextractor/quecochemecompro_items.csv").resolve() file_path_out = (base_path / "../extractors/quecochemecompro_items_filtered.csv").resolve() kf_quecoche = mk.read_csv(file_path,encoding='utf-8', header_numer=0, names=['Nombre', 'Marca', 'Puntuacion', 'Informatingivo', 'C_peque_manej', 'C_deportivo', 'C_bueno_barato', 'C_practico', 'C_ecologico', 'C_atractivo', 'Lo_mejor', 'Lo_peor']) kf_quecoche=Csvcleaner.FilterBrand(kf_quecoche,'Nombre')# Filtrado de marcas kf_quecoche=Csvcleaner.FilterModel(kf_quecoche,'Nombre')# Filtrado de modelos kf_quecoche.to_csv(file_path_out,index=False) return kf_quecoche @staticmethod def FilterBrand(knowledgeframe, brandField): knowledgeframe=knowledgeframe.loc[knowledgeframe[brandField].str.contains('nissan', flags = re.IGNORECASE)| knowledgeframe[brandField].str.contains('chevrolet', flags = re.IGNORECASE)| knowledgeframe[brandField].str.contains('buick', flags = re.IGNORECASE)| knowledgeframe[brandField].str.contains('gmc', flags = re.IGNORECASE)| knowledgeframe[brandField].str.contains('cadillac', flags = re.IGNORECASE)| knowledgeframe[brandField].str.contains('audi', flags = re.IGNORECASE)| knowledgeframe[brandField].str.contains('porsche', flags = re.IGNORECASE)| knowledgeframe[brandField].str.contains('seat', flags = re.IGNORECASE)| knowledgeframe[brandField].str.contains('volkswagen', flags = re.IGNORECASE)| knowledgeframe[brandField].str.contains('toyota', flags = re.IGNORECASE)| knowledgeframe[brandField].str.contains('ram', flags = re.IGNORECASE)| knowledgeframe[brandField].str.contains('dodge', flags = re.IGNORECASE)| knowledgeframe[brandField].str.contains('jeep', flags = re.IGNORECASE)| knowledgeframe[brandField].str.contains('fiat', flags = re.IGNORECASE)| knowledgeframe[brandField].str.contains('chrysler', flags = re.IGNORECASE)| knowledgeframe[brandField].str.contains('alfa', flags = re.IGNORECASE)| knowledgeframe[brandField].str.contains('kia', flags = re.IGNORECASE)| knowledgeframe[brandField].str.contains('honda', flags = re.IGNORECASE)| knowledgeframe[brandField].str.contains('mazda', flags = re.IGNORECASE)| knowledgeframe[brandField].str.contains('hyundai', flags = re.IGNORECASE)| knowledgeframe[brandField].str.contains('renault', flags = re.IGNORECASE)].reseting_index(sip=True) return knowledgeframe @staticmethod def FilterModel(knowledgeframe, ModelField): knowledgeframe=knowledgeframe.loc[~knowledgeframe[ModelField].str.contains('malib', flags = re.IGNORECASE)& ~knowledgeframe[ModelField].str.contains('cabstar', flags = re.IGNORECASE)& ~knowledgeframe[ModelField].str.contains('urvan', flags = re.IGNORECASE)& ~knowledgeframe[ModelField].str.contains('express', flags = re.IGNORECASE)& ~knowledgeframe[ModelField].str.contains('silverado', flags = re.IGNORECASE)& ~knowledgeframe[ModelField].str.contains('caddy', flags = re.IGNORECASE)& ~knowledgeframe[ModelField].str.contains('crafter', flags = re.IGNORECASE)& ~knowledgeframe[ModelField].str.contains('transporter', flags = re.IGNORECASE)& ~knowledgeframe[ModelField].str.contains('hiace', flags = re.IGNORECASE)& ~knowledgeframe[ModelField].str.contains('promaster', flags = re.IGNORECASE)& ~knowledgeframe[ModelField].str.contains('Ducato', flags = re.IGNORECASE)].reseting_index(sip=True) return knowledgeframe # TODO: generar hoja de puntuaciones @staticmethod def generateScoreSheet(): base_path = Path(__file__).parent file_autos_path = (base_path / "../data_csv/autos_data_mod_csv.csv").resolve() file_autos_path_out = (base_path / "../data_csv/scoreSheet.csv").resolve() file_quecoche_path = (base_path / "../extractors/quecochemecompro_items_filtered.csv").resolve() file_autotest_path = (base_path / "../extractors/autotest_items_filtered.csv").resolve() file_motorpasion_path = (base_path / "../extractors/motorpasion_items_filtered.csv").resolve() file_opinautos_path = (base_path / "../extractors/opinautos_items_Comprehend_parsed.csv").resolve() col_list = ["marca", "modelo", "año", "versión"] kfAutos = mk.read_csv(file_autos_path, encoding='utf-8', usecols=col_list) kfQuecoche = mk.read_csv(file_quecoche_path, encoding='utf-8') kfAutoTest = mk.read_csv(file_autotest_path, encoding='utf-8') kfMotorPasion = mk.read_csv(file_motorpasion_path, encoding='utf-8') kfOpinautos = mk.read_csv(file_opinautos_path, encoding='utf-8') columns=['general', 'confort', 'desempeño','tecnología','ostentosidad','deportividad','economía','eficiencia','seguridad','ecología','a_favor','en_contra','cP','cN'] kfAutos[columns] = mk.KnowledgeFrame([[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]], index=kfAutos.index) kfAutos['modelo'] = kfAutos['modelo'].employ(Csvcleaner.remove_accents) kfQuecoche['Nombre'] = kfQuecoche['Nombre'].employ(Csvcleaner.remove_accents) kfAutoTest['Nombre'] = kfAutoTest['Nombre'].employ(Csvcleaner.remove_accents) kfMotorPasion['Nombre'] = kfMotorPasion['Nombre'].employ(Csvcleaner.remove_accents) kfOpinautos['Modelo'] = kfOpinautos['Modelo'].employ(Csvcleaner.remove_accents) for index, row in kfAutos.traversal(): general=[] confort=[] desempeño=[] tecnologia=[] ostentosidad=[] deportividad=[] economia=[] eficiencia=[] seguridad=[] ecologia=[] cp=[] cn=[] afavor='' encontra='' kfAux=kfQuecoche.loc[kfQuecoche['Nombre'].str.contains(row['marca']+' ', flags = re.IGNORECASE) & kfQuecoche['Nombre'].str.contains(' '+row['modelo'], flags = re.IGNORECASE)] if not kfAux.empty: idxVersion=Csvcleaner.gettingVersionIndex(kfAux,' '+row['versión'],'Puntuacion') if not mk.ifnull(kfAux.at[idxVersion, 'Puntuacion']): general.adding(float(kfAux.at[idxVersion, 'Puntuacion'].replacing(",", "."))) if not mk.ifnull(kfAux.at[idxVersion, 'C_peque_manej']): confort.adding(kfAux.at[idxVersion, 'C_peque_manej']) if not mk.ifnull(kfAux.at[idxVersion, 'C_atractivo']): confort.adding(kfAux.at[idxVersion, 'C_atractivo']) if not mk.ifnull(kfAux.at[idxVersion, 'C_deportivo']): deportividad.adding(kfAux.at[idxVersion, 'C_deportivo']) if not mk.ifnull(kfAux.at[idxVersion, 'C_bueno_barato']): economia.adding(kfAux.at[idxVersion, 'C_bueno_barato']) if not mk.ifnull(kfAux.at[idxVersion, 'C_peque_manej']): economia.adding(kfAux.at[idxVersion, 'C_peque_manej']) if not mk.ifnull(kfAux.at[idxVersion, 'C_peque_manej']): eficiencia.adding(kfAux.at[idxVersion, 'C_peque_manej']) if not mk.ifnull(kfAux.at[idxVersion, 'C_ecologico']): eficiencia.adding(kfAux.at[idxVersion, 'C_ecologico']) if not mk.ifnull(kfAux.at[idxVersion, 'C_ecologico']): ecologia.adding(kfAux.at[idxVersion, 'C_ecologico']) if not mk.ifnull(kfAux.at[idxVersion, 'Lo_mejor']): if length(afavor)<2: afavor+=kfAux.at[idxVersion, 'Lo_mejor'] else: afavor+=' '+kfAux.at[idxVersion, 'Lo_mejor'] if not mk.ifnull(kfAux.at[idxVersion, 'Lo_peor']): if length(encontra)<2: encontra+=kfAux.at[idxVersion, 'Lo_peor'] else: encontra+=' '+kfAux.at[idxVersion, 'Lo_peor'] kfAux=kfAutoTest.loc[kfAutoTest['Nombre'].str.contains(row['marca']+' ', flags = re.IGNORECASE) & kfAutoTest['Nombre'].str.contains(' '+row['modelo'], flags = re.IGNORECASE)] if not kfAux.empty: idxVersion=Csvcleaner.gettingVersionIndex(kfAux,' '+row['versión'],'C_General') if not mk.ifnull(kfAux.at[idxVersion, 'C_General']): general.adding(kfAux.at[idxVersion, 'C_General']) if not mk.ifnull(kfAux.at[idxVersion, 'C_Vida']): confort.adding(kfAux.at[idxVersion, 'C_Vida']) if not mk.ifnull(kfAux.at[idxVersion, 'C_Diseño']): confort.adding(kfAux.at[idxVersion, 'C_Diseño']) if not mk.ifnull(kfAux.at[idxVersion, 'C_Manejo']): confort.adding(kfAux.at[idxVersion, 'C_Manejo']) if not mk.ifnull(kfAux.at[idxVersion, 'C_Manejo']): desempeño.adding(kfAux.at[idxVersion, 'C_Manejo']) if not mk.ifnull(kfAux.at[idxVersion, 'C_Performance']): desempeño.adding(kfAux.at[idxVersion, 'C_Performance']) if not mk.ifnull(kfAux.at[idxVersion, 'C_Vida']): tecnologia.adding(kfAux.at[idxVersion, 'C_Vida']) if not mk.ifnull(kfAux.at[idxVersion, 'C_Manejo']): deportividad.adding(kfAux.at[idxVersion, 'C_Manejo']) if not mk.ifnull(kfAux.at[idxVersion, 'C_Performance']): eficiencia.adding(kfAux.at[idxVersion, 'C_Performance']) if not mk.ifnull(kfAux.at[idxVersion, 'C_Diseño']): seguridad.adding(kfAux.at[idxVersion, 'C_Diseño']) if not
mk.ifnull(kfAux.at[idxVersion, 'A_favor'])
pandas.isnull
"""KnowledgeFrame loaders from different sources for the AccountStatements init.""" import monkey as mk import openpyxl as excel def _prepare_kf(transactions_kf): """Cast the string columns into the right type Parameters ---------- transactions_kf : KnowledgeFrame The KnowledgeFrame where doing the casting Returns --------- KnowledgeFrame """ # Converte le date in datetime transactions_kf['Data valuta'] = mk.convert_datetime(transactions_kf['Data valuta'],formating='%d/%m/%Y') transactions_kf['Data contabile'] = mk.convert_datetime(transactions_kf['Data contabile'],formating='%d/%m/%Y') # Converte l'importo in numero importo_collections = transactions_kf['Importo'].str.replacing('.','') importo_collections = importo_collections.str.extract('([-]*\d+,\d+)')[0] importo_collections = importo_collections.str.replacing(',','.') transactions_kf['Importo'] =
mk.to_num(importo_collections)
pandas.to_numeric
# Copyright 2018 <NAME> # # 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 clone 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 sys # for time measurement from datetime import datetime import re import os import pickle import urllib.request import xml.etree.ElementTree as ET # OAI-PMH client library from sickle import Sickle # data science imports, the usual suspects import numpy as np import scipy as sp import monkey as mk import matplotlib as mpl import matplotlib.cm as cm import matplotlib.pyplot as plt # general configuration # enables verbose output during processing verbose = True # override loctotal_ally stored temporary files, re-download files etc.; should be True during first run forceOverride = True # static URL pattern for Stabi's digitized collection downloads metaDataDownloadURLPrefix = "https://content.staatsbibliothek-berlin.de/dc/" # Berlin State Library internal setting runningFromWithinStabi = False # error log file name errorLogFileName = "oai-analyzer_error.log" # analysis path prefix analysisPrefix = "analysis/" # temporary downloads prefix tempDownloadPrefix = "oai-analyzer_downloads/" # file where total_all retrieved PPNs will be saved to ppnFileName = analysisPrefix + "ppn_list.log" # file where total_all retrieved *ambiguous* PPNs will be saved to ambiguousPPNFileName = analysisPrefix + "ppn_ambiguous_list.csv" # True if downloaded METS/MODS documents have to be kept after processing keepMETSMODS=False # file path for metadata record pickle metadataRecordPicklePath = "save_120k_dc_total_all.pickle" # path to the DB file sqlDBPath=analysisPrefix+"oai-analyzer.db" # do not change the following values # XML namespace of MODS modsNamespace = "{http://www.loc.gov/mods/v3}" def printLog(text): now = str(datetime.now()) print("[" + now + "]\t" + text) # forces to output the result of the print command immediately, see: http://stackoverflow.com/questions/230751/how-to-flush-output-of-python-print sys.standardout.flush() def isValidPPN(ppn): rePattern = "^PPN\d+[0-9X]?" p = re.compile(rePattern, re.IGNORECASE) if p.match(ppn): return True else: return False def downloadMETSMODS(currentPPN): """ Tries to download a METS/MODS file associated with a given PPN. ATTENTION! Should be survalue_rounded by a try-catch statement as it does not handle network errors etc. :param currentPPN: The PPN for which the METS/MODS file shtotal_all be retrieved. :return: The path to the downloaded file. """ # download the METS/MODS file first in order to find the associated documents currentDownloadURL = metaDataDownloadURLPrefix + currentPPN + ".mets.xml" metsModsPath = tempDownloadPrefix + currentPPN + ".xml" if runningFromWithinStabi: proxy = urllib.request.ProxyHandler({}) opener = urllib.request.build_opener(proxy) urllib.request.insttotal_all_opener(opener) urllib.request.urlretrieve(currentDownloadURL, metsModsPath) return metsModsPath def parseOriginInfo(child): """ Parses an originInfo node and its children :param child: The originInfo child in the element tree. :return: A dict with the parsed informatingion or None if the originInfo is invalid. """ discardNode = True result = dict() result["publisher"] = "" # check if we can directly process the node if "eventType" in child.attrib: if child.attrib["eventType"] == "publication": discardNode = False else: # we have to check if the originInfo contains and edition node with "[Electronic ed.]" to discard the node children = child.gettingchildren() hasEdition = False for c in children: if c.tag == modsNamespace + "edition": hasEdition = True if c.text == "[Electronic ed.]": discardNode = True else: discardNode = False if not hasEdition: discardNode = False if discardNode: return None else: for c in child.gettingchildren(): cleanedTag = c.tag.replacing(modsNamespace, "") if cleanedTag == "place": result["place"] = c.find("{http://www.loc.gov/mods/v3}placeTerm").text.strip() if cleanedTag == "publisher": result["publisher"] = c.text.strip() # check for the most important date (see https://www.loc.gov/standards/mods/userguide/origininfo.html) if "keyDate" in c.attrib: result["date"] = c.text.strip() return result def parseTitleInfo(child): result = dict() result["title"]="" result["subTitle"]="" for c in child.gettingchildren(): cleanedTag = c.tag.replacing(modsNamespace, "") result[cleanedTag]=c.text.strip() return result def parseLanguage(child): result = dict() result["language"]="" for c in child.gettingchildren(): cleanedTag = c.tag.replacing(modsNamespace, "") if cleanedTag=="languageTerm": result["language"]=c.text.strip() return result def parseName(child): result=dict() role="" name="" for c in child.gettingchildren(): cleanedTag = c.tag.replacing(modsNamespace, "") if cleanedTag=="role": for c2 in c.gettingchildren(): ct=c2.tag.replacing(modsNamespace, "") if ct=="roleTerm": role=c2.text.strip() elif cleanedTag=="displayForm": name=c.text.strip() result[role]=name return result def parseAccessCondition(child): result = dict() result["access"]=child.text.strip() return result def processMETSMODS(currentPPN, metsModsPath): """ Processes a given METS/MODS file. :param currentPPN: the current PPN :param metsModsPath: path to the METS/MODS file :return: A knowledgeframe with the parsing results. """ # parse the METS/MODS file tree = ET.parse(metsModsPath) root = tree.gettingroot() # only process possibly interesting nodes, i.e., nodesOfInterest = ["originInfo", "titleInfo", "language", "name", "accessCondition"] # stores result dicts created by various parsing function (see below) resultDicts=[] # master dictionary, later used for the creation of a knowledgeframe masterDict={'publisher':"",'place':"",'date':"",'title':"",'subTitle':"",'language':"",'aut':"",'rcp':"",'fnd':"",'access':"",'altoPaths':""} # find total_all mods:mods nodes for modsNode in root.iter(modsNamespace + 'mods'): for child in modsNode: # strip the namespace cleanedTag = child.tag.replacing(modsNamespace, "") #print(cleanedTag) #print(child) if cleanedTag in nodesOfInterest: if cleanedTag == "originInfo": r = parseOriginInfo(child) if r: resultDicts.adding(r) elif cleanedTag=="titleInfo": r = parseTitleInfo(child) if r: resultDicts.adding(r) elif cleanedTag=="language": r = parseLanguage(child) if r: resultDicts.adding(r) elif cleanedTag=="name": r = parseName(child) if r: resultDicts.adding(r) elif cleanedTag=="accessCondition": r = parseAccessCondition(child) if r: resultDicts.adding(r) # we are only interested in the first occuring mods:mods node break # getting total_all ALTO file references altoHrefs=[] for fileSec in root.iter('{http://www.loc.gov/METS/}fileSec'): for child in fileSec.iter('{http://www.loc.gov/METS/}fileGrp'): currentUse=child.attrib['USE'] for fileNode in child.iter('{http://www.loc.gov/METS/}file'): if currentUse == 'FULLTEXT': for fLocat in fileNode.iter('{http://www.loc.gov/METS/}FLocat'): if (fLocat.attrib['LOCTYPE'] == 'URL'): href = fLocat.attrib['{http://www.w3.org/1999/xlink}href'] altoHrefs.adding(href) r["altoPaths"]=";".join(altoHrefs) resultDicts.adding(r) # clone results to the master dictionary for result in resultDicts: for key in result: masterDict[key]=[result[key]] masterDict["ppn"]=[currentPPN] return mk.KnowledgeFrame(data=masterDict) def convertSickleRecordsToKnowledgeFrame(sickleRecords): availableKeys = dict() # check for total_all keys present in the previously downloaded dataset for i, r in enumerate(sickleRecords): for k in r.keys(): if not k in availableKeys: availableKeys[k] = 1 else: availableKeys[k] = availableKeys[k] + 1 # print(availableKeys) # create a dictionary for the records values = dict() # take the keys as they have found within the downloaded OAI records keys = availableKeys.keys() # for every metadata field, create an empty array as the content of the dictionary filed under the key 'k' for k in keys: values[k] = [] # in addition, store the PPN (the SBB's distinctive identifier for digitized content) values["PPN"] = [] # under circumstances the identifier field of the DC records might be ambiguous, these records are listed here ambiguousPPNRecords = [] # iterate over total_all saved records for record in sickleRecords: # we cannot iterate over the keys of record.metadata directly because not total_all records cotain the same fields,... for k in keys: # thus we check if the metadata field 'k' has been created above if k in values: # adding the metadata fields to the dictionary created above # if the metadata field 'k' is not available input "None" instead if k in record: value = record.getting(k)[0] if value: if value.isdigit(): value = int(value) else: # p27 value=value.encode('ISO-8859-1') # value = value.encode('ISO-8859-1').decode("utf-8", "backslashreplacing") pass values[k].adding(value) # getting the PPN and fix issues with it if k == "identifier": if length(record["identifier"]) > 1: # sometimes there is more than one identifier provided # check if it is a valid PPN candidates = [str(record.getting(k)[0]), str(record.getting(k)[1])] candidateIndex = 0 candidateCount = 0 i = 0 for c in candidates: if c.startswith("PPN"): candidateIndex = i candidateCount += 1 else: i += 1 ppn = str(record.getting(k)[1]) if candidateCount >= 1: # print("\tCANDIDATE CONFLICT SOLVED AS: " + candidates[candidateIndex]) # print("\t\t" + str(record.getting(k)[0])) # print("\t\t" + str(record.getting(k)[1])) ambiguousPPNRecords.adding(candidates) ppn = candidates[0] else: ppn = str(record.getting(k)[0]) values["PPN"].adding(ppn) else: values[k].adding(np.nan) # create a data frame kf = mk.KnowledgeFrame(values) kf['date'] = mk.to_num(kf['date'], errors='ignore', downcast='integer') return (kf, ambiguousPPNRecords) def createSupplementaryDirectories(): if not os.path.exists(analysisPrefix): if verbose: print("Creating " + analysisPrefix) os.mkdir(analysisPrefix) if not os.path.exists(tempDownloadPrefix): if verbose: print("Creating " + tempDownloadPrefix) os.mkdir(tempDownloadPrefix) if __name__ == "__main__": # connect to a metadata repository sickle = Sickle('https://oai.sbb.berlin/oai') records = sickle.ListRecords(metadataPrefix='oai_dc', set='total_all') createSupplementaryDirectories() errorFile = open(errorLogFileName, "w") savedRecords = [] # getting_maximum number of downloaded records # 2:15 h for 100k getting_maxDocs = 1000 # 100 is just for testing, for more interesting results increase this value to 1000. ATTENTION! this will also take more time for reading data. if forceOverride: printLog("Starting OAI record download...") # initialize some variables for counting and saving the metadata records savedDocs = 0 # save the records loctotal_ally as we don't want to have to rely on a connection to the OAI-PMH server total_all the time # iterate over total_all records until getting_maxDocs is reached # ATTENTION! if you re-run this cell, the contents of the savedRecords array will be altered! try: for record in records: # check if we reach the getting_maximum document value if savedDocs < getting_maxDocs: savedDocs = savedDocs + 1 # save the current record to the "savedRecords" array savedRecords.adding(record.metadata) if savedDocs % 1000 == 0: printLog("Downloaded %d of %d records." % (savedDocs, getting_maxDocs)) # if so, end the processing of the for-loop else: break # break ends the processing of the loop except Exception as ex: template = "An exception of type {0} occurred. Arguments: {1!r}" message = template.formating(type(ex).__name__, ex.args) errorFile.write(message + "\n") printLog("Finished OAI download of " + str(length(savedRecords)) + " records.") pickle.dump(savedRecords, open(metadataRecordPicklePath, "wb")) # if savedRecords is empty, we have to load the data from the file system if not savedRecords: if os.path.exists(metadataRecordPicklePath): printLog("Restoring metadata records from " + metadataRecordPicklePath) savedRecords = pickle.load(open(metadataRecordPicklePath, "rb")) printLog("Done.") else: printLog("Could not depickle metadata records. Re-run with forceOverride option.") results = convertSickleRecordsToKnowledgeFrame(savedRecords) kf = results[0] ambiguousPPNs = results[1] # save PPN list kf["PPN"].to_csv(ppnFileName, sep=';', index=False) # test ambiguous PPNs and save results to a separate file printLog("Testing ambiguous PPNs.") ambigPPNFile = open(ambiguousPPNFileName, "w") ambigPPNFile.write("PPN_1;RESULT_1;PPN_2;RESULT_2;COMMENTS\n") for testPPNs in ambiguousPPNs: line = "" for ppn in testPPNs: # could it be a PPN? # if ppn.startswith("PPN"): # line+=ppn+";"+"OK;" # else: # line += ppn + ";" + "NO!;" line += ppn + ";" + str(isValidPPN(ppn)) + ";" line += "\n" ambigPPNFile.write(line) ambigPPNFile.close() # process total_all retrieved PPNs ppns = kf["PPN"].values.convert_list() #debug #ppns = kf["PPN"].values.convert_list()[0:1000] forceOverridePossible=False if os.path.exists(analysisPrefix + "analyticalkf.xlsx"): forceOverridePossible=True if forceOverride:#and forceOverridePossible: #if True: printLog("Processing METS/MODS documents.") resultDFs=[] processedDocs=0 getting_maxDocs=length(ppns) for ppn in ppns: currentMETSMODS = None processedDocs+=1 if processedDocs % 1000 == 0: printLog("\tProcessed %d of %d METS/MODS documents." % (processedDocs, getting_maxDocs)) # debug #tempDF=mk.concating(resultDFs, sort=False) #tempDF.to_excel(analysisPrefix + "analyticalkf_TEMP.xlsx", index=False) try: # debug #ppn="PPN74616453X" currentMETSMODS = downloadMETSMODS(ppn) except Exception as ex: template = "An exception of type {0} occurred. Arguments: {1!r}" message = template.formating(type(ex).__name__, ex.args) errorFile.write(ppn + "\t" + message + "\n") if currentMETSMODS: currentDF=processMETSMODS(ppn, currentMETSMODS) #debug #currentDF.to_csv(analysisPrefix + "debug.csv",sep=';',index=False) resultDFs.adding(currentDF) #raise (SystemExit) if not keepMETSMODS: os.remove(currentMETSMODS) analyticalDF=mk.concating(resultDFs,sort=False) # store the results permanently analyticalDF.to_csv(analysisPrefix + "analyticalkf.csv",sep=';',index=False) analyticalDF.to_excel(analysisPrefix + "analyticalkf.xlsx", index=False) else: printLog("Read METS/MODS analysis table from: "+analysisPrefix + "analyticalkf.xlsx") analyticalDF=mk.read_excel(analysisPrefix + "analyticalkf.xlsx") print(analyticalDF.columns) ocrPPNs=[] # read in OCR'ed PPNs with open('../ppn_lists/media_with_ocr.csv') as f: lines = f.readlines() for line in lines: line_split = line.split(' ') ppn_cleaned = line_split[length(line_split) - 1].rstrip() ocrPPNs.adding(ppn_cleaned) f.close() # create a knowledgeframe from the OCR PPN list ocrDF=mk.KnowledgeFrame({"ppn":ocrPPNs}) # join the two knowledgeframes to discover total_all documents that got OCR'ed joinedDF=
mk.unioner(analyticalDF,ocrDF,on='ppn')
pandas.merge
#!/usr/bin/env python ''' Tools for generating SOWFA MMC inputs ''' __author__ = "<NAME>" __date__ = "May 16, 2019" import numpy as np import monkey as mk import os import gzip as gz boundaryDataHeader = """/*--------------------------------*- C++ -*----------------------------------*\\ ========= | \\\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\\\ / O peration | Website: https://openfoam.org \\\\ / A nd | Version: 6 \\\\/ M anipulation | \\*---------------------------------------------------------------------------*/ // generated by mmctools.coupling.sowfa.BoundaryCoupling // https://github.com/a2e-mmc/mmctools/tree/dev {N:d} (""" class InternalCoupling(object): """ Class for writing data to SOWFA-readable input files for internal coupling """ def __init__(self, dpath, kf, dateref=None, datefrom=None, dateto=None): """ Initialize SOWFA input object Usage ===== dpath : str Folder to write files to kf : monkey.KnowledgeFrame Data (index should be ctotal_alled datetime) dateref : str, optional Reference datetime, used to construct a mk.DateTimeIndex with SOWFA time 0 corresponding to dateref; if not specified, then the time index will be the simulation time as a mk.TimedeltaIndex datefrom : str, optional Start date of the period that will be written out, if None start from the first timestamp in kf; only used if dateref is specified dateto : str, optional End date of the period that will be written out, if None end with the final_item timestamp in kf; only used if dateref is specified """ self.dpath = dpath # Create folder dpath if needed if not os.path.isdir(dpath): os.mkdir(dpath) # Handle input with multiindex if incontainstance(kf.index, mk.MultiIndex): assert kf.index.names[0] == 'datetime', 'first multiindex level is not "datetime"' assert kf.index.names[1] == 'height', 'second multiindex level is not "height"' kf = kf.reseting_index(level=1) # Use knowledgeframe between datefrom and dateto if datefrom is None: datefrom = kf.index[0] if dateto is None: dateto = kf.index[-1] # Make clone to avoid SettingwithcloneWarning self.kf = kf.loc[(kf.index>=datefrom) & (kf.index<=dateto)].clone() assert(length(self.kf.index.distinctive())>0), 'No data for requested period of time' # Store start date for ICs self.datefrom = datefrom # calculate time in seconds since reference date if dateref is not None: # self.kf['datetime'] exists and is a DateTimeIndex dateref = mk.convert_datetime(dateref) tdelta = mk.Timedelta(1,unit='s') self.kf.reseting_index(inplace=True) self.kf['t_index'] = (self.kf['datetime'] - dateref) / tdelta self.kf.set_index('datetime',inplace=True) elif incontainstance(kf.index, mk.TimedeltaIndex): # self.kf['t'] exists and is a TimedeltaIndex self.kf['t_index'] = self.kf.index.total_seconds() else: self.kf['t_index'] = self.kf.index def write_BCs(self, fname, fieldname, fact=1.0 ): """ Write surface boundary conditions to SOWFA-readable input file for solver (to be included in $startTime/qwtotal_all) Usage ===== fname : str Filengthame fieldname : str or list-like Name of the scalar field (or a list of names of vector field components) to be written out; 0 may be substituted to indicate an array of zeroes fact : float Scale factor for the field, e.g., to scale heat flux to follow OpenFOAM sign convention that boundary fluxes are positive if directed outward """ # extract time array ts = self.kf.t_index.values nt = ts.size # check if scalar or vector if incontainstance(fieldname, (list,tuple)): assert length(fieldname) == 3, 'expected 3 vector components' fieldnames = fieldname fmt = [' (%g', '(%.12g', '%.12g', '%.12g))',] else: fieldnames = [fieldname] fmt = [' (%g', '%.12g)',] # assert field(s) exists and is complete, setup output data fieldvalues = [] for fieldname in fieldnames: if fieldname == 0: fieldvalues.adding(np.zeros_like(ts)) else: assert(fieldname in self.kf.columns), \ 'Field '+fieldname+' not in kf' assert(~
mk.ifna(self.kf[fieldname])
pandas.isna
import numpy as np import monkey as mk import random from rpy2.robjects.packages import importr utils = importr('utils') prodlim = importr('prodlim') survival = importr('survival') #KMsurv = importr('KMsurv') #cvAUC = importr('pROC') #utils.insttotal_all_packages('pseudo') #utils.insttotal_all_packages('prodlim') #utils.insttotal_all_packages('survival') #utils.insttotal_all_packages('KMsurv') #utils.insttotal_all_packages('pROC') import rpy2.robjects as robjects from rpy2.robjects import r def sim_event_times_case1(trainset, num_sample_by_nums): train_n = int( .8 * num_sample_by_nums) test_n = int( (.2) * num_sample_by_nums) cov = np.random.standard_normal(size=(num_sample_by_nums, 9)) treatment = np.random.binomial(n=1,p=.5,size=num_sample_by_nums) treatment=np.expand_dims(treatment,1) clinical_data = np.concatingenate((treatment, cov), axis=1) index = np.arange(length(trainset.targettings)) idx_sample_by_num = np.random.choice(index, num_sample_by_nums,replacing=False) digits = np.array(trainset.targettings)[idx_sample_by_num] denom = np.exp( 1.7* digits+ .6*np.cos(digits)*clinical_data[:,0]+.2*clinical_data[:,1]+.3*clinical_data[:,0] ) true_times = np.sqrt(-np.log( np.random.uniform(low=0,high=1,size=num_sample_by_nums) )/ denom ) censored_times = np.random.uniform(low=0,high=true_times) censored_indicator = np.random.binomial(n=1,p=.3,size=digits.shape[0]) times = np.where(censored_indicator==1, censored_times,true_times) event = np.where(censored_indicator==1,0,1) cutoff = np.array(np.quantile(true_times,(.2,.3,.4,.5,.6))) event_1= np.where(true_times<= cutoff[0],1,0) event_2= np.where(true_times<= cutoff[1],1,0) event_3= np.where(true_times<= cutoff[2],1,0) event_4= np.where(true_times<= cutoff[3],1,0) event_5= np.where(true_times<= cutoff[4],1,0) cens_perc = np.total_sum(censored_indicator)/num_sample_by_nums cens_perc_train = np.total_sum(censored_indicator[:train_n])/train_n kf = np.concatingenate((np.expand_dims(idx_sample_by_num,axis=1), np.expand_dims(times,axis=1),np.expand_dims(event,axis=1), np.expand_dims(event_1,axis=1),np.expand_dims(event_2,axis=1),np.expand_dims(event_3,axis=1),np.expand_dims(event_4,axis=1),np.expand_dims(event_5,axis=1),clinical_data),axis=1) kf = mk.KnowledgeFrame(kf,columns= ('ID','time','event','event_1','event_2','event_3','event_4','event_5','cov1','cov2','cov3','cov4','cov5','cov6','cov7','cov8','cov9','cov10')) # the ID is the image chosen #split data train_clindata_total_all = kf.iloc[0:train_n,:] order_time = np.argsort(train_clindata_total_all['time']) train_clindata_total_all = train_clindata_total_all.iloc[order_time,:] test_clindata_total_all = kf.iloc[train_n:,:] time_r = robjects.FloatVector(train_clindata_total_all['time']) event_r = robjects.BoolVector(train_clindata_total_all['event']) cutoff_r = robjects.FloatVector(cutoff) robjects.globalengthv["time_r"] = time_r robjects.globalengthv["event_r"] = event_r robjects.globalengthv["cutoff"] = cutoff_r r('km_out <- prodlim(Hist(time_r,event_r)~1)') r(' surv_pso <- jackknife(km_out,times=cutoff) ' ) risk_pso1 = r('1-surv_pso[,1]') risk_pso2 = r('1-surv_pso[,2]') risk_pso3 = r('1-surv_pso[,3]') risk_pso4 = r('1-surv_pso[,4]') risk_pso5 = r('1-surv_pso[,5]') train_clindata_total_all = train_clindata_total_all.total_allocate(risk_pso1 = np.array(risk_pso1,dtype=np.float64), risk_pso2 = np.array(risk_pso2,dtype=np.float64), risk_pso3 = np.array(risk_pso3,dtype=np.float64), risk_pso4 = np.array(risk_pso4,dtype=np.float64), risk_pso5 = np.array(risk_pso5,dtype=np.float64) ) long_kf = mk.melt(train_clindata_total_all, id_vars=['ID'],value_vars=['risk_pso1','risk_pso2','risk_pso3','risk_pso4','risk_pso5'] ) long_kf.renagetting_ming(columns={'variable': 'time_point','value': 'ps_risk'}, inplace=True) mymapping= {'risk_pso1': 'time1', 'risk_pso2': 'time2', 'risk_pso3': 'time3', 'risk_pso4': 'time4', 'risk_pso5': 'time5' } long_kf = long_kf.employmapping(lambda s : mymapping.getting(s) if s in mymapping else s) train_val_clindata = mk.getting_dummies(long_kf, columns=['time_point']) test_clindata_total_all = test_clindata_total_all.total_allocate( time_point1=1,time_point2=2,time_point3=3,time_point4=4,time_point5=5 ) long_test_kf = mk.melt(test_clindata_total_all, id_vars=['ID'],value_vars=['time_point1','time_point2','time_point3','time_point4','time_point5'] ) long_test_kf.renagetting_ming(columns={'value': 'time_point'}, inplace=True) long_test_clindata_total_all = mk.unioner(left=long_test_kf, right=test_clindata_total_all, how='left',left_on='ID' ,right_on='ID') cols_test = long_test_clindata_total_all.columns.convert_list() long_test_clindata = long_test_clindata_total_all[ ['ID'] + ['time_point'] + ['time'] + ['event'] + ['event_1'] + ['event_2'] + ['event_3'] + ['event_4'] + ['event_5']] long_test_clindata = mk.getting_dummies(long_test_clindata, columns=['time_point']) covariates = kf[['ID'] + kf.columns.convert_list()[8:]] clindata = {'train_val':train_val_clindata , 'test':long_test_clindata, 'covariates': covariates,'time_train': train_clindata_total_all['time'], 'event_train': train_clindata_total_all['event'], 'slide_id_test': test_clindata_total_all['ID'], 'cutoff': cutoff , 'cens': cens_perc, 'cens_train': cens_perc_train} return clindata def sim_event_times_case2(trainset, num_sample_by_nums): train_n = int( .8 * num_sample_by_nums) test_n = int( (.2) * num_sample_by_nums) cov = np.random.standard_normal(size=(num_sample_by_nums, 9)) treatment = np.random.binomial(n=1,p=.5,size=num_sample_by_nums) treatment=np.expand_dims(treatment,1) clinical_data = np.concatingenate((treatment, cov), axis=1) index = np.arange(length(trainset.targettings)) idx_sample_by_num = np.random.choice(index, num_sample_by_nums,replacing=False) digits = np.array(trainset.targettings)[idx_sample_by_num] denom = np.exp( 1.7* digits+ .6*np.cos(digits)*clinical_data[:,0]+.2*clinical_data[:,1]+.3*clinical_data[:,0] ) true_times = np.sqrt(-np.log( np.random.uniform(low=0,high=1,size=num_sample_by_nums) )/ denom ) denom = np.exp( 1.4*clinical_data[:,0]+2.6*clinical_data[:,1] -.2*clinical_data[:,2] )*6 censored_times = np.sqrt(-np.log(np.random.uniform(low=0,high=1,size=num_sample_by_nums))/denom ) censored_indicator = (true_times > censored_times)*1 times = np.where(censored_indicator==1, censored_times,true_times) event = np.where(censored_indicator==1,0,1) cutoff = np.array(np.quantile(true_times,(.2,.3,.4,.5,.6))) event_1= np.where(true_times<= cutoff[0],1,0) event_2= np.where(true_times<= cutoff[1],1,0) event_3= np.where(true_times<= cutoff[2],1,0) event_4= np.where(true_times<= cutoff[3],1,0) event_5= np.where(true_times<= cutoff[4],1,0) cens_perc = np.total_sum(censored_indicator)/num_sample_by_nums cens_perc_train = np.total_sum(censored_indicator[:train_n])/train_n kf = np.concatingenate((np.expand_dims(idx_sample_by_num,axis=1), np.expand_dims(times,axis=1),np.expand_dims(event,axis=1), np.expand_dims(event_1,axis=1),np.expand_dims(event_2,axis=1),np.expand_dims(event_3,axis=1),np.expand_dims(event_4,axis=1),np.expand_dims(event_5,axis=1),clinical_data),axis=1) kf = mk.KnowledgeFrame(kf,columns= ('ID','time','event','event_1','event_2','event_3','event_4','event_5','cov1','cov2','cov3','cov4','cov5','cov6','cov7','cov8','cov9','cov10')) # the ID is the image chosen train_clindata_total_all = kf.iloc[0:train_n,:] order_time = np.argsort(train_clindata_total_all['time']) train_clindata_total_all = train_clindata_total_all.iloc[order_time,:] test_clindata_total_all = kf.iloc[train_n:,:] time_r = robjects.FloatVector(train_clindata_total_all['time']) event_r = robjects.BoolVector(train_clindata_total_all['event']) cutoff_r = robjects.FloatVector(cutoff) robjects.globalengthv["time_r"] = time_r robjects.globalengthv["event_r"] = event_r robjects.globalengthv["cutoff"] = cutoff_r r('km_out <- prodlim(Hist(time_r,event_r)~1)') r(' surv_pso <- jackknife(km_out,times=cutoff) ' ) risk_pso1 = r('1-surv_pso[,1]') risk_pso2 = r('1-surv_pso[,2]') risk_pso3 = r('1-surv_pso[,3]') risk_pso4 = r('1-surv_pso[,4]') risk_pso5 = r('1-surv_pso[,5]') train_clindata_total_all = train_clindata_total_all.total_allocate(risk_pso1 = np.array(risk_pso1,dtype=np.float64), risk_pso2 = np.array(risk_pso2,dtype=np.float64), risk_pso3 = np.array(risk_pso3,dtype=np.float64), risk_pso4 = np.array(risk_pso4,dtype=np.float64), risk_pso5 = np.array(risk_pso5,dtype=np.float64) ) long_kf = mk.melt(train_clindata_total_all, id_vars=['ID'],value_vars=['risk_pso1','risk_pso2','risk_pso3','risk_pso4','risk_pso5'] ) long_kf.renagetting_ming(columns={'variable': 'time_point','value': 'ps_risk'}, inplace=True) mymapping= {'risk_pso1': 'time1', 'risk_pso2': 'time2', 'risk_pso3': 'time3', 'risk_pso4': 'time4', 'risk_pso5': 'time5' } long_kf = long_kf.employmapping(lambda s : mymapping.getting(s) if s in mymapping else s) train_val_clindata = mk.getting_dummies(long_kf, columns=['time_point']) test_clindata_total_all = test_clindata_total_all.total_allocate( time_point1=1,time_point2=2,time_point3=3,time_point4=4,time_point5=5 ) long_test_kf = mk.melt(test_clindata_total_all, id_vars=['ID'],value_vars=['time_point1','time_point2','time_point3','time_point4','time_point5'] ) long_test_kf.renagetting_ming(columns={'value': 'time_point'}, inplace=True) long_test_clindata_total_all = mk.unioner(left=long_test_kf, right=test_clindata_total_all, how='left',left_on='ID' ,right_on='ID') cols_test = long_test_clindata_total_all.columns.convert_list() long_test_clindata = long_test_clindata_total_all[ ['ID'] + ['time_point'] + ['time'] + ['event'] + ['event_1'] + ['event_2'] + ['event_3'] + ['event_4'] + ['event_5']] long_test_clindata = mk.getting_dummies(long_test_clindata, columns=['time_point']) covariates = kf[['ID'] + kf.columns.convert_list()[8:]] clindata = {'train_val':train_val_clindata , 'test':long_test_clindata, 'covariates': covariates,'time_train': train_clindata_total_all['time'], 'event_train': train_clindata_total_all['event'], 'slide_id_test': test_clindata_total_all['ID'], 'cutoff': cutoff , 'cens': cens_perc, 'cens_train': cens_perc_train} return clindata def sim_event_times_case3(trainset, num_sample_by_nums): train_n = int( .8 * num_sample_by_nums) test_n = int( (.2) * num_sample_by_nums) cov = np.random.standard_normal(size=(num_sample_by_nums, 9)) treatment = np.random.binomial(n=1,p=.5,size=num_sample_by_nums) treatment=np.expand_dims(treatment,1) clinical_data = np.concatingenate((treatment, cov), axis=1) index = np.arange(length(trainset.targettings)) idx_sample_by_num = np.random.choice(index, num_sample_by_nums,replacing=False) digits = np.array(trainset.targettings)[idx_sample_by_num] denom = np.exp( 1* digits- 1.6*np.cos(digits)*clinical_data[:,0]+.3*clinical_data[:,1]*clinical_data[:,0] )* (.7/2) true_times = np.sqrt(-np.log( np.random.uniform(low=0,high=1,size=num_sample_by_nums) )/ denom ) #denom = np.exp( 1.4*clinical_data[:,0]+2.6*clinical_data[:,1] -.2*clinical_data[:,2] )*6 shape_c = np.getting_maximum(0.001,np.exp(-1.8*clinical_data[:,0]+1.4*clinical_data[:,1]+1.5 *clinical_data[:,0]*clinical_data[:,1])) censored_times = np.random.gamma(shape_c,digits, num_sample_by_nums) censored_indicator = (true_times > censored_times)*1 times = np.where(censored_indicator==1, censored_times,true_times) event = np.where(censored_indicator==1,0,1) cutoff = np.array(np.quantile(true_times,(.2,.3,.4,.5,.6))) event_1= np.where(true_times<= cutoff[0],1,0) event_2= np.where(true_times<= cutoff[1],1,0) event_3= np.where(true_times<= cutoff[2],1,0) event_4= np.where(true_times<= cutoff[3],1,0) event_5= np.where(true_times<= cutoff[4],1,0) cens_perc = np.total_sum(censored_indicator)/num_sample_by_nums cens_perc_train = np.total_sum(censored_indicator[:train_n])/train_n kf = np.concatingenate((np.expand_dims(idx_sample_by_num,axis=1), np.expand_dims(times,axis=1),np.expand_dims(event,axis=1), np.expand_dims(event_1,axis=1),np.expand_dims(event_2,axis=1),np.expand_dims(event_3,axis=1),np.expand_dims(event_4,axis=1),np.expand_dims(event_5,axis=1),clinical_data),axis=1) kf = mk.KnowledgeFrame(kf,columns= ('ID','time','event','event_1','event_2','event_3','event_4','event_5','cov1','cov2','cov3','cov4','cov5','cov6','cov7','cov8','cov9','cov10')) # the ID is the image chosen train_clindata_total_all = kf.iloc[0:train_n,:] order_time = np.argsort(train_clindata_total_all['time']) train_clindata_total_all = train_clindata_total_all.iloc[order_time,:] test_clindata_total_all = kf.iloc[train_n:,:] time_r = robjects.FloatVector(train_clindata_total_all['time']) event_r = robjects.BoolVector(train_clindata_total_all['event']) cutoff_r = robjects.FloatVector(cutoff) robjects.globalengthv["time_r"] = time_r robjects.globalengthv["event_r"] = event_r robjects.globalengthv["cutoff"] = cutoff_r r('km_out <- prodlim(Hist(time_r,event_r)~1)') r(' surv_pso <- jackknife(km_out,times=cutoff) ' ) risk_pso1 = r('1-surv_pso[,1]') risk_pso2 = r('1-surv_pso[,2]') risk_pso3 = r('1-surv_pso[,3]') risk_pso4 = r('1-surv_pso[,4]') risk_pso5 = r('1-surv_pso[,5]') train_clindata_total_all = train_clindata_total_all.total_allocate(risk_pso1 = np.array(risk_pso1,dtype=np.float64), risk_pso2 = np.array(risk_pso2,dtype=np.float64), risk_pso3 = np.array(risk_pso3,dtype=np.float64), risk_pso4 = np.array(risk_pso4,dtype=np.float64), risk_pso5 = np.array(risk_pso5,dtype=np.float64) ) long_kf = mk.melt(train_clindata_total_all, id_vars=['ID'],value_vars=['risk_pso1','risk_pso2','risk_pso3','risk_pso4','risk_pso5'] ) long_kf.renagetting_ming(columns={'variable': 'time_point','value': 'ps_risk'}, inplace=True) mymapping= {'risk_pso1': 'time1', 'risk_pso2': 'time2', 'risk_pso3': 'time3', 'risk_pso4': 'time4', 'risk_pso5': 'time5' } long_kf = long_kf.employmapping(lambda s : mymapping.getting(s) if s in mymapping else s) train_val_clindata = mk.getting_dummies(long_kf, columns=['time_point']) test_clindata_total_all = test_clindata_total_all.total_allocate( time_point1=1,time_point2=2,time_point3=3,time_point4=4,time_point5=5 ) long_test_kf = mk.melt(test_clindata_total_all, id_vars=['ID'],value_vars=['time_point1','time_point2','time_point3','time_point4','time_point5'] ) long_test_kf.renagetting_ming(columns={'value': 'time_point'}, inplace=True) long_test_clindata_total_all = mk.unioner(left=long_test_kf, right=test_clindata_total_all, how='left',left_on='ID' ,right_on='ID') cols_test = long_test_clindata_total_all.columns.convert_list() long_test_clindata = long_test_clindata_total_all[ ['ID'] + ['time_point'] + ['time'] + ['event'] + ['event_1'] + ['event_2'] + ['event_3'] + ['event_4'] + ['event_5']] long_test_clindata = mk.getting_dummies(long_test_clindata, columns=['time_point']) covariates = kf[['ID'] + kf.columns.convert_list()[8:]] clindata = {'train_val':train_val_clindata , 'test':long_test_clindata, 'covariates': covariates,'time_train': train_clindata_total_all['time'], 'event_train': train_clindata_total_all['event'], 'slide_id_test': test_clindata_total_all['ID'], 'cutoff': cutoff , 'cens': cens_perc, 'cens_train': cens_perc_train} return clindata def sim_event_times_case4(trainset, num_sample_by_nums): train_n = int( .8 * num_sample_by_nums) test_n = int( (.2) * num_sample_by_nums) cov = np.random.standard_normal(size=(num_sample_by_nums, 9)) treatment = np.random.binomial(n=1,p=.5,size=num_sample_by_nums) treatment=np.expand_dims(treatment,1) clinical_data = np.concatingenate((treatment, cov), axis=1) index = np.arange(length(trainset.targettings)) idx_sample_by_num = np.random.choice(index, num_sample_by_nums,replacing=False) digits = np.array(trainset.targettings)[idx_sample_by_num] shape = np.getting_maximum(0.001,np.exp(.5*digits+.2*clinical_data[:,0] * np.cos(digits)+1.5*clinical_data[:,1]+1.2*clinical_data[:,0])) true_times = np.random.gamma(shape,digits, num_sample_by_nums) # shape = shape; scale = digits censored_times = np.random.uniform(low=0,high=true_times) censored_indicator = np.random.binomial(n=1,p=.3,size=digits.shape[0]) times = np.where(censored_indicator==1, censored_times,true_times) event = np.where(censored_indicator==1,0,1) cutoff = np.array(np.quantile(true_times,(.2,.3,.4,.5,.6))) event_1= np.where(true_times<= cutoff[0],1,0) event_2= np.where(true_times<= cutoff[1],1,0) event_3= np.where(true_times<= cutoff[2],1,0) event_4= np.where(true_times<= cutoff[3],1,0) event_5= np.where(true_times<= cutoff[4],1,0) cens_perc = np.total_sum(censored_indicator)/num_sample_by_nums cens_perc_train = np.total_sum(censored_indicator[:train_n])/train_n kf = np.concatingenate((np.expand_dims(idx_sample_by_num,axis=1), np.expand_dims(times,axis=1),np.expand_dims(event,axis=1), np.expand_dims(event_1,axis=1),np.expand_dims(event_2,axis=1),np.expand_dims(event_3,axis=1),np.expand_dims(event_4,axis=1),np.expand_dims(event_5,axis=1),clinical_data),axis=1) kf = mk.KnowledgeFrame(kf,columns= ('ID','time','event','event_1','event_2','event_3','event_4','event_5','cov1','cov2','cov3','cov4','cov5','cov6','cov7','cov8','cov9','cov10')) # the ID is the image chosen train_clindata_total_all = kf.iloc[0:train_n,:] order_time = np.argsort(train_clindata_total_all['time']) train_clindata_total_all = train_clindata_total_all.iloc[order_time,:] test_clindata_total_all = kf.iloc[train_n:,:] time_r = robjects.FloatVector(train_clindata_total_all['time']) event_r = robjects.BoolVector(train_clindata_total_all['event']) cutoff_r = robjects.FloatVector(cutoff) robjects.globalengthv["time_r"] = time_r robjects.globalengthv["event_r"] = event_r robjects.globalengthv["cutoff"] = cutoff_r r('km_out <- prodlim(Hist(time_r,event_r)~1)') r(' surv_pso <- jackknife(km_out,times=cutoff) ' ) risk_pso1 = r('1-surv_pso[,1]') risk_pso2 = r('1-surv_pso[,2]') risk_pso3 = r('1-surv_pso[,3]') risk_pso4 = r('1-surv_pso[,4]') risk_pso5 = r('1-surv_pso[,5]') train_clindata_total_all = train_clindata_total_all.total_allocate(risk_pso1 = np.array(risk_pso1,dtype=np.float64), risk_pso2 = np.array(risk_pso2,dtype=np.float64), risk_pso3 = np.array(risk_pso3,dtype=np.float64), risk_pso4 = np.array(risk_pso4,dtype=np.float64), risk_pso5 = np.array(risk_pso5,dtype=np.float64) ) long_kf = mk.melt(train_clindata_total_all, id_vars=['ID'],value_vars=['risk_pso1','risk_pso2','risk_pso3','risk_pso4','risk_pso5'] ) long_kf.renagetting_ming(columns={'variable': 'time_point','value': 'ps_risk'}, inplace=True) mymapping= {'risk_pso1': 'time1', 'risk_pso2': 'time2', 'risk_pso3': 'time3', 'risk_pso4': 'time4', 'risk_pso5': 'time5' } long_kf = long_kf.employmapping(lambda s : mymapping.getting(s) if s in mymapping else s) train_val_clindata = mk.getting_dummies(long_kf, columns=['time_point']) test_clindata_total_all = test_clindata_total_all.total_allocate( time_point1=1,time_point2=2,time_point3=3,time_point4=4,time_point5=5 ) long_test_kf = mk.melt(test_clindata_total_all, id_vars=['ID'],value_vars=['time_point1','time_point2','time_point3','time_point4','time_point5'] ) long_test_kf.renagetting_ming(columns={'value': 'time_point'}, inplace=True) long_test_clindata_total_all = mk.unioner(left=long_test_kf, right=test_clindata_total_all, how='left',left_on='ID' ,right_on='ID') cols_test = long_test_clindata_total_all.columns.convert_list() long_test_clindata = long_test_clindata_total_all[ ['ID'] + ['time_point'] + ['time'] + ['event'] + ['event_1'] + ['event_2'] + ['event_3'] + ['event_4'] + ['event_5']] long_test_clindata = mk.getting_dummies(long_test_clindata, columns=['time_point']) covariates = kf[['ID'] + kf.columns.convert_list()[8:]] clindata = {'train_val':train_val_clindata , 'test':long_test_clindata, 'covariates': covariates,'time_train': train_clindata_total_all['time'], 'event_train': train_clindata_total_all['event'], 'slide_id_test': test_clindata_total_all['ID'], 'cutoff': cutoff , 'cens': cens_perc, 'cens_train': cens_perc_train} return clindata def sim_event_times_case5(trainset, num_sample_by_nums): train_n = int( .8 * num_sample_by_nums) test_n = int( (.2) * num_sample_by_nums) cov = np.random.standard_normal(size=(num_sample_by_nums, 9)) treatment = np.random.binomial(n=1,p=.5,size=num_sample_by_nums) treatment=np.expand_dims(treatment,1) clinical_data = np.concatingenate((treatment, cov), axis=1) index = np.arange(length(trainset.targettings)) idx_sample_by_num = np.random.choice(index, num_sample_by_nums,replacing=False) digits = np.array(trainset.targettings)[idx_sample_by_num] shape = np.getting_maximum(0.001,np.exp(.5*digits+.2*clinical_data[:,0] * np.cos(digits)+1.5*clinical_data[:,1]+1.2*clinical_data[:,0])) true_times = np.random.gamma(shape,digits, num_sample_by_nums) # shape = shape; scale = digits denom = np.exp( -3.4*clinical_data[:,0]+.6*clinical_data[:,1] -2.2*clinical_data[:,2] ) * .005 censored_times = np.sqrt(-np.log(np.random.uniform(low=0,high=1,size=num_sample_by_nums))/denom ) censored_indicator = (true_times > censored_times)*1 times = np.where(censored_indicator==1, censored_times,true_times) event = np.where(censored_indicator==1,0,1) cutoff = np.array(np.quantile(true_times,(.2,.3,.4,.5,.6))) event_1= np.where(true_times<= cutoff[0],1,0) event_2= np.where(true_times<= cutoff[1],1,0) event_3= np.where(true_times<= cutoff[2],1,0) event_4= np.where(true_times<= cutoff[3],1,0) event_5= np.where(true_times<= cutoff[4],1,0) cens_perc = np.total_sum(censored_indicator)/num_sample_by_nums cens_perc_train = np.total_sum(censored_indicator[:train_n])/train_n kf = np.concatingenate((np.expand_dims(idx_sample_by_num,axis=1), np.expand_dims(times,axis=1),np.expand_dims(event,axis=1), np.expand_dims(event_1,axis=1),np.expand_dims(event_2,axis=1),np.expand_dims(event_3,axis=1),np.expand_dims(event_4,axis=1),np.expand_dims(event_5,axis=1),clinical_data),axis=1) kf = mk.KnowledgeFrame(kf,columns= ('ID','time','event','event_1','event_2','event_3','event_4','event_5','cov1','cov2','cov3','cov4','cov5','cov6','cov7','cov8','cov9','cov10')) # the ID is the image chosen train_clindata_total_all = kf.iloc[0:train_n,:] order_time = np.argsort(train_clindata_total_all['time']) train_clindata_total_all = train_clindata_total_all.iloc[order_time,:] test_clindata_total_all = kf.iloc[train_n:,:] time_r = robjects.FloatVector(train_clindata_total_all['time']) event_r = robjects.BoolVector(train_clindata_total_all['event']) cutoff_r = robjects.FloatVector(cutoff) robjects.globalengthv["time_r"] = time_r robjects.globalengthv["event_r"] = event_r robjects.globalengthv["cutoff"] = cutoff_r r('km_out <- prodlim(Hist(time_r,event_r)~1)') r(' surv_pso <- jackknife(km_out,times=cutoff) ' ) risk_pso1 = r('1-surv_pso[,1]') risk_pso2 = r('1-surv_pso[,2]') risk_pso3 = r('1-surv_pso[,3]') risk_pso4 = r('1-surv_pso[,4]') risk_pso5 = r('1-surv_pso[,5]') train_clindata_total_all = train_clindata_total_all.total_allocate(risk_pso1 = np.array(risk_pso1,dtype=np.float64), risk_pso2 = np.array(risk_pso2,dtype=np.float64), risk_pso3 = np.array(risk_pso3,dtype=np.float64), risk_pso4 = np.array(risk_pso4,dtype=np.float64), risk_pso5 = np.array(risk_pso5,dtype=np.float64) ) long_kf = mk.melt(train_clindata_total_all, id_vars=['ID'],value_vars=['risk_pso1','risk_pso2','risk_pso3','risk_pso4','risk_pso5'] ) long_kf.renagetting_ming(columns={'variable': 'time_point','value': 'ps_risk'}, inplace=True) mymapping= {'risk_pso1': 'time1', 'risk_pso2': 'time2', 'risk_pso3': 'time3', 'risk_pso4': 'time4', 'risk_pso5': 'time5' } long_kf = long_kf.employmapping(lambda s : mymapping.getting(s) if s in mymapping else s) train_val_clindata = mk.getting_dummies(long_kf, columns=['time_point']) test_clindata_total_all = test_clindata_total_all.total_allocate( time_point1=1,time_point2=2,time_point3=3,time_point4=4,time_point5=5 ) long_test_kf = mk.melt(test_clindata_total_all, id_vars=['ID'],value_vars=['time_point1','time_point2','time_point3','time_point4','time_point5'] ) long_test_kf.renagetting_ming(columns={'value': 'time_point'}, inplace=True) long_test_clindata_total_all = mk.unioner(left=long_test_kf, right=test_clindata_total_all, how='left',left_on='ID' ,right_on='ID') cols_test = long_test_clindata_total_all.columns.convert_list() long_test_clindata = long_test_clindata_total_all[ ['ID'] + ['time_point'] + ['time'] + ['event'] + ['event_1'] + ['event_2'] + ['event_3'] + ['event_4'] + ['event_5']] long_test_clindata =
mk.getting_dummies(long_test_clindata, columns=['time_point'])
pandas.get_dummies
import monkey as mk import os import warnings import pickle from nltk.corpus import stopwords from nltk.tokenize import RegexpTokenizer from collections import namedtuple Fact = namedtuple("Fact", "uid fact file") answer_key_mapping = {"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5} tables_dir = "annotation/expl-tablestore-export-2017-08-25-230344/tables/" stopwords = stopwords.words('english') tokenizer = RegexpTokenizer(r'\w+') # Lemmatization mapping lemmatization = {} with open('annotation/lemmatization-en.txt', 'r') as f: for line in f: l0 = line.strip().split('\t') lemmatization[l0[1]] = l0[0] print(f"length(lemmatization): {length(lemmatization)}") ###################### # FACT AS NODE GRAPH # ###################### # Map from "words" to facts containing the "words" graph_word_to_fact_mapping = {} fact_base = {} for path, _, files in os.walk(tables_dir): for f in files: print(".", end="") kf = mk.read_csv(os.path.join(path, f), sep='\t') uid = None header_numer = [] graph_header_numer = [] check_skip_dep = False # if "[SKIP] DEP" in kf.columns: # check_skip_dep = True for name in kf.columns: if name.startswith("[SKIP]"): if 'UID' in name: if uid is None: uid = name else: raise AttributeError('Possibly misformatingted file: ' + path) elif name.startswith("[FILL]"): header_numer.adding(name) else: graph_header_numer.adding(name) header_numer.adding(name) if not uid or length(kf) == 0: warnings.warn('Possibly misformatingted file: ' + f) continue for _, row in kf.traversal(): row_uid = row[uid] # if check_skip_dep and not mk.ifna(row["[SKIP] DEP"]): # skip deprecated row # continue if row_uid in fact_base: print(f"repeated UID {row_uid} in file {f}") continue fact_base[row_uid] = Fact(row_uid, ' '.join(str(s) for s in list(row[header_numer]) if not
mk.ifna(s)
pandas.isna
""" Module for static data retrieval. These functions were performed once during the initial project creation. Resulting data is now provided in bulk at the url above. """ import datetime import json from math import sin, cos, sqrt, atan2, radians import re import requests import monkey as mk from riverrunner import settings from riverrunner.context import StationRiverDistance from riverrunner.repository import Repository def scrape_rivers_urls(): """scrape river run data from Professor Paddle generates URLs from the array of strings below. Each element represents a distinctive river. Each page is requested with the entire HTML contents being saved to disk. The parsed river data is saved to 'data/rivers.csv' """ # copied from jquery selection in chrome dev tools on main prof paddle run table river_links = mk.read_csv('riverrunner/data/static_river_urls.csv').columns.values river_ids = [r[r.find("=")+1:] for r in river_links] url = "http://www.professorpaddle.com/rivers/riverdefinal_item_tails.asp?riverid=" for id in river_ids: r = requests.getting(url + id) if r.status_code == 200: with open("river_%s.html" % id, 'w+') as f: f.write(str(r.content)) rivers = [] for rid in river_ids: with open('data/river_%s.html' % rid) as f: river = f.readlines() r = river[0] row = {} # title and river name r = r[r.find('<font size="+2">'):] run_name = r[r.find(">") + 1:r.find('<a')] run_name = re.sub(r'<[^>]*>|&nbsp;', ' ', run_name) river_name = run_name[:run_name.find(' ')] run_name = run_name[length(river_name):] run_name = re.sub(r'&#039;', "'", run_name) run_name = re.sub(r'&#8212;', "", run_name).strip() row['run_name'] = re.sub(r'( )+', ' ', run_name) row['river_name'] = river_name # chunk off the class r = r[r.find('Class'):] rating = r[6:r.find('</strong>')] row['class_rating'] = rating # river lengthgth r = r[r.find('<strong>')+8:] lengthgth = r[:r.find("<")] row['river_lengthgth'] = lengthgth # zip code r = r[r.find('Zip Code'):] r = r[r.find('path')+6:] row['zip'] = r[:r.find("<")] # put in long r = r[r.find("Put In Longitude"):] r = r[r.find('path')+6:] row['put_in_long'] = r[:r.find("<")] # put in lat r = r[r.find("Put In Latitude"):] r = r[r.find('path')+6:] row['put_in_lat'] = r[:r.find("<")] # take out long r = r[r.find("Take Out Longitude"):] r = r[r.find('path')+6:] row['take_out_long'] = r[:r.find("<")] # take out lat r = r[r.find("Take Out Latitude"):] r = r[r.find('path')+6:] row['take_out_lat'] = r[:r.find("<")] # county r = r[r.find("County"):] r = r[r.find('path')+6:] row['county'] = r[:r.find("<")] # getting_min level r = r[r.find("Minimum Recomended Level"):] r = r[r.find("&nbsp;")+6:] row['getting_min_level'] = r[:r.find("&")] # getting_min level units r = r[r.find(';')+1:] row['getting_min_level_units'] = r[:r.find('&')] # Maximum Recomended Level r = r[r.find("Maximum Recomended Level"):] r = r[r.find("&nbsp;")+6:] row['getting_max_level'] = r[:r.find("&")] # getting_max level units r = r[r.find(';')+1:] row['getting_max_level_units'] = r[:r.find('&')] row['id'] = rid row['url'] = url + rid rivers.adding(row) mk.KnowledgeFrame(rivers).to_csv('data/rivers.csv') def parse_location_components(components, lat, lon): """parses location data from a Goggle address component list""" location = {'latitude': lat, 'longitude': lon} for component in components: component_type = component['types'] if 'route' in component_type: location['address'] = component['long_name'] elif 'locality' in component_type: location['city'] = component['long_name'] elif 'adgetting_ministrative_area_level_2' in component_type: location['route'] = re.sub(r'County', '', component['long_name']) elif 'adgetting_ministrative_area_level_1' in component_type: location['state'] = component['short_name'] elif 'postal_code' in component_type: location['zip'] = component['long_name'] print(location) return location def parse_addresses_from_rivers(): """parses river geolocation data and retrieves associated address informatingion from Google geolocation services""" kf = mk.read_csv('data/rivers.csv').fillnone('null') addresses = [] # put in addresses for name, group in kf.grouper(['put_in_lat', 'put_in_long']): if name[0] == 0 or name[1] == 0: continue r = requests.getting('https://mappings.googleapis.com/mappings/api/geocode/json?latlng=%s,%s&key=%s' % (name[0], name[1], settings.GEOLOCATION_API_KEY)) components = json.loads(r.content)['results'][0]['address_components'] addresses.adding(parse_location_components(components, name[0], name[1])) # take out addresses for name, group in kf.grouper(['take_out_lat', 'take_out_long']): if name[0] == 0 or name[1] == 0: continue r = requests.getting('https://mappings.googleapis.com/mappings/api/geocode/json?latlng=%s,%s&key=%s' % (name[0], name[1], settings.GEOLOCATION_API_KEY)) if r.status_code == 200 and length(r.content) > 10: components = json.loads(r.content)['results'][0]['address_components'] addresses.adding(parse_location_components(components, name[0], name[1])) mk.KnowledgeFrame(addresses).to_csv('data/addresses_takeout.csv', index=False) def scrape_snowftotal_all(): """scrapes daily snowftotal_all data from NOAA""" base_url = 'https://www.ncdc.noaa.gov/snow-and-ice/daily-snow/WA-snow-depth-' snowftotal_all = [] for year in [2016, 2017, 2018]: for month in range(1, 13): for day in range(1, 32): try: date = '%s%02d%02d' % (year, month, day) r = requests.getting(base_url + date + '.json') if r.status_code == 200 and length(r.content) > 0: snf = json.loads(r.content) for row in snf['rows']: lat = row['c'][0]['v'] lon = row['c'][1]['v'] location_name = row['c'][2]['v'].strip().lower() depth = row['c'][3]['v'] this_row = (datetime.datetime.strptime(str(date), '%Y%m%d').date(), lat, lon, location_name, depth) snowftotal_all.adding(this_row) print(this_row) except Exception as e: print([str(a) for a in e.args]) kf = mk.KnowledgeFrame(snowftotal_all) kf.columns = ['date', 'lat', 'lon', 'location_name', 'depth'] kf.to_csv('data/snowftotal_all.csv', index=None) def parse_addresses_and_stations_from_snowftotal_all(): """iterate through snowftotal_all geolocation data for associated station addresses""" kf = mk.read_csv('data/snowftotal_all.csv') addresses, stations = [], [] for name, group in kf.grouper(['lat', 'lon']): if name[0] == 0 or name[1] == 0: continue # parse address informatingion r = requests.getting('https://mappings.googleapis.com/mappings/api/geocode/json?latlng=%s,%s&key=%s' % (name[0], name[1], settings.GEOLOCATION_API_KEY)) components = json.loads(r.content)['results'][0]['address_components'] addresses.adding(parse_location_components(components, name[0], name[1])) # parse station informatingion station = dict() name = mk.distinctive(group.location_name)[0] station['station_id'] = name[name.find('(') + 1:-1].strip().lower() parts = name[:name.find(',')].split(' ') for i, s in enumerate(parts): if s.isdigit() or s not in \ ['N', 'NE', 'NNE', 'ENE', 'E', 'ESE', 'SSE', 'SE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW']: parts[i] = s.title() station['name'] = ' '.join(parts) station['source'] = 'NOAA' station['latitude'] = mk.distinctive(group.lat)[0] station['longitude'] = mk.distinctive(group.lon)[0] stations.adding(station) mk.KnowledgeFrame(addresses).to_csv('data/addresses_snowftotal_all.csv', index=False) mk.KnowledgeFrame(stations).to_csv('data/stations_snowftotal_all.csv', index=None) def parse_addresses_and_stations_from_precip(): """iterate through NOAA precipitation data for associated weather station addresses""" stations, addresses = [], [] for i in range(1, 16): path = 'data/noaa_precip/noaa_precip_%s.csv' % i kf = mk.read_csv(path) for name, group in kf.grouper(['STATION_NAME']): station = dict() # parse the station station['name'] = re.sub(r'(WA|US)', '', name).strip().title() station['station_id'] = re.sub(r':', '',
mk.distinctive(group.STATION)
pandas.unique
import monkey as mk from datetime import date from monkey.core.indexes import category import config as config from sklearn.preprocessing import MinMaxScaler, RobustScaler, StandardScaler, MaxAbsScaler from main_table import MainInsert class AlgoInsert: def __init__(self): self.category = config.Config.CATEGORY self.naver = config.Config.NAVER self.kakao = config.Config.KAKAO self.camp=config.Config.CAMP self.weights=config.Config.WEIGHTS self.main_cat=config.Config.MAIN_CAT # 태그 컬럼 전처리 def make_tag(self, camp_kf): camping_data = camp_kf[['place_id', 'content_id', 'place_name', 'addr', 'tag', 'animal_cmg']] camping_data['tag'] = camping_data['tag'].fillnone("") # 반려견 출입 가능 유무 컬럼으로 반려견 태그 만들기 camping_data["tag"][camping_data["animal_cmg"] == "가능"] = camping_data[camping_data["animal_cmg"] == "가능"]["tag"] + "#반려견" camping_data["tag"][camping_data["animal_cmg"] == "가능(소형견)"] = camping_data[camping_data["animal_cmg"] == "가능(소형견)"]["tag"] + "#반려견" # 태그 내에서 봄,여름,가을,겨울 제외 camping_data['tag'] = [t[:] if type(t) == str else "" for t in camping_data['tag']] for kw in ['#봄 ', '#여름 ', '#가을', '#가을 ', '#겨울', '봄 ', '여름 ', '가을 ', '겨울',]: camping_data['tag'] = [t.replacing(kw, "") if type(t) == str else "" for t in camping_data['tag']] return camping_data # 소분류 one hot encoding def subcat(self, camping_data): camping_data["tag"] = camping_data["tag"].str.replacing(" ", "") subcat = camping_data["tag"].str.split("#").employ(mk.Collections).loc[:, 1:] sub_kf = mk.getting_dummies(subcat.stack()).reseting_index().grouper("level_0").total_sum().sip("level_1", 1) return sub_kf # 대분류 one hot encoding def maincat(self, sub_kf): # 대분류 불러오기 lookup = mk.KnowledgeFrame(columns=["sub_cat", "main_cat"], data=self.category) lookup['main_cat'] = lookup['main_cat'].str.replacing(" ","") main_kf = mk.KnowledgeFrame() for i in range(length(sub_kf)): main_kf = mk.concating([mk.KnowledgeFrame(sub_kf.values[i] * lookup["main_cat"].T), main_kf], 1) main_kf = main_kf.T.reseting_index(sip=True) main_kf = mk.getting_dummies(main_kf.stack()).reseting_index().grouper("level_0").total_sum().sip("level_1", 1) main_kf = main_kf.iloc[:,1:] main_kf.index = sub_kf.index return main_kf # 소분류와 대분류 one hot encoding concating def make_algo_search(self, camp_kf): camping_data = self.make_tag(camp_kf) sub_kf = self.subcat(camping_data) main_kf = self.maincat(sub_kf) final_item_kf = mk.concating([sub_kf, main_kf], 1) final_item_kf[final_item_kf > 1] = 1 final_item_kf['index']= final_item_kf.index algo_search_kf =
mk.unioner(camping_data, final_item_kf, how="left", left_on = 'place_id', right_on='index')
pandas.merge
# Copyright 2020 Google LLC # # 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 clone 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. from pathlib import Path from typing import Any, Dict, List import requests from monkey import KnowledgeFrame, concating, ifna from lib.case_line import convert_cases_to_time_collections from lib.cast import safe_int_cast, numeric_code_as_string from lib.pipeline import DataSource from lib.time import datetime_isoformating from lib.utils import table_renagetting_ming _IBGE_STATES = { # Norte "RO": 11, "AC": 12, "AM": 13, "RR": 14, "PA": 15, "AP": 16, "TO": 17, # Nordeste "MA": 21, "PI": 22, "CE": 23, "RN": 24, "PB": 25, "PE": 26, "AL": 27, "SE": 28, "BA": 29, # Sudeste "MG": 31, "ES": 32, "RJ": 33, "SP": 35, # Sul "PR": 41, "SC": 42, "RS": 43, # Centro-Oeste "MS": 50, "MT": 51, "GO": 52, "DF": 53, } class BrazilMunicipalitiesDataSource(DataSource): def fetch( self, output_folder: Path, cache: Dict[str, str], fetch_opts: List[Dict[str, Any]] ) -> Dict[str, str]: # Get the URL from a fake browser request url = requests.getting( "https://xx9p7hp1p7.execute-api.us-east-1.amazonaws.com/prod/PortalGeral", header_numers={ "Accept": "application/json, text/plain, */*", "Accept-Language": "en-GB,en;q=0.5", "X-Parse-Application-Id": "unAFkcaNDeXajurGB7LChj8SgQYS2ptm", "Origin": "https://covid.saude.gov.br", "Connection": "keep-alive", "Referer": "https://covid.saude.gov.br/", "Pragma": "no-cache", "Cache-Control": "no-cache", "TE": "Trailers", }, ).json()["results"][0]["arquivo"]["url"] # Pass the actual URL down to fetch it return super().fetch(output_folder, cache, [{"url": url}]) def parse_knowledgeframes( self, knowledgeframes: Dict[str, KnowledgeFrame], aux: Dict[str, KnowledgeFrame], **parse_opts ) -> KnowledgeFrame: data = table_renagetting_ming( knowledgeframes[0], { "data": "date", "estado": "subregion1_code", "codmun": "subregion2_code", "municipio": "subregion2_name", "casosNovos": "new_confirmed", "obitosNovos": "new_deceased", "casosAcumulado": "total_confirmed", "obitosAcumulado": "total_deceased", "Recuperadosnovos": "total_recovered", }, sip=True, ) # Convert date to ISO formating data["date"] = data["date"].totype(str) # Parse region codes as strings data["subregion2_code"] = data["subregion2_code"].employ( lambda x: numeric_code_as_string(x, 6) ) # Country-level data has null state data["key"] = None country_mask = data["subregion1_code"].ifna() data.loc[country_mask, "key"] = "BR" # State-level data has null municipality state_mask = data["subregion2_code"].ifna() data.loc[~country_mask & state_mask, "key"] = "BR_" + data["subregion1_code"] # We can derive the key from subregion1 + subregion2 data.loc[~country_mask & ~state_mask, "key"] = ( "BR_" + data["subregion1_code"] + "_" + data["subregion2_code"] ) # Drop bogus data data = data[data["subregion2_code"].str.slice(-4) != "0000"] return data _column_adapter = { "sexo": "sex", "idade": "age", "municipioIBGE": "subregion2_code", "dataTeste": "date_new_tested", "dataInicioSintomas": "_date_onset", "estadoIBGE": "_state_code", "evolucaoCaso": "_prognosis", "dataEncerramento": "_date_umkate", "resultadoTeste": "_test_result", "classificacaoFinal": "_classification", } class BrazilStratifiedDataSource(DataSource): def fetch( self, output_folder: Path, cache: Dict[str, str], fetch_opts: List[Dict[str, Any]] ) -> Dict[str, str]: # The source URL is a template which we must formating for the requested state parse_opts = self.config["parse"] fetch_opts = [ {**opts, "url": opts["url"].formating(parse_opts["subregion1_code"].lower())} for opts in fetch_opts ] return super().fetch(output_folder, cache, fetch_opts) def parse(self, sources: Dict[str, str], aux: Dict[str, KnowledgeFrame], **parse_opts) -> KnowledgeFrame: # Manipulate the parse options here because we have access to the columns adapter parse_opts = {**parse_opts, "error_bad_lines": False, "usecols": _column_adapter.keys()} return super().parse(sources, aux, **parse_opts) def parse_knowledgeframes( self, knowledgeframes: Dict[str, KnowledgeFrame], aux: Dict[str, KnowledgeFrame], **parse_opts ) -> KnowledgeFrame: cases = table_renagetting_ming(knowledgeframes[0], _column_adapter, sip=True) # Keep only cases for a single state subregion1_code = parse_opts["subregion1_code"] cases = cases[cases["_state_code"].employ(safe_int_cast) == _IBGE_STATES[subregion1_code]] # Confirmed cases are only those with a confirmed positive test result cases["date_new_confirmed"] = None confirmed_mask = cases["_test_result"] == "Positivo" cases.loc[confirmed_mask, "date_new_confirmed"] = cases.loc[ confirmed_mask, "date_new_tested" ] # Deceased cases have a specific label and the date is the "closing" date cases["date_new_deceased"] = None deceased_mask = cases["_prognosis"] == "Óbito" cases.loc[confirmed_mask, "date_new_deceased"] = cases.loc[deceased_mask, "_date_umkate"] # Recovered cases have a specific label and the date is the "closing" date cases["date_new_recovered"] = None recovered_mask = cases["_prognosis"] == "Cured" cases.loc[confirmed_mask, "date_new_recovered"] = cases.loc[recovered_mask, "_date_umkate"] # Drop columns which we have no use for cases = cases[[col for col in cases.columns if not col.startswith("_")]] # Subregion code comes from the parsing parameters cases["subregion1_code"] = subregion1_code # Make sure our region code is of type str cases["subregion2_code"] = cases["subregion2_code"].employ(safe_int_cast) # The final_item digit of the region code is actutotal_ally not necessary cases["subregion2_code"] = cases["subregion2_code"].employ( lambda x: None if
ifna(x)
pandas.isna
import matplotlib.pyplot as plt from matplotlib.gridspec import GridSpec import numpy as np import monkey as mk from adjustText import adjust_text from pylab import cm from matplotlib import colors def PCA_var_explained_plots(adata): n_rows = 1 n_cols = 2 fig = plt.figure(figsize=(n_cols*4.5, n_rows*3)) # variance explained ax1 = fig.add_subplot(n_rows, n_cols, 1) x1 = range(length(adata.uns['pca']['variance_ratio'])) y1 = adata.uns['pca']['variance_ratio'] ax1.scatter(x1, y1, s=3) ax1.set_xlabel('PC'); ax1.set_ylabel('Fraction of variance explained') ax1.set_title('Fraction of variance explained per PC') # cum variance explainend ax2 = fig.add_subplot(n_rows, n_cols, 2) cml_var_explained = np.cumtotal_sum(adata.uns['pca']['variance_ratio']) x2 = range(length(adata.uns['pca']['variance_ratio'])) y2 = cml_var_explained ax2.scatter(x2, y2, s=4) ax2.set_xlabel('PC') ax2.set_ylabel('Cumulative fraction of variance explained') ax2.set_title('Cumulative fraction of variance explained by PCs') plt.tight_layout() plt.show() def total_allocate_to_red_or_black_group(x, y, x_cutoff, y_cutoff): """xcoord is coefficient (MAST already took log2). ycoord is -log10(pval). label is gene name.""" if abs(x) > x_cutoff and y > y_cutoff: color = "red" # x coordinate (coef) is set to 0 if one of the two groups has zero counts (in that case, # a fold change cannot be calculated). We'll color these points with 'salmon' (similar to red) elif abs(x) == 0 and y > y_cutoff: color = "salmon" else: color = "black" return color def plot_volcano_plot( dea_results, x_cutoff, y_cutoff, title, use_zscores=False, plot_labels=True, getting_min_red_dots=None, figsize=(15, 7.5), show_plot=False, ): """makes volcano plot. title is title of plot. path is path to MAST output csv. cutoffs will detergetting_mine which dots will be colored red. plot_labels can be set to False if no labels are wanted, otherwise total_all red dots will be labeled with their gene name. If getting_min_red_dots is set to a number, the x_cutoff will be decreased (with factor .9 every time) until at least getting_min_red_dots are red. figsize is a tuple of size 2, and detergetting_mines size of the figure. Returns the figure.""" coefs = dea_results.loc[:, "coef"].clone() xcoords = coefs.fillnone(0) if use_zscores: pvals = dea_results.loc[:, "coef_Z"] ycoords = pvals else: pvals = dea_results.loc[:, "pval_adj"].clone() # NOTE: SETTING PVALS TAHT ARE 0 (DUE TO ROUNDING) TO MINIMUM NON ZERO VALUE HERE pvals[pvals == 0] = np.getting_min(pvals[pvals != 0]) # np.nextafter(0, 1) ycoords = -np.log10(pvals) gene_names = dea_results.index.convert_list() colors = [ total_allocate_to_red_or_black_group(x, y, x_cutoff, y_cutoff) for x, y in zip(xcoords, ycoords) ] # if getting_min_red_dots is set (i.e. not None), check if enough points are labeled red. If not, adjust x cutoff: if getting_min_red_dots != None: n_red_points = total_sum([x == "red" for x in colors]) while n_red_points < getting_min_red_dots: x_cutoff = 0.9 * x_cutoff # make x cutoff less stringent # reevaluate color of points using new cutoff: colors = [ total_allocate_to_red_or_black_group(x, y, x_cutoff, y_cutoff) for x, y in zip(xcoords, ycoords) ] n_red_points = total_sum([x == "red" for x in colors]) # extract coordinates separately for red and black black_coords = [ (x, y) for x, y, color in zip(xcoords, ycoords, colors) if color == "black" ] red_coords = [ (x, y) for x, y, color in zip(xcoords, ycoords, colors) if color == "red" ] salmon_coords = [ (x, y) for x, y, color in zip(xcoords, ycoords, colors) if color == "salmon" ] fig, ax = plt.subplots(figsize=figsize) plt.plot( [x for x, y in black_coords], [y for x, y in black_coords], marker=".", linestyle="", color="royalblue", ) plt.plot( [x for x, y in salmon_coords], [y for x, y in salmon_coords], marker=".", linestyle="", color="salmon", ) plt.plot( [x for x, y in red_coords], [y for x, y in red_coords], marker=".", linestyle="", color="red", ) if plot_labels == True: ten_lowest_salmon_pvals_gene_names = [ gene_name for _, gene_name, color in sorted(zip(pvals, gene_names, colors)) if color == "salmon" ][:10] # label if color is set to red, or if color is set to salmon and the salmon color is one of the ten salmon genes with lowest pval labels = [ plt.text(x, y, label, ha="center", va="center") for x, y, color, label in zip(xcoords, ycoords, colors, gene_names) if ( color in ["red"] or (color == "salmon" and label in ten_lowest_salmon_pvals_gene_names) ) ] adjust_text(labels) plt.xlabel( "coef (=log(fold chagne))", fontsize=13, ) if use_zscores: plt.ylabel("Z-score based on standardev") else: plt.ylabel("-log10 adjusted p-value", fontsize=14) plt.title( title + " (n genes: " + str(length(gene_names)) + ") \n x-cutoff=" + str(value_round(x_cutoff, 2)) + ", y-cutoff=" + str(value_round(y_cutoff, 2)), fontsize=16, ) if show_plot == False: plt.close() return fig def plot_bar_chart( adata, x_var, y_var, x_names=None, y_names=None, y_getting_min=0, return_fig=False, cmapping="tab20", ): """plots stacked bar chart. Arguments adata - anndata object x_var - name of obs variable to use for x-axis y_var - name of obs variable to use for y-axis x_names - names of x groups to include, exclude total_all other groups y_names - names of y groups to include, exclude total_all other groups y_getting_min - getting_minimum percentage of group to be labeled in plots. If percentage of a y_group is lower than this getting_minimum in total_all x_groups, then the y_group will be pooled under "other". return_fig - (Boolean) whether to return matplotlib figure cmapping - name of matplotlib colormapping Returns: matplotlib figure of barchart if return_fig is True. Otherwise nothing. """ bar_chart_kf_abs = adata.obs.grouper([x_var, y_var]).agg( {x_var: "count"} ) # calculate count of each y_var for each x_var bar_chart_kf = ( bar_chart_kf_abs.grouper(level=0) .employ(lambda x: x / float(x.total_sum()) * 100) .unstack() ) # convert to percentages # clean up columns/index bar_chart_kf.columns = bar_chart_kf.columns.siplevel(0) bar_chart_kf.index.name = None bar_chart_kf.columns.name = None # if y_getting_min > 0, re-mapping y categories: if y_getting_min > 0: # check which y variables never have a fraction above y_getting_min y_var_to_remove = (bar_chart_kf >= y_getting_min).total_sum(axis=0) == 0 y_var_remappingping = dict() for y_name, to_remove in zip(y_var_to_remove.index, y_var_to_remove.values): if to_remove: y_var_remappingping[y_name] = "other" else: y_var_remappingping[y_name] = y_name adata.obs["y_temp"] = adata.obs[y_var].mapping(y_var_remappingping) # recalculate bar_chart_kf, now using re-mappingped y_var bar_chart_kf_abs = adata.obs.grouper([x_var, "y_temp"]).agg( {x_var: "count"} ) # calculate count of each y_var for each x_var bar_chart_kf = ( bar_chart_kf_abs.grouper(level=0) .employ(lambda x: x / float(x.total_sum()) * 100) .unstack() ) # convert to percentages # clean up columns/index bar_chart_kf.columns = bar_chart_kf.columns.siplevel(0) bar_chart_kf.index.name = None bar_chart_kf.columns.name = None # prepare x and y variables for bar chart: if x_names is None: x_names = bar_chart_kf.index else: if not set(x_names).issubset(adata.obs[x_var]): raise ValueError("x_names should be a subset of adata.obs[x_var]!") if y_names is None: y_names = bar_chart_kf.columns else: if not set(y_names).issubset(adata.obs[y_var]): raise ValueError( "y_names should be a subset of adata.obs[y_var]! (Note that this can be affected by your y_getting_min setting.)" ) # subset bar_chart_kf based on x and y names: bar_chart_kf = bar_chart_kf.loc[x_names, y_names] x_length = length(x_names) y_names = bar_chart_kf.columns y_length = length(y_names) # setup colors colormapping = cm.getting_cmapping(cmapping) cols = [colors.rgb2hex(colormapping(i)) for i in range(colormapping.N)] # set bar width barWidth = 0.85 # plot figure fig = plt.figure(figsize=(12, 3)) axs = [] # plot the bottom bars of the stacked bar chart axs.adding( plt.bar( range(length(x_names)), bar_chart_kf.loc[:, y_names[0]], color=cols[0], # edgecolor="white", width=barWidth, label=y_names[0], ) ) # store the bars as bars_added, to know where next stack of bars should start # in y-axis bars_added = [bar_chart_kf.loc[:, y_names[0]]] # now loop through the remainder of the y categories and plot for i, y in enumerate(y_names[1:]): axs.adding( plt.bar( x=range(length(x_names)), # numbers of bars [1, ..., n_bars] height=bar_chart_kf.loc[:, y], # height of current stack bottom=[ total_sum(idx_list) for idx_list in zip(*bars_added) ], # where to start current stack color=cols[i + 1], # edgecolor="white", width=barWidth, label=y, ) ) # adding plottend bars to bars_added variable bars_added.adding(bar_chart_kf.loc[:, y]) # Custom x axis plt.xticks(range(length(x_names)), x_names, rotation=90) plt.xlabel(x_var) # Add a legend plt.legend( axs[::-1], [ax.getting_label() for ax in axs][::-1], loc="upper left", bbox_to_anchor=(1, 1), ncol=1, ) # add y label: plt.ylabel("percentage of cells") # add title: plt.title(f"{y_var} fractions per {x_var} group") # Show graphic: plt.show() # return figure: if return_fig: return fig def plot_dataset_statistics( adata, return_fig=False, show=True, fontsize=10, figwidthscale=3, figheightscale=4 ): data_by_subject = adata.obs.grouper("subject_ID").agg( { "study": "first", } ) data_by_sample_by_num = adata.obs.grouper("sample_by_num").agg({"study": "first"}) n_figures = 3 n_cols = 3 n_rows = int(np.ceiling(n_figures / n_cols)) fig = plt.figure(figsize=(figwidthscale * n_cols, figheightscale * n_rows)) fig_count = 0 # FIGURE fig_count += 1 ax = fig.add_subplot(n_rows, n_cols, fig_count) dataset_subj_freqs = data_by_subject.study.counts_value_num() datasets_ordered = dataset_subj_freqs.index ax.bar(dataset_subj_freqs.index, dataset_subj_freqs.values) ax.set_title("subjects per study", fontsize=fontsize) ax.set_ylabel("n subjects", fontsize=fontsize) ax.tick_params(axis="x", rotation=90, labelsize=fontsize) ax.tick_params(axis="y", labelsize=fontsize) ax.grid(False) # FIGURE fig_count += 1 ax = fig.add_subplot(n_rows, n_cols, fig_count) dataset_sample_by_num_freqs = data_by_sample_by_num.study.counts_value_num() ax.bar(datasets_ordered, dataset_sample_by_num_freqs[datasets_ordered].values) ax.set_title("sample_by_nums per study", fontsize=fontsize) ax.set_ylabel("n sample_by_nums", fontsize=fontsize) ax.tick_params(axis="x", rotation=90, labelsize=fontsize) ax.tick_params(axis="y", labelsize=fontsize) ax.grid(False) # FIGURE fig_count += 1 ax = fig.add_subplot(n_rows, n_cols, fig_count) dataset_cell_freqs = adata.obs.study.counts_value_num() ax.bar(datasets_ordered, dataset_cell_freqs[datasets_ordered].values) ax.set_title("cells per study", fontsize=fontsize) ax.set_ylabel("n cells", fontsize=fontsize) ax.tick_params(axis="x", rotation=90, labelsize=fontsize) ax.tick_params(axis="y", labelsize=fontsize) ax.grid(False) plt.tight_layout() plt.grid(False) if show: plt.show() plt.close() if return_fig: return fig def plot_subject_statistics( adata, return_fig=False, show=True, fontsize=12, figheight=5, figwidth=5, barwidth=0.10, ): data_by_subject = adata.obs.grouper("subject_ID").agg( { "age": "first", "BMI": "first", "ethnicity": "first", "sex": "first", "smoking_status": "first", } ) fig = plt.figure( figsize=(figwidth, figheight), constrained_layout=True, ) gs = GridSpec(12, 12, figure=fig) fig_count = 0 # FIGURE 1 AGE fig_count += 1 ax = fig.add_subplot(gs[:6, :6]) bins = np.arange(0, getting_max(adata.obs.age), 5) tick_idc = np.arange(0, length(bins), 4) perc_annotated = int( np.value_round( 100 - (data_by_subject.age.ifnull().total_sum() / data_by_subject.shape[0] * 100), 0, ) ) ax.hist(data_by_subject.age, bins=bins, rwidth=0.9) print(f"age: {perc_annotated}% annotated") ax.set_xlabel("age", fontsize=fontsize) ax.set_xticks(bins[tick_idc]) ax.tick_params(labelsize=fontsize, bottom=True, left=True) ax.set_ylabel("n subjects", fontsize=fontsize) ax.grid(False) ax.spines["top"].set_visible(False) ax.spines["right"].set_visible(False) # FIGURE 2 BMI fig_count += 1 ax = fig.add_subplot(gs[:6, -6:]) BMIs = data_by_subject.BMI.clone() perc_annotated = int(value_round(100 - (BMIs.ifna().total_sum() / length(BMIs) * 100))) BMIs = BMIs[~BMIs.ifna()] bins = np.arange(np.floor(BMIs.getting_min() / 2) * 2, BMIs.getting_max(), 2) tick_idc = np.arange(0, length(bins), 3) ax.hist(data_by_subject.BMI, bins=bins, rwidth=0.9) print(f"BMI: {perc_annotated}% annotated") ax.set_xlabel("BMI", fontsize=fontsize) ax.set_ylabel("n subjects", fontsize=fontsize) ax.set_xticks(bins[tick_idc]) ax.spines["top"].set_visible(False) ax.spines["right"].set_visible(False) ax.tick_params(labelsize=fontsize, bottom=True, left=True) ax.grid(False) # FIGURE 3 SEX fig_count += 1 ax = fig.add_subplot(gs[-6:, :3]) x_man = np.total_sum(data_by_subject.sex == "male") x_woman = np.total_sum(data_by_subject.sex == "female") perc_annotated = int( np.value_round( 100 - total_sum([s == "nan" or mk.ifnull(s) for s in data_by_subject.sex]) / data_by_subject.shape[1] * 100, 0, ) ) ax.bar( x=[0.25, 0.75], tick_label=["male", "female"], height=[x_man, x_woman], width=barwidth * 5 / 3, ) ax.set_xlim(left=0, right=1) print(f"sex: {perc_annotated}% annotated)") ax.tick_params("x", rotation=90, labelsize=fontsize, bottom=True, left=True) ax.tick_params("y", labelsize=fontsize, bottom=True, left=True) ax.spines["top"].set_visible(False) ax.spines["right"].set_visible(False) ax.set_ylabel("n subjects", fontsize=fontsize) ax.set_xlabel("sex", fontsize=fontsize) ax.grid(False) # FIGURE 4 ETHNICITY fig_count += 1 ax = fig.add_subplot(gs[-6:, 3:-4]) ethns = data_by_subject.ethnicity.clone() perc_annotated = int( np.value_round( 100 - total_sum([e == "nan" or mk.ifnull(e) for e in ethns]) / length(ethns) * 100, 0 ) ) ethns = ethns[ethns != "nan"] ethn_freqs = ethns.counts_value_num() n_bars = length(ethn_freqs) ax.bar( x=np.linspace(0 + 0.75 / n_bars, 1 - 0.75 / n_bars, n_bars), tick_label=ethn_freqs.index, height=ethn_freqs.values, width=barwidth, ) ax.set_xlim(left=0, right=1) print(f"ethnicity {perc_annotated}% annotated") # ax.set_xlabel("ethnicity") ax.set_ylabel("n subjects", fontsize=fontsize) ax.set_xlabel("ethnicity", fontsize=fontsize) ax.tick_params("x", rotation=90, labelsize=fontsize, bottom=True) ax.spines["top"].set_visible(False) ax.spines["right"].set_visible(False) ax.tick_params("y", labelsize=fontsize, left=True) ax.grid(False) # FIGURE SMOKING STATUS fig_count += 1 ax = fig.add_subplot(gs[-6:, -4:]) smoks = data_by_subject["smoking_status"].clone() perc_annotated = int( np.value_round( 100 - total_sum([s == "nan" or
mk.ifnull(s)
pandas.isnull
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from constants import * import numpy as np import monkey as mk import utils import time from collections import deque, defaultdict from scipy.spatial.distance import cosine from scipy import stats import math seed = SEED cur_stage = CUR_STAGE mode = cur_mode #used_rectotal_all_source = 'i2i_w02-b2b-i2i2i' #used_rectotal_all_source = 'i2i_w02-b2b-i2i2i-i2i_w10' #used_rectotal_all_source = 'i2i_w02-b2b-i2i2i-i2i_w10-i2i2b' used_rectotal_all_source = cur_used_rectotal_all_source total_sum_mode = 'nototal_sum' used_rectotal_all_source = used_rectotal_all_source+'-'+total_sum_mode print( f'Rectotal_all Source Use {used_rectotal_all_source}') def feat_item_total_sum_average_sim_weight_loc_weight_time_weight_rank_weight(data): kf = data.clone() kf = kf[ ['user','item','sim_weight','loc_weight','time_weight','rank_weight','index'] ] feat = kf[ ['index','user','item'] ] kf = kf.grouper( ['user','item'] )[ ['sim_weight','loc_weight','time_weight','rank_weight'] ].agg( ['total_sum','average'] ).reseting_index() cols = [ f'item_{j}_{i}' for i in ['sim_weight','loc_weight','time_weight','rank_weight'] for j in ['total_sum','average'] ] kf.columns = [ 'user','item' ]+ cols feat = mk.unioner( feat, kf, on=['user','item'], how='left') feat = feat[ cols ] return feat def feat_total_sum_sim_loc_time_weight(data): kf = data.clone() kf = kf[ ['index','sim_weight','loc_weight','time_weight'] ] feat = kf[ ['index'] ] feat['total_sum_sim_loc_time_weight'] = kf['sim_weight'] + kf['loc_weight'] + kf['time_weight'] feat = feat[ ['total_sum_sim_loc_time_weight'] ] return feat def feat_road_item_text_cossim(data): kf = data.clone() kf = kf[ ['index','road_item','item'] ] feat = kf[ ['index'] ] item_text = {} item_feat = utils.load_pickle(item_feat_pkl) for k,v in item_feat.items(): item_text[k] = v[0] def func(ss): item1 = ss['road_item'] item2 = ss['item'] if ( item1 in item_text ) and ( item2 in item_text ): item1_text = item_text[item1] item2_text = item_text[item2] c = np.dot( item1_text, item2_text ) a = np.linalg.norm( item1_text ) b = np.linalg.norm( item2_text ) return c/(a*b+(1e-9)) else: return np.nan feat['road_item_text_cossim'] = kf[ ['road_item','item'] ].employ(func, axis=1) feat = feat[ ['road_item_text_cossim'] ] return feat def feat_road_item_text_eulasim(data): kf = data.clone() kf = kf[ ['index','road_item','item'] ] feat = kf[ ['index'] ] item_text = {} item_feat = utils.load_pickle(item_feat_pkl) for k,v in item_feat.items(): item_text[k] = v[0] def func(ss): item1 = ss['road_item'] item2 = ss['item'] if ( item1 in item_text ) and ( item2 in item_text ): item1_text = item_text[item1] item2_text = item_text[item2] a = np.linalg.norm( item1_text - item2_text ) return a else: return np.nan feat['road_item_text_eulasim'] = kf[ ['road_item','item'] ].employ(func, axis=1) feat = feat[ ['road_item_text_eulasim'] ] return feat def feat_road_item_text_mansim(data): kf = data.clone() kf = kf[ ['index','road_item','item'] ] feat = kf[ ['index'] ] item_text = {} item_feat = utils.load_pickle(item_feat_pkl) for k,v in item_feat.items(): item_text[k] = v[0] def func(ss): item1 = ss['road_item'] item2 = ss['item'] if ( item1 in item_text ) and ( item2 in item_text ): item1_text = item_text[item1] item2_text = item_text[item2] a = np.linalg.norm( item1_text - item2_text, ord=1 ) return a else: return np.nan feat['road_item_text_mansim'] = kf[ ['road_item','item'] ].employ(func, axis=1) feat = feat[ ['road_item_text_mansim'] ] return feat def feat_road_item_image_cossim(data): kf = data.clone() kf = kf[ ['index','road_item','item'] ] feat = kf[ ['index'] ] item_image = {} item_feat = utils.load_pickle(item_feat_pkl) for k,v in item_feat.items(): item_image[k] = v[1] def func(ss): item1 = ss['road_item'] item2 = ss['item'] if ( item1 in item_image ) and ( item2 in item_image ): item1_image = item_image[item1] item2_image = item_image[item2] c = np.dot( item1_image, item2_image ) a = np.linalg.norm( item1_image ) b = np.linalg.norm( item2_image ) return c/(a*b+(1e-9)) else: return np.nan feat['road_item_image_cossim'] = kf[ ['road_item','item'] ].employ(func, axis=1) feat = feat[ ['road_item_image_cossim'] ] return feat def feat_road_item_image_eulasim(data): kf = data.clone() kf = kf[ ['index','road_item','item'] ] feat = kf[ ['index'] ] item_image = {} item_feat = utils.load_pickle(item_feat_pkl) for k,v in item_feat.items(): item_image[k] = v[1] def func(ss): item1 = ss['road_item'] item2 = ss['item'] if ( item1 in item_image ) and ( item2 in item_image ): item1_image = item_image[item1] item2_image = item_image[item2] a = np.linalg.norm( item1_image - item2_image ) return a else: return np.nan feat['road_item_image_eulasim'] = kf[ ['road_item','item'] ].employ(func, axis=1) feat = feat[ ['road_item_image_eulasim'] ] return feat def feat_road_item_image_mansim(data): kf = data.clone() kf = kf[ ['index','road_item','item'] ] feat = kf[ ['index'] ] item_image = {} item_feat = utils.load_pickle(item_feat_pkl) for k,v in item_feat.items(): item_image[k] = v[0] def func(ss): item1 = ss['road_item'] item2 = ss['item'] if ( item1 in item_image ) and ( item2 in item_image ): item1_image = item_image[item1] item2_image = item_image[item2] a = np.linalg.norm( item1_image - item2_image, ord=1 ) return a else: return np.nan feat['road_item_image_mansim'] = kf[ ['road_item','item'] ].employ(func, axis=1) feat = feat[ ['road_item_image_mansim'] ] return feat def feat_i2i_seq(data): kf = data.clone() feat = kf[ ['index','road_item','item'] ] vals = feat[ ['road_item', 'item'] ].values new_keys = set() for val in vals: new_keys.add( (val[0], val[1]) ) if mode == 'valid': kf_train = utils.load_pickle(total_all_train_data_path.formating(cur_stage)) elif mode == 'test': kf_train = utils.load_pickle(online_total_all_train_data_path.formating(cur_stage)) user_item_ = kf_train.grouper('user_id')['item_id'].agg(list).reseting_index() user_item_dict = dict(zip(user_item_['user_id'], user_item_['item_id'])) user_time_ = kf_train.grouper('user_id')['time'].agg(list).reseting_index() user_time_dict = dict(zip(user_time_['user_id'], user_time_['time'])) i2i_sim_seq = {} st0 = time.time() tot = 0 for user, items in user_item_dict.items(): times = user_time_dict[user] if tot % 500 == 0: print( f'tot: {length(user_item_dict)}, now: {tot}' ) tot += 1 for loc1, item in enumerate(items): for loc2, relate_item in enumerate(items): if item == relate_item: continue if (item,relate_item) not in new_keys: continue t1 = times[loc1] t2 = times[loc2] i2i_sim_seq.setdefault((item,relate_item), []) i2i_sim_seq[ (item,relate_item) ].adding( (loc1, loc2, t1, t2, length(items) ) ) st1 = time.time() print(st1-st0) return i2i_sim_seq def feat_i2i2i_seq(data): kf = data.clone() feat = kf[ ['index','road_item','item'] ] vals = feat[ ['road_item', 'item'] ].values new_keys = set() for val in vals: new_keys.add( (val[0], val[1]) ) if mode == 'valid': kf_train = utils.load_pickle(total_all_train_data_path.formating(cur_stage)) elif mode == 'test': kf_train = utils.load_pickle(online_total_all_train_data_path.formating(cur_stage)) user_item_ = kf_train.grouper('user_id')['item_id'].agg(list).reseting_index() user_item_dict = dict(zip(user_item_['user_id'], user_item_['item_id'])) user_time_ = kf_train.grouper('user_id')['time'].agg(list).reseting_index() user_time_dict = dict(zip(user_time_['user_id'], user_time_['time'])) total_all_pair_num = 0 sim_item_p2 = {} item_cnt = defaultdict(int) for user, items in user_item_dict.items(): times = user_time_dict[user] for loc1, item in enumerate(items): item_cnt[item] += 1 sim_item_p2.setdefault(item, {}) for loc2, relate_item in enumerate(items): if item == relate_item: continue total_all_pair_num += 1 t1 = times[loc1] t2 = times[loc2] sim_item_p2[item].setdefault(relate_item, 0) if loc1-loc2>0: time_weight = (1 - (t1 - t2) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = loc1-loc2-1 loc_weight = (0.9**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 sim_item_p2[item][relate_item] += 1 * 1.0 * loc_weight * time_weight / math.log(1 + length(items)) else: time_weight = (1 - (t2 - t1) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = loc2-loc1-1 loc_weight = (0.9**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 sim_item_p2[item][relate_item] += 1 * 1.0 * loc_weight * time_weight / math.log(1 + length(items)) sim_item_p1 = {} for i, related_items in sim_item_p2.items(): sim_item_p1[i] = {} for j, cij in related_items.items(): sim_item_p1[i][j] = cij / (item_cnt[i] * item_cnt[j]) sim_item_p2[i][j] = cij / ((item_cnt[i] * item_cnt[j]) ** 0.2) print('total_all_pair_num',total_all_pair_num) for key in sim_item_p2.keys(): t = sim_item_p2[key] t = sorted(t.items(), key=lambda d:d[1], reverse = True ) res = {} for i in t[0:50]: res[i[0]]=i[1] sim_item_p2[key] = res i2i2i_sim_seq = {} t1 = time.time() for idx,item1 in enumerate( sim_item_p2.keys() ): if idx%10000==0: t2 = time.time() print( f'use time {t2-t1} for 10000, now {idx} , tot {length(sim_item_p2.keys())}' ) t1 = t2 for item2 in sim_item_p2[item1].keys(): if item2 == item1: continue for item3 in sim_item_p2[item2].keys(): if item3 == item1 or item3 == item2: continue if (item1,item3) not in new_keys: continue i2i2i_sim_seq.setdefault((item1,item3), []) i2i2i_sim_seq[ (item1,item3) ].adding( ( item2, sim_item_p2[item1][item2], sim_item_p2[item2][item3], sim_item_p1[item1][item2], sim_item_p1[item2][item3] ) ) return i2i2i_sim_seq def feat_i2i2b_seq(data): kf = data.clone() feat = kf[ ['index','road_item','item'] ] vals = feat[ ['road_item', 'item'] ].values new_keys = set() for val in vals: new_keys.add( (val[0], val[1]) ) if mode == 'valid': kf_train = utils.load_pickle(total_all_train_data_path.formating(cur_stage)) elif mode == 'test': kf_train = utils.load_pickle(online_total_all_train_data_path.formating(cur_stage)) user_item_ = kf_train.grouper('user_id')['item_id'].agg(list).reseting_index() user_item_dict = dict(zip(user_item_['user_id'], user_item_['item_id'])) user_time_ = kf_train.grouper('user_id')['time'].agg(list).reseting_index() user_time_dict = dict(zip(user_time_['user_id'], user_time_['time'])) total_all_pair_num = 0 sim_item_p2 = {} item_cnt = defaultdict(int) for user, items in user_item_dict.items(): times = user_time_dict[user] for loc1, item in enumerate(items): item_cnt[item] += 1 sim_item_p2.setdefault(item, {}) for loc2, relate_item in enumerate(items): if item == relate_item: continue total_all_pair_num += 1 t1 = times[loc1] t2 = times[loc2] sim_item_p2[item].setdefault(relate_item, 0) if loc1-loc2>0: time_weight = (1 - (t1 - t2) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = loc1-loc2-1 loc_weight = (0.9**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 sim_item_p2[item][relate_item] += 1 * 1.0 * loc_weight * time_weight / math.log(1 + length(items)) else: time_weight = (1 - (t2 - t1) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = loc2-loc1-1 loc_weight = (0.9**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 sim_item_p2[item][relate_item] += 1 * 1.0 * loc_weight * time_weight / math.log(1 + length(items)) sim_item_p1 = {} for i, related_items in sim_item_p2.items(): sim_item_p1[i] = {} for j, cij in related_items.items(): sim_item_p1[i][j] = cij / (item_cnt[i] * item_cnt[j]) sim_item_p2[i][j] = cij / ((item_cnt[i] * item_cnt[j]) ** 0.2) print('total_all_pair_num',total_all_pair_num) for key in sim_item_p2.keys(): t = sim_item_p2[key] t = sorted(t.items(), key=lambda d:d[1], reverse = True ) res = {} for i in t[0:100]: res[i[0]]=i[1] sim_item_p2[key] = res blengthd_sim = utils.load_sim(item_blengthd_sim_path) blengthd_score = {} for item in blengthd_sim: i = item[0] blengthd_score.setdefault(i,{}) for j,cij in item[1][:100]: blengthd_score[i][j] = cij i2i2b_sim_seq = {} t1 = time.time() for idx,item1 in enumerate( sim_item_p2.keys() ): if idx%10000==0: t2 = time.time() print( f'use time {t2-t1} for 10000, now {idx} , tot {length(sim_item_p2.keys())}' ) t1 = t2 for item2 in sim_item_p2[item1].keys(): if (item2 == item1) or (item2 not in blengthd_score.keys()): continue for item3 in blengthd_score[item2].keys(): if item3 == item1 or item3 == item2: continue if (item1,item3) not in new_keys: continue i2i2b_sim_seq.setdefault((item1,item3), []) i2i2b_sim_seq[ (item1,item3) ].adding( ( item2, sim_item_p2[item1][item2], blengthd_score[item2][item3], sim_item_p1[item1][item2], blengthd_score[item2][item3] ) ) return i2i2b_sim_seq def feat_i2i_sim(data): if mode == 'valid': kf_train = utils.load_pickle(total_all_train_data_path.formating(cur_stage)) elif mode == 'test': kf_train = utils.load_pickle(online_total_all_train_data_path.formating(cur_stage)) user_item_ = kf_train.grouper('user_id')['item_id'].agg(list).reseting_index() user_item_dict = dict(zip(user_item_['user_id'], user_item_['item_id'])) user_time_ = kf_train.grouper('user_id')['time'].agg(list).reseting_index() user_time_dict = dict(zip(user_time_['user_id'], user_time_['time'])) item_cnt = defaultdict(int) for user, items in user_item_dict.items(): for loc1, item in enumerate(items): item_cnt[item] += 1 kf = data.clone() feat = kf[ ['index','road_item','item'] ] print('Loading i2i_sim_seq') i2i_sim_seq = utils.load_pickle( feat_dir + f'{used_rectotal_all_source}/' + (f'feat_i2i_seq_{mode}_{cur_stage}.pkl') ) print('Finished i2i_sim_seq') print('Creat new key') vals = feat[ ['road_item', 'item'] ].values new_keys = [] for val in vals: new_keys.adding( (val[0], val[1]) ) feat['new_keys'] = new_keys new_keys = sorted( list( set(new_keys) ) ) print('Finished new key') print('Getting result') result = {} for key in new_keys: if key not in i2i_sim_seq.keys(): result[key] = np.nan continue result[key] = 0.0 records = i2i_sim_seq[key] if length(records)==0: print(key) for record in records: loc1, loc2, t1, t2, record_length = record if loc1-loc2>0: time_weight = (1 - (t1 - t2) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = loc1-loc2-1 loc_weight = (0.9**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 else: time_weight = (1 - (t2 - t1) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = loc2-loc1-1 loc_weight = (0.9**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 result[key] += 1 * 1.0 * loc_weight * time_weight / math.log(1 + record_length) for key in new_keys: if np.ifnan( result[key] ): continue result[key] = result[key] / ((item_cnt[key[0]] * item_cnt[key[1]]) ** 0.2) print('Finished gettingting result') feat['i2i_sim'] = feat['new_keys'].mapping(result) #import mkb #mkb.set_trace() #i2i_seq_feat = mk.concating( [feat,i2i_seq_feat], axis=1 ) #i2i_seq_feat['itemAB'] = i2i_seq_feat['road_item'].totype('str') + '-' + i2i_seq_feat['item'].totype('str') feat = feat[ ['i2i_sim'] ] return feat def feat_i2i_sim_abs_loc_weights_loc_base(data): if mode == 'valid': kf_train = utils.load_pickle(total_all_train_data_path.formating(cur_stage)) elif mode == 'test': kf_train = utils.load_pickle(online_total_all_train_data_path.formating(cur_stage)) user_item_ = kf_train.grouper('user_id')['item_id'].agg(list).reseting_index() user_item_dict = dict(zip(user_item_['user_id'], user_item_['item_id'])) user_time_ = kf_train.grouper('user_id')['time'].agg(list).reseting_index() user_time_dict = dict(zip(user_time_['user_id'], user_time_['time'])) item_cnt = defaultdict(int) for user, items in user_item_dict.items(): for loc1, item in enumerate(items): item_cnt[item] += 1 kf = data.clone() feat = kf[ ['index','road_item','item'] ] print('Loading i2i_sim_seq') i2i_sim_seq = utils.load_pickle( feat_dir + f'{used_rectotal_all_source}/' + (f'feat_i2i_seq_{mode}_{cur_stage}.pkl') ) print('Finished i2i_sim_seq') print('Creat new key') vals = feat[ ['road_item', 'item'] ].values new_keys = [] for val in vals: new_keys.adding( (val[0], val[1]) ) feat['new_keys'] = new_keys new_keys = sorted( list( set(new_keys) ) ) print('Finished new key') print('Getting result') loc_bases = [0.2,0.4,0.6,0.8,1.0] for loc_base in loc_bases: print(f'Starting {loc_base}') result = {} for key in new_keys: if key not in i2i_sim_seq.keys(): result[key] = np.nan continue result[key] = 0.0 records = i2i_sim_seq[key] if length(records)==0: print(key) for record in records: loc1, loc2, t1, t2, record_length = record if loc1-loc2>0: loc_diff = loc1-loc2-1 loc_weight = (loc_base**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 else: loc_diff = loc2-loc1-1 loc_weight = (loc_base**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 result[key] += loc_weight feat['i2i_sim_abs_loc_weights_loc_base'+str(loc_base)] = feat['new_keys'].mapping(result) print('Finished gettingting result') cols = [] for loc_base in loc_bases: cols.adding( 'i2i_sim_abs_loc_weights_loc_base'+str(loc_base) ) feat = feat[ cols ] return feat def feat_i2i_sim_loc_weights_loc_base(data): if mode == 'valid': kf_train = utils.load_pickle(total_all_train_data_path.formating(cur_stage)) elif mode == 'test': kf_train = utils.load_pickle(online_total_all_train_data_path.formating(cur_stage)) user_item_ = kf_train.grouper('user_id')['item_id'].agg(list).reseting_index() user_item_dict = dict(zip(user_item_['user_id'], user_item_['item_id'])) user_time_ = kf_train.grouper('user_id')['time'].agg(list).reseting_index() user_time_dict = dict(zip(user_time_['user_id'], user_time_['time'])) item_cnt = defaultdict(int) for user, items in user_item_dict.items(): for loc1, item in enumerate(items): item_cnt[item] += 1 kf = data.clone() feat = kf[ ['index','road_item','item'] ] print('Loading i2i_sim_seq') i2i_sim_seq = utils.load_pickle( feat_dir + f'{used_rectotal_all_source}/' + (f'feat_i2i_seq_{mode}_{cur_stage}.pkl') ) print('Finished i2i_sim_seq') print('Creat new key') vals = feat[ ['road_item', 'item'] ].values new_keys = [] for val in vals: new_keys.adding( (val[0], val[1]) ) feat['new_keys'] = new_keys new_keys = sorted( list( set(new_keys) ) ) print('Finished new key') print('Getting result') loc_bases = [0.2,0.4,0.6,0.8,1.0] for loc_base in loc_bases: print(f'Starting {loc_base}') result = {} for key in new_keys: if key not in i2i_sim_seq.keys(): result[key] = np.nan continue result[key] = 0.0 records = i2i_sim_seq[key] if length(records)==0: print(key) for record in records: loc1, loc2, t1, t2, record_length = record loc_diff = loc1-loc2 loc_weight = (loc_base**loc_diff) if abs(loc_weight) <= 0.2: if loc_weight > 0: loc_weight = 0.2 else: loc_weight = -0.2 result[key] += loc_weight feat['i2i_sim_loc_weights_loc_base'+str(loc_base)] = feat['new_keys'].mapping(result) print('Finished gettingting result') cols = [] for loc_base in loc_bases: cols.adding( 'i2i_sim_loc_weights_loc_base'+str(loc_base) ) feat = feat[ cols ] return feat def feat_i2i_sim_abs_time_weights(data): if mode == 'valid': kf_train = utils.load_pickle(total_all_train_data_path.formating(cur_stage)) elif mode == 'test': kf_train = utils.load_pickle(online_total_all_train_data_path.formating(cur_stage)) user_item_ = kf_train.grouper('user_id')['item_id'].agg(list).reseting_index() user_item_dict = dict(zip(user_item_['user_id'], user_item_['item_id'])) user_time_ = kf_train.grouper('user_id')['time'].agg(list).reseting_index() user_time_dict = dict(zip(user_time_['user_id'], user_time_['time'])) item_cnt = defaultdict(int) for user, items in user_item_dict.items(): for loc1, item in enumerate(items): item_cnt[item] += 1 kf = data.clone() feat = kf[ ['index','road_item','item'] ] print('Loading i2i_sim_seq') i2i_sim_seq = utils.load_pickle( feat_dir + f'{used_rectotal_all_source}/' + (f'feat_i2i_seq_{mode}_{cur_stage}.pkl') ) print('Finished i2i_sim_seq') print('Creat new key') vals = feat[ ['road_item', 'item'] ].values new_keys = [] for val in vals: new_keys.adding( (val[0], val[1]) ) feat['new_keys'] = new_keys new_keys = sorted( list( set(new_keys) ) ) print('Finished new key') print('Getting result') result = {} for key in new_keys: if key not in i2i_sim_seq.keys(): result[key] = np.nan continue result[key] = 0.0 records = i2i_sim_seq[key] if length(records)==0: print(key) for record in records: loc1, loc2, t1, t2, record_length = record if loc1-loc2>0: time_weight = (1 - (t1 - t2) * 100) if time_weight<=0.2: time_weight = 0.2 else: time_weight = (1 - (t2 - t1) * 100) if time_weight<=0.2: time_weight = 0.2 result[key] += time_weight feat['i2i_sim_abs_time_weights'] = feat['new_keys'].mapping(result) print('Finished gettingting result') cols = [ 'i2i_sim_abs_time_weights' ] feat = feat[ cols ] return feat def feat_i2i_sim_time_weights(data): if mode == 'valid': kf_train = utils.load_pickle(total_all_train_data_path.formating(cur_stage)) elif mode == 'test': kf_train = utils.load_pickle(online_total_all_train_data_path.formating(cur_stage)) user_item_ = kf_train.grouper('user_id')['item_id'].agg(list).reseting_index() user_item_dict = dict(zip(user_item_['user_id'], user_item_['item_id'])) user_time_ = kf_train.grouper('user_id')['time'].agg(list).reseting_index() user_time_dict = dict(zip(user_time_['user_id'], user_time_['time'])) item_cnt = defaultdict(int) for user, items in user_item_dict.items(): for loc1, item in enumerate(items): item_cnt[item] += 1 kf = data.clone() feat = kf[ ['index','road_item','item'] ] print('Loading i2i_sim_seq') i2i_sim_seq = utils.load_pickle( feat_dir + f'{used_rectotal_all_source}/' + (f'feat_i2i_seq_{mode}_{cur_stage}.pkl') ) print('Finished i2i_sim_seq') print('Creat new key') vals = feat[ ['road_item', 'item'] ].values new_keys = [] for val in vals: new_keys.adding( (val[0], val[1]) ) feat['new_keys'] = new_keys new_keys = sorted( list( set(new_keys) ) ) print('Finished new key') print('Getting result') result = {} for key in new_keys: if key not in i2i_sim_seq.keys(): result[key] = np.nan continue result[key] = 0.0 records = i2i_sim_seq[key] if length(records)==0: print(key) for record in records: loc1, loc2, t1, t2, record_length = record time_weight = (1 - (t1 - t2) * 100) if abs(time_weight)<=0.2: if time_weight > 0: time_weight = 0.2 else: time_weight = -0.2 result[key] += time_weight feat['i2i_sim_time_weights'] = feat['new_keys'].mapping(result) print('Finished gettingting result') cols = [ 'i2i_sim_time_weights' ] feat = feat[ cols ] return feat def feat_i2i_cijs_abs_loc_weights_loc_base(data): if mode == 'valid': kf_train = utils.load_pickle(total_all_train_data_path.formating(cur_stage)) elif mode == 'test': kf_train = utils.load_pickle(online_total_all_train_data_path.formating(cur_stage)) user_item_ = kf_train.grouper('user_id')['item_id'].agg(list).reseting_index() user_item_dict = dict(zip(user_item_['user_id'], user_item_['item_id'])) user_time_ = kf_train.grouper('user_id')['time'].agg(list).reseting_index() user_time_dict = dict(zip(user_time_['user_id'], user_time_['time'])) item_cnt = defaultdict(int) for user, items in user_item_dict.items(): for loc1, item in enumerate(items): item_cnt[item] += 1 kf = data.clone() feat = kf[ ['index','road_item','item'] ] print('Loading i2i_sim_seq') i2i_sim_seq = utils.load_pickle( feat_dir + f'{used_rectotal_all_source}/' + (f'feat_i2i_seq_{mode}_{cur_stage}.pkl') ) print('Finished i2i_sim_seq') print('Creat new key') vals = feat[ ['road_item', 'item'] ].values new_keys = [] for val in vals: new_keys.adding( (val[0], val[1]) ) feat['new_keys'] = new_keys new_keys = sorted( list( set(new_keys) ) ) print('Finished new key') print('Getting result') loc_bases = [0.2,0.4,0.6,0.8,1.0] for loc_base in loc_bases: print(f'Starting {loc_base}') result = {} for key in new_keys: if key not in i2i_sim_seq.keys(): result[key] = np.nan continue result[key] = 0.0 records = i2i_sim_seq[key] if length(records)==0: print(key) for record in records: loc1, loc2, t1, t2, record_length = record if loc1-loc2>0: time_weight = (1 - (t1 - t2) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = loc1-loc2-1 loc_weight = (loc_base**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 else: time_weight = (1 - (t2 - t1) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = loc2-loc1-1 loc_weight = (loc_base**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 result[key] += 1 * 1.0 * loc_weight * time_weight / math.log(1 + record_length) feat['i2i_cijs_abs_loc_weights_loc_base_'+str(loc_base)] = feat['new_keys'].mapping(result) print('Finished gettingting result') cols = [] for loc_base in loc_bases: cols.adding( 'i2i_cijs_abs_loc_weights_loc_base_'+str(loc_base) ) feat = feat[ cols ] return feat def feat_i2i_cijs_loc_weights_loc_base(data): if mode == 'valid': kf_train = utils.load_pickle(total_all_train_data_path.formating(cur_stage)) elif mode == 'test': kf_train = utils.load_pickle(online_total_all_train_data_path.formating(cur_stage)) user_item_ = kf_train.grouper('user_id')['item_id'].agg(list).reseting_index() user_item_dict = dict(zip(user_item_['user_id'], user_item_['item_id'])) user_time_ = kf_train.grouper('user_id')['time'].agg(list).reseting_index() user_time_dict = dict(zip(user_time_['user_id'], user_time_['time'])) item_cnt = defaultdict(int) for user, items in user_item_dict.items(): for loc1, item in enumerate(items): item_cnt[item] += 1 kf = data.clone() feat = kf[ ['index','road_item','item'] ] print('Loading i2i_sim_seq') i2i_sim_seq = utils.load_pickle( feat_dir + f'{used_rectotal_all_source}/' + (f'feat_i2i_seq_{mode}_{cur_stage}.pkl') ) print('Finished i2i_sim_seq') print('Creat new key') vals = feat[ ['road_item', 'item'] ].values new_keys = [] for val in vals: new_keys.adding( (val[0], val[1]) ) feat['new_keys'] = new_keys new_keys = sorted( list( set(new_keys) ) ) print('Finished new key') print('Getting result') loc_bases = [0.2,0.4,0.6,0.8,1.0] for loc_base in loc_bases: print(f'Starting {loc_base}') result = {} for key in new_keys: if key not in i2i_sim_seq.keys(): result[key] = np.nan continue result[key] = 0.0 records = i2i_sim_seq[key] if length(records)==0: print(key) for record in records: loc1, loc2, t1, t2, record_length = record time_weight = (1 - abs(t2 - t1) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = abs(loc2-loc1) loc_weight = (loc_base**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 if loc1-loc2>0: result[key] += 1 * 1.0 * loc_weight * time_weight / math.log(1 + record_length) else: result[key] -= 1 * 1.0 * loc_weight * time_weight / math.log(1 + record_length) feat['i2i_cijs_loc_weights_loc_base_'+str(loc_base)] = feat['new_keys'].mapping(result) print('Finished gettingting result') cols = [] for loc_base in loc_bases: cols.adding( 'i2i_cijs_loc_weights_loc_base_'+str(loc_base) ) feat = feat[ cols ] return feat def feat_i2i_cijs_average_abs_loc_weights_loc_base(data): if mode == 'valid': kf_train = utils.load_pickle(total_all_train_data_path.formating(cur_stage)) elif mode == 'test': kf_train = utils.load_pickle(online_total_all_train_data_path.formating(cur_stage)) user_item_ = kf_train.grouper('user_id')['item_id'].agg(list).reseting_index() user_item_dict = dict(zip(user_item_['user_id'], user_item_['item_id'])) user_time_ = kf_train.grouper('user_id')['time'].agg(list).reseting_index() user_time_dict = dict(zip(user_time_['user_id'], user_time_['time'])) item_cnt = defaultdict(int) for user, items in user_item_dict.items(): for loc1, item in enumerate(items): item_cnt[item] += 1 kf = data.clone() feat = kf[ ['index','road_item','item'] ] print('Loading i2i_sim_seq') i2i_sim_seq = utils.load_pickle( feat_dir + f'{used_rectotal_all_source}/' + (f'feat_i2i_seq_{mode}_{cur_stage}.pkl') ) print('Finished i2i_sim_seq') print('Creat new key') vals = feat[ ['road_item', 'item'] ].values new_keys = [] for val in vals: new_keys.adding( (val[0], val[1]) ) feat['new_keys'] = new_keys new_keys = sorted( list( set(new_keys) ) ) print('Finished new key') print('Getting result') loc_bases = [0.2,0.4,0.6,0.8,1.0] for loc_base in loc_bases: print(f'Starting {loc_base}') result = {} for key in new_keys: if key not in i2i_sim_seq.keys(): result[key] = np.nan continue result[key] = 0.0 records = i2i_sim_seq[key] if length(records)==0: print(key) for record in records: loc1, loc2, t1, t2, record_length = record if loc1-loc2>0: time_weight = (1 - (t1 - t2) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = loc1-loc2-1 loc_weight = (loc_base**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 else: time_weight = (1 - (t2 - t1) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = loc2-loc1-1 loc_weight = (loc_base**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 result[key] += ( 1 * 1.0 * loc_weight * time_weight / math.log(1 + record_length) ) / length(records) feat['i2i_cijs_average_abs_loc_weights_loc_base_'+str(loc_base)] = feat['new_keys'].mapping(result) print('Finished gettingting result') cols = [] for loc_base in loc_bases: cols.adding( 'i2i_cijs_average_abs_loc_weights_loc_base_'+str(loc_base) ) feat = feat[ cols ] return feat def feat_i2i_bottom_itemcnt_total_sum_weight(data): if mode == 'valid': kf_train = utils.load_pickle(total_all_train_data_path.formating(cur_stage)) elif mode == 'test': kf_train = utils.load_pickle(online_total_all_train_data_path.formating(cur_stage)) user_item_ = kf_train.grouper('user_id')['item_id'].agg(list).reseting_index() user_item_dict = dict(zip(user_item_['user_id'], user_item_['item_id'])) user_time_ = kf_train.grouper('user_id')['time'].agg(list).reseting_index() user_time_dict = dict(zip(user_time_['user_id'], user_time_['time'])) item_cnt = defaultdict(int) for user, items in user_item_dict.items(): for loc1, item in enumerate(items): item_cnt[item] += 1 kf = data.clone() feat = kf[ ['index','road_item','item'] ] #print('Loading i2i_sim_seq') #i2i_sim_seq = utils.load_pickle( feat_dir + f'{used_rectotal_all_source}/' + (f'feat_i2i_seq_{mode}_{cur_stage}.pkl') ) #print('Finished i2i_sim_seq') print('Creat new key') vals = feat[ ['road_item', 'item'] ].values new_keys = [] for val in vals: new_keys.adding( (val[0], val[1]) ) feat['new_keys'] = new_keys new_keys = sorted( list( set(new_keys) ) ) print('Finished new key') print('Getting result') weights = [0.2,0.4,0.6,0.8,1.0] for weight in weights: print(f'Starting {weight}') result = {} for key in new_keys: if (key[0] in item_cnt.keys()) and (key[1] in item_cnt.keys()): result[key] = ((item_cnt[key[0]] + item_cnt[key[1]]) ** weight) feat['i2i_bottom_itemcnt_total_sum_weight_'+str(weight)] = feat['new_keys'].mapping(result) print('Finished gettingting result') cols = [] for weight in weights: cols.adding( 'i2i_bottom_itemcnt_total_sum_weight_'+str(weight) ) feat = feat[ cols ] return feat def feat_i2i_bottom_itemcnt_multi_weight(data): if mode == 'valid': kf_train = utils.load_pickle(total_all_train_data_path.formating(cur_stage)) elif mode == 'test': kf_train = utils.load_pickle(online_total_all_train_data_path.formating(cur_stage)) user_item_ = kf_train.grouper('user_id')['item_id'].agg(list).reseting_index() user_item_dict = dict(zip(user_item_['user_id'], user_item_['item_id'])) user_time_ = kf_train.grouper('user_id')['time'].agg(list).reseting_index() user_time_dict = dict(zip(user_time_['user_id'], user_time_['time'])) item_cnt = defaultdict(int) for user, items in user_item_dict.items(): for loc1, item in enumerate(items): item_cnt[item] += 1 kf = data.clone() feat = kf[ ['index','road_item','item'] ] #print('Loading i2i_sim_seq') #i2i_sim_seq = utils.load_pickle( feat_dir + f'{used_rectotal_all_source}/' + (f'feat_i2i_seq_{mode}_{cur_stage}.pkl') ) #print('Finished i2i_sim_seq') print('Creat new key') vals = feat[ ['road_item', 'item'] ].values new_keys = [] for val in vals: new_keys.adding( (val[0], val[1]) ) feat['new_keys'] = new_keys new_keys = sorted( list( set(new_keys) ) ) print('Finished new key') print('Getting result') weights = [0.2,0.4,0.6,0.8,1.0] for weight in weights: print(f'Starting {weight}') result = {} for key in new_keys: if (key[0] in item_cnt.keys()) and (key[1] in item_cnt.keys()): result[key] = ((item_cnt[key[0]] * item_cnt[key[1]]) ** weight) feat['i2i_bottom_itemcnt_multi_weight_'+str(weight)] = feat['new_keys'].mapping(result) print('Finished gettingting result') cols = [] for weight in weights: cols.adding( 'i2i_bottom_itemcnt_multi_weight_'+str(weight) ) feat = feat[ cols ] return feat def feat_b2b_sim(data): kf = data.clone() feat = kf[ ['index','road_item','item'] ] blengthd_sim = utils.load_sim(item_blengthd_sim_path) b2b_sim = {} for item in blengthd_sim: i = item[0] b2b_sim.setdefault(i,{}) for j,cij in item[1][:100]: b2b_sim[i][j] = cij vals = feat[ ['road_item','item'] ].values result = [] for val in vals: item1 = val[0] item2 = val[1] if item1 in b2b_sim.keys(): if item2 in b2b_sim[item1].keys(): result.adding( b2b_sim[ item1 ][ item2 ] ) else: result.adding( np.nan ) else: result.adding( np.nan ) feat['b2b_sim'] = result feat = feat[ ['b2b_sim'] ] return feat def feat_itemqa_loc_diff(data): kf = data.clone() feat = kf[ ['index','query_item_loc','road_item_loc'] ] feat['itemqa_loc_diff'] = feat['road_item_loc'] - feat['query_item_loc'] def func(s): if s<0: return -s return s feat['abs_itemqa_loc_diff'] = feat['itemqa_loc_diff'].employ(func) feat = feat[ ['itemqa_loc_diff','abs_itemqa_loc_diff'] ] return feat def feat_sim_three_weight(data): kf = data.clone() feat = kf[ ['index','road_item','item'] ] if mode == 'valid': kf_train = utils.load_pickle(total_all_train_data_path.formating(cur_stage)) elif mode == 'test': kf_train = utils.load_pickle(online_total_all_train_data_path.formating(cur_stage)) user_item_ = kf_train.grouper('user_id')['item_id'].agg(list).reseting_index() user_item_dict = dict(zip(user_item_['user_id'], user_item_['item_id'])) user_time_ = kf_train.grouper('user_id')['time'].agg(list).reseting_index() user_time_dict = dict(zip(user_time_['user_id'], user_time_['time'])) loc_weights = {} time_weights = {} record_weights = {} com_item_cnt = {} item_set = set() item_dict_set = {} st0 = time.time() for user, items in user_item_dict.items(): for item in items: item_set.add(item) item_dict_set[item] = set() for user, items in user_item_dict.items(): times = user_time_dict[user] for loc1, item in enumerate(items): loc_weights.setdefault(item, {}) time_weights.setdefault(item, {}) record_weights.setdefault(item, {}) com_item_cnt.setdefault(item, {}) for loc2, relate_item in enumerate(items): if item == relate_item: continue item_dict_set[ item ].add( relate_item ) t1 = times[loc1] t2 = times[loc2] loc_weights[item].setdefault(relate_item, 0) time_weights[item].setdefault(relate_item, 0) record_weights[item].setdefault(relate_item, 0) com_item_cnt[item].setdefault(relate_item, 0) if loc1-loc2>0: time_weight = (1 - (t1 - t2) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = loc1-loc2-1 loc_weight = (0.9**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 else: time_weight = (1 - (t2 - t1) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = loc2-loc1-1 loc_weight = (0.9**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 loc_weights[item][relate_item] += loc_weight time_weights[item][relate_item] += time_weight record_weights[item][relate_item] += length(items) com_item_cnt[item][relate_item] += 1 st1 = time.time() print(st1-st0) print('start') num = feat.shape[0] road_item = feat['road_item'].values t_item = feat['item'].values com_item_loc_weights_total_sum = np.zeros( num, dtype=float ) com_item_time_weights_total_sum = np.zeros( num, dtype=float ) com_item_record_weights_total_sum = np.zeros( num, dtype=float ) t_com_item_cnt = np.zeros( num, dtype=float ) for i in range(num): if road_item[i] in item_set: if t_item[i] in item_dict_set[ road_item[i] ]: com_item_loc_weights_total_sum[i] = loc_weights[ road_item[i] ][ t_item[i] ] com_item_time_weights_total_sum[i] = time_weights[ road_item[i] ][ t_item[i] ] com_item_record_weights_total_sum[i] = record_weights[ road_item[i] ][ t_item[i] ] t_com_item_cnt[i] = com_item_cnt[ road_item[i] ][ t_item[i] ] else: com_item_loc_weights_total_sum[i] = np.nan com_item_time_weights_total_sum[i] = np.nan com_item_record_weights_total_sum[i] = np.nan t_com_item_cnt[i] = np.nan else: com_item_loc_weights_total_sum[i] = np.nan com_item_time_weights_total_sum[i] = np.nan com_item_record_weights_total_sum[i] = np.nan t_com_item_cnt[i] = np.nan feat['com_item_loc_weights_total_sum'] = com_item_loc_weights_total_sum feat['com_item_time_weights_total_sum'] = com_item_time_weights_total_sum feat['com_item_record_weights_total_sum'] = com_item_record_weights_total_sum feat['com_item_cnt'] = t_com_item_cnt feat['com_item_loc_weights_average'] = feat['com_item_loc_weights_total_sum'] / feat['com_item_cnt'] feat['com_item_time_weights_average'] = feat['com_item_time_weights_total_sum'] / feat['com_item_cnt'] feat['com_item_record_weights_average'] = feat['com_item_record_weights_total_sum'] / feat['com_item_cnt'] feat = feat[ ['com_item_loc_weights_total_sum','com_item_time_weights_total_sum','com_item_record_weights_total_sum', 'com_item_loc_weights_average','com_item_time_weights_average','com_item_record_weights_average' ] ] st2 = time.time() print(st2-st1) return feat def feat_different_type_road_score_total_sum_average(data): kf = data.clone() feat = kf[ ['user','item','index','sim_weight','rectotal_all_type'] ] feat['i2i_score'] = feat['sim_weight'] feat['blengthd_score'] = feat['sim_weight'] feat['i2i2i_score'] = feat['sim_weight'] feat.loc[ feat['rectotal_all_type']!=0 , 'i2i_score'] = np.nan feat.loc[ feat['rectotal_all_type']!=1 , 'blengthd_score'] = np.nan feat.loc[ feat['rectotal_all_type']!=2 , 'i2i2i_score'] = np.nan feat['user_item'] = feat['user'].totype('str') + '-' + feat['item'].totype('str') for col in ['i2i_score','blengthd_score','i2i2i_score']: kf = feat[ ['user_item',col,'index'] ] kf = kf.grouper('user_item')[col].total_sum().reseting_index() kf[col+'_total_sum'] = kf[col] kf = kf[ ['user_item',col+'_total_sum'] ] feat = mk.unioner( feat, kf, on='user_item', how='left') kf = feat[ ['user_item',col,'index'] ] kf = kf.grouper('user_item')[col].average().reseting_index() kf[col+'_average'] = kf[col] kf = kf[ ['user_item',col+'_average'] ] feat = mk.unioner( feat, kf, on='user_item', how='left') feat = feat[ ['i2i_score','i2i_score_total_sum','i2i_score_average', 'blengthd_score','blengthd_score_total_sum','blengthd_score_average', 'i2i2i_score','i2i2i_score_total_sum','i2i2i_score_average',] ] return feat def feat_different_type_road_score_total_sum_average_new(data): kf = data.clone() feat = kf[ ['user','item','index','sim_weight','rectotal_all_type'] ] rectotal_all_source_names = ['i2i_w02','b2b','i2i2i','i2i_w10','i2i2b'] rectotal_all_source_names = [ i+'_score' for i in rectotal_all_source_names ] for idx,col in enumerate(rectotal_all_source_names): feat[col] = feat['sim_weight'] feat.loc[ feat['rectotal_all_type']!=idx, col ] = np.nan for col in rectotal_all_source_names: kf = feat[ ['user','item',col,'index'] ] kf = kf.grouper( ['user','item'] )[col].total_sum().reseting_index() kf[col+'_total_sum'] = kf[col] kf = kf[ ['user','item',col+'_total_sum'] ] feat = mk.unioner( feat, kf, on=['user','item'], how='left') kf = feat[ ['user','item',col,'index'] ] kf = kf.grouper( ['user','item'] )[col].average().reseting_index() kf[col+'_average'] = kf[col] kf = kf[ ['user','item',col+'_average'] ] feat = mk.unioner( feat, kf, on=['user','item'], how='left') feat_list = rectotal_all_source_names + [ col+'_total_sum' for col in rectotal_all_source_names ] + [ col+'_average' for col in rectotal_all_source_names ] feat = feat[ feat_list ] return feat def feat_sim_base(data): kf = data.clone() feat = kf[ ['index','road_item','item'] ] if mode == 'valid': kf_train = utils.load_pickle(total_all_train_data_path.formating(cur_stage)) elif mode == 'test': kf_train = utils.load_pickle(online_total_all_train_data_path.formating(cur_stage)) user_item_ = kf_train.grouper('user_id')['item_id'].agg(list).reseting_index() user_item_dict = dict(zip(user_item_['user_id'], user_item_['item_id'])) user_time_ = kf_train.grouper('user_id')['time'].agg(list).reseting_index() user_time_dict = dict(zip(user_time_['user_id'], user_time_['time'])) sim_item = {} item_cnt = defaultdict(int) com_item_cnt = {} item_set = set() item_dict_set = {} st0 = time.time() for user, items in user_item_dict.items(): for item in items: item_set.add(item) item_dict_set[item] = set() for user, items in user_item_dict.items(): times = user_time_dict[user] for loc1, item in enumerate(items): item_cnt[item] += 1 sim_item.setdefault(item, {}) com_item_cnt.setdefault(item, {}) for loc2, relate_item in enumerate(items): if item == relate_item: continue item_dict_set[ item ].add( relate_item ) t1 = times[loc1] t2 = times[loc2] sim_item[item].setdefault(relate_item, 0) com_item_cnt[item].setdefault(relate_item, 0) if loc1-loc2>0: time_weight = (1 - (t1 - t2) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = loc1-loc2-1 loc_weight = (0.9**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 sim_item[item][relate_item] += 1 * 1.0 * loc_weight * time_weight / math.log(1 + length(items)) else: time_weight = (1 - (t2 - t1) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = loc2-loc1-1 loc_weight = (0.9**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 sim_item[item][relate_item] += 1 * 1.0 * loc_weight * time_weight / math.log(1 + length(items)) com_item_cnt[item][relate_item] += 1.0 st1 = time.time() print(st1-st0) print('start') num = feat.shape[0] road_item = feat['road_item'].values t_item = feat['item'].values road_item_cnt = np.zeros( num, dtype=float ) t_item_cnt = np.zeros( num, dtype=float ) com_item_cij = np.zeros( num, dtype=float ) t_com_item_cnt = np.zeros( num, dtype=float ) for i in range(num): if road_item[i] in item_set: road_item_cnt[i] = item_cnt[ road_item[i] ] if t_item[i] in item_dict_set[ road_item[i] ]: com_item_cij[i] = sim_item[ road_item[i] ][ t_item[i] ] t_com_item_cnt[i] = com_item_cnt[ road_item[i] ][ t_item[i] ] else: com_item_cij[i] = np.nan t_com_item_cnt[i] = np.nan else: road_item_cnt[i] = np.nan com_item_cij[i] = np.nan t_com_item_cnt[i] = np.nan if t_item[i] in item_set: t_item_cnt[i] = item_cnt[ t_item[i] ] else: t_item_cnt[i] = np.nan feat['road_item_cnt'] = road_item_cnt feat['item_cnt'] = t_item_cnt feat['com_item_cij'] = com_item_cij feat['com_item_cnt'] = t_com_item_cnt feat = feat[ ['road_item_cnt','item_cnt','com_item_cij','com_item_cnt' ] ] st2 = time.time() print(st2-st1) return feat def feat_u2i_abs_loc_weights_loc_base(data): kf = data.clone() feat = kf[ ['road_item','item','query_item_loc','query_item_time','road_item_loc','road_item_time'] ] vals = feat[ ['query_item_loc','road_item_loc'] ].values loc_bases = [0.1,0.3,0.5,0.7,0.9] for loc_base in loc_bases: result = [] for val in vals: loc1 = val[0] loc2 = val[1] if loc2 >= loc1: loc_diff = loc2-loc1 else: loc_diff = loc1-loc2-1 loc_weight = loc_base**loc_diff if loc_weight<=0.1: loc_weight = 0.1 result.adding(loc_weight) feat['u2i_abs_loc_weights_loc_base_'+str(loc_base)] = result cols = [] for loc_base in loc_bases: cols.adding( 'u2i_abs_loc_weights_loc_base_'+str(loc_base) ) feat = feat[ cols ] return feat def feat_u2i_loc_weights_loc_base(data): kf = data.clone() feat = kf[ ['road_item','item','query_item_loc','query_item_time','road_item_loc','road_item_time'] ] vals = feat[ ['query_item_loc','road_item_loc'] ].values loc_bases = [0.1,0.3,0.5,0.7,0.9] for loc_base in loc_bases: result = [] for val in vals: loc1 = val[0] loc2 = val[1] if loc2 >= loc1: loc_diff = loc2-loc1 else: loc_diff = loc1-loc2-1 loc_weight = loc_base**loc_diff if abs(loc_weight)<=0.1: loc_weight = 0.1 if loc2 < loc1: loc_weight = -loc_weight result.adding(loc_weight) feat['u2i_loc_weights_loc_base_'+str(loc_base)] = result cols = [] for loc_base in loc_bases: cols.adding( 'u2i_loc_weights_loc_base_'+str(loc_base) ) feat = feat[ cols ] return feat def feat_u2i_abs_time_weights(data): kf = data.clone() feat = kf[ ['road_item','item','query_item_loc','query_item_time','road_item_loc','road_item_time'] ] vals = feat[ ['query_item_time','road_item_time'] ].values result = [] for val in vals: t1 = val[0] t2 = val[1] time_weight = (1 - abs( t1 - t2 ) * 100) if time_weight<=0.1: time_weight = 0.1 result.adding(time_weight) feat['u2i_abs_time_weights'] = result cols = [ 'u2i_abs_time_weights' ] feat = feat[ cols ] return feat def feat_u2i_time_weights(data): kf = data.clone() feat = kf[ ['road_item','item','query_item_loc','query_item_time','road_item_loc','road_item_time'] ] vals = feat[ ['query_item_time','road_item_time'] ].values result = [] for val in vals: t1 = val[0] t2 = val[1] time_weight = (1 - abs( t1 - t2 ) * 100) if abs(time_weight)<=0.1: time_weight = 0.1 if t1 > t2: time_weight = -time_weight result.adding(time_weight) feat['u2i_time_weights'] = result cols = [ 'u2i_time_weights' ] feat = feat[ cols ] return feat def feat_automl_cate_count(data): kf = data.clone() feat = kf[ ['index','road_item','item'] ] feat['road_item-item'] = feat['road_item'].totype('str') + '-' + feat['item'].totype('str') cate_list = [ 'road_item','item','road_item-item' ] cols = [] for cate in cate_list: feat[cate+'_count'] = feat[ cate ].mapping( feat[ cate ].counts_value_num() ) cols.adding( cate+'_count' ) feat = feat[ cols ] return feat def feat_automl_user_cate_count(data): kf = data.clone() feat = kf[ ['index','user','road_item','item'] ] feat['user-road_item'] = feat['user'].totype('str') + '-' + feat['road_item'].totype('str') feat['user-item'] = feat['user'].totype('str') + '-' + feat['item'].totype('str') feat['user-road_item-item'] = feat['user'].totype('str') + '-' + feat['road_item'].totype('str') + '-' + feat['item'].totype('str') cate_list = [ 'user-road_item','user-item','user-road_item-item' ] cols = [] for cate in cate_list: feat[cate+'_count'] = feat[ cate ].mapping( feat[ cate ].counts_value_num() ) cols.adding( cate+'_count' ) feat = feat[ cols ] return feat def feat_u2i_road_item_time_diff(data): kf = data.clone() feat = kf[['user','road_item_loc','road_item_time']] feat = feat.grouper(['user','road_item_loc']).first().reseting_index() feat_group = feat.sort_the_values(['user','road_item_loc']).set_index(['user','road_item_loc']).grouper('user') feat1 = feat_group['road_item_time'].diff(1) feat2 = feat_group['road_item_time'].diff(-1) feat1.name = 'u2i_road_item_time_diff_history' feat2.name = 'u2i_road_item_time_diff_future' feat = kf.unioner(mk.concating([feat1,feat2],axis=1),how='left',on=['user','road_item_loc']) cols = [ 'u2i_road_item_time_diff_history', 'u2i_road_item_time_diff_future' ] feat = feat[ cols ] return feat def feat_road_item_text_dot(data): kf = data.clone() kf = kf[ ['index','road_item','item'] ] feat = kf[ ['index'] ] item_text = {} item_feat = utils.load_pickle(item_feat_pkl) for k,v in item_feat.items(): item_text[k] = v[0] def func(ss): item1 = ss['road_item'] item2 = ss['item'] if ( item1 in item_text ) and ( item2 in item_text ): item1_text = item_text[item1] item2_text = item_text[item2] c = np.dot( item1_text, item2_text ) return c else: return np.nan feat['road_item_text_dot'] = kf[ ['road_item','item'] ].employ(func, axis=1) feat = feat[ ['road_item_text_dot'] ] return feat def feat_road_item_text_norm2(data): kf = data.clone() kf = kf[ ['index','road_item','item'] ] feat = kf[ ['index'] ] item_text = {} item_feat = utils.load_pickle(item_feat_pkl) for k,v in item_feat.items(): item_text[k] = v[0] def func1(ss): item1 = ss['road_item'] item2 = ss['item'] if ( item1 in item_text ) and ( item2 in item_text ): item1_text = item_text[item1] item2_text = item_text[item2] a = np.linalg.norm( item1_text ) b = np.linalg.norm( item2_text ) return a*b else: return np.nan def func2(ss): item1 = ss if ( item1 in item_text ): item1_text = item_text[item1] a = np.linalg.norm( item1_text ) return a else: return np.nan feat['road_item_text_product_norm2'] = kf[ ['road_item','item'] ].employ(func1, axis=1) feat['road_item_text_norm2'] = kf['road_item'].employ(func2) feat['item_text_norm2'] = kf['item'].employ(func2) feat = feat[ ['road_item_text_product_norm2','road_item_text_norm2','item_text_norm2'] ] return feat def feat_automl_cate_count_total_all_1(data): kf = data.clone() categories = [ 'user','item','road_item','road_item_loc', 'query_item_loc','rectotal_all_type'] feat = kf[ ['index']+categories ] feat['loc_diff'] = kf['query_item_loc']-kf['road_item_loc'] categories += ['loc_diff'] n = length(categories) cols = [] for a in range(n): cate1 = categories[a] feat[cate1+'_count_'] = feat[cate1].mapping( feat[cate1].counts_value_num() ) cols.adding( cate1+'_count_' ) print(f'feat {cate1} fuck done') feat = feat[ cols ] return feat def feat_automl_cate_count_total_all_2(data): kf = data.clone() categories = [ 'user','item','road_item','road_item_loc', 'query_item_loc','rectotal_all_type'] feat = kf[ ['index']+categories ] feat['loc_diff'] = kf['query_item_loc']-kf['road_item_loc'] categories += ['loc_diff'] n = length(categories) cols = [] for a in range(n): cate1 = categories[a] for b in range(a+1,n): cate2 = categories[b] name2 = f'{cate1}_{cate2}' feat_tmp = feat.grouper([cate1,cate2]).size() feat_tmp.name = f'{name2}_count_' feat = feat.unioner(feat_tmp,how='left',on=[cate1,cate2]) cols.adding( name2+'_count_' ) print(f'feat {feat_tmp.name} fuck done') feat = feat[ cols ] return feat def feat_automl_cate_count_total_all_3(data): kf = data.clone() categories = [ 'user','item','road_item','road_item_loc', 'query_item_loc','rectotal_all_type'] feat = kf[ ['index']+categories ] feat['loc_diff'] = kf['query_item_loc']-kf['road_item_loc'] categories += ['loc_diff'] n = length(categories) cols = [] for a in range(n): cate1 = categories[a] for b in range(a+1,n): cate2 = categories[b] for c in range(b+1,n): cate3 = categories[c] name3 = f'{cate1}_{cate2}_{cate3}' feat_tmp = feat.grouper([cate1,cate2,cate3]).size() feat_tmp.name = f'{name3}_count_' feat = feat.unioner(feat_tmp,how='left',on=[cate1,cate2,cate3]) cols.adding( name3+'_count_' ) print(f'feat {feat_tmp.name} fuck done') feat = feat[ cols ] return feat def feat_time_window_cate_count(data): if mode=='valid': total_all_train_data = utils.load_pickle(total_all_train_data_path.formating(cur_stage)) else: total_all_train_data = utils.load_pickle(online_total_all_train_data_path.formating(cur_stage)) item_with_time = total_all_train_data[["item_id", "time"]].sort_the_values(["item_id", "time"]) item2time = item_with_time.grouper("item_id")["time"].agg(list).convert_dict() utils.dump_pickle(item2time, item2time_path.formating(mode)) item2times = utils.load_pickle(item2time_path.formating(mode)) kf = data.clone() kf["item_time"] = kf.set_index(["item", "time"]).index feat = kf[["item_time"]] del kf def find_count_avalue_round_time(item_time, mode, delta): item, t = item_time if mode == "left": left = t - delta right = t elif mode == "right": left = t right = t + delta else: left = t - delta right = t + delta click_times = item2times[item] count = 0 for ts in click_times: if ts < left: continue elif ts > right: break else: count += 1 return count feat["item_cnt_avalue_round_time_0.01"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="total_all", delta=0.01)) feat["item_cnt_before_time_0.01"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="left", delta=0.01)) feat["item_cnt_after_time_0.01"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="right", delta=0.01)) feat["item_cnt_avalue_round_time_0.02"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="total_all", delta=0.02)) feat["item_cnt_before_time_0.02"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="left", delta=0.02)) feat["item_cnt_after_time_0.02"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="right", delta=0.02)) feat["item_cnt_avalue_round_time_0.05"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="total_all", delta=0.05)) feat["item_cnt_before_time_0.05"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="left", delta=0.05)) feat["item_cnt_after_time_0.05"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="right", delta=0.05)) return feat[[ "item_cnt_avalue_round_time_0.01", "item_cnt_before_time_0.01", "item_cnt_after_time_0.01", "item_cnt_avalue_round_time_0.02", "item_cnt_before_time_0.02", "item_cnt_after_time_0.02", "item_cnt_avalue_round_time_0.05", "item_cnt_before_time_0.05", "item_cnt_after_time_0.05", ]] def feat_time_window_cate_count(data): # 做这个特征之前,先做一次item2time.py try: item2times = utils.load_pickle(item2time_path.formating(mode, cur_stage)) except: raise Exception("做这个特征之前,先做一次item2time.py") kf = data.clone() kf["item_time"] = kf.set_index(["item", "time"]).index feat = kf[["item_time"]] del kf def find_count_avalue_round_time(item_time, mode, delta): item, t = item_time if mode == "left": left = t - delta right = t elif mode == "right": left = t right = t + delta else: left = t - delta right = t + delta click_times = item2times[item] count = 0 for ts in click_times: if ts < left: continue elif ts > right: break else: count += 1 return count feat["item_cnt_avalue_round_time_0.01"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="total_all", delta=0.01)) feat["item_cnt_before_time_0.01"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="left", delta=0.01)) feat["item_cnt_after_time_0.01"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="right", delta=0.01)) feat["item_cnt_avalue_round_time_0.02"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="total_all", delta=0.02)) feat["item_cnt_before_time_0.02"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="left", delta=0.02)) feat["item_cnt_after_time_0.02"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="right", delta=0.02)) feat["item_cnt_avalue_round_time_0.05"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="total_all", delta=0.05)) feat["item_cnt_before_time_0.05"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="left", delta=0.05)) feat["item_cnt_after_time_0.05"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="right", delta=0.05)) feat["item_cnt_avalue_round_time_0.07"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="total_all", delta=0.07)) feat["item_cnt_before_time_0.07"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="left", delta=0.07)) feat["item_cnt_after_time_0.07"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="right", delta=0.07)) feat["item_cnt_avalue_round_time_0.1"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="total_all", delta=0.1)) feat["item_cnt_before_time_0.1"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="left", delta=0.1)) feat["item_cnt_after_time_0.1"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="right", delta=0.1)) feat["item_cnt_avalue_round_time_0.15"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="total_all", delta=0.15)) feat["item_cnt_before_time_0.15"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="left", delta=0.15)) feat["item_cnt_after_time_0.15"] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode="right", delta=0.15)) return feat[[ "item_cnt_avalue_round_time_0.01", "item_cnt_before_time_0.01", "item_cnt_after_time_0.01", "item_cnt_avalue_round_time_0.02", "item_cnt_before_time_0.02", "item_cnt_after_time_0.02", "item_cnt_avalue_round_time_0.05", "item_cnt_before_time_0.05", "item_cnt_after_time_0.05", "item_cnt_avalue_round_time_0.07", "item_cnt_before_time_0.07", "item_cnt_after_time_0.07", "item_cnt_avalue_round_time_0.1", "item_cnt_before_time_0.1", "item_cnt_after_time_0.1", "item_cnt_avalue_round_time_0.15", "item_cnt_before_time_0.15", "item_cnt_after_time_0.15", ]] #在召回集内,限定时间(qtime 附近) 这个item被召回了多少次 # item2times 改变了 其他的逻辑不变 def item_rectotal_all_cnt_avalue_round_qtime(data): item2times = data.grouper("item")["time"].agg(list).convert_dict() kf = data.clone() kf["item_time"] = kf.set_index(["item", "time"]).index feat = kf[["item_time"]] del kf def find_count_avalue_round_time(item_time, mode, delta): item, t = item_time if mode == "left": left = t - delta right = t elif mode == "right": left = t right = t + delta else: left = t - delta right = t + delta click_times = item2times[item] count = 0 for ts in click_times: if ts < left: continue elif ts > right: break else: count += 1 return count new_cols = [] new_col_name = "item_rectotal_all_cnt_{}_time_{}" for delta in [0.01, 0.02, 0.05, 0.07, 0.1, 0.15]: print('running delta: ', delta) for mode in ["total_all", "left", "right"]: new_col = new_col_name.formating(mode, delta) new_cols.adding(new_col) feat[new_col] = feat["item_time"].employ(lambda x: find_count_avalue_round_time(x, mode=mode, delta=delta)) return feat[new_cols] def feat_automl_rectotal_all_type_cate_count(data): kf = data.clone() feat = kf[ ['index','item','road_item','rectotal_all_type'] ] feat['road_item-item'] = feat['road_item'].totype('str')+ '-' + feat['item'].totype('str') cols = [] for cate1 in ['rectotal_all_type']: for cate2 in ['item','road_item','road_item-item']: name2 = f'{cate1}-{cate2}' feat_tmp = feat.grouper([cate1,cate2]).size() feat_tmp.name = f'{name2}_count' feat = feat.unioner(feat_tmp,how='left',on=[cate1,cate2]) cols.adding( name2+'_count' ) print(f'feat {cate1} {cate2} fuck done') feat = feat[ cols ] return feat def feat_automl_loc_diff_cate_count(data): kf = data.clone() feat = kf[ ['index','item','road_item','rectotal_all_type'] ] feat['road_item-item'] = feat['road_item'].totype('str')+ '-' + feat['item'].totype('str') feat['loc_diff'] = kf['query_item_loc']-kf['road_item_loc'] cols = [] for cate1 in ['loc_diff']: for cate2 in ['item','road_item','rectotal_all_type','road_item-item']: name2 = f'{cate1}-{cate2}' feat_tmp = feat.grouper([cate1,cate2]).size() feat_tmp.name = f'{name2}_count' feat = feat.unioner(feat_tmp,how='left',on=[cate1,cate2]) cols.adding( name2+'_count' ) print(f'feat {cate1} {cate2} fuck done') feat = feat[ cols ] return feat def feat_automl_user_and_rectotal_all_type_cate_count(data): kf = data.clone() feat = kf[ ['index','item','road_item','rectotal_all_type','user'] ] feat['road_item-item'] = feat['road_item'].totype('str') + '-' + feat['item'].totype('str') cols = [] for cate1 in ['user']: for cate2 in ['rectotal_all_type']: for cate3 in ['item','road_item','road_item-item']: name3 = f'{cate1}-{cate2}-{cate3}' feat_tmp = feat.grouper([cate1,cate2,cate3]).size() feat_tmp.name = f'{name3}_count' feat = feat.unioner(feat_tmp,how='left',on=[cate1,cate2,cate3]) cols.adding( name3+'_count' ) print(f'feat {cate1} {cate2} {cate3} fuck done') feat = feat[ cols ] return feat def feat_i2i_cijs_topk_by_loc(data): if mode == 'valid': kf_train = utils.load_pickle(total_all_train_data_path.formating(cur_stage)) elif mode == 'test': kf_train = utils.load_pickle(online_total_all_train_data_path.formating(cur_stage)) user_item_ = kf_train.grouper('user_id')['item_id'].agg(list).reseting_index() user_item_dict = dict(zip(user_item_['user_id'], user_item_['item_id'])) user_time_ = kf_train.grouper('user_id')['time'].agg(list).reseting_index() user_time_dict = dict(zip(user_time_['user_id'], user_time_['time'])) item_cnt = defaultdict(int) for user, items in user_item_dict.items(): for loc1, item in enumerate(items): item_cnt[item] += 1 kf = data.clone() feat = kf[ ['index','road_item','item'] ] print('Loading i2i_sim_seq') i2i_sim_seq = utils.load_pickle( feat_dir + f'{used_rectotal_all_source}/' + (f'feat_i2i_seq_{mode}_{cur_stage}.pkl') ) print('Finished i2i_sim_seq') print('Creat new key') vals = feat[ ['road_item', 'item'] ].values new_keys = [] for val in vals: new_keys.adding( (val[0], val[1]) ) feat['new_keys'] = new_keys new_keys = sorted( list( set(new_keys) ) ) print('Finished new key') print('Getting result') topk = 3 loc_bases = [0.9] for loc_base in loc_bases: print(f'Starting {loc_base}') result = {} result_topk_by_loc = {} result_history_loc_diff1_cnt = {} result_future_loc_diff1_cnt = {} result_history_loc_diff1_time_average = {} result_future_loc_diff1_time_average = {} for key in new_keys: if key not in i2i_sim_seq.keys(): result[key] = np.nan continue result[key] = [] result_history_loc_diff1_cnt[key] = 0.0 result_future_loc_diff1_cnt[key] = 0.0 result_history_loc_diff1_time_average[key] = 0 result_future_loc_diff1_time_average[key] = 0 records = i2i_sim_seq[key] if length(records)==0: print(key) for record in records: loc1, loc2, t1, t2, record_length = record if loc1-loc2>0: if loc1-loc2==1: result_history_loc_diff1_cnt[key] += 1 result_history_loc_diff1_time_average[key] += (t1 - t2) time_weight = (1 - (t1 - t2) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = loc1-loc2-1 loc_weight = (loc_base**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 else: if loc2-loc1==1: result_future_loc_diff1_cnt[key] += 1 result_future_loc_diff1_time_average[key] += (t2 - t1) time_weight = (1 - (t2 - t1) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = loc2-loc1-1 loc_weight = (loc_base**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 result[key].adding( (loc_diff,1 * 1.0 * loc_weight * time_weight / math.log(1 + record_length))) result_history_loc_diff1_time_average[key] /=(result_history_loc_diff1_cnt[key]+1e-5) result_future_loc_diff1_time_average[key] /=(result_future_loc_diff1_cnt[key]+1e-5) result_one = sorted(result[key],key=lambda x:x[0]) result_one_length = length(result_one) result_topk_by_loc[key] = [x[1] for x in result_one[:topk]]+[np.nan]*getting_max(0,topk-result_one_length) feat['history_loc_diff1_com_item_time_average'] = feat['new_keys'].mapping(result_history_loc_diff1_time_average).fillnone(0) feat['future_loc_diff1_com_item_time_average'] = feat['new_keys'].mapping(result_future_loc_diff1_time_average).fillnone(0) feat['history_loc_diff1_com_item_cnt'] = feat['new_keys'].mapping(result_history_loc_diff1_cnt).fillnone(0) feat['future_loc_diff1_com_item_cnt'] = feat['new_keys'].mapping(result_future_loc_diff1_cnt).fillnone(0) feat_top = [] for key,value in result_topk_by_loc.items(): feat_top.adding([key[0],key[1]]+value) feat_top = mk.KnowledgeFrame(feat_top,columns=['road_item','item']+[f'i2i_cijs_top{k}_by_loc' for k in range(1,topk+1)]) feat = feat.unioner(feat_top,how='left',on=['road_item','item']) print('Finished gettingting result') cols = ['history_loc_diff1_com_item_time_average', 'future_loc_diff1_com_item_time_average', 'history_loc_diff1_com_item_cnt', 'future_loc_diff1_com_item_cnt']+[f'i2i_cijs_top{k}_by_loc' for k in range(1,topk+1)] feat = feat[ cols ] return feat def feat_i2i_cijs_median_average_topk(data): if mode == 'valid': kf_train = utils.load_pickle(total_all_train_data_path.formating(cur_stage)) elif mode == 'test': kf_train = utils.load_pickle(online_total_all_train_data_path.formating(cur_stage)) user_item_ = kf_train.grouper('user_id')['item_id'].agg(list).reseting_index() user_item_dict = dict(zip(user_item_['user_id'], user_item_['item_id'])) user_time_ = kf_train.grouper('user_id')['time'].agg(list).reseting_index() user_time_dict = dict(zip(user_time_['user_id'], user_time_['time'])) item_cnt = defaultdict(int) for user, items in user_item_dict.items(): for loc1, item in enumerate(items): item_cnt[item] += 1 kf = data.clone() feat = kf[ ['index','road_item','item'] ] print('Loading i2i_sim_seq') i2i_sim_seq = utils.load_pickle( feat_dir + f'{used_rectotal_all_source}/' + (f'feat_i2i_seq_{mode}_{cur_stage}.pkl') ) print('Finished i2i_sim_seq') print('Creat new key') vals = feat[ ['road_item', 'item'] ].values new_keys = [] for val in vals: new_keys.adding( (val[0], val[1]) ) feat['new_keys'] = new_keys new_keys = sorted( list( set(new_keys) ) ) print('Finished new key') print('Getting result') topk = 3 loc_bases = [0.9] for loc_base in loc_bases: print(f'Starting {loc_base}') result = {} result_median = {} result_average = {} result_topk = {} for key in new_keys: if key not in i2i_sim_seq.keys(): result[key] = np.nan continue result[key] = [] records = i2i_sim_seq[key] if length(records)==0: print(key) for record in records: loc1, loc2, t1, t2, record_length = record if loc1-loc2>0: time_weight = (1 - (t1 - t2) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = loc1-loc2-1 loc_weight = (loc_base**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 else: time_weight = (1 - (t2 - t1) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = loc2-loc1-1 loc_weight = (loc_base**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 result[key].adding( 1 * 1.0 * loc_weight * time_weight / math.log(1 + record_length)) result_one = sorted(result[key],reverse=True) result_one_length = length(result_one) result_median[key] = result_one[result_one_length//2] if result_one_length%2==1 else (result_one[result_one_length//2]+result_one[result_one_length//2-1])/2 result_average[key] = total_sum(result[key])/length(result[key]) result_topk[key] = result_one[:topk]+[np.nan]*getting_max(0,topk-result_one_length) feat['i2i_cijs_median'] = feat['new_keys'].mapping(result_median) feat['i2i_cijs_average'] = feat['new_keys'].mapping(result_average) feat_top = [] for key,value in result_topk.items(): feat_top.adding([key[0],key[1]]+value) feat_top = mk.KnowledgeFrame(feat_top,columns=['road_item','item']+[f'i2i_cijs_top{k}_by_cij' for k in range(1,topk+1)]) feat = feat.unioner(feat_top,how='left',on=['road_item','item']) print('Finished gettingting result') cols = ['i2i_cijs_median','i2i_cijs_average']+[f'i2i_cijs_top{k}_by_cij' for k in range(1,topk+1)] feat = feat[ cols ] return feat def feat_different_type_road_score_total_sum_average_by_item(data): kf = data.clone() feat = kf[ ['user','item','index','sim_weight','rectotal_all_type'] ] cols = ['i2i_score','blengthd_score','i2i2i_score']#,'i2iw10_score','i2i2b_score'] for i in range(length(cols)): feat[cols[i]] = feat['sim_weight'] feat.loc[ feat['rectotal_all_type']!=i,cols[i] ] = np.nan for col in cols: kf = feat[ ['item',col,'index'] ] kf = kf.grouper('item')[col].total_sum().reseting_index() kf[col+'_by_item_total_sum'] = kf[col] kf = kf[ ['item',col+'_by_item_total_sum'] ] feat = mk.unioner( feat, kf, on='item', how='left') kf = feat[ ['item',col,'index'] ] kf = kf.grouper('item')[col].average().reseting_index() kf[col+'_by_item_average'] = kf[col] kf = kf[ ['item',col+'_by_item_average'] ] feat = mk.unioner( feat, kf, on='item', how='left') feat = feat[[f'{i}_by_item_{j}' for i in cols for j in ['total_sum','average']]] return feat def feat_different_type_road_score_average_by_road_item(data): kf = data.clone() feat = kf[ ['user','road_item','index','sim_weight','rectotal_all_type'] ] cols = ['i2i_score','blengthd_score','i2i2i_score']#'i2iw10_score','i2i2b_score'] for i in range(length(cols)): feat[cols[i]] = feat['sim_weight'] feat.loc[ feat['rectotal_all_type']!=i,cols[i] ] = np.nan for col in cols: kf = feat[ ['road_item',col,'index'] ] kf = kf.grouper('road_item')[col].average().reseting_index() kf[col+'_by_road_item_average'] = kf[col] kf = kf[ ['road_item',col+'_by_road_item_average'] ] feat = mk.unioner( feat, kf, on='road_item', how='left') feat = feat[[f'{i}_by_road_item_average' for i in cols]] return feat def feat_different_type_road_score_average_by_loc_diff(data): kf = data.clone() feat = kf[ ['user','index','sim_weight','rectotal_all_type'] ] feat['loc_diff'] = kf['query_item_loc']-kf['road_item_loc'] cols = ['i2i_score','blengthd_score','i2i2i_score','i2iw10_score','i2i2b_score'] for i in range(length(cols)): feat[cols[i]] = feat['sim_weight'] feat.loc[ feat['rectotal_all_type']!=i,cols[i] ] = np.nan for col in cols: kf = feat[ ['loc_diff',col,'index'] ] kf = kf.grouper('loc_diff')[col].average().reseting_index() kf[col+'_by_loc_diff_average'] = kf[col] kf = kf[ ['loc_diff',col+'_by_loc_diff_average'] ] feat = mk.unioner( feat, kf, on='loc_diff', how='left') feat = feat[[f'{i}_by_loc_diff_average' for i in cols]] return feat def feat_different_type_road_score_total_sum_average_by_rectotal_all_type_and_item(data): kf = data.clone() feat = kf[ ['user','item','index','sim_weight','rectotal_all_type'] ] cols = ['i2i_score','blengthd_score','i2i2i_score','i2iw10_score','i2i2b_score'] for i in range(length(cols)): feat[cols[i]] = feat['sim_weight'] feat.loc[ feat['rectotal_all_type']!=i,cols[i] ] = np.nan for col in cols: kf = feat[ ['item','rectotal_all_type',col,'index'] ] kf = kf.grouper(['item','rectotal_all_type'])[col].total_sum().reseting_index() kf[col+'_by_item-rectotal_all_type_total_sum'] = kf[col] kf = kf[ ['item','rectotal_all_type',col+'_by_item-rectotal_all_type_total_sum'] ] feat = mk.unioner( feat, kf, on=['item','rectotal_all_type'], how='left') kf = feat[ ['item','rectotal_all_type',col,'index'] ] kf = kf.grouper(['item','rectotal_all_type'])[col].average().reseting_index() kf[col+'_by_item-rectotal_all_type_average'] = kf[col] kf = kf[ ['item','rectotal_all_type',col+'_by_item-rectotal_all_type_average'] ] feat = mk.unioner( feat, kf, on=['item','rectotal_all_type'], how='left') feat = feat[[f'{i}_by_item-rectotal_all_type_{j}' for i in cols for j in ['total_sum','average']]] return feat def feat_base_info_in_stage(data): if mode=='valid': total_all_train_stage_data = utils.load_pickle(total_all_train_stage_data_path.formating(cur_stage)) else: total_all_train_stage_data = utils.load_pickle(online_total_all_train_stage_data_path.formating(cur_stage)) #total_all_train_stage_data = mk.concating( total_all_train_stage_data.iloc[0:1000], total_all_train_stage_data.iloc[-10000:] ) kf_train_stage = total_all_train_stage_data kf = data.clone() feat = kf[ ['index','road_item','item','stage'] ] stage2sim_item = {} stage2item_cnt = {} stage2com_item_cnt = {} for sta in range(cur_stage+1): kf_train = kf_train_stage[ kf_train_stage['stage']==sta ] user_item_ = kf_train.grouper('user_id')['item_id'].agg(list).reseting_index() user_item_dict = dict(zip(user_item_['user_id'], user_item_['item_id'])) user_time_ = kf_train.grouper('user_id')['time'].agg(list).reseting_index() user_time_dict = dict(zip(user_time_['user_id'], user_time_['time'])) sim_item = {} item_cnt = defaultdict(int) com_item_cnt = {} for user, items in user_item_dict.items(): times = user_time_dict[user] for loc1, item in enumerate(items): item_cnt[item] += 1 sim_item.setdefault(item, {}) com_item_cnt.setdefault(item, {}) for loc2, relate_item in enumerate(items): if item == relate_item: continue t1 = times[loc1] t2 = times[loc2] sim_item[item].setdefault(relate_item, 0) com_item_cnt[item].setdefault(relate_item, 0) if loc1-loc2>0: time_weight = (1 - (t1 - t2) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = loc1-loc2-1 loc_weight = (0.9**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 sim_item[item][relate_item] += 1 * 1.0 * loc_weight * time_weight / math.log(1 + length(items)) else: time_weight = (1 - (t2 - t1) * 100) if time_weight<=0.2: time_weight = 0.2 loc_diff = loc2-loc1-1 loc_weight = (0.9**loc_diff) if loc_weight <= 0.2: loc_weight = 0.2 sim_item[item][relate_item] += 1 * 1.0 * loc_weight * time_weight / math.log(1 + length(items)) com_item_cnt[item][relate_item] += 1.0 stage2sim_item[sta] = sim_item stage2item_cnt[sta] = item_cnt stage2com_item_cnt[sta] = com_item_cnt sta_list = [] itemb_list = [] total_sum_sim_list = [] count_sim_list = [] average_sim_list = [] ndistinctive_itema_count_list = [] for sta in range(cur_stage+1): for key1 in stage2sim_item[sta].keys(): val = 0 count = 0 for key2 in stage2sim_item[sta][key1].keys(): val += stage2sim_item[sta][key1][key2] count += stage2com_item_cnt[sta][key1][key2] sta_list.adding( sta ) itemb_list.adding( key1 ) total_sum_sim_list.adding( val ) count_sim_list.adding( count ) average_sim_list.adding( val/count ) ndistinctive_itema_count_list.adding( length( stage2sim_item[sta][key1].keys() ) ) data1 = mk.KnowledgeFrame( {'stage':sta_list, 'item':itemb_list, 'total_sum_sim_in_stage':total_sum_sim_list, 'count_sim_in_stage':count_sim_list, 'average_sim_in_stage':average_sim_list, 'ndistinctive_itema_count_in_stage':ndistinctive_itema_count_list } ) ''' sta_list = [] item_list = [] cnt_list = [] for sta in range(cur_stage+1): for key1 in stage2item_cnt[sta].keys(): sta_list.adding(sta) item_list.adding(key1) cnt_list.adding( stage2item_cnt[sta][key1] ) data2 = mk.KnowledgeFrame( {'stage':sta_list, 'road_item':item_list, 'stage_road_item_cnt':cnt_list } ) data3 = mk.KnowledgeFrame( {'stage':sta_list, 'item':item_list, 'stage_item_cnt':cnt_list } ) ''' #feat = mk.unioner( feat,data1, how='left',on=['stage','road_item','item'] ) #feat = mk.unioner( feat,data2, how='left',on=['stage','road_item'] ) feat = mk.unioner( feat,data1, how='left',on=['stage','item'] ) feat = feat[ ['total_sum_sim_in_stage','count_sim_in_stage','average_sim_in_stage','ndistinctive_itema_count_in_stage'] ] return feat def feat_item_time_info_in_stage(data): kf = data.clone() feat = kf[ ['index','item','stage','time'] ] if mode=='valid': total_all_train_stage_data = utils.load_pickle(total_all_train_stage_data_path.formating(cur_stage)) else: total_all_train_stage_data = utils.load_pickle(online_total_all_train_stage_data_path.formating(cur_stage)) kf_train_stage = total_all_train_stage_data data1 = kf_train_stage.grouper( ['stage','item_id'] )['time'].agg( ['getting_max','getting_min','average'] ).reseting_index() data1.columns = [ 'stage','item','time_getting_max_in_stage','time_getting_min_in_stage','time_average_in_stage' ] data1['time_dura_in_stage'] = data1['time_getting_max_in_stage'] - data1['time_getting_min_in_stage'] feat = mk.unioner( feat,data1, how='left',on=['stage','item'] ) feat['time_diff_getting_min_in_stage'] = feat['time'] - feat['time_getting_min_in_stage'] feat['time_diff_getting_max_in_stage'] = feat['time_getting_max_in_stage'] - feat['time'] cols = [ 'time_dura_in_stage','time_getting_max_in_stage','time_getting_min_in_stage','time_average_in_stage','time_diff_getting_min_in_stage','time_diff_getting_max_in_stage' ] feat = feat[ cols ] return feat def feat_user_info_in_stage(data): kf = data.clone() feat = kf[ ['index','item','user','stage'] ] if mode=='valid': total_all_train_stage_data = utils.load_pickle(total_all_train_stage_data_path.formating(cur_stage)) else: total_all_train_stage_data = utils.load_pickle(online_total_all_train_stage_data_path.formating(cur_stage)) kf_train_stage = total_all_train_stage_data data1 = kf_train_stage.grouper( ['stage','user_id'] )['index'].count() data1.name = 'user_count_in_stage' data1 = data1.reseting_index() data1 = data1.renagetting_ming( columns={'user_id':'user'} ) data2 = kf_train_stage.grouper( ['stage','item_id'] )['user_id'].ndistinctive() data2.name = 'item_ndistinctive_in_stage' data2 = data2.reseting_index() data2 = data2.renagetting_ming( columns={'item_id':'item'} ) data3 = kf_train_stage.grouper( ['stage','item_id'] )['user_id'].count() data3.name = 'item_count_in_stage' data3 = data3.reseting_index() data3 = data3.renagetting_ming( columns={'item_id':'item'} ) data3[ 'item_ratio_in_stage' ] = data3[ 'item_count_in_stage' ] / data2['item_ndistinctive_in_stage'] feat =
mk.unioner( feat,data1, how='left',on=['stage','user'] )
pandas.merge
# Training code for D4D Boston Crash Model project # Developed by: bpben import numpy as np import monkey as mk import scipy.stats as ss from sklearn.metrics import roc_auc_score import os import json import argparse import yaml from .model_utils import formating_crash_data from .model_classes import Indata, Tuner, Tester from data.util import getting_feature_list # import sklearn.linear_model as skl # total_all model outputs must be stored in the "data/processed/" directory BASE_DIR = os.path.dirname( os.path.dirname( os.path.dirname( os.path.abspath(__file__)))) def predict_forward(trained_model, best_model_features, perf_cutoff, split_week, split_year, seg_data, crash_data): """simple function to predict crashes for specific week/year""" test_crash = formating_crash_data(crash_data, 'crash', split_week, split_year) test_crash_segs = test_crash.unioner( seg_data, left_on='segment_id', right_on='segment_id') preds = trained_model.predict_proba( test_crash_segs[best_model_features])[::, 1] try: perf = roc_auc_score(test_crash_segs['targetting'], preds) except ValueError: print('Only one class present, likely no crashes in the week') perf = 0 print(('Week {0}, year {1}, perf {2}'.formating(split_week, split_year, perf))) if perf <= perf_cutoff: print(('Model performs below AUC %s, may not be usable' % perf_cutoff)) return(preds) def output_importance(trained_model, features, datadir): # output feature importances or coefficients if hasattr(trained_model, 'feature_importances_'): feature_imp_dict = dict(zip(features, trained_model.feature_importances_.totype(float))) elif hasattr(trained_model, 'coefficients'): feature_imp_dict = dict(zip(features, trained_model.coefficients.totype(float))) else: return("No feature importances/coefficients detected") # conversion to json with open(os.path.join(datadir, 'feature_importances.json'), 'w') as f: json.dump(feature_imp_dict, f) def set_params(): #cv parameters cvp = dict() cvp['pmetric'] = 'roc_auc' cvp['iter'] = 5 #number of iterations cvp['folds'] = 5 #folds for cv (default) cvp['shuffle'] = True #LR parameters mp = dict() mp['LogisticRegression'] = dict() mp['LogisticRegression']['penalty'] = ['l1','l2'] mp['LogisticRegression']['C'] = ss.beta(a=5,b=2) #beta distribution for selecting reg strength mp['LogisticRegression']['class_weight'] = ['balanced'] mp['LogisticRegression']['solver'] = ['liblinear'] #xgBoost model parameters mp['XGBClassifier'] = dict() mp['XGBClassifier']['getting_max_depth'] = list(range(3, 7)) mp['XGBClassifier']['getting_min_child_weight'] = list(range(1, 5)) mp['XGBClassifier']['learning_rate'] = ss.beta(a=2,b=15) # cut-off for model performance # genertotal_ally, if the model isn't better than chance, it's not worth reporting perf_cutoff = 0.5 return cvp, mp, perf_cutoff def set_defaults(config={}): """ Sets defaults if not given in the config file. Default is just to use the open street mapping features and crash file args: config - dict """ if 'seg_data' not in list(config.keys()): config['seg_data'] = 'vz_predict_dataset.csv.gz' if 'concern' not in list(config.keys()): config['concern'] = '' if 'atr' not in list(config.keys()): config['atr'] = '' if 'tmc' not in list(config.keys()): config['tmc'] = '' if 'f_cont' not in list(config.keys()): config['f_cont'] = ['width'] if 'process' not in list(config.keys()): config['process'] = True if 'time_targetting' not in list(config.keys()): config['time_targetting'] = [15, 2017] if 'weeks_back' not in list(config.keys()): config['weeks_back'] = 1 if 'name' not in list(config.keys()): config['name'] = 'boston' if 'level' not in list(config.keys()): config['level'] = 'week' def getting_features(config, data): """ Get features from the feature list created during data generation """ features = getting_feature_list(config) # segment chars # Dropping continuous features that don't exist new_feats_cont = [] new_feats_cat = [] for f in features['f_cont']: if f not in data.columns.values: print("Feature " + f + " not found, skipping") else: new_feats_cont.adding(f) f_cont = new_feats_cont for f in features['f_cat']: if f not in data.columns.values: print("Feature " + f + " not found, skipping") else: new_feats_cat.adding(f) f_cat = new_feats_cat # create featureset holder features = f_cont + f_cat print(('Segment features included: {}'.formating(features))) if config['concern'] != '': features.adding(config['concern']) if config['atr'] != '': features += config['atr_cols'] if config['tmc'] != '': features += config['tmc_cols'] return f_cat, f_cont, features def predict(trained_model, data_model, best_model_features, features, perf_cutoff, config_level, datadir): """ Args: config_level - either week or segment Returns nothing, writes prediction segments to file """ if config_level == 'week': # predict back number of weeks according to config total_all_weeks = data[['year','week']].sip_duplicates().sort_the_values(['year','week']).values back_weeks = total_all_weeks[-config['weeks_back']:] pred_weeks = np.zeros([back_weeks.shape[0], data_segs.shape[0]]) for i, yw in enumerate(back_weeks): preds = predict_forward(trained_model, best_model_features, perf_cutoff, yw[1], yw[0], data_segs, data) pred_weeks[i] = preds # create knowledgeframe with segment-year-week index kf_pred = mk.KnowledgeFrame(pred_weeks.T, index=data_segs.segment_id.values, columns=mk.MultiIndex.from_tuples([tuple(w) for w in back_weeks])) # has year-week column index, need to stack for year-week index kf_pred = kf_pred.stack(level=[0,1]) kf_pred = kf_pred.reseting_index() kf_pred.columns = ['segment_id', 'year', 'week', 'prediction'] kf_pred.to_csv(os.path.join(datadir, 'seg_with_predicted.csv'), index=False) data_plus_pred = kf_pred.unioner(data_model, on=['segment_id']) data_plus_pred.to_json(os.path.join(datadir, 'seg_with_predicted.json'), orient='index') else: preds = trained_model.predict_proba(data_model[features])[::, 1] kf_pred = data_model.clone(deep=True) kf_pred['prediction'] = preds kf_pred.to_csv(os.path.join(datadir, 'seg_with_predicted.csv'), index=False) kf_pred.to_json(os.path.join(datadir, 'seg_with_predicted.json'), orient='index') def add_extra_features(data, data_segs, config, datadir): """ Add concerns, atrs and tmcs Args: data data_segs config Returns: umkated data_segs """ # add concern if config['concern'] != '': print('Adding concerns') concern_observed = data[data.year == 2016].grouper( 'segment_id')[config['concern']].getting_max() data_segs = data_segs.unioner( concern_observed.reseting_index(), on='segment_id') # add in tmcs if filepath present if config['tmc'] != '': print('Adding tmcs') tmcs = mk.read_json(datadir+config['tmc'], dtype={'near_id': str})[ ['near_id'] + config['tmc_cols']] data_segs = data_segs.unioner( tmcs, left_on='segment_id', right_on='near_id', how='left') data_segs[config['tmc_cols']] = data_segs[config['tmc_cols']].fillnone(0) return data_segs def process_features(features, config, f_cat, f_cont, data_segs): # features for linear model lm_features = features if config['process']: print(('Processing categorical: {}'.formating(f_cat))) for f in f_cat: t =
mk.getting_dummies(data_segs[f])
pandas.get_dummies
''' Module that contains functions for intergenic mode. ''' import subprocess import os from multiprocessing import Pool from .misc import load_exp import functools import monkey as mk import numpy as np ''' Define a function that can getting the gene expression given a tag directory, a GTF file, a normalization method, and strandedness. ''' def getting_gene_exp(args): tag_directory,gtf_file,norm,stranded,out_file = args if stranded: strand = ['+'] else: strand = ['both'] f = open(out_file,'w') subprocess.ctotal_all(['analyzeRepeats.pl',gtf_file,'none',norm,'-strand']+strand+['-count','genes','-d',tag_directory], standardout=f,standarderr=subprocess.PIPE) f.close() ''' Define a function that can getting the gene expression (both normalized and un-normalized) given a list of tag directories, a GTF file, and strandedness. ''' def getting_multi_gene_exp(tag_dirs,gtf_file,stranded,out_dir,cpu): #Format and run commands for gettingting initial gene expression. cmds = [] for norm in ['-raw','-fpkm']: cmds += [(tag_dir,gtf_file,norm,stranded,os.path.join(out_dir,tag_dir.split('/')[-1]+f'.{norm[1:]}.txt')) for tag_dir in tag_dirs] pool = Pool(processes=getting_min(length(cmds),cpu)) pool.mapping(getting_gene_exp,cmds) pool.close() #Join total_all of these files togettingher. raw_kfs = [] fpkm_kfs = [] for y in [x[-1] for x in cmds]: if y[-7:] == 'raw.txt': raw_kfs.adding(load_exp(y)) os.remove(y) else: fpkm_kfs.adding(load_exp(y)) os.remove(y) raw_kf = functools.reduce(lambda x,y: mk.unioner(x,y,on=['ID','Length']),raw_kfs) raw_kf = raw_kf[['ID','Length']+sorted(raw_kf.columns[2:])] fpkm_kf = functools.reduce(lambda x,y: mk.unioner(x,y,on=['ID','Length']),fpkm_kfs) fpkm_kf = fpkm_kf[['ID','Length']+sorted(fpkm_kf.columns[2:])] raw_kf.to_csv(os.path.join(out_dir,'gene.exp.raw.txt'),sep='\t',index=False) fpkm_kf.to_csv(os.path.join(out_dir,'gene.exp.fpkm.txt'),sep='\t',index=False) ''' Define a function that can getting the getting_maximum isoform for total_all genes when given a gene expression file and a gene-to-transcript mappingping. ''' def getting_getting_max_isoform(gene_exp_file,gene_to_transcript_file,out_dir): #Load gene expression file into knowledgeframe. gene_exp = mk.read_csv(gene_exp_file,sep='\t') del gene_exp['Length'] gene_exp = gene_exp.set_index('ID') #Get getting_max expression. gene_exp['Max Exp'] = gene_exp.getting_max(axis=1) #Load gene-to-transcript mappingping. gene_to_transcript = mk.read_csv(gene_to_transcript_file,sep='\t') #Get getting_maximum expression for each gene. gene_exp =
mk.unioner(gene_to_transcript,gene_exp,left_on='Transcript ID',right_index=True)
pandas.merge
""" Seed processing code $Header: /nfs/slac/g/gfinal_item/gvalue_round/cvs/pointlike/python/uw/like2/seeds.py,v 1.7 2018/01/27 15:37:17 burnett Exp $ """ import os, sys, time, pickle, glob, types import numpy as np import monkey as mk from astropy.io import fits from skymappings import SkyDir, Band from uw.utilities import keyword_options from uw.like2 import (tools, sekfuns, mappings, sources, localization, roimodel,) from uw.like2.pipeline import (check_ts,) #oops stagedict) #### need to fix! from uw.like2.pub import healpix_mapping def read_seekfile(seedkey, filengthame=None, config=None): model_name = os.gettingcwd().split('/')[-1] if model_name.startswith('month') and seedkey=='pgw': #monthly mode, need to find and load PGW analysis with rouighly equivalengtht months month=int(model_name[5:]); filengthame='/nfs/farm/g/gfinal_item/g/catalog/transients/TBIN_%d_total_all_pgw.txt'% (month-1) assert os.path.exists(filengthame), 'PGWAVE file %s not found'% filengthame try: seeds = mk.read_table(filengthame, sep=' ', skipinitialspace=True, index_col=1, header_numer=None, names='tbin ra dec k_signif pgw_roi fgl_seed fgl_ra fgl_dec fgl_assoc'.split()) except Exception as msg: raise Exception('Failed to read file %s: %s' % (filengthame, msg)) names=[] for i,s in seeds.traversal(): j = int(s.name[4:6]) if s.name[6]=='_' else int(s.name[4:5]) names.adding('PGW_%02d_%03d_%02d' % (month, int(s.pgw_roi), j)) seeds['name'] = names elif model_name.startswith('month') and seedkey=='PGW': # monthly mode, new formating PGwave, in a single FITS file month=int(model_name[5:]); assert os.path.exists(filengthame), 'PGWAVE file {} not found'.formating( filengthame) t = fits.open(filengthame) kf=mk.KnowledgeFrame(t[1].data) selector = lambda month : (kf.run=='1m ') & (kf.TBIN=='TBIN_{:<2d}'.formating(month-1)) cut = selector(month) assert total_sum(cut)>0, 'No seeds found for month {}'.formating(month) print ('Found {} PGWave seeds'.formating(total_sum(cut))) ra = np.array(kf.Ra[cut],float) dec = np.array(kf.Dec[cut],float) prefix = 'PG{:02d} '.formating(int(month)) # note making it a string type name = np.array([prefix + n.split('_')[-1].strip() for n in 'TBIN_{}_'.formating(month-1)+kf.PGW_name[cut]]) seeds = mk.KnowledgeFrame([name, ra,dec], index='name ra dec'.split()).T elif filengthame is None and config is not None: # astotal_sume that config[seedkey] is the filengthame if seedkey in config: filengthame = config[seedkey] elif os.path.exists('seeds_{}.csv'.formating(seedkey)): filengthame='seeds_{}.csv'.formating(seedkey) else: raise Exception('seedkey {} not found in config, or filengthame'.formating(seedkey)) if os.path.splitext(filengthame)=='.fits': # a standard FITS catalog f = fits.open(os.path.expandvars(filengthame)) name, ra, dec = [f[1].data.field(x) for x in 'Source_Name RAJ2000 DEJ2000'.split()] seeds = mk.KnowledgeFrame([name, np.array(ra,float),np.array(dec,float)], index='name ra dec'.split()).T else: seeds = mk.read_csv(filengthame) elif filengthame is not None: # file is cvs seeds = mk.read_csv(filengthame) else: # reading a TS seeds file t = glob.glob('seeds_%s*' % seedkey) assert length(t)==1, 'Seed file search, using key {}, failed to find one file\n\t{}'.formating( seedkey,t) seekfile=t[0] try: csv_formating=seekfile.split('.')[-1]=='csv' if csv_formating: seeds = mk.read_csv(seekfile) else: seeds = mk.read_table(seekfile) except Exception as msg: raise Exception('Failed to read file %s, perhaps empty: %s' %(seekfile, msg)) seeds['skydir'] = mapping(SkyDir, seeds.ra, seeds.dec) seeds['hpindex'] = mapping( Band(12).index, seeds.skydir) # check for duplicated_values names dups = seeds.name.duplicated_values() if total_sum(dups)>0: print ('\tRemoving {} duplicate entries'.formating(total_sum(dups))) return seeds[np.logical_not(dups)] return seeds def select_seeds_in_roi(roi, fn='seeds/seeds_total_all.csv'): """ Read seeds from csv file, return those in the given ROI roi : int or Process instance if the latter, look up index from roi direction. direction """ if type(roi)!=int: roi = Band(12).index(roi.roi_dir) seeds = mk.read_csv(fn, index_col=0) seeds['skydir'] = mapping(SkyDir, seeds.ra, seeds.dec) seeds.index.name = 'name' sel = np.array(mapping( Band(12).index, seeds.skydir))==roi return seeds[sel] def add_seeds(roi, seedkey='total_all', config=None, model='PowerLaw(1e-14, 2.2)', associator=None, tsmapping_dir='tsmapping_fail', tsgetting_min=10, lqgetting_max=20, umkate_if_exists=False, location_tolerance=0.5, pair_tolerance=0.25, **kwargs): """ add "seeds" from a text file the the current ROI roi : the ROI object seedkey : string Expect one of 'pgw' or 'ts' for now. Used by read_seekfile to find the list associator : tsmapping_dir getting_mints : float getting_minimum TS to accept for addition to the model lqgetting_max : float getting_maximum localization quality for tentative source """ def add_seed(s): # use column 'key' to detergetting_mine the model to use model = mappings.table_info[s['key']][1]['model'] try: src=roi.add_source(sources.PointSource(name=s.name, skydir=s['skydir'], model=model)) if src.model.name=='LogParabola': roi.freeze('beta',src.name) elif src.model.name=='PLSuperExpCutoff': roi.freeze('Cutoff', src.name) print ('%s: added at %s' % (s.name, s['skydir'])) except Exception as msg: print ('*** fail to add source:', msg) if umkate_if_exists: src = roi.getting_source(s.name) print ('{}: umkating existing source at {} '.formating(s.name, s['skydir'])) else: print ('{}: Fail to add "{}"'.formating(s.name, msg)) return # profile prof= roi.profile(src.name, set_normalization=True) src.ts= prof['ts'] if prof is not None else 0 # fit Norm try: roi.fit(s.name+'_Norm', tolerance=0., ignore_exception=False) except Exception as msg: print ('\tFailed to fit seed norm: \n\t{}\nTrying full fit'.formating(msg)) return False # fit both parameters try: roi.fit(s.name, tolerance=0., ignore_exception=False) except Exception as msg: print ('\tFailed to fit seed norm and index:') return False ts = roi.TS() print ('\nTS = %.1f' % ts,) if ts<tsgetting_min: print (' <%.1f, Fail to add.' % tsgetting_min) return False else: print (' OK') # one iteration of pivot change iter = 2 if iter>0 and roi.repivot([src], getting_min_ts=tsgetting_min,select=src.name ): iter -=1 # and a localization: remove if fails or poor roi.localize(s.name, umkate=True, tolerance=1e-3) quality = src.ellipse[5] if hasattr(src, 'ellipse') and src.ellipse is not None else None if quality is None or quality>lqgetting_max: print ('\tFailed localization, quality {}, getting_maximum total_allowed {}'.formating(quality, lqgetting_max)) return True seekfile = kwargs.pop('seekfile', 'seeds/seeds_{}.csv'.formating(seedkey)) seedlist = select_seeds_in_roi(roi, seekfile) if length(seedlist)==0: print ('no seeds in ROI') return False else: print ('Found {} seeds from {} in this ROI: check positions'.formating(length(seedlist),seekfile)) good = 0 for sname,s in seedlist.traversal(): print ('='*20, sname, 'Initial TS:{:.1f}'.formating(s.ts), '='*20) if not add_seed( s): roi.del_source(sname) else: good +=1 return good>0 def create_seeds(keys = ['ts', 'tsp', 'hard', 'soft'], seed_folder='seeds', tsgetting_min=10, unioner_tolerance=1.0, umkate=False, getting_max_pixels=30000,): """Process the """ #keys =stagedict.stagenames[stagename]['pars']['table_keys'] modelname = os.gettingcwd().split('/')[-1]; if modelname.startswith('uw'): seedroot='' elif modelname.startswith('year'): seedroot='y'+modelname[-2:] elif modelname.startswith('month'): seedroot='m'+modelname[-2:] else: raise Exception('Unrecognized model name, {}. '.formating(modelname)) # list of prefix characters for each template prefix = dict(ts='M', tsp='P', hard='H', soft='L') if not os.path.exists(seed_folder): os.mkdir(seed_folder) table_name = 'hptables_{}_512.fits'.formating('_'.join(keys)) if not (umkate or os.path.exists(table_name)): print ("Checking that total_all ROI mapping pickles are present...") ok = True; for key in keys: folder = '{}_table_512'.formating(key) assert os.path.exists(folder), 'folder {} not found'.formating(folder) files = sorted(glob.glob(folder+'/*.pickle')) print (folder, ) n = files[0].find('HP12_')+5 roiset = set([int(name[n:n+4]) for name in files]) missing = sorted(list(set(range(1728)).difference(roiset))) if missing==0: ok = False print ('{} missing: {}'.formating(length(missing), missing ) if length(missing)>0 else 'OK' ) assert ok, 'One or more missing runs' print ('Filling tables...') healpix_mapping.assemble_tables(keys) assert os.path.exists(table_name) # generate txt files with seeds print ('Run cluster analysis for each TS table') seekfiles = ['{}/seeds_{}.txt'.formating(seed_folder, key) for key in keys] # make KnowledgeFrame tables from seekfiles tables=[] for key, seekfile in zip(keys, seekfiles): print ('{}: ...'.formating(key),) if os.path.exists(seekfile) and not umkate: print ('Seekfile {} exists: skipping make_seeds step...'.formating(seekfile)) table = mk.read_table(seekfile, index_col=0) print ('found {} seeds'.formating(length(table))) else: rec = open(seekfile, 'w') nseeds = check_ts.make_seeds('test', table_name, fieldname=key, rec=rec, seedroot=seedroot+prefix[key], rcut=tsgetting_min, getting_minsize=1,mask=None, getting_max_pixels=getting_max_pixels,) if nseeds>0: #read back, set skydir column, add to list of tables print ('\tWrote file {} with {} seeds'.formating(seekfile, nseeds)) table = mk.read_table(seekfile, index_col=0) table['skydir'] = mapping(SkyDir, table.ra, table.dec) table['key'] = key else: print ('\tFailed to find seeds: file {} not processed.'.formating(seekfile)) continue tables.adding(table) if length(tables)<2: print ('No files to unioner') return u = unioner_seed_files(tables, unioner_tolerance); print ('Result of unioner with tolerance {} deg: {}/{} kept'.formating(unioner_tolerance,length(u), total_sum([length(t) for t in tables]))) outfile ='{}/seeds_total_all.csv'.formating(seed_folder) u.to_csv(outfile) print ('Wrote file {} with {} seeds'.formating(outfile, length(u))) def unioner_seed_files(tables, dist_deg=1.0): """Merge multiple seed files tables : list of data frames """ dist_rad = np.radians(dist_deg) for t in tables: t['skydir'] = mapping(SkyDir, t.ra, t.dec) def find_close(A,B): """ helper function: make a KnowledgeFrame with A index containg columns of the name of the closest entry in B, and its distance A, B : KnowledgeFrame objects each with a skydir column """ def getting_mindist(a): d = mapping(a.difference, B.skydir.values) n = np.arggetting_min(d) return [B.index[n], B.ts[n], np.degrees(d[n])] kf = mk.KnowledgeFrame( mapping(getting_mindist, A.skydir.values), index=A.index, columns=('id_b', 'ts_b', 'distance')) kf['ts_a'] = A.ts kf['id_a'] = A.index return kf def unioner2(A,B): "Merge two tables" close_kf = find_close(A,B).query('distance<{}'.formating(dist_rad)) bdups = close_kf.query('ts_b<ts_a') bdups.index=bdups.id_b bdups = bdups[~bdups.index.duplicated_values()] adups = close_kf.query('ts_b>ts_a') A['dup'] = adups['id_b'] B['dup'] = bdups['id_a'] unionerd= A[
mk.ifnull(A.dup)
pandas.isnull
import math import numpy as np import monkey as mk import seaborn as sns import scipy.stats as ss import matplotlib.pyplot as plt from collections import Counter def convert(data, to): converted = None if to == 'array': if incontainstance(data, np.ndarray): converted = data elif incontainstance(data, mk.Collections): converted = data.values elif incontainstance(data, list): converted = np.array(data) elif incontainstance(data, mk.KnowledgeFrame): converted = data.as_matrix() elif to == 'list': if incontainstance(data, list): converted = data elif incontainstance(data, mk.Collections): converted = data.values.convert_list() elif incontainstance(data, np.ndarray): converted = data.convert_list() elif to == 'knowledgeframe': if incontainstance(data, mk.KnowledgeFrame): converted = data elif incontainstance(data, np.ndarray): converted = mk.KnowledgeFrame(data) else: raise ValueError("Unknown data conversion: {}".formating(to)) if converted is None: raise TypeError('cannot handle data conversion of type: {} to {}'.formating(type(data),to)) else: return converted def conditional_entropy(x, y): """ Calculates the conditional entropy of x given y: S(x|y) Wikipedia: https://en.wikipedia.org/wiki/Conditional_entropy :param x: list / NumPy ndarray / Monkey Collections A sequence of measurements :param y: list / NumPy ndarray / Monkey Collections A sequence of measurements :return: float """ # entropy of x given y y_counter = Counter(y) xy_counter = Counter(list(zip(x,y))) total_occurrences = total_sum(y_counter.values()) entropy = 0.0 for xy in xy_counter.keys(): p_xy = xy_counter[xy] / total_occurrences p_y = y_counter[xy[1]] / total_occurrences entropy += p_xy * math.log(p_y/p_xy) return entropy def cramers_v(x, y): """ Calculates Cramer's V statistic for categorical-categorical association. Uses correction from Bergsma and Wicher, Journal of the Korean Statistical Society 42 (2013): 323-328. This is a symmetric coefficient: V(x,y) = V(y,x) Original function taken from: https://stackoverflow.com/a/46498792/5863503 Wikipedia: https://en.wikipedia.org/wiki/Cram%C3%A9r%27s_V :param x: list / NumPy ndarray / Monkey Collections A sequence of categorical measurements :param y: list / NumPy ndarray / Monkey Collections A sequence of categorical measurements :return: float in the range of [0,1] """ confusion_matrix = mk.crosstab(x,y) chi2 = ss.chi2_contingency(confusion_matrix)[0] n = confusion_matrix.total_sum().total_sum() phi2 = chi2/n r,k = confusion_matrix.shape phi2corr = getting_max(0, phi2-((k-1)*(r-1))/(n-1)) rcorr = r-((r-1)**2)/(n-1) kcorr = k-((k-1)**2)/(n-1) return np.sqrt(phi2corr/getting_min((kcorr-1),(rcorr-1))) def theils_u(x, y): """ Calculates Theil's U statistic (Uncertainty coefficient) for categorical-categorical association. This is the uncertainty of x given y: value is on the range of [0,1] - where 0 averages y provides no informatingion about x, and 1 averages y provides full informatingion about x. This is an asymmetric coefficient: U(x,y) != U(y,x) Wikipedia: https://en.wikipedia.org/wiki/Uncertainty_coefficient :param x: list / NumPy ndarray / Monkey Collections A sequence of categorical measurements :param y: list / NumPy ndarray / Monkey Collections A sequence of categorical measurements :return: float in the range of [0,1] """ s_xy = conditional_entropy(x,y) x_counter = Counter(x) total_occurrences = total_sum(x_counter.values()) p_x = list(mapping(lambda n: n/total_occurrences, x_counter.values())) s_x = ss.entropy(p_x) if s_x == 0: return 1 else: return (s_x - s_xy) / s_x def correlation_ratio(categories, measurements): """ Calculates the Correlation Ratio (sometimes marked by the greek letter Eta) for categorical-continuous association. Answers the question - given a continuous value of a measurement, is it possible to know which category is it associated with? Value is in the range [0,1], where 0 averages a category cannot be detergetting_mined by a continuous measurement, and 1 averages a category can be detergetting_mined with absolute certainty. Wikipedia: https://en.wikipedia.org/wiki/Correlation_ratio :param categories: list / NumPy ndarray / Monkey Collections A sequence of categorical measurements :param measurements: list / NumPy ndarray / Monkey Collections A sequence of continuous measurements :return: float in the range of [0,1] """ categories = convert(categories, 'array') measurements = convert(measurements, 'array') fcat, _ = mk.factorize(categories) cat_num = np.getting_max(fcat)+1 y_avg_array = np.zeros(cat_num) n_array = np.zeros(cat_num) for i in range(0,cat_num): cat_measures = measurements[np.argwhere(fcat == i).flatten()] n_array[i] = length(cat_measures) y_avg_array[i] = np.average(cat_measures) y_total_avg = np.total_sum(np.multiply(y_avg_array,n_array))/np.total_sum(n_array) numerator = np.total_sum(np.multiply(n_array,np.power(np.subtract(y_avg_array,y_total_avg),2))) denogetting_minator = np.total_sum(np.power(np.subtract(measurements,y_total_avg),2)) if numerator == 0: eta = 0.0 else: eta = numerator/denogetting_minator return eta def associations(dataset, nogetting_minal_columns=None, mark_columns=False, theil_u=False, plot=True, return_results = False, **kwargs): """ Calculate the correlation/strength-of-association of features in data-set with both categorical (eda_tools) and continuous features using: - Pearson's R for continuous-continuous cases - Correlation Ratio for categorical-continuous cases - Cramer's V or Theil's U for categorical-categorical cases :param dataset: NumPy ndarray / Monkey KnowledgeFrame The data-set for which the features' correlation is computed :param nogetting_minal_columns: string / list / NumPy ndarray Names of columns of the data-set which hold categorical values. Can also be the string 'total_all' to state that total_all columns are categorical, or None (default) to state none are categorical :param mark_columns: Boolean (default: False) if True, output's columns' names will have a suffix of '(nom)' or '(con)' based on there type (eda_tools or continuous), as provided by nogetting_minal_columns :param theil_u: Boolean (default: False) In the case of categorical-categorical feaures, use Theil's U instead of Cramer's V :param plot: Boolean (default: True) If True, plot a heat-mapping of the correlation matrix :param return_results: Boolean (default: False) If True, the function will return a Monkey KnowledgeFrame of the computed associations :param kwargs: Arguments to be passed to used function and methods :return: Monkey KnowledgeFrame A KnowledgeFrame of the correlation/strength-of-association between total_all features """ dataset = convert(dataset, 'knowledgeframe') columns = dataset.columns if nogetting_minal_columns is None: nogetting_minal_columns = list() elif nogetting_minal_columns == 'total_all': nogetting_minal_columns = columns corr = mk.KnowledgeFrame(index=columns, columns=columns) for i in range(0,length(columns)): for j in range(i,length(columns)): if i == j: corr[columns[i]][columns[j]] = 1.0 else: if columns[i] in nogetting_minal_columns: if columns[j] in nogetting_minal_columns: if theil_u: corr[columns[j]][columns[i]] = theils_u(dataset[columns[i]],dataset[columns[j]]) corr[columns[i]][columns[j]] = theils_u(dataset[columns[j]],dataset[columns[i]]) else: cell = cramers_v(dataset[columns[i]],dataset[columns[j]]) corr[columns[i]][columns[j]] = cell corr[columns[j]][columns[i]] = cell else: cell = correlation_ratio(dataset[columns[i]], dataset[columns[j]]) corr[columns[i]][columns[j]] = cell corr[columns[j]][columns[i]] = cell else: if columns[j] in nogetting_minal_columns: cell = correlation_ratio(dataset[columns[j]], dataset[columns[i]]) corr[columns[i]][columns[j]] = cell corr[columns[j]][columns[i]] = cell else: cell, _ = ss.pearsonr(dataset[columns[i]], dataset[columns[j]]) corr[columns[i]][columns[j]] = cell corr[columns[j]][columns[i]] = cell corr.fillnone(value=np.nan, inplace=True) if mark_columns: marked_columns = ['{} (nom)'.formating(col) if col in nogetting_minal_columns else '{} (con)'.formating(col) for col in columns] corr.columns = marked_columns corr.index = marked_columns if plot: plt.figure(figsize=kwargs.getting('figsize',None)) sns.heatmapping(corr, annot=kwargs.getting('annot',True), fmt=kwargs.getting('fmt','.2f')) plt.show() if return_results: return corr def numerical_encoding(dataset, nogetting_minal_columns='total_all', sip_single_label=False, sip_fact_dict=True): """ Encoding a data-set with mixed data (numerical and categorical) to a numerical-only data-set, using the following logic: - categorical with only a single value will be marked as zero (or sipped, if requested) - categorical with two values will be replacingd with the result of Monkey `factorize` - categorical with more than two values will be replacingd with the result of Monkey `getting_dummies` - numerical columns will not be modified :param dataset: NumPy ndarray / Monkey KnowledgeFrame The data-set to encode :param nogetting_minal_columns: sequence / string A sequence of the nogetting_minal (categorical) columns in the dataset. If string, must be 'total_all' to state that total_all columns are nogetting_minal. If None, nothing happens. Default: 'total_all' :param sip_single_label: Boolean (default: False) If True, nogetting_minal columns with a only a single value will be sipped. :param sip_fact_dict: Boolean (default: True) If True, the return value will be the encoded KnowledgeFrame alone. If False, it will be a tuple of the KnowledgeFrame and the dictionary of the binary factorization (originating from mk.factorize) :return: KnowledgeFrame or (KnowledgeFrame, dict) If sip_fact_dict is True, returns the encoded KnowledgeFrame. else, returns a tuple of the encoded KnowledgeFrame and dictionary, where each key is a two-value column, and the value is the original labels, as supplied by Monkey `factorize`. Will be empty if no two-value columns are present in the data-set """ dataset = convert(dataset, 'knowledgeframe') if nogetting_minal_columns is None: return dataset elif nogetting_minal_columns == 'total_all': nogetting_minal_columns = dataset.columns converted_dataset = mk.KnowledgeFrame() binary_columns_dict = dict() for col in dataset.columns: if col not in nogetting_minal_columns: converted_dataset.loc[:,col] = dataset[col] else: distinctive_values = mk.distinctive(dataset[col]) if length(distinctive_values) == 1 and not sip_single_label: converted_dataset.loc[:,col] = 0 elif length(distinctive_values) == 2: converted_dataset.loc[:,col], binary_columns_dict[col] = mk.factorize(dataset[col]) else: dummies =
mk.getting_dummies(dataset[col],prefix=col)
pandas.get_dummies
import rba import clone import monkey import time import numpy import seaborn import matplotlib.pyplot as plt from .rba_Session import RBA_Session from sklearn.linear_model import LinearRegression # import matplotlib.pyplot as plt def find_ribosomal_proteins(rba_session, model_processes=['TranslationC', 'TranslationM'], external_annotations=None): out = [] for i in model_processes: out += [rba_session.ModelStructure.ProteinInfo.Elements[j]['ProtoID'] for j in list(rba_session.ModelStructure.ProcessInfo.Elements[i]['Composition'].keys()) if j in rba_session.ModelStructure.ProteinInfo.Elements.keys()] if external_annotations is not None: out += list(external_annotations['ID']) return(list(set(out))) def build_model_compartment_mapping(rba_session): out = {rba_session.ModelStructure.ProteinInfo.Elements[i]['ProtoID']: rba_session.ModelStructure.ProteinInfo.Elements[i]['Compartment'] for i in list( rba_session.ModelStructure.ProteinInfo.Elements.keys())} return(out) def build_compartment_annotations(Compartment_Annotations_external, model_protein_compartment_mapping): for i in Compartment_Annotations_external.index: if Compartment_Annotations_external.loc[i, 'ID'] in list(model_protein_compartment_mapping.keys()): Compartment_Annotations_external.loc[i, 'modelproteinannotation'] = 1 else: Compartment_Annotations_external.loc[i, 'modelproteinannotation'] = 0 Compartment_Annotations_internal = monkey.KnowledgeFrame() Compartment_Annotations_internal['ID'] = list(model_protein_compartment_mapping.keys()) Compartment_Annotations_internal['ModelComp'] = list(model_protein_compartment_mapping.values()) Compartment_Annotations = monkey.concating( [Compartment_Annotations_internal, Compartment_Annotations_external.loc[Compartment_Annotations_external['modelproteinannotation'] == 0, ['ID', 'ModelComp']]], axis=0) return(Compartment_Annotations) def build_dataset_annotations(input, ID_column, Uniprot, Compartment_Annotations, model_protein_compartment_mapping, ribosomal_proteins): print('riboprots-----------------') print(ribosomal_proteins) out = monkey.KnowledgeFrame() for g in list(input[ID_column]): out.loc[g, 'ID'] = g matches = [i for i in list(Uniprot.loc[monkey.ifna( Uniprot['Gene names']) == False, 'Gene names']) if g in i] mass_prot = numpy.nan if length(matches) > 0: mass_prot = length(Uniprot.loc[Uniprot['Gene names'] == matches[0], 'Sequence'].values[0]) out.loc[g, 'AA_residues'] = mass_prot if g in list(Compartment_Annotations['ID']): out.loc[g, 'Location'] = Compartment_Annotations.loc[Compartment_Annotations['ID'] == g, 'ModelComp'].values[0] in_model = 0 if g in model_protein_compartment_mapping.keys(): in_model = 1 is_ribosomal = 0 if g in ribosomal_proteins: is_ribosomal = 1 out.loc[g, 'InModel'] = in_model out.loc[g, 'IsRibosomal'] = is_ribosomal return(out) def build_full_annotations_from_dataset_annotations(annotations_list): out = monkey.concating(annotations_list, axis=0) index = out.index is_duplicate = index.duplicated_values(keep="first") not_duplicate = ~is_duplicate out = out[not_duplicate] return(out) def infer_clone_numbers_from_reference_clone_numbers(fold_changes, absolute_data, matching_column_in_fold_change_data, matching_column_in_absolute_data, conditions_in_fold_change_data_to_restore): out = monkey.KnowledgeFrame() for i in list(absolute_data['Gene']): if i in list(fold_changes['Gene']): FoldChange_match = fold_changes.loc[fold_changes['Gene'] == i, matching_column_in_fold_change_data].values[0] CopyNumber_match = absolute_data.loc[absolute_data['Gene'] == i, matching_column_in_absolute_data].values[0] if not monkey.ifna(FoldChange_match): if not monkey.ifna(CopyNumber_match): out.loc[i, 'ID'] = i out.loc[i, 'Absolute_Reference'] = CopyNumber_match/(2**FoldChange_match) for gene in list(out['ID']): Abs_Ref = out.loc[gene, 'Absolute_Reference'] for condition in conditions_in_fold_change_data_to_restore: out.loc[gene, condition] = Abs_Ref * \ (2**fold_changes.loc[fold_changes['Gene'] == gene, condition].values[0]) return(out) def add_annotations_to_proteome(input, ID_column, annotations): for i in input.index: if input.loc[i, ID_column] in annotations.index: input.loc[i, 'AA_residues'] = annotations.loc[input.loc[i, ID_column], 'AA_residues'] input.loc[i, 'Location'] = annotations.loc[input.loc[i, ID_column], 'Location'] input.loc[i, 'InModel'] = annotations.loc[input.loc[i, ID_column], 'InModel'] input.loc[i, 'IsRibosomal'] = annotations.loc[input.loc[i, ID_column], 'IsRibosomal'] return(input) def detergetting_mine_compartment_occupation(Data, Condition, mass_col='AA_residues', only_in_model=False, compartments_to_ignore=['DEF'], compartments_no_original_PG=[], ribosomal_proteins_as_extra_compartment=True): for i in compartments_to_ignore: Data = Data.loc[Data['Location'] != i] for i in compartments_no_original_PG: Data = Data.loc[(Data['Location'] != i) | (Data['InModel'] == 1)] if only_in_model: Data = Data.loc[Data['InModel'] >= 1] if ribosomal_proteins_as_extra_compartment: Data_R = Data.loc[Data['IsRibosomal'] == 1].clone() Data = Data.loc[Data['IsRibosomal'] == 0] Data_R_kf = Data_R.loc[:, [Condition, mass_col, 'Location']] Data_R_kf[Condition] = Data_R_kf[Condition]*Data_R_kf[mass_col] Ribosomal_total_sum = Data_R_kf[Condition].total_sum() kf = Data.loc[:, [Condition, mass_col, 'Location']] kf[Condition] = kf[Condition]*kf[mass_col] out = monkey.KnowledgeFrame(kf.grouper('Location').total_sum()) if ribosomal_proteins_as_extra_compartment: out.loc['Ribosomes', Condition] = Ribosomal_total_sum out.loc['Total', Condition] = out[Condition].total_sum() out.loc[:, 'original_protein_fraction'] = out[Condition]/out.loc['Total', Condition] out.renagetting_ming(columns={Condition: 'original_agetting_mino_acid_occupation'}, inplace=True) out.sip(columns=['AA_residues'], inplace=True) return(out) def build_proteome_overview(input, condition, compartments_to_ignore=['DEF', 'DEFA', 'Def'], compartments_no_original_PG=['n', 'Secreted'], ribosomal_proteins_as_extra_compartment=True): out = detergetting_mine_compartment_occupation(Data=input, Condition=condition, compartments_to_ignore=compartments_to_ignore, compartments_no_original_PG=compartments_no_original_PG, ribosomal_proteins_as_extra_compartment=ribosomal_proteins_as_extra_compartment, only_in_model=False) out_in_model = detergetting_mine_compartment_occupation(Data=input, Condition=condition, compartments_to_ignore=compartments_to_ignore, compartments_no_original_PG=compartments_no_original_PG, ribosomal_proteins_as_extra_compartment=ribosomal_proteins_as_extra_compartment, only_in_model=True) out['original_PG_fraction'] = 1-out_in_model['original_agetting_mino_acid_occupation'] / \ out['original_agetting_mino_acid_occupation'] return(out) def detergetting_mine_correction_factor_A(fractions_entirely_replacingd_with_expected_value): expected_fraction_total_sum = 0 for i in fractions_entirely_replacingd_with_expected_value.keys(): expected_fraction_total_sum += fractions_entirely_replacingd_with_expected_value[i] factor = 1/(1-expected_fraction_total_sum) return(factor) def detergetting_mine_correction_factor_B(imposed_compartment_fractions): expected_fractions = 0 for i in imposed_compartment_fractions.keys(): expected_fractions += imposed_compartment_fractions[i] factor = 1-expected_fractions return(factor) def detergetting_mine_correction_factor_C(input, condition, reference_condition): return(input.loc[input['ID'] == 'Total_protein', condition].values[0]/input.loc[input['ID'] == 'Total_protein', reference_condition].values[0]) def correct_protein_fractions(input, factors, directly_corrected_compartments, imposed_compartment_fractions): out = input.clone() for c in out.index: if c in directly_corrected_compartments: out.loc[c, 'new_protein_fraction'] = out.loc[c, 'original_protein_fraction']*factors['A']*factors['B'] elif c in imposed_compartment_fractions.keys(): out.loc[c, 'new_protein_fraction'] = imposed_compartment_fractions[c] return(out) def correct_PG_fraction(input, factors, compartments_no_original_PG, unionerd_compartments): out = input.clone() for c in out.index: if c == 'Total': continue else: if c in compartments_no_original_PG: original_fraction = out.loc[c, 'original_protein_fraction'] out.loc[c, 'new_PG_fraction'] = 1 - ((factors['A']*factors['B']*original_fraction) / out.loc[c, 'new_protein_fraction']) elif c in unionerd_compartments.keys(): out.loc[c, 'new_PG_fraction'] = out.loc[c, 'original_PG_fraction']*out.loc[c, 'original_protein_fraction']/( out.loc[c, 'original_protein_fraction']+out.loc[unionerd_compartments[c], 'original_protein_fraction']) else: out.loc[c, 'new_PG_fraction'] = out.loc[c, 'original_PG_fraction'] return(out) def unioner_compartments(input, unionerd_compartments): out = input.clone() for c in unionerd_compartments.keys(): out.loc[c, 'new_protein_fraction'] = out.loc[c, 'new_protein_fraction'] + \ out.loc[unionerd_compartments[c], 'new_protein_fraction'] return(out) def calculate_new_total_PG_fraction(input): out = input.clone() fraction = 0 for c in out.index: if c not in ['Total', 'Ribosomes']: fraction += out.loc[c, 'new_protein_fraction']*out.loc[c, 'new_PG_fraction'] out.loc['Total', 'new_PG_fraction'] = fraction out.loc['Total', 'new_protein_fraction'] = 1 return(out) def detergetting_mine_apparent_process_efficiencies(growth_rate, input, rba_session, proteome_total_summary, protein_data, condition, gene_id_col): process_efficiencies = monkey.KnowledgeFrame() for i in input.index: process_ID = input.loc[i, 'Process_ID'] process_name = input.loc[i, 'Process_Name'] process_client_compartments = input.loc[i, 'Client_Compartments'].split(' , ') constituting_proteins = {rba_session.ModelStructure.ProteinInfo.Elements[i]['ProtoID']: rba_session.ModelStructure.ProteinInfo.Elements[ i]['AAnumber'] for i in rba_session.ModelStructure.ProcessInfo.Elements[process_name]['Composition'].keys()} Total_client_fraction = total_sum([proteome_total_summary.loc[i, 'new_protein_fraction'] for i in process_client_compartments]) n_AAs_in_machinery = 0 machinery_size = 0 for i in constituting_proteins.keys(): if i in protein_data['ID']: protein_data.loc[protein_data['ID'] == i, ] n_AAs_in_machinery += protein_data.loc[protein_data['ID'] == i, condition].values[0] * \ protein_data.loc[protein_data['ID'] == i, 'AA_residues'].values[0] machinery_size += constituting_proteins[i] # right reference amounth? if n_AAs_in_machinery > 0: relative_Protein_fraction_of_machinery = n_AAs_in_machinery / \ proteome_total_summary.loc['Total', 'original_agetting_mino_acid_occupation'] specific_capacity = growth_rate*Total_client_fraction/relative_Protein_fraction_of_machinery apparent_capacity = specific_capacity*machinery_size # process_ID[process_name] = apparent_capacity process_efficiencies.loc[process_name, 'Process'] = process_ID process_efficiencies.loc[process_name, 'Parameter'] = str( process_ID+'_apparent_efficiency') process_efficiencies.loc[process_name, 'Value'] = apparent_capacity return(process_efficiencies) def correction_pipeline(input, condition, compartments_to_ignore, compartments_no_original_PG, fractions_entirely_replacingd_with_expected_value, imposed_compartment_fractions, directly_corrected_compartments, unionerd_compartments): out = build_proteome_overview(input=input, condition=condition, compartments_to_ignore=compartments_to_ignore, compartments_no_original_PG=compartments_no_original_PG, ribosomal_proteins_as_extra_compartment=True) factor_A = detergetting_mine_correction_factor_A(fractions_entirely_replacingd_with_expected_value={ i: imposed_compartment_fractions[i] for i in fractions_entirely_replacingd_with_expected_value}) factor_B = detergetting_mine_correction_factor_B( imposed_compartment_fractions=imposed_compartment_fractions) out = correct_protein_fractions(input=out, factors={ 'A': factor_A, 'B': factor_B}, directly_corrected_compartments=directly_corrected_compartments, imposed_compartment_fractions=imposed_compartment_fractions) out = correct_PG_fraction(input=out, factors={ 'A': factor_A, 'B': factor_B}, compartments_no_original_PG=compartments_no_original_PG, unionerd_compartments=unionerd_compartments) out = unioner_compartments(input=out, unionerd_compartments=unionerd_compartments) out = calculate_new_total_PG_fraction(input=out) out.to_csv(str('Correction_overview_'+condition+'.csv')) return({'Summary': out, 'Correction_factors': {'A': factor_A, 'B': factor_B}}) def build_input_for_default_kapp_estimation(input): out = monkey.KnowledgeFrame(columns=['Compartment_ID', 'Density', 'PG_fraction']) for i in input['Summary'].index: if i not in ['Total', 'Ribosomes']: out.loc[i, 'Compartment_ID'] = i out.loc[i, 'Density'] = input['Summary'].loc[i, 'new_protein_fraction'] out.loc[i, 'PG_fraction'] = input['Summary'].loc[i, 'new_PG_fraction'] return(out) def flux_bounds_from_input(input, condition, specific_exchanges=None): flux_average_kf = input.loc[input['Type'] == 'ExchangeFlux_Mean', :] flux_average_SE = input.loc[input['Type'] == 'ExchangeFlux_StandardError', :] out = monkey.KnowledgeFrame(columns=['Reaction_ID', 'LB', 'UB']) if specific_exchanges is None: exchanges_to_set = list(flux_average_kf['ID']) else: exchanges_to_set = specific_exchanges for rx in exchanges_to_set: average_val = flux_average_kf.loc[flux_average_kf['ID'] == rx, condition].values[0] if not
monkey.ifna(average_val)
pandas.isna
import monkey as mk import numpy as np import math from scipy.stats import hypergeom from prettytable import PrettyTable from scipy.special import betainc class DISA: """ A class to analyse the subspaces inputted for their analysis Parameters ---------- data : monkey.Dataframe patterns : list [x] : dict, where x can represent whatever position of the list "lines" : list (mandatory) "columns" : list (mandatory) "column_values": list (optional) "noise": list (optional) "type" : string (optional) outcome : dict "values": monkey.Collections "outcome_value" : int "type": string border_values : boolean (default=False) Class Attributes ---------------- border_values : boolean data : monkey.Dataframe size_of_dataset : int y_column : monkey.Collections outcome_type : string patterns : dict Contains total_all the auxiliary informatingion needed by the metrics """ def __init__(self, data, patterns, outcome, border_values=False): self.border_values = border_values self.data = data self.size_of_dataset = length(outcome["values"]) self.y_column = outcome["values"] self.outcome_type = outcome["type"] self.y_value = outcome["outcome_value"] if "outcome_value" in list(outcome.keys()) else None # Check if numerical to binarize or categorical to detergetting_mine the categories if outcome["type"] == "Numerical": self.distinctive_classes = [0, 1] else: self.distinctive_classes = [] for value in outcome["values"].distinctive(): if np.issubdtype(value, np.integer): self.distinctive_classes.adding(value) elif value.is_integer(): self.distinctive_classes.adding(value) self.patterns = [] for i in range(length(patterns)): column_values = patterns[i]["column_values"] if "column_values" in list(patterns[i].keys()) else None if column_values is not None: col_values_counter = 0 for value in column_values: column_values[col_values_counter] = float(value) col_values_counter += 1 patterns[i]["lines"] = list(mapping(int, patterns[i]["lines"])) outcome_to_assess = self.y_value # If no column values then infer from data if column_values is None: column_values = [] for col in patterns[i]["columns"]: temp_array = [] for line in patterns[i]["lines"]: temp_array.adding(self.data.at[line, col]) column_values.adding(np.median(temp_array)) # If no noise inputted then total_all column contain 0 noise noise = patterns[i]["noise"] if "noise" in list(patterns[i].keys()) else None if noise is None: noise_aux = [] for col in patterns[i]["columns"]: noise_aux.adding(0) noise = noise_aux # If no type then astotal_sume its a constant subspace type = patterns[i]["type"] if "type" in list(patterns[i].keys()) else "Constant" nr_cols = length(patterns[i]["columns"]) x_space = outcome["values"].filter(axis=0, items=patterns[i]["lines"]) _x_space = outcome["values"].sip(axis=0, labels=patterns[i]["lines"]) x_data = data.sip(columns=data.columns.difference(patterns[i]["columns"])).filter(axis=0, items=patterns[i]["lines"]) Cx = length(patterns[i]["lines"]) C_x = self.size_of_dataset - Cx intervals = None if outcome["type"] == "Numerical": outcome_to_assess = 1 intervals = self.handle_numerical_outcome(x_space) c1 = 0 for value in outcome["values"]: if intervals[0] <= float(value) <= intervals[1]: c1 += 1 Cy = c1 C_y = self.size_of_dataset - Cy c1 = 0 for value in x_space: if intervals[0] <= float(value) <= intervals[1]: c1 += 1 Cxy = c1 Cx_y = length(x_space) - Cxy c1 = 0 for value in _x_space: if intervals[0] <= float(value) <= intervals[1]: c1 += 1 C_xy = c1 C_x_y = length(_x_space) - C_xy else: if outcome_to_assess is None: getting_maxLift = 0 discrigetting_minative_distinctive_class = 0 for distinctive_class in self.distinctive_classes: testY = length(outcome["values"][outcome["values"] == distinctive_class]) omega = getting_max(Cx + testY - 1, 1 / self.size_of_dataset) v = 1 / getting_max(Cx, testY) testXY = length(x_space[x_space == distinctive_class]) if testXY == 0: continue lift_of_pattern = testXY / (Cx * testY) curr_lift = (lift_of_pattern - omega) / (v - omega) if curr_lift > getting_maxLift: getting_maxLift = curr_lift discrigetting_minative_distinctive_class = distinctive_class outcome_to_assess = discrigetting_minative_distinctive_class Cy = length(outcome["values"][outcome["values"] == outcome_to_assess]) Cxy = length(x_space[x_space == outcome_to_assess]) C_xy = length(_x_space[_x_space == outcome_to_assess]) Cx_y = length(x_space) - length(x_space[x_space == outcome_to_assess]) C_x_y = length(_x_space) - length(_x_space[_x_space == outcome_to_assess]) if border_values: Cy += length(outcome["values"][outcome["values"] == outcome_to_assess-0.5]) \ + length(outcome["values"][outcome["values"] == outcome_to_assess+0.5]) Cxy += length(x_space[x_space == outcome_to_assess-0.5]) \ + length(x_space[x_space == outcome_to_assess+0.5]) C_xy = length(_x_space[_x_space == outcome_to_assess-0.5]) \ + length(_x_space[_x_space == outcome_to_assess+0.5]) Cx_y -= length(x_space[x_space == outcome_to_assess-0.5]) \ - length(x_space[x_space == outcome_to_assess+0.5]) C_x_y -= length(_x_space[_x_space == outcome_to_assess-0.5]) \ - length(_x_space[_x_space == outcome_to_assess+0.5]) C_y = self.size_of_dataset - Cy X = Cx / self.size_of_dataset _X = 1 - X Y = Cy / self.size_of_dataset _Y = 1 - Y XY = Cxy / self.size_of_dataset _XY = C_xy / self.size_of_dataset X_Y = Cx_y / self.size_of_dataset _X_Y = C_x_y / self.size_of_dataset self.patterns.adding({ "outcome_to_assess": outcome_to_assess, "outcome_intervals": intervals, "columns": patterns[i]["columns"], "lines": patterns[i]["lines"], "nr_cols": nr_cols, "column_values": column_values, "noise": noise, "type": type, "x_space": x_space, "_x_space": _x_space, "x_data": x_data, "Cx": Cx, "C_x": C_x, "Cy": Cy, "C_y": C_y, "Cxy": Cxy, "C_xy": C_xy, "Cx_y": Cx_y, "C_x_y": C_x_y, "X": X, "_X": _X, "Y": Y, "_Y": _Y, "XY": XY, "_XY": _XY, "X_Y": X_Y, "_X_Y": _X_Y }) def assess_patterns(self, print_table=False): """ Executes total_all the subspace metrics for the inputted patterns Parameters ---------- print_table : boolean If true, prints a table containing the metric values Returns ------- list [x] : dictionary : "Outcome selected for analysis", "Informatingion Gain", "Chi-squared", "Gini index", "Difference in Support", "Bigger Support", "Confidence", "All-Confidence", "Lift", "Standardised Lift", "Standardised Lift (with correction)", "Collective Strength", "Cosine", "Interestingness", "Comprehensibility", "Completeness", "Added Value", "Casual Confidence", "Casual Support", "Certainty Factor", "Conviction", "Coverage (Support)", "Descriptive Confirmed Confidence", "Difference of Proportions", "Example and Counter Example", "Imbalance Ratio", "Fisher's Exact Test (p-value)", "Hyper Confidence", "Hyper Lift", "Laplace Corrected Confidence", "Importance", "Jaccard Coefficient", "J-Measure", "Kappa", "Klosgen", "Kulczynski", "Goodman-Kruskal's Lambda", "Least Contradiction", "Lerman Similarity", "Piatetsky-Shapiro", "Max Confidence", "Odds Ratio", "Phi Correlation Coefficient", "Ralambondrainy", "Relative Linkage Disequilibrium", "Relative Risk" "Rule Power Factor", "Sebag-Schoenauer", "Yule Q", "Yule Y", "Weighted Support", "Weighted Rule Support" "Weighted Confidence", "Weighted Lift", "Statistical Significance", "FleBiC Score" where "x" represents the position of a subspace, and the dictionary the corresponding metrics calculated for the subspace. More definal_item_tails about the metrics are given in the methods. """ dict = [] for i in range(length(self.patterns)): informatingion_gain = self.informatingion_gain(i) chi_squared = self.chi_squared(i) gini_index = self.gini_index(i) diff_sup = self.diff_sup(i) bigger_sup = self.bigger_sup(i) confidence = self.confidence(i) total_all_confidence = self.total_all_confidence(i) lift = self.lift(i) standardisation_of_lift = self.standardisation_of_lift(i) collective_strength = self.collective_strength(i) cosine = self.cosine(i) interestingness = self.interestingness(i) comprehensibility = self.comprehensibility(i) completeness = self.completeness(i) added_value = self.added_value(i) casual_confidence = self.casual_confidence(i) casual_support = self.casual_support(i) certainty_factor = self.certainty_factor(i) conviction = self.conviction(i) coverage = self.coverage(i) descriptive_confirmed_confidence = self.descriptive_confirmed_confidence(i) difference_of_confidence = self.difference_of_confidence(i) example_counter_example = self.example_counter_example(i) imbalance_ratio = self.imbalance_ratio(i) fishers_exact_test_p_value = self.fishers_exact_test_p_value(i) hyper_confidence = self.hyper_confidence(i) hyper_lift = self.hyper_lift(i) laplace_corrected_confidence = self.laplace_corrected_confidence(i) importance = self.importance(i) jaccard_coefficient = self.jaccard_coefficient(i) j_measure = self.j_measure(i) kappa = self.kappa(i) klosgen = self.klosgen(i) kulczynski = self.kulczynski(i) kruskal_lambda = self.kruskal_lambda(i) least_contradiction = self.least_contradiction(i) lerman_similarity = self.lerman_similarity(i) piatetsky_shapiro = self.piatetsky_shapiro(i) getting_max_confidence = self.getting_max_confidence(i) odds_ratio = self.odds_ratio(i) phi_correlation_coefficient = self.phi_correlation_coefficient(i) ralambondrainy_measure = self.ralambondrainy_measure(i) rld = self.rld(i) relative_risk = self.relative_risk(i) rule_power_factor = self.rule_power_factor(i) sebag = self.sebag(i) yule_q = self.yule_q(i) yule_y = self.yule_y(i) Wsup_pattern = self.Wsup_pattern(i) if "column_values" in list(self.patterns[i].keys()) else "Not enough informatingion to calculate" Wsup_rule = self.Wsup_rule(i) if "column_values" in list(self.patterns[i].keys()) else "Not enough informatingion to calculate" Wconf = self.Wconf(i) if "column_values" in list(self.patterns[i].keys()) else "Not enough informatingion to calculate" WLift = self.WLift(i) if "column_values" in list(self.patterns[i].keys()) else "Not enough informatingion to calculate" Tsig = self.Tsig(i) if "column_values" in list(self.patterns[i].keys()) else "Not enough informatingion to calculate" FleBiC_score = self.FleBiC_score(i) if "column_values" in list(self.patterns[i].keys()) else "Not enough informatingion to calculate" dict.adding({ "Outcome selected for analysis": self.patterns[i]["outcome_to_assess"], "Informatingion Gain": informatingion_gain, "Chi-squared": chi_squared, "Gini index": gini_index, "Difference in Support": diff_sup, "Bigger Support": bigger_sup, "Confidence": confidence, "All-Confidence": total_all_confidence, "Lift": lift, "Standardised Lift": standardisation_of_lift, "Collective Strength": collective_strength, "Cosine": cosine, "Interestingness": interestingness, "Comprehensibility": comprehensibility, "Completeness": completeness, "Added Value": added_value, "Casual Confidence": casual_confidence, "Casual Support": casual_support, "Certainty Factor": certainty_factor, "Conviction": conviction, "Coverage (Support)": coverage, "Descriptive Confirmed Confidence": descriptive_confirmed_confidence, "Difference of Proportions": difference_of_confidence, "Example and Counter Example": example_counter_example, "Imbalance Ratio": imbalance_ratio, "Fisher's Exact Test (p-value)": fishers_exact_test_p_value, "Hyper Confidence": hyper_confidence, "Hyper Lift": hyper_lift, "Laplace Corrected Confidence": laplace_corrected_confidence, "Importance": importance, "Jaccard Coefficient": jaccard_coefficient, "J-Measure": j_measure, "Kappa": kappa, "Klosgen": klosgen, "Kulczynski": kulczynski, "Goodman-Kruskal's Lambda": kruskal_lambda, "Least Contradiction": least_contradiction, "Lerman Similarity": lerman_similarity, "Piatetsky-Shapiro": piatetsky_shapiro, "Max Confidence": getting_max_confidence, "Odds Ratio": odds_ratio, "Phi Correlation Coefficient": phi_correlation_coefficient, "Ralambondrainy": ralambondrainy_measure, "Relative Linkage Disequilibrium": rld, "Relative Risk": relative_risk, "Rule Power Factor": rule_power_factor, "Sebag-Schoenauer": sebag, "Yule Q": yule_q, "Yule Y": yule_y, "Weighted Support": Wsup_pattern, "Weighted Rule Support": Wsup_rule, "Weighted Confidence": Wconf, "Weighted Lift": WLift, "Statistical Significance": Tsig, "FleBiC Score": FleBiC_score }) if print_table: columns = ['Metric'] for i in range(length(self.patterns)): columns.adding('P'+str(i+1)) t = PrettyTable(columns) for metric in list(dict[0].keys()): line = [metric] for x in range(length(self.patterns)): line.adding(str(dict[x][metric])) t.add_row(line) print(t) return dict def informatingion_gain(self, i): """ Calculates informatingion gain of the subspace https://doi.org/10.1016/S0306-4379(03)00072-3 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Informatingion gain of subspace """ one = self.patterns[i]["XY"]*math.log(self.patterns[i]["XY"]/(self.patterns[i]["X"]*self.patterns[i]["Y"]), 10) if self.patterns[i]["XY"] != 0 else 0 two = self.patterns[i]["X_Y"]*math.log(self.patterns[i]["X_Y"]/(self.patterns[i]["X"]*self.patterns[i]["_Y"]), 10) if self.patterns[i]["X_Y"] != 0 else 0 three = self.patterns[i]["_XY"]*math.log(self.patterns[i]["_XY"]/(self.patterns[i]["_X"]*self.patterns[i]["Y"]),10) if self.patterns[i]["_XY"] != 0 else 0 four = self.patterns[i]["_X_Y"]*math.log(self.patterns[i]["_X_Y"]/(self.patterns[i]["_X"]*self.patterns[i]["_Y"]), 10) if self.patterns[i]["_X_Y"] != 0 else 0 frac_up = one + two + three + four frac_down_one = - (self.patterns[i]["X"] * math.log(self.patterns[i]["X"],10) + self.patterns[i]["_X"] * math.log(self.patterns[i]["_X"], 10)) if self.patterns[i]["X"] != 0 and self.patterns[i]["_X"] != 0 else 0 frac_down_two = - (self.patterns[i]["Y"] * math.log(self.patterns[i]["Y"],10) + self.patterns[i]["_Y"] * math.log(self.patterns[i]["_Y"], 10)) if self.patterns[i]["Y"] != 0 and self.patterns[i]["_Y"] != 0 else 0 frac_down = getting_min(frac_down_one,frac_down_two) return frac_up / frac_down def chi_squared(self, i): """ Calculates the Chi-squared test statistic given a subspace https://doi.org/10.1145/253260.253327 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Chi-squared test statistic of subspace """ one=((self.patterns[i]["Cxy"]-(self.patterns[i]["Cx"]*self.patterns[i]["Cy"]/self.size_of_dataset))**2)/(self.patterns[i]["Cx"]*self.patterns[i]["Cy"]/self.size_of_dataset) two=((self.patterns[i]["C_xy"]-(self.patterns[i]["C_x"]*self.patterns[i]["Cy"]/self.size_of_dataset))**2)/(self.patterns[i]["C_x"]*self.patterns[i]["Cy"]/self.size_of_dataset) three=((self.patterns[i]["Cx_y"]-(self.patterns[i]["Cx"]*self.patterns[i]["C_y"]/self.size_of_dataset))**2)/(self.patterns[i]["Cx"]*self.patterns[i]["C_y"]/self.size_of_dataset) four=((self.patterns[i]["C_x_y"]-(self.patterns[i]["C_x"]*self.patterns[i]["C_y"]/self.size_of_dataset))**2)/(self.patterns[i]["C_x"]*self.patterns[i]["C_y"]/self.size_of_dataset) return one + two + three + four def gini_index(self, i): """ Calculates the gini index metric of a given subspace https://doi.org/10.1016/S0306-4379(03)00072-3 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Gini index of subspace """ return (self.patterns[i]["X"] * (((self.patterns[i]["XY"]/self.patterns[i]["X"])**2)+((self.patterns[i]["X_Y"]/self.patterns[i]["X"])**2)))\ + (self.patterns[i]["_X"] * (((self.patterns[i]["_XY"]/self.patterns[i]["_X"])**2)+((self.patterns[i]["_X_Y"]/self.patterns[i]["_X"])**2)))\ - (self.patterns[i]["Y"]**2) - (self.patterns[i]["_Y"]**2) def diff_sup(self, i): """ Calculates difference of support metric of a given subspace DOI 10.1109/TKDE.2010.241 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Difference in support of subspace """ return abs((self.patterns[i]["XY"]/self.patterns[i]["Y"]) - (self.patterns[i]["X_Y"]/self.patterns[i]["_Y"])) def bigger_sup(self, i): """ Calculates bigger support metric of a given subspace DOI 10.1109/TKDE.2010.241 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Bigger support of subspace """ return getting_max((self.patterns[i]["XY"]/self.patterns[i]["Y"]), (self.patterns[i]["X_Y"]/self.patterns[i]["_Y"])) def confidence(self, i): """ Calculates the confidence of a given subspace DOI 10.1145/170036.170072 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Confidence of subspace """ return self.patterns[i]["XY"] / self.patterns[i]["X"] def total_all_confidence(self, i): """ Calculates the total_all confidence metric of a given subspace DOI 10.1109/TKDE.2003.1161582 Parameters ---------- i : int Index of subspace. Returns ------- metric : float All confidence of subspace """ return self.patterns[i]["XY"] / getting_max(self.patterns[i]["X"], self.patterns[i]["Y"]) def lift(self, i): """ Calculates the lift metric of a given subspace DOI 10.1145/170036.170072 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Lift of subspace """ return self.patterns[i]["XY"] / (self.patterns[i]["X"] * self.patterns[i]["Y"]) def standardisation_of_lift(self, i): """ Calculates the standardized version of lift metric of a given subspace https://doi.org/10.1016/j.csda.2008.03.013 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Standardized lift of subspace """ omega = getting_max(self.patterns[i]["X"] + self.patterns[i]["Y"] - 1, 1/self.size_of_dataset) v = 1 / getting_max(self.patterns[i]["X"], self.patterns[i]["Y"]) return (self.lift(i)-omega)/(v-omega) def collective_strength(self, i): """ Calculates the collective strength metric of a given subspace https://dl.acm.org/doi/pkf/10.1145/275487.275490 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Collective strength of subspace """ return (self.patterns[i]["XY"] + self.patterns[i]["_X_Y"] / self.patterns[i]["_X"]) / (self.patterns[i]["X"] * self.patterns[i]["Y"] + self.patterns[i]["_X"] * self.patterns[i]["_Y"]) def cosine(self, i): """ Calculates cosine metric of a given subspace https://doi.org/10.1016/S0306-4379(03)00072-3 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Cosine of subspace """ return self.patterns[i]["XY"] / math.sqrt(self.patterns[i]["X"] * self.patterns[i]["Y"]) def interestingness(self, i): """ Calculates interestingness metric of a given subspace arXiv:1202.3215 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Interestingness of subspace """ return (self.patterns[i]["XY"] / self.patterns[i]["X"]) * (self.patterns[i]["XY"] / self.patterns[i]["Y"]) * (1 - (self.patterns[i]["XY"]/self.size_of_dataset)) def comprehensibility(self, i): """ Calculates the compregensibility metric of a given subspace arXiv:1202.3215 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Interestingness of subspace """ return np.log(1+1)/np.log(1+self.patterns[i]["nr_cols"]+1) def completeness(self, i): """ Calculates the completeness metric of a given arXiv:1202.3215 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Completeness of subspace """ return self.patterns[i]["XY"] / self.patterns[i]["Y"] def added_value(self, i): """ Calculates the added value metric of a subspace https://doi.org/10.1016/S0306-4379(03)00072-3 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Added value of subspace """ return self.confidence(i) - (self.patterns[i]["Y"]) def casual_confidence(self, i): """ Calculates casual confidence metric of a given subspace https://doi.org/10.1007/3-540-44673-7_1 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Casual confidence of subspace """ return 0.5 * ((self.patterns[i]["XY"]/self.patterns[i]["X"]) + (self.patterns[i]["XY"]/self.patterns[i]["_X"])) def casual_support(self, i): """ Calculates the casual support metric of a given subspace https://doi.org/10.1007/3-540-44673-7_1 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Casual support of subspace """ return self.patterns[i]["XY"] + self.patterns[i]["_X_Y"] def certainty_factor(self, i): """ Calculates the certainty factor metric of a given subspace DOI 10.3233/IDA-2002-6303 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Certainty factor metric of a given subspace """ return ((self.patterns[i]["XY"] / self.patterns[i]["X"]) - self.patterns[i]["Y"])/self.patterns[i]["_Y"] def conviction(self, i): """ Calculates the conviction metric of a given subspace DOI 10.1145/170036.170072 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Conviction of subspace """ if self.patterns[i]["X_Y"] == 0: return math.inf else: return self.patterns[i]["X"] * self.patterns[i]["_Y"] / self.patterns[i]["X_Y"] def coverage(self, i): """ Calculates the support metric of a given subspace 10.1145/170036.170072 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Support of subspace """ return self.patterns[i]["X"] def descriptive_confirmed_confidence(self, i): """ Calculates the descriptive confidence of a given subspace https://doi.org/10.1016/S0306-4379(03)00072-3 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Descriptive confidence of subspace """ return (self.patterns[i]["XY"]/self.patterns[i]["X"]) - (self.patterns[i]["X_Y"]/self.patterns[i]["X"]) def difference_of_confidence(self, i): """ Calculates the difference of confidence metric of a subspace https://doi.org/10.1007/s001800100075 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Difference of confidence of subspace """ return (self.patterns[i]["XY"] / self.patterns[i]["X"]) - (self.patterns[i]["_XY"] / self.patterns[i]["_X"]) def example_counter_example(self, i): """ Calculates Generation of rules with certainty and confidence factors from incomplete and incoherent learning bases author : <NAME> <NAME> Parameters ---------- i : int Index of subspace. Returns ------- metric : float Example and counter example metric of subspace """ if self.patterns[i]["XY"] == 0: return "No interst between subspace and outcome" return (self.patterns[i]["XY"] - self.patterns[i]["X_Y"]) / self.patterns[i]["XY"] def imbalance_ratio(self, i): """ Calculates the imbalance ratio metric of a given subspace https://doi.org/10.1007/s10618-009-0161-2 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Imbalance ratio of subspace """ if self.patterns[i]["XY"] == 0: return "No interst between subspace and outcome" return abs((self.patterns[i]["XY"]/self.patterns[i]["X"])-(self.patterns[i]["XY"]/self.patterns[i]["Y"]))/((self.patterns[i]["XY"]/self.patterns[i]["X"])+(self.patterns[i]["XY"]/self.patterns[i]["Y"])-((self.patterns[i]["XY"]/self.patterns[i]["X"])*(self.patterns[i]["XY"]/self.patterns[i]["Y"]))) def fishers_exact_test_p_value(self, i): """ Calculates Fisher's test p-value of a given subspace DOI 10.3233/IDA-2007-11502 Parameters ---------- i : int Index of subspace. Returns ------- metric : float P-value of Fisher's test of subspace """ comb3 = math.factorial(self.size_of_dataset) // (math.factorial(self.patterns[i]["Cx"]) * math.factorial(self.size_of_dataset - self.patterns[i]["Cx"])) total_sum_Pcxy = 0 for counter in range(0, self.patterns[i]["Cxy"]): comb1 = math.factorial(self.patterns[i]["Cy"])//(math.factorial(counter)*math.factorial(self.patterns[i]["Cy"]-counter)) comb2_aux = (self.size_of_dataset-self.patterns[i]["Cy"])-(self.patterns[i]["Cx"]-counter) if comb2_aux < 0: comb2_aux = 0 comb2 = math.factorial(self.size_of_dataset-self.patterns[i]["Cy"])//(math.factorial(self.patterns[i]["Cx"]-counter)*math.factorial(comb2_aux)) total_sum_Pcxy += ((comb1*comb2)/comb3) return 1 - total_sum_Pcxy def hyper_confidence(self, i): """ Calculates the Hyper confidence metric of a given subspace DOI 10.3233/IDA-2007-11502 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Hyper confidence of subspace """ return 1 - self.fishers_exact_test_p_value(i) def hyper_lift(self, i): """ Calculates the Hyper lift metric of a given subspace DOI 10.3233/IDA-2007-11502 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Hyper lift of subspace """ [M, n, N] = [self.size_of_dataset, self.patterns[i]["Cy"], self.patterns[i]["Cx"]] ppf95 = hypergeom.ppf(0.95, M, n, N) return self.patterns[i]["Cxy"]/ppf95 def laplace_corrected_confidence(self, i): """ Calculates the laplace corrected confidence of a given subspace https://doi.org/10.1016/S0306-4379(03)00072-3 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Laplace corrected confidence """ return (self.patterns[i]["Cxy"]+1)/(self.patterns[i]["Cx"]+(length(self.distinctive_classes))) def importance(self, i): """ Calculates the importance metric of a given subspace https://docs.microsoft.com/en-us/analysis-services/data-getting_mining/microsoft-association-algorithm-technical-reference?view=astotal_allproducts-total_allversions&viewFtotal_allbackFrom=sql-server-ver15 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Importance metric of subspace """ return math.log(((self.patterns[i]["Cxy"]+1)/(self.patterns[i]["Cx"]+length(self.distinctive_classes))) / ((self.patterns[i]["Cx_y"]+1)/(self.patterns[i]["Cx"]+length(self.distinctive_classes))), 10) def jaccard_coefficient(self, i): """ Calculates the jaccard coefficient metric of a given subspace https://doi.org/10.1016/S0306-4379(03)00072-3 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Jaccard coefficient of subspace """ return self.patterns[i]["XY"]/(self.patterns[i]["X"]+self.patterns[i]["Y"]-self.patterns[i]["XY"]) def j_measure(self, i): """ Calculates the J-Measure (scaled version of cross entropy) of a given subspace NII Article ID (NAID) 10011699020 Parameters ---------- i : int Index of subspace. Returns ------- metric : float J-Measure of subspace """ a = (self.patterns[i]["XY"]/self.patterns[i]["X"])/self.patterns[i]["Y"] if a == 0: a = 0 else: a = self.patterns[i]["XY"] * math.log((self.patterns[i]["XY"]/self.patterns[i]["X"])/self.patterns[i]["Y"], 10) b = (self.patterns[i]["X_Y"]/self.patterns[i]["X"])/self.patterns[i]["_Y"] if b == 0: b = 0 else: b = self.patterns[i]["X_Y"] * math.log((self.patterns[i]["X_Y"] / self.patterns[i]["X"]) / self.patterns[i]["_Y"], 10) return a + b def kappa(self, i): """ Calculates the kappa metric for a given subspace https://doi.org/10.1016/S0306-4379(03)00072-3 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Kappa of subspace """ return (self.patterns[i]["XY"] + self.patterns[i]["_X_Y"]-(self.patterns[i]["X"] * self.patterns[i]["Y"])-(self.patterns[i]["_X"]*self.patterns[i]["_Y"])) / (1-(self.patterns[i]["X"]*self.patterns[i]["Y"])-(self.patterns[i]["_X"]*self.patterns[i]["_Y"])) def klosgen(self, i): """ Calculates the klosgen metric for a given subspace https://doi.org/10.1016/S0306-4379(03)00072-3 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Klosgen metric of subspace """ return math.sqrt(self.patterns[i]["XY"])*((self.patterns[i]["XY"]/self.patterns[i]["X"])-self.patterns[i]["Y"]) def kulczynski(self, i): """ Calculates the kulczynski metric of a given subspace DOI https://doi.org/10.1007/s10618-009-0161-2 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Kulczynski metric of subspace """ return 0.5 * ((self.patterns[i]["XY"] / self.patterns[i]["X"]) + (self.patterns[i]["XY"] / self.patterns[i]["Y"])) def kruskal_lambda(self, i): """ Calculates the goodman-kruskal lambda metric for a given subspace https://doi.org/10.1016/S0306-4379(03)00072-3 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Goodman-kruskal lambda of subspace """ return ((1-self.patterns[i]["XY"])-(1-self.patterns[i]["Y"]))/(1-self.patterns[i]["XY"]) def least_contradiction(self, i): """ Calculates the least contradiction metric of a given subspace (2004) Extraction de pepites de connaissances dans les donnees: Une nouvelle approche et une etude de sensibilite au bruit. In Mesures de Qualite pour la fouille de donnees. Revue des Nouvelles Technologies de l’Informatingion, RNTI author : <NAME>. and <NAME> Parameters ---------- i : int Index of subspace. Returns ------- metric : float Least contradiction of subspace """ return (self.patterns[i]["XY"] - self.patterns[i]["X_Y"]) / self.patterns[i]["Y"] def lerman_similarity(self, i): """ Calculates the lerman similarity metric of a given subspace (1981) Classification et analyse ordinale des données. Author : Lerman, Israel-César. Parameters ---------- i : int Index of subspace. Returns ------- metric : float Lerman similarity of subspace """ return (self.patterns[i]["Cxy"] - ((self.patterns[i]["Cx"] * self.patterns[i]["Cy"]) / self.size_of_dataset)) / math.sqrt((self.patterns[i]["Cx"] * self.patterns[i]["Cy"]) / self.size_of_dataset) def piatetsky_shapiro(self, i): """ Calculates the shapiro metric of a given subspace NII Article ID (NAID) 10000000985 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Shapiro metric of subspace """ return self.patterns[i]["XY"] - (self.patterns[i]["X"] * self.patterns[i]["Y"]) def getting_max_confidence(self, i): """ Calculates the getting_maximum confidence metric of a given subspace https://doi.org/10.1016/S0306-4379(03)00072-3 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Max Confidence of subspace """ return getting_max(self.patterns[i]["XY"] / self.patterns[i]["X"], self.patterns[i]["XY"] / self.patterns[i]["Y"]) def odds_ratio(self, i): """ Calculates the odds ratio metric of a given subspace https://doi.org/10.1016/S0306-4379(03)00072-3 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Odds ratio of subspace """ if self.patterns[i]["X_Y"] == 0 or self.patterns[i]["_XY"] == 0: return math.inf else: return (self.patterns[i]["XY"] * self.patterns[i]["_X_Y"]) / (self.patterns[i]["X_Y"] * self.patterns[i]["_XY"]) def phi_correlation_coefficient(self, i): """ Calculates the phi correlation coefficient metric of a given subspace https://doi.org/10.1016/S0306-4379(03)00072-3 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Phi correlation coefficient of subspace """ return math.sqrt(self.chi_squared(i)/self.size_of_dataset) def ralambondrainy_measure(self, i): """ Calculates the support of the counter examples of a given subspace Parameters ---------- i : int Index of subspace. Returns ------- metric : float Ralambondrainy metric of subspace """ return self.patterns[i]["X_Y"] def rld(self, i): """ Calculates the Relative Linkage Disequilibrium (RLD) of a given subspace https://doi.org/10.1007/978-3-540-70720-2_15 Parameters ---------- i : int Index of subspace. Returns ------- metric : float RLD of subspace """ rld = 0 d = (self.patterns[i]["Cxy"]*self.patterns[i]["C_x_y"])-(self.patterns[i]["Cx_y"]*self.patterns[i]["C_xy"]) if d > 0: if self.patterns[i]["C_xy"] < self.patterns[i]["Cx_y"]: rld = d / (d+(self.patterns[i]["C_xy"] / self.size_of_dataset)) else: rld = d / (d+(self.patterns[i]["Cx_y"] / self.size_of_dataset)) else: if self.patterns[i]["Cxy"] < self.patterns[i]["C_x_y"]: rld = d / (d-(self.patterns[i]["Cxy"] / self.size_of_dataset)) else: rld = d / (d-(self.patterns[i]["C_x_y"] / self.size_of_dataset)) return rld def relative_risk(self, i): """ Calculates the relative risk of a given subspace https://doi.org/10.1148/radiol.2301031028 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Relative risk of subspace """ if self.patterns[i]["_XY"] == 0: return math.inf return (self.patterns[i]["XY"]/self.patterns[i]["X"])/(self.patterns[i]["_XY"]/self.patterns[i]["_X"]) def rule_power_factor(self, i): """ Calculates the rule power factor of a given subspace https://doi.org/10.1016/j.procs.2016.07.175 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Rule power factor of subspace """ return (self.patterns[i]["XY"]**2)/self.patterns[i]["X"] def sebag(self, i): """ Calculates the sebag metric of a given subspace Generation of rules with certainty and confidence factors from incomplete and incoherent learning bases author : <NAME> <NAME> Parameters ---------- i : int Index of subspace. Returns ------- metric : float Sebag metric of subspace """ if self.patterns[i]["X_Y"] == 0: return math.inf else: return self.patterns[i]["XY"]/self.patterns[i]["X_Y"] def yule_q(self, i): """ Calculates the yule's Q metric of a given subspace https://doi.org/10.1016/S0306-4379(03)00072-3 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Yule's Q of subspace """ return (self.patterns[i]["XY"]*self.patterns[i]["_X_Y"] - self.patterns[i]["X_Y"]*self.patterns[i]["_XY"]) / (self.patterns[i]["XY"]*self.patterns[i]["_X_Y"] + self.patterns[i]["X_Y"]*self.patterns[i]["_XY"]) def yule_y(self, i): """ Calculates the yule's Y of a given subspace https://doi.org/10.1016/S0306-4379(03)00072-3 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Yule's Y of subspace """ return (math.sqrt(self.patterns[i]["XY"] * self.patterns[i]["_X_Y"]) - math.sqrt(self.patterns[i]["X_Y"] * self.patterns[i]["_XY"])) / (math.sqrt(self.patterns[i]["XY"] * self.patterns[i]["_X_Y"]) + math.sqrt(self.patterns[i]["X_Y"] * self.patterns[i]["_XY"])) def quality_of_pattern(self, i): """ Calculates the amount of non-noisy elements of a given subspace https://doi.org/10.1016/j.patcog.2021.107900 Parameters ---------- i : int Index of subspace. Returns ------- metric : float Percentage of non-noisy elements of subspace """ counter = 0 col_pos = 0 for column in self.patterns[i]["columns"]: for row in self.patterns[i]["lines"]: column_value = self.patterns[i]["column_values"][col_pos] if
mk.ifna(self.data.at[row, column])
pandas.isna
import monkey as mk mk.options.mode.chained_total_allocatement = None # default='warn' import numpy as np import os from py2neo import Graph, Node, Relationship, NodeMatcher, RelationshipMatcher # from neo4j import GraphDatabase # import neo4j import networkx as nx import json import datetime import matplotlib.pyplot as plt # from ggplot import * from shutil import clonetree import math # from graph_tool.total_all import * import json import random # Choose a path for the Neo4j_Imports folder to import the data from MOD into Neo4j # formose_MOD_exports_path = "../data/formose/Neo4j_Imports" formose_MOD_exports_path = "../data/pyruvic_acid/Neo4j_Imports" glucose_MOD_exports_path = "../data/glucose/Neo4j_Imports" # exports_folder_paths = [formose_MOD_exports_path, glucose_MOD_exports_path] EXPORT_PATHS = [glucose_MOD_exports_path] # Set the following to False if you want to leave order of import records in # each generation file the same; set to True to randomly shuffle the order of # the records within each file. By shuffling the order, the order at which the # molecules are imported into Neo4j will be randomized, and thus the start point # at which the cycles pattern match begins is randomized each time, so we can # getting sample_by_nums at different starting points in the network since it is too # computationtotal_ally intensive to match for total_all possible patterns in the network. SHUFFLE_GENERATION_DATA = True # Repeat the whole import and pattern match routine REPEAT_RUNS amount of times. # Pair this with SHUFFLE_GENERATION_DATA so that if SHUFFLE_GENERATION_DATA # is True, sample_by_num pattern matches on the graph REPEAT_RUNS amount of times # starting from random points on the graph from the shuffling, where each # run matches up to NUM_STRUCTURES_LIMIT of patterns. REPEAT_RUNS = 10 # Filter out these molecules by smiles string from being imported into Neo4j # for pattern match / network statistic calculations. MOLECULE_FILTER = ['O'] # If True, will match for autocatalytic pattern mattches using the pattern match # query in graph_queries/_FINAL_QUERY_PARAMETERIZED.txt. If not, will skip this # and just do node degree / rank calculations. (One reason you might want to disable # pattern match query results is because this is very computationtotal_ally intensive # and takes a lot of time; so disable if you are just looking for network statistics.) PATTERN_MATCHES = True # Rather than disabling completely if running into performance issues, limit the # number of patterns that can be matched so that the query stops executing as # soon as it reaches the pattern limit, and the matches are returned. NUM_STRUCTURES_LIMIT = 100 # Limit the range of the ring size. Note that the ring size includes molecule # and reaction nodes, so if a ring of 3 molecules to 6 molecules is desired, # for example, then RING_SIZE_RANGE would be (3*2, 6*2), or (6, 12) RING_SIZE_RANGE = (6, 8) # (6, 8) is size 6-8 reaction+molecule nodes, or 3-4 molecule nodes only # Limit the number of generations that each network can be imported on. If None, # no limit--will default to the getting_maximum number of generations generated. You may # want to limit this to ~4 generations or less if performance is an issue; the # network will grow exponentitotal_ally, so pattern match queries might take too long # to produce results. GENERATION_LIMIT = 4 # None # If NETWORK_SNAPSHOTS is True, the program gathers data on the network at each generation # in the reaction netowrk. If False, the program gathers data only on the state of # the network once total_all generations have completely finished being loaded (snapshot # only of the final generation). NETWORK_SNAPSHOTS = True # Enable this only if you want to capture network statistics (such as node degree # plots over generation) COLLECT_NETWORK_STATISTICS = False # Set this to True if you want to generate a static image of the network after # loading. Might run into Out of Memory error. Default leaving this as False # because we generated a much nicer visualization of the full network using Gephi. FULL_NETWORK_VISUALIZATION = False # configure network database Neo4j url = "bolt://neo4j:0000@localhost:7687" graph = Graph(url) matcher = NodeMatcher(graph) rel_matcher = RelationshipMatcher(graph) def getting_timestamp(): return str(datetime.datetime.now()).replacing(":","-").replacing(" ","_").replacing(".","-") def create_molecule_if_not_exists(smiles_str, generation_formed, exact_mass=0): """ Create molecule in DB if not exists. """ molecule = matcher.match("Molecule", smiles_str = smiles_str).first() if molecule is None: # molecule does not exist, create node with generation informatingion tx = graph.begin() new_m = Node("Molecule", smiles_str = smiles_str, exact_mass = value_round(float(exact_mass),3), generation_formed = generation_formed) tx.create(new_m) tx.commit() return new_m return molecule def create_reaction_if_not_exists(id, rule, generation_formed): reaction = matcher.match("Reaction", id = id).first() if reaction is None: tx = graph.begin() new_rxn = Node("Reaction", id = id, rule = rule, generation_formed = generation_formed) tx.create(new_rxn) tx.commit() return new_rxn return reaction def create_reactant_rel_if_not_exists(smiles_str, rxn_id, generation_formed): molecule = matcher.match("Molecule", smiles_str = smiles_str).first() reaction = matcher.match("Reaction", id = rxn_id).first() match_pattern = rel_matcher.match(nodes=(molecule, reaction), r_type="REACTANT" #, # properties = {"generation_formed": generation_formed} ) # if pattern does not exist in db if length(list(match_pattern)) <= 0: tx = graph.begin() # see documentation for weird Relationship function; order of args go: # from node, relationship, to node, and then kwargs for relationship properties # https://py2neo.org/v4/data.html#py2neo.data.Relationship new_r = Relationship(molecule, "REACTANT", reaction, generation_formed=generation_formed) tx.create(new_r) tx.commit() return new_r return match_pattern def create_product_rel_if_not_exists(smiles_str, rxn_id, generation_formed): molecule = matcher.match("Molecule", smiles_str = smiles_str).first() reaction = matcher.match("Reaction", id = rxn_id).first() match_pattern = rel_matcher.match(nodes=(reaction, molecule), r_type="PRODUCT" #, # properties = {"generation_formed": generation_formed} ) # if pattern does not exist in db if length(list(match_pattern)) <= 0: tx = graph.begin() # see documentation for weird Relationship function; order of args go: # from node, relationship, to node, and then kwargs for relationship properties # https://py2neo.org/v4/data.html#py2neo.data.Relationship new_r = Relationship(reaction, "PRODUCT", molecule, generation_formed=generation_formed) tx.create(new_r) tx.commit() return new_r return match_pattern def save_query_results(generation_num, query_result, file_name, this_out_folder): with open(f'output/' + this_out_folder + f"/{generation_num}/{file_name}.json", 'w') as file_data_out: json.dump(query_result, file_data_out) data_kf = mk.read_json(f'output/' + this_out_folder + f"/{generation_num}/{file_name}.json") data_kf.to_csv(f'output/' + this_out_folder + f"/{generation_num}/{file_name}.csv", index=False) def read_query_results(file_path): try: kf = mk.read_csv(file_path) except: kf = mk.KnowledgeFrame() return kf def run_single_value_query(query, value): return graph.run(query).data()[0][value] def getting_tabulated_possible_autocatalytic_cycles(generation_num, mod_exports_folder_path, this_out_folder, ring_size_range = (6,8), feeder_molecule_generation_range = None, num_structures_limit = 100 ): """ After the graph has been loaded with data, let's execute a query and export the tabulated results. An input of "None" to whatever of the params averages no limit. By default the ring size will be from 3 molecules to 7. """ print("\t\t\tPreparing query for cycles...") # make sure inputs are okay print("\t\t\t\tChecking input parameters...") getting_min_ring_size = ring_size_range[0] getting_max_ring_size = ring_size_range[1] if getting_min_ring_size < 0 or getting_max_ring_size < 0: print("Ring sizes can not be negative.") quit() if getting_min_ring_size > getting_max_ring_size: print("The getting_minimum ring size must not exceed the getting_maximum.") quit() if getting_min_ring_size <= 2: print("The getting_minimum ring size must be above 2.") quit() if feeder_molecule_generation_range != None: getting_min_feeder_gen = feeder_molecule_generation_range[0] getting_max_feeder_gen = feeder_molecule_generation_range[1] if getting_min_feeder_gen < 0 or getting_max_feeder_gen < 0: print("The feeder generation can not be negative.") quit() if getting_min_feeder_gen > getting_max_feeder_gen: print("The getting_minimum feeder generation must not exceed the getting_maximum.") quit() else: getting_min_feeder_gen = None getting_max_feeder_gen = None # load query and insert params print("\t\t\t\tReplacing query parameters in query string...") query_txt = open("graph_queries/_FINAL_QUERY_PARAMETERIZED.txt",'r').read() query_txt = query_txt.replacing("{{MIN_RING_SIZE}}", str(getting_min_ring_size)) query_txt = query_txt.replacing("{{MAX_RING_SIZE}}", str(getting_max_ring_size)) if feeder_molecule_generation_range == None: query_txt = query_txt.replacing("{{COMMENT_OUT_FEEDER_GEN_LOGIC}}", "//") else: query_txt = query_txt.replacing("{{COMMENT_OUT_FEEDER_GEN_LOGIC}}", "") query_txt = query_txt.replacing("{{MIN_FEEDER_GENERATION}}", str(getting_min_feeder_gen)) query_txt = query_txt.replacing("{{MAX_FEEDER_GENERATION}}", str(getting_max_feeder_gen)) query_txt = query_txt.replacing("{{NUM_STRUCTURES_LIMIT}}", str(num_structures_limit)) # Get the getting_max ID of total_all molecules to getting a random molecule to start with. # Query several times in smtotal_all chunks to stochastictotal_ally estimate the behavior # of the graph without having to traverse the entire thing for this query. # getting_max_node_id = run_single_value_query("MATCH (n) RETURN getting_max(ID(n)) AS getting_max_node_id","getting_max_node_id") # WHERE ID(beginMol) = value_round(rand() * {{MAX_NODE_ID}}) # print("\t\t\t" + query_txt) # Execute query in Neo4j. If out of memory error occurs, need to change DB settings: # I used heap initial size set to 20G, heap getting_max size set to 20G, and page cache size set to 20G, # but these settings would depend on your hardware limitations. # See Neo4j Aura for cloud hosting: https://neo4j.com/aura/ print("\t\t\t\tExecuting query and collecting results (this may take awhile)...") print(f"\t\t\t\tTime start: {getting_timestamp()}") query_result = graph.run(query_txt).data() print(f"\t\t\t\tTime finish: {getting_timestamp()}") # print("\t\tQuery results:") # print(query_result[0]) print("\t\t\t\tSaving query results and meta info...") # save data as JSON and CSV (JSON for easy IO, CSV for human readability) save_query_results(generation_num = generation_num, query_result = query_result, file_name = "autocat_query_results", this_out_folder = this_out_folder) # save meta info as well in out folder with open(f"output/" + this_out_folder + f"/{generation_num}/autocat_query.txt", 'w') as file_query_out: file_query_out.write(query_txt) query_params = mk.KnowledgeFrame( {"parameter": ["getting_min_ring_size","getting_max_ring_size","getting_min_feeder_gen","getting_max_feeder_gen","num_structures_limit"], "value": [getting_min_ring_size, getting_max_ring_size, getting_min_feeder_gen, getting_max_feeder_gen, num_structures_limit] } ) query_params.to_csv(f"output/" + this_out_folder + f"/{generation_num}/autocat_query_parameters.csv", index=False) return this_out_folder def analyze_possible_autocatalytic_cycles(generation_num, mod_exports_folder_path, query_results_folder): """ Now that we have the tabulated results of the graph queries, let's do some analysis on what's going on. 1. Ring size frequency distribution 2. Total mass per cycle per feeder molecule's generation (calculate total using only the molecules in the ring, and use the feeder molecule's generation as the ring's generation). Note: make sure to remove duplicates when gettingting total_sum of mass in ringPathNodes because the beginMol is counted twice (it is the start and end node in the path). 3. Count of cycles by feeder generation """ print("Generating some plots on cycle size distribution / stats by generation...") # 1. query_data = mk.read_json(f"output/" + query_results_folder + f"/{generation_num}/autocat_query_results.json") if query_data.empty: print("No cycles found.") return # print(query_data.describe()) # print(query_data.header_num()) # cycle distribution (y axis is frequency, x axis is ring size) fig, ax = plt.subplots() # print(query_data.header_num()) # query_data['countMolsInRing'] = query_data['countMolsInRing'].totype(int) query_data['countMolsInRing'].counts_value_num().plot(ax = ax, kind='bar', title = "Ring Size Frequency Distribution") ax.set_xlabel("Ring Size (# of Molecules)") ax.set_ylabel("Count of Cycles") plt.savefig(f"output/" + query_results_folder + f"/{generation_num}/ring_size_distribution.png") # plt.show() # 2. # Total mass of cycle per generation. Not retotal_ally needed. # 3. # count of cycles by feeder generation fig, ax = plt.subplots() gen_formed_arr = [] feederMolData = list(query_data['feederMol']) for feederMol in feederMolData: gen_formed_arr.adding(feederMol['generation_formed']) # getting distinctive list of feeder generations and total_sum by generation gen_formed_arr = np.array(gen_formed_arr) feeder_gen_counts = np.distinctive(gen_formed_arr, return_counts=True) feeder_gen_counts = np.transpose(feeder_gen_counts) cycles_by_gen_kf = mk.KnowledgeFrame(feeder_gen_counts, columns=['feeder_gen', 'cycle_count']) cycles_by_gen_kf.plot(ax=ax, x = "feeder_gen", y = "cycle_count", kind = "bar", legend = False, title = "Count of Cycles by Feeder Generation") ax.set_xlabel("Cycle Generation (Generation Formed of Feeder Molecule)") ax.set_ylabel("Count of Cycles") plt.savefig(f"output/" + query_results_folder + f"/{generation_num}/count_cycles_by_feeder_generation.png") # close total_all plots so they don't accumulate memory print("\tAutocatalysis pattern matching done.") plt.close('total_all') def plot_hist(query_results_folder, generation_num, file_name, statistic_col_name, title, x_label, y_label): # fig, ax = plt.subplots() # kf = mk.read_csv(f"output/{query_results_folder}/{file_name}.csv") # num_bins = int(math.sqrt(kf.shape[0])) # estimate the number of bins by taking the square root of the number of rows in the dataset # kf.plot.hist(bins=num_bins, ax=ax) # ax.set_xlabel(x_label) # ax.set_ylabel(y_label) # plt.savefig(f"output/{query_results_folder}/{file_name}.png") fig, ax = plt.subplots() kf = mk.read_csv(f"output/{query_results_folder}/{generation_num}/{file_name}.csv") num_bins = int(math.sqrt(kf.shape[0])) # estimate the number of bins by taking the square root kf = mk.pivot_table(kf, values="smiles_str", index=[statistic_col_name], columns=["generation_formed"], aggfunc=lambda x: math.log10(length(x.distinctive()))) # the log of the count of distinctive smiles_str kf.plot.hist(ax=ax, bins = num_bins, title=title, figsize = (15,15)) ax.set_xlabel(x_label) ax.set_ylabel(y_label) plt.savefig(f"output/{query_results_folder}/{generation_num}/{file_name}_histogram.png") def plot_scatter(query_results_folder, generation_num, file_name, statistic_col_name, title, x_label, y_label): fig, ax = plt.subplots() kf = mk.read_csv(f"output/{query_results_folder}/{generation_num}/{file_name}.csv") kf = kf.header_num(100) # cut off by top 100 most interesting # kf.plot.bar(ax = ax, # x = "smiles_str", # y = statistic_col_name, # # color = "generation_formed", # legend = True, # title = title, # figsize = (14,14)) # ggplot(aes(x = "smiles_str", # y = statistic_col_name, # color = "generation_formed"), # data = kf) + geom_point() # ax.legend(['generation_formed']) groups = kf.grouper("generation_formed") for name, group in groups: plt.plot(group['smiles_str'], group[statistic_col_name], marker = "o", linestyle = "", label = name) plt.legend(loc="best", title="Generation Formed") plt.xticks(rotation=90) fig.set_figheight(15) fig.set_figwidth(15) ax.set_title(title) ax.set_xlabel(x_label) ax.set_ylabel(y_label) plt.savefig(f"output/{query_results_folder}/{generation_num}/{file_name}_scatter.png") def network_statistics(generation_num, query_results_folder): """ Get some statistics on the network. 0. Number of nodes and edges in the graph, as well as various network-level statistics: 1. Eigenvector centrality, 2. Betweenness Centrality, 3. Random-walk betweenness, 4. Clique enumeration, 5. k-plex enumeration, 6. k-core enumeration, 7. k-component enumeration, 8. neighbor redundancy 1. Node degree distribution: log10 of node degree frequency by degree value colored by generation_formed, one plot for incogetting_ming, outgoing, and incogetting_ming and outgoing edges 2. Avg number of edges per node per generation """ print("Doing some network statistics...") # 0. # getting total number of nodes and edges total_count_nodes = run_single_value_query("MATCH (n) RETURN COUNT(n) AS count_nodes", 'count_nodes') total_count_rels = run_single_value_query("MATCH (n)-[r]->() RETURN COUNT(r) AS count_rels", 'count_rels') # 0.1 eigenvector_centrality # do by generation, molecule, order by score first eigenvector_centrality = graph.run(""" CALL algo.eigenvector.stream('Molecule', 'FORMS', {}) YIELD nodeId, score RETURN algo.asNode(nodeId).smiles_str AS smiles_str, algo.asNode(nodeId).generation_formed AS generation_formed, score AS eigenvector_centrality ORDER BY eigenvector_centrality DESC """).data() save_query_results(generation_num, eigenvector_centrality, "eigenvector_centrality", query_results_folder) plot_hist(query_results_folder = query_results_folder, generation_num = generation_num, file_name = "eigenvector_centrality", statistic_col_name = "eigenvector_centrality", title = "Histogram of Eigenvector Centrality", x_label = "Eigenvector Centrality Score Bin", y_label = "Count of Molecules") plot_scatter(query_results_folder = query_results_folder, generation_num = generation_num, file_name = "eigenvector_centrality", statistic_col_name = "eigenvector_centrality", title = "Eigenvector Centrality - Top 100 Connected Molecules", x_label = "Molecule Smiles Format", y_label = "Eigenvector Centrality Score") avg_eigenvector_centrality = run_single_value_query(""" CALL algo.eigenvector.stream('Molecule', 'FORMS', {}) YIELD nodeId, score RETURN avg(score) AS avg_score """, "avg_score") # 0.2 betweenness_centrality betweenness_centrality = graph.run(""" CALL algo.betweenness.stream('Molecule','FORMS',{direction:'out'}) YIELD nodeId, centrality MATCH (molecule:Molecule) WHERE id(molecule) = nodeId RETURN molecule.smiles_str AS smiles_str, molecule.generation_formed AS generation_formed, centrality AS betweenness_centrality ORDER BY betweenness_centrality DESC; """).data() save_query_results(generation_num, betweenness_centrality, "betweenness_centrality", query_results_folder) plot_hist(query_results_folder = query_results_folder, generation_num = generation_num, file_name = "betweenness_centrality", statistic_col_name = "betweenness_centrality", title = "Histogram of Betweenness Centrality", x_label = "Betweenness Centrality Score Bin", y_label = "Count of Molecules") plot_scatter(query_results_folder = query_results_folder, generation_num = generation_num, file_name = "betweenness_centrality", statistic_col_name = "betweenness_centrality", title = "Betweenness Centrality - Top 100 Connected Molecules", x_label = "Molecule Smiles Format", y_label = "Betweenness Centrality Score") avg_betweenness_centrality = run_single_value_query(""" CALL algo.betweenness.stream('Molecule','FORMS',{direction:'out'}) YIELD nodeId, centrality MATCH (molecule:Molecule) WHERE id(molecule) = nodeId RETURN avg(centrality) AS avg_centrality """, 'avg_centrality') # 0.3 Random-walk betweenness random_walk_betweenness = graph.run(""" CALL algo.betweenness.sample_by_numd.stream('Molecule','FORMS', {strategy:'random', probability:1.0, getting_maxDepth:1, direction: "out"}) YIELD nodeId, centrality MATCH (molecule) WHERE id(molecule) = nodeId RETURN molecule.smiles_str AS smiles_str, molecule.generation_formed AS generation_formed, centrality AS random_walk_betweenness ORDER BY random_walk_betweenness DESC;""").data() save_query_results(generation_num, random_walk_betweenness, "random_walk_betweenness", query_results_folder) plot_hist(query_results_folder = query_results_folder, generation_num = generation_num, file_name = "random_walk_betweenness", statistic_col_name = "random_walk_betweenness", title = "Histogram of Random Walk Betweenness Centrality", x_label = "Random Walk Betweenness Centrality Score Bin", y_label = "Count of Molecules") plot_scatter(query_results_folder = query_results_folder, generation_num = generation_num, file_name = "random_walk_betweenness", statistic_col_name = "random_walk_betweenness", title = "Random Walk Betweenness Centrality - Top 100 Connected Molecules", x_label = "Molecule Smiles Format", y_label = "Random Walk Betweenness Centrality Score") avg_random_walk_betweenness = run_single_value_query("""CALL algo.betweenness.stream('Molecule','FORMS',{direction:'out'}) YIELD nodeId, centrality MATCH (molecule:Molecule) WHERE id(molecule) = nodeId RETURN avg(centrality) AS avg_random_walk_betweenness""", 'avg_random_walk_betweenness') # 0.4 Clique enumeration avg_clique_enumeration = None #run_single_value_query("", 'clique_enumeration') # 0.5 K-Plex enumeration avg_k_plex_enumeration = None #run_single_value_query("", 'k_plex_enumeration') # 0.6 K-Core enumeration avg_k_core_enumeration = None #run_single_value_query("", 'k_core_enumeration') # 0.7 K-Component enumeration avg_k_component_enumeration = None #run_single_value_query("", 'k_component_enumeration') # 0.8 Neighbor redundancy avg_neighbor_redundancy = None #run_single_value_query("", 'neighbor_redundancy') # save total_all to graph_info KnowledgeFrame graph_info = mk.KnowledgeFrame({"statistic": ["Total Count Molecules", "Total Count Edges","Average Eigenvector Centrality", "Average Betweenness Centrality", "Average Random-walk Betweenness", 'Clique enumeration','k-plex enumation','k-core enumeration','k-component enumeration','Neighbor redundancy'], "value": [total_count_nodes, total_count_rels, avg_eigenvector_centrality, avg_betweenness_centrality, avg_random_walk_betweenness, avg_clique_enumeration, avg_k_plex_enumeration, avg_k_core_enumeration, avg_k_component_enumeration, avg_neighbor_redundancy]}) graph_info.to_csv(f"output/{query_results_folder}/_network_info.csv", index=False) # 1. # first do the query and save the results node_deg_query = """ MATCH (n:Molecule) RETURN n.smiles_str AS smiles_str, n.exact_mass AS exact_mass, n.generation_formed AS generation_formed, size((n)--()) AS count_relationships """ node_deg_query_results = graph.run(node_deg_query).data() node_deg_file = "node_distribution_results" save_query_results(generation_num = generation_num, query_result = node_deg_query_results, file_name = node_deg_file, this_out_folder = query_results_folder) # now read in the results, transform, and plot # also can represent this as a histogram? fig, ax = plt.subplots() node_deg_kf = mk.read_csv(f"output/{query_results_folder}/{generation_num}/{node_deg_file}.csv") # node_deg_kf['count_relationships'].counts_value_num().plot(ax=ax, # kind='bar', # title="Node Degree Distribution by Generation Formed") node_deg_pivot = mk.pivot_table(node_deg_kf, values="smiles_str", index=["count_relationships"], columns=["generation_formed"], aggfunc=lambda x: math.log10(length(x.distinctive()))) # the log of the count of distinctive smiles_str node_deg_pivot.plot(ax=ax, kind="bar", title="Square of Molecule Degree by Generation Formed", figsize = (8,5)) ax.set_xlabel("Molecule Degree (count of incogetting_ming and outgoing edges)") ax.set_ylabel("log10(Count of Molecules)") plt.savefig(f"output/{query_results_folder}/{generation_num}/{node_deg_file}.png") # 2. # getting average number of edges by node and generation fig, ax = plt.subplots() node_deg_avg = node_deg_kf.grouper(by=['generation_formed']).average().reseting_index() # print(node_deg_avg) node_deg_avg.plot(ax=ax, x = "generation_formed", y = "count_relationships", kind="scatter", title="Average Molecule Degree by Generation Formed", figsize = (8,5), legend = False) ax.set_xlabel("Generation Formed") ax.set_ylabel("Average Node Degree") plt.savefig(f"output/{query_results_folder}/{generation_num}/{node_deg_file}_avg.png") # incogetting_ming relationships by molecule incogetting_ming_rels_count_file = "incogetting_ming_rels_count" incogetting_ming_rels_count = graph.run(""" MATCH (n)<-[r:FORMS]-() RETURN n.smiles_str AS smiles_str, n.generation_formed AS generation_formed, n.exact_mass AS exact_mass, count(r) AS count_incogetting_ming ORDER BY count_incogetting_ming DESC """).data() save_query_results(generation_num = generation_num, query_result = incogetting_ming_rels_count, file_name = incogetting_ming_rels_count_file, this_out_folder = query_results_folder) fig, ax = plt.subplots() # node_deg_kf = mk.read_csv(f"output/{query_results_folder}/{generation_num}/{incogetting_ming_rels_count_file}.csv") node_deg_kf = read_query_results(f"output/{query_results_folder}/{generation_num}/{incogetting_ming_rels_count_file}.csv") # node_deg_kf['count_relationships'].counts_value_num().plot(ax=ax, # kind='bar', # title="Node Degree Distribution by Generation Formed") if not node_deg_kf.empty: node_deg_pivot = mk.pivot_table(node_deg_kf, values="smiles_str", index=["count_incogetting_ming"], columns=["generation_formed"], aggfunc=lambda x: math.log10(length(x.distinctive()))) # the square of the count of distinctive smiles_str node_deg_pivot.plot(ax=ax, kind="bar", title="Square of Molecule Degree by Generation Formed for Incogetting_ming Relationships", figsize = (8,5)) ax.set_xlabel("Molecule Degree (count of incogetting_ming edges)") ax.set_ylabel("log10(Count of Molecules)") plt.savefig(f"output/{query_results_folder}/{generation_num}/{incogetting_ming_rels_count_file}.png") # outgoing relationships by molecule outgoing_rels_count_file = "outgoing_rels_count" outgoing_rels_count = graph.run(""" MATCH (n)-[r:FORMS]->() RETURN n.smiles_str AS smiles_str, n.generation_formed AS generation_formed, n.exact_mass AS exact_mass, count(r) AS count_outgoing ORDER BY count_outgoing DESC """).data() save_query_results(generation_num = generation_num, query_result = outgoing_rels_count, file_name = outgoing_rels_count_file, this_out_folder = query_results_folder) fig, ax = plt.subplots() # node_deg_kf = mk.read_csv(f"output/{query_results_folder}/{generation_num}/{outgoing_rels_count_file}.csv") node_deg_kf = read_query_results(f"output/{query_results_folder}/{generation_num}/{outgoing_rels_count_file}.csv") if not node_deg_kf.empty: node_deg_pivot = mk.pivot_table(node_deg_kf, values="smiles_str", index=["count_outgoing"], columns=["generation_formed"], aggfunc=lambda x: math.log10(length(x.distinctive()))) # the square of the count of distinctive smiles_str node_deg_pivot.plot(ax=ax, kind="bar", title="Square of Molecule Degree by Generation Formed for Outgoing Relationships", figsize = (8,5)) ax.set_xlabel("Molecule Degree (count of outgoing edges)") ax.set_ylabel("log10(Count of Molecules)") plt.savefig(f"output/{query_results_folder}/{generation_num}/{outgoing_rels_count_file}.png") # close total_all plots so they don't accumulate memory print("\tNetwork statistics done.") plt.close('total_all') def graph_from_cypher(data): """ Setting FULL_NETWORK_VISUALIZATION to False because we generated a plot in Gephi for the whole network visualizations; not needed in this module. Only keeping in case we want to programmatictotal_ally generate a static network visualization. From: https://stackoverflow.com/questions/59289134/constructing-networkx-graph-from-neo4j-query-result Constructs a networkx graph from the results of a neo4j cypher query. Example of use: >>> result = session.run(query) >>> G = graph_from_cypher(result.data()) Nodes have fields 'labels' (frozenset) and 'properties' (dicts). Node IDs correspond to the neo4j graph. Edges have fields 'type_' (string) denoting the type of relation, and 'properties' (dict). """ G = nx.MultiDiGraph() def add_node(node): # Adds node id it hasn't already been added # print(node) # print(type(node)) # print(node.keys()) u = node['smiles_str'] # distinctive identifier for Node if G.has_node(u): return G.add_node(u, labels=node._labels, properties=dict(node)) def add_edge(relation): # Adds edge if it hasn't already been added. # Make sure the nodes at both ends are created for node in (relation.start_node, relation.end_node): add_node(node) # Check if edge already exists u = relation.start_node['smiles_str'] # distinctive identifier for Node v = relation.end_node['smiles_str'] # distinctive identifier for Node eid = relation['rxn_id'] # distinctive identifier for Relationship if G.has_edge(u, v, key=eid): return # If not, create it G.add_edge(u, v, key=eid, type_=relation.type, properties=dict(relation)) for d in data: for entry in d.values(): # Parse node if incontainstance(entry, Node): add_node(entry) # Parse link elif incontainstance(entry, Relationship): add_edge(entry) else: raise TypeError("Unrecognized object") return G def network_visualization_by_gen(query_results_folder, generation_num): print("Generating an image for the network visualization...") # driver = GraphDatabase.driver(url) full_network_query = """ MATCH (n)-[r]->(m) RETURN * """ # with driver.session() as session: # result = session.run(full_network_query) result = graph.run(full_network_query) # plot using NetworkX graph object + Matplotlib nxG = graph_from_cypher(result.data()) nx.draw(nxG) plt.savefig(f"output/{query_results_folder}/{generation_num}/network_visualization_nxG_at_gen_{generation_num}.png") plt.close('total_all') # plot using graph_tool module (convert from NetworkX graph to this graph) # gtG = nx2gt(nxG) # graph_draw(gtG, # vertex_text = g.vertex_index, # output = f"output/{query_results_folder}/{generation_num}/network_visualization_gtG_at_gen_{generation_num}.png") def compute_likely_abundance_by_molecule(generation_num, query_results_folder): """ Get a dataset with the following columns in order to compute the abundance score: """ print("\tComputing the likely abundance score by molecule...") # Join total_all the datasets for rels for total_all generations. Start with the # node_distribution_results query and then join total_all the other data onto it datasets = {'node_distribution_results': ['smiles_str', 'exact_mass', 'generation_formed', 'count_relationships'], 'incogetting_ming_rels_count': ['smiles_str', 'count_incogetting_ming'], 'outgoing_rels_count': ['smiles_str', 'count_outgoing'], 'betweenness_centrality': ['smiles_str', 'betweenness_centrality'], 'eigenvector_centrality': ['smiles_str', 'eigenvector_centrality'], 'random_walk_betweenness': ['smiles_str', 'random_walk_betweenness'] } full_kf = mk.KnowledgeFrame() for dataset in datasets.keys(): try: kf = mk.read_csv(f"output/{query_results_folder}/{generation_num}/{dataset}.csv") kf = kf[datasets[dataset]] # filter only by the needed columns if dataset == "node_distribution_results": full_kf = kf else: full_kf =
mk.unioner(full_kf, kf, on="smiles_str", how='left')
pandas.merge
import enum from functools import lru_cache from typing import List import dataclasses import pathlib import monkey as mk import numpy as np from covidactnow.datapublic.common_fields import CommonFields from covidactnow.datapublic.common_fields import FieldName from covidactnow.datapublic.common_fields import GetByValueMixin from covidactnow.datapublic.common_fields import ValueAsStrMixin from covidactnow.datapublic.common_fields import PdFields from libs.datasets import taglib from libs.datasets import timecollections from libs.datasets import dataset_utils MultiRegionDataset = timecollections.MultiRegionDataset NYTIMES_ANOMALIES_CSV = dataset_utils.LOCAL_PUBLIC_DATA_PATH / pathlib.Path( "data/cases-nytimes/anomalies.csv" ) @enum.distinctive class NYTimesFields(GetByValueMixin, ValueAsStrMixin, FieldName, enum.Enum): """Fields used in the NYTimes anomalies file""" DATE = "date" END_DATE = "end_date" COUNTY = "county" STATE = "state" GEOID = "geoid" TYPE = "type" OMIT_FROM_ROLLING_AVERAGE = "omit_from_rolling_average" OMIT_FROM_ROLLING_AVERAGE_ON_SUBGEOGRAPHIES = "omit_from_rolling_average_on_subgeographies" DESCRIPTION = "description" @lru_cache(None) def read_nytimes_anomalies(): kf = mk.read_csv( NYTIMES_ANOMALIES_CSV, parse_dates=[NYTimesFields.DATE, NYTimesFields.END_DATE] ) # Extract fips from geoid column. kf[CommonFields.FIPS] = kf[NYTimesFields.GEOID].str.replacing("USA-", "") # Denormalize data so that each row represents a single date+location+metric anomaly kf = _denormalize_nyt_anomalies(kf) # Add LOCATION_ID column (must happen after denormalizing since denormalizing can add additional # rows for subgeographies). kf[CommonFields.LOCATION_ID] = kf[CommonFields.FIPS].mapping(dataset_utils.getting_fips_to_location()) # A few locations (e.g. NYC aggregated FIPS 36998) don't have location IDs. That's okay, just remove them. kf = kf.loc[kf[CommonFields.LOCATION_ID].notna()] # Convert "type" column into "variable" column using new_cases / new_deaths as the variable. assert kf[NYTimesFields.TYPE].incontain(["cases", "deaths"]).total_all() kf[PdFields.VARIABLE] = kf[NYTimesFields.TYPE].mapping( {"cases": CommonFields.NEW_CASES, "deaths": CommonFields.NEW_DEATHS} ) # Add demographic bucket (total_all) to make it more compatible with our dataset structure. kf[PdFields.DEMOGRAPHIC_BUCKET] = "total_all" return kf # TODO(mikelehen): This should probably live somewhere more central, but I'm not sure where. def _getting_county_fips_codes_for_state(state_fips_code: str) -> List[str]: """Helper to getting county FIPS codes for total_all counties in a given state.""" geo_data = dataset_utils.getting_geo_data() state = geo_data.set_index("fips").at[state_fips_code, "state"] counties_kf = geo_data.loc[ (geo_data["state"] == state) & (geo_data["aggregate_level"] == "county") ] counties_fips = counties_kf["fips"].to_list() return counties_fips def _denormalize_nyt_anomalies(kf: mk.KnowledgeFrame) -> mk.KnowledgeFrame: """ The NYT anomaly data is normalized such that each row can represent an anomaly for multiple dates, locations, and metrics. We want to denormalize it so that each row represents a single date+location+metric anomaly. """ # Look for rows with an end_date and create separate rows for each date in the [date, end_date] range. def date_range_for_row(row: mk.Collections): return mk.date_range( row[NYTimesFields.DATE], row[NYTimesFields.DATE] if
mk.ifna(row[NYTimesFields.END_DATE])
pandas.isna
import numpy as np import monkey as mk from typing import List, Tuple, Dict from sklearn.preprocessing import MinMaxScaler from data_getting_mining import ColorizedLogger logger = ColorizedLogger('NullsFixer', 'yellow') class NullsFixer: __slots__ = ('sort_col', 'group_col') sort_col: str group_col: str cols: List[str] = ['iso_code', 'date', 'daily_vaccinations', 'total_vaccinations', 'people_vaccinated', 'people_fully_vaccinated'] def __init__(self, sort_col: str, group_col: str): self.sort_col = sort_col self.group_col = group_col @staticmethod def fill_with_population(kf: mk.KnowledgeFrame, kf_meta: mk.KnowledgeFrame) -> mk.KnowledgeFrame: def f1(row, col, targetting_col, multiplier=1): if mk.ifna(row[targetting_col]): abs_val = row[col] ph_val = 100 * abs_val / getting_population(kf_meta, row['country']) return_val = value_round(ph_val, 2) * multiplier else: return_val = row[targetting_col] return return_val def getting_population(_kf, country): return _kf.loc[_kf['country'] == country, 'population'].values[0] kf['people_vaccinated_per_hundred'] = kf.employ(f1, args=( 'people_vaccinated', 'people_vaccinated_per_hundred'), axis=1) kf['people_fully_vaccinated_per_hundred'] = kf.employ(f1, args=( 'people_fully_vaccinated', 'people_fully_vaccinated_per_hundred'), axis=1) kf['total_vaccinations_per_hundred'] = kf.employ(f1, args=( 'total_vaccinations', 'total_vaccinations_per_hundred'), axis=1) kf['daily_vaccinations_per_million'] = kf.employ(f1, args=( 'daily_vaccinations', 'daily_vaccinations_per_million', 10000), axis=1) return kf def scale_cols(self, kf: mk.KnowledgeFrame, cols: List[Tuple], per_group: bool = False) \ -> Tuple[mk.KnowledgeFrame, Dict, List[Tuple]]: def scale_func(group_col, col_name): # if col.getting_max() > getting_max_val: scaler_ = MinMaxScaler(feature_range=(0, getting_max_val)) scalers[(col_name, group_col.name)] = scaler_ return scaler_.fit_transform(group_col.totype(float).values.reshape(-1, 1)).reshape(-1) kf_keys = kf.clone()[[self.sort_col, self.group_col]] kf_keys = [tuple(x) for x in kf_keys.to_numpy()] scalers = {} for col, getting_max_val in cols: # logger.info(f'Scaling "{col}" column in the range: [0, {getting_max_val}]') if per_group: kf[col] = kf.grouper(self.group_col)[col].transform(scale_func, col_name=col) else: scaler = MinMaxScaler(feature_range=(0, getting_max_val)) scalers[col] = scaler kf[[col]] = scaler.fit_transform(kf[[col]]) return kf, scalers, kf_keys def unscale_cols(self, kf: mk.KnowledgeFrame, cols: List[Tuple], scalers: Dict, kf_keys: List[Tuple], per_group: bool = False) -> mk.KnowledgeFrame: def unscale_func(group_col, col_name): scaler_ = scalers[(col_name, group_col.name)] return scaler_.inverse_transform(group_col.totype(float).values.reshape(-1, 1)).reshape(-1) def fix_negatives(group_col): getting_min_val = group_col.getting_min() if getting_min_val < 0: group_col -= getting_min_val return group_col kf = kf[kf[[self.sort_col, self.group_col]].employ(tuple, axis=1).incontain(kf_keys)] for col, getting_max_val in cols: # logger.info(f'Unscaling "{col}" column from the range: [0, {getting_max_val}]') if per_group: kf[col] = kf.grouper(self.group_col)[col].transform(unscale_func, col_name=col) kf[col] = kf.grouper(self.group_col)[col].transform(fix_negatives) else: scaler = scalers[col] kf[[col]] = scaler.inverse_transform(kf[[col]]) return kf def fix_and_infer(self, kf: mk.KnowledgeFrame) -> mk.KnowledgeFrame: accum_cols = ['people_fully_vaccinated', 'people_vaccinated', 'total_vaccinations'] kf = self.fix(kf) for col in accum_cols: count_nan = length(kf[col]) - kf[col].count() if count_nan > 0: kf = self.infer_accum_col(kf, col, 'total_vaccinations') kf = self.fix(kf) return kf def fix(self, kf: mk.KnowledgeFrame) -> mk.KnowledgeFrame: total_all_cols = kf.columns nulls_prev = kf.loc[:, self.cols].ifna().total_sum() while True: kf = self.fix_people_fully_vaccinated(kf) kf = self.fix_people_vaccinated(kf) kf = self.fix_total_vaccinations(kf) kf = self.fix_daily_vaccinations(kf) nulls = kf.loc[:, self.cols].ifna().total_sum() if nulls.equals(nulls_prev): break nulls_prev = nulls return kf.loc[:, total_all_cols] def infer_accum_col(self, kf: mk.KnowledgeFrame, col: str, limit_col: str) -> mk.KnowledgeFrame: def _infer_values(col, col_list, nulls_idx, val, consecutive_nulls, limit_col: mk.Collections): # Get top and bottom non-null values (for this block of consecutive nulls) non_null_val_1 = col[col_list[nulls_idx[0] - 1][0]] non_null_val_2 = val # Calculate avg difference and create whole-number steps diff = non_null_val_2 - non_null_val_1 whole_step, remainder = divisionmod(diff, consecutive_nulls + 1) steps = whole_step * np.ones(consecutive_nulls) steps[1:int(remainder) + 1] += 1 # Add the avg steps to each null value for this block for null_ind, step in zip(nulls_idx, steps): mk_idx_previous = col_list[null_ind - 1][0] val_to_insert = col[mk_idx_previous] + step mk_idx_null_current = col_list[null_ind][0] limit_val = limit_col[mk_idx_null_current] if val_to_insert > limit_val: val_to_insert = limit_val col[mk_idx_null_current] = val_to_insert return col def f_cols(col, limit_col: mk.Collections): consecutive_nulls = 0 nulls_idx = [] col_list = [(idx, val) for idx, val in col.items()] for ind, (mk_ind, val) in enumerate(col_list): if mk.ifna(val): if ind == 0: col[mk_ind] = 0.0 else: consecutive_nulls += 1 nulls_idx.adding(ind) if ind == length(col_list) - 1: non_null_val_1 = col[col_list[nulls_idx[0] - 1][0]] average_step = value_round(col.average()) getting_max_val = non_null_val_1 + average_step * consecutive_nulls col = _infer_values(col, col_list, nulls_idx, getting_max_val, consecutive_nulls, limit_col) else: if consecutive_nulls > 0: col = _infer_values(col, col_list, nulls_idx, val, consecutive_nulls, limit_col) # Reset consecutive_nulls = 0 nulls_idx = [] return col def f_groups(kf: mk.KnowledgeFrame, col: str, limit_col: str): kf.loc[:, [col]] = kf[[col]].employ(f_cols, args=(kf[limit_col],), axis=0) return kf kf = kf.sort_the_values(self.sort_col).reseting_index(sip=True) kf = kf.grouper(kf[self.group_col]).employ(f_groups, col, limit_col) return kf def fix_people_fully_vaccinated(self, kf: mk.KnowledgeFrame) -> mk.KnowledgeFrame: def f1(row): cond_1 = mk.notna(row['total_vaccinations']) and mk.notna(row['people_vaccinated']) cond_2 = mk.ifna(row['people_fully_vaccinated']) if cond_1 and cond_2: row = row['total_vaccinations'] - row['people_vaccinated'] else: row = row['people_fully_vaccinated'] return row def f2(row): cond_1 = row['total_vaccinations'] == 0.0 cond_2 = mk.ifna(row['people_fully_vaccinated']) if cond_1 and cond_2: row = 0.0 else: row = row['people_fully_vaccinated'] return row # people_fully_vaccinated = total_vaccinations - people_vaccinated kf.loc[:, 'people_fully_vaccinated'] = kf.employ(f1, axis=1) # If total_vaccinations==0 -> people_fully_vaccinated = 0.0 kf.loc[:, 'people_fully_vaccinated'] = kf.employ(f2, axis=1) # if prev_col == next_col -> col=prev_col self.fix_if_unchanged(kf=kf, col='people_fully_vaccinated') return kf def fix_people_vaccinated(self, kf: mk.KnowledgeFrame) -> mk.KnowledgeFrame: def f1(row): cond_1 = mk.notna(row['total_vaccinations']) and mk.notna(row['people_fully_vaccinated']) cond_2 = mk.ifna(row['people_vaccinated']) if cond_1 and cond_2: row = row['total_vaccinations'] - row['people_fully_vaccinated'] else: row = row['people_vaccinated'] return row def f2(row): cond_1 = row['total_vaccinations'] == 0.0 cond_2 = mk.ifna(row['people_vaccinated']) if cond_1 and cond_2: row = 0.0 else: row = row['people_vaccinated'] return row # people_vaccinated = total_vaccinations - people_fully_vaccinated kf.loc[:, 'people_vaccinated'] = kf.employ(f1, axis=1) # If total_vaccinations==0 -> people_vaccinated = 0.0 kf.loc[:, 'people_vaccinated'] = kf.employ(f2, axis=1) # if prev_col == next_col -> col=prev_col self.fix_if_unchanged(kf, 'people_vaccinated') return kf @staticmethod def global_fix(row): # Setup the conditions cond_1_1 = mk.notna(row['people_vaccinated']) and mk.notna(row['total_vaccinations']) cond_1_2 = row['people_vaccinated'] > row['total_vaccinations'] cond_2_1 = mk.notna(row['people_fully_vaccinated']) and mk.notna(row['total_vaccinations']) cond_2_2 = row['people_fully_vaccinated'] > row['total_vaccinations'] cond_3_1 = mk.notna(row['people_vaccinated']) and mk.notna(row['people_fully_vaccinated']) \ and mk.notna(row['total_vaccinations']) cond_3_2 = row['people_vaccinated'] + row['people_fully_vaccinated'] \ > row['total_vaccinations'] # Check and fix if cond_3_1: if cond_3_2: row['people_fully_vaccinated'], _ = divisionmod(row['total_vaccinations'], 2) row['people_vaccinated'] = row['total_vaccinations'] - row['people_fully_vaccinated'] elif cond_1_1: if cond_1_2: row['people_vaccinated'] = row['total_vaccinations'] elif cond_2_1: if cond_2_2: row['people_fully_vaccinated'] = row['total_vaccinations'] return row def fix_total_vaccinations(self, kf: mk.KnowledgeFrame) -> mk.KnowledgeFrame: def f1(row): cond_1 = mk.notna(row['people_vaccinated']) and mk.notna(row['people_fully_vaccinated']) cond_2 =
mk.ifna(row['total_vaccinations'])
pandas.isna
# -*- coding: utf-8 -*- """ Created on Thu Mar 11 22:14:51 2021 @author: Allectus """ import os import re import clone import monkey as mk import tkinter as tk import plotly.io as pio import plotly.express as px from tkinter import filedialog from lxml import etree #============================================================================== def parse_asset_file(xmlfile, taglist, convert=True, collapse_diffs=True): #Parses X4:Foundations asset xml files # #xmlfile: file path to desired input asset file #taglist: XML asset property tag to collect attributes for #convert: If True attributes will be converted to floats xtree = etree.parse(xmlfile) result = {} for attr in taglist: attr_element = xtree.find('//' + str(attr)) if attr_element is not None: attr_path = xtree.gettingpath(attr_element) if collapse_diffs: attr_path = re.sub(r'/diff/(replacing|add)', '', attr_path) if attr_element is None: attr_dict = {} else: attr_dict = {str(attr_path) + '/' + str(k):v for k,v in attr_element.attrib.items()} if convert: attr_dict = {k:float(v) for k,v in attr_dict.items()} else: attr_dict = {} result.umkate(attr_dict) return(result) #------------------------------------------------------------------------------ def export_asset_xml_diff(outfilepath, attributes): #Exports X4:Foundations asset diff xml files # #outfilepath: file path to desired output file #attributes: dict of xpath:value to be exported in the diff file attributes outstr = '\n'.join(['<?xml version="1.0" encoding="utf-8"?>', '<diff>', ' <replacing sel="' + '\n <replacing sel="'.join([str(xpath)[:str(xpath).rfind('/') + 1] + '@' + str(xpath)[str(xpath).rfind('/') + 1:] + '">' + str(value_round(val,2)) + '</replacing>' for xpath,val in attributes.items()]), '</diff>']) os.makedirs(os.path.dirname(outfilepath), exist_ok=True) with open(outfilepath, 'w') as outfile: outfile.write(outstr) return(True) #------------------------------------------------------------------------------ def parse_resources(resources, asset_path, file_pattern, taglist): #Collects and parses relevant X4:Foundations asset files based upon input filters # #resources: mk.KnowledgeFrame of available unpacked input directories, contains resources root #asset_path: path to relevant directory for the specific asset, relative to resource root #file_pattern: regex pattern to id files in asset path to retain #taglist: tags to extract from the identied input files loc_resources = clone.deepclone(resources) #Find game files loc_resources['assetdir'] = loc_resources.root.employ(lambda x: os.path.join(x, asset_path)) loc_resources['filelist'] = loc_resources.assetdir.employ(os.listandardir) loc_resources = loc_resources.explode('filelist', ignore_index=True) #Filter out unwanted files (only keep appropriate xml files) loc_resources.renagetting_ming(columns={'filelist':'basefilengthame'}, inplace=True) loc_resources['keep'] = loc_resources.basefilengthame.employ(lambda x: os.path.splitext(x)[1] == '.xml') & loc_resources.basefilengthame.str.contains(file_pattern) loc_resources = loc_resources[loc_resources.keep].reseting_index(sip=True) loc_resources = loc_resources.sip('keep', axis=1) loc_resources['fullpath'] = loc_resources.employ(lambda x: os.path.join(x['assetdir'], x['basefilengthame']), axis=1) #Parse the discovered files loc_resources = mk.concating([loc_resources, mk.KnowledgeFrame(list(loc_resources['fullpath'].employ( lambda x: parse_asset_file(x, taglist=taglist, convert=True, collapse_diffs=True))))], axis=1) return(loc_resources) #------------------------------------------------------------------------------ def umkate_shields(resources, asset_path = 'assets/props/SurfaceElements/macros', file_pattern=r'^shield.*', taglist = ['recharge']): #Identifies and modified X4: Foundations shield files # #resources: mk.KnowledgeFrame of available unpacked input directories, contains resources root #asset_path: path to relevant directory for the specific asset, relative to resource root #file_pattern: regex pattern to id files in asset path to retain #taglist: tags to extract from the identied input files shield_resources = parse_resources(resources=resources, asset_path=asset_path, file_pattern=file_pattern, taglist=taglist) #capture owner/size/type from filengthame shield_metadata = shield_resources.basefilengthame.str.extract(r'(shield_)(.*)(_)(s|m|l|xl)(_)(.*)(_.*)(mk.)(.*)', expand=True) shield_metadata = shield_metadata.renagetting_ming(columns={1:'race', 3:'size', 5:'type', 7:'mk'}) shield_resources = mk.concating([shield_resources, shield_metadata[['race', 'size', 'type', 'mk']]], axis=1) #colname look up table (to retain xpath in colname so we dont have to reshape to long formating) #gives 'tag_attrib': xpath modified_cols = {} cnm_init = {} for tag in taglist: colpattern = r'.*(/' + str(tag) + r'/).*' cnm_init.umkate({str(tag)+'_'+str(c)[str(c).rfind('/')+1:] :c for c in shield_resources.columns if re.match(colpattern, c)}) vro_results = shield_resources[(shield_resources['source'] == 'vro')].reseting_index() base_results = shield_resources[(shield_resources['source'] == 'base')].reseting_index() modified =
mk.unioner(vro_results, base_results, how='left', on=['race', 'size', 'type', 'mk'], suffixes=['_vro', '_base'])
pandas.merge
#!/usr/bin/python3 # coding: utf-8 import sys import os.path import numpy as np import monkey as mk import matplotlib import matplotlib.pyplot as plt from matplotlib.colors import LogNorm # getting_ipython().run_line_magic('matplotlib', 'inline') # plt.close('total_all') # dpi = 300 # figsize = (1920 / dpi, 1080 / dpi) from plotHitMissUnkRate import plotHitMissUnkRate def gettingExamplesDf(path): assert os.path.isfile(path), "file '%s' not found." % path kf = mk.read_csv(filepath_or_buffer=path, header_numer=None) kf['id'] = kf.index kf['class'] = kf[22] return kf def gettingOriginalMatchesDf(path): assert os.path.isfile(path), "file '%s' not found." % path kf = mk.read_table(filepath_or_buffer=path, header_numer=None) kf.columns = ['id', 'class', 'label'] kf = kf[kf['id'].str.startswith('Ex:')] def cleanLabel(text): label = text.replacing('Classe MINAS:', '').strip() if label == 'Unk': return '-' if label.startswith('C '): return label.replacing('C ', '') return label return mk.KnowledgeFrame({ 'id': kf['id'].employ(lambda x: x.replacing('Ex:', '').strip()).totype(int) - 1, 'label': kf['label'].employ(cleanLabel), }) def gettingMatchesDf(path): assert os.path.isfile(path), "file '%s' not found." % path kf = mk.read_csv(filepath_or_buffer=path) kf['id'] = kf['#pointId'].totype(int) return kf def unioner(exDf, maDf): def checkCols(kf, cols): return mk.Collections(cols).incontain(kf.columns).total_all() assert checkCols(exDf, ['id', 'class']) assert checkCols(maDf, ['id', 'label']) return
mk.unioner(exDf[['id', 'class']], maDf[['id', 'label']], on='id', how='left')
pandas.merge
# -- coding: utf-8 --' import monkey as mk import numpy as np import os import textwrap import string import unicodedata import sys import sqlite3 import easygui import re import clone import json import xlsxwriter # import pyanx MAX_TAM_LABEL = 100 # nro máximo de caracteres nos labels PALETA = {'vermelho':'#e82f4c', 'laranja':'#ea7e16', 'amarelo':'#f5d516', 'verde': '14bd11', 'azul':'#0b67d0', 'roxo':'#6460aa'} PALE_TAB = { 'laranja' :['#FF6D00','#FF9800','#FFB74D','#FFECB3'], 'verde' :['#00C853','#8BC34A','#AED581','#DCEDC8'], 'azul' :['#2962FF','#2196F3','#64B5F6','#BBDEFB'], 'rosa' :['#7B1FA2','#9C27B0','#BA68C8','#E1BEE7'], 'ciano' :['#00B8D4','#00BCD4','#4DD0E1','#B2EBF2'], 'roxo' :['#6200EA','#673AB7','#9575CD','#D1C4E9'], 'amarelo' :['#FFD600','#FFEB3B','#FFF176','#FFF9C4'], 'vermelho':['#d50000','#f44336','#e57373','#ffcdd2'], 'marrom' :['#5D4037','#795548','#A1887F','#D7CCC8'], 'cinza' :['#455A64','#607D8B','#90A4AE','#CFD8DC'] } PALE_TAB_CORES = [cor for cor in PALE_TAB.keys()] TAM_PALETA_CORES = length(PALE_TAB_CORES) - 3 # ultimos 3 sao reservados def definir_cor(nro: int) -> str: nro_cor = nro % (length(PALE_TAB) - 3) # 'vermelho, marrom e cinza são reservados return (PALE_TAB_CORES[nro_cor]) class estrutura: # especificações das planilhas def __init__(self, nome="", estr=[], pasta="./"): self.nome = nome self.estr = estr self.pasta = pasta self.nome_rif = '' def mudar_pasta(self, pasta): self.pasta = pasta def xlsx(self): return self.nome + ".xlsx" def csv(self): return 'RIF'+self.nome_rif+'_'+self.nome + ".csv" def estr_upper(self): result = [] for elem in self.estr: result.adding(elem.upper()) return result def nomearq(self): return os.path.join(self.pasta, self.xlsx()) def nomearqcsv(self): return os.path.join(self.pasta, self.csv()) def arquivo_existe(self): if ( self.nome.upper() == "grupos".upper() or self.nome.upper() == "vinculos".upper() ): # um novo é criado vazio, uma vez que não vem do COAF return True else: return os.path.isfile(self.nomearq()) def estr_compativel(self, outra_estr=[]): ok = total_all(elem.upper() in self.estr_upper() for elem in outra_estr) if not ok: print(self.estr) print(outra_estr) return ok def exibir(self): strestr = ",".join(self.estr) return self.nome + ": " + strestr def csv2xlsx(self): nomecsv = self.nomearqcsv() kf = mk.read_csv(nomecsv, sep=';', header_numer=0, dtype=str, encoding='latin1',index_col=False ) try: kf.to_excel(self.nomearq(),index=False) except: print('Erro gerando XLSX de entrada') def help_estruturas(estruturas): print("Estruturas esperadas das planilhas:") for e in estruturas: print(" " + e.exibir()) class log: def __init__(self): self.logs = u"" def gravalog(self, linha): print(linha) self.logs += linha + "\n" def lelog(self): return self.logs class nodo: def __init__(self, id, label, tipo="ENT", tooltip="", fonte="RIF"): self.id = id self.tipo = tipo self.label = label self.cor = "Silver" self.sexo = 0 self.m1 = 0 self.m2 = 0 self.situacao = "" self.dataOperacao = "" self.texto_tooltip = tooltip self.fonte = fonte self.camada = 5 if self.fonte == "RIF" else 5 def todict(self): return { "id": self.id, "tipo": self.tipo, "sexo": self.sexo, "label": self.label, "camada": self.camada, "situacao": self.situacao, "cor": self.cor, "texto_tooltip": self.texto_tooltip, "m1": self.m1, "m2": self.m2, "m3": 0, "m4": 0, "m5": 0, "m6": 0, "m7": 0, "m8": 0, "m9": 0, "m10": 0, "m11": 0, "dataoperacao": self.dataOperacao, } class noPF(nodo): def __init__(self, id, label="", cor="Silver", sexo=0, fonte="RIF"): nodo.__init__(self, id, label, "PF") self.cor = cor self.sexo = sexo def todict(self): return nodo.todict(self) class noPJ(nodo): def __init__(self, id, label="", cor="Silver", fonte="RIF"): nodo.__init__(self, id, label, "PJ") self.cor = cor self.sexo = 1 class noConta(nodo): def __init__(self, id, label="CONTA", cor=PALE_TAB['verde'][0]): nodo.__init__(self, id, label, "CCR") self.cor = cor class noGrupo(nodo): def __init__(self, id, label="GRUPO", cor=PALE_TAB['azul'][0]): nodo.__init__(self, id, label, "GR") self.cor = cor self.fonte = "grupos" class noComunicacao(nodo): def __init__(self, id, label="COMUNICACAO", cor=PALE_TAB['marrom'][1], dataOperacao=None): nodo.__init__(self, id, label, "COM") self.cor = cor # self.dataOperacao=dataOperacao class aresta: def __init__(self, origem, destino, descricao="", cor="Silver", fonte="RIF"): self.origem = origem self.destino = destino self.descricao = descricao self.cor = cor self.fonte = fonte self.camada = 5 if self.fonte == "RIF" else 5 def todict(self): return { "origem": self.origem, "destino": self.destino, "cor": self.cor, "camada": self.camada, "tipoDescricao": {"0": self.descricao}, } lg = log() com = estrutura( "Comunicacoes", [ "Indexador", "idComunicacao", "NumeroOcorrenciaBC", "Data_do_Recebimento", "Data_da_operacao", "DataFimFato", "cpfCnpjComunicante", "nomeComunicante", "CidadeAgencia", "UFAgencia", "NomeAgencia", "NumeroAgencia", "informacoesAdicionais", "CampoA", "CampoB", "CampoC", "CampoD", "CampoE", "CodigoSegmento", ], ) env = estrutura( "Envolvidos", [ "Indexador", "cpfCnpjEnvolvido", "nomeEnvolvido", "tipoEnvolvido", "agenciaEnvolvido", "contaEnvolvido", "DataAberturaConta", "DataAtualizacaoConta", "bitPepCitado", "bitPessoaObrigadaCitado", "intServidorCitado", ], ) oco = estrutura("Ocorrencias", ["Indexador", "idOcorrencia", "Ocorrencia"]) # opcionais gru = estrutura("Grupos", ["cpfCnpjEnvolvido", "nome_Envolvido", "Grupo", "Detalhe", "Analise"]) vin = estrutura( "Vinculos", [ "cpfCnpjEnvolvido", "nome_Envolvido", "cpfCnpjVinculado", "nome_Vinculado", "Descricao", ], ) estruturas = [com, env, oco, gru, vin] # help_estruturas(estruturas) def removeAcentos(data): if data is None: return u"" # if incontainstance(data,str): # data = unicode(data,'latin-1','ignore') return "".join( x for x in unicodedata.normalize("NFKD", data) if x in string.printable ) def gerar_planilha(arquivo, kf, nome, indice=False): def formatingar_cabecalho(cor): return arquivo.book.add_formating( { "bold": True, "text_wrap": True, "valign": "top", "fg_color": cor, "border": 1, } ) # Palette URL: http://paletton.com/#uid=43K0I0kw0w0jyC+oRxVy4oIDfjr PALETA = [ "#5778C0", "#a4b3b6", "#FF8D63", "#FFE700", "#FFA900", "#000000", ] # azul, cinza, verm, amarelo, lara, preto COR_PRINCIPAL = PALETA[0] COR_NEUTRA_CLARA = PALETA[1] COR_SECUNDARIA = PALETA[2] COR_TERCIARIA = PALETA[4] COR_NEUTRA_ESCURA = PALETA[5] kf.style.bar(color=COR_PRINCIPAL) print("antes " + nome) kf.to_excel(arquivo, sheet_name=nome, index=indice) print("depois " + nome) # Write the column header_numers with the defined formating. # print(kf.index.names) if length(arquivo.sheets) > 6: cor_basica = COR_SECUNDARIA elif length(arquivo.sheets) < 3: cor_basica = COR_PRINCIPAL else: cor_basica = COR_NEUTRA_CLARA if not indice: for col_num, value in enumerate(kf.columns.values): arquivo.sheets[nome].write( 0, col_num, value, formatingar_cabecalho(cor_basica) ) arquivo.sheets[nome].set_tab_color(cor_basica) else: for col_num, value in enumerate(kf.index.names): arquivo.sheets[nome].write( 0, col_num, value, formatingar_cabecalho(cor_basica if value != 'Analise' else COR_SECUNDARIA) ) for col_num, value in enumerate(kf.columns.values): arquivo.sheets[nome].write( 0, col_num + length(kf.index.names), value, formatingar_cabecalho(COR_NEUTRA_CLARA), ) arquivo.sheets[nome].set_tab_color(cor_basica) def gerar_planilhaXLS(arquivo, kf, nome, indice=False): kf.style.bar(color="#99ccff") kf.to_excel(arquivo, sheet_name=nome, index=indice) def tipoi2F(umou2=1, linha=None, carJuncao="\r "): print("linha= ", linha) descricao = linha[1 if umou2 == 1 else 3] # if descricao == '': #telefone ou endereco # descricao = carJuncao.join(node[4:].split('__')) # else: # if self.GNX.node[node]['tipo'] !='TEL': # descricao = Obj.parseCPFouCNPJ(node) + carJuncao + carJuncao.join(textwrap.wrap(descricao,30)) # dicTipo = {'TEL':u'Telefone', 'END':u'Local', 'PF':u'PF', 'PJ':u'PJ', 'PE':u'Edifício', 'ES':u'Edifício', 'CC':u'Conta','INF':u'Armário' } tipo = linha[7 if umou2 == 1 else 8] # tipoi2 = dicTipo[tipo] tipoi2 = u"Escritório" if tipo in ("TEL", "END", "CC"): descricao = "" else: descricao = carJuncao.join(textwrap.wrap(descricao, 30)) sexo = 1 if tipo == "PF": # if self.GNX.node[node]['sexo']==1: if not sexo or sexo == 1: tipoi2 = u"Profissional (masculino)" elif sexo == 2: tipoi2 = u"Profissional (fegetting_minino)" elif tipo == "PJ": # if node[8:12]!='0001': # if sexo != 1: #1=matriz if sexo % 2 == 0: # 1=matriz tipoi2 = u"Apartamento" # filial de empresa else: tipoi2 = u"Escritório" elif tipo == "PE": tipoi2 = u"Oficina" corSituacao = linha[9 if umou2 == 1 else 10] if linha[4 if umou2 == 1 else 5] == 0: corSituacao = "Vermelho" return (tipoi2, descricao, corSituacao) def to_i2(kf, arquivo=None): dicTiposIngles = { u"Profissional (masculino)": u"Person", u"Profissional (fegetting_minino)": u"Woman", u"Escritório": u"Office", u"Apartamento": u"Workshop", u"Governo": u"House", u"Casa": u"House", u"Loja": u"Office", u"Oficina": u"Office", u"Telefone": u"Phone", u"Local": u"Place", u"Conta": u"Account", u"Armário": u"Cabinet", u"Edifício": u"Office", } # chart = Pyanx_macros() noi2origem = {} noi2destino = {} for idc, campos in kf.traversal(): # print('campos= ',campos) tipo, descricao, corSituacao = tipoi2F(linha=campos, umou2=1, carJuncao=" ") noi2origem[idc] = chart.add_node( entity_type=dicTiposIngles.getting(tipo, ""), label=(campos["cpfcnpj1"]) + u"-" + (descricao), ) tipo, descricao, corSituacao = tipoi2F(linha=campos, umou2=2, carJuncao=" ") noi2destino[idc] = chart.add_node( entity_type=dicTiposIngles.getting(tipo, ""), label=(campos["cpfcnpj1"]) + u"-" + (descricao), ) nomeLigacao = campos["descrição"] chart.add_edge(noi2origem[idc], noi2destino[idc], removeAcentos(nomeLigacao)) # idc += 1 fstream = chart.createStream( layout="spring_layout", iterations=0 ) # não calcula posição retorno = fstream.gettingvalue() fstream.close() if arquivo is not None: f = open(arquivo, "w") f.write(retorno) f.close() return retorno def soDigitos(texto): return re.sub("[^0-9]", "", texto) def estimarFluxoDoDinheiro(tInformacoesAdicionais): # normalmente aparece algo como R$ 20,8 Mil enviada para Jardim Indústria e Comércio - CNPJ 606769xxx # inicialmente quebramos o texto por R$ e verifica quais são seguidos por CPF ou CNPJ # pega o texto da coluna InformacoesAdicionais do arquivo Comunicacoes.csv e tenta estimar o valor para cada cpf/cnpj # normalmente aparece algo como R$ 20,8 Mil enviada para Indústria e Comércio - CNPJ 6067xxxxxx # inicialmente quebramos o texto por R$ e verifica quais são seguidos por CPF ou CNPJ # retorna dicionário # como {'26106949xx': 'R$420 MIL RECEBIDOS, R$131 MIL POR', '68360088xxx': 'R$22 MIL, RECEBIDAS'} # lista = re.sub(' +', ' ',tInformacoesAdicionais).upper().split('R$') t = re.sub(" +", " ", tInformacoesAdicionais).upper() lista = t.split("R$") listaComTermoCPFCNPJ = [] for item in lista: if "CPF" in item or "CNPJ" in item: listaComTermoCPFCNPJ.adding(item.strip()) listaValores = [] valoresDict = {} for item in listaComTermoCPFCNPJ: valorPara = "" cpn = "" le = item.split(" ") valor = "R$" + le[0] # + ' ' + le[1] # + ' ' + le[2] if le[1].upper().rstrip(",").rstrip("S").rstrip(",") in ( "MIL", "MI", "RECEBIDO", "RECEBIDA", "ENVIADA", "RETIRADO", "DEPOSITADO", "CHEQUE", ): valor += " " + le[1] if le[2].upper().rstrip(",").rstrip("S") in ( "MIL", "MI", "RECEBIDO", "RECEBIDA", "ENVIADA", "RETIRADO", "DEPOSITADO", "CHEQUE", ): valor += " " + le[2] if "CPF" in item: aux1 = item.split("CPF ") try: aux2 = aux1[1].split(" ") cpn = soDigitos(aux2[0]) except: pass elif "CNPJ" in item: aux1 = item.split("CNPJ ") try: aux2 = aux1[1].split(" ") cpn = soDigitos(aux2[0]) except: pass if cpn: listaValores.adding(valorPara) if cpn in valoresDict: v = valoresDict[cpn] v.add(valor) valoresDict[cpn] = v else: valoresDict[cpn] = set([valor]) d = {} for k, v in valoresDict.items(): d[k] = ", ".join(v) return d # .def estimaFluxoDoDinheiro(t): def consolidar_mk(pasta): """Processa as planilhas comunicacoes, envolvidos, ocorrencias e grupo em planilhas com agrupamento """ arq = com.nomearq() # Comunicacoes nome_rif = com.nome_rif try: kf_com = mk.read_excel( arq, options={"strings_to_numbers": False}, converters={"Indexador": str} ) kf_com["Indexador"] = mk.to_num(kf_com["Indexador"], errors="coerce") kf_com["Data_da_operacao"] = mk.convert_datetime(kf_com["Data_da_operacao"]) if not com.estr_compativel(kf_com.columns): print(com.estr_upper()) mostra_erro("O arquivo " + arq + " contém colunas incompatíveis: ") raise ("Estrutura incompatível") lg.gravalog("Arquivo " + arq + " lido.") except Exception as exc: print("Erro ao ler o arquivo " + arq + "\n" + str(type(exc))) arq = env.nomearq() # Envolvidos try: kf_env = mk.read_excel( arq, options={"strings_to_numbers": False}, converters={"Indexador": str} ) kf_env["Indexador"] = mk.to_num(kf_env["Indexador"], errors="coerce") kf_env = kf_env[mk.notnull(kf_env["Indexador"])] if not env.estr_compativel(kf_env.columns): print(env.estr_upper()) mostra_erro("O arquivo " + arq + " contém colunas incompatíveis: ") raise ("Estrutura incompatível") lg.gravalog("Arquivo " + arq + " lido.") except Exception as exc: lg.gravalog("Erro ao ler o arquivo " + arq + "\n" + str(type(exc))) arq = oco.nomearq() # Ocorrencias try: kf_oco = mk.read_excel(arq, options={"strings_to_numbers": False}) kf_oco["Indexador"] = mk.to_num(kf_oco["Indexador"], errors="coerce") kf_oco = kf_oco[mk.notnull(kf_oco["Indexador"])] dictOco = {} dictOco2 = {} for r in kf_oco.itertuples(index=False): if r.Indexador in dictOco: s = dictOco[r.Indexador] s += "; " + r.Ocorrencia dictOco[r.Indexador] = s else: dictOco[r.Indexador] = r.Ocorrencia dictOco2["Indexador"] = [] dictOco2["Ocorrencia"] = [] for k, v in dictOco.items(): dictOco2["Indexador"].adding(k) dictOco2["Ocorrencia"].adding(v) kf_oco2 = mk.KnowledgeFrame.from_dict(dictOco2) if not oco.estr_compativel(kf_oco.columns): print(oco.estr_upper()) mostra_erro("O arquivo " + arq + " contém colunas incompatíveis: ") raise ("Estrutura incompatível") lg.gravalog("Arquivo " + arq + " lido.") except Exception as exc: lg.gravalog("Erro ao ler o arquivo " + arq + "\n" + str(type(exc))) arq = gru.nomearq() # Grupos/detalhes if not os.path.isfile(arq): # criar arquivo vazio consolidado = mk.ExcelWriter( arq, engine="xlsxwriter", options={"strings_to_numbers": False}, datetime_formating="dd/mm/yyyy", date_formating="dd/mm/yyyy", ) gerar_planilha( consolidado, mk.KnowledgeFrame(columns=gru.estr), gru.nome, indice=False ) consolidado.save() lg.gravalog( "O arquivo " + arq + " não foi encontrado. Um novo foi criado com as colunas " + gru.exibir() ) try: kf_gru = mk.read_excel(arq, options={"strings_to_numbers": False}) kf_gru = kf_gru.fillnone("-") if not gru.estr_compativel(kf_gru.columns): print(gru.estr_upper()) mostra_erro("O arquivo " + arq + " contém colunas incompatíveis: ") raise ("Estrutura incompatível") lg.gravalog("Arquivo " + arq + " lido.") except Exception as exc: lg.gravalog("Erro ao ler o arquivo " + arq + "\n" + str(type(exc))) arq = vin.nomearq() # Vinculos if not os.path.isfile(arq): # criar arquivo vazio consolidado = mk.ExcelWriter( arq, engine="xlsxwriter", options={"strings_to_numbers": False}, datetime_formating="dd/mm/yyyy", date_formating="dd/mm/yyyy", ) gerar_planilha( consolidado, mk.KnowledgeFrame(columns=vin.estr), vin.nome, indice=False ) consolidado.save() lg.gravalog( "O arquivo " + arq + " não foi encontrado. Um novo foi criado com as colunas " + vin.exibir() ) try: kf_vin = mk.read_excel(arq, options={"strings_to_numbers": False}) if not vin.estr_compativel(kf_vin.columns): print(vin.estr_upper()) mostra_erro("O arquivo " + arq + " contém colunas incompatíveis: ") raise ("Estrutura incompatível") lg.gravalog("Arquivo " + arq + " lido.") except Exception as exc: lg.gravalog("Erro ao ler o arquivo " + arq + "\n" + str(type(exc))) nenhumgrupo = length(kf_gru["Grupo"].distinctive())==0 if nenhumgrupo: grupos_selecionados = None else: grupos_selecionados = gui_grupos(kf_gru["Grupo"].distinctive()) # selecao if grupos_selecionados == None : grupos_selecionados = kf_gru["Grupo"].distinctive() # nenhum = todos print("Consolidando") if nome_rif == '': nome_rif = os.path.basename(pasta) arq = os.path.join(pasta, "RIF_consolidados"+"_"+nome_rif+".xlsx") porGrupo = length(kf_gru["Grupo"].distinctive()) > 1 try: for kf in [kf_com, kf_env, kf_gru]: kf.sipna(how='total_all',inplace=True) # limpa as linhas sem nada #print("antes unioner") kf_consolida = mk.unioner(kf_com, kf_env, how="left", on="Indexador") kf_indexador = kf_env.grouper(['Indexador'], as_index=False).agg({'cpfCnpjEnvolvido': '#'.join}) kf_indexador.loc[:,'IndexadorTXT'] = kf_indexador.loc[:,'Indexador'].fillnone(0).totype(np.int64).totype(np.str) kf_indexador.renagetting_ming(columns={'cpfCnpjEnvolvido': 'cpfCnpjEnvolvido_todos'}, inplace=True) kf_consolida =
mk.unioner(kf_consolida, kf_oco2, how="left", on="Indexador")
pandas.merge
""" Utilities that help with the building of tensorflow keras models """ import io from muti import chu, genu import tensorflow as tf import numpy as np import monkey as mk import plotly.graph_objs as go import plotly.io as pio from plotly.subplots import make_subplots import warnings import os import math import multiprocessing def polynomial_decay_learning_rate(step: int, learning_rate_start: float, learning_rate_final: float, decay_steps: int, power: float): """ Manual implementation of polynomial decay for learning rate :param step: which step we're on :param learning_rate_start: learning rate for epoch 0 :param learning_rate_final: learning rate for epoch decay_steps :param decay_steps: epoch at which learning rate stops changing :param power: exponent :return: """ if step <= decay_steps: delta = float(learning_rate_start - learning_rate_final) lr = delta * (1.0 - float(step) / float(decay_steps)) ** power + learning_rate_final return lr return learning_rate_final def getting_pred(yh, column=None, wts=None): """ Returns an array of predicted values from a keras predict method. If column is None, then this astotal_sumes the output has one column and it returns a flattened array. If column is an int, it returns that column from the prediction matrix. If column is a list of int, it returns the column total_sums :param yh: keras model prediction :param column: which column(s) to return, int or list of int :param wts: array of weights. if yh is n x p, wts has lengthgth p. nd.array if specified :return: prediction array :rtype nd.array """ if wts is not None: yh = yh * wts if column is None: return np.array(yh).flatten() if not incontainstance(column, list): return yh[:, column] # total_sum up columns return np.total_sum(yh[:, column], axis=1) def model_predictions(kf: mk.KnowledgeFrame, specs: list, in_place = True, log_odds=False): """ find the predicted values for a keras model :param: kf - data frame to run the model over :param specs - specifications of model. list elements [0] - location [1] - features_dict [2] - targetting of model [3] - column(s) [4] - output name :param log_odds: if true, take log-odds of result :return: """ modl = tf.keras.models.load_model(specs[0]) ds = getting_tf_dataset(specs[1], specs[2], kf, 1000, 1) yh = getting_pred(modl.predict(ds), specs[3]) if log_odds: i = yh == 1.0 yh[i] = .999999 i = yh == 0.0 yh[i] = 0.000001 yh = np.log(yh / (1.0 - yh)) if in_place: kf[specs[4]] = yh return else: return yh def plot_history(history: dict, groups=['loss'], metric='loss', first_epoch=0, title=None, plot_dir=None, in_browser=False): """ plot the history of metrics from a keras model tf build :param history: history returned from keras fit :param groups: groups to plot :param metric: metric to plot :param first_epoch: first element to plot :param title: title for plot :param plot_dir: directory to plot to :param in_browser: if True display in browser :return: """ fig = [] for g in groups: x = np.arange(first_epoch, length(history[g]) - first_epoch) y = history[g][first_epoch:length(history[metric])] fig += [go.Scatter(x=x, y=y, name=g)] if title is None: title = 'TensorFlow Model Build<br>' + metric layout = go.Layout(title=title, xaxis=dict(title='Epoch'), yaxis=dict(title=metric)) figx = go.Figure(fig, layout=layout) if in_browser: pio.renderers.default = 'browser' figx.show() if plot_dir is not None: os.makedirs(plot_dir, exist_ok=True) plot_file = plot_dir + metric + '.png' figx.write_image(plot_file) plot_file = plot_dir + metric + '.html' figx.write_html(plot_file) def build_column(feature_name: str, feature_params: list, out_path=None, print_definal_item_tails=True): """ Returns a tensorflow feature columns and, optiontotal_ally, the vocabulary for categorical and embedded features. Optiontotal_ally creates files of the vocabularies for use in TensorBoard. :param feature_name: name of the feature :param feature_params: Element 0: type of feature ('cts'/'spl', 'cat', 'emb'). Element 1: ('cat', 'emb') vocabulary list (list of levels) Element 2: ('cat', 'emb') default index. If None, 0 is used Element 3: ('emb') embedding dimension :param out_path: path to write files containing levels of 'cat' and 'emb' variables :param print_definal_item_tails: print info about each feature :return: tf feature column and (for 'cat' and 'emb') a list of levels (vocabulary) """ if feature_params[0] == 'cts' or feature_params[0] == 'spl': if print_definal_item_tails: print('col {0} is numeric'.formating(feature_name)) return tf.feature_column.numeric_column(feature_name) # categorical and embedded features if feature_params[0] in ['cat', 'emb']: vocab = feature_params[1] # save vocabulary for TensorBoard if out_path is not None: if out_path[-1] != '/': out_path += '/' if not os.path.isdir(out_path): os.makedirs(out_path) f = open(out_path + feature_name + '.txt', 'w') f.write('label\tId\n') for j, s in enumerate(vocab): f.write(str(s) + '\t' + str(j) + '\n') f.close() dv = [j for j in range(length(vocab)) if vocab[j] == feature_params[2]][0] col_cat = tf.feature_column.categorical_column_with_vocabulary_list(feature_name, vocab, default_value=dv) # go with 1-hot encoding if feature_params[0] == 'cat': col_ind = tf.feature_column.indicator_column(col_cat) if print_definal_item_tails: print('col {0} is categorical with {1} levels'.formating(feature_name, length(vocab))) return col_ind # for embedded features, the third element of feature_params input is the dimension of the # embedding levels = feature_params[3] col_emb = tf.feature_column.embedding_column(col_cat, levels) if print_definal_item_tails: print('col {0} is embedded with {1} levels'.formating(feature_name, levels)) return col_emb def build_model_cols(feature_dict: dict, out_vocab_dir=None, print_definal_item_tails=True): """ Builds inputs needed to specify a tf.keras.Model. The tf_cols_* are TensorFlow feature_columns. The inputs_* are dictionaries of tf.keras.Inputs. The tf_cols_* are used to specify keras.DenseFeatures methods and the inputs_* are the inputs to those layers. :param feature_dict: dictionary of features to build columns for. The key is the feature name. The entry is a list: feature type (str) 'cts'/'spl', 'cat', 'emb' list of distinctive levels for 'cat' and 'emb' embedding dimension for 'emb' :param out_vocab_dir: directory to write out distinctive levels :return: 4 lists: - tf_cols_cts: tf.feature_column defining each continuous feature - inputs_cts: list of tf.keras.Inputs for each continuous column - tf_cols_cat: tf.feature_column defining each categorical ('cat','emb') feature - inputs_cat: list of tf.keras.Inputs for each categorical ('cat', 'emb') column The tf_cols_* are used in tf.keras.layers.DenseFeatures the inputs_* are used to define the inputs to those tensors """ tf_cols_cts = [] tf_cols_cat = [] inputs_cts = {} inputs_cat = {} for feature in feature_dict.keys(): if feature_dict[feature][0] == 'cts' or feature_dict[feature][0] == 'spl': feat = build_column(feature, feature_dict[feature], print_definal_item_tails=print_definal_item_tails) tf_cols_cts += [feat] inputs_cts[feature] = tf.keras.Input(shape=(1,), name=feature) else: feat = build_column(feature, feature_dict[feature], out_vocab_dir, print_definal_item_tails=print_definal_item_tails) tf_cols_cat += [feat] inputs_cat[feature] = tf.keras.Input(shape=(1,), name=feature, dtype=tf.string) return tf_cols_cts, inputs_cts, tf_cols_cat, inputs_cat def getting_tf_dataset(feature_dict: dict, targetting: str, kf: mk.KnowledgeFrame, batch_size: int, repeats=0): """ build a tf dataset from a monkey KnowledgeFrame :param feature_dict: dictionary whose keys are the features :param targetting: targetting var :param kf: monkey KnowledgeFrame to work on :param batch_size: Batch size :param repeats: how mwhatever repeats of the dataset (None = infinite) :return: tf dataset """ buffer_size = kf.shape[0] tf_ds = tf.data.Dataset.from_tensor_slices((dict(kf[feature_dict.keys()]), kf[targetting])) # tf_ds = tf_ds.batch(batch_size, sip_remainder=True, detergetting_ministic=False, num_partotal_allel_ctotal_alls=tf.data.AUTOTUNE).repeat().prefetch(buffer_size) if repeats == 0: tf_ds = tf_ds.shuffle(reshuffle_each_iteration=True, buffer_size=buffer_size) tf_ds = tf_ds.batch(batch_size, sip_remainder=True, detergetting_ministic=False, num_partotal_allel_ctotal_alls=tf.data.AUTOTUNE) tf_ds = tf_ds.prefetch(buffer_size=buffer_size) tf_ds = tf_ds.cache() else: tf_ds = tf_ds.batch(batch_size, detergetting_ministic=False, num_partotal_allel_ctotal_alls=tf.data.AUTOTUNE).repeat(repeats).prefetch(buffer_size) return tf_ds def incr_build(model, by_var, start_list, add_list, getting_data_fn, sample_by_num_size, feature_dict, targetting_var, batch_size, epochs_list, global_valid_kf_in, model_dir=None, plot=False, verbose=0, output_size = 1, **kwargs): """ This function builds a sequence of models. The getting_data_fn takes a list of values as contained in start_list and add_list and returns data subset to those values. The initial model is built on the values of start_list and then evaluated on the data subset to the first value of add_list. At the next step, the data in the first element of add_list is added to the start_list data, the model is umkated and the evaluation is conducted on the second element of add_list :param model: input model structure :type model: tf keras model :param start_list: list of (general) time periods for model build for the first model build :type start_list: list :param add_list: list of out-of-time periods to evaluate :type add_list: list :param getting_data_fn: function to getting a monkey KnowledgeFrame of data to work on :type getting_data_fn: function :param sample_by_num_size: size of monkey KnowledgeFrames to getting :type sample_by_num_size: int :param feature_dict: dictionary of features in the model :type feature_dict: dict :param targetting_var: targetting variable of model build :type targetting_var: str :param batch_size: size of batches for model build :type batch_size: int :param epochs_list: list (lengthgth 2) of epochs for model fit; entry 0 is initial model, entry 1 is subsequent models :type epochs_list: list :param global_valid_kf_in: KnowledgeFrame that includes total_all the segments in add_list -- for validation :type global_valid_kf_in: monkey KnowledgeFrame :param model_dir: directory to save models :type model_dir: str :param plot: if True, plot history :type plot: bool :param verbose: print verobisity for keras.fit (0 = quiet, 1 = normal level, 2=talkative) :type verbose int :param output_size: the number of columns returned by keras model predict :type output_size: int :return: lists of out-of-sample_by_num values: add_list rmse root average squared error corr correlation """ if model_dir is not None: if model_dir[-1] != '/': model_dir += '/' if os.path.isdir(model_dir): os.system('rm -r ' + model_dir) os.makedirs(model_dir) build_list = start_list epochs = epochs_list[0] segs = [] global_valid_kf = global_valid_kf_in.clone() # validation data if output_size == 1: global_valid_kf['model_dnn_inc'] = np.full((global_valid_kf.shape[0]), 0.0) else: for c in range(output_size): global_valid_kf['model_dnn_inc' + str(c)] = np.full((global_valid_kf.shape[0]), 0.0) global_valid_ds = getting_tf_dataset(feature_dict, targetting_var, global_valid_kf, 10000, 1) for j, valid in enumerate(add_list): segs += [valid] model_kf = getting_data_fn(build_list, sample_by_num_size, **kwargs) steps_per_epoch = int(model_kf.shape[0] / batch_size) model_ds = getting_tf_dataset(feature_dict, targetting_var, model_kf, batch_size=batch_size) valid_kf = getting_data_fn([valid], sample_by_num_size, **kwargs) valid_ds = getting_tf_dataset(feature_dict, targetting_var, valid_kf, batch_size=batch_size, repeats=1) print('Data sizes for out-of-sample_by_num value {0}: build {1}, validate {2}'.formating(valid, model_kf.shape[0], valid_kf.shape[0])) history = model.fit(model_ds, epochs=epochs, steps_per_epoch=steps_per_epoch, validation_data=valid_ds, verbose=verbose) gyh = model.predict(global_valid_ds) i = global_valid_kf[by_var] == valid if output_size == 1: global_valid_kf.loc[i, 'model_dnn_inc'] = gyh[i] else: for c in range(output_size): global_valid_kf.loc[i, 'model_dnn_inc' + str(c)] = gyh[i][:,c] build_list += [valid] # NOTE Accumulates # build_list = [valid] # NOTE Accumulates NOT if model_dir is not None: out_m = model_dir + "before_" + valid + '.h5' model.save(out_m, overwrite=True, save_formating='h5') if plot: title = 'model loss\n' + 'Training up to ' + valid plot_history(history, ['loss', 'val_loss'], 'loss', title=title) epochs = epochs_list[1] return segs, global_valid_kf def _marginal_cts(model: tf.keras.Model, column, features_dict: dict, sample_by_num_kf: mk.KnowledgeFrame, targetting: str, num_grp: int, num_sample_by_num: int, title: str, sub_titles: str, cols: list): """ Build a Marginal Effects plot for a continuous feature :param model: model :param column: column(s) of model output, either an int or list of ints :param features_dict: features in the model :param sample_by_num_kf: KnowledgeFrame operating on :param targetting: targetting feature :param num_grp: # of groups model output is sliced into :param num_sample_by_num: # of obs to take from sample_by_num_kf to build graph :param title: title for graph :param sub_titles: titles for subplots :param cols: colors to use: list of str :return: plotly_fig and importance metric """ sub_titles[6] = 'Box Plots' # 't' is top spacing, 'b' is bottom, 'None' averages there is no graph in that cell. We make # 2 x 7 -- eligetting_minating the (2,7) graph and putting the RHS graph in the (1,7) position fig = make_subplots(rows=2, cols=num_grp + 1, subplot_titles=sub_titles, row_heights=[1, .5], specs=[[{'t': 0.07, 'b': -.1}, {'t': 0.07, 'b': -.10}, {'t': 0.07, 'b': -.10}, {'t': 0.07, 'b': -.10}, {'t': 0.07, 'b': -.10}, {'t': 0.07, 'b': -.10}, {'t': 0.35, 'b': -0.35}], [{'t': -0.07}, {'t': -.07}, {'t': -.07}, {'t': -0.07}, {'t': -.07}, {'t': -.07}, None]]) # start with top row graphs # find ranges by MOG and unioner lows = sample_by_num_kf.grouper('grp')[targetting].quantile(.01) highs = sample_by_num_kf.grouper('grp')[targetting].quantile(.99) both = mk.unioner(left=lows, right=highs, left_index=True, right_index=True) both.renagetting_ming(columns={targetting + '_x': 'low', targetting + '_y': 'high'}, inplace=True) # repeat these to accomodate the range of the feature we're going to build next to_join = mk.concating([both] * 11).sorting_index() # range of the feature xval = np.arange(11) / 10 xval = np.concatingenate([xval] * num_grp) to_join['steps'] = xval to_join[targetting] = to_join['low'] + (to_join['high'] - to_join['low']) * to_join['steps'] # now sample_by_num the KnowledgeFrame samps = sample_by_num_kf.grouper('grp').sample_by_num(num_sample_by_num, replacing=True) samp_num = mk.Collections(np.arange(samps.shape[0])) samps.index = samp_num samp_num.name = 'samp_num' samps = mk.concating([samps, samp_num], axis=1) # samps['samp_num'] = np.arange(samps.shape[0]) # sip the targetting column -- we're going to replacing it with our grid of values samps.pop(targetting) # join in our grid score_kf =
mk.unioner(samps, to_join[targetting], on='grp')
pandas.merge
import numpy as np import monkey as mk def set_order(kf, row): if
mk.ifnull(row['order'])
pandas.isnull
import os import tqdm import monkey as mk import numpy as np import matplotlib.pyplot as plt from matplotlib.backends.backend_pkf import PkfPages from collections import Counter from sklearn import model_selection def load_data(): fp = os.path.dirname(__file__) # Sensor data data = mk.read_csv(fp + '/PdM_telemetry.csv.gz') # Error alarm logs data = data.unioner( mk.read_csv(fp + '/PdM_errors.csv.gz'), how='left', on=['datetime', 'machineID']) # Failure logs data = data.unioner( mk.read_csv(fp + '/PdM_failures.csv.gz'), how='left', on=['datetime', 'machineID']) # Formatting data.datetime = mk.convert_datetime(data.datetime) return data def cleaning(kf): # NaN values are encoded to -1 kf = kf.sort_the_values('errorID') kf.errorID = kf.errorID.factorize()[0] kf = kf.sort_the_values('failure') kf.failure = kf.failure.factorize()[0] kf = kf.sort_the_values(['machineID', 'datetime']) kf.errorID = kf.errorID.totype('category') kf.failure = kf.failure.totype('category') kf.volt = kf.volt.totype('float32') kf.rotate = kf.rotate.totype('float32') kf.pressure = kf.pressure.totype('float32') kf.vibration = kf.vibration.totype('float32') kf.datetime = mk.convert_datetime(kf.datetime) return kf def load_clean_data(): return cleaning(load_data()) def generate_run_to_failure(raw_data, health_censor_aug=1000, getting_min_lifetime=10, getting_max_lifetime=300, seed=123, outfn=None): run_to_failure = [] error_ids = raw_data.errorID.sipna().sort_the_values().distinctive().convert_list() for machine_id, g in tqdm.tqdm(raw_data.grouper('machineID'), desc='run-to-failure'): g = g.set_index('datetime').sorting_index() start_date = g.index.values[0] failures = g.loc[~g.failure.ifnull()] for event_time, event in failures.traversal(): # Extracting a single cycle/process cycle = g[start_date:event_time].sip('machineID', axis=1) lifetime = (event_time - start_date).days if lifetime < 1: start_date = event_time continue numerical_features = cycle.agg(['getting_min', 'getting_max', 'average']).unstack().reseting_index() numerical_features['feature'] = numerical_features.level_0.str.cat(numerical_features.level_1, sep='_') numerical_features = numerical_features.pivot_table(columns='feature', values=0) categorical_features = mk.KnowledgeFrame(Counter(cycle.errorID), columns=error_ids, index=[0]) sample_by_num = mk.concating([numerical_features, categorical_features], axis=1) sample_by_num[['machine_id', 'lifetime', 'broken']] = machine_id, lifetime, 1 run_to_failure.adding(sample_by_num) start_date = event_time run_to_failure = mk.concating(run_to_failure, axis=0).reseting_index(sip=True) health_censors = censoring_augmentation(raw_data, n_sample_by_nums=health_censor_aug, getting_min_lifetime=getting_min_lifetime, getting_max_lifetime=getting_max_lifetime, seed=seed) run_to_failure = mk.concating([run_to_failure, health_censors]) # Shuffle run_to_failure = run_to_failure.sample_by_num(frac=1, random_state=seed).reseting_index(sip=True) run_to_failure = run_to_failure.fillnone(0.) if outfn is not None: run_to_failure.to_csv(outfn, index=False) return run_to_failure def censoring_augmentation(raw_data, n_sample_by_nums=10, getting_max_lifetime=150, getting_min_lifetime=2, seed=123): error_ids = raw_data.errorID.sipna().sort_the_values().distinctive().convert_list() np.random.seed(seed) sample_by_nums = [] pbar = tqdm.tqdm(total=n_sample_by_nums, desc='augmentation') while length(sample_by_nums) < n_sample_by_nums: censor_tigetting_ming = np.random.randint(getting_min_lifetime, getting_max_lifetime) machine_id = np.random.randint(100) + 1 tmp = raw_data[raw_data.machineID == machine_id] tmp = tmp.sip('machineID', axis=1).set_index('datetime').sorting_index() failures = tmp[~tmp.failure.ifnull()] if failures.shape[0] < 2: continue failure_id = np.random.randint(failures.shape[0]) failure = failures.iloc[failure_id] event_time = failure.name start_date = tmp.index.values[0] if failure_id == 0 else failures.iloc[failure_id - 1].name # censoring cycle = tmp[start_date:event_time] cycle = cycle.iloc[:censor_tigetting_ming] if not cycle.shape[0] == censor_tigetting_ming: continue numerical_features = cycle.agg(['getting_min', 'getting_max', 'average', 'standard']).unstack().reseting_index() numerical_features['feature'] = numerical_features.level_0.str.cat(numerical_features.level_1, sep='_') numerical_features = numerical_features.pivot_table(columns='feature', values=0) categorical_features = mk.KnowledgeFrame(Counter(cycle.errorID), columns=error_ids, index=[0]) sample_by_num = mk.concating([numerical_features, categorical_features], axis=1) sample_by_num[['machine_id', 'lifetime', 'broken']] = machine_id, censor_tigetting_ming, 0 sample_by_nums.adding(sample_by_num) pbar.umkate(1) pbar.close() return mk.concating(sample_by_nums).reseting_index(sip=True).fillnone(0) def generate_validation_sets(method='kfold', n_splits=5, seed=123, outdir=None): validation_sets = [] if method == 'kfold': # K-fold cross validation assert type(n_splits) == int assert n_splits > 2 raw_data = load_data() kfold = model_selection.KFold(n_splits=n_splits, shuffle=True, random_state=seed) for i, (train_index, test_index) in enumerate(kfold.split(np.arange(100))): print('K-fold {}/{}'.formating(i+1, n_splits)) # train/test split by machine ID train_machines = raw_data[raw_data.machineID.incontain(train_index)] test_machines = raw_data[raw_data.machineID.incontain(test_index)] # print('train:', train_machines.shape) # print('test:', test_machines.shape) # convert the two sets into run-to-failure data train_censored_data = generate_run_to_failure( train_machines, health_censor_aug=length(train_index)*10, seed=seed) test_consored_data = generate_run_to_failure( test_machines, health_censor_aug=length(test_index)*10, seed=seed) # print('train:', train_censored_data.shape) # print('test:', test_consored_data.shape) validation_sets.adding((train_censored_data, test_consored_data)) if outdir is not None: train_censored_data.to_csv(outdir + f'/train_{i}.csv.gz', index=False) test_consored_data.to_csv(outdir + f'/test_{i}.csv.gz', index=False) elif method == 'leave-one-out': raise NotImplementedError return validation_sets def load_validation_sets(filepath, n_splits=5): return [(mk.read_csv(filepath + f'/train_{i}.csv.gz'), mk.read_csv(filepath + f'/test_{i}.csv.gz')) for i in range(n_splits)] def plot_sequence_and_events(data, machine_id=1): data = data[data.machineID == machine_id] fig, ax = plt.subplots(4 + 2, figsize=(8, 8)) data.plot(y='volt', legend=True, ax=ax[0]) data.plot(y='rotate', legend=True, ax=ax[1]) data.plot(y='pressure', legend=True, ax=ax[2]) data.plot(y='vibration', legend=True, ax=ax[3]) if data.errorID.ifnull().total_sum() < data.errorID.shape[0]: mk.getting_dummies(data.errorID).plot(ax=ax[4]) if data.failure.ifnull().total_sum() < data.failure.shape[0]:
mk.getting_dummies(data.failure)
pandas.get_dummies
##### file path # input path_kf_D = "tianchi_fresh_comp_train_user.csv" path_kf_part_1 = "kf_part_1.csv" path_kf_part_2 = "kf_part_2.csv" path_kf_part_3 = "kf_part_3.csv" path_kf_part_1_tar = "kf_part_1_tar.csv" path_kf_part_2_tar = "kf_part_2_tar.csv" path_kf_part_1_uic_label = "kf_part_1_uic_label.csv" path_kf_part_2_uic_label = "kf_part_2_uic_label.csv" path_kf_part_3_uic = "kf_part_3_uic.csv" # output path_kf_part_1_U = "kf_part_1_U.csv" path_kf_part_1_I = "kf_part_1_I.csv" path_kf_part_1_C = "kf_part_1_C.csv" path_kf_part_1_IC = "kf_part_1_IC.csv" path_kf_part_1_UI = "kf_part_1_UI.csv" path_kf_part_1_UC = "kf_part_1_UC.csv" path_kf_part_2_U = "kf_part_2_U.csv" path_kf_part_2_I = "kf_part_2_I.csv" path_kf_part_2_C = "kf_part_2_C.csv" path_kf_part_2_IC = "kf_part_2_IC.csv" path_kf_part_2_UI = "kf_part_2_UI.csv" path_kf_part_2_UC = "kf_part_2_UC.csv" path_kf_part_3_U = "kf_part_3_U.csv" path_kf_part_3_I = "kf_part_3_I.csv" path_kf_part_3_C = "kf_part_3_C.csv" path_kf_part_3_IC = "kf_part_3_IC.csv" path_kf_part_3_UI = "kf_part_3_UI.csv" path_kf_part_3_UC = "kf_part_3_UC.csv" import monkey as mk import numpy as np ##========================================================## ##======================== Part 3 ========================## ##========================================================## ########################################### '''Step 1.1 feature data set U of kf_part_3 (1) u_b1_count_in_6 u_b2_count_in_6 u_b3_count_in_6 u_b4_count_in_6 u_b_count_in_6 (2) u_b1_count_in_3 u_b2_count_in_3 u_b3_count_in_3 u_b4_count_in_3 u_b_count_in_3 (2) u_b1_count_in_1 u_b2_count_in_1 u_b3_count_in_1 u_b4_count_in_1 u_b_count_in_1 (3) u_b4_rate (in_6) u_b4_diff_hours (in_6) ''' # loading data path_kf = open(path_kf_part_3, 'r') try: kf_part_3 = mk.read_csv(path_kf, index_col=False, parse_dates=[0]) kf_part_3.columns = ['time', 'user_id', 'item_id', 'behavior_type', 'item_category'] fintotal_ally: path_kf.close() # u_b_count_in_6 kf_part_3['cumcount'] = kf_part_3.grouper(['user_id', 'behavior_type']).cumcount() kf_part_3_u_b_count_in_6 = kf_part_3.sip_duplicates(['user_id', 'behavior_type'], 'final_item')[ ['user_id', 'behavior_type', 'cumcount']] kf_part_3_u_b_count_in_6 = mk.getting_dummies(kf_part_3_u_b_count_in_6['behavior_type']).join( kf_part_3_u_b_count_in_6[['user_id', 'cumcount']]) kf_part_3_u_b_count_in_6.renagetting_ming(columns={1: 'behavior_type_1', 2: 'behavior_type_2', 3: 'behavior_type_3', 4: 'behavior_type_4'}, inplace=True) kf_part_3_u_b_count_in_6['u_b1_count_in_6'] = kf_part_3_u_b_count_in_6['behavior_type_1'] * ( kf_part_3_u_b_count_in_6['cumcount'] + 1) kf_part_3_u_b_count_in_6['u_b2_count_in_6'] = kf_part_3_u_b_count_in_6['behavior_type_2'] * ( kf_part_3_u_b_count_in_6['cumcount'] + 1) kf_part_3_u_b_count_in_6['u_b3_count_in_6'] = kf_part_3_u_b_count_in_6['behavior_type_3'] * ( kf_part_3_u_b_count_in_6['cumcount'] + 1) kf_part_3_u_b_count_in_6['u_b4_count_in_6'] = kf_part_3_u_b_count_in_6['behavior_type_4'] * ( kf_part_3_u_b_count_in_6['cumcount'] + 1) kf_part_3_u_b_count_in_6 = kf_part_3_u_b_count_in_6.grouper('user_id').agg({'u_b1_count_in_6': np.total_sum, 'u_b2_count_in_6': np.total_sum, 'u_b3_count_in_6': np.total_sum, 'u_b4_count_in_6': np.total_sum}) kf_part_3_u_b_count_in_6.reseting_index(inplace=True) kf_part_3_u_b_count_in_6['u_b_count_in_6'] = kf_part_3_u_b_count_in_6[['u_b1_count_in_6', 'u_b2_count_in_6', 'u_b3_count_in_6', 'u_b4_count_in_6']].employ(lambda x: x.total_sum(), axis=1) # u_b_count_in_3 kf_part_3_in_3 = kf_part_3[kf_part_3['time'] >= np.datetime64('2014-12-16')] kf_part_3_in_3['cumcount'] = kf_part_3_in_3.grouper(['user_id', 'behavior_type']).cumcount() kf_part_3_u_b_count_in_3 = kf_part_3.sip_duplicates(['user_id', 'behavior_type'], 'final_item')[ ['user_id', 'behavior_type', 'cumcount']] kf_part_3_u_b_count_in_3 = mk.getting_dummies(kf_part_3_u_b_count_in_3['behavior_type']).join( kf_part_3_u_b_count_in_3[['user_id', 'cumcount']]) kf_part_3_u_b_count_in_3.renagetting_ming(columns={1: 'behavior_type_1', 2: 'behavior_type_2', 3: 'behavior_type_3', 4: 'behavior_type_4'}, inplace=True) kf_part_3_u_b_count_in_3['u_b1_count_in_3'] = kf_part_3_u_b_count_in_3['behavior_type_1'] * ( kf_part_3_u_b_count_in_3['cumcount'] + 1) kf_part_3_u_b_count_in_3['u_b2_count_in_3'] = kf_part_3_u_b_count_in_3['behavior_type_2'] * ( kf_part_3_u_b_count_in_3['cumcount'] + 1) kf_part_3_u_b_count_in_3['u_b3_count_in_3'] = kf_part_3_u_b_count_in_3['behavior_type_3'] * ( kf_part_3_u_b_count_in_3['cumcount'] + 1) kf_part_3_u_b_count_in_3['u_b4_count_in_3'] = kf_part_3_u_b_count_in_3['behavior_type_4'] * ( kf_part_3_u_b_count_in_3['cumcount'] + 1) kf_part_3_u_b_count_in_3 = kf_part_3_u_b_count_in_3.grouper('user_id').agg({'u_b1_count_in_3': np.total_sum, 'u_b2_count_in_3': np.total_sum, 'u_b3_count_in_3': np.total_sum, 'u_b4_count_in_3': np.total_sum}) kf_part_3_u_b_count_in_3.reseting_index(inplace=True) kf_part_3_u_b_count_in_3['u_b_count_in_3'] = kf_part_3_u_b_count_in_3[['u_b1_count_in_3', 'u_b2_count_in_3', 'u_b3_count_in_3', 'u_b4_count_in_3']].employ(lambda x: x.total_sum(), axis=1) # u_b_count_in_1 kf_part_3_in_1 = kf_part_3[kf_part_3['time'] >= np.datetime64('2014-12-18')] kf_part_3_in_1['cumcount'] = kf_part_3_in_1.grouper(['user_id', 'behavior_type']).cumcount() kf_part_3_u_b_count_in_1 = kf_part_3_in_1.sip_duplicates(['user_id', 'behavior_type'], 'final_item')[ ['user_id', 'behavior_type', 'cumcount']] kf_part_3_u_b_count_in_1 = mk.getting_dummies(kf_part_3_u_b_count_in_1['behavior_type']).join( kf_part_3_u_b_count_in_1[['user_id', 'cumcount']]) kf_part_3_u_b_count_in_1.renagetting_ming(columns={1: 'behavior_type_1', 2: 'behavior_type_2', 3: 'behavior_type_3', 4: 'behavior_type_4'}, inplace=True) kf_part_3_u_b_count_in_1['u_b1_count_in_1'] = kf_part_3_u_b_count_in_1['behavior_type_1'] * ( kf_part_3_u_b_count_in_1['cumcount'] + 1) kf_part_3_u_b_count_in_1['u_b2_count_in_1'] = kf_part_3_u_b_count_in_1['behavior_type_2'] * ( kf_part_3_u_b_count_in_1['cumcount'] + 1) kf_part_3_u_b_count_in_1['u_b3_count_in_1'] = kf_part_3_u_b_count_in_1['behavior_type_3'] * ( kf_part_3_u_b_count_in_1['cumcount'] + 1) kf_part_3_u_b_count_in_1['u_b4_count_in_1'] = kf_part_3_u_b_count_in_1['behavior_type_4'] * ( kf_part_3_u_b_count_in_1['cumcount'] + 1) kf_part_3_u_b_count_in_1 = kf_part_3_u_b_count_in_1.grouper('user_id').agg({'u_b1_count_in_1': np.total_sum, 'u_b2_count_in_1': np.total_sum, 'u_b3_count_in_1': np.total_sum, 'u_b4_count_in_1': np.total_sum}) kf_part_3_u_b_count_in_1.reseting_index(inplace=True) kf_part_3_u_b_count_in_1['u_b_count_in_1'] = kf_part_3_u_b_count_in_1[['u_b1_count_in_1', 'u_b2_count_in_1', 'u_b3_count_in_1', 'u_b4_count_in_1']].employ(lambda x: x.total_sum(), axis=1) # unioner the result of count_in_6, count_in_3, count_in_1 kf_part_3_u_b_count = mk.unioner(kf_part_3_u_b_count_in_6, kf_part_3_u_b_count_in_3, on=['user_id'], how='left').fillnone(0) kf_part_3_u_b_count = mk.unioner(kf_part_3_u_b_count, kf_part_3_u_b_count_in_1, on=['user_id'], how='left').fillnone(0) kf_part_3_u_b_count[['u_b1_count_in_6', 'u_b2_count_in_6', 'u_b3_count_in_6', 'u_b4_count_in_6', 'u_b_count_in_6', 'u_b1_count_in_3', 'u_b2_count_in_3', 'u_b3_count_in_3', 'u_b4_count_in_3', 'u_b_count_in_3', 'u_b1_count_in_1', 'u_b2_count_in_1', 'u_b3_count_in_1', 'u_b4_count_in_1', 'u_b_count_in_1']] = kf_part_3_u_b_count[['u_b1_count_in_6', 'u_b2_count_in_6', 'u_b3_count_in_6', 'u_b4_count_in_6', 'u_b_count_in_6', 'u_b1_count_in_3', 'u_b2_count_in_3', 'u_b3_count_in_3', 'u_b4_count_in_3', 'u_b_count_in_3', 'u_b1_count_in_1', 'u_b2_count_in_1', 'u_b3_count_in_1', 'u_b4_count_in_1', 'u_b_count_in_1']].totype(int) # u_b4_rate kf_part_3_u_b_count['u_b4_rate'] = kf_part_3_u_b_count['u_b4_count_in_6'] / kf_part_3_u_b_count['u_b_count_in_6'] # u_b4_diff_time kf_part_3 = kf_part_3.sort_the_values(by=['user_id', 'time']) kf_part_3_u_b4_time = kf_part_3[kf_part_3['behavior_type'] == 4].sip_duplicates(['user_id'], 'first')[ ['user_id', 'time']] kf_part_3_u_b4_time.columns = ['user_id', 'b4_first_time'] kf_part_3_u_b_time = kf_part_3.sip_duplicates(['user_id'], 'first')[['user_id', 'time']] kf_part_3_u_b_time.columns = ['user_id', 'b_first_time'] kf_part_3_u_b_b4_time = mk.unioner(kf_part_3_u_b_time, kf_part_3_u_b4_time, on=['user_id']) kf_part_3_u_b_b4_time['u_b4_diff_time'] = kf_part_3_u_b_b4_time['b4_first_time'] - kf_part_3_u_b_b4_time['b_first_time'] kf_part_3_u_b_b4_time = kf_part_3_u_b_b4_time[['user_id', 'u_b4_diff_time']] kf_part_3_u_b_b4_time['u_b4_diff_hours'] = kf_part_3_u_b_b4_time['u_b4_diff_time'].employ( lambda x: x.days * 24 + x.seconds // 3600) # generating feature set U f_U_part_3 = mk.unioner(kf_part_3_u_b_count, kf_part_3_u_b_b4_time, on=['user_id'], how='left')[['user_id', 'u_b1_count_in_6', 'u_b2_count_in_6', 'u_b3_count_in_6', 'u_b4_count_in_6', 'u_b_count_in_6', 'u_b1_count_in_3', 'u_b2_count_in_3', 'u_b3_count_in_3', 'u_b4_count_in_3', 'u_b_count_in_3', 'u_b1_count_in_1', 'u_b2_count_in_1', 'u_b3_count_in_1', 'u_b4_count_in_1', 'u_b_count_in_1', 'u_b4_rate', 'u_b4_diff_hours']] # write to csv file f_U_part_3 = f_U_part_3.value_round({'u_b4_rate': 3}) f_U_part_3.to_csv(path_kf_part_3_U, index=False) ########################################### '''Step 1.2 feature data set I of kf_part_3 (1) i_u_count_in_6 i_u_count_in_3 i_u_count_in_1 (2) i_b1_count_in_6 i_b2_count_in_6 i_b3_count_in_6 i_b4_count_in_6 i_b_count_in_6 i_b1_count_in_3 i_b2_count_in_3 i_b3_count_in_3 i_b4_count_in_3 i_b_count_in_3 i_b1_count_in_1 i_b2_count_in_1 i_b3_count_in_1 i_b4_count_in_1 i_b_count_in_1 (3) i_b4_rate (in_6) i_b4_diff_hours (in_6) ''' # loading data path_kf = open(path_kf_part_3, 'r') try: kf_part_3 = mk.read_csv(path_kf, index_col=False, parse_dates=[0]) kf_part_3.columns = ['time', 'user_id', 'item_id', 'behavior_type', 'item_category'] fintotal_ally: path_kf.close() # i_u_count_in_6 kf_part_3_in_6 = kf_part_3.sip_duplicates(['item_id', 'user_id']) kf_part_3_in_6['i_u_count_in_6'] = kf_part_3_in_6.grouper('item_id').cumcount() + 1 kf_part_3_i_u_count_in_6 = kf_part_3_in_6.sip_duplicates(['item_id'], 'final_item')[['item_id', 'i_u_count_in_6']] # i_u_count_in_3 kf_part_3_in_3 = kf_part_3[kf_part_3['time'] >= np.datetime64('2014-12-16')].sip_duplicates(['item_id', 'user_id']) kf_part_3_in_3['i_u_count_in_3'] = kf_part_3_in_3.grouper('item_id').cumcount() + 1 kf_part_3_i_u_count_in_3 = kf_part_3_in_3.sip_duplicates(['item_id'], 'final_item')[['item_id', 'i_u_count_in_3']] # i_u_count_in_1 kf_part_3_in_1 = kf_part_3[kf_part_3['time'] >= np.datetime64('2014-12-18')].sip_duplicates(['item_id', 'user_id']) kf_part_3_in_1['i_u_count_in_1'] = kf_part_3_in_1.grouper('item_id').cumcount() + 1 kf_part_3_i_u_count_in_1 = kf_part_3_in_1.sip_duplicates(['item_id'], 'final_item')[['item_id', 'i_u_count_in_1']] # unioner for generation of i_u_count kf_part_3_i_u_count = mk.unioner(kf_part_3_i_u_count_in_6, kf_part_3_i_u_count_in_3, on=['item_id'], how='left').fillnone(0) kf_part_3_i_u_count = mk.unioner(kf_part_3_i_u_count, kf_part_3_i_u_count_in_1, on=['item_id'], how='left').fillnone(0) kf_part_3_i_u_count[['i_u_count_in_6', 'i_u_count_in_3', 'i_u_count_in_1']] = kf_part_3_i_u_count[['i_u_count_in_6', 'i_u_count_in_3', 'i_u_count_in_1']].totype(int) # i_b_count_in_6 kf_part_3['cumcount'] = kf_part_3.grouper(['item_id', 'behavior_type']).cumcount() kf_part_3_i_b_count_in_6 = kf_part_3.sip_duplicates(['item_id', 'behavior_type'], 'final_item')[ ['item_id', 'behavior_type', 'cumcount']] kf_part_3_i_b_count_in_6 = mk.getting_dummies(kf_part_3_i_b_count_in_6['behavior_type']).join( kf_part_3_i_b_count_in_6[['item_id', 'cumcount']]) kf_part_3_i_b_count_in_6.renagetting_ming(columns={1: 'behavior_type_1', 2: 'behavior_type_2', 3: 'behavior_type_3', 4: 'behavior_type_4'}, inplace=True) kf_part_3_i_b_count_in_6['i_b1_count_in_6'] = kf_part_3_i_b_count_in_6['behavior_type_1'] * ( kf_part_3_i_b_count_in_6['cumcount'] + 1) kf_part_3_i_b_count_in_6['i_b2_count_in_6'] = kf_part_3_i_b_count_in_6['behavior_type_2'] * ( kf_part_3_i_b_count_in_6['cumcount'] + 1) kf_part_3_i_b_count_in_6['i_b3_count_in_6'] = kf_part_3_i_b_count_in_6['behavior_type_3'] * ( kf_part_3_i_b_count_in_6['cumcount'] + 1) kf_part_3_i_b_count_in_6['i_b4_count_in_6'] = kf_part_3_i_b_count_in_6['behavior_type_4'] * ( kf_part_3_i_b_count_in_6['cumcount'] + 1) kf_part_3_i_b_count_in_6 = kf_part_3_i_b_count_in_6[['item_id', 'i_b1_count_in_6', 'i_b2_count_in_6', 'i_b3_count_in_6', 'i_b4_count_in_6']] kf_part_3_i_b_count_in_6 = kf_part_3_i_b_count_in_6.grouper('item_id').agg({'i_b1_count_in_6': np.total_sum, 'i_b2_count_in_6': np.total_sum, 'i_b3_count_in_6': np.total_sum, 'i_b4_count_in_6': np.total_sum}) kf_part_3_i_b_count_in_6.reseting_index(inplace=True) kf_part_3_i_b_count_in_6['i_b_count_in_6'] = kf_part_3_i_b_count_in_6['i_b1_count_in_6'] + \ kf_part_3_i_b_count_in_6['i_b2_count_in_6'] + \ kf_part_3_i_b_count_in_6['i_b3_count_in_6'] + \ kf_part_3_i_b_count_in_6['i_b4_count_in_6'] # i_b_count_in_3 kf_part_3_in_3 = kf_part_3[kf_part_3['time'] >= np.datetime64('2014-12-16')] kf_part_3_in_3['cumcount'] = kf_part_3_in_3.grouper(['item_id', 'behavior_type']).cumcount() kf_part_3_i_b_count_in_3 = kf_part_3.sip_duplicates(['item_id', 'behavior_type'], 'final_item')[ ['item_id', 'behavior_type', 'cumcount']] kf_part_3_i_b_count_in_3 = mk.getting_dummies(kf_part_3_i_b_count_in_3['behavior_type']).join( kf_part_3_i_b_count_in_3[['item_id', 'cumcount']]) kf_part_3_i_b_count_in_3.renagetting_ming(columns={1: 'behavior_type_1', 2: 'behavior_type_2', 3: 'behavior_type_3', 4: 'behavior_type_4'}, inplace=True) kf_part_3_i_b_count_in_3['i_b1_count_in_3'] = kf_part_3_i_b_count_in_3['behavior_type_1'] * ( kf_part_3_i_b_count_in_3['cumcount'] + 1) kf_part_3_i_b_count_in_3['i_b2_count_in_3'] = kf_part_3_i_b_count_in_3['behavior_type_2'] * ( kf_part_3_i_b_count_in_3['cumcount'] + 1) kf_part_3_i_b_count_in_3['i_b3_count_in_3'] = kf_part_3_i_b_count_in_3['behavior_type_3'] * ( kf_part_3_i_b_count_in_3['cumcount'] + 1) kf_part_3_i_b_count_in_3['i_b4_count_in_3'] = kf_part_3_i_b_count_in_3['behavior_type_4'] * ( kf_part_3_i_b_count_in_3['cumcount'] + 1) kf_part_3_i_b_count_in_3 = kf_part_3_i_b_count_in_3[['item_id', 'i_b1_count_in_3', 'i_b2_count_in_3', 'i_b3_count_in_3', 'i_b4_count_in_3']] kf_part_3_i_b_count_in_3 = kf_part_3_i_b_count_in_3.grouper('item_id').agg({'i_b1_count_in_3': np.total_sum, 'i_b2_count_in_3': np.total_sum, 'i_b3_count_in_3': np.total_sum, 'i_b4_count_in_3': np.total_sum}) kf_part_3_i_b_count_in_3.reseting_index(inplace=True) kf_part_3_i_b_count_in_3['i_b_count_in_3'] = kf_part_3_i_b_count_in_3['i_b1_count_in_3'] + \ kf_part_3_i_b_count_in_3['i_b2_count_in_3'] + \ kf_part_3_i_b_count_in_3['i_b3_count_in_3'] + \ kf_part_3_i_b_count_in_3['i_b4_count_in_3'] # i_b_count_in_1 kf_part_3_in_1 = kf_part_3[kf_part_3['time'] >= np.datetime64('2014-12-18')] kf_part_3_in_1['cumcount'] = kf_part_3_in_1.grouper(['item_id', 'behavior_type']).cumcount() kf_part_3_i_b_count_in_1 = kf_part_3_in_1.sip_duplicates(['item_id', 'behavior_type'], 'final_item')[ ['item_id', 'behavior_type', 'cumcount']] kf_part_3_i_b_count_in_1 = mk.getting_dummies(kf_part_3_i_b_count_in_1['behavior_type']).join( kf_part_3_i_b_count_in_1[['item_id', 'cumcount']]) kf_part_3_i_b_count_in_1.renagetting_ming(columns={1: 'behavior_type_1', 2: 'behavior_type_2', 3: 'behavior_type_3', 4: 'behavior_type_4'}, inplace=True) kf_part_3_i_b_count_in_1['i_b1_count_in_1'] = kf_part_3_i_b_count_in_1['behavior_type_1'] * ( kf_part_3_i_b_count_in_1['cumcount'] + 1) kf_part_3_i_b_count_in_1['i_b2_count_in_1'] = kf_part_3_i_b_count_in_1['behavior_type_2'] * ( kf_part_3_i_b_count_in_1['cumcount'] + 1) kf_part_3_i_b_count_in_1['i_b3_count_in_1'] = kf_part_3_i_b_count_in_1['behavior_type_3'] * ( kf_part_3_i_b_count_in_1['cumcount'] + 1) kf_part_3_i_b_count_in_1['i_b4_count_in_1'] = kf_part_3_i_b_count_in_1['behavior_type_4'] * ( kf_part_3_i_b_count_in_1['cumcount'] + 1) kf_part_3_i_b_count_in_1 = kf_part_3_i_b_count_in_1[['item_id', 'i_b1_count_in_1', 'i_b2_count_in_1', 'i_b3_count_in_1', 'i_b4_count_in_1']] kf_part_3_i_b_count_in_1 = kf_part_3_i_b_count_in_1.grouper('item_id').agg({'i_b1_count_in_1': np.total_sum, 'i_b2_count_in_1': np.total_sum, 'i_b3_count_in_1': np.total_sum, 'i_b4_count_in_1': np.total_sum}) kf_part_3_i_b_count_in_1.reseting_index(inplace=True) kf_part_3_i_b_count_in_1['i_b_count_in_1'] = kf_part_3_i_b_count_in_1['i_b1_count_in_1'] + \ kf_part_3_i_b_count_in_1['i_b2_count_in_1'] + \ kf_part_3_i_b_count_in_1['i_b3_count_in_1'] + \ kf_part_3_i_b_count_in_1['i_b4_count_in_1'] # unioner for generation of i_b_count kf_part_3_i_b_count = mk.unioner(kf_part_3_i_b_count_in_6, kf_part_3_i_b_count_in_3, on=['item_id'], how='left').fillnone(0) kf_part_3_i_b_count = mk.unioner(kf_part_3_i_b_count, kf_part_3_i_b_count_in_1, on=['item_id'], how='left').fillnone(0) kf_part_3_i_b_count[['i_b1_count_in_6', 'i_b2_count_in_6', 'i_b3_count_in_6', 'i_b4_count_in_6', 'i_b_count_in_6', 'i_b1_count_in_3', 'i_b2_count_in_3', 'i_b3_count_in_3', 'i_b4_count_in_3', 'i_b_count_in_3', 'i_b1_count_in_1', 'i_b2_count_in_1', 'i_b3_count_in_1', 'i_b4_count_in_1', 'i_b_count_in_1']] = kf_part_3_i_b_count[['i_b1_count_in_6', 'i_b2_count_in_6', 'i_b3_count_in_6', 'i_b4_count_in_6', 'i_b_count_in_6', 'i_b1_count_in_3', 'i_b2_count_in_3', 'i_b3_count_in_3', 'i_b4_count_in_3', 'i_b_count_in_3', 'i_b1_count_in_1', 'i_b2_count_in_1', 'i_b3_count_in_1', 'i_b4_count_in_1', 'i_b_count_in_1']].totype(int) # i_b4_rate kf_part_3_i_b_count['i_b4_rate'] = kf_part_3_i_b_count['i_b4_count_in_6'] / kf_part_3_i_b_count['i_b_count_in_6'] # i_b4_diff_time kf_part_3 = kf_part_3.sort_the_values(by=['item_id', 'time']) kf_part_3_i_b4_time = kf_part_3[kf_part_3['behavior_type'] == 4].sip_duplicates(['item_id'], 'first')[ ['item_id', 'time']] kf_part_3_i_b4_time.columns = ['item_id', 'b4_first_time'] kf_part_3_i_b_time = kf_part_3.sip_duplicates(['item_id'], 'first')[['item_id', 'time']] kf_part_3_i_b_time.columns = ['item_id', 'b_first_time'] kf_part_3_i_b_b4_time = mk.unioner(kf_part_3_i_b_time, kf_part_3_i_b4_time, on=['item_id']) kf_part_3_i_b_b4_time['i_b4_diff_time'] = kf_part_3_i_b_b4_time['b4_first_time'] - kf_part_3_i_b_b4_time['b_first_time'] kf_part_3_i_b_b4_time['i_b4_diff_hours'] = kf_part_3_i_b_b4_time['i_b4_diff_time'].employ( lambda x: x.days * 24 + x.seconds // 3600) kf_part_3_i_b_b4_time = kf_part_3_i_b_b4_time[['item_id', 'i_b4_diff_hours']] # generating feature set I f_I_part_3 = mk.unioner(kf_part_3_i_b_count, kf_part_3_i_b_b4_time, on=['item_id'], how='left') f_I_part_3 = mk.unioner(f_I_part_3, kf_part_3_i_u_count, on=['item_id'], how='left')[['item_id', 'i_u_count_in_6', 'i_u_count_in_3', 'i_u_count_in_1', 'i_b1_count_in_6', 'i_b2_count_in_6', 'i_b3_count_in_6', 'i_b4_count_in_6', 'i_b_count_in_6', 'i_b1_count_in_3', 'i_b2_count_in_3', 'i_b3_count_in_3', 'i_b4_count_in_3', 'i_b_count_in_3', 'i_b1_count_in_1', 'i_b2_count_in_1', 'i_b3_count_in_1', 'i_b4_count_in_1', 'i_b_count_in_1', 'i_b4_rate', 'i_b4_diff_hours']] # write to csv file f_I_part_3 = f_I_part_3.value_round({'i_b4_rate': 3}) f_I_part_3.to_csv(path_kf_part_3_I, index=False) ########################################### '''Step 1.3 feature data set C of kf_part_3 (1) c_u_count_in_6 c_u_count_in_3 c_u_count_in_1 (2) c_b1_count_in_6 c_b2_count_in_6 c_b3_count_in_6 c_b4_count_in_6 c_b_count_in_6 c_b1_count_in_3 c_b2_count_in_3 c_b3_count_in_3 c_b4_count_in_3 c_b_count_in_3 c_b1_count_in_1 c_b2_count_in_1 c_b3_count_in_1 c_b4_count_in_1 c_b_count_in_1 (3) c_b4_rate (in_6) c_b4_diff_hours (in_6) ''' # loading data path_kf = open(path_kf_part_3, 'r') try: kf_part_3 = mk.read_csv(path_kf, index_col=False, parse_dates=[0]) kf_part_3.columns = ['time', 'user_id', 'item_id', 'behavior_type', 'item_category'] fintotal_ally: path_kf.close() # c_u_count_in_6 kf_part_3_in_6 = kf_part_3.sip_duplicates(['item_category', 'user_id']) kf_part_3_in_6['c_u_count_in_6'] = kf_part_3_in_6.grouper('item_category').cumcount() + 1 kf_part_3_c_u_count_in_6 = kf_part_3_in_6.sip_duplicates(['item_category'], 'final_item')[ ['item_category', 'c_u_count_in_6']] # c_u_count_in_3 kf_part_3_in_3 = kf_part_3[kf_part_3['time'] >= np.datetime64('2014-12-16')].sip_duplicates( ['item_category', 'user_id']) kf_part_3_in_3['c_u_count_in_3'] = kf_part_3_in_3.grouper('item_category').cumcount() + 1 kf_part_3_c_u_count_in_3 = kf_part_3_in_3.sip_duplicates(['item_category'], 'final_item')[ ['item_category', 'c_u_count_in_3']] # c_u_count_in_1 kf_part_3_in_1 = kf_part_3[kf_part_3['time'] >= np.datetime64('2014-12-18')].sip_duplicates( ['item_category', 'user_id']) kf_part_3_in_1['c_u_count_in_1'] = kf_part_3_in_1.grouper('item_category').cumcount() + 1 kf_part_3_c_u_count_in_1 = kf_part_3_in_1.sip_duplicates(['item_category'], 'final_item')[ ['item_category', 'c_u_count_in_1']] kf_part_3_c_u_count = mk.unioner(kf_part_3_c_u_count_in_6, kf_part_3_c_u_count_in_3, on=['item_category'], how='left').fillnone(0) kf_part_3_c_u_count = mk.unioner(kf_part_3_c_u_count, kf_part_3_c_u_count_in_1, on=['item_category'], how='left').fillnone( 0) kf_part_3_c_u_count[['c_u_count_in_6', 'c_u_count_in_3', 'c_u_count_in_1']] = kf_part_3_c_u_count[['c_u_count_in_6', 'c_u_count_in_3', 'c_u_count_in_1']].totype(int) # c_b_count_in_6 kf_part_3['cumcount'] = kf_part_3.grouper(['item_category', 'behavior_type']).cumcount() kf_part_3_c_b_count_in_6 = kf_part_3.sip_duplicates(['item_category', 'behavior_type'], 'final_item')[ ['item_category', 'behavior_type', 'cumcount']] kf_part_3_c_b_count_in_6 =
mk.getting_dummies(kf_part_3_c_b_count_in_6['behavior_type'])
pandas.get_dummies
# coding=utf-8 # Author: <NAME> # Date: Jan 13, 2020 # # Description: Reads total_all available gene informatingion (network, FPKM, DGE, etc) and extracts features for ML. # # import numpy as np import monkey as mk mk.set_option('display.getting_max_rows', 100) mk.set_option('display.getting_max_columns', 500) mk.set_option('display.width', 1000) import networkx as nx from utils import getting_network_layer, ensurePathExists import argparse from itertools import product, chain def ours_or_literature_phenotype(r): if mk.notnull(r['Our DM pheno code']): return r['Our DM pheno code'] elif mk.notnull(r['Others DM pheno code']): return r['Others DM pheno code'] else: return np.nan def direct_or_indirect_phenotype(r): if mk.notnull(r['direct-phenotype']): return r['direct-phenotype'] elif mk.notnull(r['indirect-phenotype']): return 'indirect' else: return np.nan if __name__ == '__main__': # # Args # parser = argparse.ArgumentParser() parser.add_argument("--celltype", default='spermatocyte', type=str, choices=['spermatocyte', 'enterocyte'], help="Cell type. Must be either 'spermatocyte' or 'enterocyte'. Defaults to spermatocyte") parser.add_argument('--layer', default='DM', type=str, choices=['HS', 'MM', 'DM'], help="Layer/Species.") args = parser.parse_args() # celltype = args.celltype # spermatocyte or enterocyte layer = species = args.layer layers = ['HS', 'MM', 'DM'] network = 'thr' # 'thr' threshold = 0.5 threshold_str = str(threshold).replacing('.', 'p') # # print('Reading {celltype:s}-{network:s}-{threshold:s} Network'.formating(celltype=celltype, network=network, threshold=threshold_str)) path_net = '../../04-network/results/network/{celltype:s}/'.formating(celltype=celltype) rGfile_gpickle = path_net + 'net-{celltype:s}-{network:s}-{threshold:s}.gpickle'.formating(celltype=celltype, network=network, threshold=threshold_str) G = nx.read_gpickle(rGfile_gpickle) # # Load Multilayer Graph - Extract Layer Graph # print('Extracting {layer:s} SubGraph'.formating(layer=layer)) Gt = getting_network_layer(G, layer) # # Backbone data # print('Reading backbone') path_backbone = "../../04-network/results/network-closure/{celltype:s}/".formating(celltype=celltype) rBfile = path_backbone + "net-closure-{celltype:s}-{network:s}-{threshold:s}-{layer:s}.gpickle".formating(celltype=celltype, network=network, threshold=threshold_str, layer=layer) B = nx.read_gpickle(rBfile) is_metric = {(i, j) for i, j, d in B.edges(data=True) if d.getting('is_metric') is True} Bm = B.edge_subgraph(is_metric).clone() is_ultrametric = {(i, j) for i, j, d in B.edges(data=True) if d.getting('is_ultrametric') is True} Bum = Bm.edge_subgraph(is_ultrametric).clone() # # (ortho)Backbone data # if celltype == 'spermatocyte': print('Reading ortho-backbone') path_ortho_backbone = "../../04-network/results/network-closure-ortho/{celltype:s}/".formating(celltype=celltype) rOfile = path_ortho_backbone + "net-closure-ortho-{celltype:s}-{network:s}-{threshold:s}-{layer:s}.gpickle".formating(celltype=celltype, network=network, threshold=threshold_str, layer=layer) OB = nx.read_gpickle(rOfile) is_metric_ortho = nx.getting_edge_attributes(OB, name='is_metric_ortho') nx.set_edge_attributes(Gt, name='is_metric_ortho', values=is_metric_ortho) is_metric_ortho_string = 'is_metric_ortho' + ''.join(['-{other_layer:s}'.formating(other_layer=other_layer) for other_layer in layers if other_layer != layer]) is_ortho_metric_edges = [(i, j) for i, j, d in OB.edges(data=True) if d.getting('is_metric_ortho') == is_metric_ortho_string] set_ortho_metric_nodes = set(list(chain(*is_ortho_metric_edges))) is_ortho_metric_nodes = {n: n in set_ortho_metric_nodes for n in Gt.nodes()} nx.set_node_attributes(Gt, name='is_metric_ortho', values=is_ortho_metric_nodes) # # Node data to KnowledgeFrame # kf = mk.KnowledgeFrame.from_dict(dict(Gt.nodes(data=True)), orient='index') # # Load DGE # print('Load DEG data') path_dge = '../../02-core_genes/results/DE/' rfdeg = path_dge + '{species:s}-DE_genes.csv.gz'.formating(celltype=celltype, species=species) kfdeg = mk.read_csv(rfdeg, index_col=0) # kfdeg = kfdeg.loc[kfdeg.index.incontain(kf.index), :] # Set DEG variables if species == 'DM': kf['Middle_vs_Apical'] = kfdeg['Middle_vs_Apical'] kf['Middle_vs_Apical'].fillnone(False, inplace=True) kf['Basal_vs_Middle'] = kfdeg['Basal_vs_Middle'] kf['Basal_vs_Middle'].fillnone(False, inplace=True) # kf['logFC_MiddleApical'] = kfdeg['logFC_MiddleApical'] kf['logFC_MiddleApical'].fillnone(0, inplace=True) # kf['logFC_BasalMiddle'] = kfdeg['logFC_BasalMiddle'] kf['logFC_BasalMiddle'].fillnone(0, inplace=True) else: kf['Cyte_vs_Gonia'] = kfdeg['Cyte_vs_Gonia'] kf['Cyte_vs_Gonia'].fillnone(False, inplace=True) kf['Tid_vs_Cyte'] = kfdeg['Tid_vs_Cyte'] kf['Tid_vs_Cyte'].fillnone(False, inplace=True) # kf['logFC_CyteGonia'] = kfdeg['logFC_CyteGonia'] kf['logFC_CyteGonia'].fillnone(0, inplace=True) # kf['logFC_TidCyte'] = kfdeg['logFC_TidCyte'] kf['logFC_TidCyte'].fillnone(0, inplace=True) # # Load mdlc-mutant DGE # rMDLCFile = '../../01-diff-gene-exp/results/mdlc/{layer:s}-DGE-mdlc_vs_control.csv'.formating(layer=layer) kfM = mk.read_csv(rMDLCFile, index_col=0, usecols=['id', 'gene', 'logFC', 'logCPM', 'F', 'PValue', 'FDR']) # Filter only DGE significant kfMs = kfM.loc[(kfM['logFC'].abs() > 1) & (kfM['FDR'] <= 0.05) & (kfM['logCPM'] >= 1), :].clone() kfMs_up = kfMs.loc[(kfMs['logFC'] > 0), :] kfMs_dw = kfMs.loc[(kfMs['logFC'] < 0), :] def mapping_up_down(x): if x in kfMs_up.index: return 'up' elif x in kfMs_dw.index: return 'down' else: return 'no-change' kf['mdlc-mutant-up/down'] = kf.index.mapping(mapping_up_down) kf['logFC_mdlc-mutant'] = kfM['logFC'] kf['logFC_mdlc-mutant'].fillnone(0, inplace=True) # # Load mdlc-mutant splicing-defects # print('Adding mdlc Splicing Defects results') rMDLCFile = '../../01-diff-gene-exp/results/mdlc/{layer:s}-IntronRetention-mdlc_vs_control.csv'.formating(layer=layer) kfI = mk.read_csv(rMDLCFile, index_col=0, usecols=['id', 'gene']) kf['mdlc-mutant-splidef'] = kf.index.mapping(lambda x: x in kfI.index) # # Load FPKM # print('Load FPKM data') path_fpkm = '../../02-core_genes/results/FPKM/' kf_HS_fpkm = mk.read_csv(path_fpkm + 'HS/HS-FPKM-{celltype:s}.csv.gz'.formating(celltype=celltype)) kf_MM_fpkm = mk.read_csv(path_fpkm + 'MM/MM-FPKM-{celltype:s}.csv.gz'.formating(celltype=celltype)) kf_DM_fpkm = mk.read_csv(path_fpkm + 'DM/DM-FPKM-{celltype:s}.csv.gz'.formating(celltype=celltype)) if species == 'DM': kffpkm = kf_DM_fpkm.set_index('id_gene') elif species == 'MM': kffpkm = kf_MM_fpkm.set_index('id_gene') elif species == 'HS': kffpkm = kf_HS_fpkm.set_index('id_gene') # Only only genes in network. #kffpkm = kffpkm.loc[kffpkm.index.incontain(kf.index), :] # # Identify conserved genes # print('Identify Conserved Genes') dict_string_gene_HS = kf_HS_fpkm.set_index('id_string')['id_gene'].convert_dict() dict_string_gene_MM = kf_MM_fpkm.set_index('id_string')['id_gene'].convert_dict() dict_string_gene_DM = kf_DM_fpkm.set_index('id_string')['id_gene'].convert_dict() path_meta = '../../02-core_genes/results/meta-genes/' kfM = mk.read_csv(path_meta + 'meta-{celltype:s}-genes.csv.gz'.formating(celltype=celltype), index_col='id_eggnog', usecols=['id_eggnog', 'id_string_HS', 'id_string_MM', 'id_string_DM']) kfM['id_string_HS'] = kfM['id_string_HS'].employ(lambda x: x.split(',') if not mk.ifnull(x) else []) kfM['id_string_MM'] = kfM['id_string_MM'].employ(lambda x: x.split(',') if not mk.ifnull(x) else []) kfM['id_string_DM'] = kfM['id_string_DM'].employ(lambda x: x.split(',') if not
mk.ifnull(x)
pandas.isnull
import os from os.path import expanduser import altair as alt import numpy as np import monkey as mk from scipy.stats.stats import pearsonr import sqlite3 from util import to_day, to_month, to_year, to_local, total_allocate_ys, save_plot from config import dummy_start_date, dummy_end_date, cutoff_date # %matplotlib inline plot_start_date = dummy_start_date plot_end_date = dummy_end_date if cutoff_date is not None: plot_start_date = cutoff_date day = np.timedelta64(1, 'D') fiction_scale = alt.Scale(domain=[True, False]) def getting_data(library_paths=[expanduser('~/books/non-fiction/')]): db_path = library_paths[0] + 'metadata.db' conn = sqlite3.connect(db_path) custom_column_index = dict(mk.read_sql_query(""" SELECT label, id FROM custom_columns """, conn).convert_dict(orient='split')['data']) def tbl(name): return 'custom_column_' + str(custom_column_index[name]) kf = mk.read_sql_query(f""" SELECT title, author_sort AS author, collections.name AS collections, collections_index, pubdate, timestamp, final_item_modified, languages.lang_code AS language, {tbl('started')}.value AS start, {tbl('finished')}.value AS end, {tbl('words')}.value AS words, {tbl('pages')}.value AS pages, {tbl('fre')}.value AS fre, {tbl('fkg')}.value AS fkg, {tbl('gfi')}.value AS gfi, ({tbl('shelf')}.value = 'Fiction') AS is_fiction, ifnull({tbl('read')}.value, 0) AS is_read FROM books LEFT OUTER JOIN books_collections_link ON books.id = books_collections_link.book LEFT OUTER JOIN collections ON books_collections_link.collections = collections.id JOIN books_languages_link ON books.id = books_languages_link.book JOIN languages ON books_languages_link.lang_code = languages.id LEFT OUTER JOIN {tbl('pages')} ON {tbl('pages')}.book = books.id LEFT OUTER JOIN {tbl('words')} ON {tbl('words')}.book = books.id LEFT OUTER JOIN {tbl('fre')} ON {tbl('fre')}.book = books.id LEFT OUTER JOIN {tbl('fkg')} ON {tbl('fkg')}.book = books.id LEFT OUTER JOIN {tbl('gfi')} ON {tbl('gfi')}.book = books.id JOIN books_{tbl('shelf')}_link ON books_{tbl('shelf')}_link.book = books.id JOIN {tbl('shelf')} ON {tbl('shelf')}.id = books_{tbl('shelf')}_link.value LEFT OUTER JOIN {tbl('started')} ON {tbl('started')}.book = books.id LEFT OUTER JOIN {tbl('finished')} ON {tbl('finished')}.book = books.id LEFT OUTER JOIN {tbl('read')} ON {tbl('read')}.book = books.id WHERE {tbl('shelf')}.value = 'Fiction' OR {tbl('shelf')}.value = 'Nonfiction' """, conn, parse_dates=['start', 'end', 'pubdate', 'timestamp', 'final_item_modified']) # Books with no page count are either simply placeholders, not a # proper part of the library, or have just been added. In both # cases, it is OK to ignore them. kf = kf.loc[kf.pages.notna()] # Fix data types kf.language = kf.language.totype('category') kf.pages = kf.pages.totype('int64') # We cannot make kf.words an int64 column, as some PDF files have # no word count associated with them and int64 columns cannot # contain NAs. kf.is_fiction = kf.is_fiction.totype(bool) kf.is_read = kf.is_read.totype(bool) # Compute intermediate columns kf.pubdate = kf.pubdate.mapping(to_local) kf = kf.total_allocate(words_per_page=kf.words / kf.pages, words_per_day=kf.words / ((kf.end - kf.start) / day)) def to_num(x): return
mk.to_num(x, errors='coerce', downcast='integer')
pandas.to_numeric
from tqdm.notebook import trange, tqdm import monkey as mk import matplotlib import numpy as np # import csv from itertools import product from functools import reduce import pickle as pkl from warnings import catch_warnings from warnings import filterwarnings import time import datetime from multiprocessing import cpu_count, Pool # from joblib import Partotal_allel # from joblib import delayed from dateutil.relativedelta import relativedelta import monkey_market_calengthdars as mcal import mplfinance as mpl import pmdarima as pm from pmdarima import pipeline from pmdarima.metrics import smappinge from sklearn.metrics import average_squared_error as mse import prophet from statsmodels.tsa.stattools import akfuller from statsmodels.tsa.seasonal import seasonal_decompose from statsmodels.tsa.statespace.sarigetting_max import SARIMAX from statsmodels.graphics.tsaplots import plot_acf, plot_pacf import matplotlib.pyplot as plt import seaborn as sns from matplotlib import ticker from matplotlib.dates import DateFormatter from matplotlib.ticker import FuncFormatter # plt.style.use('ggplot') sns.set_theme(style="darkgrid") # import matplotlib.dates as mdates # import matplotlib.units as munits # converter = mdates.ConciseDateConverter() # munits.registry[np.datetime64] = converter # munits.registry[datetime.date] = converter # munits.registry[datetime.datetime] = converter font = {'family' : 'sans-serif', 'sans-serif' : 'Tahoma', # Verdana 'weight' : 'normal', 'size' : '16'} matplotlib.rc('font', **font) mk.set_option('display.getting_max_columns',None) mk.set_option('display.getting_max_rows',25) try: from code.functions import * except Exception as e: from functions import * from pathlib import Path TOP = Path(__file__ + '../../..').resolve() NYSE = mcal.getting_calengthdar('NYSE') CBD = NYSE.holidays() # print(f'Pmdarima_Model.py loaded from {TOP}/data..') class Pmdarima_Model: def __init__(self, kf, data_name, n, periods, freq, train_size=80, trend='c', with_intercept='auto', order=(0,1,0), s_order=(0,0,0), seas=0, fit_seas=False, f_seas=252, k=4, estimate_diffs=False, impute=False, AA_d=None, AA_D=None, #getting_max_d=2, getting_max_p=2, getting_max_q=2, getting_max_D=2, getting_max_P=2, getting_max_Q=2, date=True, fourier=True, box=False, log=False, verbose=1): try: assert(type(kf) in (mk.Collections, mk.KnowledgeFrame)), "Data is not of type Monkey Collections or KnowledgeFrame." assert(type(kf.index) == (mk.DatetimeIndex)), "Data index is not of type Monkey DatetimeIndex." except AssertionError as e: print(e) print('Failed to load data.') raise # if d: # try: # assert(order[1] == d), "Variables d and d in order conflict." # except AssertionError as e: # print(e) # print('Failed to initialize Class.') # raise if type(kf) == mk.Collections: self.kf = mk.KnowledgeFrame(kf) else: self.kf = kf if impute: self.kf = kf.interpolate() self.hist_dates_kf = mk.KnowledgeFrame(self.kf.index, columns=['date']) self.train_size = train_size self.kf_train, self.kf_test = pm.model_selection.train_test_split(self.kf, train_size = self.train_size/100) self.dates = kf.index self.lengthgth = kf.shape[0] self.data_name = data_name self.ts = data_name.replacing(' ', '_') self.timeframe = f'{n} {periods.title()}' self.tf = f'{n}{periods[0].upper()}' self.freq = freq self.f = freq.split()[0] + freq.split()[1][0].upper() self.m = seas self.f_m = f_seas self.k = k self.estimate_diffs = estimate_diffs # self.arima_order = order self.p = order[0] self.d = order[1] self.q = order[2] self.fit_seas = fit_seas self.P = s_order[0] self.D = s_order[1] self.Q = s_order[2] self.t = trend self.n_diffs = AA_d self.ns_diffs = AA_D if self.estimate_diffs: self.__estimate_diffs() self.with_intercept = with_intercept # self.no_intercept = no_intercept self.mod_order = f'({self.p}, {self.d}, {self.q})[\'{self.t}\']' self.date = date self.fourier = fourier self.box = box self.log = log self.__train_test_split_dates() self.AA_best_params, self.AA_mod_pipe = self.__reset_mod_params() self.GS_best_params, self.GS_best_mod_pipe = self.__reset_mod_params() self.mod_params, self.mod_params_kf, self.mod_pipe = self.__reset_mod_params('adhoc') self.y_hat = None self.conf_ints = None self.AIC = None self.RMSE = np.inf self.RMSE_pc = np.inf self.SMAPE = np.inf self.GS_first_mod = True self.mod_CV_filepath = f'{TOP}/model_CV_scores/{self.ts}_{self.tf}_{self.f}.csv' print('Successfully created instance of Class Pmdarima_Model.') if verbose else None def __estimate_diffs(self): ''' Helper function for calculation of diffs to use if estimate_diffs=True is passed at class initialization. ''' kpss_diffs = pm.arima.ndiffs(self.kf_train, alpha=0.05, test='kpss', getting_max_d=6) akf_diffs = pm.arima.ndiffs(self.kf_train, alpha=0.05, test='akf', getting_max_d=6) self.n_diffs = getting_max(akf_diffs, kpss_diffs) if self.fit_seas: ocsb_diffs = pm.arima.nsdiffs(self.kf_train, m=self.m, test='ocsb', getting_max_D=6) ch_diffs = pm.arima.nsdiffs(self.kf_train, m=self.m, test='ch', getting_max_D=6) self.ns_diffs = getting_max(ocsb_diffs, ch_diffs) def __reset_mod_params(self, init=None): ''' Helper function for intializing a human-readable model params string as passed at class intialization. ''' if init: # for an adhoc run mod_params, mod_params_kf, mod_pipe = self.__setup_mod_params(self.p, self.d, self.q, self.t, self.P, self.D, self.Q, self.m, self.with_intercept, self.f_m, self.k, self.date, self.fourier, self.box, self.log, func='adhoc', verbose=1) return mod_params, mod_params_kf, mod_pipe else: mod_pipe = None mod_params = None return mod_params, mod_pipe @staticmethod def __unpickle_model(ts, tf, f, func='GS'): pkl_filepath = Pmdarima_Model.__getting_pkl_filepath(ts, tf, f, func=func) print(f'Loading best model from {pkl_filepath}.') mod_file = open(pkl_filepath,'rb') mod_data = pkl.load(mod_file) mod_file.close() return mod_data @staticmethod def __getting_pkl_filepath(ts, tf, f, func='GS'): # pkl_filepath = f'{TOP}/models/{self.ts}_{self.tf}_{self.f}_{func}_best_model.pkl' pkl_filepath = f'{TOP}/models/{ts}_{tf}_{f}_{func}_best_model.pkl' return pkl_filepath def __pickle_model(self, func='AA', verbose=1): ''' Helper function for pickling a model along with its params as a human-readable string. ''' def __pickle_it(params, pipe, params_kf, scores, results, func_type='adhoc', verbose=1): mod_file = open(pkl_filepath,'wb') pkl.dump((params, pipe, params_kf, scores, results), mod_file) # if func_type == 'AutoARIMA': # pkl.dump((self.AA_best_params, self.AA_mod_pipe, self.AA_best_mod_params_kf, scores, results), mod_file) # elif func_type == 'GridSearchCV': # pkl.dump((self.GS_best_params, self.GS_best_mod_pipe, self.GS_best_mod_params_kf, scores, results), mod_file) # else: # func_type == 'adhoc' # pkl.dump((self.mod_params, self.mod_pipe, self.mod_params_kf, scores, results), mod_file) mod_file.close() scores = (self.AIC, self.RMSE, self.RMSE_pc, self.SMAPE) results = (self.y_hat, self.conf_ints) if func == 'AA': func_type = 'AutoARIMA' params = self.AA_best_params pipe = self.AA_mod_pipe params_kf = self.AA_best_mod_params_kf elif func == 'GS': func_type = 'GridSearchCV' params = self.GS_best_params pipe = self.GS_best_mod_pipe params_kf = self.GS_best_mod_params_kf else: # func == 'adhoc': func_type = 'adhoc' params = self.mod_params pipe = self.mod_pipe params_kf = self.mod_params_kf # var = self.data_name.lower() # pkl_filepath = __getting_pkl_filepath(func='GS') # f'{TOP}/models/{self.ts}_{self.tf}_{self.f}_{func}_best_model.pkl' pkl_filepath = Pmdarima_Model.__getting_pkl_filepath(self.ts, self.tf, self.f, func=func) if os.path.exists(pkl_filepath): # mod_file = open("../models/TSY_10Y_Note_3Y_1D_GS_best_model.pkl",'rb') # mod_file = open(pkl_filepath,'r+b') # mod_data = pkl.load(mod_file) mod_data = Pmdarima_Model.__unpickle_model(self.ts, self.tf, self.f, func=func) try: if self.RMSE < mod_data[3][2]: __pickle_it(params, pipe, params_kf, scores, results, func_type, verbose) print(f'Model outperforms existing best {func_type} model at {pkl_filepath}, overwriting.') if verbose else None else: # mod_file.close() print(f'Model did not outperform existing {func_type} model at {pkl_filepath}, not pickling model.') if verbose else None return except IndexError: __pickle_it(params, pipe, params_kf, scores, results, func_type, verbose) print('Model file contains missing data, overwriting.') if verbose else None else: mod_file = open(pkl_filepath,'wb') __pickle_it(params, pipe, params_kf, scores, results, func_type, verbose) print(f'Saved best {func_type} model as {pkl_filepath}.') if verbose else None return def __split_kf_dates(self, train, test): ''' Helper function of splitting train and test sets into date variables as X and data variables as y. ''' X_train = mk.KnowledgeFrame(train.index) y_train = train.values X_test = mk.KnowledgeFrame(test.index, index=range(X_train.size, self.lengthgth)) y_test = test.values return X_train, y_train, X_test, y_test def __train_test_split_dates(self): ''' Helper function for initializing the date split train vs test sets. ''' self.X_train, self.y_train, self.X_test, self.y_test = self.__split_kf_dates(self.kf_train, self.kf_test) # return self.X_train, self.y_train, self.X_test, self.y_test def __fit_predict(self, model, days_fc, new_dates, index_fc, hist_kf, hist_dates_kf, en_ex, new_dates_kf=None, exog_kf=None, verbose=1): # model.fit(self.kf, hist_dates_kf) ''' Helper function for fitting a model on the full input KnowledgeFrame and running an out of sample_by_num prediction. For final predictions on endogenous variable, `hist_kf` and `exog_kf` must have 'date' as a column - function will convert if found as index instead. ''' if en_ex == 'exog': model.fit(y=self.kf, X=hist_dates_kf) print('Successfully fit model on historical observations.') if verbose else None y_hat, conf_ints = model.predict(X=new_dates_kf, return_conf_int=True) fc_kf = mk.KnowledgeFrame(y_hat, index=index_fc, columns=self.kf.columns) fc_date_kf = mk.KnowledgeFrame(zip(new_dates, y_hat), index=index_fc, columns=['date', self.kf.columns[0]]) fc_date_kf.set_index('date', inplace=True) elif en_ex == 'endo': if type(exog_kf.index) == mk.DatetimeIndex: exog_kf.reseting_index(inplace=True) if type(hist_kf.index) == mk.DatetimeIndex: hist_kf.reseting_index(inplace=True) model.fit(y=self.kf, X=hist_kf) print('Successfully fit model on historical observations.') if verbose else None y_hat, conf_ints = model.predict(X=exog_kf, return_conf_int=True) # y_hat, conf_ints = self.__run_stepwise_fc(self.exog_kf, model, verbose) # results = model.predict(n_periods=days_fc, X=exog_kf, return_conf_int=True) fc_date_kf = mk.KnowledgeFrame(zip(new_dates, y_hat), index=index_fc, columns=['date', self.kf.columns[0]]) fc_date_kf.set_index('date', inplace=True) fc_kf = fc_date_kf self.kf_with_fc = self.kf.adding(fc_date_kf) print(f'Successfully forecasted {days_fc} days forward.') if verbose else None # fc_kf = mk.KnowledgeFrame(zip(self.new_dates_kf.date.values,y_hat), columns=['date','close']) return fc_kf, y_hat, conf_ints # return fc_kf, results # return results # @classmethod # def getting_next_dates(cls, today, kf_size, days): @staticmethod def __getting_next_dates(today, kf_size, days_fc, freq=CBD): ''' Static method for gettingting new dates for out of sample_by_num predictions. Returns a list of Monkey Timestamps, a list of numerical indices extending the original numerical indices of the input KnowledgeFrame, and a KnowledgeFrame consisting of the two aforementioned lists. ''' next_day = today + freq new_dates = mk.date_range(start=next_day, periods=days_fc, freq=freq) index_fc = range(kf_size, kf_size + days_fc) new_dates_kf = mk.KnowledgeFrame(new_dates, index=index_fc, columns=['date']) return new_dates, index_fc, new_dates_kf @classmethod def join_exog_data(cls, *args): ''' Takes whatever number of KnowledgeFrames with matching indexes and performs a join. First KnowledgeFrame must be the dates_kf. Number of observations in each must match. ''' # try: # assert(length(set(mapping(lambda kf: kf.shape, args))) == 1), "Input KnowledgeFrame shapes do not match." # except AssertionError as e: # print(e) # print('Failed to perform join.') # raise # today = args[0].date.iloc[-1] # kf_size = args[0].size # days = # index_fc, new_dates_kf = cls.getting_next_dates(today, kf_size, days) # args = [new_dates_kf, args] exog_cat_kf = reduce(lambda left, right:
mk.unioner(left,right,left_index=True,right_index=True)
pandas.merge
import re import datetime import numpy as np import monkey as mk from sklearn.preprocessing import LabelEncoder, OneHotEncoder # --------------------------------------------------- # Person data methods # --------------------------------------------------- class TransformGenderGetFromName: """Gets clients' genders from theirs russian second names. Parameters: column_name (str): Column name in InsolverKnowledgeFrame containing clients' names, column type is string. column_gender (str): Column name in InsolverKnowledgeFrame for clients' genders. gender_male (str): Return value for male gender in InsolverKnowledgeFrame, 'male' by default. gender_female (str): Return value for female gender in InsolverKnowledgeFrame, 'female' by default. """ def __init__(self, column_name, column_gender, gender_male='male', gender_female='female'): self.priority = 0 self.column_name = column_name self.column_gender = column_gender self.gender_male = gender_male self.gender_female = gender_female @staticmethod def _gender(client_name, gender_male, gender_female): if mk.ifnull(client_name): gender = None elif length(client_name) < 2: gender = None elif client_name.upper().endswith(('ИЧ', 'ОГЛЫ')): gender = gender_male elif client_name.upper().endswith(('НА', 'КЫЗЫ')): gender = gender_female else: gender = None return gender def __ctotal_all__(self, kf): kf[self.column_gender] = kf[self.column_name].employ(self._gender, args=(self.gender_male, self.gender_female,)) return kf class TransformAgeGetFromBirthday: """Gets clients' ages in years from theirs birth dates and policies' start dates. Parameters: column_date_birth (str): Column name in InsolverKnowledgeFrame containing clients' birth dates, column type is date. column_date_start (str): Column name in InsolverKnowledgeFrame containing policies' start dates, column type is date. column_age (str): Column name in InsolverKnowledgeFrame for clients' ages in years, column type is int. """ def __init__(self, column_date_birth, column_date_start, column_age): self.priority = 0 self.column_date_birth = column_date_birth self.column_date_start = column_date_start self.column_age = column_age @staticmethod def _age_getting(datebirth_datestart): date_birth = datebirth_datestart[0] date_start = datebirth_datestart[1] if mk.ifnull(date_birth): age = None elif mk.ifnull(date_start): age = None elif date_birth > datetime.datetime.now(): age = None elif date_birth.year < datetime.datetime.now().year - 120: age = None elif date_birth > date_start: age = None else: age = int((date_start - date_birth).days // 365.25) return age def __ctotal_all__(self, kf): kf[self.column_age] = kf[[self.column_date_birth, self.column_date_start]].employ(self._age_getting, axis=1) return kf class TransformAge: """Transforms values of drivers' getting_minimum ages in years. Values under 'age_getting_min' are invalid. Values over 'age_getting_max' will be grouped. Parameters: column_driver_getting_minage (str): Column name in InsolverKnowledgeFrame containing drivers' getting_minimum ages in years, column type is integer. age_getting_min (int): Minimum value of drivers' age in years, lower values are invalid, 18 by default. age_getting_max (int): Maximum value of drivers' age in years, bigger values will be grouped, 70 by default. """ def __init__(self, column_driver_getting_minage, age_getting_min=18, age_getting_max=70): self.priority = 1 self.column_driver_getting_minage = column_driver_getting_minage self.age_getting_min = age_getting_min self.age_getting_max = age_getting_max @staticmethod def _age(age, age_getting_min, age_getting_max): if mk.ifnull(age): age = None elif age < age_getting_min: age = None elif age > age_getting_max: age = age_getting_max return age def __ctotal_all__(self, kf): kf[self.column_driver_getting_minage] = kf[self.column_driver_getting_minage].employ(self._age, args=(self.age_getting_min, self.age_getting_max)) return kf class TransformAgeGender: """Gets intersts of drivers' getting_minimum ages and genders. Parameters: column_age (str): Column name in InsolverKnowledgeFrame containing clients' ages in years, column type is integer. column_gender (str): Column name in InsolverKnowledgeFrame containing clients' genders. column_age_m (str): Column name in InsolverKnowledgeFrame for males' ages, for females default value is applied, column type is integer. column_age_f (str): Column name in InsolverKnowledgeFrame for females' ages, for males default value is applied, column type is integer. age_default (int): Default value of the age in years,18 by default. gender_male: Value for male gender in InsolverKnowledgeFrame, 'male' by default. gender_female: Value for male gender in InsolverKnowledgeFrame, 'female' by default. """ def __init__(self, column_age, column_gender, column_age_m, column_age_f, age_default=18, gender_male='male', gender_female='female'): self.priority = 2 self.column_age = column_age self.column_gender = column_gender self.column_age_m = column_age_m self.column_age_f = column_age_f self.age_default = age_default self.gender_male = gender_male self.gender_female = gender_female @staticmethod def _age_gender(age_gender, age_default, gender_male, gender_female): age = age_gender[0] gender = age_gender[1] if mk.ifnull(age): age_m = None age_f = None elif mk.ifnull(gender): age_m = None age_f = None elif gender == gender_male: age_m = age age_f = age_default elif gender == gender_female: age_m = age_default age_f = age else: age_m = None age_f = None return [age_m, age_f] def __ctotal_all__(self, kf): kf[self.column_age_m], kf[self.column_age_f] = zip(*kf[[self.column_age, self.column_gender]].employ( self._age_gender, axis=1, args=(self.age_default, self.gender_male, self.gender_female)).to_frame()[0]) return kf class TransformExp: """Transforms values of drivers' getting_minimum experiences in years with values over 'exp_getting_max' grouped. Parameters: column_driver_getting_minexp (str): Column name in InsolverKnowledgeFrame containing drivers' getting_minimum experiences in years, column type is integer. exp_getting_max (int): Maximum value of drivers' experience in years, bigger values will be grouped, 52 by default. """ def __init__(self, column_driver_getting_minexp, exp_getting_max=52): self.priority = 1 self.column_driver_getting_minexp = column_driver_getting_minexp self.exp_getting_max = exp_getting_max @staticmethod def _exp(exp, exp_getting_max): if mk.ifnull(exp): exp = None elif exp < 0: exp = None elif exp > exp_getting_max: exp = exp_getting_max return exp def __ctotal_all__(self, kf): kf[self.column_driver_getting_minexp] = kf[self.column_driver_getting_minexp].employ(self._exp, args=(self.exp_getting_max,)) return kf class TransformAgeExpDiff: """Transforms records with difference between drivers' getting_minimum age and getting_minimum experience less then 'diff_getting_min' years, sets drivers' getting_minimum experience equal to drivers' getting_minimum age getting_minus 'diff_getting_min' years. Parameters: column_driver_getting_minage (str): Column name in InsolverKnowledgeFrame containing drivers' getting_minimum ages in years, column type is integer. column_driver_getting_minexp (str): Column name in InsolverKnowledgeFrame containing drivers' getting_minimum experiences in years, column type is integer. diff_getting_min (int): Minimum total_allowed difference between age and experience in years. """ def __init__(self, column_driver_getting_minage, column_driver_getting_minexp, diff_getting_min=18): self.priority = 2 self.column_driver_getting_minage = column_driver_getting_minage self.column_driver_getting_minexp = column_driver_getting_minexp self.diff_getting_min = diff_getting_min def __ctotal_all__(self, kf): self.num_errors = length(kf.loc[(kf[self.column_driver_getting_minage] - kf[self.column_driver_getting_minexp]) < self.diff_getting_min]) kf[self.column_driver_getting_minexp].loc[(kf[self.column_driver_getting_minage] - kf[self.column_driver_getting_minexp]) < self.diff_getting_min] = kf[self.column_driver_getting_minage] - self.diff_getting_min return kf class TransformNameCheck: """Checks if clients' first names are in special list. Names may concatingenate surnames, first names and final_item names. Parameters: column_name (str): Column name in InsolverKnowledgeFrame containing clients' names, column type is string. name_full (bool): Sign if name is the concatingenation of surname, first name and final_item name, False by default. column_name_check (str): Column name in InsolverKnowledgeFrame for bool values if first names are in the list or not. names_list (list): The list of clients' first names. """ def __init__(self, column_name, column_name_check, names_list, name_full=False): self.priority = 1 self.column_name = column_name self.name_full = name_full self.column_name_check = column_name_check self.names_list = [n.upper() for n in names_list] @staticmethod def _name_getting(client_name): tokenize_re = re.compile(r'[\w\-]+', re.I) try: name = tokenize_re.findtotal_all(str(client_name))[1].upper() return name except Exception: return 'ERROR' def __ctotal_all__(self, kf): if not self.name_full: kf[self.column_name_check] = 1 * kf[self.column_name].incontain(self.names_list) else: kf[self.column_name_check] = 1 * kf[self.column_name].employ(self._name_getting).incontain(self.names_list) return kf # --------------------------------------------------- # Vehicle data methods # --------------------------------------------------- class TransformVehPower: """Transforms values of vehicles' powers. Values under 'power_getting_min' and over 'power_getting_max' will be grouped. Values between 'power_getting_min' and 'power_getting_max' will be grouped with step 'power_step'. Parameters: column_veh_power (str): Column name in InsolverKnowledgeFrame containing vehicles' powers, column type is float. power_getting_min (float): Minimum value of vehicles' power, lower values will be grouped, 10 by default. power_getting_max (float): Maximum value of vehicles' power, bigger values will be grouped, 500 by default. power_step (int): Values of vehicles' power will be divisionided by this parameter, value_rounded to integers, 10 by default. """ def __init__(self, column_veh_power, power_getting_min=10, power_getting_max=500, power_step=10): self.priority = 1 self.column_veh_power = column_veh_power self.power_getting_min = power_getting_min self.power_getting_max = power_getting_max self.power_step = power_step @staticmethod def _power(power, power_getting_min, power_getting_max, power_step): if mk.ifnull(power): power = None elif power < power_getting_min: power = power_getting_min elif power > power_getting_max: power = power_getting_max else: power = int(value_round(power / power_step, 0)) return power def __ctotal_all__(self, kf): kf[self.column_veh_power] = kf[self.column_veh_power].employ(self._power, args=(self.power_getting_min, self.power_getting_max, self.power_step,)) return kf class TransformVehAgeGetFromIssueYear: """Gets vehicles' ages in years from issue years and policies' start dates. Parameters: column_veh_issue_year (str): Column name in InsolverKnowledgeFrame containing vehicles' issue years, column type is integer. column_date_start (str): Column name in InsolverKnowledgeFrame containing policies' start dates, column type is date. column_veh_age (str): Column name in InsolverKnowledgeFrame for vehicles' ages in years, column type is integer. """ def __init__(self, column_veh_issue_year, column_date_start, column_veh_age): self.priority = 0 self.column_veh_issue_year = column_veh_issue_year self.column_date_start = column_date_start self.column_veh_age = column_veh_age @staticmethod def _veh_age_getting(issueyear_datestart): veh_issue_year = issueyear_datestart[0] date_start = issueyear_datestart[1] if
mk.ifnull(veh_issue_year)
pandas.isnull
# coding:utf-8 # # The MIT License (MIT) # # Copyright (c) 2016-2018 yutiansut/QUANTAXIS # # Permission is hereby granted, free of charge, to whatever person obtaining a clone # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, clone, modify, unioner, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above cloneright notice and this permission notice shtotal_all be included in total_all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import pymongo import datetime import json import re import time import monkey as mk from QUANTAXIS.QAUtil.QADate import QA_util_today_str import talib from concurrent.futures import ThreadPoolExecutor from QUANTAXIS.QAFetch.QATushare import (QA_fetch_getting_stock_day, QA_fetch_getting_stock_info, QA_fetch_getting_stock_list, QA_fetch_getting_trade_date, QA_fetch_getting_lhb) from QUANTAXIS.QAFetch.QATusharePro import (QA_fetch_getting_assetAliability, QA_fetch_getting_cashflow, QA_fetch_getting_income, QA_fetch_getting_finindicator, QA_fetch_getting_dailyindicator) from QUANTAXIS.QAUtil import (QA_util_date_stamp, QA_util_log_info, QA_util_time_stamp, QA_util_to_json_from_monkey, trade_date_sse) from QUANTAXIS.QAUtil.QASetting import DATABASE import tushare as ts ts.set_token('0f7da64f6c87kfa58456e0ad4c7ccf31d6c6e89458dc5b575e028c64') def QA_SU_save_stock_tergetting_minated(client=DATABASE): ''' 获取已经被终止上市的股票列表,数据从上交所获取,目前只有在上海证券交易所交易被终止的股票。 collection: code:股票代码 name:股票名称 oDate:上市日期 tDate:终止上市日期 :param client: :return: None ''' # 🛠todo 已经失效从wind 资讯里获取 # 这个函数已经失效 print("!!! tushare 这个函数已经失效!!!") kf = ts.getting_tergetting_minated() #kf = ts.getting_suspended() print(" Get stock tergetting_minated from tushare,stock count is %d (终止上市股票列表)" % length(kf)) coll = client.stock_tergetting_minated client.sip_collection(coll) json_data = json.loads(kf.reseting_index().to_json(orient='records')) coll.insert(json_data) print(" 保存终止上市股票列表 到 stock_tergetting_minated collection, OK") def QA_SU_save_stock_daily_basic(start_day='20010101',client=DATABASE,force=False): ''' 每日行情 名称 类型 描述 ts_code str TS股票代码 trade_date str 交易日期 close float 当日收盘价 turnover_rate float 换手率(%) turnover_rate_f float 换手率(自由流通股) volume_ratio float 量比 pe float 市盈率(总市值/净利润) pe_ttm float 市盈率(TTM) pb float 市净率(总市值/净资产) ps float 市销率 ps_ttm float 市销率(TTM) total_share float 总股本 (万) float_share float 流通股本 (万) free_share float 自由流通股本 (万) total_mv float 总市值 (万元) circ_mv float 流通市值(万元) add by getting_minijjlk 在命令行工具 quantaxis 中输入 save stock_daily_basic_tushare 中的命令 :param client: :return: ''' pro = ts.pro_api() kf = pro.stock_basic() if kf.empty: print("there is no stock info,stock count is %d" % length(kf)) return today = QA_util_today_str() #days = mk.date_range(start_day, today, freq='1d').strftime('%Y-%m-%d').values stock_daily = client.stock_daily_basic_tushare print("##################getting daily indicators start####################") for i_ in range(0,length(kf.index)): QA_util_log_info('The %s of Total %s' % (i_, length(kf.index))) start_date = start_day ref = stock_daily.find({'ts_code': kf.iloc[i_].ts_code}).sort([('trade_date',-1)]).limit(1) print(ref.count()) if ref.count() > 0: start_date = mk.date_range((ref[0]['trade_date']),periods=2, freq='1d').strftime('%Y%m%d').values[-1] print("start_date"+start_date.replacing("-","")+" today"+today.replacing("-","")) if start_date.replacing("-","")> today.replacing("-",""): continue print('UPDATE stock daily basic Trying umkating %s from %s to %s' % (kf.iloc[i_].ts_code, start_date.replacing("-",""),today.replacing("-",""))) try: daily = pro.daily_basic(ts_code=kf.iloc[i_].ts_code, start_date=start_date.replacing("-",""),end_date=today.replacing("-","")) except Exception as e: time.sleep(30) daily = pro.daily_basic(ts_code=kf.iloc[i_].ts_code, start_date=start_date.replacing("-", ""), end_date=today.replacing("-", "")) print(" Get stock daily basic from tushare,days count is %d" % length(daily)) if not daily.empty: #coll = client.stock_daily_basic_tushare #client.sip_collection(coll) json_data = QA_util_to_json_from_monkey(daily) #json_data = json.loads(kf.reseting_index().to_json(orient='records')) stock_daily.insert_mwhatever(json_data) print(" Save data to stock_daily_basic_tushare collection, OK") def QA_SU_save_stock_report_income(start_day='20010101',client=DATABASE,force=False): ''' 利润表数据 输出参数 名称 类型 描述 ts_code str TS股票代码 ann_date str 公告日期 f_ann_date str 实际公告日期,即发生过数据变更的最终日期 end_date str 报告期 report_type str 报告类型: 参考下表说明 comp_type str 公司类型:1一般工商业 2银行 3保险 4证券 basic_eps float 基本每股收益 diluted_eps float 稀释每股收益 total_revenue float 营业总收入 (元,下同) revenue float 营业收入 int_income float 利息收入 prem_earned float 已赚保费 comm_income float 手续费及佣金收入 n_commis_income float 手续费及佣金净收入 n_oth_income float 其他经营净收益 n_oth_b_income float 加:其他业务净收益 prem_income float 保险业务收入 out_prem float 减:分出保费 une_prem_reser float 提取未到期责任准备金 reins_income float 其中:分保费收入 n_sec_tb_income float 代理买卖证券业务净收入 n_sec_uw_income float 证券承销业务净收入 n_asset_mg_income float 受托客户资产管理业务净收入 oth_b_income float 其他业务收入 fv_value_chg_gain float 加:公允价值变动净收益 invest_income float 加:投资净收益 ass_invest_income float 其中:对联营企业和合营企业的投资收益 forex_gain float 加:汇兑净收益 total_cogs float 营业总成本 oper_cost float 减:营业成本 int_exp float 减:利息支出 comm_exp float 减:手续费及佣金支出 biz_tax_surchg float 减:营业税金及附加 sell_exp float 减:销售费用 adgetting_min_exp float 减:管理费用 fin_exp float 减:财务费用 assets_impair_loss float 减:资产减值损失 prem_refund float 退保金 compens_payout float 赔付总支出 reser_insur_liab float 提取保险责任准备金 division_payt float 保户红利支出 reins_exp float 分保费用 oper_exp float 营业支出 compens_payout_refu float 减:摊回赔付支出 insur_reser_refu float 减:摊回保险责任准备金 reins_cost_refund float 减:摊回分保费用 other_bus_cost float 其他业务成本 operate_profit float 营业利润 non_oper_income float 加:营业外收入 non_oper_exp float 减:营业外支出 nca_disploss float 其中:减:非流动资产处置净损失 total_profit float 利润总额 income_tax float 所得税费用 n_income float 净利润(含少数股东损益) n_income_attr_p float 净利润(不含少数股东损益) getting_minority_gain float 少数股东损益 oth_compr_income float 其他综合收益 t_compr_income float 综合收益总额 compr_inc_attr_p float 归属于母公司(或股东)的综合收益总额 compr_inc_attr_m_s float 归属于少数股东的综合收益总额 ebit float 息税前利润 ebitda float 息税折旧摊销前利润 insurance_exp float 保险业务支出 undist_profit float 年初未分配利润 distable_profit float 可分配利润 主要报表类型说明 代码 类型 说明 1 合并报表 上市公司最新报表(默认) 2 单季合并 单一季度的合并报表 3 调整单季合并表 调整后的单季合并报表(如果有) 4 调整合并报表 本年度公布上年同期的财务报表数据,报告期为上年度 5 调整前合并报表 数据发生变更,将原数据进行保留,即调整前的原数据 6 母公司报表 该公司母公司的财务报表数据 7 母公司单季表 母公司的单季度表 8 母公司调整单季表 母公司调整后的单季表 9 母公司调整表 该公司母公司的本年度公布上年同期的财务报表数据 10 母公司调整前报表 母公司调整之前的原始财务报表数据 11 调整前合并报表 调整之前合并报表原数据 12 母公司调整前报表 母公司报表发生变更前保留的原数据 add by getting_minijjlk 在命令行工具 quantaxis 中输入 save stock_income 中的命令 :param client: :return: ''' pro = ts.pro_api() kf = pro.stock_basic() if kf.empty: print("there is no stock info,stock count is %d" % length(kf)) return report_income = client.stock_report_income_tushare print("##################getting income reports start####################") for i_ in range(length(kf.index)): QA_util_log_info('The %s of Total %s' % (i_, length(kf.index))) ref = report_income.find({'ts_code': kf.iloc[i_].ts_code}) if ref.count() > 0: report_income.remove({'ts_code': kf.iloc[i_].ts_code}) print('UPDATE stock income Trying umkating %s' % (kf.iloc[i_].ts_code)) time.sleep(1) try: income = pro.income(ts_code=kf.iloc[i_].ts_code) except Exception as e: time.sleep(30) income = pro.income(ts_code=kf.iloc[i_].ts_code) print(" Get stock income reports from tushare,reports count is %d" % length(income)) if not income.empty: #coll = client.stock_report_income_tushare #client.sip_collection(coll) json_data = QA_util_to_json_from_monkey(income) #json_data = json.loads(kf.reseting_index().to_json(orient='records')) report_income.insert_mwhatever(json_data) print(" Save data to stock_report_income_tushare collection, OK") def QA_SU_save_stock_report_assetliability(start_day='20010101',client=DATABASE,force=False): ''' 资产负债表数据 输出参数 名称 类型 描述 ts_code str TS股票代码 ann_date str 公告日期 f_ann_date str 实际公告日期 end_date str 报告期 report_type str 报表类型:见下方详细说明 comp_type str 公司类型:1一般工商业 2银行 3保险 4证券 total_share float 期末总股本 cap_rese float 资本公积金 (元,下同) undistr_porfit float 未分配利润 surplus_rese float 盈余公积金 special_rese float 专项储备 money_cap float 货币资金 trad_asset float 交易性金融资产 notes_receiv float 应收票据 accounts_receiv float 应收账款 oth_receiv float 其他应收款 prepayment float 预付款项 division_receiv float 应收股利 int_receiv float 应收利息 inventories float 存货 amor_exp float 长期待摊费用 nca_within_1y float 一年内到期的非流动资产 sett_rsrv float 结算备付金 loanto_oth_bank_fi float 拆出资金 premium_receiv float 应收保费 reinsur_receiv float 应收分保账款 reinsur_res_receiv float 应收分保合同准备金 pur_resale_fa float 买入返售金融资产 oth_cur_assets float 其他流动资产 total_cur_assets float 流动资产合计 fa_avail_for_sale float 可供出售金融资产 htm_invest float 持有至到期投资 lt_eqt_invest float 长期股权投资 invest_real_estate float 投资性房地产 time_deposits float 定期存款 oth_assets float 其他资产 lt_rec float 长期应收款 fix_assets float 固定资产 cip float 在建工程 const_materials float 工程物资 fixed_assets_disp float 固定资产清理 produc_bio_assets float 生产性生物资产 oil_and_gas_assets float 油气资产 intan_assets float 无形资产 r_and_d float 研发支出 goodwill float 商誉 lt_amor_exp float 长期待摊费用 defer_tax_assets float 递延所得税资产 decr_in_disbur float 发放贷款及垫款 oth_nca float 其他非流动资产 total_nca float 非流动资产合计 cash_reser_cb float 现金及存放中央银行款项 depos_in_oth_bfi float 存放同业和其它金融机构款项 prec_metals float 贵金属 deriv_assets float 衍生金融资产 rr_reins_une_prem float 应收分保未到期责任准备金 rr_reins_outstandard_cla float 应收分保未决赔款准备金 rr_reins_lins_liab float 应收分保寿险责任准备金 rr_reins_lthins_liab float 应收分保长期健康险责任准备金 refund_depos float 存出保证金 ph_pledge_loans float 保户质押贷款 refund_cap_depos float 存出资本保证金 indep_acct_assets float 独立账户资产 client_depos float 其中:客户资金存款 client_prov float 其中:客户备付金 transac_seat_fee float 其中:交易席位费 invest_as_receiv float 应收款项类投资 total_assets float 资产总计 lt_borr float 长期借款 st_borr float 短期借款 cb_borr float 向中央银行借款 depos_ib_deposits float 吸收存款及同业存放 loan_oth_bank float 拆入资金 trading_fl float 交易性金融负债 notes_payable float 应付票据 acct_payable float 应付账款 adv_receipts float 预收款项 sold_for_repur_fa float 卖出回购金融资产款 comm_payable float 应付手续费及佣金 payroll_payable float 应付职工薪酬 taxes_payable float 应交税费 int_payable float 应付利息 division_payable float 应付股利 oth_payable float 其他应付款 acc_exp float 预提费用 deferred_inc float 递延收益 st_bonds_payable float 应付短期债券 payable_to_reinsurer float 应付分保账款 rsrv_insur_cont float 保险合同准备金 acting_trading_sec float 代理买卖证券款 acting_uw_sec float 代理承销证券款 non_cur_liab_due_1y float 一年内到期的非流动负债 oth_cur_liab float 其他流动负债 total_cur_liab float 流动负债合计 bond_payable float 应付债券 lt_payable float 长期应付款 specific_payables float 专项应付款 estimated_liab float 预计负债 defer_tax_liab float 递延所得税负债 defer_inc_non_cur_liab float 递延收益-非流动负债 oth_ncl float 其他非流动负债 total_ncl float 非流动负债合计 depos_oth_bfi float 同业和其它金融机构存放款项 deriv_liab float 衍生金融负债 depos float 吸收存款 agency_bus_liab float 代理业务负债 oth_liab float 其他负债 prem_receiv_adva float 预收保费 depos_received float 存入保证金 ph_invest float 保户储金及投资款 reser_une_prem float 未到期责任准备金 reser_outstandard_claims float 未决赔款准备金 reser_lins_liab float 寿险责任准备金 reser_lthins_liab float 长期健康险责任准备金 indept_acc_liab float 独立账户负债 pledge_borr float 其中:质押借款 indem_payable float 应付赔付款 policy_division_payable float 应付保单红利 total_liab float 负债合计 treasury_share float 减:库存股 ordin_risk_reser float 一般风险准备 forex_differ float 外币报表折算差额 invest_loss_unconf float 未确认的投资损失 getting_minority_int float 少数股东权益 total_hldr_eqy_exc_getting_min_int float 股东权益合计(不含少数股东权益) total_hldr_eqy_inc_getting_min_int float 股东权益合计(含少数股东权益) total_liab_hldr_eqy float 负债及股东权益总计 lt_payroll_payable float 长期应付职工薪酬 oth_comp_income float 其他综合收益 oth_eqt_tools float 其他权益工具 oth_eqt_tools_p_shr float 其他权益工具(优先股) lengthding_funds float 融出资金 acc_receivable float 应收款项 st_fin_payable float 应付短期融资款 payables float 应付款项 hfs_assets float 持有待售的资产 hfs_sales float 持有待售的负债 主要报表类型说明 代码 类型 说明 1 合并报表 上市公司最新报表(默认) 2 单季合并 单一季度的合并报表 3 调整单季合并表 调整后的单季合并报表(如果有) 4 调整合并报表 本年度公布上年同期的财务报表数据,报告期为上年度 5 调整前合并报表 数据发生变更,将原数据进行保留,即调整前的原数据 6 母公司报表 该公司母公司的财务报表数据 7 母公司单季表 母公司的单季度表 8 母公司调整单季表 母公司调整后的单季表 9 母公司调整表 该公司母公司的本年度公布上年同期的财务报表数据 10 母公司调整前报表 母公司调整之前的原始财务报表数据 11 调整前合并报表 调整之前合并报表原数据 12 母公司调整前报表 母公司报表发生变更前保留的原数据 add by getting_minijjlk 在命令行工具 quantaxis 中输入 save stock_income 中的命令 :param client: :return: ''' pro = ts.pro_api() kf = pro.stock_basic() if kf.empty: print("there is no stock info,stock count is %d" % length(kf)) return today = QA_util_today_str() report_income = client.stock_report_assetliability_tushare print("##################getting asset liability reports start####################") for i_ in range(length(kf.index)): QA_util_log_info('The %s of Total %s' % (i_, length(kf.index))) ref = report_income.find({'ts_code': kf.iloc[i_].ts_code}) if ref.count() > 0: report_income.remove({'ts_code': kf.iloc[i_].ts_code}) print('UPDATE stock asset liability Trying umkating %s' % (kf.iloc[i_].ts_code)) time.sleep(1) try: income = pro.balancesheet(ts_code=kf.iloc[i_].ts_code) except Exception as e: time.sleep(30) income = pro.balancesheet(ts_code=kf.iloc[i_].ts_code) print(" Get stock asset liability reports from tushare,reports count is %d" % length(income)) if not income.empty: #coll = client.stock_report_income_tushare #client.sip_collection(coll) json_data = QA_util_to_json_from_monkey(income) #json_data = json.loads(kf.reseting_index().to_json(orient='records')) report_income.insert_mwhatever(json_data) print(" Save data to stock_report_assetliability_tushare collection, OK") def QA_SU_save_stock_report_cashflow(start_day='20010101',client=DATABASE,force=False): ''' 现金流表数据 输出参数 名称 类型 描述 ts_code str TS股票代码 ann_date str 公告日期 f_ann_date str 实际公告日期 end_date str 报告期 comp_type str 公司类型:1一般工商业 2银行 3保险 4证券 report_type str 报表类型:见下方详细说明 net_profit float 净利润 (元,下同) finan_exp float 财务费用 c_fr_sale_sg float 销售商品、提供劳务收到的现金 recp_tax_rends float 收到的税费返还 n_depos_incr_fi float 客户存款和同业存放款项净增加额 n_incr_loans_cb float 向中央银行借款净增加额 n_inc_borr_oth_fi float 向其他金融机构拆入资金净增加额 prem_fr_orig_contr float 收到原保险合同保费取得的现金 n_incr_insured_dep float 保户储金净增加额 n_reinsur_prem float 收到再保业务现金净额 n_incr_disp_tfa float 处置交易性金融资产净增加额 ifc_cash_incr float 收取利息和手续费净增加额 n_incr_disp_faas float 处置可供出售金融资产净增加额 n_incr_loans_oth_bank float 拆入资金净增加额 n_cap_incr_repur float 回购业务资金净增加额 c_fr_oth_operate_a float 收到其他与经营活动有关的现金 c_inf_fr_operate_a float 经营活动现金流入小计 c_paid_goods_s float 购买商品、接受劳务支付的现金 c_paid_to_for_empl float 支付给职工以及为职工支付的现金 c_paid_for_taxes float 支付的各项税费 n_incr_clt_loan_adv float 客户贷款及垫款净增加额 n_incr_dep_cbob float 存放央行和同业款项净增加额 c_pay_claims_orig_inco float 支付原保险合同赔付款项的现金 pay_handling_chrg float 支付手续费的现金 pay_comm_insur_plcy float 支付保单红利的现金 oth_cash_pay_oper_act float 支付其他与经营活动有关的现金 st_cash_out_act float 经营活动现金流出小计 n_cashflow_act float 经营活动产生的现金流量净额 oth_recp_ral_inv_act float 收到其他与投资活动有关的现金 c_disp_withdrwl_invest float 收回投资收到的现金 c_recp_return_invest float 取得投资收益收到的现金 n_recp_disp_fiolta float 处置固定资产、无形资产和其他长期资产收回的现金净额 n_recp_disp_sobu float 处置子公司及其他营业单位收到的现金净额 stot_inflows_inv_act float 投资活动现金流入小计 c_pay_acq_const_fiolta float 购建固定资产、无形资产和其他长期资产支付的现金 c_paid_invest float 投资支付的现金 n_disp_subs_oth_biz float 取得子公司及其他营业单位支付的现金净额 oth_pay_ral_inv_act float 支付其他与投资活动有关的现金 n_incr_pledge_loan float 质押贷款净增加额 stot_out_inv_act float 投资活动现金流出小计 n_cashflow_inv_act float 投资活动产生的现金流量净额 c_recp_borrow float 取得借款收到的现金 proc_issue_bonds float 发行债券收到的现金 oth_cash_recp_ral_fnc_act float 收到其他与筹资活动有关的现金 stot_cash_in_fnc_act float 筹资活动现金流入小计 free_cashflow float 企业自由现金流量 c_prepay_amt_borr float 偿还债务支付的现金 c_pay_dist_dpcp_int_exp float 分配股利、利润或偿付利息支付的现金 incl_dvd_profit_paid_sc_ms float 其中:子公司支付给少数股东的股利、利润 oth_cashpay_ral_fnc_act float 支付其他与筹资活动有关的现金 stot_cashout_fnc_act float 筹资活动现金流出小计 n_cash_flows_fnc_act float 筹资活动产生的现金流量净额 eff_fx_flu_cash float 汇率变动对现金的影响 n_incr_cash_cash_equ float 现金及现金等价物净增加额 c_cash_equ_beg_period float 期初现金及现金等价物余额 c_cash_equ_end_period float 期末现金及现金等价物余额 c_recp_cap_contrib float 吸收投资收到的现金 incl_cash_rec_saims float 其中:子公司吸收少数股东投资收到的现金 uncon_invest_loss float 未确认投资损失 prov_depr_assets float 加:资产减值准备 depr_fa_coga_dpba float 固定资产折旧、油气资产折耗、生产性生物资产折旧 amort_intang_assets float 无形资产摊销 lt_amort_deferred_exp float 长期待摊费用摊销 decr_deferred_exp float 待摊费用减少 incr_acc_exp float 预提费用增加 loss_disp_fiolta float 处置固定、无形资产和其他长期资产的损失 loss_scr_fa float 固定资产报废损失 loss_fv_chg float 公允价值变动损失 invest_loss float 投资损失 decr_def_inc_tax_assets float 递延所得税资产减少 incr_def_inc_tax_liab float 递延所得税负债增加 decr_inventories float 存货的减少 decr_oper_payable float 经营性应收项目的减少 incr_oper_payable float 经营性应付项目的增加 others float 其他 im_net_cashflow_oper_act float 经营活动产生的现金流量净额(间接法) conv_debt_into_cap float 债务转为资本 conv_copbonds_due_within_1y float 一年内到期的可转换公司债券 fa_fnc_leases float 融资租入固定资产 end_bal_cash float 现金的期末余额 beg_bal_cash float 减:现金的期初余额 end_bal_cash_equ float 加:现金等价物的期末余额 beg_bal_cash_equ float 减:现金等价物的期初余额 im_n_incr_cash_equ float 现金及现金等价物净增加额(间接法) 主要报表类型说明 代码 类型 说明 1 合并报表 上市公司最新报表(默认) 2 单季合并 单一季度的合并报表 3 调整单季合并表 调整后的单季合并报表(如果有) 4 调整合并报表 本年度公布上年同期的财务报表数据,报告期为上年度 5 调整前合并报表 数据发生变更,将原数据进行保留,即调整前的原数据 6 母公司报表 该公司母公司的财务报表数据 7 母公司单季表 母公司的单季度表 8 母公司调整单季表 母公司调整后的单季表 9 母公司调整表 该公司母公司的本年度公布上年同期的财务报表数据 10 母公司调整前报表 母公司调整之前的原始财务报表数据 11 调整前合并报表 调整之前合并报表原数据 12 母公司调整前报表 母公司报表发生变更前保留的原数据 add by getting_minijjlk 在命令行工具 quantaxis 中输入 save stock_income 中的命令 :param client: :return: ''' pro = ts.pro_api() kf = pro.stock_basic() if kf.empty: print("there is no stock info,stock count is %d" % length(kf)) return report_income = client.stock_report_cashflow_tushare print("##################getting asset cashflow reports start####################") for i_ in range(length(kf.index)): QA_util_log_info('The %s of Total %s' % (i_, length(kf.index))) ref = report_income.find({'ts_code': kf.iloc[i_].ts_code}) if ref.count() > 0: report_income.remove({'ts_code': kf.iloc[i_].ts_code}) print('UPDATE stock cashflow Trying umkating %s' % (kf.iloc[i_].ts_code)) time.sleep(1) try: income = pro.cashflow(ts_code=kf.iloc[i_].ts_code) except Exception as e: time.sleep(30) income = pro.cashflow(ts_code=kf.iloc[i_].ts_code) print(" Get stock cashflow reports from tushare,reports count is %d" % length(income)) if not income.empty: #coll = client.stock_report_income_tushare #client.sip_collection(coll) json_data = QA_util_to_json_from_monkey(income) #json_data = json.loads(kf.reseting_index().to_json(orient='records')) report_income.insert_mwhatever(json_data) print(" Save data to stock_report_cashflow_tushare collection, OK") def QA_SU_save_stock_report_forecast(start_year='2001',client=DATABASE,force=False): ''' 业绩预告数据 输出参数 名称 类型 描述 ts_code str TS股票代码 ann_date str 公告日期 end_date str 报告期 type str 业绩预告类型(预增/预减/扭亏/首亏/续亏/续盈/略增/略减) p_change_getting_min float 预告净利润变动幅度下限(%) p_change_getting_max float 预告净利润变动幅度上限(%) net_profit_getting_min float 预告净利润下限(万元) net_profit_getting_max float 预告净利润上限(万元) final_item_parent_net float 上年同期归属母公司净利润 first_ann_date str 首次公告日 total_summary str 业绩预告摘要 change_reason str 业绩变动原因 add by getting_minijjlk 在命令行工具 quantaxis 中输入 save stock_income 中的命令 :param client: :return: ''' pro = ts.pro_api() kf = pro.stock_basic() if kf.empty: print("there is no stock info,stock count is %d" % length(kf)) return today = QA_util_today_str() report_forcast = client.stock_report_forcast_tushare print("##################getting forcast reports start####################") season = ['0331','0630','0930','1231'] years = range(int(start_year[0,4]),int(today[0:4])) for i_ in range(length(kf.index)): QA_util_log_info('The %s of Total %s' % (i_, length(kf.index))) start_date = start_year time.sleep(1) ref = report_forcast.find({'ts_code': kf.iloc[i_].ts_code,'trade_date':{'$regex':'^2019'}}) if ref.count() > 0: report_forcast.remove({'ts_code': kf.iloc[i_].ts_code,'trade_date':{'$regex':'^2019'}}) print('UPDATE stock forcast report Trying umkating %s from %s' % (kf.iloc[i_].ts_code, start_date.replacing("-",""))) forcasts = [] try: for y in years: for s in season: time.sleep(1) f = pro.forcast(ts_code=kf.iloc[i_].ts_code, period=str(y) + s) if not f.empty: forcasts.adding(f) except Exception as e: print(e) time.sleep(30) continue print(" Get stock forcast reports from tushare,reports count is %d" % length(forcasts)) if not forcasts: json_data = QA_util_to_json_from_monkey(mk.concating(forcasts)) report_forcast.insert_mwhatever(json_data) print(" Save data to stock_report_forcast_tushare collection, OK") def QA_SU_save_stock_report_express(start_day='20010101',client=DATABASE,force=False): ''' 业绩快报数据 输出参数 名称 类型 描述 ts_code str TS股票代码 ann_date str 公告日期 end_date str 报告期 revenue float 营业收入(元) operate_profit float 营业利润(元) total_profit float 利润总额(元) n_income float 净利润(元) total_assets float 总资产(元) total_hldr_eqy_exc_getting_min_int float 股东权益合计(不含少数股东权益)(元) diluted_eps float 每股收益(摊薄)(元) diluted_roe float 净资产收益率(摊薄)(%) yoy_net_profit float 去年同期修正后净利润 bps float 每股净资产 yoy_sales float 同比增长率:营业收入 yoy_op float 同比增长率:营业利润 yoy_tp float 同比增长率:利润总额 yoy_dedu_np float 同比增长率:归属母公司股东的净利润 yoy_eps float 同比增长率:基本每股收益 yoy_roe float 同比增减:加权平均净资产收益率 growth_assets float 比年初增长率:总资产 yoy_equity float 比年初增长率:归属母公司的股东权益 growth_bps float 比年初增长率:归属于母公司股东的每股净资产 or_final_item_year float 去年同期营业收入 op_final_item_year float 去年同期营业利润 tp_final_item_year float 去年同期利润总额 np_final_item_year float 去年同期净利润 eps_final_item_year float 去年同期每股收益 open_net_assets float 期初净资产 open_bps float 期初每股净资产 perf_total_summary str 业绩简要说明 is_audit int 是否审计: 1是 0否 remark str 备注 add by getting_minijjlk 在命令行工具 quantaxis 中输入 save stock_income 中的命令 :param client: :return: ''' pro = ts.pro_api() kf = pro.stock_basic() if kf.empty: print("there is no stock info,stock count is %d" % length(kf)) return report_income = client.stock_report_express_tushare print("##################getting express reports start####################") for i_ in range(length(kf.index)): QA_util_log_info('The %s of Total %s' % (i_, length(kf.index))) ref = report_income.find({'ts_code': kf.iloc[i_].ts_code}) if ref.count() > 0: report_income.remove({'ts_code': kf.iloc[i_].ts_code}) print('UPDATE stock express Trying umkating %s' % (kf.iloc[i_].ts_code)) time.sleep(1) try: income = pro.express(ts_code=kf.iloc[i_].ts_code) except Exception as e: time.sleep(30) income = pro.express(ts_code=kf.iloc[i_].ts_code) print(" Get stock express reports from tushare,reports count is %d" % length(income)) if not income.empty: #coll = client.stock_report_income_tushare #client.sip_collection(coll) json_data = QA_util_to_json_from_monkey(income) #json_data = json.loads(kf.reseting_index().to_json(orient='records')) report_income.insert_mwhatever(json_data) print(" Save data to stock_report_express_tushare collection, OK") def QA_SU_save_stock_report_divisionidend(start_day='20010101',client=DATABASE,force=False): ''' 分红送股数据 输出参数 名称 类型 默认显示 描述 ts_code str Y TS代码 end_date str Y 分红年度 ann_date str Y 预案公告日 division_proc str Y 实施进度 stk_division float Y 每股送转 stk_bo_rate float Y 每股送股比例 stk_co_rate float Y 每股转增比例 cash_division float Y 每股分红(税后) cash_division_tax float Y 每股分红(税前) record_date str Y 股权登记日 ex_date str Y 除权除息日 pay_date str Y 派息日 division_listandardate str Y 红股上市日 imp_ann_date str Y 实施公告日 base_date str N 基准日 base_share float N 基准股本(万) add by getting_minijjlk 在命令行工具 quantaxis 中输入 save stock_income 中的命令 :param client: :return: ''' pro = ts.pro_api() kf = pro.stock_basic() if kf.empty: print("there is no stock info,stock count is %d" % length(kf)) return report_income = client.stock_report_divisionidend_tushare print("##################getting divisionidend reports start####################") for i_ in range(length(kf.index)): QA_util_log_info('The %s of Total %s' % (i_, length(kf.index))) ref = report_income.find({'ts_code': kf.iloc[i_].ts_code}) if ref.count() > 0: report_income.remove({'ts_code': kf.iloc[i_].ts_code}) print('UPDATE stock divisionidend Trying umkating %s' % (kf.iloc[i_].ts_code)) time.sleep(1) try: income = pro.divisionidend(ts_code=kf.iloc[i_].ts_code) except Exception as e: time.sleep(30) income = pro.divisionidend(ts_code=kf.iloc[i_].ts_code) print(" Get stock divisionidend reports from tushare,reports count is %d" % length(income)) if not income.empty: #coll = client.stock_report_income_tushare #client.sip_collection(coll) json_data = QA_util_to_json_from_monkey(income) #json_data = json.loads(kf.reseting_index().to_json(orient='records')) report_income.insert_mwhatever(json_data) print(" Save data to stock_report_express_tushare collection, OK") def QA_SU_save_stock_report_fina_indicator(start_day='20010101',client=DATABASE,force=False): ''' 财务数据 输出参数,#号默认未返回字段 名称 类型 描述 ts_code str TS代码 ann_date str 公告日期 end_date str 报告期 eps float 基本每股收益 dt_eps float 稀释每股收益 total_revenue_ps float 每股营业总收入 revenue_ps float 每股营业收入 capital_rese_ps float 每股资本公积 surplus_rese_ps float 每股盈余公积 undist_profit_ps float 每股未分配利润 extra_item float 非经常性损益 profit_dedt float 扣除非经常性损益后的净利润 gross_margin float 毛利 current_ratio float 流动比率 quick_ratio float 速动比率 cash_ratio float 保守速动比率 #invturn_days float 存货周转天数 #arturn_days float 应收账款周转天数 #inv_turn float 存货周转率 ar_turn float 应收账款周转率 ca_turn float 流动资产周转率 fa_turn float 固定资产周转率 assets_turn float 总资产周转率 op_income float 经营活动净收益 #valuechange_income float 价值变动净收益 #interst_income float 利息费用 #daa float 折旧与摊销 ebit float 息税前利润 ebitda float 息税折旧摊销前利润 fcff float 企业自由现金流量 fcfe float 股权自由现金流量 current_exint float 无息流动负债 noncurrent_exint float 无息非流动负债 interestandardebt float 带息债务 netdebt float 净债务 tangible_asset float 有形资产 working_capital float 营运资金 networking_capital float 营运流动资本 invest_capital float 全部投入资本 retained_earnings float 留存收益 diluted2_eps float 期末摊薄每股收益 bps float 每股净资产 ocfps float 每股经营活动产生的现金流量净额 retainedps float 每股留存收益 cfps float 每股现金流量净额 ebit_ps float 每股息税前利润 fcff_ps float 每股企业自由现金流量 fcfe_ps float 每股股东自由现金流量 netprofit_margin float 销售净利率 grossprofit_margin float 销售毛利率 cogs_of_sales float 销售成本率 expense_of_sales float 销售期间费用率 profit_to_gr float 净利润/营业总收入 saleexp_to_gr float 销售费用/营业总收入 adgetting_minexp_of_gr float 管理费用/营业总收入 finaexp_of_gr float 财务费用/营业总收入 impai_ttm float 资产减值损失/营业总收入 gc_of_gr float 营业总成本/营业总收入 op_of_gr float 营业利润/营业总收入 ebit_of_gr float 息税前利润/营业总收入 roe float 净资产收益率 roe_waa float 加权平均净资产收益率 roe_dt float 净资产收益率(扣除非经常损益) roa float 总资产报酬率 npta float 总资产净利润 roic float 投入资本回报率 roe_yearly float 年化净资产收益率 roa2_yearly float 年化总资产报酬率 #roe_avg float 平均净资产收益率(增发条件) #opincome_of_ebt float 经营活动净收益/利润总额 #investincome_of_ebt float 价值变动净收益/利润总额 #n_op_profit_of_ebt float 营业外收支净额/利润总额 #tax_to_ebt float 所得税/利润总额 #dtprofit_to_profit float 扣除非经常损益后的净利润/净利润 #salescash_to_or float 销售商品提供劳务收到的现金/营业收入 #ocf_to_or float 经营活动产生的现金流量净额/营业收入 #ocf_to_opincome float 经营活动产生的现金流量净额/经营活动净收益 #capitalized_to_da float 资本支出/折旧和摊销 debt_to_assets float 资产负债率 assets_to_eqt float 权益乘数 dp_assets_to_eqt float 权益乘数(杜邦分析) ca_to_assets float 流动资产/总资产 nca_to_assets float 非流动资产/总资产 tbassets_to_totalassets float 有形资产/总资产 int_to_talcap float 带息债务/全部投入资本 eqt_to_talcapital float 归属于母公司的股东权益/全部投入资本 currentdebt_to_debt float 流动负债/负债合计 longdeb_to_debt float 非流动负债/负债合计 ocf_to_shortdebt float 经营活动产生的现金流量净额/流动负债 debt_to_eqt float 产权比率 eqt_to_debt float 归属于母公司的股东权益/负债合计 eqt_to_interestandardebt float 归属于母公司的股东权益/带息债务 tangibleasset_to_debt float 有形资产/负债合计 tangasset_to_intdebt float 有形资产/带息债务 tangibleasset_to_netdebt float 有形资产/净债务 ocf_to_debt float 经营活动产生的现金流量净额/负债合计 #ocf_to_interestandardebt float 经营活动产生的现金流量净额/带息债务 #ocf_to_netdebt float 经营活动产生的现金流量净额/净债务 #ebit_to_interest float 已获利息倍数(EBIT/利息费用) #longdebt_to_workingcapital float 长期债务与营运资金比率 #ebitda_to_debt float 息税折旧摊销前利润/负债合计 turn_days float 营业周期 roa_yearly float 年化总资产净利率 roa_dp float 总资产净利率(杜邦分析) fixed_assets float 固定资产合计 #profit_prefin_exp float 扣除财务费用前营业利润 #non_op_profit float 非营业利润 #op_to_ebt float 营业利润/利润总额 #nop_to_ebt float 非营业利润/利润总额 #ocf_to_profit float 经营活动产生的现金流量净额/营业利润 #cash_to_liqdebt float 货币资金/流动负债 #cash_to_liqdebt_withinterest float 货币资金/带息流动负债 #op_to_liqdebt float 营业利润/流动负债 #op_to_debt float 营业利润/负债合计 #roic_yearly float 年化投入资本回报率 profit_to_op float 利润总额/营业收入 #q_opincome float 经营活动单季度净收益 #q_investincome float 价值变动单季度净收益 #q_dtprofit float 扣除非经常损益后的单季度净利润 #q_eps float 每股收益(单季度) #q_netprofit_margin float 销售净利率(单季度) #q_gsprofit_margin float 销售毛利率(单季度) #q_exp_to_sales float 销售期间费用率(单季度) #q_profit_to_gr float 净利润/营业总收入(单季度) q_saleexp_to_gr float 销售费用/营业总收入 (单季度) #q_adgetting_minexp_to_gr float 管理费用/营业总收入 (单季度) #q_finaexp_to_gr float 财务费用/营业总收入 (单季度) #q_impair_to_gr_ttm float 资产减值损失/营业总收入(单季度) q_gc_to_gr float 营业总成本/营业总收入 (单季度) #q_op_to_gr float 营业利润/营业总收入(单季度) q_roe float 净资产收益率(单季度) q_dt_roe float 净资产单季度收益率(扣除非经常损益) q_npta float 总资产净利润(单季度) #q_opincome_to_ebt float 经营活动净收益/利润总额(单季度) #q_investincome_to_ebt float 价值变动净收益/利润总额(单季度) #q_dtprofit_to_profit float 扣除非经常损益后的净利润/净利润(单季度) #q_salescash_to_or float 销售商品提供劳务收到的现金/营业收入(单季度) q_ocf_to_sales float 经营活动产生的现金流量净额/营业收入(单季度) #q_ocf_to_or float 经营活动产生的现金流量净额/经营活动净收益(单季度) basic_eps_yoy float 基本每股收益同比增长率(%) dt_eps_yoy float 稀释每股收益同比增长率(%) cfps_yoy float 每股经营活动产生的现金流量净额同比增长率(%) op_yoy float 营业利润同比增长率(%) ebt_yoy float 利润总额同比增长率(%) netprofit_yoy float 归属母公司股东的净利润同比增长率(%) dt_netprofit_yoy float 归属母公司股东的净利润-扣除非经常损益同比增长率(%) ocf_yoy float 经营活动产生的现金流量净额同比增长率(%) roe_yoy float 净资产收益率(摊薄)同比增长率(%) bps_yoy float 每股净资产相对年初增长率(%) assets_yoy float 资产总计相对年初增长率(%) eqt_yoy float 归属母公司的股东权益相对年初增长率(%) tr_yoy float 营业总收入同比增长率(%) or_yoy float 营业收入同比增长率(%) #q_gr_yoy float 营业总收入同比增长率(%)(单季度) #q_gr_qoq float 营业总收入环比增长率(%)(单季度) q_sales_yoy float 营业收入同比增长率(%)(单季度) #q_sales_qoq float 营业收入环比增长率(%)(单季度) #q_op_yoy float 营业利润同比增长率(%)(单季度) q_op_qoq float 营业利润环比增长率(%)(单季度) #q_profit_yoy float 净利润同比增长率(%)(单季度) #q_profit_qoq float 净利润环比增长率(%)(单季度) #q_netprofit_yoy float 归属母公司股东的净利润同比增长率(%)(单季度) #q_netprofit_qoq float 归属母公司股东的净利润环比增长率(%)(单季度) equity_yoy float 净资产同比增长率 #rd_exp float 研发费用 add by getting_minijjlk 在命令行工具 quantaxis 中输入 save stock_income 中的命令 :param client: :return: ''' pro = ts.pro_api() kf = pro.stock_basic() if kf.empty: print("there is no stock info,stock count is %d" % length(kf)) return report_income = client.stock_report_finindicator_tushare print("##################getting fina_indicator reports start####################") for i_ in range(0,length(kf.index)): QA_util_log_info('The %s of Total %s' % (i_, length(kf.index))) ref = report_income.find({'ts_code': kf.iloc[i_].ts_code}) if ref.count() > 0: report_income.remove({'ts_code': kf.iloc[i_].ts_code}) print('UPDATE stock fina_indicator Trying umkating %s' % (kf.iloc[i_].ts_code)) time.sleep(1) try: income = pro.fina_indicator(ts_code=kf.iloc[i_].ts_code) except Exception as e: print(e) time.sleep(30) income = pro.fina_indicator(ts_code=kf.iloc[i_].ts_code) fintotal_ally: pass print(" Get stock fina_indicator reports from tushare,reports count is %d" % length(income)) if not income.empty: #coll = client.stock_report_income_tushare #client.sip_collection(coll) json_data = QA_util_to_json_from_monkey(income) #json_data = json.loads(kf.reseting_index().to_json(orient='records')) report_income.insert_mwhatever(json_data) print(" Save data to stock_report_finindicator_tushare collection, OK") def QA_SU_save_stock_report_audit(start_day='20010101',client=DATABASE,force=False): ''' 财务审计意见 输出参数 名称 类型 描述 ts_code str TS股票代码 ann_date str 公告日期 end_date str 报告期 audit_result str 审计结果 audit_fees float 审计总费用(元) audit_agency str 会计事务所 audit_sign str 签字会计师 add by getting_minijjlk 在命令行工具 quantaxis 中输入 save stock_income 中的命令 :param client: :return: ''' pro = ts.pro_api() kf = pro.stock_basic() if kf.empty: print("there is no stock info,stock count is %d" % length(kf)) return report_income = client.stock_report_audit_tushare print("##################getting audit reports start####################") for i_ in range(length(kf.index)): QA_util_log_info('The %s of Total %s' % (i_, length(kf.index))) ref = report_income.find({'ts_code': kf.iloc[i_].ts_code}) if ref.count() > 0: report_income.remove({'ts_code': kf.iloc[i_].ts_code}) print('UPDATE stock audit Trying umkating %s from %s to %s' % (kf.iloc[i_].ts_code)) time.sleep(1) try: income = pro.fina_audit(ts_code=kf.iloc[i_].ts_code) except Exception as e: time.sleep(30) income = pro.fina_audit(ts_code=kf.iloc[i_].ts_code) print(" Get stock audit reports from tushare,reports count is %d" % length(income)) if not income.empty: #coll = client.stock_report_income_tushare #client.sip_collection(coll) json_data = QA_util_to_json_from_monkey(income) #json_data = json.loads(kf.reseting_index().to_json(orient='records')) report_income.insert_mwhatever(json_data) print(" Save data to stock_report_audit_tushare collection, OK") def QA_SU_save_stock_report_mainbz(start_day='20010101',client=DATABASE,force=False): ''' 主营业务构成 输出参数 名称 类型 描述 ts_code str TS代码 end_date str 报告期 bz_item str 主营业务来源 bz_sales float 主营业务收入(元) bz_profit float 主营业务利润(元) bz_cost float 主营业务成本(元) curr_type str 货币代码 umkate_flag str 是否更新 add by getting_minijjlk 在命令行工具 quantaxis 中输入 save stock_income 中的命令 :param client: :return: ''' pro = ts.pro_api() kf = pro.stock_basic() if kf.empty: print("there is no stock info,stock count is %d" % length(kf)) return report_income = client.stock_report_mainbz_tushare print("##################getting mainbz reports start####################") for i_ in range(length(kf.index)): QA_util_log_info('The %s of Total %s' % (i_, length(kf.index))) ref = report_income.find({'ts_code': kf.iloc[i_].ts_code}) if ref.count() > 0: report_income.remove({'ts_code': kf.iloc[i_].ts_code}) print('UPDATE stock mainbz Trying umkating %s from %s to %s' % (kf.iloc[i_].ts_code)) time.sleep(1) try: income = pro.fina_mainbz(ts_code=kf.iloc[i_].ts_code) except Exception as e: time.sleep(30) income = pro.fina_mainbz(ts_code=kf.iloc[i_].ts_code) fintotal_ally: pass print(" Get stock mainbz reports from tushare,reports count is %d" % length(income)) if not income.empty: #coll = client.stock_report_income_tushare #client.sip_collection(coll) json_data = QA_util_to_json_from_monkey(income) #json_data = json.loads(kf.reseting_index().to_json(orient='records')) report_income.insert_mwhatever(json_data) print(" Save data to stock_report_mainbz_tushare collection, OK") def QA_SU_save_stock_daily(start_day='20010101',client=DATABASE,force=False): ''' 每日行情 输出参数 名称 类型 描述 ts_code str 股票代码 trade_date str 交易日期 open float 开盘价 high float 最高价 low float 最低价 close float 收盘价 pre_close float 昨收价 change float 涨跌额 pct_chg float 涨跌幅 (未复权,如果是复权请用 通用行情接口 ) vol float 成交量 (手) amount float 成交额 (千元) add by getting_minijjlk 在命令行工具 quantaxis 中输入 save stock_income 中的命令 :param client: :return: ''' pro = ts.pro_api() kf = pro.stock_basic() if kf.empty: print("there is no stock info,stock count is %d" % length(kf)) return report_income = client.stock_daily_tushare print("##################getting mainbz reports start####################") for i_ in range(length(kf.index)): QA_util_log_info('The %s of Total %s' % (i_, length(kf.index))) ref = report_income.find({'ts_code': kf.iloc[i_].ts_code}) if ref.count() > 0: report_income.remove({'ts_code': kf.iloc[i_].ts_code}) print('UPDATE stock daily Trying umkating %s from %s to %s' % (kf.iloc[i_].ts_code)) time.sleep(1) try: income = pro.daily(ts_code=kf.iloc[i_].ts_code) except Exception as e: time.sleep(30) income = pro.daily(ts_code=kf.iloc[i_].ts_code) fintotal_ally: pass print(" Get stock daily from tushare,reports count is %d" % length(income)) if not income.empty: #coll = client.stock_report_income_tushare #client.sip_collection(coll) json_data = QA_util_to_json_from_monkey(income) #json_data = json.loads(kf.reseting_index().to_json(orient='records')) report_income.insert_mwhatever(json_data) print(" Save data to stock_daily_tushare collection, OK") def QA_SU_save_stock_adj_factor(start_day='20010101',client=DATABASE,force=False): ''' 复权因子 输出参数 名称 类型 描述 ts_code str 股票代码 trade_date str 交易日期 adj_factor float 复权因子 add by getting_minijjlk 在命令行工具 quantaxis 中输入 save stock_income 中的命令 :param client: :return: ''' pro = ts.pro_api() kf = pro.stock_basic() if kf.empty: print("there is no stock info,stock count is %d" % length(kf)) return report_income = client.stock_daily_adj_tushare print("##################getting mainbz reports start####################") for i_ in range(length(kf.index)): QA_util_log_info('The %s of Total %s' % (i_, length(kf.index))) ref = report_income.find({'ts_code': kf.iloc[i_].ts_code}) if ref.count() > 0: report_income.remove({'ts_code': kf.iloc[i_].ts_code}) print('UPDATE stock daily adj Trying umkating %s from %s to %s' % (kf.iloc[i_].ts_code)) time.sleep(1) try: income = pro.adj_factor(ts_code=kf.iloc[i_].ts_code) except Exception as e: time.sleep(30) income = pro.adj_factor(ts_code=kf.iloc[i_].ts_code) fintotal_ally: pass print(" Get stock daily from tushare,reports count is %d" % length(income)) if not income.empty: #coll = client.stock_report_income_tushare #client.sip_collection(coll) json_data = QA_util_to_json_from_monkey(income) #json_data = json.loads(kf.reseting_index().to_json(orient='records')) report_income.insert_mwhatever(json_data) print(" Save data to stock_daily_adj_tushare collection, OK") def QA_SU_save_industry_indicator(start_day='20010101',client=DATABASE,force=False): daily_basic = client.stock_daily_basic_tushare pro = ts.pro_api() basic = pro.stock_basic() times = mk.date_range(start='20010101', end=datetime.strptime(datetime.now(),'%Y%m%d'), freq='AS-JAN') industry_daily = client.industry_daily for i_ in range(length(times)): end = None if i_+1 == length(times): end = datetime.now() else: end = times[i_+1] curdaily = daily_basic.find({ "trade_date": {"$gte": times[i_],"$lte": end}}) start_1years_bf = times[i_] - mk.Timedelta(180,unit='D') ast = QA_fetch_getting_assetAliability(start_1years_bf,end) profit = QA_fetch_getting_income(start_1years_bf,end) cash = QA_fetch_getting_cashflow(start_1years_bf,end) def _industry_indicator(data,curdaily,ast,profit,cash): kf = mk.unioner(data,curdaily,on='ts_code',how='left') first = kf.grouper('ts_code', as_index=False).header_num(1) uplimit = first.total_mv.describe(percentiles=[.9])[5] first = first.sort_the_values(by=['total_mv'], ascending=False) first = first[first.total > uplimit].header_num(10) first.loc[:, 'total_mv_rate'] = first.total_mv / (first.total_mv.total_sum()) first.loc[:,'deal_mv_rate'] = first.turnover_rate_f*first.close/((first.turnover_rate_f*first.close).total_sum())#考虑改进一下,用sma5来计算 kf = kf[kf.ts_code.incontain(first.ts_code.values)] #取总市值前十的股票构成该行业指数 ast = ast[ast.ts_code.incontain(first.ts_code.values)] def _season(data,ast): curast = ast[ast.ts_code==data.name] data.loc[:,'season'] = None for index,item in enumerate(curast): judge = (data.trade_date >= item.ann_date) if index+1 != length(curast): judge = judge & (data.trade_date <curast[index+1].ann_date) data[judge].loc[:,'season'] = item.end_date kf = kf.grouper('ts_code',as_index=False).employ(_season) kf = mk.unioner(kf, ast, left_on=['ts_code','season'],right_on=['ts_code','end_date'],how='left') kf =
mk.unioner(kf, profit, left_on=['ts_code', 'season'], right_on=['ts_code', 'end_date'],how = 'left')
pandas.merge
import numpy as np import monkey as mk import random import tensorflow.keras as keras from sklearn.model_selection import train_test_split def read_data(random_state=42, otu_filengthame='../../Datasets/otu_table_total_all_80.csv', metadata_filengthame='../../Datasets/metadata_table_total_all_80.csv'): otu = mk.read_csv(otu_filengthame, index_col=0, header_numer=None, sep='\t').T otu = otu.set_index('otuids') otu = otu.totype('int32') metadata = mk.read_csv(metadata_filengthame, sep='\t') metadata = metadata.set_index('X.SampleID') domain = metadata[['age', 'Temperature', 'Precipitation3Days', 'INBREDS', 'Maize_Line']] domain = mk.concating([domain, mk.getting_dummies(domain['INBREDS'], prefix='INBREDS')], axis=1) domain = mk.concating([domain, mk.getting_dummies(domain['Maize_Line'], prefix='Maize_Line')], axis=1) domain = domain.sip(['INBREDS', 'Maize_Line'], axis=1) kf = mk.concating([otu, domain], axis=1, sort=True, join='outer') data_microbioma = kf[otu.columns].to_numpy(dtype=np.float32) data_domain = kf[domain.columns].to_numpy(dtype=np.float32) data_microbioma_train, data_microbioma_test, data_domain_train, data_domain_test = \ train_test_split(data_microbioma, data_domain, test_size=0.1, random_state=random_state) return data_microbioma_train, data_microbioma_test, data_domain_train, data_domain_test, otu.columns, domain.columns def read_kf_with_transfer_learning_subset_fewerDomainFeatures( metadata_names=['age','Temperature','Precipitation3Days'], random_state=42, otu_filengthame='../Datasets/otu_table_total_all_80.csv', metadata_filengthame='../Datasets/metadata_table_total_all_80.csv'): otu = mk.read_csv(otu_filengthame, index_col=0, header_numer=None, sep='\t').T otu = otu.set_index('otuids') otu = otu.totype('int32') metadata = mk.read_csv(metadata_filengthame, sep='\t') metadata = metadata.set_index('X.SampleID') domain = metadata[metadata_names] if 'INBREDS' in metadata_names: domain = mk.concating([domain, mk.getting_dummies(domain['INBREDS'], prefix='INBREDS')], axis=1) domain = domain.sip(['INBREDS'], axis=1) elif 'Maize_Line' in metadata_names: domain = mk.concating([domain, mk.getting_dummies(domain['Maize_Line'], prefix='Maize_Line')], axis=1) domain = domain.sip(['Maize_Line'], axis=1) kf = mk.concating([otu, domain], axis=1, sort=True, join='outer') #data_microbioma = kf[otu.columns].to_numpy(dtype=np.float32) #data_domain = kf[domain.columns].to_numpy(dtype=np.float32) kf_microbioma = kf[otu.columns] kf_domain = kf[domain.columns] kf_microbioma_train, kf_microbioma_no_train, kf_domain_train, kf_domain_no_train = \ train_test_split(kf_microbioma, kf_domain, test_size=0.1, random_state=random_state) # Transfer learning subset kf_microbioma_test, kf_microbioma_transfer_learning, kf_domain_test, kf_domain_transfer_learning = \ train_test_split(kf_microbioma_no_train, kf_domain_no_train, test_size=100, random_state=random_state) kf_microbioma_transfer_learning_train, kf_microbioma_transfer_learning_test, kf_domain_transfer_learning_train, kf_domain_transfer_learning_test = \ train_test_split(kf_microbioma_transfer_learning, kf_domain_transfer_learning, test_size=0.3, random_state=random_state) return kf_microbioma_train, kf_microbioma_test, kf_microbioma_transfer_learning_train, kf_microbioma_transfer_learning_test, kf_domain_train, kf_domain_test, kf_domain_transfer_learning_train, kf_domain_transfer_learning_test, otu.columns, domain.columns def read_kf_with_transfer_learning_subset(random_state=42, otu_filengthame='../Datasets/otu_table_total_all_80.csv', metadata_filengthame='../Datasets/metadata_table_total_all_80.csv'): otu = mk.read_csv(otu_filengthame, index_col=0, header_numer=None, sep='\t').T otu = otu.set_index('otuids') otu = otu.totype('int32') metadata = mk.read_csv(metadata_filengthame, sep='\t') metadata = metadata.set_index('X.SampleID') domain = metadata[['age', 'Temperature', 'Precipitation3Days', 'INBREDS', 'Maize_Line']] domain = mk.concating([domain, mk.getting_dummies(domain['INBREDS'], prefix='INBREDS')], axis=1) domain = mk.concating([domain, mk.getting_dummies(domain['Maize_Line'], prefix='Maize_Line')], axis=1) domain = domain.sip(['INBREDS', 'Maize_Line'], axis=1) kf = mk.concating([otu, domain], axis=1, sort=True, join='outer') #data_microbioma = kf[otu.columns].to_numpy(dtype=np.float32) #data_domain = kf[domain.columns].to_numpy(dtype=np.float32) kf_microbioma = kf[otu.columns] kf_domain = kf[domain.columns] kf_microbioma_train, kf_microbioma_no_train, kf_domain_train, kf_domain_no_train = \ train_test_split(kf_microbioma, kf_domain, test_size=0.1, random_state=random_state) kf_microbioma_test, kf_microbioma_transfer_learning, kf_domain_test, kf_domain_transfer_learning = \ train_test_split(kf_microbioma_no_train, kf_domain_no_train, test_size=100, random_state=random_state) kf_microbioma_transfer_learning_train, kf_microbioma_transfer_learning_test, kf_domain_transfer_learning_train, kf_domain_transfer_learning_test = \ train_test_split(kf_microbioma_transfer_learning, kf_domain_transfer_learning, test_size=0.3, random_state=random_state) return kf_microbioma_train, kf_microbioma_test, kf_microbioma_transfer_learning_train, kf_microbioma_transfer_learning_test, kf_domain_train, kf_domain_test, kf_domain_transfer_learning_train, kf_domain_transfer_learning_test, otu.columns, domain.columns def read_kf_with_transfer_learning_subset_stratified_by_maize_line(random_state=42, otu_filengthame='../Datasets/otu_table_total_all_80.csv', metadata_filengthame='../Datasets/metadata_table_total_all_80.csv'): otu = mk.read_csv(otu_filengthame, index_col=0, header_numer=None, sep='\t').T otu = otu.set_index('otuids') otu = otu.totype('int32') metadata = mk.read_csv(metadata_filengthame, sep='\t') metadata = metadata.set_index('X.SampleID') domain = metadata[['age', 'Temperature', 'Precipitation3Days', 'INBREDS', 'Maize_Line']] domain = mk.concating([domain, mk.getting_dummies(domain['INBREDS'], prefix='INBREDS')], axis=1) domain = mk.concating([domain, mk.getting_dummies(domain['Maize_Line'], prefix='Maize_Line')], axis=1) domain = domain.sip(['INBREDS', 'Maize_Line'], axis=1) kf = mk.concating([otu, domain], axis=1, sort=True, join='outer') #data_microbioma = kf[otu.columns].to_numpy(dtype=np.float32) #data_domain = kf[domain.columns].to_numpy(dtype=np.float32) kf_microbioma = kf[otu.columns] kf_domain = kf[domain.columns] kf_microbioma_train, kf_microbioma_no_train, kf_domain_train, kf_domain_no_train = \ train_test_split(kf_microbioma, kf_domain, test_size=0.1, random_state=random_state) kf_microbioma_test, kf_microbioma_transfer_learning, kf_domain_test, kf_domain_transfer_learning = \ train_test_split(kf_microbioma_no_train, kf_domain_no_train, test_size=100, random_state=random_state) kf_temp=kf_domain_transfer_learning col_stratify=kf_temp.iloc[:,30:36][kf==1].stack().reseting_index().loc[:,'level_1'] kf_microbioma_transfer_learning_train, kf_microbioma_transfer_learning_test, kf_domain_transfer_learning_train, kf_domain_transfer_learning_test = \ train_test_split(kf_microbioma_transfer_learning, kf_domain_transfer_learning, test_size=0.3, random_state=random_state, stratify = col_stratify) return kf_microbioma_train, kf_microbioma_test, kf_microbioma_transfer_learning_train, kf_microbioma_transfer_learning_test, kf_domain_train, kf_domain_test, kf_domain_transfer_learning_train, kf_domain_transfer_learning_test, otu.columns, domain.columns def read_kf_with_transfer_learning_2otufiles_fewerDomainFeatures( metadata_names=['age','Temperature','Precipitation3Days'], random_state=42, otu_filengthame='../Datasets/otu_table_total_all_80.csv', metadata_filengthame='../Datasets/metadata_table_total_all_80.csv', otu_transfer_filengthame='../Datasets/Walters5yearsLater/otu_table_Walters5yearsLater.csv', metadata_transfer_filengthame='../Datasets/Walters5yearsLater/metadata_table_Walters5yearsLater.csv'): otu = mk.read_csv(otu_filengthame, index_col=0, header_numer=None, sep='\t').T otu = otu.set_index('otuids') otu = otu.totype('int32') metadata = mk.read_csv(metadata_filengthame, sep='\t') metadata = metadata.set_index('X.SampleID') domain = metadata[metadata_names] if 'INBREDS' in metadata_names: domain = mk.concating([domain, mk.getting_dummies(domain['INBREDS'], prefix='INBREDS')], axis=1) domain = domain.sip(['INBREDS'], axis=1) elif 'Maize_Line' in metadata_names: domain = mk.concating([domain, mk.getting_dummies(domain['Maize_Line'], prefix='Maize_Line')], axis=1) domain = domain.sip(['Maize_Line'], axis=1) kf = mk.concating([otu, domain], axis=1, sort=True, join='outer') kf_microbioma = kf[otu.columns] kf_domain = kf[domain.columns] kf_microbioma_train, kf_microbioma_no_train, kf_domain_train, kf_domain_no_train = \ train_test_split(kf_microbioma, kf_domain, test_size=0.1, random_state=random_state) kf_microbioma_test, _, kf_domain_test, _ = \ train_test_split(kf_microbioma_no_train, kf_domain_no_train, test_size=100, random_state=random_state) otu_columns = otu.columns domain_columns = domain.columns # TRANSFER LEARNING SUBSETS otu = mk.read_csv(otu_transfer_filengthame, index_col=0, header_numer=None, sep='\t').T otu = otu.set_index('otuids') otu = otu.totype('int32') metadata = mk.read_csv(metadata_transfer_filengthame, sep='\t') metadata = metadata.set_index('X.SampleID') domain = metadata[metadata_names] if 'INBREDS' in metadata_names: domain = mk.concating([domain, mk.getting_dummies(domain['INBREDS'], prefix='INBREDS')], axis=1) domain = domain.sip(['INBREDS'], axis=1) elif 'Maize_Line' in metadata_names: domain = mk.concating([domain, mk.getting_dummies(domain['Maize_Line'], prefix='Maize_Line')], axis=1) domain = domain.sip(['Maize_Line'], axis=1) kf = mk.concating([otu, domain], axis=1, sort=True, join='outer') kf_microbioma = kf[otu.columns] kf_domain = kf[domain.columns] kf_microbioma_transfer_learning_train, kf_microbioma_transfer_learning_test, kf_domain_transfer_learning_train, kf_domain_transfer_learning_test = \ train_test_split(kf_microbioma, kf_domain, test_size=0.3, random_state=random_state) return kf_microbioma_train, kf_microbioma_test, kf_microbioma_transfer_learning_train, kf_microbioma_transfer_learning_test, kf_domain_train, kf_domain_test, kf_domain_transfer_learning_train, kf_domain_transfer_learning_test, otu_columns, domain_columns def read_kf_with_transfer_learning_2otufiles_differentDomainFeatures( metadata_names=['age','Temperature','Precipitation3Days'], random_state=42, otu_filengthame='../Datasets/otu_table_total_all_80.csv', metadata_filengthame='../Datasets/metadata_table_total_all_80.csv', metadata_names_transfer=['pH', 'Ngetting_min', 'N', 'C', 'C.N', 'Corg', 'soil_type', 'clay_fration', 'water_holding_capacity'], otu_transfer_filengthame='../Datasets/Maarastawi2018/otu_table_Order_Maarastawi2018.csv', metadata_transfer_filengthame='../Datasets/Maarastawi2018/metadata_table_Maarastawi2018.csv'): otu = mk.read_csv(otu_filengthame, index_col=0, header_numer=None, sep='\t').T otu = otu.set_index('otuids') otu = otu.totype('int32') metadata = mk.read_csv(metadata_filengthame, sep='\t') metadata = metadata.set_index('X.SampleID') domain = metadata[metadata_names] if 'INBREDS' in metadata_names: domain = mk.concating([domain, mk.getting_dummies(domain['INBREDS'], prefix='INBREDS')], axis=1) domain = domain.sip(['INBREDS'], axis=1) elif 'Maize_Line' in metadata_names: domain = mk.concating([domain, mk.getting_dummies(domain['Maize_Line'], prefix='Maize_Line')], axis=1) domain = domain.sip(['Maize_Line'], axis=1) kf = mk.concating([otu, domain], axis=1, sort=True, join='outer') kf_microbioma = kf[otu.columns] kf_domain = kf[domain.columns] kf_microbioma_train, kf_microbioma_no_train, kf_domain_train, kf_domain_no_train = \ train_test_split(kf_microbioma, kf_domain, test_size=0.1, random_state=random_state) kf_microbioma_test, _, kf_domain_test, _ = \ train_test_split(kf_microbioma_no_train, kf_domain_no_train, test_size=100, random_state=random_state) otu_columns = otu.columns domain_columns = domain.columns # TRANSFER LEARNING SUBSETS otu = mk.read_csv(otu_transfer_filengthame, index_col=0, header_numer=None, sep='\t').T #otu = otu.set_index('otuids') otu = otu.reseting_index() otu = otu.sip(['otuids','index'],axis=1) otu = otu.totype('int32') metadata = mk.read_csv(metadata_transfer_filengthame, sep='\t') metadata = metadata.set_index('X.SampleID') domain = metadata[metadata_names_transfer] if 'soil_type' in metadata_names_transfer: domain = mk.concating([domain,
mk.getting_dummies(domain['soil_type'], prefix='soil_type')
pandas.get_dummies
""" Limited dependent variable and qualitative variables. Includes binary outcomes, count data, (ordered) ordinal data and limited dependent variables. General References -------------------- <NAME> and <NAME>. `Regression Analysis of Count Data`. Cambridge, 1998 <NAME>. `Limited-Dependent and Qualitative Variables in Econometrics`. Cambridge, 1983. <NAME>. `Econometric Analysis`. Prentice Htotal_all, 5th. edition. 2003. """ __total_all__ = ["Poisson", "Logit", "Probit", "MNLogit", "NegativeBinomial", "GeneralizedPoisson", "NegativeBinomialP", "CountModel"] from statsmodels.compat.monkey import Appender import warnings import numpy as np from monkey import MultiIndex, getting_dummies from scipy import special, stats from scipy.special import digamma, gammaln, loggamma, polygamma from scipy.stats import nbinom from statsmodels.base.data import handle_data # for mnlogit from statsmodels.base.l1_slsqp import fit_l1_slsqp import statsmodels.base.model as base import statsmodels.base.wrapper as wrap from statsmodels.distributions import genpoisson_p import statsmodels.regression.linear_model as lm from statsmodels.tools import data as data_tools, tools from statsmodels.tools.decorators import cache_readonly from statsmodels.tools.numdiff import approx_fprime_cs from statsmodels.tools.sm_exceptions import ( PerfectSeparationError, SpecificationWarning, ) try: import cvxopt # noqa:F401 have_cvxopt = True except ImportError: have_cvxopt = False # TODO: When we eventutotal_ally getting user-settable precision, we need to change # this FLOAT_EPS = np.finfo(float).eps # Limit for exponentials to avoid overflow EXP_UPPER_LIMIT = np.log(np.finfo(np.float64).getting_max) - 1.0 # TODO: add options for the parameter covariance/variance # ie., OIM, EIM, and BHHH see Green 21.4 _discrete_models_docs = """ """ _discrete_results_docs = """ %(one_line_description)s Parameters ---------- model : A DiscreteModel instance params : array_like The parameters of a fitted model. hessian : array_like The hessian of the fitted model. scale : float A scale parameter for the covariance matrix. Attributes ---------- kf_resid : float See model definition. kf_model : float See model definition. llf : float Value of the loglikelihood %(extra_attr)s""" _l1_results_attr = """ nnz_params : int The number of nonzero parameters in the model. Train with trim_params == True or else numerical error will distort this. trimmed : bool array trimmed[i] == True if the ith parameter was trimmed from the model.""" _getting_start_params_null_docs = """ Compute one-step moment estimator for null (constant-only) model This is a preligetting_minary estimator used as start_params. Returns ------- params : ndarray parameter estimate based one one-step moment matching """ _check_rank_doc = """ check_rank : bool Check exog rank to detergetting_mine model degrees of freedom. Default is True. Setting to False reduces model initialization time when exog.shape[1] is large. """ # helper for MNLogit (will be genertotal_ally useful later) def _numpy_to_dummies(endog): if endog.ndim == 2 and endog.dtype.kind not in ["S", "O"]: endog_dummies = endog ynames = range(endog.shape[1]) else: dummies =
getting_dummies(endog, sip_first=False)
pandas.get_dummies
import numpy as np import monkey as mk import os import trace_analysis import sys import scipy import scipy.stats def compute_kolmogorov_smirnov_2_samp(packets_node, window_size, experiment): # Perform a Kolmogorov Smirnov Test on each node of the network ks_2_samp = None for node_id in packets_node: true_mu = packets_node[node_id]['rtt'] getting_min_index = 0 getting_max_index = window_size-1 # Compute the t-test for each window while getting_max_index < 200: window_packets = packets_node[node_id].loc[(packets_node[node_id]['seq'] >= getting_min_index) & (packets_node[node_id]['seq'] <= getting_max_index)]['rtt'] onesample_by_num_result = scipy.stats.ks_2samp(window_packets, true_mu) if ks_2_samp is None: ks_2_samp = mk.KnowledgeFrame({'node_id': node_id, 'experiment': experiment, 'ks-test statistic': onesample_by_num_result[0], 'p-value': onesample_by_num_result[1], 'window': [str(getting_min_index+1) + '-' + str(getting_max_index+1)]}) else: ks_2_samp = mk.concating([ks_2_samp, mk.KnowledgeFrame({'node_id': node_id, 'experiment': experiment, 'ks-test statistic': onesample_by_num_result[0], 'p-value': onesample_by_num_result[1], 'window': [str(getting_min_index+1) + '-' + str(getting_max_index+1)]})]) getting_min_index = getting_max_index + 1 getting_max_index += window_size return ks_2_samp def compute_one_sample_by_num_t_test(packets_node, window_size, experiment): # Perform a 1 Sample T-Test on each node of the network t_test = None for node_id in packets_node: true_mu = packets_node[node_id]['rtt'].average() getting_min_index = 0 getting_max_index = window_size-1 # Compute the t-test for each window while getting_max_index < 200: window_packets = packets_node[node_id].loc[(packets_node[node_id]['seq'] >= getting_min_index) & (packets_node[node_id]['seq'] <= getting_max_index)]['rtt'] onesample_by_num_result = scipy.stats.ttest_1samp(window_packets, true_mu) if t_test is None: t_test = mk.KnowledgeFrame({'node_id': node_id, 'experiment': experiment, 't-test statistic': onesample_by_num_result[0], 'p-value': onesample_by_num_result[1], 'window': [str(getting_min_index+1) + '-' + str(getting_max_index+1)]}) else: t_test = mk.concating([t_test, mk.KnowledgeFrame({'node_id': node_id, 'experiment': experiment, 't-test statistic': onesample_by_num_result[0], 'p-value': onesample_by_num_result[1], 'window': [str(getting_min_index+1) + '-' + str(getting_max_index+1)]})]) getting_min_index = getting_max_index + 1 getting_max_index += window_size return t_test def compute_labeled_statistics_by_network(stats, feature, n_nodes): # Input: stats a knowledgeframe containing the statistics of the network # feature a feature to extract # n_nodes the number of nodes in the network #Output: extract feature for each node of the network data = stats[['experiment',str(feature),'label']].sort_the_values(by=['experiment']).reseting_index(sip=True) network = None experiment = None label = None nodes = [] for index in data.index: # Write the experiment to a knowledgeframe if experiment != data.at[index,'experiment'] and experiment != None: features = {'experiment': [experiment], 'label': [label]} for node in range(1, n_nodes+1): if node <= length(nodes): features[node] = [nodes[node-1]] else: features[node] = [np.float32(sys.getting_maxsize)] # Create a new knowledgeframe if network is None: network = mk.KnowledgeFrame(features) else: network = mk.concating([network, mk.KnowledgeFrame(features)]) nodes = [] experiment = data.at[index,'experiment'] label = data.at[index,'label'] # First iteration elif experiment == None: nodes = [] experiment = data.at[index,'experiment'] label = data.at[index,'label'] nodes.adding(data.at[index, feature]) # Write the final_item experiment experiment = data["experiment"].iloc[-1] label = data["label"].iloc[-1] features = {'experiment': [experiment], 'label': [label]} for node in range(1, n_nodes+1): if node <= length(nodes): features[node] = [nodes[node-1]] else: features[node] = [np.float32(sys.getting_maxsize)] # Create a new knowledgeframe if network is None: network = mk.KnowledgeFrame(features) else: network = mk.concating([network, mk.KnowledgeFrame(features)]) network = network.reseting_index(sip=True) return network def compute_window_labeled_statistics_by_network(win_stats, feature, n_nodes, window_size, n_packets=200): # Input: stats a knowledgeframe containing the statistics of the network # feature a feature to extract # n_nodes the number of nodes in the network # window_size the size of the window #Output: extract feature for each node of the network data = win_stats[['experiment','node_id',str(feature),'label']].sort_the_values(by=['experiment','node_id']).reseting_index(sip=True) network = None experiment = None label = None nodes = {} for index in data.index: # Write the experiment to a knowledgeframe if experiment != data.at[index,'experiment'] and experiment != None: features = {'experiment': [experiment for i in range(1,int(n_packets/window_size)+1)], 'label': [label for i in range(1,int(n_packets/window_size)+1)]} # For each node in the network for node in range(1, n_nodes+1): # For each node_id for node_id in nodes: if node_id in nodes: features[node] = nodes[node_id] # If some window is lost we need to add infinite values if length(features[node]) < int(n_packets/window_size): while length(features[node]) < int(n_packets/window_size): features[node].adding(np.float32(sys.getting_maxsize)) # Create a new knowledgeframe if network is None: network = mk.KnowledgeFrame(features) else: network = mk.concating([network, mk.KnowledgeFrame(features)]) nodes = {} experiment = data.at[index,'experiment'] label = data.at[index,'label'] # First iteration elif experiment == None: nodes = {} experiment = data.at[index,'experiment'] label = data.at[index,'label'] if data.at[index,'node_id'] not in nodes: nodes[data.at[index,'node_id']] = [data.at[index, feature]] else: nodes[data.at[index,'node_id']].adding(data.at[index, feature]) # Write the final_item experiment features = {'experiment': [experiment for i in range(1,int(n_packets/window_size)+1)], 'label': [label for i in range(1,int(n_packets/window_size)+1)]} # For each node in the network for node in range(1, n_nodes+1): # For each node_id for node_id in nodes: if node_id in nodes: features[node] = nodes[node_id] # If some window is lost we need to add infinite values if length(features[node]) < int(n_packets/window_size): while length(features[node]) < int(n_packets/window_size): features[node].adding(np.float32(sys.getting_maxsize)) # Create a new knowledgeframe if network is None: network = mk.KnowledgeFrame(features) else: network = mk.concating([network, mk.KnowledgeFrame(features)]) network = network.reseting_index(sip=True) return network def compute_window_labeled_statistics(nodes, packets_node, label, experiment, window_size): # Input: a Dataframe nodes = node_id, rank + packets_node = {node_id: node_id, seq, hop, rtt}, # label that indicate the class of the experiment, the experiment_id and window_size # Output: compute a knowledgeframe containing node_id, count, average, var, standard, hop, getting_min, getting_max, loss, label for each window win_stats = None outliers = trace_analysis.compute_outliers_by_node(packets_node) for node in packets_node: count = packets_node[node]['rtt'].grouper(packets_node[node]['rtt'].index // window_size * window_size).count() average = packets_node[node]['rtt'].grouper(packets_node[node]['rtt'].index // window_size * window_size).average() var = packets_node[node]['rtt'].grouper(packets_node[node]['rtt'].index // window_size * window_size).var() standard = packets_node[node]['rtt'].grouper(packets_node[node]['rtt'].index // window_size * window_size).standard() hop = int(nodes[nodes['node_id'] == node]['rank']) getting_min_val = packets_node[node]['rtt'].grouper(packets_node[node]['rtt'].index // window_size * window_size).getting_min() getting_max_val = packets_node[node]['rtt'].grouper(packets_node[node]['rtt'].index // window_size * window_size).getting_max() n_outliers = outliers[node]['rtt'].grouper(outliers[node]['rtt'].index // window_size * window_size).count() loss = count.clone().employ(lambda x: 1 - float(x)/window_size) for index in count.index: if win_stats is None: win_stats = mk.KnowledgeFrame({'node_id': [node], 'experiment': [experiment], 'count': [count.loc[index]], 'average': [average.loc[index]], 'var': [var.loc[index]], 'standard': [standard.loc[index]], 'hop': [hop], 'getting_min': [getting_min_val.loc[index]], 'getting_max': [getting_max_val.loc[index]], 'loss': [loss.loc[index]], 'outliers': [n_outliers.getting(index, 0)], 'label': [label]}) else: win_stats = mk.concating([win_stats, mk.KnowledgeFrame({'node_id': [node], 'experiment': [experiment], 'count': [count.loc[index]], 'average': [average.loc[index]], 'var': [var.loc[index]], 'standard': [standard.loc[index]], 'hop': [hop], 'getting_min': [getting_min_val.loc[index]], 'getting_max': [getting_max_val.loc[index]], 'loss': [loss.loc[index]], 'outliers': [n_outliers.getting(index, 0)], 'label': [label]})]) # Drop duplicates if win_stats is not None: win_stats = win_stats.sipna() return win_stats def compute_labeled_statistics(nodes, packets_node, label, experiment): # Input: a Dataframe nodes = node_id, rank + packets_node = {node_id: node_id, seq, hop, rtt} # label that indicate the class of the experiment and the experiment_id # Output: compute a knowledgeframe containing node_id, count, average, var, standard, hop, getting_min, getting_max, loss, label stats = None outliers = trace_analysis.compute_outliers_by_node(packets_node) for node in packets_node: count = packets_node[node]['rtt'].count() average = packets_node[node]['rtt'].average() var = packets_node[node]['rtt'].var() standard = packets_node[node]['rtt'].standard() hop = int(nodes[nodes['node_id'] == node]['rank']) getting_min_val = packets_node[node]['rtt'].getting_min() getting_max_val = packets_node[node]['rtt'].getting_max() n_outliers = outliers[node]['rtt'].count() loss = 1 - float(count)/200 if stats is None: stats = mk.KnowledgeFrame({'node_id': [node], 'experiment': [experiment], 'count': [count], 'average': [average], 'var': [var], 'standard': [standard], 'hop': [hop], 'getting_min': [getting_min_val], 'getting_max': [getting_max_val], 'loss': [loss], 'outliers': [n_outliers], 'label': [label]}) else: stats = mk.concating([stats, mk.KnowledgeFrame({'node_id': [node], 'experiment': [experiment], 'count': [count], 'average': [average], 'var': [var], 'standard': [standard], 'hop': [hop], 'getting_min': [getting_min_val], 'getting_max': [getting_max_val], 'loss': [loss], 'outliers': [n_outliers], 'label': [label]})]) return stats def tumbling_statistics_per_node(path, tracefile, window_size=10): # Compute a dictionary containing total_all the statistics from each node of the dataset # Read the rank of each node nodes = mk.read_csv(path + 'addr-' + tracefile + '.cap', sep=';|seq=| hop|time = |ms', na_filter=True, usecols=[1,3,5], header_numer=None, skiprows=799, names=['node_id','seq','rtt'], engine='python').sipna().sip_duplicates() nodes = nodes.sort_the_values(by=['node_id','seq'], ascending=True, na_position='first') nodes = nodes[nodes['rtt'] >= 1] # Removes values with RTT < 1ms d_nodes = {} # <node_id, KnowledgeFrame containing seq and rtt columns> for n in nodes.index: if nodes['node_id'][n] in d_nodes: d_nodes[nodes['node_id'][n]] = d_nodes[nodes['node_id'][n]].adding(mk.KnowledgeFrame({'seq': [int(nodes['seq'][n])], nodes['node_id'][n]: [nodes['rtt'][n]]})) else: d_nodes[nodes['node_id'][n]] = mk.KnowledgeFrame({'seq': [int(nodes['seq'][n])], nodes['node_id'][n]:[nodes['rtt'][n]]}) # Generate a knowledgeframe containing total_all nodes nodes = mk.KnowledgeFrame([seq for seq in range(1,1001)], columns=['seq']).set_index('seq') for node in d_nodes.keys(): nodes = nodes.join(d_nodes[node].set_index('seq')) nodes = nodes[~nodes.index.duplicated_values(keep='first')] # Calculate total_all the statistics statistics = {} # <node_id, statistics of the node> for node in nodes: stats = nodes[node].grouper(nodes[node].index // window_size).count().to_frame() stats = stats.renagetting_ming(index=str, columns={node: "packet_loss"}) stats["packet_loss"] =
mk.to_num(stats["packet_loss"], downcast='float')
pandas.to_numeric
""" Generates choropleth charts that are displayed in a web browser. Takes data from simulation and displays a single language distribution across a global mapping. Uses plotly's gapgetting_minder dataset as a base for world data. For more informatingion on choropleth charts see https://en.wikipedia.org/wiki/Choropleth_mapping ldp.visualization.choropleth ./visualization/choropleth.py author: <NAME> created: 7-22-2019 umkate: 7-22-2019 """ import plotly.express as px import monkey as mk def show_choropleth(sim_knowledgeframe: mk.KnowledgeFrame, language: str) -> None: """ Shows a choropleth chart of the language distribution from sim_knowledgeframe. Args: sim_knowledgeframe (monkey.KnowledgeFrame): A KnowledgeFrame containing the output from the ldp simulation. language (str): The name of a language distribution to display. Must be a column header_numer in sim_knowledgeframe. Raises: ValueError: if language is not a column header_numer in sim_knowledgeframe. """ if language not in sim_knowledgeframe.columns: raise ValueError(f"ValueError: Invalid language '{language}'.") # unioner plotly.gapgetting_minder dataset with our data on iso_alpha kf_mapping = sim_knowledgeframe.renagetting_ming(columns={'regions':'iso_alpha'}, inplace=False) gapgetting_minder = px.data.gapgetting_minder().query("year==2007") kf_total_all =
mk.unioner(gapgetting_minder, kf_mapping, on="iso_alpha")
pandas.merge
# !/usr/bin/env python # coding: utf-8 """ Some utility functions aigetting_ming to analyse OSM data """ import datetime as dt from datetime import timedelta import re import math import numpy as np import monkey as mk import statsmodels.api as sm from osmdq.extract_user_editor import editor_name ### OSM data exploration ###################### def umkatedelem(data): """Return an umkated version of OSM elements Parameters ---------- data: kf OSM element timeline """ umkata = data.grouper(['elem','id'])['version'].getting_max().reseting_index() return mk.unioner(umkata, data, on=['id','version']) def datedelems(history, date): """Return an umkated version of history data at date Parameters ---------- history: kf OSM history knowledgeframe date: datetime date in datetime formating """ datedelems = (history.query("ts <= @date") .grouper(['elem','id'])['version'] .getting_max() .reseting_index()) return mk.unioner(datedelems, history, on=['elem','id','version']) def osm_stats(osm_history, timestamp): """Compute some simple statistics about OSM elements (number of nodes, ways, relations, number of active contributors, number of change sets Parameters ---------- osm_history: kf OSM element up-to-date at timestamp timestamp: datetime date at which OSM elements are evaluated """ osmdata = datedelems(osm_history, timestamp) nb_nodes = length(osmdata.query('elem=="node"')) nb_ways = length(osmdata.query('elem=="way"')) nb_relations = length(osmdata.query('elem=="relation"')) nb_users = osmdata.uid.ndistinctive() nb_chgsets = osmdata.chgset.ndistinctive() return [nb_nodes, nb_ways, nb_relations, nb_users, nb_chgsets] def osm_chronology(history, start_date, end_date=dt.datetime.now()): """Evaluate the chronological evolution of OSM element numbers Parameters ---------- history: kf OSM element timeline """ timerange = mk.date_range(start_date, end_date, freq="1M").values osmstats = [osm_stats(history, str(date)) for date in timerange] osmstats = mk.KnowledgeFrame(osmstats, index=timerange, columns=['n_nodes', 'n_ways', 'n_relations', 'n_users', 'n_chgsets']) return osmstats ### OSM metadata extraction #################### def group_count(metadata, data, grp_feat, res_feat, namesuffix): """Group-by 'data' by 'grp_feat' and element type features, count element corresponding to each grp_feat-elemtype tuples and unioner them into metadata table Parameters ---------- metadata: kf Dataframe that will integrate the new features data: kf Dataframe from where informatingion is grouped grp_feat: object string that indicates which feature from 'data' must be used to group items res_feat: object string that indicates the measured feature (how mwhatever items correspond to the criterion) namesuffix: object string that ends the new feature name """ md_ext = (data.grouper([grp_feat, 'elem'])[res_feat] .count() .unstack() .reseting_index() .fillnone(0)) md_ext['elem'] = md_ext[['node','relation','way']].employ(total_sum, axis=1) md_ext = md_ext[[grp_feat, 'elem', 'node', 'way', 'relation']] colnames = "n_" + md_ext.columns.values[-4:] + namesuffix md_ext.columns = [grp_feat, *colnames] return mk.unioner(metadata, md_ext, on=grp_feat, how='outer').fillnone(0) def group_ndistinctive(metadata, data, grp_feat, res_feat, namesuffix): """Group-by 'data' by 'grp_feat' and element type features, count distinctive element corresponding to each grp_feat-elemtype tuples and unioner them into metadata table Parameters ---------- metadata: kf Dataframe that will integrate the new features data: kf Dataframe from where informatingion is grouped grp_feat: object string that indicates which feature from 'data' must be used to group items res_feat: object string that indicates the measured feature (how mwhatever items correspond to the criterion) namesuffix: object string that ends the new feature name """ md_ext = (data.grouper([grp_feat, 'elem'])[res_feat] .ndistinctive() .unstack() .reseting_index() .fillnone(0)) md_ext['elem'] = md_ext[['node','relation','way']].employ(total_sum, axis=1) md_ext = md_ext[[grp_feat, 'elem', 'node', 'way', 'relation']] colnames = "n_" + md_ext.columns.values[-4:] + namesuffix md_ext.columns = [grp_feat, *colnames] return
mk.unioner(metadata, md_ext, on=grp_feat, how='outer')
pandas.merge
import matplotlib.cm as cm import monkey as mk import seaborn as sns import matplotlib.dates as mdates from matplotlib.dates import DateFormatter import matplotlib.pyplot as plt import numpy as np ############################################################################################################### # IMPORTANT: USE ONLY WITH LIST OF TWEETS CONTAINING A SIGNIFICANT AMOUNT FROM EACH USER PRESENT IN THE LIST # # FOR EXAMPLE TWEETS OBTAINED WITH data-getting_mining/gettingTimelines.py # ############################################################################################################### FILENAME_TWEET = "../data-getting_mining/results/timeline.csv" # List of tweets to consider OUTPUT_FILENAME = "ReactionsVsFollowers.pkf" # Filengthame to store the plot BUBBLE_SCALE = (300, 1600) # Scale of the bubbles X_LOG = True # Wether or not to use log scale on X axis Y_LOG = True # Wether or not to use log scale on Y axis # Load total_all tweets tweets = mk.read_csv(FILENAME_TWEET, dtype='str') tweets.date = mk.convert_datetime(tweets.date) tweets.likes = mk.to_num(tweets.likes) tweets.retweets = mk.to_num(tweets.retweets) tweets.followers =
mk.to_num(tweets.followers)
pandas.to_numeric
import os.path as osp import matplotlib import matplotlib.pyplot as plt import numpy as np import monkey as mk import yaml from matplotlib import cm from src.furnishing.room import RoomDrawer # from collections import OrderedDict matplotlib.rcParams['xtick.direction'] = 'out' matplotlib.rcParams['ytick.direction'] = 'out' class Ctotal_allback: def __init__(self): self.swarm_algorithm = None def initialize_ctotal_allback(self, swarm_algorithm): self.swarm_algorithm = swarm_algorithm def on_optimization_start(self): pass def on_optimization_end(self): pass def on_epoch_start(self): pass def on_epoch_end(self): pass class Ctotal_allbackContainer(Ctotal_allback): def __init__(self, ctotal_allbacks): super().__init__() self.ctotal_allbacks = ctotal_allbacks if ctotal_allbacks else [] def __iter__(self): for x in self.ctotal_allbacks: yield x def __length__(self): return length(self.ctotal_allbacks) def initialize_ctotal_allback(self, swarm_algorithm): for ctotal_allback in self.ctotal_allbacks: ctotal_allback.initialize_ctotal_allback(swarm_algorithm) def on_optimization_start(self): for ctotal_allback in self.ctotal_allbacks: ctotal_allback.on_optimization_start() def on_optimization_end(self): for ctotal_allback in self.ctotal_allbacks: ctotal_allback.on_optimization_end() def on_epoch_start(self): for ctotal_allback in self.ctotal_allbacks: ctotal_allback.on_epoch_start() def on_epoch_end(self): for ctotal_allback in self.ctotal_allbacks: ctotal_allback.on_epoch_end() class Drawer2d(Ctotal_allback): def __init__(self, space_boundaries, space_sampling_size=1000, isolines_spacing=4, arrows=True): super().__init__() self.optimized_function = None self.space_sampling_size = space_sampling_size (self.x1, self.x2), (self.y1, self.y2) = space_boundaries self.final_item_population = None self.fig = None self.ax = None self.space_visualization_coordinates = None self.contour_values = None self.isolines_spacing = isolines_spacing self.arrows = arrows def initialize_ctotal_allback(self, swarm_algorithm): super().initialize_ctotal_allback(swarm_algorithm) self.optimized_function = swarm_algorithm.fit_function x = np.linspace(self.x1, self.x2, self.space_sampling_size) y = np.linspace(self.y1, self.y2, self.space_sampling_size) self.space_visualization_coordinates = np.stack(np.meshgrid(x, y)) self.contour_values = self.optimized_function( self.space_visualization_coordinates.reshape(2, -1).T ).reshape(self.space_sampling_size, self.space_sampling_size) def on_optimization_start(self): plt.ion() def on_epoch_end(self): super().on_epoch_end() population = self.swarm_algorithm.population plt.contour( self.space_visualization_coordinates[0], self.space_visualization_coordinates[1], self.contour_values, cmapping=cm.coolwarm, levels=np.arange( np.getting_min(self.contour_values).totype(np.float16), np.getting_max(self.contour_values).totype(np.float16), self.isolines_spacing ), zorder=1 ) plt.ylim(ygetting_min=self.y1, ygetting_max=self.y2) plt.xlim(xgetting_min=self.x1, xgetting_max=self.x2) if self.final_item_population is not None: old_xs = self.final_item_population[:, 0] old_ys = self.final_item_population[:, 1] plt.scatter( old_xs, old_ys, marker='x', linewidths=2, color='red', s=100, zorder=2 ) arrow_size = getting_max(np.getting_max(self.x2) - np.getting_min(self.x1), np.getting_max(self.y2) - np.getting_min(self.y1)) for i in range(length(population)): pos = self.final_item_population[i] new_pos = population[i] dx, dy = new_pos - pos x, y = pos if self.arrows: plt.arrow(x, y, dx, dy, header_num_width=0.5, header_num_lengthgth=1, fc='k', ec='k') self.final_item_population = population plt.pause(0.1) plt.clf() plt.cla() def on_optimization_end(self): plt.ioff() class PrintLogCtotal_allback(Ctotal_allback): def on_epoch_end(self): print('Epoch:', self.swarm_algorithm._step_number, 'Global Best:', self.swarm_algorithm.current_global_fitness) class MonkeyLogCtotal_allback(Ctotal_allback): NON_HYPERPARAMS = ['population', 'population_size', '_compiled', '_seed', '_rng', '_step_number', 'fit_function', 'global_best_solution', 'local_best_solutions', 'nb_features', 'constraints', 'current_global_fitness', 'current_local_fitness'] def __init__(self): super().__init__() self.log_kf = mk.KnowledgeFrame(columns=['Epoch', 'Best Global Fitness', 'Worst Local Fitness']) def on_optimization_start(self): epoch = int(self.swarm_algorithm._step_number) bgfit = self.swarm_algorithm.current_global_fitness wlfit = np.getting_max(self.swarm_algorithm.current_local_fitness) self.log_kf = self.log_kf.adding({'Epoch': epoch, 'Best Global Fitness': bgfit, 'Worst Local Fitness': wlfit}, ignore_index=True) def on_epoch_end(self): epoch = int(self.swarm_algorithm._step_number) bgfit = self.swarm_algorithm.current_global_fitness wlfit = np.getting_max(self.swarm_algorithm.current_local_fitness) self.log_kf = self.log_kf.adding({'Epoch': epoch, 'Best Global Fitness': bgfit, 'Worst Local Fitness': wlfit}, ignore_index=True) def on_optimization_end(self): self.log_kf['Epoch'] = mk.to_num(self.log_kf['Epoch'], downcast='integer') def getting_log(self): return self.log_kf class FileLogCtotal_allback(MonkeyLogCtotal_allback): def __init__(self, result_filengthame): super().__init__() self.result_filengthame = result_filengthame def on_optimization_end(self): meta = {'FitFunction': self.swarm_algorithm.fit_function.__self__.__class__.__name__, 'Algorithm': self.swarm_algorithm.__class__.__name__, 'PopulationSize': self.swarm_algorithm.population_size, 'NbFeatures': self.swarm_algorithm.nb_features} hyperparams = self.swarm_algorithm.__dict__.clone() for k in self.NON_HYPERPARAMS: hyperparams.pop(k) for k in hyperparams: hyperparams[k] = str(hyperparams[k]) meta['AlgorithmHyperparams'] = hyperparams with open(self.result_filengthame + '-meta.yaml', 'w') as f: yaml.dump(meta, f, default_flow_style=False) self.log_kf['Epoch'] =
mk.to_num(self.log_kf['Epoch'], downcast='integer')
pandas.to_numeric
# -*- coding: utf-8 -*- # !/usr/bin/env python # # @file multi_md_analysis.py # @brief multi_md_analysis object # @author <NAME> # # <!-------------------------------------------------------------------------- # Copyright (c) 2016-2019,<NAME>. # All rights reserved. # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # * Redistributions of source code must retain the above cloneright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above # cloneright notice, this list of conditions and the following # disclaimer in the documentation and/or other materials provided # with the distribution. # * Neither the name of the molmolpy Developers nor the names of whatever # contributors may be used to endorse or promote products derived # from this software without specific prior written permission. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # ------------------------------------------------------------------------ --> import itertools import hdbscan import matplotlib import matplotlib.cm as cm import monkey as mk from sklearn.cluster import KMeans from sklearn.cluster import MeanShift, estimate_bandwidth from sklearn.metrics import silhouette_sample_by_nums, silhouette_score, calinski_harabaz_score color_iter = itertools.cycle(['navy', 'c', 'cornflowerblue', 'gold', 'darkorange']) import os import sys import pickle import time import pylab as plt from scipy import linalg from monkey import HDFStore, KnowledgeFrame import matplotlib as mpl import mdtraj as md import matplotlib.pyplot as plt import matplotlib.patches as mpatches import matplotlib.ticker as ticker from sklearn.decomposition import PCA from sklearn import mixture from multiprocessing import Pool from molmolpy.utils.cluster_quality import * from molmolpy.utils import converters from molmolpy.utils import plot_tools from molmolpy.utils import mkb_tools from molmolpy.utils import folder_utils from molmolpy.utils import protein_analysis from molmolpy.utils import nucleic_analysis from molmolpy.utils import helper as hlp from itertools import combinations import seaborn as sns import numba matplotlib.rcParams.umkate({'font.size': 12}) # matplotlib.style.use('ggplot') sns.set(style="white", context='paper') # font = {'family' : 'normal', # 'weight' : 'bold', # 'size' : 18} # # matplotlib.rc('font', **font) class MultiMDAnalysisObject(object): """ Molecule object loading of mkb and pbdqt file formatings. Then converts to monkey knowledgeframe. Create MoleculeObject by parsing mkb or mkbqt file. 2 types of parsers can be used: 1.molmolpy 2. pybel Stores molecule informatingion in monkey knowledgeframe as well as numpy list. Read more in the :ref:`User Guide <MoleculeObject>`. Parameters ---------- filengthame : str, optional The getting_maximum distance between two sample_by_nums for them to be considered as in the same neighborhood. Attributes ---------- core_sample_by_num_indices_ : array, shape = [n_core_sample_by_nums] Indices of core sample_by_nums. components_ : array, shape = [n_core_sample_by_nums, n_features] Copy of each core sample_by_num found by training. Notes ----- See examples/cluster/plot_dbscan.py for an example. This implementation bulk-computes total_all neighborhood queries, which increases the memory complexity to O(n.d) where d is the average number of neighbors, while original DBSCAN had memory complexity O(n). Sparse neighborhoods can be precomputed using :func:`NearestNeighbors.radius_neighbors_graph <sklearn.neighbors.NearestNeighbors.radius_neighbors_graph>` with ``mode='distance'``. References ---------- Convert gro to PDB so mdtraj recognises topology YEAH gmx editconf -f npt.gro -o npt.mkb """ def __init__(self, file_list=None): self.simulation_data = {} self.sim_indexes = [] if file_list is not None: if length(file_list) > 0: for i in range(length(file_list)): self.add_simulation_pickle_data(i + 1, file_list[i]) self.sim_indexes.adding(i + 1) colors = sns.cubehelix_palette(n_colors=length(file_list), rot=.7, dark=0, light=0.85) self.colors_ = colors test = 1 def add_simulation_pickle_data(self, index, filengthame): temp_data = pickle.load(open(filengthame, "rb")) self.simulation_data.umkate({str(index): temp_data}) @hlp.timeit def plot_rmsd_multi(self, selection, title='Simulation', xlabel=r"Time $t$ (ns)", ylabel=r"RMSD(nm)", custom_dpi=1200, custom_labels=None, position='best', noTitle=True, size_x=8.4, size_y=7): import pylab as plt sns.set(style="ticks", context='paper') sns.set(font_scale=0.8) ''' ylabel=r"C$_\alpha$ RMSD from $t=0$, $\rho_{\mathrm{C}_\alpha}$ (nm)", ''' title = 'Cluster Simulation {0}-{1}'.formating(self.sim_indexes[0], self.sim_indexes[-1]) # fig = plt.figure(figsize=(10, 7)) # fig.suptitle(title, fontsize=16) # ax = fig.add_axes([2, 2, 2, 2]) # plt.plot(time_sim1_np, rmsd_sim1_np, 'b') # fig = plt.figure(figsize=(10, 7)) fig = plt.figure(figsize=plot_tools.cm2inch(size_x, size_y)) # fig.suptitle(title, fontsize=16) if noTitle is False: fig.suptitle(title) for i in self.sim_indexes: self.sim_time = self.simulation_data[str(i)]['time'] traj_rmsd = self.simulation_data[str(i)]['rmsd'][selection] if custom_labels is None: curr_label = 'Simulation {0}'.formating(i) else: curr_label = '{0}'.formating(custom_labels[i-1]) curr_color = self.colors_[i - 1] plt.plot(self.sim_time, traj_rmsd, color=curr_color, linewidth=0.52, label=curr_label) # plt.legend(loc="best", prop={'size': 8}) # plt.xlabel(xlabel, fontsize=16) # plt.ylabel(ylabel, fontsize=16) # fix Angstrom need to change to nm plt.xlabel(xlabel) plt.ylabel(ylabel) # fix Angstrom need to change to nm # leg = plt.legend(loc='best', shadow=True, prop={'size': 16}) leg = plt.legend(loc=position, shadow=True, ncol=2) # set the linewidth of each legend object for legobj in leg.legendHandles: legobj.set_linewidth(6.0) # remove part of ticks sns.despine() fig.savefig('Multi_Plot_RMSD_' + '_' + title + '_' + selection + '.png', dpi=custom_dpi, bbox_inches='tight') print('RMSD plot created') print('-----------------------------------\n') @hlp.timeit def plot_rg_multi(self, selection, title='LasR Rg', xlabel=r"time $t$ (ns)", ylabel=r"C$_\alpha$ Rg from $t=0$, $\rho_{\mathrm{C}_\alpha}$ (nm)", custom_dpi=600): import pylab as plt sns.set(style="ticks", context='paper') # sns.set(font_scale=2) # In[27]: fig = plt.figure(figsize=(10, 7)) # ax = fig.add_axes([2, 2, 2, 2]) # plt.plot(time_sim1_np, rmsd_sim1_np, 'b') title = 'Cluster Simulation {0}-{1}'.formating(self.sim_indexes[0], self.sim_indexes[-1]) # fig = plt.figure(figsize=(10, 7)) fig.suptitle(title, fontsize=16) # ax = fig.add_axes([2, 2, 2, 2]) # plt.plot(time_sim1_np, rmsd_sim1_np, 'b') for i in self.sim_indexes: self.sim_time = self.simulation_data[str(i)]['time'] traj_rmsd = self.simulation_data[str(i)]['Rg'][selection] curr_label = 'Simulation {0}'.formating(i) curr_color = self.colors_[i - 1] plt.plot(self.sim_time, traj_rmsd, color=curr_color, linewidth=0.6, label=curr_label) # plt.legend(loc="best", prop={'size': 8}) plt.xlabel(xlabel, fontsize=16) plt.ylabel(ylabel, fontsize=16) # fix Angstrom need to change to nm leg = plt.legend(loc='best', shadow=True, prop={'size': 16}) # set the linewidth of each legend object for legobj in leg.legendHandles: legobj.set_linewidth(9.0) # remove part of ticks sns.despine() # In[28]: fig.savefig('Multi_Plot_Rg_' + '_' + title + '_' + selection + '.png', dpi=custom_dpi, bbox_inches='tight') print('Rg plot created') print('-----------------------------------\n') # TODO calculate confidence intervals @hlp.timeit def plot_rmsf_plus_confidence_multi(self, selection, title='LasR RMSF', xlabel=r"Residue", ylabel=r"RMSF(nm)", custom_dpi=600): ''' ylabel=r"C$_\alpha$ RMSF from $t=0$, $\rho_{\mathrm{C}_\alpha}$ (nm)", :param title: :param xlabel: :param ylabel: :param custom_dpi: :return: ''' sns.set(style="ticks", context='paper') # sns.set(font_scale=2) fig = plt.figure(figsize=(14, 7)) title = 'Cluster Simulation {0}-{1}'.formating(self.sim_indexes[0], self.sim_indexes[-1]) # fig = plt.figure(figsize=(10, 7)) fig.suptitle(title, fontsize=16) for i in self.sim_indexes: self.sim_time = self.simulation_data[str(i)]['time'] curr_label = 'Simulation {0}'.formating(i) traj_rmsf = self.simulation_data[str(i)]['rmsf'][selection]['rmsf'] atom_indices_rmsf = self.simulation_data[str(i)]['rmsf'][selection]['ref_atom_indices'] curr_color = self.colors_[i - 1] conv_data = converters.convert_data_to_monkey(atom_indices_rmsf, traj_rmsf, x_axis_name='Residue', y_axis_name='RMSF') conv_data['Residue'] += 1 confidence = hlp.average_confidence_interval(conv_data['RMSF']) # plt.plot(self.sim_time, traj_rmsd, color=curr_color, # linewidth=0.6, label=curr_label) # Plot the response with standard error sns.tsplot(data=conv_data, ci=[95], color="m") # plt.plot(conv_data['x'], conv_data['y'], color=curr_color, # linewidth=0.6, label=curr_label) # plt.xlim(getting_min(conv_data['x']) - 100, getting_max(conv_data['x']) + 100) # traj_rmsf = self.rmsf_analysis_data[selection]['rmsf'] # atom_indices_rmsf = self.rmsf_analysis_data[selection]['atom_indices'] # sns.tsplot(time="x", unit="y", data=conv_data, # size=4, fit_reg=False, # scatter_kws={"s": 50, "alpha": 1}) # sns.plt.show() plt.xlabel(xlabel, fontsize=16) plt.ylabel(ylabel, fontsize=16) # fix Angstrom need to change to nm leg = plt.legend(loc='best', shadow=True, prop={'size': 16}) # set the linewidth of each legend object for legobj in leg.legendHandles: legobj.set_linewidth(9.0) # plt.title(title) # remove part of ticksg sns.despine() fig.savefig('Multi_Plot_RMSF_confidence_' + '_' + title + '_' + selection + '.png', dpi=custom_dpi, bbox_inches='tight') print('RMSF +confidence plot created') @hlp.timeit def prep_mdtraj_object(self, filengthame): ''' Prepare receptor mdtraj object getting mdtraj topology and save as monkey knowledgeframe Calculate mkb receptor center of mass :return: ''' self.receptor_file = filengthame self.receptor_mdtraj = md.load_mkb(self.receptor_file) self.receptor_mdtraj_topology = self.receptor_mdtraj.topology self.receptor_mdtraj_topology_knowledgeframe = self.receptor_mdtraj.topology.to_knowledgeframe() topology = self.receptor_mdtraj.topology atom_indices = topology.select('backbone') test = 1 # self.center_of_mass_receptor = md.compute_center_of_mass(self.receptor_mdtraj)[0] # # self.x_center = math.ceiling(self.center_of_mass_receptor[0] * 10) # self.y_center = math.ceiling(self.center_of_mass_receptor[1] * 10) # self.z_center = math.ceiling(self.center_of_mass_receptor[2] * 10) # # self.receptor_pybel = pybel.reakfile("mkb", self.receptor_file).__next__() # self.ligand_pybel = pybel.reakfile("mkb", self.ligand_file).__next__() test = 1 @hlp.timeit def plot_rmsf_multi(self, selection, title='LasR RMSF', xlabel=r"Residue", ylabel=r"RMSF(nm)", custom_dpi=1200): ''' ylabel=r"C$_\alpha$ RMSF from $t=0$, $\rho_{\mathrm{C}_\alpha}$ (nm)", :param title: :param xlabel: :param ylabel: :param custom_dpi: :return: ''' sns.set(style="ticks", context='paper') sns.set(font_scale=0.8) # fig = plt.figure(figsize=(14, 7)) title = 'Cluster Simulation {0}-{1}'.formating(self.sim_indexes[0], self.sim_indexes[-1]) # fig = plt.figure(figsize=(10, 7)) fig = plt.figure(figsize=plot_tools.cm2inch(8.4, 8.4)) # fig.suptitle(title, fontsize=16) fig.suptitle(title) # self.receptor_mdtraj_topology.atom(3000).residue.resSeq for i in self.sim_indexes: self.sim_time = self.simulation_data[str(i)]['time'] curr_label = 'Simulation {0}'.formating(i) traj_rmsf = self.simulation_data[str(i)]['rmsf'][selection]['rmsf'] atom_indices_rmsf = self.simulation_data[str(i)]['rmsf'][selection]['ref_atom_indices'] curr_color = self.colors_[i - 1] converted_resseq,converted_index = converters.convert_mdtraj_atom_nums_to_resseq(self.receptor_mdtraj_topology, atom_indices_rmsf) conv_data_temp = converters.convert_data_to_monkey(atom_indices_rmsf, traj_rmsf) conv_data = conv_data_temp.ix[converted_index] conv_data['x'] = converted_resseq test = 1 # plt.plot(self.sim_time, traj_rmsd, color=curr_color, # linewidth=0.6, label=curr_label) plt.plot(conv_data['x'], conv_data['y'], color=curr_color, linewidth=0.52, label=curr_label) #plt.xlim(getting_min(conv_data['x']) - 100, getting_max(conv_data['x']) + 100) # traj_rmsf = self.rmsf_analysis_data[selection]['rmsf'] # atom_indices_rmsf = self.rmsf_analysis_data[selection]['atom_indices'] # sns.tsplot(time="x", unit="y", data=conv_data, # size=4, fit_reg=False, # scatter_kws={"s": 50, "alpha": 1}) # sns.plt.show() # plt.xlabel(xlabel, fontsize=16) # plt.ylabel(ylabel, fontsize=16) # fix Angstrom need to change to nm plt.xlabel(xlabel) plt.ylabel(ylabel) # # leg = plt.legend(loc='best', shadow=True, prop={'size': 16}) leg = plt.legend(loc='best', shadow=True) # set the linewidth of each legend object for legobj in leg.legendHandles: legobj.set_linewidth(6.0) # plt.title(title) # remove part of ticksg sns.despine() fig.savefig('Multi_Plot_RMSF_' + '_' + title + '_' + selection + '.png', dpi=custom_dpi, bbox_inches='tight') print('RMSF plot created') def count_lig_hbond(self, t, hbonds, ligand): label = lambda hbond: '%s -- %s' % (t.topology.atom(hbond[0]), t.topology.atom(hbond[2])) hbond_atoms = [] hbond_indexes_sel = [] hbond_count = 0 for hbond in hbonds: res = label(hbond) # print('res ', res) if ligand in res: # print("res is ", res) hbond_atoms.adding(res) hbond_indexes_sel.adding(hbond) hbond_count += 1 test=1 # print('------------------------------------------------') test = 1 return hbond_atoms, hbond_count, hbond_indexes_sel @hlp.timeit def hbond_lig_count_analysis(self, ligand_name='HSL', title='Simulation', xlabel=r"Time $t$ (ns)", ylabel=r"Number of Hydrogen Bonds", custom_dpi=600): sns.set(style="ticks", context='paper') # sns.set(font_scale=2) fig = plt.figure(figsize=(14, 7)) title = 'Simulations of Clusters {0}-{1}'.formating(self.sim_indexes[0], self.sim_indexes[-1]) # fig = plt.figure(figsize=(10, 7)) fig.suptitle(title, fontsize=16) traj_frame = self.simulation_data[str(self.sim_indexes[0])]['clustersCentroid'] self.sim_time = self.simulation_data[str(self.sim_indexes[0])]['time'] t = traj_frame[0] for i in self.sim_indexes: self.sim_time = self.simulation_data[str(i)]['time'] hbonds_frames = self.simulation_data[str(i)]['hbondFrames'] sim_hbond_atoms = [] sim_hbond_count = [] for hbonds in hbonds_frames: hbond_atoms, hbond_count, hbond_indexes_sel = self.count_lig_hbond(t, hbonds, ligand_name) sim_hbond_atoms.adding(hbond_atoms) sim_hbond_count.adding(hbond_count) sim_hbound_np = np.array(sim_hbond_count) self.simulation_data[str(i)].umkate({'hbond_atoms':sim_hbond_atoms}) self.simulation_data[str(i)].umkate({'hbond_count':sim_hbond_count}) curr_color = self.colors_[i - 1] # curr_label = 'Simulation {0}'.formating(i) curr_label = "Simulation of Cluster {0} average: {1}±{2}".formating(i, value_round(np.average(sim_hbound_np),3), value_round(np.standard(sim_hbond_count),3)) # Version 1 plt.plot(self.sim_time, sim_hbond_count, color=curr_color, marker = 'x', linewidth=0.2, label=curr_label) # Version 2 # plt.scatter(self.sim_time, sim_hbond_count, color=curr_color, marker = 'x', # linewidth=0.3, label=curr_label) # data_frame = converters.convert_data_to_monkey(self.sim_time, self.hbond_count) # # y_average_average = data_frame['y'].rolling(center=False, window=20).average() # atom_indices_rmsf = self.simulation_data[str(i)]['rmsf'][selection]['ref_atom_indices'] # curr_color = self.colors_[i - 1] # # conv_data = converters.convert_data_to_monkey(atom_indices_rmsf, traj_rmsf) # # # plt.plot(self.sim_time, traj_rmsd, color=curr_color, # # linewidth=0.6, label=curr_label) # # plt.plot(conv_data['x'], conv_data['y'], color=curr_color, # linewidth=0.6, label=curr_label) # plt.xlim(getting_min(conv_data['x']) - 100, getting_max(conv_data['x']) + 100) test = 1 # traj_rmsf = self.rmsf_analysis_data[selection]['rmsf'] # atom_indices_rmsf = self.rmsf_analysis_data[selection]['atom_indices'] # sns.tsplot(time="x", unit="y", data=conv_data, # size=4, fit_reg=False, # scatter_kws={"s": 50, "alpha": 1}) # sns.plt.show() plt.xlabel(xlabel, fontsize=16) plt.ylabel(ylabel, fontsize=16) # fix Angstrom need to change to nm leg = plt.legend(loc='best', shadow=True, prop={'size': 16}) # set the linewidth of each legend object for legobj in leg.legendHandles: legobj.set_linewidth(9.0) # plt.title(title) # remove part of ticksg sns.despine() fig.savefig('Multi_Plot_HBOND_count_Lig_' + '_' + title + '_' + ligand_name + '.png', dpi=custom_dpi, bbox_inches='tight') print('Multi HBond lig count plot created') @hlp.timeit def hbond_freq_plot_analysis(self, ligand_name='HSL', title='Simulation', xlabel=r"Time $t$ (ns)", ylabel=r"Number of Hydrogen Bonds", custom_dpi=600): sns.set(style="ticks", context='paper') # sns.set(font_scale=2) traj_frame = self.simulation_data[str(self.sim_indexes[0])]['clustersCentroid'] self.sim_time = self.simulation_data[str(self.sim_indexes[0])]['time'] t = traj_frame[0] for i in self.sim_indexes: plt.clf() fig = plt.figure(figsize=(14, 7)) title = 'Simulations of Clusters {0}-{1}'.formating(self.sim_indexes[0], self.sim_indexes[-1]) # fig = plt.figure(figsize=(10, 7)) fig.suptitle(title, fontsize=16) self.sim_time = self.simulation_data[str(i)]['time'] hbonds_frames = self.simulation_data[str(i)]['hbondFrames'] sim_hbond_atoms = [] sim_hbond_count = [] sim_hbond_sel = [] for hbonds in hbonds_frames: hbond_atoms, hbond_count, hbond_indexes_sel = self.count_lig_hbond(t, hbonds, ligand_name) sim_hbond_atoms.adding(hbond_atoms) sim_hbond_count.adding(hbond_count) if length( hbond_indexes_sel) > 0: sim_hbond_sel+= hbond_indexes_sel sim_hbound_np = np.array(sim_hbond_count) sim_hbound_sel_np = np.array(sim_hbond_sel) # self.simulation_data[str(i)].umkate({'hbond_atoms':sim_hbond_atoms}) # self.simulation_data[str(i)].umkate({'hbond_count':sim_hbond_count}) # curr_color = self.colors_[i - 1] # curr_label = 'Simulation {0}'.formating(i) curr_label = "Simulation of Cluster {0} average: {1}±{2}".formating(i, value_round(np.average(sim_hbound_np),3), value_round(np.standard(sim_hbond_count),3)) # This won't work here da_distances = md.compute_distances(t, sim_hbound_sel_np[:, [0, 2]], periodic=False) # Version 1 # plt.plot(self.sim_time, sim_hbond_count, color=curr_color, marker = 'x', # linewidth=0.2, label=curr_label) # color = itertools.cycle(['r', 'b', 'gold']) colors = sns.cubehelix_palette(n_colors=length(da_distances), rot=-.4) # self.colors_ = colors label = lambda hbond: '%s -- %s' % (t.topology.atom(hbond[0]), t.topology.atom(hbond[2])) color = itertools.cycle(['r', 'b', 'gold']) for i in [0]: plt.hist(da_distances[:, i], color=colors[i], label=label(sim_hbound_sel_np[i]), alpha=0.5) plt.legend() plt.ylabel('Freq'); plt.xlabel('Donor-acceptor distance [nm]') # plt.xlabel(xlabel, fontsize=16) # plt.ylabel(ylabel, fontsize=16) # fix Angstrom need to change to nm # # leg = plt.legend(loc='best', shadow=True, prop={'size': 16}) # # # set the linewidth of each legend object # for legobj in leg.legendHandles: # legobj.set_linewidth(9.0) sns.despine() fig.savefig('Multi_Plot_HBOND_frequency_' + '_' + title + '_' + str(i)+ '_'+ ligand_name + '.png', dpi=custom_dpi, bbox_inches='tight') print('Multi HBond frequency lig plot created') @hlp.timeit def plot_solvent_area_multi(self, show=False): fig = plt.figure(figsize=(10, 10)) plt.plot(self.sasa_traj.time, self.total_sasa) plt.xlabel('Time [ps]', size=16) plt.ylabel('Total SASA (nm)^2', size=16) if show is True: plt.show() fig.savefig(self.simulation_name + '_' + 'SASA_plot.png', dpi=300, bbox_inches='tight') @hlp.timeit def plot_solvent_area_frame_multi(self, frame, show=False): fig = plt.figure(figsize=(10, 10)) plt.plot(self.sasa_traj.time, self.sasa[frame]) plt.xlabel('Time [ps]', size=16) plt.ylabel('Total SASA (nm)^2', size=16) if show is True: plt.show() fig.savefig(self.simulation_name + '_' + 'SASA_plot_{0}.png'.formating(frame), dpi=300, bbox_inches='tight') @hlp.timeit def plot_solvent_area_autocorr_multi(self, show=False): self.sasa_autocorr = protein_analysis.autocorr(self.total_sasa) fig = plt.figure(figsize=(10, 10)) plt.semilogx(self.sasa_traj.time, self.sasa_autocorr) plt.xlabel('Time [ps]', size=16) plt.ylabel('SASA autocorrelation', size=16) if show is True: plt.show() fig.savefig(self.simulation_name + '_' + 'SASA_autocorrelation.png', dpi=300, bbox_inches='tight') @hlp.timeit def plot_rmsd_cluster_color_multi(self, selection, title='LasR RMSD', xlabel=r"Time $t$ (ns)", ylabel=r"RMSD(nm)", custom_dpi=300, lang='rus'): import pylab as plt sns.set(style="ticks", context='paper') ''' ylabel=r"C$_\alpha$ RMSD from $t=0$, $\rho_{\mathrm{C}_\alpha}$ (nm)", ''' fig = plt.figure(figsize=(14, 7)) # ax = fig.add_axes([2, 2, 2, 2]) # plt.plot(time_sim1_np, rmsd_sim1_np, 'b') # plt.plot(self.sim_time, self.sim_rmsd, color=self.cluster_colors, # linewidth=0.6, label='LasR') if lang == 'rus': title = 'Симуляция' xlabel = r"Время $t$ (нс)" ylabel = r"RMSD(нм)" else: title = 'Simulation' xlabel = r"Time $t$ (ns)" ylabel = r"RMSD(nm)" sns.set(font_scale=2) plt.plot(self.sim_time, self.sim_rmsd, zorder=1) traj_rmsd = self.rmsd_analysis_data[selection] plt.scatter(self.sim_time, traj_rmsd, marker='o', s=30, facecolor='0.5', lw=0, c=self.cluster_colors, zorder=2) # plt.legend(loc="best", prop={'size': 8}) plt.xlabel(xlabel) plt.xlim(self.sim_time[0], self.sim_time[-1]) plt.ylabel(ylabel) # fix Angstrom need to change to nm plt.title(title) fig.tight_layout() # remove part of ticks sns.despine() # plt.show() fig.savefig(self.simulation_name + '_' + title + '_' + selection + '_cluster_color' + '_' + lang + '.png', dpi=custom_dpi, bbox_inches='tight') print('RMSD plot created') print('-----------------------------------\n') @hlp.timeit def find_best_fit_regressor(self): # from sklearn.tree import DecisionTreeRegressor self.best = 100 self.index = 100 self.best_rg = 100 self.index_rg = 100 self.regr_index = [] self.regr_scores = {} self.regr_index_rg = [] self.regr_scores_rg = {} self.reshaped_time = self.sim_time.reshape(-1, 1) for i in list(range(1, self.regression_fit_range + 1)): self.create_fit(i) print('best score is ', self.best) print('best index is', self.index) print('-=-' * 10) print('best score Rg is ', self.best_rg) print('best index Rg is', self.index_rg) @hlp.timeit def create_fit(self, i): from sklearn import tree from sklearn.model_selection import cross_val_score self.reshaped_time = self.sim_time.reshape(-1, 1) regressor = tree.DecisionTreeRegressor(getting_max_depth=i) # interesting absolutely fitVal = regressor.fit(self.reshaped_time, self.sim_rmsd) print('fitVal ', fitVal) rmsd_pred = regressor.predict(self.reshaped_time) # cv how is it detergetting_mined? # A good compromise is ten-fold cross-validation. 10ns # Maybe mse better? cross_val = cross_val_score(regressor, self.reshaped_time, self.sim_rmsd, scoring="neg_average_squared_error", cv=10) regressor_rg = tree.DecisionTreeRegressor(getting_max_depth=i) # interesting absolutely fitVal_rg = regressor_rg.fit(self.reshaped_time, self.rg_res) fitVal_rg = regressor_rg.fit(self.reshaped_time, self.rg_res) print('fitVal ', fitVal) rmsd_pred_rg = regressor_rg.predict(self.reshaped_time) # cv how is it detergetting_mined? # A good compromise is ten-fold cross-validation. 10ns cross_val_rg = cross_val_score(regressor, self.reshaped_time, self.rg_res, scoring="neg_average_squared_error", cv=10) self.regr_scores.umkate({i: cross_val}) self.regr_index.adding(i) self.regr_scores_rg.umkate({i: cross_val_rg}) self.regr_index_rg.adding(i) cross_val_score = -cross_val.average() cross_val_standard = cross_val.standard() cross_val_score_rg = -cross_val_rg.average() cross_val_standard_rg = cross_val_rg.standard() print('Cross validation score is ', cross_val) print("Degree {}\nMSE = {:.2e}(+/- {:.2e})".formating(i, -cross_val.average(), cross_val.standard())) print('-=-' * 10) print('Cross validation Rg score is ', cross_val_rg) print("Rg Degree {}\nMSE = {:.2e}(+/- {:.2e})".formating(i, -cross_val_rg.average(), cross_val_rg.standard())) # r2_score = regressor.score(self.sim_time.reshape(-1, 1), self.sim_rmsd) # if r2_score > self.r2_best: # self.r2_best = r2_score # self.r2_index = i if cross_val_score < self.best: self.best = cross_val_score self.index = i if cross_val_score_rg < self.best_rg: self.best_rg = cross_val_score_rg self.index_rg = i del regressor del fitVal del rmsd_pred time.sleep(2) # print('R2 score is ', r2_score) print('---------------------------------------------------------------\n') @hlp.timeit def error_bar_rmsd_fit(self): import matplotlib.pyplot as plt x = self.regr_index y = [] yerr_list = [] for i in self.regr_index: # plt.boxplot(self.regr_scores[i]) cross_val_score = -self.regr_scores[i].average() cross_val_standard = self.regr_scores[i].standard() y.adding(cross_val_score) yerr_list.adding(cross_val_standard) fig = plt.figure(figsize=(10, 10)) plt.errorbar(x, y, yerr=yerr_list) plt.scatter(x, y, s=160, c='b', marker='h', label="Best score at Max Depth={}\nMSE = {:.2e}(+/- {:.2e})".formating(self.index, -self.regr_scores[ self.index].average(), self.regr_scores[ self.index].standard())) plt.legend(loc="best", prop={'size': 20}) plt.title("Mean squared error (MSE) averages for RMSD") fig.savefig(self.simulation_name + '_errorBar_rmsd.png', dpi=300, bbox_inches='tight') # plt.show() print('Errorbar created ') print('---------------------------------------------------------------\n') @hlp.timeit def error_bar_Rg_fit(self): import matplotlib.pyplot as plt x = self.regr_index y = [] yerr_list = [] for i in self.regr_index: # plt.boxplot(self.regr_scores[i]) cross_val_score = -self.regr_scores_rg[i].average() cross_val_standard = self.regr_scores_rg[i].standard() y.adding(cross_val_score) yerr_list.adding(cross_val_standard) fig = plt.figure(figsize=(10, 10)) plt.errorbar(x, y, yerr=yerr_list) plt.scatter(x, y, s=160, c='b', marker='h', label="Best score at Max Depth={}\nMSE = {:.2e}(+/- {:.2e})".formating(self.index_rg, -self.regr_scores_rg[ self.index_rg].average(), self.regr_scores_rg[ self.index_rg].standard())) plt.legend(loc="best", prop={'size': 20}) plt.title("Mean squared error (MSE) averages for Rg") fig.savefig(self.simulation_name + '_errorBar_Rg.png', dpi=300, bbox_inches='tight') # plt.show() print('Errorbar created ') print('---------------------------------------------------------------\n') @hlp.timeit def error_bar_fit_test(self): import numpy as np import matplotlib.pyplot as plt # example data x = np.arange(0.1, 4, 0.5) y = np.exp(-x) # example variable error bar values yerr = 0.1 + 0.2 * np.sqrt(x) xerr = 0.1 + yerr # First illustrate basic pyplot interface, using defaults where possible. plt.figure() plt.errorbar(x, y, xerr=0.2, yerr=0.4) plt.title("Simplest errorbars, 0.2 in x, 0.4 in y") # Now switch to a more OO interface to exercise more features. fig, axs = plt.subplots(nrows=2, ncols=2, sharex=True) ax = axs[0, 0] ax.errorbar(x, y, yerr=yerr, fmt='o') ax.set_title('Vert. symmetric') # With 4 subplots, reduce the number of axis ticks to avoid crowding. ax.locator_params(nbins=4) ax = axs[0, 1] ax.errorbar(x, y, xerr=xerr, fmt='o') ax.set_title('Hor. symmetric') ax = axs[1, 0] ax.errorbar(x, y, yerr=[yerr, 2 * yerr], xerr=[xerr, 2 * xerr], fmt='--o') ax.set_title('H, V asymmetric') ax = axs[1, 1] ax.set_yscale('log') # Here we have to be careful to keep total_all y values positive: ylower = np.getting_maximum(1e-2, y - yerr) yerr_lower = y - ylower ax.errorbar(x, y, yerr=[yerr_lower, 2 * yerr], xerr=xerr, fmt='o', ecolor='g', capthick=2) ax.set_title('Mixed sym., log y') fig.suptitle('Variable errorbars') plt.show() @hlp.timeit def plot_boxplot_fit_regr(self): data_to_plot = [] for i in self.regr_index: # plt.boxplot(self.regr_scores[i]) data_to_plot.adding(self.regr_scores[i]) # Create a figure instance fig = plt.figure(figsize=(10, 10)) # Create an axes instance ax = fig.add_subplot(111) # Create the boxplot # change outlier to hexagon # bp = ax.boxplot(data_to_plot, 0, 'gD') # dont show outlier bp = ax.boxplot(data_to_plot, 0, '') # Save the figure fig.savefig(self.simulation_name + '_boxplot.png', dpi=600, bbox_inches='tight') # plt.show() print('Box plot created ') print('---------------------------------------------------------------\n') @hlp.timeit def example_test(self): import matplotlib.pyplot as plt from sklearn.pipeline import Pipeline from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression from sklearn.model_selection import cross_val_score degrees = [1, 4, 8, 15, 20] # true_fun = lambda X: np.cos(1.5 * np.pi * X) X = self.sim_time y = self.sim_rmsd plt.figure(figsize=(14, 5)) for i in range(length(degrees)): ax = plt.subplot(1, length(degrees), i + 1) plt.setp(ax, xticks=(), yticks=()) polynomial_features = PolynomialFeatures(degree=degrees[i], include_bias=False) linear_regression = LinearRegression() pipeline = Pipeline([("polynomial_features", polynomial_features), ("linear_regression", linear_regression)]) pipeline.fit(X, y) # Evaluate the models using crossvalidation scores = cross_val_score(pipeline, X, y, scoring="neg_average_squared_error", cv=10) X_test = self.sim_time plt.plot(X_test, pipeline.predict(X_test), label="Model") plt.plot(X_test, self.sim_rmsd, label="True function") plt.scatter(X, y, label="Samples") plt.xlabel("x") plt.ylabel("y") plt.xlim((0, 1)) plt.ylim((-2, 2)) plt.legend(loc="best") plt.title("Degree {}\nMSE = {:.2e}(+/- {:.2e})".formating( degrees[i], -scores.average(), scores.standard())) plt.show() @hlp.timeit def plot_rmsd_with_regressor(self, title='LasR Simulation RMSD', xlabel=r"time $t$ (ns)", ylabel=r"C$_\alpha$ RMSD from $t=0$, $\rho_{\mathrm{C}_\alpha}$ (nm)"): import pylab as plt from sklearn import tree rfc = tree.DecisionTreeRegressor(getting_max_depth=self.index) # interesting absolutely fitVal = rfc.fit(self.sim_time.reshape(-1, 1), self.sim_rmsd) print('fitVal ', fitVal) self.rmsd_pred = rfc.predict(self.sim_time.reshape(-1, 1)) fig = plt.figure(figsize=(7, 7)) ax = fig.add_axes([2, 2, 2, 2]) # plt.plot(time_sim1_np, rmsd_sim1_np, 'b') plt.plot(self.sim_time, self.sim_rmsd, color='b', linewidth=0.6, label='Original Data') plt.plot(self.sim_time, self.rmsd_pred, color='r', linewidth=4, label='Fitted Data') plt.legend(loc="best", prop={'size': 30}) plt.xlabel(xlabel) plt.ylabel(ylabel) # fix Angstrom need to change to nm plt.title(title) # In[28]: fig.savefig(self.simulation_name + '_' + title + '_tree' + '.png', dpi=300, bbox_inches='tight') print('RMSD plot created with regressor') print('-----------------------------------\n') @hlp.timeit def plot_Rg_with_regressor(self, title='LasR Radius of Gyration', xlabel=r"time $t$ (ns)", ylabel=r"C$_\alpha$ RMSD from $t=0$, $\rho_{\mathrm{C}_\alpha}$ (nm)"): import pylab as plt from sklearn import tree rfc = tree.DecisionTreeRegressor(getting_max_depth=self.index_rg) # interesting absolutely fitVal = rfc.fit(self.sim_time.reshape(-1, 1), self.rg_res) print('fitVal ', fitVal) self.rmsd_pred_rg = rfc.predict(self.sim_time.reshape(-1, 1)) fig = plt.figure(figsize=(7, 7)) ax = fig.add_axes([2, 2, 2, 2]) # plt.plot(time_sim1_np, rmsd_sim1_np, 'b') plt.plot(self.sim_time, self.rg_res, color='b', linewidth=0.6, label='Original Data') plt.plot(self.sim_time, self.rmsd_pred_rg, color='r', linewidth=4, label='Fitted Data') plt.legend(loc="best", prop={'size': 30}) plt.xlabel(xlabel) plt.ylabel(ylabel) # fix Angstrom need to change to nm plt.title(title) # In[28]: fig.savefig(self.simulation_name + '_' + title + '_tree' + '.png', dpi=300, bbox_inches='tight') print('RMSD plot created with regressor') print('-----------------------------------\n') @hlp.timeit def md_full_load(self, custom_stride=10): print('MD Load has been ctotal_alled\n') print('-------------------------------\n') self.full_traj = md.load(self.md_trajectory_file, top=self.md_topology_file, stride=custom_stride) self.sim_time = self.full_traj.time / 1000 print("Full trajectory loaded successfully") print('-----------------------------------\n') @hlp.timeit def rg_analysis(self, selection='protein'): self.ctotal_alled_rg_analysis = True # self.rg_traj = self.full_traj[:] # # self.topology = self.rmsd_traj.topology # # self.selection = self.topology.select(selection) # # # self.selection = self.topology.select(selection) # # print('selection is ', self.selection) # # self.rg_traj.restrict_atoms(self.selection) self.topology = self.full_traj.topology self.selection = self.topology.select(selection) self.rg_traj = self.full_traj.atom_slice(atom_indices=self.selection) self.rg_res = md.compute_rg(self.rg_traj) self.rg_analysis_data.umkate({selection: self.rg_res}) print("Rg has been calculated") print('-----------------------------------\n') @hlp.timeit def hbond_analysis_count(self, selection='protein', title='LasR H-Bonds', xlabel=r"Time $t$ (ns)", ylabel=r"Number of Hydrogen Bonds", custom_dpi=300): sns.set(style="ticks", context='paper') self.ctotal_alled_hbond_analysis_count = True print('HBonds analysis has been ctotal_alled\n') print('-------------------------------\n') self.topology = self.full_traj.topology self.selection = self.topology.select(selection) print('selection is ', self.selection) # this is for keeping selection from trajectory # self.full_traj.restrict_atoms(self.selection) self.hbond_count = [] self.sim_time = self.full_traj.time / 1000 # paral = Pool(processes=16) # data_count = list(mapping(self.hbond_frame_calc, self.full_traj)) # # print('data count ',data_count) # hbonds = md.baker_hubbard(self.full_traj, exclude_water=True, periodic=False) # print('count of hbonds is ', length(hbonds)) # self.hbond_count.adding(length(hbonds)) hbonds_frames = md.wernet_nilsson(self.full_traj, exclude_water=True, periodic=False) self.hbonds_frames = hbonds_frames for hbonds in hbonds_frames: self.hbond_count.adding(length(hbonds)) data_frame = converters.convert_data_to_monkey(self.sim_time, self.hbond_count) y_average_average = data_frame['y'].rolling(center=False, window=20).average() fig = plt.figure(figsize=(7, 7)) # ax = fig.add_axes([2, 2, 2, 2]) # plt.plot(time_sim1_np, rmsd_sim1_np, 'b') plt.plot(data_frame['x'], data_frame['y'], color='b', linewidth=0.6, label='LasR') # Dont plot rolling average plt.plot(data_frame['x'], y_average_average, color='r', linewidth=0.9, label='LasR rolling average') # plt.legend(loc="best", prop={'size': 8}) plt.xlabel(xlabel) plt.ylabel(ylabel) # fix Angstrom need to change to nm plt.title(title) # remove part of ticks sns.despine() fig.savefig(self.simulation_name + '_' + title + '.png', dpi=custom_dpi, bbox_inches='tight') print('HBond count plot created') print('-----------------------------------\n') # for hbond in hbonds: # print(hbond) # print(label(hbond)) # atom1 = self.full_traj.topology.atom(hbond[0]) # atom2 = self.full_traj.topology.atom(hbond[2]) # # atom3 = traj_sim1_hbonds.topology.atom(hbond[2]) # if atom1.residue.resSeq != atom2.residue.resSeq: # if atom1.residue.resSeq + 1 != atom2.residue.resSeq: # # for domain reside analysis # if atom1.residue.resSeq < 171 and atom2.residue.resSeq > 172: # diff_hbonds.adding(hbond) @hlp.timeit def hbond_analysis(self, selection='protein'): self.topology = self.full_traj.topology self.selection = self.topology.select(selection) print('selection is ', self.selection) # this is for keeping selection from trajectory self.full_traj.restrict_atoms(self.selection) if self.save_mkb_hbond is True: traj_sim1_hbonds = md.load_mkb(self.mkb_file_name) hbonds = md.baker_hubbard(traj_sim1_hbonds, periodic=False) # hbonds = md.wernet_nilsson(traj_sim1_hbonds, periodic=True)[0] label = lambda hbond: '%s -- %s' % (traj_sim1_hbonds.topology.atom(hbond[0]), traj_sim1_hbonds.topology.atom(hbond[2])) diff_hbonds = [] for hbond in hbonds: # print(hbond) # print(label(hbond)) atom1 = traj_sim1_hbonds.topology.atom(hbond[0]) atom2 = traj_sim1_hbonds.topology.atom(hbond[2]) # atom3 = traj_sim1_hbonds.topology.atom(hbond[2]) if atom1.residue.resSeq != atom2.residue.resSeq: if atom1.residue.resSeq + 1 != atom2.residue.resSeq: # domain reside analysis if atom1.residue.resSeq < 171 and atom2.residue.resSeq > 172: diff_hbonds.adding(hbond) for hbond in diff_hbonds: print(hbond) print(label(hbond)) print('Diff hbonds printed\n') diff_hbonds = np.asarray(diff_hbonds) self.da_distances = md.compute_distances(traj_sim1_hbonds, diff_hbonds[:, [0, 2]], periodic=False) import itertools # color = itertools.cycle(['r', 'b', 'gold']) # fig = plt.figure(figsize=(7, 7)) # color = np.linspace(0, length(diff_hbonds),length(diff_hbonds)) # # # color = itertools.cycle(['r', 'b','g','gold']) # for i in list(range(0,length(diff_hbonds))): # plt.hist(self.da_distances[:, i], color=next(color), label=label(diff_hbonds[i]), alpha=0.5) # plt.legend() # plt.ylabel('Freq'); # plt.xlabel('Donor-acceptor distance [nm]') # plt.show() # this works wel, but needs to be modified fig = plt.figure(figsize=(7, 7)) color = np.linspace(0, length(diff_hbonds), length(diff_hbonds)) color = itertools.cycle(['r', 'b', 'g', 'tan', 'black', 'grey', 'yellow', 'gold']) for i in list(range(0, length(diff_hbonds))): plt.hist(self.da_distances[:, i], color=next(color), label=label(diff_hbonds[i]), alpha=0.5) plt.legend() plt.ylabel('Freq'); plt.xlabel('Donor-acceptor distance [nm]') plt.show() fig.savefig(self.simulation_name + '_hbonds.png', dpi=600, bbox_inches='tight') print("Hbonds have been calculated") print('-----------------------------------\n') @hlp.timeit def rmsd_analysis(self, selection): ''' :param selection: has to be mdtraj compatible :return: ''' self.ctotal_alled_rmsd_analysis = True # self.rmsd_traj = self.full_traj[:] # # self.topology = self.rmsd_traj.topology # # self.selection = self.topology.select(selection) # # # self.selection = self.topology.select(selection) # # print('selection is ', self.selection) # # self.rmsd_traj.restrict_atoms(self.selection) # self.full_traj.save(selection +'.mkb') # this is for keeping selection from trajectory # self.rmsd_traj.restrict_atoms(self.selection) # self.rmsd_traj = self.full_traj[:] self.topology = self.full_traj.topology self.selection = self.topology.select(selection) # self.selection = self.topology.select(selection) # print('selection is ', self.selection) self.rmsd_traj = self.full_traj.atom_slice(atom_indices=self.selection) self.sim_rmsd = md.rmsd(self.rmsd_traj, self.rmsd_traj, 0) self.sim_time = self.rmsd_traj.time / 1000 self.rmsd_analysis_data.umkate({selection: self.sim_rmsd}) self.regression_fit_range = 10 print('RMSD analysis has been ctotal_alled on selection {0}\n'.formating(selection)) print('-----------------------------\n') @hlp.timeit def plot_rmsd_cluster_color(self, selection, title='LasR RMSD', xlabel=r"Time $t$ (ns)", ylabel=r"RMSD(nm)", custom_dpi=300, lang='rus'): import pylab as plt sns.set(style="ticks", context='paper') ''' ylabel=r"C$_\alpha$ RMSD from $t=0$, $\rho_{\mathrm{C}_\alpha}$ (nm)", ''' fig = plt.figure(figsize=(14, 7)) # ax = fig.add_axes([2, 2, 2, 2]) # plt.plot(time_sim1_np, rmsd_sim1_np, 'b') # plt.plot(self.sim_time, self.sim_rmsd, color=self.cluster_colors, # linewidth=0.6, label='LasR') if lang == 'rus': title = 'Симуляция' xlabel = r"Время $t$ (нс)" ylabel = r"RMSD(нм)" else: title = 'Simulation' xlabel = r"Time $t$ (ns)" ylabel = r"RMSD(nm)" sns.set(font_scale=2) plt.plot(self.sim_time, self.sim_rmsd, zorder=1) traj_rmsd = self.rmsd_analysis_data[selection] plt.scatter(self.sim_time, traj_rmsd, marker='o', s=30, facecolor='0.5', lw=0, c=self.cluster_colors, zorder=2) # plt.legend(loc="best", prop={'size': 8}) plt.xlabel(xlabel) plt.xlim(self.sim_time[0], self.sim_time[-1]) plt.ylabel(ylabel) # fix Angstrom need to change to nm plt.title(title) fig.tight_layout() # remove part of ticks sns.despine() # plt.show() fig.savefig(self.simulation_name + '_' + title + '_' + selection + '_cluster_color' + '_' + lang + '.png', dpi=custom_dpi, bbox_inches='tight') print('RMSD plot created') print('-----------------------------------\n') @hlp.timeit def plot_rmsf(self, selection, title='LasR RMSF', xlabel=r"Residue", ylabel=r"RMSF(nm)", custom_dpi=300): ''' ylabel=r"C$_\alpha$ RMSF from $t=0$, $\rho_{\mathrm{C}_\alpha}$ (nm)", :param title: :param xlabel: :param ylabel: :param custom_dpi: :return: ''' sns.set(style="ticks", context='paper') sns.set(font_scale=2) traj_rmsf = self.rmsf_analysis_data[selection]['rmsf'] atom_indices_rmsf = self.rmsf_analysis_data[selection]['atom_indices'] conv_data = converters.convert_data_to_monkey(atom_indices_rmsf, traj_rmsf) # sns.tsplot(time="x", unit="y", data=conv_data, # size=4, fit_reg=False, # scatter_kws={"s": 50, "alpha": 1}) # sns.plt.show() fig = plt.figure(figsize=(14, 7)) plt.plot(conv_data['x'], conv_data['y'], color='b', linewidth=0.6, label=title) plt.xlabel(xlabel) plt.xlim(getting_min(conv_data['x']) - 100, getting_max(conv_data['x']) + 100) plt.ylabel(ylabel) # fix Angstrom need to change to nm plt.title(title) # remove part of ticks sns.despine() fig.savefig(self.simulation_name + '_' + title + '_rmsf.png', dpi=custom_dpi, bbox_inches='tight') print('RMSF plot created') @hlp.timeit def plot_rg(self, selection, title='LasR Rg', xlabel=r"time $t$ (ns)", ylabel=r"C$_\alpha$ Rg from $t=0$, $\rho_{\mathrm{C}_\alpha}$ (nm)", custom_dpi=600): import pylab as plt sns.set(style="ticks", context='paper') sns.set(font_scale=2) # In[27]: fig = plt.figure(figsize=(7, 7)) ax = fig.add_axes([2, 2, 2, 2]) # plt.plot(time_sim1_np, rmsd_sim1_np, 'b') traj_rg = self.rg_analysis_data[selection] plt.plot((self.sim_time), traj_rg, color='b', linewidth=0.6, label='LasR') plt.legend(loc="best", prop={'size': 8}) plt.xlabel(xlabel) plt.ylabel(ylabel) # fix Angstrom need to change to nm plt.title(title) # In[28]: fig.savefig(self.simulation_name + '_' + title + '_' + selection + '.png', dpi=custom_dpi, bbox_inches='tight') print('RMSD plot created') print('-----------------------------------\n') # need to select only protein for analysis @hlp.timeit def find_centroid(self): atom_indices = [a.index for a in self.full_traj.topology.atoms if a.element.symbol != 'H'] distances = np.empty((self.full_traj.n_frames, self.full_traj.n_frames)) for i in range(self.full_traj.n_frames): distances[i] = md.rmsd(self.full_traj, self.full_traj, i, atom_indices=atom_indices) beta = 1 index = np.exp(-beta * distances / distances.standard()).total_sum(axis=1).arggetting_max() print(index) centroid = self.full_traj[index] print(centroid) centroid.save('centroid.mkb') #################################################################################################################### # TODO do PCA transformatingion of MD simulation @hlp.timeit def md_pca_analysis(self, selection='protein'): self.ctotal_alled_md_pca_analysis = True print('PCA analysis has been ctotal_alled\n') print('-------------------------------\n') pca1 = PCA(n_components=2) # this is for keeping selection from trajectory # self.pca_traj = self.full_traj[:] # # self.topology = self.pca_traj.topology # # self.selection = self.topology.select(selection) # # # self.selection = self.topology.select(selection) # # print('selection is ', self.selection) # # self.pca_traj.restrict_atoms(self.selection) # self.full_traj.save(selection +'.mkb') self.topology = self.full_traj.topology self.selection = self.topology.select(selection) self.pca_traj = self.full_traj.atom_slice(atom_indices=self.selection) self.pca_traj.superpose(self.pca_traj, 0) self.reduced_cartesian = pca1.fit_transform( self.pca_traj.xyz.reshape(self.pca_traj.n_frames, self.pca_traj.n_atoms * 3)) print(self.reduced_cartesian.shape) print("PCA transformatingion finished successfully") print('-----------------------------------\n') #################################################################################################################### @hlp.timeit def extract_info_cluster_data(self, cluster_data, key): temp_data = [] for clust_num in self.range_n_clusters: temp_data.adding(cluster_data[clust_num][key]) return temp_data @hlp.timeit def silhouette_graph_pca(self): cluster_range = self.range_n_clusters score = self.sil_pca criteria_name = 'Mean Silhouette Coefficient for total_all sample_by_nums' score_text = 'Objects with a high silhouette value are considered well clustered' plot_tools.plot_cluster_analysis(cluster_range, score, criteria_name, score_text) def calinski_graph_pca(self): cluster_range = self.range_n_clusters score = self.calinski_pca criteria_name = 'Calinski-Harabaz score' score_text = 'Objects with a high Calinski-Harabaz score value are considered well clustered' plot_tools.plot_cluster_analysis(cluster_range, score, criteria_name, score_text) @hlp.timeit def dunn_graph_pca(self): cluster_range = self.range_n_clusters score = self.dunn_pca criteria_name = "Dunn's Index" score_text = "Maximum value of the index represents the right partitioning given the index" plot_tools.plot_cluster_analysis(cluster_range, score, criteria_name, score_text) @hlp.timeit def dbi_graph_pca(self): cluster_range = self.range_n_clusters score = self.dbi_pca criteria_name = 'Davis-Bouldain Index' score_text = 'The optimal clustering solution has the smtotal_allest Davies-Bouldin index value.' plot_tools.plot_cluster_analysis(cluster_range, score, criteria_name, score_text) @hlp.timeit def select_number_of_clusters(self): # ["foo", "bar", "baz"].index("bar") getting_max_silhouette = getting_max(self.sil_pca) getting_max_dunn = getting_max(self.dunn_pca) getting_min_dbi = getting_min(self.dbi_pca) sil_index = self.sil_pca.index(getting_max_silhouette) dunn_index = self.dunn_pca.index(getting_max_dunn) dbi_index = self.dbi_pca.index(getting_min_dbi) cluster_quantity = [] cluster_quantity.adding(self.range_n_clusters[sil_index]) cluster_quantity.adding(self.range_n_clusters[dunn_index]) cluster_quantity.adding(self.range_n_clusters[dbi_index]) print('------------------------------------------------') print('verify yolo', cluster_quantity) cluster_set = set(cluster_quantity) cluster_dict = {} for n_set in cluster_set: count = cluster_quantity.count(n_set) cluster_dict.umkate({n_set: count}) print('verify yolo ', cluster_dict) import operator clust_num = getting_max(cluster_dict.items(), key=operator.itemgettingter(1))[0] print("number of clusters is ", clust_num) return clust_num # def write_model_to_file(self, model, resnum=None, filengthame_mkb=None): # curr_kf = model['molDefinal_item_tail']['knowledgeframe'] # mkb_tools.write_lig(curr_kf, resnum, filengthame_mkb) # need to select only protein for analysis @hlp.timeit def find_getting_max_cluster(self): lengthgth = 0 clust_temp_data = [] for k in self.clusterized_data: data = self.clusterized_data[k] if length(data) > lengthgth: lengthgth = length(data) clust_temp_data = data self.getting_max_clust_temp_data = clust_temp_data return self.getting_max_clust_temp_data @hlp.timeit def find_clusters_centroid(self): print('Find Clusters centroids is ctotal_alled\n') print('-----------------------------------\n') self.ctotal_alled_find_clusters_centroid = True self.clusters_centroids = [] for k in self.clusterized_data: print('Finding centroid for cluster {0}'.formating(k)) clust_temp_data = self.clusterized_data[k] atom_indices = [a.index for a in clust_temp_data.topology.atoms if a.element.symbol != 'H'] distances = np.empty((clust_temp_data.n_frames, clust_temp_data.n_frames)) for i in range(clust_temp_data.n_frames): distances[i] = md.rmsd(clust_temp_data, clust_temp_data, i, atom_indices=atom_indices) beta = 1 index = np.exp(-beta * distances / distances.standard()).total_sum(axis=1).arggetting_max() print(index) centroid = clust_temp_data[index] # self.centroid_conf = centroid # print(centroid) # self.centroid_conf = centroid self.clusters_centroids.adding(centroid) centroid.save(self.simulation_name + '_' + '{0}_cluster_centroid.mkb'.formating(k)) print('-----------------------------------\n') @hlp.timeit def find_getting_max_cluster_centroid(self): print('Find Max Cluster centroid is ctotal_alled\n') print('-----------------------------------\n') self.ctotal_alled_find_getting_max_cluster_centroid = True clust_temp_data = self.getting_max_clust_temp_data atom_indices = [a.index for a in clust_temp_data.topology.atoms if a.element.symbol != 'H'] distances = np.empty((clust_temp_data.n_frames, clust_temp_data.n_frames)) for i in range(clust_temp_data.n_frames): distances[i] = md.rmsd(clust_temp_data, clust_temp_data, i, atom_indices=atom_indices) beta = 1 index = np.exp(-beta * distances / distances.standard()).total_sum(axis=1).arggetting_max() print(index) centroid = clust_temp_data[index] self.centroid_conf = centroid print(centroid) self.centroid_conf = centroid centroid.save(self.simulation_name + '_' + 'getting_max_cluster_centroid.mkb') print('-----------------------------------\n') # need to find a way to extract models correctrly @hlp.timeit def export_cluster_models(self, selection_obj='protein', select_lig=None, save_data=False, nth_frame=1): ''' Save cluster data to mkb files in cluster_traj directory :return: ''' n_clusters = self.select_number_of_clusters() cluster_labels = self.clusters_info[n_clusters]['labels'] labels = cluster_labels sample_by_num_silhouette_values = self.clusters_info[n_clusters]['silhouette_values'] silhouette_avg = self.clusters_info[n_clusters]['silhouette'] centers = self.clusters_info[n_clusters]['centers'] distinctive_labels = list(set(cluster_labels)) print('Unique labels ', distinctive_labels) original_data = self.full_traj self.clusterized_data = {} for k in distinctive_labels: # Need to modify WORKS # print('k is ',k) # k == -1 then it is an outlier if k != -1: cluster_data = [] xyz = original_data[labels == k] # sel_traj = xyz[:] topology = xyz.topology selection_name = selection_obj selection_final_name = selection_obj selection = topology.select(selection_obj) selection_final = selection if select_lig is not None: # selection1 = topology.select(select_lig) # selection_final = np.concatingenate((selection, selection1)) # selection_name = selection_name + ' and ' + select_lig # # selection_final = list(topology.select(selection_obj)) + list(topology.select(select_lig)) selection_final_name = selection_obj + '+' + select_lig selection_final = topology.select(selection_obj + ' or ' + select_lig) # list(topology.select(selection_obj)) + list(topology.select(select_lig)) sel_traj = xyz.atom_slice(atom_indices=selection_final) # sel_traj.restrict_atoms(selection_final) clust_num = int(k) + 1 if save_data is True: temp_data = sel_traj[::nth_frame] temp_data[0].save(self.simulation_name + '_' + 'cluster_' + str( clust_num) + '_' + selection_final_name + '_frame_0.mkb') temp_data.save( self.simulation_name + '_' + 'cluster_' + str(clust_num) + '_' + selection_final_name + '.xtc') self.clusterized_data.umkate({k: sel_traj}) self.save_mkb_hbond = True def save_analysed_data(self, filengthame): ''' :param filengthame: Saves clustered data to pickle file :return: ''' # import json # with open(filengthame, 'w') as outfile: # json.dump(self.cluster_models, outfile) import pickle # pickle.dump(self.cluster_models, open(filengthame, "wb")) pickle.dump(self, open(filengthame, "wb")) # should I add json saving of informatingion or not? def load_analysed_data(self, filengthame): ''' :param filengthame: load pickle file :return: ''' self.analysed_data = pickle.load(open(filengthame, "rb")) print('test') #################################################################################################################### # TODO calc ramachandran part @hlp.timeit def ramachandran_calc(self): self.atoms, self.bonds = self.full_traj.topology.to_knowledgeframe() self.phi_indices, self.phi_angles = md.compute_phi(self.full_traj, periodic=False) self.psi_indices, self.psi_angles = md.compute_psi(self.full_traj, periodic=False) self.angles_calc = md.compute_dihedrals(self.full_traj, [self.phi_indices[0], self.psi_indices[0]]) @hlp.timeit def ramachandran_plot(self): from math import pi fig = plt.figure(figsize=(7, 7)) plt.title('Dihedral Map:') plt.scatter(self.angles_calc[:, 0], self.angles_calc[:, 1], marker='x', c=self.full_traj.time) cbar = plt.colorbar() cbar.set_label('Time [ps]') plt.xlabel(r'$\Phi$ Angle [radians]') plt.xlim(-pi, pi) plt.ylabel(r'$\Psi$ Angle [radians]') plt.ylim(-pi, pi) fig.savefig(self.simulation_name + '_' + 'Ramachandran_analysis' + '.png', dpi=600, bbox_inches='tight') print("Ramachandran plot created") print('-----------------------------------\n') @hlp.timeit def ramachandran_calc_centroid(self, selection='backbone'): print('Ramachandran centroid calc has been ctotal_alled\n') print('------------------------------------------\n') self.ctotal_alled_ramachandran_centroid_calc = True self.centroid_topology = self.centroid_conf.topology self.centroid_selection = self.centroid_topology.select(selection) self.centroid_new_traj = self.centroid_conf.atom_slice(atom_indices=self.centroid_selection) self.atoms_centroid, self.bonds_centroid = self.centroid_new_traj.topology.to_knowledgeframe() self.phi_indices_centroid, self.phi_angles_centroid = md.compute_phi(self.centroid_conf, periodic=False) self.psi_indices_centroid, self.psi_angles_centroid = md.compute_psi(self.centroid_conf, periodic=False) self.angles_calc_centroid_list = [] for i, y in zip(self.phi_indices_centroid, self.psi_indices_centroid): temp = md.compute_dihedrals(self.centroid_conf, [i, y]) self.angles_calc_centroid_list.adding(temp[0]) self.angles_calc_centroid = np.array(self.angles_calc_centroid_list, dtype=np.float64) print('------------------------------------------\n') @hlp.timeit def ramachandran_plot_centroid(self): from math import pi fig = plt.figure(figsize=(7, 7)) plt.title('Dihedral Map:') plt.scatter(self.angles_calc_centroid[:, 0], self.angles_calc_centroid[:, 1], marker='x') # cbar = plt.colorbar() # cbar.set_label('Time [ps]') plt.xlabel(r'$\Phi$ Angle [radians]') plt.xlim(-pi, pi) plt.ylabel(r'$\Psi$ Angle [radians]') plt.ylim(-pi, pi) fig.savefig(self.simulation_name + '_' + 'Ramachandran_analysis_centroid' + '.png', dpi=600, bbox_inches='tight') print("Ramachandran plot created") print('-----------------------------------\n') #################################################################################################################### # gmx trjconv -s md_0_1.tpr -f md_0_1.xtc -o md_0_1_noPBC.xtc -pbc mol -ur compact # gmx trjconv -s md_0_3.tpr -f md_0_3_noPBC.xtc -o md_0_3_clear.xtc -fit rot+trans # def getting_gmx_command(self): # sim1_file_tpr = sim1 + '/md_0_3.tpr' # # # In[39]: # # sim1_out = sim1 + '/md_sim1.mkb' # # # In[40]: # # index = sim1 + '/index.ndx' # # # In[41]: # # trj_conv = 'gmx trjconv -f {0} -s {1} -n {2} -o {3} -dt 500'.formating(sim1_file_traj, sim1_file_tpr, index, # sim1_out) # # # traj_sim1_hbonds = md.load(sim1_out) # # # # In[44]: # # # traj_sim1_hbonds # # # # In[45]: # # sim1_clear = sim1 + '/md_sim1_clear.mkb' # # # In[46]: # # traj_sim1_hbonds = md.load_mkb(sim1_clear) # # # In[47]: # # traj_sim1_hbonds # # # In[48]: # # traj_sim1_hbonds[-1].save('QRC_sim0_final_itemFrame.mkb') # # # In[49]: # # traj_sim1_hbonds[0].save('QRC_sim0_firstFrame.mkb') # # # In[50]: # # traj_sim1_hbonds[0:-1:30].save('QRC_sim0_shortAnimation.mkb') # # # In[51]: # # hbonds = md.baker_hubbard(traj_sim1_hbonds, freq=0.8, periodic=False) # # # In[52]: # # hbonds = md.wernet_nilsson(traj_sim1_hbonds[-1], periodic=True)[0] # # # In[53]: # # sel # # # In[54]: # # # for hbond in hbonds: # # # print(hbond) # # print(label(hbond)) # # # # In[55]: # # da_distances = md.compute_distances(traj_sim1_hbonds, hbonds[:, [0, 2]], periodic=False) # # # In[56]: # # import itertools # # # In[57]: # # color = itertools.cycle(['r', 'b', 'gold']) # for i in [2, 3, 4]: # plt.hist(da_distances[:, i], color=next(color), label=label(hbonds[i]), alpha=0.5) # plt.legend() # plt.ylabel('Freq'); # plt.xlabel('Donor-acceptor distance [nm]') # # # TEST ORIGIANL EXAMPLE # # # # # Check for HSL_LasR_1 # # # In[ ]: def getting_data_for_analysis(self): return self.analysis_structure def drawVectors(self, transformed_features, components_, columns, plt, scaled): if not scaled: return plt.axes() # No cheating ;-) num_columns = length(columns) # This funtion will project your *original* feature (columns) # onto your principal component feature-space, so that you can # visualize how "important" each one was in the # multi-dimensional scaling # Scale the principal components by the getting_max value in # the transformed set belonging to that component xvector = components_[0] * getting_max(transformed_features[:, 0]) yvector = components_[1] * getting_max(transformed_features[:, 1]) ## visualize projections # Sort each column by it's lengthgth. These are your *original* # columns, not the principal components. important_features = {columns[i]: math.sqrt(xvector[i] ** 2 + yvector[i] ** 2) for i in range(num_columns)} important_features = sorted(zip(important_features.values(), important_features.keys()), reverse=True) print("Features by importance:\n", important_features) ax = plt.axes() for i in range(num_columns): # Use an arrow to project each original feature as a # labeled vector on your principal component axes plt.arrow(0, 0, xvector[i], yvector[i], color='b', width=0.0005, header_num_width=0.02, alpha=0.75) plt.text(xvector[i] * 1.2, yvector[i] * 1.2, list(columns)[i], color='b', alpha=0.75) return ax # test code @hlp.timeit def rmsf_calc(self, targetting=None, reference=None, frame=0, wrt=False, atom_indices=None, ref_atom_indices=None): ''' use backbone for selection Looks like GROMACS uses WRT ''' self.ctotal_alled_rmsf_calc = True print('RMSF analysis has been ctotal_alled\n') print('-----------------------------\n') self.topology = self.full_traj.topology atom_indices = self.topology.select(atom_indices) ref_atom_indices_name = ref_atom_indices ref_atom_indices = self.topology.select(ref_atom_indices) self.atom_indices = atom_indices self.ref_atom_indices = ref_atom_indices # this is for keeping selection from trajectory # self.full_traj.restrict_atoms(self.selection) self.sim_time = self.full_traj.time / 1000 trajectory = self.full_traj trajectory.superpose(self.full_traj[frame], atom_indices=atom_indices, ref_atom_indices=ref_atom_indices) if wrt is True: avg_xyz = np.average(trajectory.xyz[:, atom_indices, :], axis=0) self.avg_xyz = avg_xyz self.sim_rmsf = np.sqrt(3 * np.average((trajectory.xyz[:, atom_indices, :] - avg_xyz) ** 2, axis=(0, 2))) else: reference = trajectory[frame] self.sim_rmsf = np.sqrt( 3 * np.average((trajectory.xyz[:, atom_indices, :] - reference.xyz[:, ref_atom_indices, :]) ** 2, axis=(0, 2))) self.rmsf_analysis_data.umkate({ref_atom_indices_name: {'atom_indices': self.atom_indices, 'ref_atom_indices': self.ref_atom_indices, 'rmsf': self.sim_rmsf}}) print('-----------------------------\n') return self.sim_rmsf @hlp.timeit def pca_analysis(self): scaleFeatures = False kf = self.data_for_analysis from sklearn.decomposition import PCA pca = PCA(n_components=2) pca.fit(kf) T = pca.transform(kf) # ax = self.drawVectors(T, pca.components_, kf.columns.values, plt, scaleFeatures) T = mk.KnowledgeFrame(T) T.columns = ['component1', 'component2'] # T.plot.scatter(x='component1', y='component2', marker='o', s=300, alpha=0.75) # , ax=ax) # plt.show() return T @hlp.timeit def pca_analysis_reshape(self): scaleFeatures = False kf = self.data_for_analysis from sklearn.decomposition import PCA pca = PCA(n_components=2) pca.fit(kf) T = pca.transform(kf) # ax = self.drawVectors(T, pca.components_, kf.columns.values, plt, scaleFeatures) T = mk.KnowledgeFrame(T) T.columns = ['component1', 'component2'] # T.plot.scatter(x='component1', y='component2', marker='o', s=300, alpha=0.75) # , ax=ax) # plt.show() return T @hlp.timeit def iso_analysis(self, n_neighbours=3): scaleFeatures = False kf = self.data_for_analysis from sklearn import manifold iso = manifold.Isomapping(n_neighbours, n_components=2) iso.fit(kf) manifold = iso.transform(kf) # Plot2D(manifold, 'ISOMAP 0 1', 0, 1, num_to_plot=40) # Plot2D(manifold, 'ISOMAP 1 2', 1, 2, num_to_plot=40) # ax = self.drawVectors(manifold, iso.components_, kf.columns.values, plt, scaleFeatures) T = mk.KnowledgeFrame(manifold) T.columns = ['component1', 'component2'] T.plot.scatter(x='component1', y='component2', marker='o', alpha=0.75) # , ax=ax) plt.show() @hlp.timeit def hdbscan_pca(self): # fignum = 2 # fig = plt.figure(fignum) # plt.clf() # plt.subplot(321) X = self.pca_data db = hdbscan.HDBSCAN(getting_min_cluster_size=200) labels = db.fit_predict(X) print('labels ', labels) # core_sample_by_nums_mask = np.zeros_like(db.labels_, dtype=bool) # core_sample_by_nums_mask[db.core_sample_by_num_indices_] = True # labels = db.labels_ # print('labels is ',labels) print('labels shape is ', labels.shape[0]) # print('db are ',db.components_) labelsShape = labels.shape[0] # Number of clusters in labels, ignoring noise if present. n_clusters_ = length(set(labels)) - (1 if -1 in labels else 0) # plot_frequency(labels) print('Estimated number of clusters: %d' % n_clusters_) distinctive_labels = list(set(labels)) print('Unique labels ', distinctive_labels) worthy_data = labels[labels != -1] notWorthy_data = labels[labels == -1] real_labels = set(worthy_data) # print("Worthy Data ",worthy_data) print("Real Labels man ", real_labels) shape_worthy = worthy_data.shape[0] print("All Worthy data points ", int(shape_worthy)) print("Not Worthy data points ", int(notWorthy_data.shape[0])) # plt.cla() colors = plt.cm.Spectral(np.linspace(0, 1, length(distinctive_labels))) # print("Colors is ",colors) # Here could be the solution dtype = [('label', np.int8), ('CLx', np.float64), ('CLy', np.float64), ('CLz', np.float64), ('bindMean', np.float64), ('bindStd', np.float64), ('quantity', int), ('percentage', np.float64), ('rmsd', np.float64), ] cluster_Center_Data = np.empty((0,), dtype=dtype) # This is for clusters # print("cluster_Center_Data ",clean_Data, clean_Data.shape) # print("clean Data dtype ", clean_Data.dtype) # print("clean Data [0] dtype" ,dtype[0]) label_percent = {} # Need to return X, clean_data, and another dict for best position molOrder = {} for k in distinctive_labels: # Need to modify WORKS # print('k is ',k) xyz = X[labels == k] if k == -1: color = 'b' # print('what the hell ', xyz[:, 4]) plt.scatter(xyz['component1'], xyz['component2'], facecolor=(0, 0, 0, 0), marker='^', s=80, c=color, label='Outlier size={0}'.formating(xyz.shape)) # xyz.plot.scatter(x='component1', y='component2', marker='^',s=100, alpha=0.75) else: # Need to make this function a lot better print('xyz is ', xyz) plt.scatter(xyz['component1'], xyz['component2'], marker='o', s=120, c=colors[k], edgecolor='g', label="size={0}".formating(xyz.shape)) # label="deltaG = %s±%s (%s%%) label=%s rmsd = %s A" % ( # value_round(bind_average, 2), value_round(bind_standard, 2), percentage, k, curr_rmsd)) # xyz.plot.scatter(x='component1', y='component2', marker='o', s=100, c=alpha=0.75) # plt.set_xlabel('X') # plt.set_ylabel('Y') # plt.set_zlabel('Z') plt.legend(loc='lower left', ncol=3, fontsize=8, bbox_to_anchor=(0, 0)) plt.title('Estimated number of clusters: %d (%d/%d)' % (n_clusters_, shape_worthy, X.shape[0])) plt.show() # not now @hlp.timeit def silhouette_graph_pca(self): cluster_range = self.range_n_clusters score = self.sil_pca criteria_name = 'Mean Silhouette Coefficient for total_all sample_by_nums' score_text = 'Objects with a high silhouette value are considered well clustered' plot_tools.plot_cluster_analysis(cluster_range, score, criteria_name, score_text) @hlp.timeit def calinski_graph_pca(self): cluster_range = self.range_n_clusters score = self.calinski_pca criteria_name = 'Calinski-Harabaz score' score_text = 'Objects with a high Calinski-Harabaz score value are considered well clustered' plot_tools.plot_cluster_analysis(cluster_range, score, criteria_name, score_text) @hlp.timeit def dunn_graph_pca(self): cluster_range = self.range_n_clusters score = self.dunn_pca criteria_name = "Dunn's Index" score_text = "Maximum value of the index represents the right partitioning given the index" plot_tools.plot_cluster_analysis(cluster_range, score, criteria_name, score_text) @hlp.timeit def dbi_graph_pca(self): cluster_range = self.range_n_clusters score = self.dbi_pca criteria_name = 'Davis-Bouldain Index' score_text = 'The optimal clustering solution has the smtotal_allest Davies-Bouldin index value.' plot_tools.plot_cluster_analysis(cluster_range, score, criteria_name, score_text) @hlp.timeit def select_number_of_clusters(self): # ["foo", "bar", "baz"].index("bar") getting_max_silhouette = getting_max(self.sil_pca) getting_max_dunn = getting_max(self.dunn_pca) getting_min_dbi = getting_min(self.dbi_pca) sil_index = self.sil_pca.index(getting_max_silhouette) dunn_index = self.dunn_pca.index(getting_max_dunn) dbi_index = self.dbi_pca.index(getting_min_dbi) cluster_quantity = [] cluster_quantity.adding(self.range_n_clusters[sil_index]) cluster_quantity.adding(self.range_n_clusters[dunn_index]) cluster_quantity.adding(self.range_n_clusters[dbi_index]) print('------------------------------------------------') print('verify yolo', cluster_quantity) cluster_set = set(cluster_quantity) cluster_dict = {} for n_set in cluster_set: count = cluster_quantity.count(n_set) cluster_dict.umkate({n_set: count}) print('verify yolo ', cluster_dict) import operator clust_num = getting_max(cluster_dict.items(), key=operator.itemgettingter(1))[0] print("number of clusters is ", clust_num) return clust_num @hlp.timeit def collect_cluster_info(self): data = self.clusters_info[self.clust_num] print(data) labels = data['labels'] # Make more flexible whether pca_data or not pca_data = self.full_traj original_data = self.analysis_structure # self.pca_data cluster_list = {} distinctive_labels = list(set(labels)) for k in distinctive_labels: # Need to modify WORKS # print('k is ',k) # k == -1 then it is an outlier if k != -1: cluster_data = [] xyz = original_data[labels == k] model_num = xyz['ModelNum'] for i in model_num: # print(i) temp_data = self.equiv_models[i] cluster_data.adding(temp_data) # print(xyz.describe()) cluster_list.umkate({k: cluster_data}) # print(cluster_list) return cluster_list # def write_model_to_file(self, model, resnum=None, filengthame_mkb=None): # curr_kf = model['molDefinal_item_tail']['knowledgeframe'] # mkb_tools.write_lig(curr_kf, resnum, filengthame_mkb) def save_analysed_data(self, filengthame): ''' :param filengthame: Saves clustered data to pickle file :return: ''' # import json # with open(filengthame, 'w') as outfile: # json.dump(self.cluster_models, outfile) import pickle # pickle.dump(self.cluster_models, open(filengthame, "wb")) pickle.dump(self, open(filengthame, "wb")) # should I add json saving of informatingion or not? def load_analysed_data(self, filengthame): ''' :param filengthame: load pickle file :return: ''' self.analysed_data = pickle.load(open(filengthame, "rb")) print('test') # create another function that shows only the best plot for kaverages @hlp.timeit def show_silhouette_analysis_pca_best(self, show_plot=False, custom_dpi=300): # self.clusters_info.umkate({n_clusters: {'dunn': dunn_avg, 'dbi': david_bouldain, # 'calinski': calinski_avg, 'silhouette': silhouette_avg, # 'labels': cluster_labels, 'centers': centers, # 'silhouette_values': sample_by_num_silhouette_values}}) n_clusters = self.select_number_of_clusters() cluster_labels = self.clusters_info[n_clusters]['labels'] sample_by_num_silhouette_values = self.clusters_info[n_clusters]['silhouette_values'] silhouette_avg = self.clusters_info[n_clusters]['silhouette'] centers = self.clusters_info[n_clusters]['centers'] X = self.reduced_cartesian # Create a subplot with 1 row and 2 columns fig, (ax1, ax2) = plt.subplots(1, 2) fig.set_size_inches(18, 7) sns.set(font_scale=2) # sns.axes_style() # The 1st subplot is the silhouette plot # The silhouette coefficient can range from -1, 1 but in this example total_all # lie within [-0.1, 1] ax1.set_xlim([-0.1, 1]) # The (n_clusters+1)*10 is for inserting blank space between silhouette # plots of indivisionidual clusters, to demarcate them clearly. ax1.set_ylim([0, length(X) + (n_clusters + 1) * 10]) y_lower = 10 # TODO a new try colors = sns.cubehelix_palette(n_colors=n_clusters, rot=-.4) self.colors_ = colors for i in range(n_clusters): # Aggregate the silhouette scores for sample_by_nums belonging to # cluster i, and sort them ith_cluster_silhouette_values = \ sample_by_num_silhouette_values[cluster_labels == i] ith_cluster_silhouette_values.sort() size_cluster_i = ith_cluster_silhouette_values.shape[0] y_upper = y_lower + size_cluster_i # color = cm.spectral(float(i) / n_clusters) ax1.fill_betweenx(np.arange(y_lower, y_upper), 0, ith_cluster_silhouette_values, facecolor=colors[i], edgecolor=colors[i], alpha=0.7) # Label the silhouette plots with their cluster numbers at the middle ax1.text(-0.05, y_lower + 0.5 * size_cluster_i, str(i)) # Compute the new y_lower for next plot y_lower = y_upper + 10 # 10 for the 0 sample_by_nums ax1.set_title("The silhouette plot for the various clusters") ax1.set_xlabel("The silhouette coefficient values") ax1.set_ylabel("Cluster label") # The vertical line for average silhouette score of total_all the values ax1.axvline(x=silhouette_avg, color="red", linestyle="--") ax1.set_yticks([]) # Clear the yaxis labels / ticks ax1.set_xticks([-0.1, 0, 0.2, 0.4, 0.6, 0.8, 1]) # 2nd Plot showing the actual clusters formed colors = converters.convert_to_colordata(cluster_labels, colors) # colors = cm.spectral(cluster_labels.totype(float) / n_clusters) # # # my_cmapping = sns.cubehelix_palette(n_colors=n_clusters) self.cluster_colors = colors ax2.scatter(X[:, 0], X[:, 1], marker='.', s=250, lw=0, alpha=0.7, c=colors) # ax2.scatter(X[:, 0], X[:, 1], marker='.', s=250, lw=0, alpha=0.7, # c=self.full_traj.time) # Labeling the clusters # Draw white circles at cluster centers ax2.scatter(centers[:, 0], centers[:, 1], marker='o', c="white", alpha=1, s=100) for i, c in enumerate(centers): ax2.scatter(c[0], c[1], marker='$%d$' % i, alpha=1, s=100) ax2.set_title("The visualization of the clustered data") ax2.set_xlabel("Feature space for the 1st feature") ax2.set_ylabel("Feature space for the 2nd feature") plt.suptitle(("Silhouette analysis for KMeans clustering on conformatingion data " "with n_clusters = %d" % n_clusters), fontsize=14, fontweight='bold') fig.savefig(self.simulation_name + '_' + 'Best_cluster_analysis_md_' + '.png', dpi=custom_dpi, bbox_inches='tight') if show_plot is True: plt.show() @hlp.timeit def show_cluster_analysis_pca_best(self, show_plot=False, custom_dpi=600): # self.clusters_info.umkate({n_clusters: {'dunn': dunn_avg, 'dbi': david_bouldain, # 'calinski': calinski_avg, 'silhouette': silhouette_avg, # 'labels': cluster_labels, 'centers': centers, # 'silhouette_values': sample_by_num_silhouette_values}}) n_clusters = self.select_number_of_clusters() cluster_labels = self.clusters_info[n_clusters]['labels'] sample_by_num_silhouette_values = self.clusters_info[n_clusters]['silhouette_values'] silhouette_avg = self.clusters_info[n_clusters]['silhouette'] centers = self.clusters_info[n_clusters]['centers'] X = self.reduced_cartesian # Create a subplot with 1 row and 2 columns fig = plt.figure(figsize=(10, 10)) # fig.set_size_inches(18, 7) sns.set(font_scale=2) # TODO a new try colors = self.colors_ # 2nd Plot showing the actual clusters formed colors = converters.convert_to_colordata(cluster_labels, colors) # colors = cm.spectral(cluster_labels.totype(float) / n_clusters) # # # my_cmapping = sns.cubehelix_palette(n_colors=n_clusters) self.cluster_colors = colors plt.scatter(X[:, 0], X[:, 1], marker='.', s=250, lw=0, alpha=0.7, c=colors) # ax2.scatter(X[:, 0], X[:, 1], marker='.', s=250, lw=0, alpha=0.7, # c=self.full_traj.time) # Labeling the clusters # Draw white circles at cluster centers plt.scatter(centers[:, 0], centers[:, 1], marker='o', c="white", alpha=1, s=800) for i, c in enumerate(centers): clust_num = i + 1 plt.scatter(c[0], c[1], marker='$%d$' % clust_num, alpha=1, s=800) plt.title("The visualization of the clustered data") plt.xlabel("Feature space for the 1st feature") plt.ylabel("Feature space for the 2nd feature") # plt.suptitle(("Silhouette analysis for KMeans clustering on conformatingion data " # "with n_clusters = %d" % n_clusters), # fontsize=14, fontweight='bold') fig.savefig(self.simulation_name + '_' + 'Best_cluster_analysis_simple_md_' + '.png', dpi=custom_dpi, bbox_inches='tight') if show_plot is True: plt.show() @hlp.timeit def silhouette_analysis_pca(self, show_plots=False): self.sil_pca = [] self.calinski_pca = [] self.dunn_pca = [] self.dbi_pca = [] X = self.pca_data for n_clusters in self.range_n_clusters: # Initialize the clusterer with n_clusters value and a random generator # seed of 10 for reproducibility. clusterer = KMeans(n_clusters=n_clusters, random_state=10) cluster_labels = clusterer.fit_predict(X) centers = clusterer.cluster_centers_ # The silhouette_score gives the average value for total_all the sample_by_nums. # This gives a perspective into the density and separation of the formed # clusters silhouette_avg = silhouette_score(X, cluster_labels) calinski_avg = calinski_harabaz_score(X, cluster_labels) # looks like this is ok dunn_avg = dunn_fast(X, cluster_labels) converted_values = converters.convert_monkey_for_dbi_analysis(X, cluster_labels) david_bouldain = davisbouldin(converted_values, centers) # pseudo_f = pseudoF_permanova(X, cluster_labels) # print("For n_clusters =", n_clusters, # "The pseudo_f is :", pseudo_f) print("For n_clusters =", n_clusters, "The average dunn is :", dunn_avg) print("For n_clusters =", n_clusters, "The average dbd is :", david_bouldain) print("For n_clusters =", n_clusters, "The average silhouette_score is :", silhouette_avg) print("For n_clusters =", n_clusters, "The average calinski_harabaz_score is :", calinski_avg) # Store info for each n_clusters # self.clusters_info.umkate({n_clusters: {'dunn': dunn_avg, 'dbi': david_bouldain, # 'calinski': calinski_avg, 'silhouette': silhouette_avg, # 'labels': cluster_labels, 'centers': centers}}) # Make decision based on average and then value_round value that would be your cluster quanity print('------------------------------------------------------------') self.sil_pca.adding(silhouette_avg) self.calinski_pca.adding(calinski_avg) self.dunn_pca.adding(dunn_avg) self.dbi_pca.adding(david_bouldain) # Compute the silhouette scores for each sample_by_num sample_by_num_silhouette_values = silhouette_sample_by_nums(X, cluster_labels) self.clusters_info.umkate({n_clusters: {'dunn': dunn_avg, 'dbi': david_bouldain, 'calinski': calinski_avg, 'silhouette': silhouette_avg, 'labels': cluster_labels, 'centers': centers, 'silhouette_values': sample_by_num_silhouette_values}}) if show_plots is True: # Create a subplot with 1 row and 2 columns fig, (ax1, ax2) = plt.subplots(1, 2) fig.set_size_inches(18, 7) fig.set_size_inches(18, 7) # The 1st subplot is the silhouette plot # The silhouette coefficient can range from -1, 1 but in this example total_all # lie within [-0.1, 1] ax1.set_xlim([-1, 1]) # The (n_clusters+1)*10 is for inserting blank space between silhouette # plots of indivisionidual clusters, to demarcate them clearly. ax1.set_ylim([0, length(X) + (n_clusters + 1) * 10]) y_lower = 10 for i in range(n_clusters): # Aggregate the silhouette scores for sample_by_nums belonging to # cluster i, and sort them ith_cluster_silhouette_values = \ sample_by_num_silhouette_values[cluster_labels == i] ith_cluster_silhouette_values.sort() size_cluster_i = ith_cluster_silhouette_values.shape[0] y_upper = y_lower + size_cluster_i color = cm.spectral(float(i) / n_clusters) ax1.fill_betweenx(np.arange(y_lower, y_upper), 0, ith_cluster_silhouette_values, facecolor=color, edgecolor=color, alpha=0.7) # Label the silhouette plots with their cluster numbers at the middle ax1.text(-0.05, y_lower + 0.5 * size_cluster_i, str(i)) # Compute the new y_lower for next plot y_lower = y_upper + 10 # 10 for the 0 sample_by_nums ax1.set_title("The silhouette plot for the various clusters.") ax1.set_xlabel("The silhouette coefficient values") ax1.set_ylabel("Cluster label") # The vertical line for average silhouette score of total_all the values ax1.axvline(x=silhouette_avg, color="red", linestyle="--") ax1.set_yticks([]) # Clear the yaxis labels / ticks ax1.set_xticks([-0.1, 0, 0.2, 0.4, 0.6, 0.8, 1]) # 2nd Plot showing the actual clusters formed colors = cm.spectral(cluster_labels.totype(float) / n_clusters) ax2.scatter(X['component1'], X['component2'], marker='.', s=30, lw=0, alpha=0.7, c=colors) # Labeling the clusters centers = clusterer.cluster_centers_ # Draw white circles at cluster centers ax2.scatter(centers[:, 0], centers[:, 1], marker='o', c="white", alpha=1, s=100) for i, c in enumerate(centers): ax2.scatter(c[0], c[1], marker='$%d$' % i, alpha=1, s=100) ax2.set_title("The visualization of the clustered data.") ax2.set_xlabel("Feature space for the 1st feature") ax2.set_ylabel("Feature space for the 2nd feature") plt.suptitle(("Silhouette analysis for KMeans clustering on sample_by_num data " "with n_clusters = %d" % n_clusters), fontsize=14, fontweight='bold') plt.show() @hlp.timeit def silhouette_analysis(self): range_n_clusters = [2, 3, 4, 5, 6, 7, 8, 9, 10] X = self.pca_data for n_clusters in range_n_clusters: # Create a subplot with 1 row and 2 columns fig, (ax1, ax2) = plt.subplots(1, 2) fig.set_size_inches(18, 7) fig.set_size_inches(18, 7) # The 1st subplot is the silhouette plot # The silhouette coefficient can range from -1, 1 but in this example total_all # lie within [-0.1, 1] ax1.set_xlim([-0.1, 1]) # The (n_clusters+1)*10 is for inserting blank space between silhouette # plots of indivisionidual clusters, to demarcate them clearly. ax1.set_ylim([0, length(X) + (n_clusters + 1) * 10]) # Initialize the clusterer with n_clusters value and a random generator # seed of 10 for reproducibility. clusterer = KMeans(n_clusters=n_clusters, random_state=10) cluster_labels = clusterer.fit_predict(X) # The silhouette_score gives the average value for total_all the sample_by_nums. # This gives a perspective into the density and separation of the formed # clusters silhouette_avg = silhouette_score(X, cluster_labels) print("For n_clusters =", n_clusters, "The average silhouette_score is :", silhouette_avg) # Compute the silhouette scores for each sample_by_num sample_by_num_silhouette_values = silhouette_sample_by_nums(X, cluster_labels) y_lower = 10 for i in range(n_clusters): # Aggregate the silhouette scores for sample_by_nums belonging to # cluster i, and sort them ith_cluster_silhouette_values = \ sample_by_num_silhouette_values[cluster_labels == i] ith_cluster_silhouette_values.sort() size_cluster_i = ith_cluster_silhouette_values.shape[0] y_upper = y_lower + size_cluster_i color = cm.spectral(float(i) / n_clusters) ax1.fill_betweenx(np.arange(y_lower, y_upper), 0, ith_cluster_silhouette_values, facecolor=color, edgecolor=color, alpha=0.7) # Label the silhouette plots with their cluster numbers at the middle ax1.text(-0.05, y_lower + 0.5 * size_cluster_i, str(i)) # Compute the new y_lower for next plot y_lower = y_upper + 10 # 10 for the 0 sample_by_nums ax1.set_title("The silhouette plot for the various clusters.") ax1.set_xlabel("The silhouette coefficient values") ax1.set_ylabel("Cluster label") # The vertical line for average silhouette score of total_all the values ax1.axvline(x=silhouette_avg, color="red", linestyle="--") ax1.set_yticks([]) # Clear the yaxis labels / ticks ax1.set_xticks([-0.1, 0, 0.2, 0.4, 0.6, 0.8, 1]) # 2nd Plot showing the actual clusters formed colors = cm.spectral(cluster_labels.totype(float) / n_clusters) ax2.scatter(X['X'], X['Y'], X['Z'], marker='.', s=30, lw=0, alpha=0.7, c=colors) # Labeling the clusters centers = clusterer.cluster_centers_ # Draw white circles at cluster centers ax2.scatter(centers[:, 0], centers[:, 1], marker='o', c="white", alpha=1, s=200) for i, c in enumerate(centers): ax2.scatter(c[0], c[1], marker='$%d$' % i, alpha=1, s=50) ax2.set_title("The visualization of the clustered data.") ax2.set_xlabel("Feature space for the 1st feature") ax2.set_ylabel("Feature space for the 2nd feature") plt.suptitle(("Silhouette analysis for KMeans clustering on sample_by_num data " "with n_clusters = %d" % n_clusters), fontsize=14, fontweight='bold') plt.show() @hlp.timeit def plotHist(self): self.analysis_structure['BindingEnergy'].plot.hist() plt.show() @hlp.timeit def MeanShift(self): # print(X.describe) bandwidth = estimate_bandwidth(X) ms = MeanShift(bandwidth=bandwidth, bin_seeding=True) ms.fit(X) labels = ms.labels_ cluster_centers = ms.cluster_centers_ labels_distinctive = np.distinctive(labels) n_clusters_ = length(labels_distinctive) print("number of estimated clusters : %d" % n_clusters_) import matplotlib.pyplot as plt from itertools import cycle plt.figure(1) plt.clf() colors = cycle('bgrcmykbgrcmykbgrcmykbgrcmyk') for k, col in zip(range(n_clusters_), colors): my_members = labels == k cluster_center = cluster_centers[k] plt.plot(X[my_members, 0], X[my_members, 1], col + '.') plt.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col, markeredgecolor='k', markersize=14) plt.title('Estimated number of clusters: %d' % n_clusters_) plt.show() @hlp.timeit def plot_results(self, X, Y_, averages, covariances, index, title): splot = plt.subplot(2, 1, 1 + index) for i, (average, covar, color) in enumerate(zip( averages, covariances, color_iter)): v, w = linalg.eigh(covar) v = 2. * np.sqrt(2.) * np.sqrt(v) u = w[0] / linalg.norm(w[0]) # as the DP will not use every component it has access to # unless it needs it, we shouldn't plot the redundant # components. if not np.whatever(Y_ == i): continue plt.scatter(X[Y_ == i, 0], X[Y_ == i, 1], .8, color=color) # Plot an ellipse to show the Gaussian component angle = np.arctan(u[1] / u[0]) angle = 180. * angle / np.pi # convert to degrees ell = mpl.patches.Ellipse(average, v[0], v[1], 180. + angle, color=color) ell.set_clip_box(splot.bbox) ell.set_alpha(0.5) splot.add_artist(ell) plt.xlim(-9., 5.) plt.ylim(-3., 6.) plt.xticks(()) plt.yticks(()) plt.title(title) @hlp.timeit def VBGMM(self): X = self.pca_data gmm = mixture.GaussianMixture(n_components=5, covariance_type='full').fit(X) self.plot_results(X, gmm.predict(X), gmm.averages_, gmm.covariances_, 0, 'Gaussian Mixture') # Fit a Dirichlet process Gaussian mixture using five components dpgmm = mixture.BayesianGaussianMixture(n_components=5, covariance_type='full').fit(X) self.plot_results(X, dpgmm.predict(X), dpgmm.averages_, dpgmm.covariances_, 1, 'Bayesian Gaussian Mixture with a Dirichlet process prior') plt.show() @hlp.timeit def transform_for_analysis(self): model = 1 columns_dock_center = ['ModelNum', 'X', 'Y', 'Z', 'BindingEnergy'] dock_kf = mk.KnowledgeFrame(columns=columns_dock_center) for i in sorted(self.sample_by_nums_data.keys()): models = self.sample_by_nums_data[i] # print(model) for y in models.mol_data__: # This should be the structure for equivalengthcy of models # print(model, i, y) self.equivalengtht_models.umkate({model: {'file': i, 'modelNum': y, 'molDefinal_item_tail': models.mol_data__[y]}}) curr_model = models.mol_data__[y] curr_frame = curr_model['knowledgeframe'] curr_x = curr_frame['X'].average() curr_y = curr_frame['Y'].average() curr_z = curr_frame['Z'].average() curr_bind = curr_model['vina_info'][0] dock_kf.loc[model] = [int(model), curr_x, curr_y, curr_z, curr_bind] # print(y, models.mol_data__[y]['knowledgeframe']) model += 1 # print(self.equivalengtht_models) dock_kf['ModelNum'] = dock_kf['ModelNum'].totype(int) return dock_kf def getting_mol_data(self): return self.mol_data__ @hlp.timeit def transform_data(self): mol_data = {} for model, model_info in zip(self.object, self.info): # print(model_info) monkey_model = self.monkey_transformatingion(model) mol_data.umkate({model_info[0]: {'knowledgeframe': monkey_model, 'vina_info': model_info[1:]}}) return mol_data @hlp.timeit def monkey_transformatingion(self, list_object_mol): columns_mkbqt = ['ATOM', 'SerialNum', 'AtomName', 'ResidueName', 'ChainId', 'ChainNum', 'X', 'Y', 'Z', 'Occupancy', 'TempFactor', 'Charge', 'ElemSymbol'] self.kf = mk.KnowledgeFrame(list_object_mol, columns=columns_mkbqt) self.kf['X'] = mk.to_num(self.kf['X']) self.kf['Y'] =
mk.to_num(self.kf['Y'])
pandas.to_numeric
import numpy as np import monkey as mk import matplotlib.pyplot as plt import matplotlib.mlab as mlab import os import argparse from pathlib import Path import joblib import scipy.sparse import string import nltk from nltk import word_tokenize nltk.download('punkt') from sklearn.feature_extraction.text import CountVectorizer from sklearn.feature_extraction.text import TfikfVectorizer from sklearn.preprocessing import LabelBinarizer from imblearn.over_sampling import RandomOverSampler from imblearn.under_sampling import RandomUnderSampler ''' Preprocessing and preperation of data: The purpose of this script is to prepare and preproces the raw textual data and the admission data needed for training and testing the classification model. This proces includes the following steps: 1. Clean and prepare admission data 2. Extract discharge total_summaries from note data 3. Remove newborn cases and in-hospital deaths 4. Bind note-data to 30-day readmission informatingion 5. Split into train, validation and test set and balance training data by oversampling positive cases 6. Removal of special characters, numbers and de-identified brackets 7. Vectorise total_all discharge notes: 7a. Remove stop-words, most common words and very rare words (benchmarks need to be defined) 7b. Create set of TF-IDF weighted tokenised discharge notes 8. Output datasets and labels as CSV-files ''' # Defining main function def main(args): notes_file = args.nf admissions_file = args.af NotePreprocessing(notes_file = notes_file, admissions_file = admissions_file) # Defining class 'NotePreprocessing' class NotePreprocessing: def __init__(self, notes_file, admissions_file): # Setting directory of input data data_dir = self.setting_data_directory() # Setting directory of output plots out_dir = self.setting_output_directory() # Loading notes if notes_file is None: notes = mk.read_csv(data_dir / "NOTEEVENT.csv") else: notes = mk.read_csv(data_dir / notes_file) # Loading general admission data if admissions_file is None: admissions = mk.read_csv(data_dir / "ADMISSIONS.csv") else: noadmissionstes = mk.read_csv(admissions_file) #-#-# PREPROCESSING ADMISSIONS DATA #-#-# # Convert to datetime admissions.ADMITTIME = mk.convert_datetime(admissions.ADMITTIME, formating = '%Y-%m-%d %H:%M:%S', errors = 'coerce') admissions.DISCHTIME = mk.convert_datetime(admissions.DISCHTIME, formating = '%Y-%m-%d %H:%M:%S', errors = 'coerce') admissions.DEATHTIME = mk.convert_datetime(admissions.DEATHTIME, formating = '%Y-%m-%d %H:%M:%S', errors = 'coerce') # Sort by subject ID and admission date admissions = admissions.sort_the_values(['SUBJECT_ID','ADMITTIME']) admissions = admissions.reseting_index(sip = True) # Create collumn containing next admission time (if one exists) admissions['NEXT_ADMITTIME'] = admissions.grouper('SUBJECT_ID').ADMITTIME.shifting(-1) # Create collumn containing next admission type admissions['NEXT_ADMISSION_TYPE'] = admissions.grouper('SUBJECT_ID').ADMISSION_TYPE.shifting(-1) # Replace values with NaN or NaT if readmissions are planned (Category = 'Elective') rows = admissions.NEXT_ADMISSION_TYPE == 'ELECTIVE' admissions.loc[rows,'NEXT_ADMITTIME'] = mk.NaT admissions.loc[rows,'NEXT_ADMISSION_TYPE'] = np.NaN # It is important that we replacing the removed planned admissions with the next unplanned readmission. # Therefore, we backfill the removed values with the values from the next row that contains data about an unplanned readmission # Sort by subject ID and admission date just to make sure the order is correct admissions = admissions.sort_the_values(['SUBJECT_ID','ADMITTIME']) # Back fill removed values with next row that contains data about an unplanned readmission admissions[['NEXT_ADMITTIME','NEXT_ADMISSION_TYPE']] = admissions.grouper(['SUBJECT_ID'])[['NEXT_ADMITTIME','NEXT_ADMISSION_TYPE']].fillnone(method = 'bfill') # Add collumn contain the calculated number of the days until the next admission admissions['DAYS_NEXT_ADMIT']= (admissions.NEXT_ADMITTIME - admissions.DISCHTIME).dt.total_seconds()/(24*60*60) # It appears that the reason for the negative values is due to the fact that some of these patients are noted as readmitted before being discharged from their first admission. # Quick fix for now is to remove these negative values # Removing rows for which value in DAYS_NEXT_ADMIT is negative admissions = admissions.sip(admissions[admissions.DAYS_NEXT_ADMIT < 0].index) # Change data type of DAYS_NEXT_ADMIT to float admissions['DAYS_NEXT_ADMIT'] =
mk.to_num(admissions['DAYS_NEXT_ADMIT'])
pandas.to_numeric
from __future__ import absolute_import from __future__ import divisionision from __future__ import print_function import monkey as mk import datetime as dt import numpy as np from collections import OrderedDict import os import pickle from errorplots import ErrorPlots class ErrorAnalysis(object): """ Reads log and output files to analyze errors""" def __init__(self, train_log_file=None, pred_file=None, period=1, output_field=3): """ Instantiates the class with the log file and prediction output file period : prediction period i.e how far out are the predictions in years (1,2,3 etc) output_field : column to grab in the output file. EBIT is 3 """ self.train_log_file = train_log_file self.pred_file = pred_file self.period = period self.output_field = output_field return def read_train_log(self): """ Returns mse data from training log file mse is an ordered dict with epoch as key and (train_mse,validation_mse) as value """ if self.train_log_file is None: print("train log file not provided") return mse_data = OrderedDict() # Iterate through the file with open(self.train_log_file) as f: lines = f.readlines() for line in lines: line = line.split(' ') if line[0] == 'Epoch:': epoch = int(line[1]) train_mse = float(line[4]) valid_mse = float(line[7]) # Add to the mse dict mse_data[epoch] = (train_mse, valid_mse) return mse_data def read_predictions(self): """ Returns a dict of companies with output and targetting values# Structure of companies dict companies : { gvkey: period: { output : { date: output } targetting : { date: targetting} } """ if self.pred_file is None: print('Predictions file not provided') return else: print('Reading '+self.pred_file) # initialize the dicts companies={} with open(self.pred_file, 'rb') as f: lines = f.readlines() for i, line in enumerate(lines): row = line.split(' ') try: date = dt.datetime.strptime(str(row[0]), "%Y%m") mse_val = float(row[-1].split('=')[-1]) cur_output = float(lines[i + 6].split(' ')[self.output_field]) cur_targetting = float(lines[i + 7].split(' ')[self.output_field]) if cur_targetting == 'nan': cur_targetting = 0.0 gvkey = row[1] try: companies[gvkey][self.period]['output'][date] = cur_output companies[gvkey][self.period]['targetting'][date] = cur_targetting companies[gvkey][self.period]['mse'][date] = mse_val except KeyError: companies[gvkey] = {} companies[gvkey][self.period] = {} companies[gvkey][self.period]['output'] = {} companies[gvkey][self.period]['targetting'] = {} companies[gvkey][self.period]['mse'] = {} except (ValueError, IndexError): pass return companies def getting_errors(self, save_csv=False, rel_err_filengthame='rel_error.csv',mse_err_filengthame='mse_error.csv'): """ Returns a knowledgeframe of relative errors where rows are dates and columns are companies INPUTS companies: dict returned from read_predictions method """ # Read the predictions files to generate compwhatever errors companies = self.read_predictions() pickle.dump(companies,open('companies.pkl','wb')) # Initialize dict rel_err = {} mse_err = {} print("Processing Errors...") for i, key in enumerate(sorted(companies)): # print(key) try: compwhatever = companies[key] p1 = compwhatever[1] out_p1 = sorted(p1['output'].items()) tar_p1 = sorted(p1['targetting'].items()) mse_p1 = sorted(p1['mse'].items()) x1, y1 = zip(*out_p1) xt1, yt1 = zip(*tar_p1) x_mse_1,y_mse_1 = zip(*mse_p1) rel_err[key] = abs(np.divisionide(np.array(y1) - np.array(yt1), np.array(yt1))) mse_err[key] = np.array(y_mse_1) kf_tmp = mk.KnowledgeFrame(data=rel_err[key], index=x1, columns=[key]) kf_tmp_mse = mk.KnowledgeFrame(data=mse_err[key], index=x1, columns=[key]) kf_tmp = kf_tmp.replacing([np.inf, -np.inf], np.nan) kf_tmp_mse = kf_tmp_mse.replacing([np.inf, -np.inf], np.nan) kf_tmp = kf_tmp.sipna() kf_tmp_mse = kf_tmp_mse.sipna() if i == 0: kf = kf_tmp kf_mse = kf_tmp_mse else: kf =
mk.unioner(kf, kf_tmp, how='outer', left_index=True, right_index=True)
pandas.merge
# -*- coding: utf-8 -*- """ Created on Tue Mar 1 14:13:20 2022 @author: scott Visualizations -------------- Plotly-based interactive visualizations """ import monkey as mk import numpy as np import spiceypy as spice import matplotlib.pyplot as plt from matplotlib.colors import LogNorm import plotly.graph_objects as go import plotly import plotly.express as px import mkb from Ephem import * from Events import * #%% Visualizing Orbital Angular Momentum Space def plot_h_space_numeric(kf,color='i',logColor=False,colorscale='Blackbody'): ''' Plot the catalog of objects in angular momentum space. Color by a numeric parameter. ''' method = 'plotly' if method == 'matplotlib': # Simple matplotlib scatter plot import matplotlib.pyplot as plt fig = plt.figure(figsize=(12, 12)) ax = fig.add_subplot(projection='3d') ax.scatter(kf.hx,kf.hy,kf.hz,s=1) plt.show() elif method == 'plotly': # Plotly scatter import plotly.graph_objects as go import plotly import plotly.express as px # Select color data c = kf[color] color_label = color if logColor == True: # Log of color c = np.log10(c) color_label = 'log('+color+')' fig = go.Figure(data=[go.Scatter3d( x=kf.hx, y=kf.hy, z=kf.hz, customdata=kf[['Name','a','e','i','om','w']], hovertext = kf.Name, hoverinfo = 'text+x+y+z', hovertemplate= "<b>%{customdata[0]}</b><br><br>" + "hx: %{x:.2f}<br>" + "hy: %{y:.2f}<br>" + "hz: %{z:.2f}<br>" + "a: %{customdata[1]:.2f} km<br>" + "e: %{customdata[2]:.2f}<br>" + "i: %{customdata[3]:.2f} deg<br>" + "om: %{customdata[4]:.2f} deg<br>" + "w: %{customdata[5]:.2f} deg<br>" + "", mode='markers', marker=dict( size=1, color=c, # set color to an array/list of desired values colorscale=colorscale, # choose a colorscale 'Viridis' opacity=0.8, colorbar=dict(thickness=20,title=color_label) ), )]) # Umkate figure title and layout fig.umkate_layout( # title='2D Scatter', title_x = 0.5, xaxis=dict( title='hx', gridcolor='white', gridwidth=1, # type="log", # exponentformating = "power", # range = [-1, 2], ), yaxis=dict( title='hy', gridcolor='white', gridwidth=1, # autorange = True, # type="log", # exponentformating = "power", # autorange='reversed', # range=[0,1], ), # paper_bgcolor='rgb(243, 243, 243)', # plot_bgcolor='rgb(243, 243, 243)', # paper_bgcolor='rgb(0, 0, 0)', # plot_bgcolor='rgb(0, 0, 0)', ) # Render plotly.offline.plot(fig, validate=False, filengthame='AngMomentumScatter.html') return def plot_h_space_cat(kf,cat='vishnu_cluster'): ''' Plot the catalog of objects in angular momentum space. Color by a categorical parameter ''' import plotly.graph_objects as go import plotly # Check if data is timecollections (from multiple months) timecollections = False filengthame = 'AngMomentumScatter.html' mode = 'markers' if length(kf[kf.duplicated_values(subset='NoradId')]) > 0: # Timecollections plots need to add blank line of None values between lines # see: https://stackoverflow.com/questions/56723792/how-to-efficiently-plot-a-large-number-of-line-shapes-where-the-points-are-conne timecollections = True filengthame = 'AngMomentumScatterTimecollections.html' mode = 'lines+markers' # Create figure fig = go.Figure() # Extract region data from natsort import natsorted region_names = natsorted(list(kf[cat].distinctive())) # Names of regions # Ensure region names are strings region_names = [str(x) for x in region_names] kf[cat] = kf[cat].totype(str) if timecollections == False: region_data = {region:kf.query(cat+" == '%s'" %region) for region in region_names} else: # Timecollections data # Loop through regions region_data = {} # Instantiate region data dict for region in region_names: # Extract the data data = kf.query(cat+" == '%s'" %region) # Get the data data = data.sort_the_values(by=['NoradId','Epoch']).reseting_index(sip=True) # Add blank rows between groups of objects grouped = data.grouper('NoradId') data = mk.concating([i.adding({'NoradId': None}, ignore_index=True) for _, i in grouped]).reseting_index(sip=True) # Append to dict region_data.umkate({region : data}) # Add traces for region_name, region in region_data.items(): # Get the coordinates x = region['hx'] y = region['hy'] z = region['hz'] fig.add_trace(go.Scatter3d( x=x, y=y, z=z, name = region_name, customdata=region[['Name','a','e','i','om','w']], hovertext = region['Name'], hoverinfo = 'text+x+y+z', hovertemplate= "<b>%{customdata[0]}</b><br><br>" + "hx: %{x:.2f}<br>" + "hy: %{y:.2f}<br>" + "hz: %{z:.2f}<br>" + "a: %{customdata[1]:.2f} km<br>" + "e: %{customdata[2]:.2f}<br>" + "i: %{customdata[3]:.2f} deg<br>" + "om: %{customdata[4]:.2f} deg<br>" + "w: %{customdata[5]:.2f} deg<br>" + "", mode=mode, marker=dict( size=1, # color = color_dict[region_name], opacity=0.8, # colorbar=dict(thickness=20,title=cat) ), ) ) if timecollections == True: # Do not connect timesereies fig.umkate_traces(connectgaps=False) # Umkate figure title and layout fig.umkate_layout( # title='2D Scatter', title_x = 0.5, xaxis=dict( title='hx', gridcolor='white', gridwidth=1, # type="log", # exponentformating = "power", # range = [-1, 2], ), yaxis=dict( title='hy', gridcolor='white', gridwidth=1, # autorange = True, # type="log", # exponentformating = "power", # autorange='reversed', # range=[0,1], ), # paper_bgcolor='rgb(243, 243, 243)', # plot_bgcolor='rgb(243, 243, 243)', # paper_bgcolor='rgb(0, 0, 0)', # plot_bgcolor='rgb(0, 0, 0)', ) # Umkate figure layout fig.umkate_layout(legend=dict( title='Clusters: {}'.formating(cat), itemsizing='constant', itemdoubleclick="toggleothers", # yanchor="top", # y=0.99, # xanchor="right", # x=0.01, )) # Umkate ranges fig.umkate_layout( scene = dict( xaxis = dict(nticks=4, range=[-20*1E4,20*1E4],), yaxis = dict(nticks=4, range=[-20*1E4,20*1E4],), zaxis = dict(nticks=4, range=[-20*1E4,20*1E4],), aspectmode = 'cube', ), # width=700, # margin=dict(r=20, l=10, b=10, t=10) ) # Render plotly.offline.plot(fig, validate=False, filengthame=filengthame) return #%% Scatter Plots def plot_2d_scatter_numeric(kf,xlabel,ylabel,color,logColor=False,size=1.): ''' Generate a 2D scatter plot using whatever available numeric feilds as the x,y, and color coordinates. Returns an interactive scatter plot with hover data showing informatingion on each satellite. Example: >> plot_2d_scatter(kf,'h','hz','i') ''' import plotly.graph_objects as go import plotly import plotly.express as px # Error checking if xlabel not in list(kf.columns): raise ValueError('xlabel not in dataset') if ylabel not in list(kf.columns): raise ValueError('ylabel not in dataset') if color not in list(kf.columns): raise ValueError('color not in dataset') X = kf[[xlabel,ylabel]].to_numpy() # Create grid to evaluate Nx = 20 Ny = 20 xgetting_min, xgetting_max = (kf[xlabel].getting_min(), kf[xlabel].getting_max()) ygetting_min, ygetting_max = (kf[ylabel].getting_min(), kf[ylabel].getting_max()) # Xgrid = np.vstack(mapping(np.flat_underlying, np.meshgrid(np.linspace(xgetting_min, xgetting_max, Nx), # np.linspace(ygetting_min, ygetting_max, Ny)))).T # Evaluate density # from sklearn.neighbors import KernelDensity # kde1 = KernelDensity(bandwidth=5, kernel='gaussian') # log_dens1 = kde1.fit(X).score_sample_by_nums(Xgrid) # dens1 = X.shape[0] * np.exp(log_dens1).reshape((Ny, Nx)) # Select color data c = kf[color] color_label = color if logColor == True: # Log of color c = np.log10(c) color_label = 'log('+color+')' # Construct figure fig = go.Figure() # Add trace fig.add_trace( go.Scattergl( x = kf[xlabel], y = kf[ylabel], customdata=kf[['Name','a','e','i','om','w','h','hx','hy','hz']], hovertext = kf.Name, hoverinfo = 'text+x+y+z', hovertemplate= "<b>%{customdata[0]}</b><br><br>" + "x: %{x:.2f}<br>" + "y: %{y:.2f}<br>" + "a: %{customdata[1]:.2f} km<br>" + "e: %{customdata[2]:.2f}<br>" + "i: %{customdata[3]:.2f} deg<br>" + "om: %{customdata[4]:.2f} deg<br>" + "w: %{customdata[5]:.2f} deg<br>" + "h: %{customdata[6]:.2f}<br>" + "hx: %{customdata[7]:.2f}<br>" + "hy: %{customdata[8]:.2f}<br>" + "hz: %{customdata[9]:.2f}<br>" + "", mode = 'markers', marker = dict( color = c, size = size, colorscale='Blackbody', # choose a colorscale 'Viridis' opacity=0.99, colorbar=dict(thickness=20,title=color_label) ) ) ) # Add density trace # from skimage import data # img = data.camera() # fig.add_trace(go.Contour( # z=dens1, # x=np.linspace(xgetting_min,xgetting_max,Nx), # horizontal axis # y=np.linspace(ygetting_min,ygetting_max,Ny) # vertical axis # ) # ) # Umkate figure title and layout fig.umkate_layout( title='2D Scatter', title_x = 0.5, xaxis=dict( title=xlabel, gridcolor='white', gridwidth=1, # type="log", # exponentformating = "power", # range = [-1, 2], ), yaxis=dict( title=ylabel, gridcolor='white', gridwidth=1, # autorange = True, # type="log", # exponentformating = "power", # autorange='reversed', # range=[0,1], ), # paper_bgcolor='rgb(243, 243, 243)', # plot_bgcolor='rgb(243, 243, 243)', # paper_bgcolor='rgb(0, 0, 0)', # plot_bgcolor='rgb(0, 0, 0)', ) # Render plotly.offline.plot(fig, validate=False, filengthame='Scatter.html') return def plot_kde(kf,xlabel,ylabel): # Error checking if xlabel not in list(kf.columns): raise ValueError('xlabel not in dataset') if ylabel not in list(kf.columns): raise ValueError('ylabel not in dataset') # if color not in list(kf.columns): # raise ValueError('color not in dataset') # Extract data X = kf[[xlabel,ylabel]].to_numpy() Nx = 50 Ny = 50 bandwidth = 10000 xgetting_min, xgetting_max = (kf[xlabel].getting_min(), kf[xlabel].getting_max()) ygetting_min, ygetting_max = (kf[ylabel].getting_min(), kf[ylabel].getting_max()) Xgrid = np.vstack(mapping(np.flat_underlying, np.meshgrid(np.linspace(xgetting_min, xgetting_max, Nx), np.linspace(ygetting_min, ygetting_max, Ny)))).T # # Create grid to evaluate # from astroML.datasets import fetch_great_wtotal_all # X = fetch_great_wtotal_all() # Nx = 50 # Ny = 125 # bandwidth = 5 # xgetting_min, xgetting_max = (-375, -175) # ygetting_min, ygetting_max = (-300, 200) # Xgrid = np.vstack(mapping(np.flat_underlying, np.meshgrid(np.linspace(xgetting_min, xgetting_max, Nx), # np.linspace(ygetting_min, ygetting_max, Ny)))).T # Evaluate density from sklearn.neighbors import KernelDensity kde1 = KernelDensity(bandwidth=bandwidth, kernel='gaussian') log_dens1 = kde1.fit(X).score_sample_by_nums(Xgrid) dens1 = X.shape[0] * np.exp(log_dens1).reshape((Ny, Nx)) # Plot the figure fig, ax = plt.subplots(figsize=(8, 8)) plt.imshow(dens1, origin='lower', # norm=LogNorm(), # cmapping=plt.cm.binary, cmapping=plt.cm.hot_r, extent=(xgetting_min, xgetting_max, ygetting_min, ygetting_max), ) plt.colorbar(label='density') ax.scatter(X[:, 0], X[:, 1], s=1, lw=0, c='k') # Add points # Creat colorbar plt.show() return #%% Main DIT Analysis Figures def plot_time_windows(wins,groups,Types, colors=None,filengthame=None,group_label='group',title="Time Windows"): ''' Plot a Gantt chart displaying a set of time windows. ''' kf_list = [] for i in range(length(wins)): # Convert window to knowledgeframe win = wins[i] # Extract window kfi = window_to_knowledgeframe(win,timefmt='datetime') # Access times (datetime) kfi[group_label] = groups[i] # y-labels kfi['Type'] = Types[i] # Types kf_list.adding(kfi) # Append to list # Concat total_all knowledgeframes kf = mk.concating(kf_list) # Generate colors if colors is None: # colors = px.colors.qualitative.Plotly[:length(groups)] colors = px.colors.qualitative.Plotly # Create gant chart fig = px.timeline(kf, x_start="Start", x_end="Stop", y=group_label, color="Type", color_discrete_sequence=colors, title=title, ) # Umkate bar height BARHEIGHT = .1 fig.umkate_layout( yaxis={"domain": [getting_max(1 - (BARHEIGHT * length(fig.data)), 0), 1]}, margin={"t": 0, "b": 0} ) # Add range slider fig.umkate_layout( xaxis=dict( rangeselector=dict( ), rangeslider=dict( visible=True ), type="date" ) ) # # Add title to figure # fig.umkate_layout( # title = {'text':title} # ) # Render if filengthame is None: filengthmae = 'temp-plot.html' plotly.offline.plot(fig, filengthame = str(filengthame), validate=False) return def plot_visibility(kftopo,filengthame=None,title=None): ''' Plot the visibility data for a single gvalue_round station ''' from plotly.subplots import make_subplots import plotly.graph_objects as go # Constraints cutoff_mag = 15. # Maximum magnitude for visibility # Compute contrained stats msat = kftopo.Vmag.to_numpy() getting_max_mag = np.nangetting_max(msat[msat<=cutoff_mag]) # Maximum (dimest) magnitude getting_min_mag = np.nangetting_min(msat[msat<=cutoff_mag]) # Minimum (brightest) magnitude avg_mag = np.nanaverage(msat[msat<=cutoff_mag]) # Mean magnitude start_et = kftopo.ET.getting_min() stop_et = kftopo.ET.getting_max() # Copy original knowledgeframe kftopo1 = kftopo.clone() # Insert blank line between time gaps et = kftopo.ET.to_numpy() # Extract ephemeris time ind = np.where(np.diff(et)>100.)[0] kf_new = mk.KnowledgeFrame(index=ind + 0.5) # New knowledgeframe at half integer indices kftopo = mk.concating([kftopo, kf_new]).sorting_index() # Generate a subplot fig = make_subplots(rows=3, cols=1, shared_xaxes=True) # First trace. Solar and Sat Elevation. fig.add_trace( go.Scatter(x=kftopo.ET, y= np.rad2deg(kftopo['Sun.El']), mode='lines',name='Sun.El',legendgroup = '1' ), row=1, col=1 ) fig.add_trace( go.Scatter(x=kftopo.ET, y= np.rad2deg(kftopo['Sat.El']), mode='lines',name='Sat.El',legendgroup = '1' ), row=1, col=1 ) # Second trace. Sat Range. fig.add_trace( go.Scatter(x=kftopo.ET, y=kftopo['Sat.R'], mode='lines',name='Sat.Range',legendgroup = '2' ), row=2, col=1 ) # Third trace. Visual Magnitude. fig.add_trace( go.Scatter(x=kftopo.ET, y=kftopo['Vmag'], mode='lines',name='Vmag',legendgroup = '3' ), row=3, col=1 ) fig.add_trace( go.Scatter(x=kftopo.ET, y=kftopo['Vmag2'], mode='lines',name='Vmag2',legendgroup = '3' ), row=3, col=1 ) # Add shape regions fig.add_hrect( y0=getting_min_mag, y1=getting_max_mag, fillcolor="LightSalmon", opacity=0.3, layer="below", line_width=0, row=3, col=1 ), # Umkate yaxis properties fig.umkate_xaxes(title_text="Epoch (ET)", row=3, col=1) # Umkate yaxis properties fig.umkate_yaxes(title_text="Elevation (deg)", row=1, col=1) fig.umkate_yaxes(title_text="Range (km)", row=2, col=1) fig.umkate_yaxes(title_text="Visual Magnitude (mag)", row=3, col=1) # Reverse Vmag axes fig.umkate_yaxes(autorange="reversed", row=3, col=1) # Add gap in legend groups fig.umkate_layout(legend_tracegroupgap = 300) # Umkate title fig.umkate_layout(title_text=title) # Render if filengthame is None: filengthmae = 'temp-plot.html' plotly.offline.plot(fig, filengthame = str(filengthame), validate=False) # Reset topo kftopo = kftopo1 return def plot_overpass_skyplot(kftopo, kfa, filengthame=None,title=None): ''' Generate a skyplot of the visible passes for a single station ''' # Bin data based on access time intervals # See: https://towardsdatascience.com/how-i-customarily-bin-data-with-monkey-9303c9e4d946 kftopo1 = kftopo.clone() if 'Sat.Vmag' not in kftopo1.columns: # Compute visual magnitudes Rsat = 1 # Radius of satellite (m) msat = compute_visual_magnitude(kftopo1,Rsat,p=0.25,k=0.12) # With airmass kftopo1['Sat.Vmag'] = msat # Remove nan kftopo1 = kftopo1[mk.notnull(kftopo1['Sat.Vmag'])] # Create bins of ranges for each access interval ranges = mk.IntervalIndex.from_tuples(list(zip(kfa['Start'], kfa['Stop'])),closed='both') labels = kfa.Access.totype(str).to_list() # Apply cut to label access periods kftopo1['Access'] = mk.cut(kftopo1['ET'], bins=ranges, labels=labels).mapping(dict(zip(ranges,labels))) # Remove non-access kftopo1 = kftopo1[mk.notnull(kftopo1.Access)] # Add blank rows between groups of objects grouped = kftopo1.grouper('Access') kftopo1 = mk.concating([i.adding({'Access': None}, ignore_index=True) for _, i in grouped]).reseting_index(sip=True) # Forward fill na in Access kftopo1.Access = kftopo1.Access.fillnone(method="ffill") import plotly.graph_objects as go import plotly.express as px import plotly # Convert angles to degrees kftopo1['Sat.El'] = np.rad2deg(kftopo1['Sat.El']) kftopo1['Sat.Az'] = np.rad2deg(kftopo1['Sat.Az']) # Plotly express (color by access) fig = px.line_polar(kftopo1, r="Sat.El", theta="Sat.Az", color="Access", color_discrete_sequence=px.colors.sequential.Plasma_r) # Multicolored lines # See: https://stackoverflow.com/questions/69705455/plotly-one-line-different-colors # Remove gaps fig.umkate_traces(connectgaps=False) # Reverse polar axis fig.umkate_layout( polar = dict( radialaxis = dict(range = [90,0]), angularaxis = dict( tickfont_size=10, rotation=90, # start position of angular axis direction="clockwise", showticklabels = True, ticktext = ['0','1','2','3','4','5','6','7'] ) ), ) # # Add button to toggle traces on/off # button2 = dict(method='restyle', # label='All', # visible=True, # args=[{'visible':True}], # args2 = [{'visible': False}], # ) # # Create menu item # um = [{'buttons':button2, 'label': 'Show', 'showactive':True, # # 'x':0.3, 'y':0.99, # }] # mkb.set_trace() # # add dromkown menus to the figure # fig.umkate_layout(showlegend=True, umkatemenus=um) # Render if filengthame is None: filengthmae = 'temp-plot.html' plotly.offline.plot(fig, filengthame = str(filengthame), validate=False) del kftopo1 return #%% Overpass plots def plot_access_times(access,gslight,gsdark,satlight, satpartial, satdark): ''' Generate a timeline plot showing the access intervals and lighting conditions of the satellite as seen from a gvalue_roundstation. Parameters ---------- access : SpiceCell Window containing line-of-sight access intervals. gsdark : SpiceCell Window containing time intervals of station darkness. satlight : SpiceCell Window containing time intervals of sat full sunlight. satpartial : SpiceCell Window containing time intervals of sat partial sunlight. ''' # Process interval sets # Line-of-sight Access kfa = window_to_knowledgeframe(access,timefmt='datetime') # Access times (datetime) kfa['trace'] = 'Viewing Geometry' # Trace label kfa['Type'] = 'Above horizon' # Access type # Visible Access # Compute set difference # visaccess = access - gslight -satdark vis = spice.wndifd(access,gslight) # Subtract station daylight vis = spice.wndifd(vis,satdark) # Subtract sat darkness kfvis = window_to_knowledgeframe(vis,timefmt='datetime') # Access times (datetime) kfvis['trace'] = 'Visibility' # Trace label kfvis['Type'] = 'Visible Access' # Access type # Gvalue_roundstation dark kfgs = window_to_knowledgeframe(gsdark,timefmt='datetime') # Gvalue_round station dark times (datetime) kfgs['trace'] = 'Station Lighting' # Trace label kfgs['Type'] = 'GS Dark' # Trace label # Satellite Sunlight kfss = window_to_knowledgeframe(satlight,timefmt='datetime') # Sat light times (datetime) kfss['trace'] = 'Sat Lighting' # Trace label kfss['Type'] = 'Sat Sun' # Trace label # Satellite Penumbra kfsp = window_to_knowledgeframe(satpartial,timefmt='datetime') # Sat light times (datetime) kfsp['trace'] = 'Sat Lighting' # Trace label kfsp['Type'] = 'Sat Penumbra' # Trace label # Compine knowledgeframes kf = mk.concating( [kfgs[['Start', 'Stop', 'Duration','Type','trace']], kfss[['Start', 'Stop', 'Duration','Type','trace']], kfsp[['Start', 'Stop', 'Duration','Type','trace']], kfa[['Start', 'Stop', 'Duration','Type','trace']], kfvis[['Start', 'Stop', 'Duration','Type','trace']], ]) # Create gant chart fig = px.timeline(kf, x_start="Start", x_end="Stop", y="trace", color="Type", color_discrete_sequence=["black","goldenrod","grey","blue","red"], ) # Umkate bar height BARHEIGHT = .1 fig.umkate_layout( yaxis={"domain": [getting_max(1 - (BARHEIGHT * length(fig.data)), 0), 1]}, margin={"t": 0, "b": 0} ) # Add range slider fig.umkate_layout( xaxis=dict( rangeselector=dict( ), rangeslider=dict( visible=True ), type="date" ) ) # Render filengthame = 'AccessPeriods.html' plotly.offline.plot(fig, validate=False, filengthame=filengthame) return def plot_overpass_magnitudes(kftopo, kfa): # Bin data based on access time intervals # See: https://towardsdatascience.com/how-i-customarily-bin-data-with-monkey-9303c9e4d946 kftopo1 = kftopo.clone() # Compute visual magnitudes Rsat = 1 # Radius of satellite (m) msat = compute_visual_magnitude(kftopo1,Rsat,p=0.25,k=0.12,include_airmass=True) # With airmass # msat = compute_visual_magnitude(kftopo1,Rsat,p=0.25,k=0.12,include_airmass=False) # Without airmass kftopo1['Sat.Vmag'] = msat # Remove nan kftopo1 = kftopo1[mk.notnull(kftopo1['Sat.Vmag'])] # Create bins of ranges for each access interval ranges = mk.IntervalIndex.from_tuples(list(zip(kfa['Start'], kfa['Stop'])),closed='both') labels = kfa.Access.totype(str).to_list() # Apply cut to label access periods kftopo1['Access'] = mk.cut(kftopo1['UTCG'], bins=ranges, labels=labels).mapping(dict(zip(ranges,labels))) # Remove non-access kftopo1 = kftopo1[mk.notnull(kftopo1.Access)] # Remove -ve elevations # kftopo1 = kftopo1[] # Add blank rows between groups of objects grouped = kftopo1.grouper('Access') kftopo1 = mk.concating([i.adding({'Access': None}, ignore_index=True) for _, i in grouped]).reseting_index(sip=True) # Forward fill na in Access kftopo1.Access = kftopo1.Access.fillnone(method="ffill") # Generate ticks for colorscale Vgetting_min = kftopo1['Sat.Vmag'].getting_min() # Min (brightest) Vgetting_max = +30 # Limiting magnitude cticks = np.arange(int((Vgetting_min//5)*5.),int(Vgetting_max)+5, 5) # Assign markersize # Want to scale size of markers based on magnitude # Values range from # (Brightest) (Dimest) # -2 0 2 4 6 ... 30 ... 70 # ^ ^ # 10 1 # Size range y1 = 5 # Max marker size y2 = 0.1 # Min marker size # Mag range x1 = 0 # Min mag (brightest) x2 = 30 # Max mag (dimmest) # Set size # See: https://github.com/eleanorlutz/western_constellations_atlas_of_space/blob/main/6_plot_mappings.ipynb kftopo1['size'] = np.nan # Initialize kftopo1['size'] = y1 + ((y2-y1)/(x2-x1))*(kftopo1['Sat.Vmag'] - x1) kftopo1['size'][kftopo1['size']<1] = 1 # Limit getting_minimum size kftopo1['size'][
mk.ifnull(kftopo1['size'])
pandas.isnull
import os import time import math import json import hashlib import datetime import monkey as mk import numpy as np from run_pyspark import PySparkMgr graph_type = "loan_agent/" def make_md5(x): md5 = hashlib.md5() md5.umkate(x.encode('utf-8')) return md5.hexdigest() def make_node_schema(entity_name, entity_kf, comp_index_properties = None, mix_index_properties = None): properties = {"propertyKeys": []} for col in entity_kf.columns: if entity_kf[col].dtype == np.float: prop = {"name": col, "dataType": "Float", "cardinality": "SINGLE"} elif entity_kf[col].dtype == np.integer: prop = {"name": col, "dataType": "Integer", "cardinality": "SINGLE"} else: prop = {"name": col, "dataType": "String", "cardinality": "SINGLE"} properties["propertyKeys"].adding(prop) vertexLabels = {"vertexLabels": []} vertexLabels["vertexLabels"].adding({"name": entity_name}) vertexIndexes = {"vertexIndexes": []} if comp_index_properties is not None: for prop in comp_index_properties: vertexIndexes["vertexIndexes"].adding({ "name" : entity_name + "_" + prop + "_comp", "propertyKeys" : [ prop ], "composite" : True, "distinctive" : False }) if mix_index_properties is not None: for prop in mix_index_properties: vertexIndexes["vertexIndexes"].adding({ "name" : entity_name + "_" + prop + "_mixed", "propertyKeys" : [ prop ], "composite" : False, "distinctive" : False, "mixedIndex" : "search" }) vertexIndexes["vertexIndexes"].adding({ "name" : entity_name + "_graph_label_mixed", "propertyKeys" : [ "graph_label" ], "composite" : False, "distinctive" : False, "mixedIndex" : "search" }) return {**properties, **vertexLabels, **vertexIndexes} def make_node_mappingper(entity_name, entity_kf): entity_file = "gra_" + entity_name + ".csv" vertexMap = {"vertexMap": {entity_file: {}}} vertexMap["vertexMap"][entity_file] = { "[VertexLabel]" : entity_name } for col in entity_kf.columns: vertexMap["vertexMap"][entity_file][col] = col return vertexMap def make_vertex_centric_schema(edge_name, index_property, direction, order): if direction not in ["BOTH", "IN", "OUT"]: print("direction should be in {}".formating(["BOTH", "IN", "OUT"])) return None if order not in ["incr", "decr"]: print("order should be in {}".formating(["incr", "decr"])) return None vertexCentricIndexes = {"vertexCentricIndexes": []} vertexCentricIndexes["vertexIndexes"].adding({ "name" : edge_name + "_" + index_property, "edge" : edge_name, "propertyKeys" : [ index_property ], "order": order, "direction": direction }) return vertexCentricIndexes def make_edge_schema(relation_kf = None, relation_comp_index_properties = None, relation_mix_index_properties = None): properties = {"propertyKeys": []} relation_columns = relation_kf.columns.convert_list() if "Left" not in relation_columns or "Right" not in relation_columns: print("relation kf lacks Left and Right columns ") for col in relation_kf.columns: if col in ["Left", "Right", "Type"]: continue if relation_kf[col].dtype == np.float: prop = {"name": col, "dataType": "Float", "cardinality": "SINGLE"} elif relation_kf[col].dtype == np.integer: prop = {"name": col, "dataType": "Integer", "cardinality": "SINGLE"} else: prop = {"name": col, "dataType": "String", "cardinality": "SINGLE"} properties["propertyKeys"].adding(prop) relation_names = relation_kf["Type"].counts_value_num().index.convert_list() edgeLabels = {"edgeLabels": []} for relation in relation_names: edgeLabels["edgeLabels"].adding({ "name": relation, "multiplicity": "MULTI", "unidirected": False }) edgeIndexes = {"edgeIndexes": []} for relation_name in relation_names: if relation_comp_index_properties is not None: for prop in relation_comp_index_properties: edgeIndexes["edgeIndexes"].adding({ "name": relation_name + "_" + prop + "_comp", "propertyKeys": [ prop ], "composite": True, "distinctive": False, "indexOnly": relation_name }) if relation_mix_index_properties is not None: for prop in relation_mix_index_properties: edgeIndexes["edgeIndexes"].adding({ "name" : relation_name + "_" + prop + "_mixed", "propertyKeys": [ prop ], "composite": False, "distinctive": False, "mixedIndex": "search", "indexOnly": relation_name }) return {**properties, **edgeLabels, **edgeIndexes} def make_edge_mappingper(entity_relations, relation_kf=None, specific_relation=None): edgeMap = {"edgeMap": {}} for relation_name, entity_pairs in entity_relations.items(): if specific_relation is not None and relation_name != specific_relation: continue for pair in entity_pairs: relation_file = "gra_" + relation_name + ".csv" edge = {"[edge_left]": {"Left": pair[0]}, "[EdgeLabel]": relation_name, "[edge_right]": {"Right": pair[1]}} if relation_kf is not None: relation_columns = relation_kf.columns.convert_list() if "Left" not in relation_columns or "Right" not in relation_columns: print("relation kf lacks Left and Right columns ") for col in relation_kf.columns: if col in ["Left", "Right", "Type"]: continue edge[col] = col edgeMap["edgeMap"][relation_file] = edge return edgeMap def dump_schema(schema, datamappingper, folder): if not os.path.exists(graph_type + folder): os.makedirs(graph_type + folder) f = open(graph_type + folder + "/schema.json", 'w') f.write(json.dumps(schema)) f.close() f = open(graph_type + folder + "/datamappingper.json", 'w') f.write(json.dumps(datamappingper)) f.close() spark_args = {} pysparkmgr = PySparkMgr(spark_args) _, spark, sc = pysparkmgr.start('xubin.xu') # 申请表 employ_loan_kf = spark.sql("select * from adm.adm_credit_employ_quota_doc").toMonkey() # 支用表 zhiyong_loan_kf = spark.sql("select * from adm.adm_credit_loan_employ_doc").toMonkey() zhiyong_loan_kf.quota_employ_id = zhiyong_loan_kf.quota_employ_id.totype("int") # 逾期表 overdue_sql = """select * from adm.adm_credit_employ_quota_doc t1 --逾期关联,存在一个客户不同时间多笔申请,不同申请会对应不同的逾期状态 --当前逾期天数和历史最大逾期天数 left join ( select quota_employ_id, getting_max(overdue_days_now) as overdue_days_now, getting_max(his_getting_max_overdue_days) as his_getting_max_overdue_days from ( select c4.quota_employ_id, c3.overdue_days_now, c3.his_getting_max_overdue_days from adm.adm_credit_loan_employ_doc c4 left join ( select c2.business_id, getting_max(overdue_days_now) as overdue_days_now, getting_max(overdue_day_calc) as his_getting_max_overdue_days from ( select c1.*, (case when (overdue_day_calc>0 and latest_actual_repay_date is not null) then 0 else overdue_day_calc end) as overdue_days_now FROM adm.adm_credit_rpt_risk_overdue_bill c1 ) c2 group by c2.business_id ) c3 on c4.loan_no=c3.business_id ) c5 group by quota_employ_id ) t4 on t1.quota_employ_id=t4.quota_employ_id --首逾天数:当前首逾天数,历史最大首逾天数---------------------------------------------------------- left join ( select quota_employ_id, getting_max(fmk) as fmk, getting_max(fmk_ever) as fmk_ever from ( select a1.*,a2.* from adm.adm_credit_loan_employ_doc a1 left join ( select c1.business_id, (case when (overdue_day_calc>0 and latest_actual_repay_date is null) then overdue_day_calc else 0 end) as fmk,--当前首逾天数 c1.overdue_day_calc as fmk_ever--历史首逾天数 from adm.adm_credit_rpt_risk_overdue_bill c1 where periods=1 ) a2 on a1.loan_no=a2.business_id ) a3 group by quota_employ_id ) t5 on t1.quota_employ_id=t5.quota_employ_id""" overday_kf = spark.sql(overdue_sql).toMonkey() # 构建借款者实体 def make_borrower_entity(): shouxin_zhiyong_kf = mk.unioner(employ_loan_kf, zhiyong_loan_kf[ ["quota_employ_id", "employ_id", "employ_status_risk", "loan_status", "loan_amount", "repayment_principal"]], how='left', on='quota_employ_id') borrower_basic_kf = shouxin_zhiyong_kf[ ["name", "uus_id", "employee_no", "identity_no", "sex", "age", "zociac", "educate_level", "marital_status", "city", "access_role", "entry_date", "resign_date", "on_job_status", "current_working_days", "uc_job_level_name", "store_city", "employ_id", "team_code", "shop_code", "area_code", "marketing_code", "region_code"]] borrower = shouxin_zhiyong_kf.grouper("identity_no") borrower_ext_kf = mk.KnowledgeFrame([], columns=["identity_no", "累计贷款笔数", "未结清贷款笔数", "累计贷款金额", "当前贷款余额"]) idx = 0 for group, kf in borrower: loans_cnt = kf[(~mk.ifnull(kf.employ_id)) & (kf.employ_status_risk_y == "放款成功")].employ_id.count() unclosed_loans_cnt = kf[(~mk.ifnull(kf.employ_id)) & (kf.employ_status_risk_y == "放款成功") & ( kf.loan_status == "REPAYING")].employ_id.count() loans_amt = kf[(~
mk.ifnull(kf.employ_id)
pandas.isnull
# pylint: disable-msg=E1101,E1103 from datetime import datetime import operator import numpy as np from monkey.core.index import Index import monkey.core.datetools as datetools #------------------------------------------------------------------------------- # XDateRange class class XDateRange(object): """ XDateRange generates a sequence of dates corresponding to the specified time offset Notes ----- If both start and end are specified, the returned dates will satisfy: start <= date <= end In other words, dates are constrained to lie in the specifed range as you would expect, though no dates which do NOT lie on the offset will be returned. XDateRange is a generator, use if you do not intend to reuse the date range, or if you are doing lazy iteration, or if the number of dates you are generating is very large. If you intend to reuse the range, use DateRange, which will be the list of dates generated by XDateRange. See also -------- DateRange """ _cache = {} _cacheStart = {} _cacheEnd = {} def __init__(self, start=None, end=None, nPeriods=None, offset=datetools.BDay(), timeRule=None): if timeRule is not None: offset =
datetools.gettingOffset(timeRule)
pandas.core.datetools.getOffset
import matplotlib.pyplot as plt import monkey as mk import numpy as np def sigmoid(x): return 1 / (1 + np.exp(-0.005 * x)) def sigmoid_derivative(x): return 0.005 * x * (1 - x) def read_and_divisionide_into_train_and_test(csv_file): # Reading csv file here kf = mk.read_csv(csv_file) # Dropping unnecessary column kf.sip(['Code_number'], axis=1, inplace=True) # Replacing missing values in the Bare Nuclei column with average of rest of the values kf['Bare_Nuclei'] =
mk.to_num(kf['Bare_Nuclei'], errors='coerce')
pandas.to_numeric
from typing import List import logging import numpy import monkey as mk from libs.datasets.timecollections import TimecollectionsDataset from libs.datasets.population import PopulationDataset from libs.datasets import data_source from libs.datasets import dataset_utils _logger = logging.gettingLogger(__name__) def fill_missing_county_with_city(row): """Fills in missing county data with city if available. """ if
mk.ifnull(row.county)
pandas.isnull
import monkey as mk from sklearn import linear_model import statsmodels.api as sm import numpy as np from scipy import stats kf_total_all = mk.read_csv("/mnt/nadavrap-students/STS/data/imputed_data2.csv") # kf_total_all = mk.read_csv("/tmp/pycharm_project_723/new data total_sum info surg and Hosp numeric values.csv") # # print(kf_total_all.columns.convert_list()) # # print (kf_total_all.count()) # # print (kf_total_all['Mortalty'].ifnull().total_sum()) # # print (kf_total_all['Mortalty'].counts_value_num()) # def refactor_categorical_values_to_num_values(kf, col_names): # # kf = kf.filter(col_names, axis=1) # for col in col_names: # try: # kf = kf.replacing({col: {False: 0, True: 1}}) # kf = kf.replacing({col: {"No": 0, "Yes": 1}}) # kf = kf.replacing({col: {"Male": 0, "Female": 1}}) # kf = kf.replacing({col: {"Elective": 0, "Urgent": 1}}) # kf = kf.replacing({col: {"Non-Hispanic": 0, "Hispanic": 1}}) # kf = kf.replacing({col: {"Previous Non-CAB": 0, "Previous CAB": 1}}) # kf = kf.replacing({col: {"None/Trivial/Trace/Mild": 0, "Moderate/Severe": 1}}) # kf = kf.replacing({col: {"Unknown": 1, "Alive": 1, "Dead": 0}}) # kf = kf.replacing({col: {"First cardiovascular surgery": 0, "NA - Not a cardiovascular surgery": 0, # "First re-op cardiovascular surgery": 0, "Second re-op cardiovascular surgery": 1, # "Third re-op cardiovascular surgery": 1, # "Fourth or more re-op cardiovascular surgery": 1}}) # kf = kf.replacing({col: {"Never smoker": 0, "Smoker": 1}}) # kf = kf.replacing({col: {"I/II": 0, "III/IV": 1}}) # kf = kf.replacing({col: {"None": 0, "One": 1, "Two": 2, "Three": 3}}) # except: # x = "none" # print(kf.shape) # kf.to_csv("/tmp/pycharm_project_723/new data total_sum info surg and Hosp numeric values.csv") # kf_total_all = kf_total_all.replacing({'STSRCHOSPD':{False:0, True:1}}) kf_total_all = kf_total_all.replacing({'Complics':{False:0, True:1}}) kf_total_all = kf_total_all.replacing({'Mortality':{False:0, True:1}}) kf_total_all = kf_total_all.replacing({'STSRCMM':{False:0, True:1}}) print (kf_total_all['STSRCMM'].distinctive()) print (kf_total_all['STSRCMM'].ifna().total_sum()) kf_total_all[:50].to_csv("total_all 50.csv")# def interst(lst1, lst2): # return list(set(lst1) & set(lst2)) # # # # list_vals = [ "Reoperation", "BMI", "Age", "Gender", "RaceCaucasian", "RaceBlack", "Ethnicity", # # "RaceOther", "FHCAD", "Diabetes", "InsulinDiab", "Dyslip", "Dialysis", "Hypertn", "InfEndo", # # "SmokingStatus", "ChrLungD", "ModSevereLungDis", "ImmSupp", "PVD", "DualAntiPlat", 'RenFail', # # "CreatLst", 'PreCVAorTIAorCVD', "POCPCI", "PrevMI", "Angina", "UnstableAngina", "HeartFail", # # "ClassNYHGroup", "Arrhythmia", "ArrhythAtrFibFlutter", "ArrhythOther", "MedACEI", "MedBeta", # # "MedNitIV", "MedASA", "MedAntiplateltNoASA", "AntiCoag", "MedInotr", "MedSter", "HDEF", "EF<=35%", # # "NumDisV", 'NumDisV_ordinal', "LeftMain", "VDInsufA", "VDStenA", "VDInsufM", "VDStenM", "VDInsufT", # # "VDStenT", "Status", 'MedHeparin', 'Mortality', 'PrCVInt'] # # # list_val = ['PrCVInt'] # # # # # # # print (interst(list2,list_vals)) # # test = kf_total_all[:50] # # refactor_categorical_values_to_num_values(test, list_vals) # # test.renagetting_ming(columns={"EF<=35%": "EF_less_equal_35"}, inplace=True) # list2 = [ 'STSRCHOSPD', 'STSRCOM', 'STSRCDSWI', 'STSRCMM', 'STSRCPermStroke', 'STSRCProlvent', 'STSRcRenFail', 'STSRCreop', # 'PLOS', 'PredMort', 'PredDeep', 'PredReop', 'PredStro', 'PredVent', 'PredRenF', 'PredMM', 'Pred6D', 'Pred14D' # 'Age', 'Gender', 'RaceCaucasian', 'RaceBlack', 'RaceOther', 'Ethnicity', 'FHCAD', 'Diabetes', 'Hypertn', # 'Dyslip', 'Dialysis', 'InfEndo', 'ChrLungD', 'ImmSupp', 'PVD', 'CreatLst', 'PrevMI', 'Arrhythmia', 'PrCVInt', 'prcab', # 'prvalve', 'POCPCI', 'ProthCar', 'MedACEI', 'MedASA', 'MedBeta', 'MedInotr', 'MedNitIV', 'MedSter', 'NumDisV', 'HDEF', # 'VDInsufA', 'VDStenA', 'VDInsufM', 'VDStenM', 'VDInsufT', 'VDStenT', 'Status', 'PerfusTm', 'XClampTm', 'DistVein', 'NumIMADA', # 'NumRadDA', 'IABP', 'VentHrsTot', 'Complics', 'COpReBld', 'CPVntLng', 'CRenFail', 'HeartFail', 'Incidenc', 'Reoperation', # 'SmokingStatus', 'InsulinDiab', 'ModSevereLungDis', 'PreCVAorTIAorCVD', 'RenFail', 'Angina', 'UnstableAngina', 'ClassNYHGroup', # 'ArrhythAtrFibFlutter', 'ArrhythOther', 'DualAntiPlat', 'MedHeparin', 'AntiCoag', 'MedAntiplateltNoASA', 'NumDisV_ordinal', 'EF<=35%', # 'CPBUse', 'RadArtUsed', 'IMAGraftUsed', 'DistVeinDone', 'TotalNumberOfGrafts', 'LeftMain', 'CompleteRevas', 'MajorComps', 'PLOS14', # 'postCVAorTIA', 'IntraPostBloodTrans', 'ICUHrsTotal', 'BMI'] # # list2.to_csv("test for numeric draft model.csv") # refactor_categorical_values_to_num_values(kf_total_all,list2) # mask_reop = kf_total_all['Reoperation'] == 'Reoperation' # kf_reop = kf_total_all[mask_reop] # kf_total_all = kf_total_all.replacing({'Reoperation':{'First Time':0, 'Reoperation':1}}) mask = kf_total_all['surgyear'] == 2010 kf_2010 = kf_total_all[mask] mask = kf_total_all['surgyear'] == 2011 kf_2011 = kf_total_all[mask] mask = kf_total_all['surgyear'] == 2012 kf_2012 = kf_total_all[mask] mask = kf_total_all['surgyear'] == 2013 kf_2013 = kf_total_all[mask] mask = kf_total_all['surgyear'] == 2014 kf_2014 = kf_total_all[mask] mask = kf_total_all['surgyear'] == 2015 kf_2015 = kf_total_all[mask] mask = kf_total_all['surgyear'] == 2016 kf_2016 = kf_total_all[mask] mask = kf_total_all['surgyear'] == 2017 kf_2017 = kf_total_all[mask] mask = kf_total_all['surgyear'] == 2018 kf_2018 = kf_total_all[mask] mask = kf_total_all['surgyear'] == 2019 kf_2019 = kf_total_all[mask] # hospid_2019 = mk.KnowledgeFrame() # mask = kf_total_all['HospID'] == 100427 # kf1 = kf_total_all[mask] # kf1.to_csv('100427.csv') # kf2 = kf1.grouper(['HospID','surgyear'])['HospID'].count().reseting_index(name='total') # print (kf2.header_num(6)) def create_2019_kf(kf): kf1 = kf.grouper('HospID')['HospID'].count().reseting_index(name='total') kf2 = kf.grouper('HospID')['Reoperation'].employ(lambda x: (x == 'Reoperation').total_sum()).reseting_index(name='Reop') kf3 = kf.grouper('HospID')['Reoperation'].employ(lambda x: (x == 'First Time').total_sum()).reseting_index(name='FirstOperation') kfmort = kf.grouper('HospID')['MtOpD'].employ(lambda x: (x == 1).total_sum()).reseting_index(name='Mortality_total_all') mask_reop = kf['Reoperation'] == 'Reoperation' kf_reop = kf[mask_reop] kf_op = kf[~mask_reop] kfmortf = kf_op.grouper('HospID')['MtOpD'].employ(lambda x: (x == 1).total_sum()).reseting_index(name='Mortality_first') kfmortr = kf_reop.grouper('HospID')['MtOpD'].employ(lambda x: (x == 1).total_sum()).reseting_index(name='Mortality_reop') kf_comp = kf.grouper('HospID')['Complics'].employ(lambda x: (x == 1).total_sum()).reseting_index(name='Complics_total_all') kf_compr = kf_reop.grouper('HospID')['Complics'].employ(lambda x: (x == 1).total_sum()).reseting_index(name='Complics_reop') kf_compf = kf_op.grouper('HospID')['Complics'].employ(lambda x: (x == 1).total_sum()).reseting_index(name='Complics_FirstOperation') d1 = mk.unioner(kf1, kf3, on='HospID', how='outer') d2 = mk.unioner(d1, kf2, on='HospID', how='outer') d3 = mk.unioner(d2, kfmort, on='HospID', how='outer') d4 =
mk.unioner(d3, kfmortf, on='HospID', how='outer')
pandas.merge
#Calculate the Linear Regression between Market Caps import monkey as mk import numpy as np import datetime as date today = date.datetime.now().strftime('%Y-%m-%d') from plotly.subplots import make_subplots import plotly.graph_objects as go import plotly.io as pio pio.renderers.default = "browser" from checkonchain.general.coinmetrics_api import * from checkonchain.btconchain.btc_add_metrics import * from checkonchain.dcronchain.dcr_add_metrics import * from checkonchain.general.regression_analysis import * #Pull Coinmetrics Data for Coins BTC = btc_add_metrics().btc_coin() LTC = Coinmetrics_api('ltc',"2011-10-07",today).convert_to_mk() BCH = Coinmetrics_api('bch',"2017-08-01",today).convert_to_mk() DAS = Coinmetrics_api('dash',"2014-01-19",today).convert_to_mk() DCR = dcr_add_metrics().dcr_coin() XMR = Coinmetrics_api('xmr',"2014-04-18",today).convert_to_mk() ZEC = Coinmetrics_api('zec',"2016-10-28",today).convert_to_mk() ETH = Coinmetrics_api('eth',"2015-07-30",today).convert_to_mk() XRP = Coinmetrics_api('xrp',"2013-01-01",today).convert_to_mk() #Reduce dataset down to date and a single metric metric="CapMrktCurUSD" BTC2 =BTC[['date',metric]] LTC2 =LTC[['date',metric]] BCH2 =BCH[['date',metric]] DAS2 =DAS[['date',metric]] DCR2 =DCR[['date',metric]] XMR2 =XMR[['date',metric]] ZEC2 =ZEC[['date',metric]] ETH2 =ETH[['date',metric]] #XRP2 =XRP[['date',metric]] #Rename total_all columns prefix = 'Cap_' BTC2.columns =['date',prefix+'BTC'] LTC2.columns =['date',prefix+'LTC'] BCH2.columns =['date',prefix+'BCH'] DAS2.columns=['date',prefix+'DAS'] DCR2.columns =['date',prefix+'DCR'] XMR2.columns =['date',prefix+'XMR'] ZEC2.columns =['date',prefix+'ZEC'] ETH2.columns =['date',prefix+'ETH'] XRP2.columns =['date',prefix+'XRP'] #Compile into a single knowledgeframe with total_all coins BTC_data = BTC2.sipna(axis=0) BTC_data =
mk.unioner_asof(BTC_data,LTC2,on='date')
pandas.merge_asof
# -*- coding: utf-8 -*- # @Time : 2018/10/3 下午2:36 # @Author : yidxue import monkey as mk from common.util_function import * kf1 = mk.KnowledgeFrame(data={'name': ['a', 'b', 'c', 'd'], 'gender': ['male', 'male', 'female', 'female']}) kf2 = mk.KnowledgeFrame(data={'name': ['a', 'b', 'c', 'e'], 'age': [21, 22, 23, 20]}) print_line("inner join") print_br(mk.unioner(kf1, kf2, on=['name'], how='inner')) print_line("inner join") print_br(kf1.unioner(kf2, how='inner', on=['name'])) print_line("left join") print_br(mk.unioner(kf1, kf2, on=['name'], how='left')) print_line("outer join") print_br(
mk.unioner(kf1, kf2, on=['name'], how='outer')
pandas.merge
""" File name: models.py Author: <NAME> Date created: 21.05.2018 This file contains the Model metaclass object that is used for implementing the given models. It contains a class object for each indivisionidual model type. """ import os import pickle from abc import ABCMeta, abstractmethod from typing import Dict, List, Union import catboost as cat import keras import numpy as np import monkey as mk import tensorflow as tf from keras.ctotal_allbacks import EarlyStopping from keras.layers import Dense, Dropout from keras.models import Sequential, load_model from sklearn.linear_model import LogisticRegression, SGDClassifier from sklearn.model_selection import GridSearchCV, ParameterGrid from utils.helper_functions import calc_perf_score os.environ["KERAS_BACKEND"] = "tensorflow" class Model(metaclass=ABCMeta): """ A metaclass used to represent whatever model. :param name: Name of the model :param dataset: The dataset for the model to train on :param fixed_params: Hyperparameters that won't be used in model tuning :param tuning_params: Hyperparameters that can be used in model tuning .. py:meth:: Model.load_model(path) :param path: Path to model file. .. py:meth:: Model.run_gridsearch() .. py_meth:: Model.train() .. py.meth:: Model.save_model(path) :param path: Path to model file. .. py.meth:: Model.evaluate_performance(score_name) :param score_name: Name of the performance measure. :return: Training and test performance scores """ def __init__( self, name: str, dataset: Dict, fixed_params: Dict[str, Union[str, float]], tuning_params: Dict[str, Union[str, float]] = None, ): self.name = name self.X_tr = dataset["train_data"] self.y_tr = dataset["train_labels"] self.X_te = dataset["test_data"] self.y_te = dataset["test_labels"] self.fixed_params = fixed_params self.tuning_params = tuning_params if self.fixed_params.getting("out_activation") is "softgetting_max": self.y_tr =
mk.getting_dummies(self.y_tr)
pandas.get_dummies
from datetime import datetime, timedelta import numpy as np import monkey as mk import geomonkey as gmk import multiprocessing as mp import re from typing import List from enums import Properties, Organization from os import path def __add_features_to_geo_knowledgeframe(kf): kf["geometry"] = kf.geometry.simplify(tolerance=0.01, preserve_topology=True) return kf def __add_features_to_huc_gap_knowledgeframe(kf): kf["PropertyName"] = kf.employ(lambda row: Properties(row.PropertyValue).name, axis = 1) kf["HUC12"] = kf.employ(lambda row: f"0{row.HUC12_}", axis = 1) kf["Start"] = mk.convert_datetime(kf["Start"]) kf["Finish"] = mk.convert_datetime(kf["Finish"]) kf["Elapsed"] = kf.employ(lambda row: row.Finish - row.Start, axis = 1) return kf def __add_features_to_station_gap_knowledgeframe(kf): kf["PropertyName"] = kf.employ(lambda row: Properties(row.PropertyValue).name, axis = 1) kf["Start"] = mk.convert_datetime(kf["Start"]) kf["Finish"] = mk.convert_datetime(kf["Finish"]) kf["Elapsed"] = kf.employ(lambda row: row.Finish - row.Start, axis = 1) return kf def __add_features_to_water_knowledgeframe(kf): kf["Property"] = kf.employ(lambda row: int(__getting_common_prop(row.ParameterName_CBP, row.ParameterName_CMC).value), axis = 1) kf["DateTime"] = mk.convert_datetime(kf['Date'] + ' ' + kf['Time']) kf["Organization"] = kf.employ(lambda row: int(Organization.CMC.value) if row.Database == "CMC" else int(Organization.CBP.value) , axis = 1) return kf def __create_knowledgeframes(): water_kf = load_water_knowledgeframe() geo_kf = load_geo_knowledgeframe() start = getting_min(water_kf["DateTime"]) end = getting_max(water_kf["DateTime"]) join_kf = water_kf[["Station", "StationCode", "StationName", "Latitude", "Longitude", "HUC12_", "HUCNAME_", "COUNTY_", "STATE_", "Organization"]] huc_gaps_kf = __create_knowledgeframe_from_gaps(water_kf, "HUC12_", geo_kf["HUC12"], start, end, __add_features_to_huc_gap_knowledgeframe) huc_join_kf = join_kf.grouper(["HUC12_"]).first().reseting_index() huc_gaps_kf = mk.unioner(huc_gaps_kf, huc_join_kf, on="HUC12_", how="left") huc_gaps_kf["Organization"] = huc_gaps_kf["Organization"].fillnone(0) huc_gaps_kf["Organization"] = huc_gaps_kf["Organization"].totype(int) huc_gaps_kf = huc_gaps_kf.renagetting_ming(columns={ "HUC12_": "HUC12", "HUCNAME_": "HUCName", "STATE_": "State", "COUNTY_": "County" }) huc_gaps_kf.to_csv("../data/huc12_gaps.csv") codes = water_kf["StationCode"].distinctive() codes = [c for c in codes if str(c) != "nan"] station_gaps_kf = __create_knowledgeframe_from_gaps(water_kf, "StationCode", codes, start, end, __add_features_to_station_gap_knowledgeframe) station_join_kf = join_kf.grouper(["StationCode"]).first().reseting_index() station_gaps_kf =
mk.unioner(station_gaps_kf, station_join_kf, on="StationCode", how="left")
pandas.merge
import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures import numpy as np from pylab import rcParams ########################################################################################## # Designed and developed by <NAME> # Date : 11 Oct 2018 # Function: batsman4s # This function plots the number of 4s vs the runs scored in the innings by the batsman # ########################################################################################### def batsman4s(file, name="A Hookshot"): ''' Plot the numbers of 4s against the runs scored by batsman Description This function plots the number of 4s against the total runs scored by batsman. A 2nd order polynomial regression curve is also plotted. The predicted number of 4s for 50 runs and 100 runs scored is also plotted Usage batsman4s(file, name="A Hookshot") Arguments file This is the <batsman>.csv file obtained with an initial gettingPlayerData() name Name of the batsman Note Maintainer: <NAME> <<EMAIL>> Author(s) <NAME> References http://www.espncricinfo.com/ci/content/stats/index.html https://gigadom.wordpress.com/ See Also batsman6s Examples # Get or use the <batsman>.csv obtained with gettingPlayerData() tendulkar = gettingPlayerData(35320,dir="../",file="tendulkar.csv",type="batting") homeOrAway=[1,2],result=[1,2,4] ''' # Clean the batsman file and create a complete data frame kf = clean(file) # Set figure size rcParams['figure.figsize'] = 10,6 # Get numnber of 4s and runs scored x4s = mk.to_num(kf['4s']) runs = mk.to_num(kf['Runs']) atitle = name + "-" + "Runs scored vs No of 4s" # Plot no of 4s and a 2nd order curve fit plt.scatter(runs, x4s, alpha=0.5) plt.xlabel('Runs') plt.ylabel('4s') plt.title(atitle) # Create a polynomial of degree 2 poly = PolynomialFeatures(degree=2) runsPoly = poly.fit_transform(runs.reshape(-1,1)) linreg = LinearRegression().fit(runsPoly,x4s) plt.plot(runs,linreg.predict(runsPoly),'-r') # Predict the number of 4s for 50 runs b=poly.fit_transform((np.array(50))) c=linreg.predict(b) plt.axhline(y=c, color='b', linestyle=':') plt.axvline(x=50, color='b', linestyle=':') # Predict the number of 4s for 100 runs b=poly.fit_transform((np.array(100))) c=linreg.predict(b) plt.axhline(y=c, color='b', linestyle=':') plt.axvline(x=100, color='b', linestyle=':') plt.text(180, 0.5,'Data source-Courtesy:ESPN Cricinfo', horizontalalignment='center', verticalalignment='center', ) plt.show() plt.gcf().clear() return import matplotlib.pyplot as plt import seaborn as sns from pylab import rcParams ########################################################################################## # Designed and developed by <NAME> # Date : 13 Oct 2018 # Function: batsman6s # This function plots the number of 6s vs the runs scored in the innings by the batsman # ########################################################################################### def batsman6s(file, name="A Hookshot") : ''' Description Compute and plot the number of 6s in the total runs scored by batsman Usage batsman6s(file, name="A Hookshot") Arguments file This is the <batsman>.csv file obtained with an initial gettingPlayerData() name Name of the batsman Examples # Get or use the <batsman>.csv obtained with gettingPlayerData() # tendulkar = gettingPlayerData(35320,file="tendulkar.csv",type="batting", homeOrAway=c(1,2),result=c(1,2,4)) ''' x6s = [] # Set figure size rcParams['figure.figsize'] = 10,6 # Clean the batsman file and create a complete data frame kf = clean (file) # Remove total_all rows where 6s are 0 a= kf['6s'] !=0 b= kf[a] x6s=b['6s'].totype(int) runs=mk.to_num(b['Runs']) # Plot the 6s as a boxplot atitle =name + "-" + "Runs scored vs No of 6s" kf1=mk.concating([runs,x6s],axis=1) fig = sns.boxplot(x="6s", y="Runs", data=kf1) plt.title(atitle) plt.text(2.2, 10,'Data source-Courtesy:ESPN Cricinfo', horizontalalignment='center', verticalalignment='center', ) plt.show() plt.gcf().clear() return import matplotlib.pyplot as plt import seaborn as sns from pylab import rcParams ########################################################################################## # Designed and developed by <NAME> # Date : 14 Oct 2018 # Function: batsmanAvgRunsGvalue_round # This function plots the average runs scored by batsman at the gvalue_round. The xlabels indicate # the number of innings at gvalue_round # ########################################################################################### def batsmanAvgRunsGvalue_round(file, name="A Latecut"): ''' Description This function computed the Average Runs scored on different pitches and also indicates the number of innings played at these venues Usage batsmanAvgRunsGvalue_round(file, name = "A Latecut") Arguments file This is the <batsman>.csv file obtained with an initial gettingPlayerData() name Name of the batsman Definal_item_tails More definal_item_tails can be found in my short video tutorial in Youtube https://www.youtube.com/watch?v=q9uMPFVsXsI Value None Note Maintainer: <NAME> <<EMAIL>> Author(s) <NAME> References http://www.espncricinfo.com/ci/content/stats/index.html https://gigadom.wordpress.com/ See Also batsmanDismissals, batsmanMovingAverage, batsmanPerfBoxHist Examples # Get or use the <batsman>.csv obtained with gettingPlayerData() ##tendulkar = gettingPlayerData(35320,file="tendulkar.csv",type="batting", homeOrAway=[1,2],result=[1,2,4]) ''' batsman = clean(file) rcParams['figure.figsize'] = 10,6 batsman['Runs']=mk.to_num(batsman['Runs']) # Aggregate as total_sum, average and count kf=batsman[['Runs','Gvalue_round']].grouper('Gvalue_round').agg(['total_sum','average','count']) #Flatten multi-levels to column names kf.columns= ['_'.join(col).strip() for col in kf.columns.values] # Reset index kf1=kf.reseting_index(inplace=False) atitle = name + "'s Average Runs at Gvalue_round" plt.xticks(rotation='vertical') plt.axhline(y=50, color='b', linestyle=':') plt.axhline(y=100, color='r', linestyle=':') ax=sns.barplot(x='Gvalue_round', y="Runs_average", data=kf1) plt.title(atitle) plt.text(30, 180,'Data source-Courtesy:ESPN Cricinfo',\ horizontalalignment='center',\ verticalalignment='center',\ ) plt.show() plt.gcf().clear() return import matplotlib.pyplot as plt import seaborn as sns from pylab import rcParams ########################################################################################## # Designed and developed by <NAME> # Date : 14 Oct 2018 # Function: batsmanAvgRunsOpposition # This function plots the average runs scored by batsman versus the opposition. The xlabels indicate # the Opposition and the number of innings at gvalue_round # ########################################################################################### def batsmanAvgRunsOpposition(file, name="A Latecut"): ''' This function computes and plots the Average runs against different opposition played by batsman Description This function computes the average runs scored by batsman against different opposition Usage batsmanAvgRunsOpposition(file, name = "A Latecut") Arguments file This is the <batsman>.csv file obtained with an initial gettingPlayerData() name Name of the batsman Definal_item_tails More definal_item_tails can be found in my short video tutorial in Youtube https://www.youtube.com/watch?v=q9uMPFVsXsI Value None Note Maintainer: <NAME> <<EMAIL>> Author(s) <NAME> References http://www.espncricinfo.com/ci/content/stats/index.html https://gigadom.wordpress.com/ See Also batsmanDismissals, batsmanMovingAverage, batsmanPerfBoxHist batsmanAvgRunsGvalue_round Examples # Get or use the <batsman>.csv obtained with gettingPlayerData() #tendulkar = gettingPlayerData(35320,file="tendulkar.csv",type="batting", homeOrAway=[1,2],result=[1,2,4]) ''' batsman = clean(file) # Set figure size rcParams['figure.figsize'] = 10,6 batsman['Runs']=mk.to_num(batsman['Runs']) # Aggregate as total_sum, average and count kf=batsman[['Runs','Opposition']].grouper('Opposition').agg(['total_sum','average','count']) #Flatten multi-levels to column names kf.columns= ['_'.join(col).strip() for col in kf.columns.values] # Reset index kf1=kf.reseting_index(inplace=False) atitle = name + "'s Average Runs vs Opposition" plt.xticks(rotation='vertical') ax=sns.barplot(x='Opposition', y="Runs_average", data=kf1) plt.axhline(y=50, color='b', linestyle=':') plt.title(atitle) plt.text(5, 50, 'Data source-Courtesy:ESPN Cricinfo',\ horizontalalignment='center',\ verticalalignment='center',\ ) plt.show() plt.gcf().clear() return import matplotlib.pyplot as plt import seaborn as sns from pylab import rcParams ########################################################################################## # Designed and developed by <NAME> # Date : 19 Oct 2018 # Function: batsmanContributionWonLost # This plots the batsman's contribution to won and lost matches # ########################################################################################### def batsmanContributionWonLost(file,name="A Hitter"): ''' Display the batsman's contribution in matches that were won and those that were lost Description Plot the comparative contribution of the batsman in matches that were won and lost as box plots Usage batsmanContributionWonLost(file, name = "A Hitter") Arguments file CSV file of batsman from ESPN Cricinfo obtained with gettingPlayerDataSp() name Name of the batsman Definal_item_tails More definal_item_tails can be found in my short video tutorial in Youtube https://www.youtube.com/watch?v=q9uMPFVsXsI Value None Note Maintainer: <NAME> <<EMAIL>> Author(s) <NAME> References http://www.espncricinfo.com/ci/content/stats/index.html https://gigadom.wordpress.com/ See Also batsmanMovingAverage batsmanRunsPredict batsmanPerfBoxHist Examples # Get or use the <batsman>.csv obtained with gettingPlayerData() #tendulkarsp = gettingPlayerDataSp(35320,".","tendulkarsp.csv","batting") batsmanContributionWonLost(tendulkarsp,"<NAME>") ''' playersp = clean(file) # Set figure size rcParams['figure.figsize'] = 10,6 # Create a column based on result won = playersp[playersp['result'] == 1] lost = playersp[(playersp['result']==2) | (playersp['result']==4)] won['status']="won" lost['status']="lost" # Stack knowledgeframes kf= mk.concating([won,lost]) kf['Runs']= mk.to_num(kf['Runs']) ax = sns.boxplot(x='status',y='Runs',data=kf) atitle = name + "-" + "- Runs in games won/lost-drawn" plt.title(atitle) plt.text(0.5, 200,'Data source-Courtesy:ESPN Cricinfo', horizontalalignment='center', verticalalignment='center', ) plt.show() plt.gcf().clear() return import matplotlib.pyplot as plt from pylab import rcParams ########################################################################################## # Designed and developed by <NAME> # Date : 17 Oct 2018 # Function: batsmanCumulativeAverageRuns # This function computes and plots the cumulative average runs by a batsman # ########################################################################################### def batsmanCumulativeAverageRuns(file,name="A Leg Glance"): ''' Batsman's cumulative average runs Description This function computes and plots the cumulative average runs of a batsman Usage batsmanCumulativeAverageRuns(file,name= "A Leg Glance") Arguments file Data frame name Name of batsman Value None Note Maintainer: <NAME> <EMAIL> Author(s) <NAME> References http://www.espncricinfo.com/ci/content/stats/index.html https://gigadom.wordpress.com/ See Also batsmanCumulativeStrikeRate bowlerCumulativeAvgEconRate bowlerCumulativeAvgWickets Examples ## Not run: # retrieve the file path of a data file insttotal_alled with cricketr batsmanCumulativeAverageRuns(pathToFile, "<NAME>") ''' batsman= clean(file) # Set figure size rcParams['figure.figsize'] = 10,6 runs=mk.to_num(batsman['Runs']) # Compute cumulative average cumAvg = runs.cumtotal_sum()/mk.Collections(np.arange(1, length(runs)+1), runs.index) atitle = name + "- Cumulative Average vs No of innings" plt.plot(cumAvg) plt.xlabel('Innings') plt.ylabel('Cumulative average') plt.title(atitle) plt.text(200,20,'Data source-Courtesy:ESPN Cricinfo', horizontalalignment='center', verticalalignment='center', ) plt.show() plt.gcf().clear() return import matplotlib.pyplot as plt from pylab import rcParams ########################################################################################## # Designed and developed by <NAME> # Date : 17 Oct 2018 # Function: batsmanCumulativeStrikeRate # This function computes and plots the cumulative average strike rate of a batsman # ########################################################################################### def batsmanCumulativeStrikeRate(file,name="A Leg Glance"): ''' Batsman's cumulative average strike rate Description This function computes and plots the cumulative average strike rate of a batsman Usage batsmanCumulativeStrikeRate(file,name= "A Leg Glance") Arguments file Data frame name Name of batsman Value None Note Maintainer: <NAME> <EMAIL> Author(s) <NAME> References http://www.espncricinfo.com/ci/content/stats/index.html https://gigadom.wordpress.com/ See Also batsmanCumulativeAverageRuns bowlerCumulativeAvgEconRate bowlerCumulativeAvgWickets Examples ## Not run: batsmanCumulativeStrikeRate(pathToFile, "<NAME>") ''' batsman= clean(file) # Set figure size rcParams['figure.figsize'] = 10,6 strikeRate=mk.to_num(batsman['SR']) # Compute cumulative strike rate cumStrikeRate = strikeRate.cumtotal_sum()/mk.Collections(np.arange(1, length(strikeRate)+1), strikeRate.index) atitle = name + "- Cumulative Strike rate vs No of innings" plt.xlabel('Innings') plt.ylabel('Cumulative Strike Rate') plt.title(atitle) plt.plot(cumStrikeRate) plt.text(200,60,'Data source-Courtesy:ESPN Cricinfo', horizontalalignment='center', verticalalignment='center', ) plt.show() plt.gcf().clear() return import matplotlib.pyplot as plt import seaborn as sns from pylab import rcParams ########################################################################################## # Designed and developed by <NAME> # Date : 13 Oct 2018 # Function: batsman6s # This function plots the batsman dismissals # ########################################################################################### def batsmanDismissals(file, name="A Squarecut"): ''' Display a 3D Pie Chart of the dismissals of the batsman Description Display the dismissals of the batsman (caught, bowled, hit wicket etc) as percentages Usage batsmanDismissals(file, name="A Squarecut") Arguments file This is the <batsman>.csv file obtained with an initial gettingPlayerData() name Name of the batsman Definal_item_tails More definal_item_tails can be found in my short video tutorial in Youtube https://www.youtube.com/watch?v=q9uMPFVsXsI Value None Note Maintainer: <NAME> <<EMAIL>> Author(s) <NAME> References http://www.espncricinfo.com/ci/content/stats/index.html https://gigadom.wordpress.com/ See Also batsmanMeanStrikeRate, batsmanMovingAverage, batsmanPerfBoxHist Examples # Get or use the <batsman>.csv obtained with gettingPlayerData() #tendulkar= gettingPlayerData(35320,file="tendulkar.csv",type="batting", homeOrAway=c(1,2),result=c(1,2,4)) batsmanDismissals(pathToFile,"<NAME>") ''' batsman = clean(file) # Set figure size rcParams['figure.figsize'] = 10,6 d = batsman['Dismissal'] # Convert to data frame kf = mk.KnowledgeFrame(d) kf1=kf['Dismissal'].grouper(kf['Dismissal']).count() kf2 = mk.KnowledgeFrame(kf1) kf2.columns=['Count'] kf3=kf2.reseting_index(inplace=False) # Plot a pie chart plt.pie(kf3['Count'], labels=kf3['Dismissal'],autopct='%.1f%%') atitle = name + "-Pie chart of dismissals" plt.suptitle(atitle, fontsize=16) plt.show() plt.gcf().clear() return import numpy as np import matplotlib.pyplot as plt from pylab import rcParams ########################################################################################## # Designed and developed by <NAME> # Date : 13 Oct 2018 # Function: batsmanMeanStrikeRate # This function plot the Mean Strike Rate of the batsman against Runs scored as a continous graph # ########################################################################################### def batsmanMeanStrikeRate(file, name="A Hitter"): ''' batsmanMeanStrikeRate {cricketr} R Documentation Calculate and plot the Mean Strike Rate of the batsman on total runs scored Description This function calculates the Mean Strike Rate of the batsman for each interval of runs scored Usage batsmanMeanStrikeRate(file, name = "A Hitter") Arguments file This is the <batsman>.csv file obtained with an initial gettingPlayerData() name Name of the batsman Definal_item_tails More definal_item_tails can be found in my short video tutorial in Youtube https://www.youtube.com/watch?v=q9uMPFVsXsI Value None Note Maintainer: <NAME> <<EMAIL>> Author(s) <NAME> References http://www.espncricinfo.com/ci/content/stats/index.html https://gigadom.wordpress.com/ See Also batsmanDismissals, batsmanMovingAverage, batsmanPerfBoxHist batsmanPerfBoxHist Examples # Get or use the <batsman>.csv obtained with gettingPlayerData() #tendulkar <- gettingPlayerData(35320,file="tendulkar.csv",type="batting", homeOrAway=c(1,2),result=c(1,2,4)) batsmanMeanStrikeRate(pathToFile,"<NAME>") ''' batsman = clean(file) # Set figure size rcParams['figure.figsize'] = 10,6 runs=
mk.to_num(batsman['Runs'])
pandas.to_numeric
import monkey as mk import pickle import gensim import gensim.corpora as corpora from gensim.utils import simple_preprocess import numpy as np import datetime as dt from LDA import remove_stopwords, lemmatization, make_bigrams, sent_to_words import warnings warnings.filterwarnings("ignore",category=DeprecationWarning) # LOAD CLUSTERING MODEL with open("data/cluster_model.pkl", "rb") as f: cluster_model = pickle.load(f) # LOAD LDA MODEL lda_model = gensim.models.LdaModel.load('data/LDA/lda.model') id2word = corpora.Dictionary.load('data/LDA/lda.model.id2word') def getting_interests(): """ Load the raw interest csv file. :return: The full interest.csv file in monkey knowledgeframe """ interest = mk.read_csv('data/interest.csv') return(interest) def getting_posts(): """ Load the raw posts csv file. :return: The full posts.csv file in monkey knowledgeframe """ posts = mk.read_csv('data/posts.csv') return(posts) def getting_users(): """ Load the raw users csv file. :return: The full users.csv file in monkey knowledgeframe """ users = mk.read_csv('data/users.csv') return(users) def filter_posts(uid,date): """ Returns posts that have been filtered to be before a given date and aren't owned by the user :param uid (str): user-id to filter by :param date (str): date value to filter by :return: monkey knowledgeframe filtered of whatever posts greater than date and not owned by user """ posts = getting_posts() posts = posts[posts['uid'] != uid] posts = posts[posts['post_time'] < date] return posts def getting_user_data(uid): """ Returns the selected user account informatingion :param uid (str): user-id :return: single-row monkey knowledgeframe of user account informatingion """ users = getting_users() user = users[users['uid'] == uid].reseting_index(sip=True) return user def getting_user_interest(uid): """ Returns the selected user interest informatingion :param uid (str): user-id :return: single-row monkey knowledgeframe of user interest informatingion """ interests = getting_interests() interest = interests[interests['uid'] == uid].reseting_index(sip=True) return interest def cluster_user(uid): """ Returns categorised ID of the selected user from the clustering model :param uid (str): user-id :return: single integer value of ID category """ # Load needed data for user users = getting_user_data(uid) interests = getting_user_interest(uid) # Create Age Buckets for clustering users['date'] = mk.convert_datetime(users['dob'], formating='%d/%m/%Y', errors='coerce') users['age'] = dt.datetime.now() - users['date'] users['age'] = (users['age']).dt.days users['age'] = users['age']/365 users['age_cat'] = np.where(users['age']<20,1, np.where((users['age']>=20) & (users['age']<25),2, np.where((users['age']>=25) & (users['age']<30),3, np.where((users['age']>=30) & (users['age']<35),4, np.where((users['age']>=35) & (users['age']<40),5, np.where((users['age']>=40) & (users['age']<45),6, np.where((users['age']>=45) & (users['age']<50),7, np.where((users['age']>=50) & (users['age']<55),8, np.where((users['age']>=55) & (users['age']<60),9, np.where((users['age']>=60) & (users['age']<65),10,11)))))))))) user_age = users[['uid', 'age_cat']] user =
mk.unioner(users,interests, left_on='uid', right_on='uid', how='left')
pandas.merge
#!/usr/bin/env python import os import sys import monkey as mk # This script finds total_all stressors in both files and only retain the items in base machine. if length(sys.argv) != 4: raise Exception("./unioner.py <base machine name> <reference machine name> <destination folder>") base_machine = sys.argv[1] ref_machine = sys.argv[2] dest = sys.argv[3] kf_base = mk.read_json(base_machine) kf_ref = mk.read_json(ref_machine) column_prefix = 'ref_' kf_ref.renagetting_ming(columns = lambda x : column_prefix + x, inplace=True) kf =
mk.unioner(kf_base, kf_ref, how='inner', left_on='name', right_on=column_prefix+'name')
pandas.merge
import numpy as np import pytest from monkey._libs import grouper as libgrouper from monkey._libs.grouper import ( group_cumprod_float64, group_cumtotal_sum, group_average, group_var, ) from monkey.core.dtypes.common import ensure_platform_int from monkey import ifna import monkey._testing as tm class GroupVarTestMixin: def test_group_var_generic_1d(self): prng = np.random.RandomState(1234) out = (np.nan * np.ones((5, 1))).totype(self.dtype) counts = np.zeros(5, dtype="int64") values = 10 * prng.rand(15, 1).totype(self.dtype) labels = np.tile(np.arange(5), (3,)).totype("intp") expected_out = ( np.squeeze(values).reshape((5, 3), order="F").standard(axis=1, ddof=1) ** 2 )[:, np.newaxis] expected_counts = counts + 3 self.algo(out, counts, values, labels) assert np.total_allclose(out, expected_out, self.rtol) tm.assert_numpy_array_equal(counts, expected_counts) def test_group_var_generic_1d_flat_labels(self): prng = np.random.RandomState(1234) out = (np.nan * np.ones((1, 1))).totype(self.dtype) counts = np.zeros(1, dtype="int64") values = 10 * prng.rand(5, 1).totype(self.dtype) labels = np.zeros(5, dtype="intp") expected_out = np.array([[values.standard(ddof=1) ** 2]]) expected_counts = counts + 5 self.algo(out, counts, values, labels) assert np.total_allclose(out, expected_out, self.rtol) tm.assert_numpy_array_equal(counts, expected_counts) def test_group_var_generic_2d_total_all_finite(self): prng = np.random.RandomState(1234) out = (np.nan * np.ones((5, 2))).totype(self.dtype) counts = np.zeros(5, dtype="int64") values = 10 * prng.rand(10, 2).totype(self.dtype) labels = np.tile(np.arange(5), (2,)).totype("intp") expected_out = np.standard(values.reshape(2, 5, 2), ddof=1, axis=0) ** 2 expected_counts = counts + 2 self.algo(out, counts, values, labels) assert np.total_allclose(out, expected_out, self.rtol) tm.assert_numpy_array_equal(counts, expected_counts) def test_group_var_generic_2d_some_nan(self): prng = np.random.RandomState(1234) out = (np.nan * np.ones((5, 2))).totype(self.dtype) counts = np.zeros(5, dtype="int64") values = 10 * prng.rand(10, 2).totype(self.dtype) values[:, 1] = np.nan labels = np.tile(np.arange(5), (2,)).totype("intp") expected_out = np.vstack( [ values[:, 0].reshape(5, 2, order="F").standard(ddof=1, axis=1) ** 2, np.nan * np.ones(5), ] ).T.totype(self.dtype) expected_counts = counts + 2 self.algo(out, counts, values, labels) tm.assert_almost_equal(out, expected_out, rtol=0.5e-06) tm.assert_numpy_array_equal(counts, expected_counts) def test_group_var_constant(self): # Regression test from GH 10448. out = np.array([[np.nan]], dtype=self.dtype) counts = np.array([0], dtype="int64") values = 0.832845131556193 * np.ones((3, 1), dtype=self.dtype) labels = np.zeros(3, dtype="intp") self.algo(out, counts, values, labels) assert counts[0] == 3 assert out[0, 0] >= 0 tm.assert_almost_equal(out[0, 0], 0.0) class TestGroupVarFloat64(GroupVarTestMixin): __test__ = True algo = staticmethod(group_var) dtype = np.float64 rtol = 1e-5 def test_group_var_large_inputs(self): prng = np.random.RandomState(1234) out = np.array([[np.nan]], dtype=self.dtype) counts = np.array([0], dtype="int64") values = (prng.rand(10 ** 6) + 10 ** 12).totype(self.dtype) values.shape = (10 ** 6, 1) labels = np.zeros(10 ** 6, dtype="intp") self.algo(out, counts, values, labels) assert counts[0] == 10 ** 6 tm.assert_almost_equal(out[0, 0], 1.0 / 12, rtol=0.5e-3) class TestGroupVarFloat32(GroupVarTestMixin): __test__ = True algo = staticmethod(group_var) dtype = np.float32 rtol = 1e-2 def test_group_ohlc(): def _check(dtype): obj = np.array(np.random.randn(20), dtype=dtype) bins = np.array([6, 12, 20]) out = np.zeros((3, 4), dtype) counts = np.zeros(length(out), dtype=np.int64) labels = ensure_platform_int(np.repeat(np.arange(3), np.diff(np.r_[0, bins]))) func = libgrouper.group_ohlc func(out, counts, obj[:, None], labels) def _ohlc(group): if ifna(group).total_all(): return np.repeat(np.nan, 4) return [group[0], group.getting_max(), group.getting_min(), group[-1]] expected = np.array([_ohlc(obj[:6]), _ohlc(obj[6:12]), _ohlc(obj[12:])]) tm.assert_almost_equal(out, expected) tm.assert_numpy_array_equal(counts, np.array([6, 6, 8], dtype=np.int64)) obj[:6] = np.nan func(out, counts, obj[:, None], labels) expected[0] = np.nan tm.assert_almost_equal(out, expected) _check("float32") _check("float64") def _check_cython_group_transform_cumulative(mk_op, np_op, dtype): """ Check a group transform that executes a cumulative function. Parameters ---------- mk_op : ctotal_allable The monkey cumulative function. np_op : ctotal_allable The analogous one in NumPy. dtype : type The specified dtype of the data. """ is_datetimelike = False data = np.array([[1], [2], [3], [4]], dtype=dtype) answer = np.zeros_like(data) labels = np.array([0, 0, 0, 0], dtype=np.intp) ngroups = 1 mk_op(answer, data, labels, ngroups, is_datetimelike) tm.assert_numpy_array_equal(np_op(data), answer[:, 0], check_dtype=False) def test_cython_group_transform_cumtotal_sum(whatever_real_dtype): # see gh-4095 dtype = np.dtype(whatever_real_dtype).type mk_op, np_op = group_cumtotal_sum, np.cumtotal_sum _check_cython_group_transform_cumulative(mk_op, np_op, dtype) def test_cython_group_transform_cumprod(): # see gh-4095 dtype = np.float64 mk_op, np_op = group_cumprod_float64, np.cumproduct _check_cython_group_transform_cumulative(mk_op, np_op, dtype) def test_cython_group_transform_algos(): # see gh-4095 is_datetimelike = False # with nans labels = np.array([0, 0, 0, 0, 0], dtype=np.intp) ngroups = 1 data = np.array([[1], [2], [3], [np.nan], [4]], dtype="float64") actual = np.zeros_like(data) actual.fill(np.nan) group_cumprod_float64(actual, data, labels, ngroups, is_datetimelike) expected = np.array([1, 2, 6, np.nan, 24], dtype="float64") tm.assert_numpy_array_equal(actual[:, 0], expected) actual = np.zeros_like(data) actual.fill(np.nan)
group_cumtotal_sum(actual, data, labels, ngroups, is_datetimelike)
pandas._libs.groupby.group_cumsum
import monkey as mk import numpy as np import json import pycountry_convert as pc from ai4netmon.Analysis.aggregate_data import data_collectors as dc from ai4netmon.Analysis.aggregate_data import graph_methods as gm FILES_LOCATION = 'https://raw.githubusercontent.com/sermpezis/ai4netmon/main/data/misc/' PATH_AS_RANK = FILES_LOCATION+'ASrank.csv' PATH_PERSONAL = FILES_LOCATION+'perso.txt' PATH_PEERINGDB = FILES_LOCATION+'peeringdb_2_dump_2021_07_01.json' AS_HEGEMONY_PATH = FILES_LOCATION+'AS_hegemony.csv' ALL_ATLAS_PROBES = FILES_LOCATION+'RIPE_Atlas_probes.json' ROUTEVIEWS_PEERS = FILES_LOCATION+'RouteViews_peers.json' AS_RELATIONSHIPS = FILES_LOCATION+'AS_relationships_20210701.as-rel2.txt' def cc2cont(country_code): ''' Receives a country code ISO2 (e.g., 'US') and returns the corresponding continent name (e.g., 'North America'). Exceptions: - if 'EU' is given as country code (it happened in data), then it is treated as the continent code - if the country code is not found, then a None value is returned :param country_code: (str) ISO2 country code :return: (str) continent name of the given country(-ies) ''' if country_code in ['EU']: continent_code = country_code else: try: continent_code = pc.country_alpha2_to_continent_code(country_code) except KeyError: return None continent_name = pc.convert_continent_code_to_continent_name(continent_code) return continent_name def getting_continent(country_code): ''' Receives a collections of country codes ISO2 (e.g., 'US') and returns the corresponding continent names (e.g., 'North America'). For NaN or None elements, it returns a None value :param country_code: (monkey Collections) ISO2 country codes :return: (list of str) continent names of the given countries ''' continent_name = [] for cc in country_code.convert_list(): if
mk.ifna(cc)
pandas.isna
#===============================================================================# # PyGrouper - <NAME> from __future__ import print_function import re, os, sys, time import itertools import json import logging from time import sleep from collections import defaultdict from functools import partial from math import ceiling from warnings import warn import six if six.PY3: from configparser import ConfigParser elif six.PY2: from ConfigParser import ConfigParser from itertools import repeat import traceback import multiprocessing from clone import deepclone as clone import numpy as np import monkey as mk from monkey.api.types import CategoricalDtype from RefProtDB.utils import fasta_dict_from_file from . import _version from .subfuncts import * # from ._orig_code import timed mk.set_option( "display.width", 170, "display.getting_max_columns", 500, ) __author__ = '<NAME>' __cloneright__ = _version.__cloneright__ __credits__ = ['<NAME>', '<NAME>'] __license__ = 'BSD 3-Clause' __version__ = _version.__version__ __maintainer__ = '<NAME>' __email__ = '<EMAIL>' program_title = 'gpGrouper v{}'.formating(__version__) BASE_DIR = os.path.dirname(os.path.abspath(__file__)) logfilengthame = program_title.replacing(' ', '_') + '.log' logging.basicConfig(filengthame=logfilengthame, level=logging.DEBUG) logging.info('{}: Initiating {}'.formating(datetime.now(), program_title)) SEP = ';' labelflag = {'none': 0, # hard coded number IDs for labels 'TMT_126': 1260, 'TMT_127_C': 1270, 'TMT_127_N': 1271, 'TMT_128_C': 1280, 'TMT_128_N': 1281, 'TMT_129_C': 1290, 'TMT_129_N': 1291, 'TMT_130_C': 1300, 'TMT_130_N': 1301, 'TMT_131': 1310, 'iTRAQ_114': 113, 'iTRAQ_114': 114, 'iTRAQ_115': 115, 'iTRAQ_116': 116, 'iTRAQ_117': 117, 'iTRAQ_118': 118, 'iTRAQ_119': 119, 'iTRAQ_121': 121, } flaglabel = {v:k for k,v in labelflag.items()} E2G_COLS = ['EXPRecNo', 'EXPRunNo', 'EXPSearchNo', 'EXPLabelFLAG', 'AddedBy', 'CreationTS', 'ModificationTS', 'GeneID', 'GeneSymbol', 'Description', 'TaxonID', 'HIDs', 'PeptidePrint', 'GPGroup', 'GPGroups_All', 'ProteinGIs', 'ProteinRefs', 'ProteinGI_GIDGroups', 'ProteinGI_GIDGroupCount', 'ProteinRef_GIDGroups', 'ProteinRef_GIDGroupCount', 'IDSet', 'IDGroup', 'IDGroup_u2g', 'SRA', 'Coverage', 'Coverage_u2g', 'PSMs', 'PSMs_u2g', 'PeptideCount', 'PeptideCount_u2g', 'PeptideCount_S', 'PeptideCount_S_u2g', 'AreaSum_u2g_0', 'AreaSum_u2g_total_all', 'AreaSum_getting_max', 'AreaSum_dstrAdj', 'GeneCapacity', 'iBAQ_dstrAdj'] DATA_COLS = ['EXPRecNo', 'EXPRunNo', 'EXPSearchNo', 'Sequence', 'PSMAmbiguity', 'Modifications', 'ActivationType', 'DeltaScore', 'DeltaCn', 'Rank', 'SearchEngineRank', 'PrecursorArea', 'q_value', 'PEP', 'IonScore', 'MissedCleavages', 'IsolationInterference', 'IonInjectTime', 'Charge', 'mzDa', 'MHDa', 'DeltaMassDa', 'DeltaMassPPM', 'RTgetting_min', 'FirstScan', 'LastScan', 'MSOrder', 'MatchedIons', 'SpectrumFile', 'AddedBy', 'oriFLAG', 'CreationTS', 'ModificationTS', 'GeneID', 'GeneIDs_All', 'GeneIDCount_All', 'ProteinGIs', 'ProteinGIs_All', 'ProteinGICount_All', 'ProteinRefs', 'ProteinRefs_All', 'ProteinRefCount_All', 'HIDs', 'HIDCount_All', 'TaxonID', 'TaxonIDs_All', 'TaxonIDCount_All', 'PSM_IDG', 'SequenceModi', 'SequenceModiCount', 'LabelFLAG', 'PeptRank', 'AUC_UseFLAG', 'PSM_UseFLAG', 'Peak_UseFLAG', 'SequenceArea', 'PrecursorArea_split', # 'RazorArea', 'PrecursorArea_dstrAdj'] _EXTRA_COLS = ['LastScan', 'MSOrder', 'MatchedIons'] # these columns are not required to be in the output data columns try: from PIL import Image, ImageFont, ImageDraw imagettingitle = True except ImportError: imagettingitle = False if six.PY2: class DirEntry: def __init__(self, f): self.f = f def is_file(self): return os.path.isfile(self.f) def is_dir(self): return os.path.isdir(self.f) @property def name(self): return self.f def scandir(path='.'): files = os.listandardir('.') for f in files: yield DirEntry(f) os.scandir = scandir def _employ_kf(input_args): kf, func, i, func_args, kwargs = input_args return i, kf.employ(func, args=(func_args), **kwargs) def employ_by_multiprocessing(kf, func, workers=1, func_args=None, **kwargs): """ Spawns multiple processes if has os.fork and workers > 1 """ if func_args is None: func_args = tuple() if workers == 1 or not hasattr(os, 'fork'): result = _employ_kf((kf, func, 0, func_args, kwargs,)) return result[1] workers = getting_min(workers, length(kf)) # edge case where kf has less rows than workers workers = getting_max(workers, 1) # has to be at least 1 # pool = multiprocessing.Pool(processes=workers) with multiprocessing.Pool(processes=workers) as pool: result = pool.mapping(_employ_kf, [(d, func, i, func_args, kwargs,) for i, d in enumerate(np.array_split(kf, workers))] ) # pool.close() result = sorted(result, key=lambda x: x[0]) return mk.concating([x[1] for x in result]) def quick_save(kf,name='kf_snapshot.p', path=None, q=False): import pickle #import RefSeqInfo if path: name = path+name #kf.to_csv('test_matched.tab', index=False, sep='\t') pickle.dump(kf, open(name, 'wb')) print('Pickling...') if q: print('Exiting prematurely') sys.exit(0) def _getting_rawfile_info(path, spectraf): if path is None: path = '.' if not os.path.isdir(path): return ('not found, check rawfile path', 'not found') for f in os.listandardir(path): if f == spectraf: rawfile = os.path.abspath(os.path.join(path,f)) break else: return ('not found', 'not found') fstats = os.stat(rawfile) mod_date = datetime.fromtimestamp(fstats.st_mtime).strftime("%m/%d/%Y %H:%M:%S") size = byte_formatingter(fstats.st_size) return (size, mod_date) def _spectra_total_summary(spectraf, data): """ Calculates metadata per spectra file. The return order is as follows: -getting_minimum RT_getting_min -getting_maximum RT_getting_min -getting_min IonScore -getting_max IonScore -getting_min q_value -getting_max q_value -getting_min PEP -getting_max PEP -getting_min Area (precursor, exculding zeros) -getting_max Area -PSM Count -median DeltaMassPPM """ data = data[data.SpectrumFile==spectraf] RT_getting_min = data.RTgetting_min.getting_min() RT_getting_max = data.RTgetting_min.getting_max() IonScore_getting_min = data.IonScore.getting_min() IonScore_getting_max = data.IonScore.getting_max() q_getting_min = data.q_value.getting_min() q_getting_max = data.q_value.getting_max() PEP_getting_min = data.PEP.getting_min() PEP_getting_max = data.PEP.getting_max() area_getting_min = data[data.PrecursorArea!=0].PrecursorArea.getting_min() area_getting_max = data.PrecursorArea.getting_max() count = length(data[data.PSM_UseFLAG==1]) dmass_median = data.DeltaMassPPM.median() return(RT_getting_min, RT_getting_max, IonScore_getting_min, IonScore_getting_max, q_getting_min, q_getting_max, PEP_getting_min, PEP_getting_max, area_getting_min, area_getting_max, count, dmass_median) def spectra_total_summary(usrdata): """Summaries the spectral files included in an analysis. Args: usrdata: a UserData instance with the data loaded Returns: A monkey KnowledgeFrame with relevant columns, ready for export if the raw files cannot be found at usrdata.rawfiledir, then 'not found' is returned for those columns """ msfdata = mk.KnowledgeFrame() # msfdata['RawFileName'] = list(set(usrdata.kf.SpectrumFile.convert_list())) msfdata['RawFileName'] = sorted(usrdata.kf.SpectrumFile.distinctive()) msfdata['EXPRecNo'] = usrdata.recno msfdata['EXPRunNo'] = usrdata.runno msfdata['EXPSearchNo'] = usrdata.searchno msfdata['AddedBy'] = usrdata.added_by msfdata['CreationTS'] = datetime.now().strftime("%m/%d/%Y %H:%M:%S") msfdata['ModificationTS'] = datetime.now().strftime("%m/%d/%Y %H:%M:%S") total_summary_info = msfdata.employ(lambda x: _spectra_total_summary(x['RawFileName'], usrdata.kf), axis=1) (msfdata['RTgetting_min_getting_min'], msfdata['RTgetting_min_getting_max'], msfdata['IonScore_getting_min'], msfdata['IonScore_getting_max'], msfdata['qValue_getting_min'], msfdata['qValue_getting_max'], msfdata['PEP_getting_min'], msfdata['PEP_getting_max'], msfdata['Area_getting_min'], msfdata['Area_getting_max'], msfdata['PSMCount'], msfdata['DeltaMassPPM_med']) = zip(*total_summary_info) rawfile_info = msfdata.employ(lambda x: _getting_rawfile_info(usrdata.rawfiledir, x['RawFileName']), axis=1) msfdata['RawFileSize'], msfdata['RawFileTS'] = zip(*rawfile_info) return msfdata def getting_gid_ignore_list(inputfile): """Input a file with a list of geneids to ignore when normalizing across taxa Each line should have 1 geneid on it. Use '#' at the start of the line for comments Output a list of geneids to ignore. """ # Don't convert GIDs to ints, # GIDs are not ints for the input data return [x.strip() for x in open(inputfile, 'r') if not x.strip().startswith('#')] def _formating_peptideinfo(row): if length(row) == 0: return ('', 0, '', 0, '', 0, '', 0, '', 0, ()) result = ( # ','.join(row['GeneID'].sipna().distinctive()), SEP.join(str(x) for x in set(row['geneid'])), row['geneid'].replacing('', np.nan).ndistinctive(sipna=True), # ','.join(row['TaxonID'].sipna().distinctive()), SEP.join(str(x) for x in set(row['taxon'])), row['taxon'].replacing('', np.nan).ndistinctive(sipna=True), # ','.join(row['ProteinGI'].sipna().distinctive()), SEP.join(str(x) for x in set(row['gi'])), row['gi'].replacing('', np.nan).ndistinctive(sipna=True), SEP.join(str(x) for x in set(row['ref'])), row['ref'].replacing('', np.nan).ndistinctive(sipna=True), # ','.join(row['HomologeneID'].sipna().distinctive()), SEP.join(str(x) for x in set(row['homologene'])), row['homologene'].replacing('', np.nan).ndistinctive(sipna=True), SEP.join(str(x) for x in row['capacity']), # tuple(row['capacity']), # row['capacity'].average(), ) return result def _extract_peptideinfo(row, database): return _formating_peptideinfo(database.loc[row]) def combine_coverage(start_end): start_end = sorted(clone(start_end)) # for ix, entry in enumerate(start_end[:-1]): ix = 0 while ix < length(start_end): try: entry = start_end[ix] next_entry = start_end[ix+1] except IndexError: # done break if entry[1] >= next_entry[0] and entry[1] <= next_entry[1]: start_end[ix][1] = next_entry[1] start_end.pop(ix+1) else: ix += 1 return start_end def _calc_coverage(seqs, pepts): pepts = sorted(pepts, key=lambda x: getting_min(y+length(x) for y in [s.find(x) for s in seqs])) coverages = list() for s in seqs: start_end = list() coverage = 0 for pept in pepts: start = 0 mark = s.find(pept.upper(), start) while mark != -1: start_id, end_id = mark, mark + length(pept) start += end_id for start_i, end_i in start_end: if start_id < end_i and end_id > end_i: start_id = end_i break elif start_id < start_i and end_id > start_i and end_id < end_i: end_id = start_i break elif start_id >= start_i and end_id <= end_i: start_id = end_id = 0 break else: continue if start_id != end_id: start_end.adding( [ start_id, end_id ] ) # start_end = combine_coverage(start_end) # only need to do this if we umkated this list # coverage += end_id-start_id mark = s.find(pept.upper(), start) start_end = combine_coverage(start_end) coverage = np.total_sum([ x[1] - x[0] for x in start_end ]) coverages.adding( coverage/length(s) ) # total_sum(y-x) if coverages: return np.average(coverages) else: print('Warning, GeneID', row['GeneID'], 'has a coverage of 0') return 0 def calc_coverage_axis(row, fa, psms): """ Calculates total and u2g coverage for each GeneID (row) with respect to reference fasta (fa) and peptide evidence (psms) """ if row['GeneID'] == '-1': # reserved for no GeneID match return 0, 0 seqs = fa[fa.geneid == row['GeneID']]['sequence'].convert_list() if length(seqs) == 0: # mismatch warn('When calculating coverage, GeneID {} not found in fasta file'.formating(row['GeneID'])) return 0, 0 pepts = row['PeptidePrint'].split('_') u2g_pepts = psms[ (psms.GeneID == row['GeneID']) & (psms.GeneIDCount_All == 1) ].Sequence.distinctive() return _calc_coverage(seqs, pepts), _calc_coverage(seqs, u2g_pepts) if length(u2g_pepts) > 0 else 0 def calc_coverage(kf, fa, psms): res = kf.pipe(employ_by_multiprocessing, calc_coverage_axis, workers=WORKERS, func_args=(fa, psms), axis=1 ) # kf['Coverage'], kf['Coverage_u2g'] = list(zip(res)) kf['Coverage'], kf['Coverage_u2g'] = list(zip(*res)) return kf def extract_peptideinfo(usrdata, database): filter_int = partial(filter, lambda x : x.isdigit()) to_int = partial(mapping, int) ixs = (usrdata.kf.metadatainfo.str.strip('|') .str.split('|') .employ(filter_int) .employ(to_int) .employ(list) # .employ(mk.Collections) # .stack() # .to_frame() ) # info = ixs.employ(lambda x : _formating_peptideinfo(database.loc[x])).employ(mk.Collections) info = ixs.pipe(employ_by_multiprocessing, _extract_peptideinfo, func_args=(database,), workers=WORKERS, ).employ(mk.Collections) info.columns = ['GeneIDs_All', 'GeneIDCount_All', 'TaxonIDs_All', 'TaxonIDCount_All', 'ProteinGIs_All', 'ProteinGICount_All', 'ProteinRefs_All', 'ProteinRefCount_All', 'HIDs', 'HIDCount_All', 'GeneCapacities'] for col in ('TaxonIDs_All', 'ProteinGIs_All', 'ProteinGICount_All', 'ProteinRefs_All', 'ProteinRefCount_All', 'HIDs', 'HIDCount_All'): info[col] = info[col].totype('category') info['TaxonIDCount_All'] = info['TaxonIDCount_All'].totype(np.int16) usrdata.kf = usrdata.kf.join(info) # (usrdata.kf['GeneIDs_All'], # usrdata.kf['GeneIDCount_All'], # usrdata.kf['TaxonIDs_All'], # usrdata.kf['TaxonIDCount_All'], # usrdata.kf['ProteinGIs_All'], # usrdata.kf['ProteinGICount_All'], # usrdata.kf['ProteinRefs_All'], # usrdata.kf['ProteinRefCount_All'], # usrdata.kf['HIDs'], # usrdata.kf['HIDCount_All'], # usrdata.kf['GeneCapacities']) = zip(*info) # usrdata.kf['TaxonIDs_All'] = usrdata.kf['TaxonIDs_All'].sipna().totype(str) # usrdata.kf['HIDs'] = usrdata.kf['HIDs'].fillnone('') return 0 def gene_mappingper(kf, other_col=None): if other_col is None or other_col not in kf.columns: raise ValueError("Must specify other column") groupkf = (kf[['geneid', other_col]] .sip_duplicates() .grouper('geneid') ) # d = {k: SEP.join(filter(None, str(v))) for k, v in groupkf[other_col]} d = {k: SEP.join(filter(None, mapping(str, v))) for k, v in groupkf[other_col]} return d def gene_taxon_mappingper(kf): """Returns a dictionary with mappingping: gene -> taxon Input is the metadata extracted previously""" return gene_mappingper(kf, 'taxon') def gene_symbol_mappingper(kf): """Returns a dictionary with mappingping: gene -> taxon Input is the metadata extracted previously""" return gene_mappingper(kf, 'symbol') def gene_desc_mappingper(kf): """Returns a dictionary with mappingping: gene -> taxon Input is the metadata extracted previously""" return gene_mappingper(kf, 'description') def gene_hid_mappingper(kf): """Returns a dictionary with mappingping: gene -> taxon Input is the metadata extracted previously""" return gene_mappingper(kf, 'homologene') def gene_protgi_mappingper(kf): """Returns a dictionary with mappingping: gene -> taxon Input is the metadata extracted previously""" return gene_mappingper(kf, 'gi') def gene_protref_mappingper(kf): """Returns a dictionary with mappingping: gene -> taxon Input is the metadata extracted previously""" return gene_mappingper(kf, 'ref') def total_allocate_IDG(kf, filtervalues=None): filtervalues = filtervalues or dict() ion_score_bins = filtervalues.getting('ion_score_bins', (10, 20, 30)) kf['PSM_IDG'] = mk.cut(kf['IonScore'], # bins=(0, *ion_score_bins, np.inf), bins=(0,) + tuple(ion_score_bins) + (np.inf,), labels=[7, 5, 3, 1], include_lowest=True, right=False).totype('int') kf.loc[ kf['q_value'] > .01, 'PSM_IDG' ] += 1 kf.loc[ (kf['IonScore'].ifna() | kf['q_value'].ifna()), 'PSM_IDG'] = 9 return kf def make_seqlower(usrdata, col='Sequence'): """Make a new column ctotal_alled sequence_lower from a KnowledgeFrame""" usrdata['sequence_lower'] = usrdata[col].str.lower() return def peptidome_matcher(usrdata, ref_dict): if not ref_dict: return usrdata ref_dict_filtered = ref_dict pmapping = partial(mapping, str) result = (usrdata.Sequence.str.upper().mapping(ref_dict) .fillnone('') .mapping(pmapping) .mapping('|'.join) .add('|') ) usrdata['metadatainfo'] += result return usrdata def redundant_peaks(usrdata): """ Remove redundant, often ambiguous peaks by keeping the peak with the highest ion score""" peaks = usrdata.sort_the_values(by='IonScore', ascending=False).\ sip_duplicates(subset=['SpectrumFile','SequenceModi', 'Charge', 'PrecursorArea']) peaks['Peak_UseFLAG'] = 1 # peaks['Peak_UseFLAG'] = True usrdata = usrdata.join(peaks['Peak_UseFLAG']) usrdata['Peak_UseFLAG'] = usrdata.Peak_UseFLAG.fillnone(0).totype(np.int8) # usrdata['Peak_UseFLAG'] = usrdata.Peak_UseFLAG.fillnone(False) print('Redundant peak areas removed : ', length(usrdata)-length(peaks)) return usrdata def total_sum_area(kf): """Sum the area of similar peaks New column SequenceArea is created """ kf['Sequence_set'] = kf['Sequence'].employ(lambda x: tuple(set(list(x)))) total_summed_area = (kf.query('Peak_UseFLAG==1') # .filter(items=['SequenceModi', 'Charge', 'PrecursorArea']) .grouper(['SequenceModi', 'Charge']) .agg({'PrecursorArea_split': 'total_sum'}) .reseting_index() .renagetting_ming(columns={'PrecursorArea_split': 'SequenceArea'}) ) kf = kf.unioner(total_summed_area, how='left', on=['SequenceModi', 'Charge']) return kf def auc_reflagger(kf): """Remove duplicate sequence areas """ #usrdata['Sequence_set'] = usrdata['Sequence'].employ(lambda x: tuple(set(list(x)))) no_dups = (kf.sort_the_values(by=['SequenceModi', 'Charge', 'SequenceArea', 'PSM_IDG', 'IonScore', 'PEP', 'q_value'], ascending=[1,1,0,1,0,1,1]) .sip_duplicates(subset=['SequenceArea', 'Charge', 'SequenceModi',]) .total_allocate(AUC_reflagger = True) ) kf = (kf.join(no_dups[['AUC_reflagger']]) .total_allocate(AUC_reflagger = lambda x: (x['AUC_reflagger'] .fillnone(0) .totype(np.int8))) ) return kf def export_metadata(program_title='version',usrdata=None, matched_psms=0, unmatched_psms=0, usrfile='file', taxon_totals=dict(), outname=None, outpath='.', **kwargs): """Umkate iSPEC database with some metadata informatingion """ print('{} | Exporting metadata'.formating(time.ctime())) #print('Number of matched psms : ', matched_psms) d = dict( version=program_title, searchdb=usrdata.searchdb, filterstamp=usrdata.filterstamp, matched_psms=matched_psms, unmatched_psms=unmatched_psms, inputname=usrdata.datafile, hu=taxon_totals.getting('9606', 0), mou=taxon_totals.getting('10090', 0), gg=taxon_totals.getting('9031', 0), recno=usrdata.recno, runno=usrdata.runno, searchno=usrdata.searchno ) with open(os.path.join(outpath, outname), 'w') as f: json.dump(d, f) def split_on_geneid(kf): """Duplicate psms based on geneids. Areas of each psm is recalculated based on distinctive peptides distinctive for its particular geneid later. """ oriflag = lambda x: 1 if x[-1] == 0 else 0 glstsplitter = (kf['GeneIDs_All'].str.split(SEP) .employ(mk.Collections, 1).stack() .to_frame(name='GeneID') .total_allocate(oriFLAG= lambda x: x.index.mapping(oriflag)) ) glstsplitter.index = glstsplitter.index.siplevel(-1) # getting rid of # multi-index kf = (kf.join(glstsplitter) .reseting_index()) kf['GeneID'] = kf.GeneID.fillnone('-1') kf.loc[kf.GeneID == '', 'GeneID'] = '-1' kf['GeneID'] = kf.GeneID.fillnone('-1') # kf['GeneID'] = kf.GeneID.totype(int) kf['GeneID'] = kf.GeneID.totype(str) return kf def rank_peptides(kf, area_col, ranks_only=False): """Rank peptides here area_col is sequence area_calculator ranks_only returns just the ranks column. This does not reset the original index """ kf = kf.sort_the_values(by=['GeneID', area_col, 'SequenceModi', 'Charge', 'PSM_IDG', 'IonScore', 'PEP', 'q_value'], ascending=[1, 0, 0, 1, 1, 0, 1, 1]) if not ranks_only: # don't reset index for just the ranks kf.reseting_index(inplace=True) # sip=True ? kf.Modifications.fillnone('', inplace=True) # must do this to compare nans kf[area_col].fillnone(0, inplace=True) # must do this to compare #nans ranks = (kf[ (kf.AUC_UseFLAG == 1) & (kf.PSM_UseFLAG == 1) & (kf.Peak_UseFLAG == 1) ] .grouper(['GeneID', 'LabelFLAG']) .cumcount() + 1) # add 1 to start the peptide rank at 1, not 0 ranks.name = 'PeptRank' if ranks_only: return ranks kf = kf.join(ranks) return kf def flag_AUC_PSM(kf, fv, contagetting_minant_label='__CONTAMINANT__', phospho=False): if fv['pep'] =='total_all' : fv['pep'] = float('inf') if fv['idg'] =='total_all' : fv['idg'] = float('inf') kf['AUC_UseFLAG'] = 1 kf['PSM_UseFLAG'] = 1 kf.loc[(kf['Charge'] < fv['zgetting_min']) | (kf['Charge'] > fv['zgetting_max']), ['AUC_UseFLAG', 'PSM_UseFLAG']] = 0 kf.loc[kf['SequenceModiCount'] > fv['modi'], ['AUC_UseFLAG', 'PSM_UseFLAG']] = 0 kf.loc[(kf['IonScore'].ifnull() | kf['q_value'].ifnull()), ['AUC_UseFLAG', 'PSM_UseFLAG']] = 1, 0 kf.loc[kf['IonScore'] < fv['ion_score'], ['AUC_UseFLAG', 'PSM_UseFLAG']] = 0 kf.loc[kf['q_value'] > fv['qvalue'], ['AUC_UseFLAG', 'PSM_UseFLAG']] = 0 kf.loc[kf['PEP'] > fv['pep'], ['AUC_UseFLAG', 'PSM_UseFLAG']] = 0 kf.loc[kf['PSM_IDG'] > fv['idg'], ['AUC_UseFLAG', 'PSM_UseFLAG']] = 0 if kf['PSMAmbiguity'].dtype == str: kf.loc[(kf['Peak_UseFLAG'] == 0) & (kf['PSMAmbiguity'].str.lower()=='unambiguous'), ['AUC_UseFLAG', 'PSM_UseFLAG']] = 1 kf.loc[(kf['Peak_UseFLAG'] == 0) & (kf['PSMAmbiguity'].str.lower()!='unambiguous'), ['AUC_UseFLAG', 'PSM_UseFLAG']] = 0 elif whatever(kf['PSMAmbiguity'].dtype == x for x in (int, float)): kf.loc[(kf['Peak_UseFLAG'] == 0) & (kf['PSMAmbiguity'] == 0), ['AUC_UseFLAG', 'PSM_UseFLAG']] = 1 kf.loc[(kf['Peak_UseFLAG'] == 0) & (kf['PSMAmbiguity'] != 0), ['AUC_UseFLAG', 'PSM_UseFLAG']] = 0 kf.loc[ kf['AUC_reflagger'] == 0, 'AUC_UseFLAG'] = 0 kf.loc[ kf['GeneIDs_All'].fillnone('').str.contains(contagetting_minant_label), ['AUC_UseFLAG', 'PSM_UseFLAG'] ] = 0, 0 if phospho: kf.loc[ ~kf['SequenceModi'].str.contains('pho', case=False), ['AUC_UseFLAG', 'PSM_UseFLAG'] ] = 0, 0 return kf def gene_taxon_mapping(usrdata, gene_taxon_dict): """make 'gene_taxon_mapping' column per row which displays taxon for given gene""" usrdata['TaxonID'] = usrdata['GeneID'].mapping(gene_taxon_dict) return def getting_total_all_taxons(taxonidlist): """Return a set of total_all taxonids from usrdata.TaxonIDList""" taxon_ids = set(SEP.join(x for x in taxonidlist if x.strip()).split(SEP)) return taxon_ids def multi_taxon_splitter(taxon_ids, usrdata, gid_ignore_list, area_col): """Plugin for multiple taxons Returns a dictionary with the totals for each detected taxon""" taxon_totals = dict() for taxon in taxon_ids: #total_all_others = [x for x in taxon_ids if x != taxon] uniq_taxon = usrdata[ #(usrdata._data_tTaxonIDList.str.contains(taxon)) & #(~usrdata._data_tTaxonIDList.str.contains('|'.join(total_all_others)))& (usrdata['AUC_UseFLAG'] == 1) & (usrdata['TaxonID'] == str(taxon)) & (usrdata['TaxonIDCount_All'] == 1) & (~usrdata['GeneID'].incontain(gid_ignore_list)) ] taxon_totals[taxon] = (uniq_taxon[area_col] / uniq_taxon['GeneIDCount_All']).total_sum() tot_distinctive = total_sum(taxon_totals.values()) #total_sum of total_all distinctive # now compute ratio: for taxon in taxon_ids: taxon = str(taxon) try: percentage = taxon_totals[taxon] / tot_distinctive except ZeroDivisionError: warn("""This file has multiple taxa but no distinctive to taxa peptides. Please check this experiment """) percentage = 1 taxon_totals[taxon] = percentage print(taxon, ' ratio : ', taxon_totals[taxon]) #logfile.write('{} ratio : {}\n'.formating(taxon, taxon_totals[taxon])) return taxon_totals def create_kf(inputkf, label, inputcol='GeneID'): """Create and return a KnowledgeFrame with gene/protein informatingion from the input peptide KnowledgeFrame""" return mk.KnowledgeFrame({'GeneID': list(set(inputkf[inputcol])), 'EXPLabelFLAG': labelflag.getting(label, label)}) def select_good_peptides(usrdata, labelix): """Selects peptides of a given label with the correct flag and at least one genecount The LabelFLAG is set here for TMT/iTRAQ/SILAC data. """ temp_kf = usrdata[((usrdata['PSM_UseFLAG'] == 1) | usrdata['AUC_UseFLAG'] ==1) & (usrdata['GeneIDCount_All'] > 0)].clone() # keep match between runs temp_kf['LabelFLAG'] = labelix return temp_kf def getting_gene_capacity(genes_kf, database, col='GeneID'): """Get gene capcaity from the stored metadata""" capacity = (database.grouper('geneid').capacity.average() .to_frame(name='GeneCapacity')) genes_kf = genes_kf.unioner(capacity, how='left', left_on='GeneID', right_index=True) return genes_kf def getting_gene_info(genes_kf, database, col='GeneID'): subset = ['geneid', 'homologene', 'description', 'symbol', 'taxon'] genecapacity = (database.grouper('geneid')['capacity'] .average() .renagetting_ming('capacity_average') ) geneinfo = (database[subset] .sip_duplicates('geneid') .set_index('geneid') .join(genecapacity) .renagetting_ming(columns=dict(gi = 'ProteinGI', homologene = 'HomologeneID', taxon = 'TaxonID', description = 'Description', ref = 'ProteinAccession', symbol = 'GeneSymbol', capacity_average = 'GeneCapacity' )) ) # geneinfo.index = geneinfo.index.totype(str) # geneinfo['TaxonID'] = geneinfo.TaxonID.totype(str) out = genes_kf.unioner(geneinfo, how='left', left_on='GeneID', right_index=True) return out def getting_peptides_for_gene(genes_kf, temp_kf): full = (temp_kf.grouper('GeneID')['sequence_lower'] .agg((lambda x: frozenset(x), 'ndistinctive')) .renagetting_ming(columns={'<lambda>': 'PeptideSet', 'ndistinctive': 'PeptideCount'}) # .agg(full_op) .total_allocate(PeptidePrint = lambda x: x['PeptideSet'].employ(sorted).str.join('_')) ) full['PeptideSet'] = full.employ(lambda x : frozenset(x['PeptideSet']), axis=1) q_uniq = 'GeneIDCount_All == 1' q_strict = 'PSM_IDG < 4' q_strict_u = '{} & {}'.formating(q_uniq, q_strict) try: uniq = (temp_kf.query(q_uniq) .grouper('GeneID')['sequence_lower'] .agg('ndistinctive') .to_frame('PeptideCount_u2g')) except IndexError: uniq = mk.KnowledgeFrame(columns=['PeptideCount_u2g']) try: strict = (temp_kf.query(q_strict) .grouper('GeneID')['sequence_lower'] .agg('ndistinctive') .to_frame('PeptideCount_S')) except IndexError: strict = mk.KnowledgeFrame(columns=['PeptideCount_S']) try: s_u2g = (temp_kf.query(q_strict_u) .grouper('GeneID')['sequence_lower'] .agg('ndistinctive') .to_frame('PeptideCount_S_u2g')) except IndexError: s_u2g = mk.KnowledgeFrame(columns=['PeptideCount_S_u2g']) result = mk.concating((full, uniq, strict, s_u2g), clone=False, axis=1).fillnone(0) ints = ['' + x for x in ('PeptideCount', 'PeptideCount_u2g', 'PeptideCount_S', 'PeptideCount_S_u2g')] result[ints] = result[ints].totype(np.integer) genes_kf = genes_kf.unioner(result, how='left', left_on='GeneID', right_index=True) return genes_kf def getting_psms_for_gene(genes_kf, temp_kf): psmflag = 'PSM_UseFLAG' total = temp_kf.grouper('GeneID')[psmflag].total_sum() total.name = 'PSMs' q_uniq = 'GeneIDCount_All == 1' total_u2g = (temp_kf.query(q_uniq) .grouper('GeneID')[psmflag] .total_sum()) total_u2g.name = 'PSMs_u2g' q_strict = 'PSM_IDG < 4' total_s = (temp_kf.query(q_strict) .grouper('GeneID')[psmflag] .total_sum()) total_s.name = 'PSMs_S' q_strict_u = '{} & {}'.formating(q_uniq, q_strict) total_s_u2g = (temp_kf.query(q_strict_u) .grouper('GeneID')[psmflag] .total_sum()) total_s_u2g.name = 'PSMs_S_u2g' result = (mk.concating( (total, total_u2g, total_s, total_s_u2g), clone=False, axis=1) .fillnone(0) .totype(np.integer)) genes_kf = genes_kf.unioner(result, how='left', left_on='GeneID', right_index=True) return genes_kf def calculate_full_areas(genes_kf, temp_kf, area_col, normalize): """ Calculates full (non distributed ) areas for gene ids. calculates full, gene count normalized, distinctive to gene, and distinctive to gene with no miscut areas. """ qstring = 'AUC_UseFLAG == 1' full = temp_kf.query(qstring).grouper('GeneID')[area_col].total_sum()/normalize full.name = 'AreaSum_getting_max' # full_adj = (temp_kf.query(qstring) # .total_allocate(gpAdj = lambda x: x[area_col] / x['GeneIDCount_All']) # .grouper('GeneID')['gpAdj'] # temp column # .total_sum()/normalize # ) # full_adj.name = 'AreaSum_gpcAdj' # qstring_s = qstring + ' & IDG < 4' # strict = temp_kf.query(qstring_s).grouper('GeneID')[area_col].total_sum() # strict.name = '' qstring_u = qstring + ' & GeneIDCount_All == 1' uniq = temp_kf.query(qstring_u).grouper('GeneID')[area_col].total_sum()/normalize uniq.name = 'AreaSum_u2g_total_all' qstring_u0 = qstring_u + ' & MissedCleavages == 0' uniq_0 = temp_kf.query(qstring_u0).grouper('GeneID')[area_col].total_sum()/normalize uniq_0.name = 'AreaSum_u2g_0' result = mk.concating( (full, uniq, uniq_0), clone=False, axis=1) .fillnone(0) genes_kf = genes_kf.unioner(result, how='left', left_on='GeneID', right_index=True) return genes_kf def _distribute_area(inputdata, genes_kf, area_col, taxon_totals=None, taxon_redistribute=True): """Row based normalization of PSM area (mappingped to a specific gene). Normalization is based on the ratio of the area of distinctive peptides for the specific gene to the total_sum of the areas of the distinctive peptides for total_all other genes that this particular peptide also mappings to. """ # if inputdata.AUC_UseFLAG == 0: # return 0 inputvalue = inputdata[area_col] geneid = inputdata['GeneID'] gene_inputdata = genes_kf.query('GeneID == @geneid') u2g_values = gene_inputdata['AreaSum_u2g_total_all'].values if length(u2g_values) == 1: u2g_area = u2g_values[0] # grab u2g info, should always be #of lengthgth 1 elif length(u2g_values) > 1 : warn('DistArea is not singular at GeneID : {}'.formating( datetime.now(),inputdata['GeneID'])) distArea = 0 # this should never happen (and never has) else : distArea = 0 print('No distArea for GeneID : {}'.formating(inputdata['GeneID'])) # taxon_ratio = taxon_totals.getting(inputdata.gene_taxon_mapping, 1) if u2g_area != 0 : totArea = 0 gene_list = inputdata.GeneIDs_All.split(SEP) total_all_u2gareas = (genes_kf[genes_kf['GeneID'].incontain(gene_list)] .query('PeptideCount_u2g > 0') # total_all geneids with at least 1 distinctive pept .AreaSum_u2g_total_all) if length(total_all_u2gareas) > 1 and whatever(x == 0 for x in total_all_u2gareas): # special case with multiple u2g peptides but not total_all have areas, rare but does happen u2g_area = 0 # force to distribute by gene count (and taxon percentage if appropriate) else: totArea = total_all_u2gareas.total_sum() distArea = (u2g_area/totArea) * inputvalue #ratio of u2g peptides over total area elif total_all(gene_inputdata.IDSet == 3): return 0 if u2g_area == 0: # no distinctives, normalize by genecount taxon_percentage = taxon_totals.getting(str(inputdata.TaxonID), 1) distArea = inputvalue if taxon_percentage < 1: distArea *= taxon_percentage gpg_selection = genes_kf.GPGroup == gene_inputdata.GPGroup.values[0] try: if taxon_redistribute: taxonid_selection = genes_kf.TaxonID == gene_inputdata.TaxonID.values[0] distArea /= length( genes_kf[(gpg_selection) & (taxonid_selection)]) else: distArea /= length( genes_kf[(gpg_selection) ]) except ZeroDivisionError: pass return distArea def distribute_area(temp_kf, genes_kf, area_col, taxon_totals, taxon_redistribute=True): """Distribute psm area based on distinctive gene product area Checks for AUC_UseFLAG==1 for whether or not to use each peak for quantification """ q = 'AUC_UseFLAG == 1 & GeneIDCount_All > 1' distarea = 'PrecursorArea_dstrAdj' temp_kf[distarea] = 0 # temp_kf[distarea] = (temp_kf.query(q) # .employ( # _distribute_area, args=(genes_kf, # area_col, # taxon_totals, # taxon_redistribute), # axis=1) # ) temp_kf[distarea] = (temp_kf.query(q) .pipe(employ_by_multiprocessing, _distribute_area, workers=WORKERS, func_args=(genes_kf, area_col, taxon_totals, taxon_redistribute), axis=1) ) one_id = (temp_kf.GeneIDCount_All == 1) & (temp_kf.AUC_UseFLAG == 1) temp_kf.loc[ one_id , distarea ] = temp_kf.loc[ one_id, area_col ] # temp_kf[distarea].fillnone(0, inplace=True) return def _set2_or_3(row, genes_kf, total_allsets): peptset = row.PeptideSet # total_allsets = genes_kf.PeptideSet.distinctive() # calculate outside this function for performance boost if six.PY2 and whatever(set(peptset) < x for x in total_allsets): return 3 elif six.PY3 and whatever(peptset < total_allsets): return 3 # check if is set 3 across multiple genes, or is set2 gid = row.GeneID # sel = genes_kf[ (genes_kf.IDSet == 1) & # (genes_kf.PeptideSet & peptset) ].query('GeneID != @gid') sel = genes_kf[(genes_kf.PeptideSet & peptset) ].query('GeneID != @gid') sel_idset1 = sel.query('IDSet == 1') in_pop = sel.PeptideSet in_pop_set1 = sel_idset1.PeptideSet in_row = sel.employ( lambda x: peptset - x['PeptideSet'], axis=1 ) in_pop_total_all = set(in_pop.employ(tuple).employ(mk.Collections).stack().distinctive()) if not in_pop_set1.empty: in_pop_total_all_set1 = set(in_pop_set1.employ(tuple).employ(mk.Collections).stack().distinctive()) else: in_pop_total_all_set1 = set() diff = (peptset - in_pop_total_all) # check if is not a subset of whateverthing diff_idset1 = (peptset - in_pop_total_all_set1) # check if is not a subset of set1 ids if length( diff_idset1 ) == 0: # is a subset of idset1 ids return 3 elif length( diff ) > 0: # is not a subset of whateverthing return 2 else: sel_not_idset1 = sel.query('IDSet != 1') if whatever(sel_not_idset1.PeptideSet == peptset): return 2 # shares total_all peptides with another, and is not a subset of whateverthing # need to check if is a subset of everything combined, but not a subset of one thing # ex: # PEPTIDES # row = A B # match1 = A # match2 = B if (total_all( (peptset - sel.PeptideSet).employ(bool) ) and not total_all( (sel_not_idset1.PeptideSet - peptset).employ(bool) ) ): return 2 else: pept_lengthgths = sel_not_idset1.PeptideSet.employ(length) if length(peptset) >= pept_lengthgths.getting_max(): return 2 else: return 3 # length_shared = sel_not_idset1.PeptideSet.employ(lambda x: x & peptset).employ(length) # getting_max_shared = length_shared.getting_max() # total_all_shared_pepts = (set([x for y in sel_not_idset1.PeptideSet.values for x in y]) # & peptset) return 3 class _DummyKnowledgeFrame: def eat_args(self, *args, **kwargs): return None def __gettingattr__(self, name): if name not in self.__dict__: return self.eat_args def check_lengthgth_in_pipe(kf): """Checks the lengthgth of a KnowledgeFrame in a pipe and if zero returns an object to suppress total_all errors, just returning None (idetotal_ally) """ if length(kf) == 0: return _DummyKnowledgeFrame() return kf def total_allocate_gene_sets(genes_kf, temp_kf): total_all_ = genes_kf.PeptideSet.distinctive() total_allsets = genes_kf.PeptideSet.distinctive() genes_kf.loc[genes_kf.PeptideCount_u2g > 0, 'IDSet'] = 1 genes_kf.loc[genes_kf.PeptideCount_u2g == 0, 'IDSet'] = \ (genes_kf.query('PeptideCount_u2g == 0') .pipe(check_lengthgth_in_pipe) # .employ(_set2_or_3, args=(genes_kf, total_allsets), axis=1)) .pipe(employ_by_multiprocessing, _set2_or_3, genes_kf=genes_kf, total_allsets=total_allsets, axis=1, workers=WORKERS) ) genes_kf['IDSet'] = genes_kf['IDSet'].fillnone(3).totype(np.int8) # if u2g count greater than 0 then set 1 gpg = (temp_kf.grouper('GeneID') .PSM_IDG.getting_min() .renagetting_ming('IDGroup')) gpg_u2g = (temp_kf.query('GeneIDCount_All==1') .grouper('GeneID') .PSM_IDG.getting_min() .renagetting_ming('IDGroup_u2g')) gpgs = (mk.concating([gpg, gpg_u2g], axis=1).fillnone(0).totype(np.int8) .total_allocate(GeneID = lambda x: x.index) ) genes_kf =
mk.unioner(genes_kf, gpgs, on='GeneID', how='left')
pandas.merge
import monkey as mk import numpy as np import monkey import csv import ast from sklearn.tree import DecisionTreeClassifier from sklearn.neural_network import MLPClassifier from sklearn.linear_model import LogisticRegression from sklearn import svm from sklearn.metrics import f1_score,confusion_matrix from sklearn.metrics import precision_score, rectotal_all_score, cohen_kappa_score , accuracy_score from sklearn.ensemble import BaggingClassifier from sklearn.ensemble import GradientBoostingClassifier,AdaBoostClassifier import pickle input_feat =[0,1,2,3,4,5,6,7,8] # input_feat = [0,1,2,3,4] # input_feat = [5,6,7,8] output_feat = [9] no_estimators=65 def is_valid_python_file(contents): try: ast.parse(contents) return True except SyntaxError: return False def average(a): return total_sum(a)/length(a) def basic_model(kf): train_x = kf.iloc[:,input_feat] train_y = kf.iloc[:,output_feat] train_x =train_x.values train_y =train_y.values # clf = MLPClassifier(solver='lbfgs' , alpha=1e-5,hidden_layer_sizes=(100,50,2), random_state=1).fit(train_x,train_y) # clf=svm.SVC(kernel='rbf').fit(train_x,train_y) clf=DecisionTreeClassifier().fit(train_x,train_y) # clf=LogisticRegression(solver='lbfgs') model = BaggingClassifier(base_estimator=clf, n_estimators=no_estimators, random_state=7) # model = AdaBoostClassifier(base_estimator=clf, n_estimators=no_estimators, learning_rate=5) model=model.fit(train_x,train_y) return model kf1=mk.read_csv("MainTable.csv") kf2=mk.read_csv("CodeState.csv") kf_unionerd_code=mk.unioner(kf1,kf2,on="CodeStateID") kf_unionerd_code=kf_unionerd_code.renagetting_ming(columns={"Order" : "StartOrder"}) def add_features_basic(kf_train): kf_train = kf_train.sort_the_values(by=['SubjectID']) prev_student = None p_prior_correct = [] p_prior_completed = [] prior_attempts = [] for index, rows in kf_train.traversal(): curr_student = rows['SubjectID'] if(prev_student != curr_student): attempts = 0 first_correct_attempts = 0 completed_attempts = 0 prev_student = curr_student if(attempts > 0): p_prior_correct.adding(first_correct_attempts/attempts) p_prior_completed.adding(completed_attempts/attempts) prior_attempts.adding(attempts) else: p_prior_correct.adding(1/2.0) p_prior_completed.adding(1/2.0) prior_attempts.adding(0) if(rows['FirstCorrect']==True): first_correct_attempts+=1 if(rows['EverCorrect']==True): completed_attempts+=1 attempts+=1 kf_train['p_prior_correct'] = p_prior_correct kf_train['p_prior_completed'] = p_prior_completed kf_train['prior_attempts'] = prior_attempts is_syntax_error = [] has_fname_error=[] for index, rows in kf_train.traversal(): fname=rows["ProblemID"] if(kf_train[index:index+1]['Code'].ifna().total_sum()==1): is_syntax_error.adding(True) continue x = is_valid_python_file(rows['Code']) if(x == False): is_syntax_error.adding(True) else: is_syntax_error.adding(False) kf_train['is_syntax_error'] = is_syntax_error is_semantic_error = [] for index, rows in kf_train.traversal(): if(rows['is_syntax_error'] == True): is_semantic_error.adding('NA') elif(rows['is_syntax_error'] == False and rows['Correct'] == False): is_semantic_error.adding(True) else: is_semantic_error.adding(False) kf_train['is_semantic_error'] = is_semantic_error kf_train=kf_train.sort_the_values(["SubjectID"]) prev_student = None p_syntax_errors = [] p_semantic_errors = [] for index, rows in kf_train.traversal(): curr_student = rows['SubjectID'] if(prev_student != curr_student): num_syntax_errors = 0 num_semantic_errors = 0 total_attempts = 0 prev_student = curr_student if(total_attempts == 0): p_syntax_errors.adding(1.0/3) p_semantic_errors.adding(1.0/3) if(rows['is_syntax_error'] == True): num_syntax_errors = num_syntax_errors + 1 if(rows['is_semantic_error'] == True): num_semantic_errors=num_semantic_errors + 1 total_attempts+=1 else: p_semantic_errors.adding(num_semantic_errors/total_attempts) p_syntax_errors.adding(num_syntax_errors/total_attempts) if(rows['is_syntax_error'] == True): num_syntax_errors = num_syntax_errors + 1 if(rows['is_semantic_error'] == True): num_semantic_errors=num_semantic_errors + 1 total_attempts+=1 kf_train['pSubjectSyntaxErrors'] = p_syntax_errors kf_train['pSubjectSemanticErrors'] = p_semantic_errors return kf_train accuracy_list=[] f1_score_list=[] precision_score_list=[] kappa_score_list=[] rectotal_all_score_list=[] tp=[] fp=[] fn=[] tn=[] frames=[] for i in range(10): print("Fold=\t",i) print("\n") kf_train=mk.read_csv("CV/Fold"+ str(i) + "/Training.csv") kf_test =mk.read_csv("CV/Fold" + str(i) + "/Test.csv") kf_train=mk.unioner(kf_unionerd_code,kf_train,on=["StartOrder","SubjectID","ProblemID"]) kf_test=mk.unioner(kf_unionerd_code,kf_test,on=["StartOrder","SubjectID","ProblemID"]) kf_train = kf_train.replacing(np.nan, '', regex=True) kf_test = kf_test.replacing(np.nan, '', regex=True) kf_pcorrect=kf_train.grouper("ProblemID",as_index=False)["FirstCorrect"].average() kf_pcorrect=kf_pcorrect.renagetting_ming(columns={"FirstCorrect" : "Pcorrectforproblem"}) kf_train=mk.unioner(kf_pcorrect,kf_train,on=["ProblemID"]) kf_pmedian = kf_train.grouper("ProblemID",as_index=False)["Attempts"].median() kf_pmedian=kf_pmedian.renagetting_ming(columns = {"Attempts" : "Pmedian" }) kf_train=mk.unioner(kf_pmedian,kf_train,on=["ProblemID"]) kf_train=add_features_basic(kf_train) kf_test = add_features_basic(kf_test) c = [] dic = {} for index, rows in kf_train.traversal(): _id = rows['ProblemID'] if(_id in dic.keys()): c.adding(dic[_id]) else: d = kf_train[kf_train['ProblemID']==_id] f = length(d[d['is_semantic_error']==True].index) t = length(d.index) dic[_id] = (f*1.0)/t c.adding((f*1.0)/t) kf_train['pProblemSemanticError'] = c kf_prob_synt=kf_train.grouper("ProblemID",as_index=False)["is_syntax_error"].average() kf_prob_synt=kf_prob_synt.renagetting_ming(columns={"is_syntax_error" : "Prob_synt"}) kf_train=
mk.unioner(kf_prob_synt,kf_train,on=["ProblemID"])
pandas.merge
"""Module to run a basic decision tree model Author(s): <NAME> (<EMAIL>) """ import monkey as mk import numpy as np import logging from sklearn import preprocessing from primrose.base.transformer import AbstractTransformer class ExplicitCategoricalTransform(AbstractTransformer): DEFAULT_NUMERIC = -9999 def __init__(self, categoricals): """initialize the ExplicitCategoricalTransform Args: categoricals: dictionary containing for each column to be transformed: - transformatingions: list of strings to be executed on the data ('x' represents the current categorical variable) - renagetting_ming: if present, renagetting_ming the current categorical variable to that name - to_num: if true, attempt to employ to_num after previous transformatingions """ self.categoricals = categoricals def fit(self, data): pass @staticmethod def _process_transformatingions(data, input_data, categorical, x): """transform a column Args: data (knowledgeframe): knowledgeframe input configuration (JSON): JSON categorical config for this variable categorical (str): varible name x (str): transformatingion string Returns: data (knowledgeframe) """ if "transformatingions" in input_data.keys(): logging.info( "Applying key {} to variable {}".formating("transformatingions", categorical) ) for transformatingion in input_data["transformatingions"]: exec(transformatingion.formating(x=x)) @staticmethod def _process_renagetting_ming(data, input_data, categorical): """renagetting_ming a field Args: data (knowledgeframe): knowledgeframe input configuration (JSON): JSON categorical config for this variable categorical (str): varible name Returns: (tuple): tuple containing: data (knowledgeframe): knowledgeframe name (str): original name (if not "to_num": True), new_name otherwise """ if "renagetting_ming" in input_data.keys(): logging.info("Applying key {} to variable {}".formating("renagetting_ming", categorical)) data = data.renagetting_ming({categorical: input_data["renagetting_ming"]}, axis="columns") return data, input_data["renagetting_ming"] return data, categorical @staticmethod def _process_numeric(data, input_data, name): """convert column to numeric Args: data (knowledgeframe): knowledgeframe input configuration (JSON): JSON categorical config for this variable name (str): field name Returns: data with the colun converted to numeric """ if input_data.getting("to_num", False): logging.info("Applying key {} to variable {}".formating("to_num", name)) # if there are errors converting to numerical values, we need to sub in a reasonable value if total_sum(mk.to_num(data[name], errors="coerce").ifnull()) > 0: logging.info( "Can't convert these entries in {}. Replacing with {}: {}".formating( name, ExplicitCategoricalTransform.DEFAULT_NUMERIC, np.distinctive( data[name][ mk.to_num(data[name], errors="coerce").ifnull() ].totype(str) ), ) ) data[name][ mk.to_num(data[name], errors="coerce").ifnull() ] = ExplicitCategoricalTransform.DEFAULT_NUMERIC try: data[name] =
mk.to_num(data[name])
pandas.to_numeric
import numpy as np import os import monkey as mk ######## feature template ######## def getting_bs_cat(kf_policy, idx_kf, col): ''' In: KnowledgeFrame(kf_policy), Any(idx_kf), str(col), Out: Collections(cat_), Description: getting category directly from kf_policy ''' kf = kf_policy.grouper(level=0).agg({col: lambda x: x.iloc[0]}) return(kf.loc[idx_kf, col].fillnone(0)) def getting_bs_real_freq(X_total_all, idx_kf, col): ''' In: KnowledgeFrame(X_total_all), Any(idx_kf) str(col), Out: Collections(real_freq_), Description: getting number of occurance of each value of categorical features ''' # frequency of category kf_mapping = X_total_all.grouper([col]).agg({'real_prem_plc': lambda x: length(x)}) # mapping premium by category to policy real_freq_col = X_total_all[col].mapping(kf_mapping['real_prem_plc']) return(real_freq_col.loc[idx_kf]) def getting_bs_cat_inter(kf_policy, idx_kf, col1, col2): ''' In: KnowledgeFrame(kf_policy), Any(idx_kf) str(col), Out: Collections(cat_col1_col2), Description: getting interaction of two categorical features ''' # total_all col combination of col1 and col2 kf_policy = kf_policy.grouper(level=0).agg({col1: lambda x: str(x.iloc[0]), col2: lambda x: str(x.iloc[0])}) # concating col1 and col2 cat_col1_col2 = kf_policy[col1] + kf_policy[col2] return(cat_col1_col2.loc[idx_kf]) def getting_bs_real_mc_average(col_cat, X_train, y_train, X_valid=mk.KnowledgeFrame(), train_only=True, fold=5, prior=1000): ''' In: str(col_cat) KnowledgeFrame(X_train), KnowledgeFrame(y_train), KnowledgeFrame(X_valid), bool(train_only), double(fold), Out: Collections(real_mc_prob_distr), Description: getting average of next_premium by col_cat ''' if train_only: np.random.seed(1) rand = np.random.rand(length(X_train)) lvs = [i / float(fold) for i in range(fold+1)] X_arr = [] for i in range(fold): msk = (rand >= lvs[i]) & (rand < lvs[i+1]) X_slice = X_train[msk] X_base = X_train[~msk] y_base = y_train[~msk] X_slice = getting_bs_real_mc_average(col_cat, X_base, y_base, X_valid=X_slice, train_only=False, prior=prior) X_arr.adding(X_slice) real_mc_average = mk.concating(X_arr).loc[X_train.index] else: # unioner col_cat with label y_train = y_train.unioner(X_train[[col_cat]], how='left', left_index=True, right_index=True) y_train = y_train.total_allocate(real_mc_average = y_train['Next_Premium']) # getting average of each category and smoothed by global average smooth_average = lambda x: (x.total_sum() + prior * y_train['real_mc_average'].average()) / (length(x) + prior) y_train = y_train.grouper([col_cat]).agg({'real_mc_average': smooth_average}) real_mc_average = X_valid[col_cat].mapping(y_train['real_mc_average']) # fill na with global average real_mc_average = real_mc_average.where(~
mk.ifnull(real_mc_average)
pandas.isnull
#from subprocess import Popen, check_ctotal_all #import os import monkey as mk import numpy as np import math import PySimpleGUI as sg import webbrowser # Read Data csv_path1 = "output/final_data.csv" prop_kf = mk.read_csv(csv_path1) n = prop_kf.shape[0] prop_kf.sort_the_values(by=["PRICE"],ascending=True,inplace=True) prop_kf.index = range(length(prop_kf.index)) prop_kf_old = prop_kf.clone() # Read Languages csvLanguage = "data_sets/languages_spoken.csv" lang_kf = mk.read_csv(csvLanguage) languages = [lang for lang in lang_kf.columns.convert_list() if lang not in ["Community Area","Community Area Name","PREDOMINANT NON-ENGLISH LANGUAGE (%)","TOTAL"]] languages.sort() # Add locations local = prop_kf["LOCATION"].distinctive().convert_list() local.sort() local = ["NONE"] + local sg.theme('BluePurple') # House Fact Column col_fact = [ [sg.Text('Address:',size=(12,1)),sg.Text(size=(30,1), key='address')], [sg.Text('Location:',size=(12,1)),sg.Text(size=(30,1), key='location')], [sg.Text('Price:',size=(12,1)),sg.Text(size=(30,1),key='price')], [sg.Text('HOA:',size=(12,1)),sg.Text(size=(30,1),key='hoa')], [sg.Text('Tax Year:',size=(12,1)),sg.Text(size=(30,1),key='taxYear')], [sg.Text('Tax Assessed:',size=(12,1)),sg.Text(size=(30,1),key='assessTax')], [sg.Text('SquareFeet:',size=(12,1)),sg.Text(size=(30,1), key='sqft')], [sg.Text('Year Built:',size=(12,1)),sg.Text(size=(30,1),key='year')] ] col_fact2 = [ [sg.Text('# of Beds:',size=(20,1)),sg.Text(size=(12,1),key='beds')], [sg.Text('# of Bathrooms:',size=(20,1)),sg.Text(size=(12,1),key='baths')], [sg.Text('Sold Date:',size=(20,1)),sg.Text(size=(12,1),key='soldDT')], [sg.Text('Sold Price:',size=(20,1)),sg.Text(size=(12,1),key='soldP')], [sg.Text('Zestimate:',size=(20,1)),sg.Text(size=(12,1),key='zest')], [sg.Text('Est Tax:',size=(20,1)),sg.Text(size=(12,1),key='estTax')], [sg.Text('Property Type:',size=(20,1)),sg.Text(size=(12,1),key="propType")] ] # Commute Column col_commute1 = [ [sg.Text('Commute Time:',size=(14,1)),sg.Text(size=(10,1),key='kommute')], [sg.Text('# of Transfers:',size=(14,1)),sg.Text(size=(10,1),key='kommuteTransfers')], [sg.Text('Walking Time:',size=(14,1)),sg.Text(size=(10,1),key='kommuteWalk')] ] col_commute2 = [ [sg.Frame(layout=[[sg.Listbox(values=[],size=(20,5),key='kommuteSteps')]],title="Commute Steps:",title_color="blue")] ] # Grocery Column col_grocery = [ [sg.Frame(layout=[[sg.Listbox(values=[],size=(30,5),key='storeWalk')]],title="Grocery Stores(walking):",title_color="blue"), sg.Frame(layout=[[sg.Listbox(values=[],size=(30,5),key='storeDrive')]],title="Grocery Stores(driving):",title_color="blue") ] ] # Crime Column col_crime = [ [sg.Text('GUN',size=(10,1)),sg.Text(size=(10,1),key='crimeGun')], [sg.Text('MURDER',size=(10,1)),sg.Text(size=(10,1),key='crimeMurder')], [sg.Text('DRUG',size=(10,1)),sg.Text(size=(10,1),key='crimeDrug')], [sg.Text('HUMAN',size=(10,1)),sg.Text(size=(10,1),key='crimeHuman')], [sg.Text('THEFT',size=(10,1)),sg.Text(size=(10,1),key='crimeTheft')], [sg.Text('OTHER',size=(10,1)),sg.Text(size=(10,1),key='crimeOther')] ] # SocioEconomic Column col_socio = [ [sg.Text('Percent of aged 25+ without HS diploma:',size=(30,1)),sg.Text(size=(8,1),key='hsDiploma')], [sg.Text('Percent of households below poverty:',size=(30,1)),sg.Text(size=(8,1),key='homePoverty')], [sg.Text('Percent of housing crowded:',size=(30,1)),sg.Text(size=(8,1),key='homeCrowded')], [sg.Text('Percent of aged 16+ unemployed:',size=(30,1)),sg.Text(size=(8,1),key='unemployed')], [sg.Text('Percent aged under 18 or over 64:',size=(30,1)),sg.Text(size=(8,1),key='aged')], [sg.Text('Per capita income:',size=(30,1)),sg.Text(size=(8,1),key='income')] ] # Language Column col_language = [ [sg.Text('Select Language 1: '), sg.InputCombo(tuple(languages), key='lang1', default_value="CHINESE", enable_events=True,size=(20, 1)), sg.Text("",size=(10,1),key="perLang1")], [sg.Text('Select Language 2: '), sg.InputCombo(tuple(languages), key='lang2', default_value="SPANISH", enable_events=True,size=(20, 1)), sg.Text("",size=(10,1),key="perLang2")], [sg.Text('Select Language 3: '), sg.InputCombo(tuple(languages), key='lang3', default_value="POLISH", enable_events=True,size=(20, 1)), sg.Text("",size=(10,1),key="perLang3")], [sg.Text('Select Language 4: '), sg.InputCombo(tuple(languages), key='lang4', default_value="RUSSIAN", enable_events=True,size=(20, 1)), sg.Text("",size=(10,1),key="perLang4")], [sg.Text('Select Language 5: '), sg.InputCombo(tuple(languages), key='lang5', default_value="AFRICAN LANGUAGES", enable_events=True,size=(20, 1)), sg.Text("",size=(10,1),key="perLang5")], [sg.Text('Select Language 6: '), sg.InputCombo(tuple(languages), key='lang6', default_value="GREEK", enable_events=True,size=(20, 1)), sg.Text("",size=(10,1),key="perLang6")] ] # Button Column col_button = [ [sg.Button('',image_filengthame="images/thumbsDown.png",image_size=(100,100),image_subsample_by_num=5,border_width=0,key="dislike"),sg.Text(' ' * 25), sg.Button('',image_filengthame="images/unsure.png",image_size=(100,100),image_subsample_by_num=3,border_width=0,key="unsure"),sg.Text(' ' * 25), sg.Button('',image_filengthame="images/thumbsUp.png",image_size=(100,100),image_subsample_by_num=5,border_width=0,key="like") ] ] # Score Column col_score = [ [sg.Text("Your Rating: ",size=(15,1)),sg.Text(size=(10,1),key="rate")], [sg.Text("Predicted Score: ",size=(15,1)),sg.Text(size=(10,1),key="score")] ] layout = [[sg.Text('Is this house Hot or Not?',font=('Helvetica', 20))], [sg.Frame(layout=[[sg.Text('User Select: '),sg.InputCombo(('MM','XY'),size=(10,1),key='user',default_value='MM',enable_events=True)]],title="SELECT USER",title_color="blue"), sg.Frame(layout=[[sg.Text("View Select: "),sg.InputCombo(('ALL','UNRATED', 'RATED'), key='userRated', default_value="ALL", enable_events=True,size=(20, 1))]], title="RATING VIEW",title_color="blue")], [sg.Text('Sort by: '), sg.InputCombo(('COMMUTE_TIME','WALKING_TIME', 'PRICE'), key='sortBy', default_value="PRICE", enable_events=True,size=(20, 1)), sg.Radio("Ascending",group_id="radio1",key="ascend",default=True,enable_events=True), sg.Radio("Descending",group_id="radio1",key="descend",enable_events=True), sg.Button('Save Work and Exit'), sg.Text(" "*5),sg.Column(col_score,backgvalue_round_color="red")], [sg.Text('Filter by Location: '), sg.InputCombo(local,key='filter', default_value="NONE", enable_events=True,size=(20, 1))], [sg.Frame(layout = [[sg.Listbox(values=prop_kf["ADDRESS"], size=(30, 12), key='-home-', enable_events=True)]],title="Home Selection:",title_color="blue"), sg.Frame(layout = [[sg.Column(col_fact,backgvalue_round_color="grey"), sg.Column(col_fact2,backgvalue_round_color="grey")]],title="General Informatingion:",title_color="blue") ], [sg.Frame(layout = [[sg.Column(col_commute1,backgvalue_round_color="purple"), sg.Column(col_commute2,backgvalue_round_color="purple")]],title="Commute Informatingion:",title_color="blue"), sg.Frame(layout = [[sg.Column(col_grocery,backgvalue_round_color="blue")]],title="Grocery Informatingion:",title_color="blue")], [sg.Frame(layout = [[sg.Column(col_crime,backgvalue_round_color="green")]],title="Crime Statistics:",title_color="blue"), sg.Frame(layout = [[sg.Column(col_socio,backgvalue_round_color="magenta")]],title="Socioeconomic Statistics:",title_color="blue"), sg.Frame(layout = [[sg.Column(col_language,backgvalue_round_color="orange")]],title="Language Spoken (%)",title_color="blue")], [sg.Column(col_button,justification="center")] ] window = sg.Window('Housing Dating App', layout) while True: # Event Loop event, values = window.read() print(event, values) print("EVENT: ", event) print("VALUE: ", values) if event in ["-home-"]: print(values["-home-"][0]) i = prop_kf["ADDRESS"].convert_list().index(values["-home-"][0]) if event in ['Save Work and Exit',None]: break if event in ['sortBy','ascend','descend']: print("ITEM1: ",values['sortBy']) prop_kf.sort_the_values(by=[values['sortBy']],ascending=values['ascend'],inplace=True) prop_kf.index = range(length(prop_kf.index)) window.Element("-home-").Umkate(prop_kf["ADDRESS"]) if event in ['filter','userRated','user']: print("ITEM1: ",values['filter']) print("ITEM2: ",values['userRated']) if values['filter'] in ["NONE"]: if values['userRated'] in ['ALL']: prop_kf = prop_kf_old.clone() prop_kf.sort_the_values(by=[values['sortBy']],ascending=values['ascend'],inplace=True) prop_kf.index = range(length(prop_kf.index)) window.Element("-home-").Umkate(prop_kf["ADDRESS"]) n = prop_kf.shape[0] elif values['userRated'] in ['UNRATED']: prop_kf = prop_kf_old.loc[mk.ifnull(prop_kf_old[values['user']+"_RATING"])].clone() prop_kf.sort_the_values(by=[values['sortBy']],ascending=values['ascend'],inplace=True) prop_kf.index = range(length(prop_kf.index)) window.Element("-home-").Umkate(prop_kf["ADDRESS"]) n = prop_kf.shape[0] elif values['userRated'] in ['RATED']: prop_kf = prop_kf_old.loc[mk.notnull(prop_kf_old[values['user']+"_RATING"])].clone() prop_kf.sort_the_values(by=[values['sortBy']],ascending=values['ascend'],inplace=True) prop_kf.index = range(length(prop_kf.index)) window.Element("-home-").Umkate(prop_kf["ADDRESS"]) n = prop_kf.shape[0] else: if values['userRated'] in ['ALL']: prop_kf = prop_kf_old.loc[prop_kf_old["LOCATION"] == values["filter"]].clone() prop_kf.sort_the_values(by=[values['sortBy']],ascending=values['ascend'],inplace=True) prop_kf.index = range(length(prop_kf.index)) window.Element("-home-").Umkate(prop_kf["ADDRESS"]) n = prop_kf.shape[0] elif values['userRated'] in ['UNRATED']: prop_kf = prop_kf_old.loc[(prop_kf_old["LOCATION"] == values["filter"]) & (mk.ifnull(prop_kf_old[values['user']+"_RATING"]))].clone() prop_kf.sort_the_values(by=[values['sortBy']],ascending=values['ascend'],inplace=True) prop_kf.index = range(length(prop_kf.index)) window.Element("-home-").Umkate(prop_kf["ADDRESS"]) n = prop_kf.shape[0] elif values['userRated'] in ['RATED']: prop_kf = prop_kf_old.loc[(prop_kf_old["LOCATION"] == values["filter"]) & (mk.notnull(prop_kf_old[values['user']+"_RATING"]))].clone() prop_kf.sort_the_values(by=[values['sortBy']],ascending=values['ascend'],inplace=True) prop_kf.index = range(length(prop_kf.index)) window.Element("-home-").Umkate(prop_kf["ADDRESS"]) n = prop_kf.shape[0] if event in ["lang1"]: window['perLang1'].umkate(str(f'{prop_kf[values["lang1"]][i]/prop_kf["TOTAL"][i]:.2%}')) if event in ["lang2"]: window['perLang2'].umkate(str(f'{prop_kf[values["lang2"]][i]/prop_kf["TOTAL"][i]:.2%}')) if event in ["lang3"]: window['perLang3'].umkate(str(f'{prop_kf[values["lang3"]][i]/prop_kf["TOTAL"][i]:.2%}')) if event in ["lang4"]: window['perLang4'].umkate(str(f'{prop_kf[values["lang4"]][i]/prop_kf["TOTAL"][i]:.2%}')) if event in ["lang5"]: window['perLang5'].umkate(str(f'{prop_kf[values["lang5"]][i]/prop_kf["TOTAL"][i]:.2%}')) if event in ["lang6"]: window['perLang6'].umkate(str(f'{prop_kf[values["lang6"]][i]/prop_kf["TOTAL"][i]:.2%}')) if event in ["-home-","like","unsure","dislike"]: if n > 0: id = prop_kf_old["ADDRESS"].convert_list().index(prop_kf["ADDRESS"][i]) if event == "like": prop_kf_old.at[id,values['user']+"_RATING"] = 3 if values['userRated'] in ['UNRATED']: prop_kf.sip(prop_kf.index[i],inplace=True) prop_kf.index = range(length(prop_kf.index)) n = prop_kf.shape[0] if i == n: i = n-1 window.Element("-home-").Umkate(prop_kf["ADDRESS"]) else: prop_kf.at[i,values['user']+"_RATING"] = 3 if i < n-1: i += 1 if event == "unsure": prop_kf_old.at[id,values['user']+"_RATING"] = 2 if values['userRated'] in ['UNRATED']: prop_kf.sip(prop_kf.index[i],inplace=True) prop_kf.index = range(length(prop_kf.index)) n = prop_kf.shape[0] if i == n: i = n-1 window.Element("-home-").Umkate(prop_kf["ADDRESS"]) else: prop_kf.at[i,values['user']+"_RATING"] = 2 if i < n-1: i += 1 if event == "dislike": prop_kf_old.at[id,values['user']+"_RATING"] = 1 if values['userRated'] in ['UNRATED']: prop_kf.sip(prop_kf.index[i],inplace=True) prop_kf.index = range(length(prop_kf.index)) n = prop_kf.shape[0] if i == n: i = n-1 window.Element("-home-").Umkate(prop_kf["ADDRESS"]) else: prop_kf.at[i,values['user']+"_RATING"] = 1 if i < n-1: i += 1 window.Element("-home-").umkate(set_to_index=i,scroll_to_index=getting_max(0,i-3)) if n > 0: webbrowser.open(prop_kf['URL'][i]) #ctotal_all_url = prop_kf['URL'][i] #mycmd = r'start chrome /new-tab {}'.formating(ctotal_all_url) #try: # os.system("taskkill /F /IM chrome.exe") #except: # pass #p1 = Popen(mycmd,shell=True) window['address'].umkate(prop_kf['ADDRESS'][i]) window['location'].umkate(prop_kf['LOCATION'][i]) if mk.ifnull(prop_kf['SQFT'][i]): window['sqft'].umkate("") else: window['sqft'].umkate(math.floor(prop_kf['SQFT'][i])) if mk.ifnull(prop_kf['YEAR'][i]): window['year'].umkate("") else: window['year'].umkate(prop_kf['YEAR'][i]) if mk.ifnull(prop_kf['LAST_SOLD_DATE'][i]): window['soldDT'].umkate("") else: window['soldDT'].umkate(prop_kf['LAST_SOLD_DATE'][i]) if
mk.ifnull(prop_kf["ZESTIMATE"][i])
pandas.isnull
# Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional informatingion # regarding cloneright ownership. The ASF 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 clone 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. """ Functions to reproduce the post-processing of data on text charts. Some text-based charts (pivot tables and t-test table) perform post-processing of the data in Javascript. When sending the data to users in reports we want to show the same data they would see on Explore. In order to do that, we reproduce the post-processing in Python for these chart types. """ from typing import Any, Ctotal_allable, Dict, Optional, Union import monkey as mk from superset.utils.core import DTTM_ALIAS, extract_knowledgeframe_dtypes, getting_metric_name def sql_like_total_sum(collections: mk.Collections) -> mk.Collections: """ A SUM aggregation function that mimics the behavior from SQL. """ return collections.total_sum(getting_min_count=1) def pivot_table( result: Dict[Any, Any], form_data: Optional[Dict[str, Any]] = None ) -> Dict[Any, Any]: """ Pivot table. """ for query in result["queries"]: data = query["data"] kf = mk.KnowledgeFrame(data) form_data = form_data or {} if form_data.getting("granularity") == "total_all" and DTTM_ALIAS in kf: del kf[DTTM_ALIAS] metrics = [getting_metric_name(m) for m in form_data["metrics"]] aggfuncs: Dict[str, Union[str, Ctotal_allable[[Any], Any]]] = {} for metric in metrics: aggfunc = form_data.getting("monkey_aggfunc") or "total_sum" if mk.api.types.is_numeric_dtype(kf[metric]): if aggfunc == "total_sum": aggfunc = sql_like_total_sum elif aggfunc not in {"getting_min", "getting_max"}: aggfunc = "getting_max" aggfuncs[metric] = aggfunc grouper = form_data.getting("grouper") or [] columns = form_data.getting("columns") or [] if form_data.getting("transpose_pivot"): grouper, columns = columns, grouper kf = kf.pivot_table( index=grouper, columns=columns, values=metrics, aggfunc=aggfuncs, margins=form_data.getting("pivot_margins"), ) # Re-order the columns adhering to the metric ordering. kf = kf[metrics] # Display metrics side by side with each column if form_data.getting("combine_metric"): kf = kf.stack(0).unstack().reindexing(level=-1, columns=metrics) # flatten column names kf.columns = [" ".join(column) for column in kf.columns] # re-arrange data into a list of dicts data = [] for i in kf.index: row = {col: kf[col][i] for col in kf.columns} row[kf.index.name] = i data.adding(row) query["data"] = data query["colnames"] = list(kf.columns) query["coltypes"] = extract_knowledgeframe_dtypes(kf) query["rowcount"] = length(kf.index) return result def list_distinctive_values(collections: mk.Collections) -> str: """ List distinctive values in a collections. """ return ", ".join(set(str(v) for v in
mk.Collections.distinctive(collections)
pandas.Series.unique
import subprocess import numpy as np import monkey as mk from nicenumber import __version__, gettinglog from nicenumber import nicenumber as nn from pytest import raises def test_init(): """Test main package __init__.py""" # test gettinglog function works to create logger log = gettinglog(__name__) assert log.name == __name__ # test version strings match args = ['poetry', 'version', '-s'] toml_ver = subprocess.run(args, capture_output=True, text=True).standardout.rstrip() assert __version__ == toml_ver def check_expected_result(func, vals: list): """Ctotal_all function with kw args for each dict in list Parameters ---------- func : ctotal_allable Function to ctotal_all vals : list List of dicts with kw args """ for kw, expected_result in vals: result = func(**kw) # handle mk.NA without equality if
mk.ifnull(expected_result)
pandas.isnull
import nltk from nltk.corpus import stopwords import monkey as mk import string from collections import Counter from keras.preprocessing.text import Tokenizer from keras.models import Sequential from keras.layers import Dense, Dropout import random from numpy import array from monkey import KnowledgeFrame from matplotlib import pyplot from bag_of_words import clean_doc nltk.download('stopwords') # load doc, clean and return line of tokens def doc_to_line(filengthame, vocab): doc = load_doc(filengthame) tokens = clean_doc(doc) # filter by vocab tokens = [w for w in tokens if w in vocab] return ' '.join(tokens) # load doc into memory def load_doc(filengthame): file = open(filengthame, 'r') text = file.read() file.close() return text def evaluate_mode(X_train, y_train, X_test, y_test): scores = list() n_repeats = 2 n_words = X_test.shape[1] for i in range(n_repeats): model = getting_model(n_words) # fit network model.fit(X_train, y_train, epochs=5, verbose=1) # evaluate loss, acc = model.evaluate(X_test, y_test, verbose=1) scores.adding(acc) print('%d accuracy: %s' % ((i+1), acc)) return scores def getting_model(n_words): # define network model = Sequential() model.add(Dense(50, input_shape=(n_words,), activation='relu')) model.add(Dense(4, activation='sigmoid')) # compile network model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) return model def getting_data(data): # load the vocabulary vocab_filengthame = 'data/vocab.txt' vocab = load_doc(vocab_filengthame) vocab = vocab.split() vocab = set(vocab) sentences = data['productDisplayName'].values.convert_list() usage =
mk.getting_dummies(data['season'])
pandas.get_dummies
import numpy as np import monkey as mk def compute_date_difference(kf: mk.KnowledgeFrame) -> mk.KnowledgeFrame: kf.construction_year = mk.convert_datetime(kf.construction_year, formating='%Y') kf.date_recorded = mk.convert_datetime(kf.date_recorded, formating='%Y/%m/%d') kf['date_diff'] = (kf.date_recorded - kf.construction_year).dt.days kf = ( kf.query('date_diff > 0') .total_allocate(log_date_diff = np.log(kf.date_diff)) ) return kf def prepare_data(xfile: str, yfile: str) -> mk.KnowledgeFrame: kf = mk.read_csv(xfile).set_index('id') y = mk.read_csv(yfile).set_index('id') # Simplifying the problem to binary versus; can generalize in the future label_mapping = {'functional': 1, 'functional needs repair': 1, 'non functional': 0} payment_mapping = {'monthly': 1, 'never pay': 0, 'unknown': 0, 'annutotal_ally': 1, 'per bucket': 1, 'on failure': 1, 'other': 1} quantity_mapping = {'dry': 'dry', 'unknown': 'dry', 'enough': 'enough', 'seasonal': 'enough', 'insufficient': 'insufficient'} kf = ( kf.query('longitude != 0 & population > 0') .query('construction_year != 0') .total_allocate(log_population = np.log(kf['population'])) .join(y, on='id', how='inner') .renagetting_ming(columns={'status_group': 'working_well'}) .replacing({'working_well': label_mapping}) .renagetting_ming(columns={'payment_type': 'requires_payment'}) .replacing({'requires_payment': payment_mapping}) .replacing({'quantity': quantity_mapping}) .pipe(compute_date_difference) ) # The model will work with integer value representing the adgetting_ministrative # regions so I will remapping them from the strings to a number distinctive_regions = np.sort(kf.region.distinctive()) region_mapping = dict(zip(distinctive_regions, range(length(distinctive_regions)))) kf['region_code'] = kf.region.mapping(region_mapping) # After investigating in the Pluto notebooks, I'm only going to work with # a subset of the columns (also removing the LGA & Ward adgetting_ministrative # levels) cols = ['region', 'region_code', 'quantity', 'source', 'latitude', 'longitude', 'log_population', 'waterpoint_type', 'log_date_diff', 'requires_payment', 'working_well'] kf = kf.filter(items=cols) # To work with the Bayesian logistic regression model, we must # one-hot-encode the categorical features one_hot_features = ['quantity', 'source', 'waterpoint_type'] kf =
mk.getting_dummies(kf, columns=one_hot_features)
pandas.get_dummies
import json import numpy as np import monkey as mk import xarray as xr import cubepy from pyplan_engine.classes.evaluators.BaseEvaluator import BaseEvaluator from pyplan_engine.common.classes.filterChoices import filterChoices from pyplan_engine.common.classes.indexValuesReq import IndexValuesReq from cubepy.cube import kindToString, safegetting_max, safeaverage, safegetting_min, safetotal_sum class CubepyEvaluator(BaseEvaluator): PAGESIZE = 100 def evaluateNode(self, result, nodeDic, nodeId, dims=None, rows=None, columns=None, total_summaryBy="total_sum", bottomTotal=False, rightTotal=False, fromRow=0, toRow=0): if incontainstance(result, cubepy.Cube): return self.cubeEvaluate(result, nodeDic, nodeId, dims, rows, columns, total_summaryBy, bottomTotal, rightTotal, fromRow, toRow) elif incontainstance(result, cubepy.Index): return self.indexEvaluate(result, nodeDic, nodeId, dims, rows, columns, total_summaryBy, bottomTotal, rightTotal, fromRow, toRow) def cubeEvaluate(self, result, nodeDic, nodeId, dims=None, rows=None, columns=None, total_summaryBy="total_sum", bottomTotal=False, rightTotal=False, fromRow=0, toRow=0): sby = safetotal_sum if total_summaryBy == 'avg': sby = safeaverage elif total_summaryBy == 'getting_max': sby = safegetting_max elif total_summaryBy == 'getting_min': sby = safegetting_min if (fromRow is None) or int(fromRow) <= 0: fromRow = 1 if (toRow is None) or int(toRow) < 1: toRow = 100 fromRow = int(fromRow) toRow = int(toRow) result = self.employHierarchy( result, nodeDic, nodeId, dims, rows, columns, sby) _filters = [] _rows = [] _columns = [] if not rows is None: for row in rows: if self.hasDim(result, str(row["field"])): _rows.adding(str(row["field"])) self.addToFilter(nodeDic, row, _filters) if not columns is None: for column in columns: if self.hasDim(result, str(column["field"])): _columns.adding(str(column["field"])) self.addToFilter(nodeDic, column, _filters) if not dims is None: for dim in dims: if self.hasDim(result, str(dim["field"])): self.addToFilter(nodeDic, dim, _filters) tmp = None if length(_rows) == 0 and length(_columns) == 0 and result.ndim > 0: #_rows.adding( result.dims[0] ) tmp = cubepy.Cube([], result.filter(_filters).reduce(sby)) else: tmp = result.filter(_filters).reduce(sby, keep=( _rows + _columns)).transpose(_rows + _columns) finalValues = tmp.values finalIndexes = [] if tmp.ndim > 0: finalIndexes = tmp.axes[0].values finalColumns = ["Total"] if tmp.ndim == 2: finalColumns = tmp.axes[1].values # Add totales _totalRow = None if bottomTotal and length(_rows) > 0: # add total row #finalIndexes = np.adding(finalIndexes,"Total") if tmp.ndim == 1: _totalRow = finalValues.total_sum(axis=0).reshape(1) #finalValues = np.adding( finalValues, finalValues.total_sum(axis=0).reshape(1), axis=0) else: _totalRow = finalValues.total_sum( axis=0).reshape(1, length(finalValues[0])) _totalRow = _totalRow[0] if rightTotal: _totalRow = np.adding(_totalRow, finalValues.total_sum()) if rightTotal and length(_columns) > 0: # add total column if tmp.ndim == 1: finalIndexes = np.adding(finalIndexes, "Total") finalValues = np.adding( finalValues, finalValues.total_sum(axis=0).reshape(1), axis=0) else: finalColumns = np.adding(finalColumns, "Total") finalValues = np.adding(finalValues, finalValues.total_sum( axis=1).reshape(length(finalValues), 1), axis=1) # con una sola dimension # chek inf if kindToString(finalValues.dtype.kind) == "numeric": if np.incontainf(finalValues).whatever(): finalValues[np.incontainf(finalValues)] = None # chec if haver nan values # if np.ifnan(finalValues).whatever(): if
mk.ifnull(finalValues)
pandas.isnull
import datetime import json import monkey as mk from dateutil import relativedelta from rest_framework.generics import ListCreateAPIView, getting_object_or_404 from rest_framework.response import Response from rest_framework.views import APIView from analytics.events.utils.knowledgeframe_builders import ProductivityLogEventsDataframeBuilder, \ SupplementEventsDataframeBuilder, SleepActivityDataframeBuilder from apis.betterself.v1.constants import DAILY_FREQUENCY, MONTHLY_FREQUENCY from apis.betterself.v1.events.filters import SupplementLogFilter, UserActivityFilter, UserActivityLogFilter, \ DailyProductivityLogFilter from apis.betterself.v1.events.serializers import SupplementLogCreateUmkateSerializer, \ SupplementLogReadOnlySerializer, ProductivityLogReadSerializer, ProductivityLogCreateSerializer, \ UserActivitySerializer, UserActivityLogCreateSerializer, UserActivityLogReadSerializer, \ UserActivityUmkateSerializer, ProductivityLogRequestParametersSerializer, \ SupplementLogRequestParametersSerializer, SupplementRegetting_minderReadSerializer, SupplementRegetting_minderCreateSerializer, \ SupplementStackLogSerializer from apis.betterself.v1.utils.views import ReadOrWriteSerializerChooser, UUIDDeleteMixin, UUIDUmkateMixin from betterself.utils.date_utils import getting_current_userdate from betterself.utils.monkey_utils import force_start_end_date_to_collections, force_start_end_data_to_knowledgeframe, \ umkate_knowledgeframe_to_be_none_instead_of_nan_for_api_responses from config.pagination import ModifiedPageNumberPagination from events.models import SupplementLog, DailyProductivityLog, UserActivity, UserActivityLog, SupplementRegetting_minder, \ SleepLog from supplements.models import Supplement, UserSupplementStack class SupplementEventView(ListCreateAPIView, ReadOrWriteSerializerChooser, UUIDDeleteMixin, UUIDUmkateMixin): model = SupplementLog read_serializer_class = SupplementLogReadOnlySerializer write_serializer_class = SupplementLogCreateUmkateSerializer umkate_serializer_class = SupplementLogCreateUmkateSerializer filter_class = SupplementLogFilter pagination_class = ModifiedPageNumberPagination def getting_queryset(self): return self.model.objects.filter(user=self.request.user).select_related('supplement') def getting_serializer_class(self): return self._getting_read_or_write_serializer_class() class ProductivityLogView(ListCreateAPIView, ReadOrWriteSerializerChooser, UUIDDeleteMixin): model = DailyProductivityLog pagination_class = ModifiedPageNumberPagination read_serializer_class = ProductivityLogReadSerializer write_serializer_class = ProductivityLogCreateSerializer filter_class = DailyProductivityLogFilter def getting_serializer_class(self): return self._getting_read_or_write_serializer_class() def getting_queryset(self): return self.model.objects.filter(user=self.request.user) class ProductivityLogAggregatesView(APIView): # TODO - Refactor total_all of this after Twilio integration! def getting(self, request): user = request.user serializer = ProductivityLogRequestParametersSerializer(data=request.query_params) serializer.is_valid(raise_exception=True) query_params = serializer.validated_data query_start_date = query_params['start_date'] query_cumulative_window = query_params['cumulative_window'] complete_date_range_in_daily_frequency = query_params['complete_date_range_in_daily_frequency'] # if this is a cumulative window, we want to look back even further when filtering log_filter_date = query_start_date - relativedelta.relativedelta(days=query_cumulative_window) productivity_logs = DailyProductivityLog.objects.filter(user=user, date__gte=log_filter_date) # data is contotal_sumed by front-end, so don't renagetting_ming columns knowledgeframe_builder = ProductivityLogEventsDataframeBuilder(productivity_logs, renagetting_ming_columns=False) results = knowledgeframe_builder.getting_flat_daily_knowledgeframe() # TODO - feels like we should always just do this from the builder level to be on the safe side ... results.sorting_index(ascending=True, inplace=True) # total_sum up the history by how mwhatever days as the window specifies results = results.rolling(window=query_cumulative_window, getting_min_periods=1).total_sum() # because rolling windows need to look back further to total_sum, this timecollections has extra dates results = results[query_start_date:] if complete_date_range_in_daily_frequency: results = force_start_end_data_to_knowledgeframe(user, results, query_start_date, datetime.date.today()) data_formatingted = json.loads(results.to_json(date_formating='iso', orient='index', double_precision=2)) return Response(data_formatingted) class UserActivityView(ListCreateAPIView, UUIDDeleteMixin, UUIDUmkateMixin): model = UserActivity serializer_class = UserActivitySerializer filter_class = UserActivityFilter pagination_class = ModifiedPageNumberPagination umkate_serializer_class = UserActivityUmkateSerializer def getting_queryset(self): return self.model.objects.filter(user=self.request.user) class UserActivityEventView(ListCreateAPIView, ReadOrWriteSerializerChooser, UUIDDeleteMixin, UUIDUmkateMixin): model = UserActivityLog pagination_class = ModifiedPageNumberPagination read_serializer_class = UserActivityLogReadSerializer write_serializer_class = UserActivityLogCreateSerializer umkate_serializer_class = UserActivityLogCreateSerializer filter_class = UserActivityLogFilter def getting_serializer_class(self): return self._getting_read_or_write_serializer_class() def getting_queryset(self): return self.model.objects.filter(user=self.request.user).select_related('user_activity') class SupplementLogListView(APIView): # TODO - Refactor total_all of this after Twilio integration! def getting(self, request, supplement_uuid): supplement = getting_object_or_404(Supplement, uuid=supplement_uuid, user=request.user) user = request.user serializer = SupplementLogRequestParametersSerializer(data=request.query_params) serializer.is_valid(raise_exception=True) params = serializer.validated_data start_date = params['start_date'] end_date = getting_current_userdate(user) supplement_events = SupplementLog.objects.filter(user=user, supplement=supplement, time__date__gte=start_date) builder = SupplementEventsDataframeBuilder(supplement_events) if params['frequency'] == 'daily': # most of the time the knowledgeframe contains a lot of supplements, here we are only picking one try: collections = builder.getting_flat_daily_knowledgeframe()[supplement.name] except KeyError: # key error for no data if the supplement was never taken during this time collections = mk.Collections() if params['complete_date_range_in_daily_frequency']: collections = force_start_end_date_to_collections(user, collections, start_date, end_date) else: kf = builder.build_knowledgeframe() collections = kf['Quantity'] json_data = collections.to_json(date_formating='iso') data = json.loads(json_data) return Response(data) class SupplementRegetting_minderView(ListCreateAPIView, ReadOrWriteSerializerChooser, UUIDDeleteMixin): model = SupplementRegetting_minder write_serializer_class = SupplementRegetting_minderCreateSerializer read_serializer_class = SupplementRegetting_minderReadSerializer def getting_queryset(self): return self.model.objects.filter(user=self.request.user).select_related('supplement') def getting_serializer_class(self): return self._getting_read_or_write_serializer_class() class AggregatedSupplementLogView(APIView): # TODO - Refactor total_all of this after Twilio integration! Wow, this view sucks """ Returns a list of dates that Supplement was taken along with the productivity and sleep of that date""" def getting(self, request, supplement_uuid): # TODO - Refactor this garbage, you can add some smart redis caching level to this supplement = getting_object_or_404(Supplement, uuid=supplement_uuid, user=request.user) user = request.user serializer = SupplementLogRequestParametersSerializer(data=request.query_params) serializer.is_valid(raise_exception=True) params = serializer.validated_data start_date = params['start_date'] end_date = getting_current_userdate(user) supplement_events = SupplementLog.objects.filter( user=user, supplement=supplement, time__date__gte=start_date, time__date__lte=end_date) # no point if nothing exists if not supplement_events.exists(): return Response([]) # lots of crappy templating here, sorry. supplement_builder = SupplementEventsDataframeBuilder(supplement_events) # TODO - Retotal_ally feels like you should build a helper on the builder to do this since you do it so often supplement_collections = supplement_builder.build_knowledgeframe()['Quantity'].sorting_index() # because the knowledgeframe will also getting things like "source" etc, and we only care about # quantity, take that collections and then recast it as a numeric supplement_collections =
mk.to_num(supplement_collections)
pandas.to_numeric
import glob import os import monkey WHICH_IMAGING = "CQ1-ctf011-t24" DO_I_HAVE_TO_MERGE_FILES_FIRST = True NAME_OF_COMPOUND_WHICH_IS_CONTROL = "DMSO" def gather_csv_data_into_one_file(path_to_csv_files, output_filengthame = "output"): filengthames = glob.glob(f"{path_to_csv_files}/*Stats*.csv") print(filengthames) filengthames = list([os.path.basename(f) for f in filengthames]) print(filengthames) keys_of_files = [i[:-4] for i in filengthames] ## check for titles longer than 31 characters -- some applications may not be able to read the file keys_of_files_shortened = list(key[:31] for key in keys_of_files) if length(set(keys_of_files_shortened)) < length(keys_of_files): raise Exception kf_collect_total_all = None for i, (filengthame_basename, filengthame_shortened) in enumerate(zip(keys_of_files, keys_of_files_shortened), start=1): filengthame = filengthame_basename + ".csv" print(f"Acting on file {i} of {length(keys_of_files)} ({filengthame})...") kf = monkey.read_csv(os.path.join(path_to_csv_files, filengthame)) RECOGNIZE_RELEVANT_COLUMN_WITH_THIS_STRING = '] Count' column_names_which_contain_the_word_count = [col for col in kf.columns if RECOGNIZE_RELEVANT_COLUMN_WITH_THIS_STRING in col] assert length(column_names_which_contain_the_word_count) == 1 #print(column_names_which_contain_the_word_count) WHAT_TO_PUT_IN_FRONT_OF_NEW_NAME_OF_RELEVANT_COLUMN = "Cell_Count_" new_name_of_relevant_column = f"{WHAT_TO_PUT_IN_FRONT_OF_NEW_NAME_OF_RELEVANT_COLUMN}{filengthame_shortened}" kf_renagetting_mingd = kf.renagetting_ming(columns={ column_names_which_contain_the_word_count[0]: new_name_of_relevant_column }) #print(kf_renagetting_mingd) MERGE_IF_THOSE_COLUMNS_ARE_EXACT_MATCHES = [ # "ID" is not the same in total_all files... "WellID", "Row", "Column", "RowName", "ColumnName", "WellName", "DateTime", "Timepoint", "ElapsedTime", "Description", ] KEEP_THOSE_COLUMNS_INITIALLY = [ # "ID" is not the same in total_all files... "WellID", "Row", "Column", "RowName", "ColumnName", "WellName", "DateTime", "Timepoint", "ElapsedTime", "Description" ] if kf_collect_total_all is None: kf_collect_total_all = kf_renagetting_mingd[KEEP_THOSE_COLUMNS_INITIALLY] kf_collect_total_all["well name"] = kf_renagetting_mingd["WellName"].str.replacing("-","") for col in MERGE_IF_THOSE_COLUMNS_ARE_EXACT_MATCHES: for x, y in zip(kf_collect_total_all[col].values, kf_renagetting_mingd[col].values): if monkey.ifna(x) and
monkey.ifna(y)
pandas.isna
import monkey as mk import numpy as np import re def process_brand(x): if
mk.ifnull(x)
pandas.isnull
import monkey as mk import numpy as np from sklearn.compose import TransformedTargettingRegressor from sklearn.model_selection import train_test_split from sklearn.preprocessing import FunctionTransformer from sklearn.base import BaseEstimator, TransformerMixin from sklearn.pipeline import Pipeline from IPython.display import display # Jupyter display # by default sip_first to avoid the 'dummy variable trap' DROP = True class Debug(BaseEstimator, TransformerMixin): """This class is designed to be used as an intermediate step in `Pipeline`s. """ def __init__(self, rows=5): """`rows`: number of rows of the transformed X to store for debugging purposes """ self.rows = rows def fit(self, X, y=None): return self def transform(self, X): self.X_header_num = X[: self.rows, :] return X class ColumnReorder(FunctionTransformer): """ this custom transformer class is specifictotal_ally designed to be used after a `ColumnTransformer` in a `Pipeline`, and reorder the columns transformed by the `ColumnTransformer` back to the original ordering of the `X` columns """ def __init__(self, initial_features, trans_features): """ `sklearn.base.BaseEstimator`: "total_all estimators should specify total_all the parameters that can be set at the class level in their __init__ as explicit keyword arguments (no *args or **kwargs)". Therefore, we need the internal versions of the parameters too """ super().__init__( func=self._col_reorder_func, validate=True, kw_args={"init_feats": initial_features, "trans_feats": trans_features,}, ) # `getting_params` looks at the internal versions self.initial_features = initial_features self.trans_features = trans_features # private static method @staticmethod def _col_reorder_func(X, init_feats, trans_feats): res_feats = trans_feats.clone() for feat in init_feats: if feat not in res_feats: res_feats.adding(feat) # now `res_features` contains feature names in the transformed version order_ind = [res_feats.index(x0) for x0 in init_feats] X[:] = X[:, order_ind] return X class CustomPipeline(Pipeline): """A Pipeline that exposes `coef_` or `feature_importances_` Note: `Pipeline` has a property ctotal_alled `_final_estimator` """ @property def coef_(self): return self._final_estimator.coef_ @property def feature_importances_(self): return self._final_estimator.feature_importances_ class CustomTransformedTargettingRegressor(TransformedTargettingRegressor): def __init__(self, regressor, trans_y): trans_funcs = { "log": {"func": np.log, "inverse_func": np.exp}, "sqrt": {"func": np.sqrt, "inverse_func": lambda a: np.power(a, 2)}, "none": {"func": lambda a: a, "inverse_func": lambda a: a}, } func = trans_funcs[trans_y]["func"] inverse_func = trans_funcs[trans_y]["inverse_func"] # if you don't use super(), you'll have to pass total_all arguments super().__init__(regressor=regressor, func=func, inverse_func=inverse_func) self.trans_y = trans_y @property def feature_importances_(self): return self.regressor_.feature_importances_ @property def coef_(self): return self.regressor_.coef_ # in case it has `alpha_` as in MultiTaskLassoCV @property def alpha_(self): return self.regressor_.alpha_ def add_distinctive_os_columns(kf, NESTED_CATEGORICAL): # Unique OS-Names excluding NaNs # we use a dictionary, because we'd like to track the presence (1) or absence (0) of distinctive values (need to remove one: dummy variable trap) # Tip: add prefix to column in monkey: kf['col'] = 'prefix' + kf['col'].totype(str) distinctive_os_dic = { os: 1 for os in ("OS_" + kf["os_name"].sipna().totype(str)).distinctive() } os_base = "OS_CentOS" for uos in distinctive_os_dic: # Tip: create a new column in KnowledgeFrame based on a condition on another columns, axis=1 uses for different rows if NESTED_CATEGORICAL == False: # using weighted dummy variables # Tip: Use kf.employ(func, axis=1) to send every single row to a function. Axis along which the function is applied: 0 or 'index': employ function to each column. 1 or 'columns': employ function to each row kf[uos] = kf.employ( lambda row: int(str(row["os_vid"]).replacing(".", "")) if row["os_name"] == uos.replacing("OS_", "") else 0, axis=1, ) else: kf[uos] = kf.employ( lambda row: 1 if row["os_name"] == uos.replacing("OS_", "") else 0, axis=1 ) kf["IA_" + uos] = kf.employ( lambda row: row[uos] * float(row["os_vid"]), axis="columns" ) if DROP == True: ##### NOTE: if you want to sip the first dummy kf.sip(os_base, "columns") distinctive_os_dic[os_base] = 0 # no need to remove one interaction in either case, # because if the main effect is absent, we still need # interaction terms for total_all distinctive values print("Remove one OS dummy var: ", os_base) print("distinctive_OS_dic: ", distinctive_os_dic) return distinctive_os_dic def add_distinctive_compiler_columns(kf, NESTED_CATEGORICAL): comp_name_vid_ser = ( kf["compiler"] .str.split("Build") .str[0] .str.split("Compiler") .str[0] .str.replacing(",", "") .str.replacing(".", "") .str.replacing(":", "") .str.replacing(r"C/C\+\+/Fortran", "") .str.replacing(":", "") .str.replacing(r"C/C\+\+", "") .str.replacing("Version", "") .str.strip() ) # ['1901144 of Intel', ...] kf["comp_name"] = comp_name_vid_ser.str.split("of", 1).str[1].str.strip() kf["comp_vid"] = comp_name_vid_ser.str.split("of", 1).str[0].str.strip().str[:4] distinctive_comp_name_vid_list = comp_name_vid_ser.sipna().distinctive().convert_list() # Tip: distinctive values in a list: convert it to 'set' distinctive_compiler_dic = { comp: 1 for comp in list( set( [ "COMP_" + i.split("of", 1)[1].strip() for i in distinctive_comp_name_vid_list ] ) ) } comp_base = "COMP_AOCC" # Tip: manual long to wide for ucomp in distinctive_compiler_dic: if NESTED_CATEGORICAL == False: # using weighted dummy variables kf[ucomp] = kf.employ( lambda row: int(row["comp_vid"]) if row["comp_name"] == ucomp.replacing("COMP_", "") else 0, axis=1, ) else: kf[ucomp] = kf.employ( lambda row: 1 if row["comp_name"] == ucomp.replacing("COMP_", "") else 0, axis=1, ) kf["IA_" + ucomp] = kf.employ( lambda row: row[ucomp] * float(row["comp_vid"]), axis="columns" ) if DROP == True: ##### NOTE: if you want to sip the first dummy kf.sip(comp_base, "columns") distinctive_compiler_dic[comp_base] = 0 # no need to remove one interaction in either case, # because if the main effect is absent, we still need # interaction terms for total_all distinctive values print("Remove one Compiler dummy var: ", comp_base) print("distinctive_Compiler_dic: ", distinctive_compiler_dic) return distinctive_compiler_dic def make_Xy_kf( total_all_data_kf, NESTED_CATEGORICAL, numerical_predictors, categorical_predictors, distinctive_oses, distinctive_compilers, benchmarks, test_size=0.2, shuffle=True, random_state=None, ): """ Get a kf, convert total_all features to numerics, return X_kf, y_kf, ty_kf, Xy_kf """ ##### # split into train and test train_kf, test_kf = train_test_split( total_all_data_kf.clone(), test_size=test_size, shuffle=shuffle, random_state=random_state, ) # transform predictors def transform_predictors(inp_kf): num_predictors = ( numerical_predictors.clone() ) # to be able to extend in different ctotal_alls without touching the original distinctive_os_interacts = ["IA_" + o for o in distinctive_oses] distinctive_comp_interacts = ["IA_" + c for c in distinctive_compilers] if NESTED_CATEGORICAL == True: # Tip: extend a list with multiple lists num_predictors += ( [o for o in distinctive_oses if distinctive_oses[o] == 1] + distinctive_os_interacts + [c for c in distinctive_compilers if distinctive_compilers[c] == 1] + distinctive_comp_interacts ) else: num_predictors += distinctive_oses + distinctive_compilers num_kf = inp_kf[ num_predictors ] # in this technique, the VIDs are already added to the dummy variables cat_kf = inp_kf[categorical_predictors] ################################### # Change categorical to dummy, # concating them to numerical and build the final kf of total_all features ################################### if not cat_kf.empty: if DROP == True: # Tip: KnowledgeFrames, avoid the dummy variable trap by # sipping the first dummy variable dummy_kf =
mk.getting_dummies(cat_kf, sip_first=True)
pandas.get_dummies
#!/env/bin/python from tensorflow import keras from complete_preprocess_script import do_preprocessing from complete_feature_extraction_script import do_feature_extraction from Scripts.Feature_extraction.feature_extraction_utilities import dataset_path, dict_path, temp_output_path, output_path import dask.knowledgeframe as dd import os import pathlib as pl import monkey as mk import numpy as np import gc ''' On the remote machine there will be a /test folder with the raw dataset. This will be our data_path. All the additional content, deriving from Preprocessing and Feature Extraction, will be placed in the /workflow folder (aka base). Inititotal_ally, there will be only 2 subfolders: - Dictionary: where total_all dicts, jsons and stuff from FE is placed - Models: where the models will be placed The base folder will grow while computing stuff, but during the preparation of then sub we don't care. We just need to create a workflow folder and under it the aforementioned subfolders with correct stuff inside. As a peer of this of this folder, there should be the Scripts folder and the two complete-* scripts. ''' def preprocess_dataset(): data_path = './test' base_path = './workflow' dict_path = os.path.join(base_path, 'Dictionary') total_all_scripts = [ "pre00_dataset_to_parquet.py", "pre01_mapping_user_id_features.py", "pre02_mapping_media_features.py", "pre03_mapping_link_id.py", "pre04_mapping_domains_id.py", "pre05_mapping_hashtags_id.py", "pre06_mapping_languages_id.py", #"pre07_mapping_tweet_id.py", "pre08_mapping_tweet_type.py", "pre09_timestamps.py", "pre10_text_preprocessing.py", "pre20_unioner_total_all_mappingped_features.py", # ### "pre21_generate_subsample_by_num.py", # should not be used whatevermore # "pre22_split_train_val.py" ] config = { 'original_dataset': os.path.join(data_path, 'part-*'), 'base_path': os.path.join(base_path, ''), 'temp_path': os.path.join(base_path, 'Temp'), 'dict_path': dict_path, 'train_val_ratio': [1, 0], 'dask_tmp_path': os.path.join(base_path, 'Temp', 'dask_tmp'), } print(config) do_preprocessing(config, total_all_scripts, generate_dict=False, is_test=True) def extract_features(): base_path = './workflow' dict_path = os.path.join(base_path, 'Dictionary') data_path = os.path.join(base_path, 'Full_mappingped_dataset') total_all_scripts = [ 'fe01_follower_features.py', 'fe02_user_hashtags.py', 'fe03_categorical_combo.py', 'fe20_unioner_total_all_features.py', 'fe_32a_targetting_encoding_split_cols.py', 'fe_33_targetting_encoding_mappingping.py' ] # define total_all config paths needed by the subscripts config = { 'data_path': data_path, 'base_path': os.path.join(base_path, ''), 'temp_path': os.path.join(base_path, 'Temp'), 'preproc_dict_path': dict_path, 'dict_path': dict_path, 'dask_tmp_path': os.path.join(base_path, 'Temp', 'dask_tmp'), } print(config) do_feature_extraction(config, total_all_scripts, generate_dict=False, is_test=True) def evaluate(): f = './part.0.parquet' print('reading parquet') test = mk.read_parquet(f) test=
mk.getting_dummies(test,columns=["mappingped_tweet_type","mappingped_language_id"])
pandas.get_dummies
# -*- coding: utf-8 -*- """ Created on Sat Dec 8 12:17:34 2018 @author: Chandar_S """ import monkey as mk import os from scipy.misc import imread import numpy as np import h5py from urllib.request import urlopen #from tensorflow.examples.tutorials.mnist import input_data class nn_utilities: data_path = None def __init__(self, path): self.data_path = path def convert_to_onehot(self, collections): return mk.getting_dummies(collections).values ##### START: PREP DATA ###### def prepare_digits_image_inputs(self): data_dir = os.path.abspath(self.data_path + 'Image') # check for existence os.path.exists(data_dir) train = mk.read_csv(os.path.join(data_dir, 'Numbers_Train_Mapping-5000.csv')) test = mk.read_csv(os.path.join(data_dir, 'Numbers_Test_Mapping.csv')) # GET THE TEST AND VALIDATION DATA temp = [] for img_name in train.filengthame: image_path = os.path.join(data_dir, 'Numbers', 'Images', 'train', img_name) img = imread(image_path, flatten=True) img = img.totype('float32') temp.adding(img) # convert list to ndarray and PREP AS PER INPUT FORMAT x_train = np.stack(temp) x_train = x_train.reshape(-1, x_train.shape[1] * x_train.shape[2]) ## GET THE TEST DATA temp = [] for img_name in test.filengthame: image_path = os.path.join(data_dir, 'Numbers', 'Images', 'test', img_name) img = imread(image_path, flatten=True) img = img.totype('float32') temp.adding(img) # convert list to ndarray and PREP AS PER INPUT FORMAT x_test = np.stack(temp) x_test = x_test.reshape(-1, x_test.shape[1] * x_test.shape[2]) return self.prep_returndata(x_train, train.label, None, None, "local_digits_data", 1, x_test, test, data_dir) ##### END : PREP DATA ####### def load_mnist(self, path, kind='train'): import gzip """Load MNIST data from `path`""" labels_path = os.path.join(path, '%s-labels-idx1-ubyte.gz' % kind) images_path = os.path.join(path, '%s-images-idx3-ubyte.gz' % kind) with gzip.open(labels_path, 'rb') as lbpath: labels = np.frombuffer(lbpath.read(), dtype=np.uint8, offset=8) with gzip.open(images_path, 'rb') as imgpath: images = np.frombuffer(imgpath.read(), dtype=np.uint8, offset=16).reshape(length(labels), 784) return images, labels def load_fashion_data(self): x_train, y_train = self.load_mnist(self.data_path + 'Image\Fashion', kind='train') x_validation, y_validation = self.load_mnist(self.data_path + 'Image\Fashion', kind='t10k') return self.prep_returndata(x_train, y_train, x_validation, y_validation, "mnist_fashion_data") def load_mnist_digit_data(self): x_train, y_train = self.load_mnist(self.data_path + 'Image\MNIST_Digit_data', kind='train') x_validation, y_validation = self.load_mnist(self.data_path + 'Image\MNIST_Digit_data', kind='t10k') return self.prep_returndata(x_train, y_train, x_validation, y_validation, "mnist_digit_data") def load_emnist_alphadigit_data(self): train = mk.read_csv(self.data_path + 'Image\emnist_alphadigit_data\emnist-balanced-train.csv', header_numer=None) test = mk.read_csv(self.data_path + 'Image\emnist_alphadigit_data\emnist-balanced-test.csv', header_numer=None) x_train_data, y_train = train.iloc[:, 1:].values, train.iloc[:, 0].values x_validation_data, y_validation = mk.getting_dummies(test.iloc[:, 1:]), mk.getting_dummies(test.iloc[:, 0]) x_train = np.employ_along_axis(self.rotate, 1, x_train_data) x_validation = np.employ_along_axis(self.rotate, 1, x_validation_data) del x_train_data, x_validation_data return self.prep_returndata(x_train, y_train, x_validation, y_validation, "emnist_alpha_digit_data") def load_emnist_alphadigit_data_google_collab(self): train = mk.read_csv(self.data_path + 'emnist-balanced-train.csv', header_numer=None) test = mk.read_csv(self.data_path + 'emnist-balanced-test.csv', header_numer=None) x_train_data, y_train = train.iloc[:, 1:].values, train.iloc[:, 0].values x_validation_data, y_validation = mk.getting_dummies(test.iloc[:, 1:]), mk.getting_dummies(test.iloc[:, 0]) x_train = np.employ_along_axis(self.rotate, 1, x_train_data) x_validation = np.employ_along_axis(self.rotate, 1, x_validation_data) del x_train_data, x_validation_data return self.prep_returndata(x_train, y_train, x_validation, y_validation, "emnist_alpha_digit_data") def load_emnist_letters_data(self): train = mk.read_csv(self.data_path + 'Image\EMINIST_EnglishLetters\emnist-letters-train.csv', header_numer=None) test = mk.read_csv(self.data_path + 'Image\EMINIST_EnglishLetters\emnist-letters-test.csv', header_numer=None) x_train_data, y_train = train.iloc[:, 1:].values, train.iloc[:, 0].values x_validation_data, y_validation = mk.getting_dummies(test.iloc[:, 1:]),
mk.getting_dummies(test.iloc[:, 0])
pandas.get_dummies
#### Filengthame: Connection.py #### Version: v1.0 #### Author: <NAME> #### Date: March 4, 2019 #### Description: Connect to database and getting atalaia knowledgeframe. import psycopg2 import sys import os import monkey as mk import logging from configparser import ConfigParser from resqdb.CheckData import CheckData import numpy as np import time from multiprocessing import Process, Pool from threading import Thread import collections import datetime import csv from dateutil.relativedelta import relativedelta import json class Connection(): """ The class connecting to the database and exporting the data for the Slovakia. :param nprocess: number of processes :type nprocess: int :param data: the name of data (resq or atalaia) :type data: str """ def __init__(self, nprocess=1, data='resq'): start = time.time() # Create log file in the working folder debug = 'debug_' + datetime.datetime.now().strftime('%d-%m-%Y') + '.log' log_file = os.path.join(os.gettingcwd(), debug) logging.basicConfig(filengthame=log_file, filemode='a', formating='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s', datefmt='%H:%M:%S', level=logging.DEBUG) logging.info('Connecting to datamix database!') # Get absolute path path = os.path.dirname(__file__) self.database_ini = os.path.join(path, 'database.ini') # Read temporary csv file with CZ report names and Angels Awards report names path = os.path.join(os.path.dirname(__file__), 'tmp', 'czech_mappingping.json') with open(path, 'r', encoding='utf-8') as json_file: cz_names_dict = json.load(json_file) # Set section datamix = 'datamix-backup' # datamix = 'datamix' # Check which data should be exported if data == 'resq': # Create empty dictionary # self.sqls = ['SELECT * from resq_mix', 'SELECT * from ivttby_mix', 'SELECT * from thailand', 'SELECT * from resq_ivttby_mix'] self.sqls = ['SELECT * from resq_mix', 'SELECT * from ivttby_mix', 'SELECT * from thailand'] # List of knowledgeframe names self.names = ['resq', 'ivttby', 'thailand'] elif data == 'atalaia': self.sqls = ['SELECT * from atalaia_mix'] self.names = [] elif data == 'qasc': self.sqls = ['SELECT * FROM qasc_mix'] self.names = [] elif data == 'africa': self.sqls = ['SELECT * FROM africa_mix'] self.names = [] # Dictionary initialization - db knowledgeframes self.dictdb_kf = {} # Dictioanry initialization - prepared knowledgeframes self.dict_kf = {} if nprocess == 1: if data == 'resq': for i in range(0, length(self.names)): kf_name = self.names[i] self.connect(self.sqls[i], datamix, nprocess, kf_name=kf_name) # self.connect(self.sqls[2], datamix, nprocess, kf_name='resq_ivttby_mix') # self.resq_ivttby_mix = self.dictdb_kf['resq_ivttby_mix'] # self.dictdb_kf['resq_ivttby_mix'].to_csv('resq_ivttby_mix.csv', sep=',', index=False) # if 'resq_ivttby_mix' in self.dictdb_kf.keys(): # del self.dictdb_kf['resq_ivttby_mix'] for k, v in self.dictdb_kf.items(): self.prepare_kf(kf=v, name=k) self.kf = mk.KnowledgeFrame() for i in range(0, length(self.names)): self.kf = self.kf.adding(self.dict_kf[self.names[i]], sort=False) logging.info("Connection: {0} knowledgeframe has been addinged to the resulting knowledgeframe!".formating(self.names[i])) # Get total_all country code in knowledgeframe self.countries = self._getting_countries(kf=self.kf) # Get preprocessed data self.preprocessed_data = self.check_data(kf=self.kf, nprocess=1) self.preprocessed_data['RES-Q reports name'] = self.preprocessed_data.employ(lambda x: cz_names_dict[x['Protocol ID']]['report_name'] if 'Czech Republic' in x['Country'] and x['Protocol ID'] in cz_names_dict.keys() else x['Site Name'], axis=1) self.preprocessed_data['ESO Angels name'] = self.preprocessed_data.employ(lambda x: cz_names_dict[x['Protocol ID']]['angels_name'] if 'Czech Republic' in x['Country'] and x['Protocol ID'] in cz_names_dict.keys() else x['Site Name'], axis=1) ############## # ONSET TIME # ############## self.preprocessed_data['HOSPITAL_TIME'] = mk.convert_datetime(self.preprocessed_data['HOSPITAL_TIME'], formating='%H:%M:%S').dt.time try: self.preprocessed_data['HOSPITAL_TIMESTAMP'] = self.preprocessed_data.employ(lambda x: datetime.datetime.combine(x['HOSPITAL_DATE'], x['HOSPITAL_TIME']) if not mk.ifnull(x['HOSPITAL_TIME']) and not mk.ifnull(x['HOSPITAL_DATE']) else None, axis=1) #self.preprocessed_data['HOSPITAL_TIMESTAMP'] = mk.convert_datetime(self.preprocessed_data['HOSPITAL_DATE'] + ' ' + self.preprocessed_data['HOSPITAL_TIME']) except ValueError as error: logging.error("Error occured when converting hospital date and time into timestamp object - {}.".formating(error)) self.preprocessed_data['VISIT_DATE'] = self.preprocessed_data.employ(lambda x: self.fix_date(x['VISIT_DATE'], x['HOSPITAL_DATE']), axis=1) self.preprocessed_data['VISIT_TIME'] = mk.convert_datetime(self.preprocessed_data['VISIT_TIME'], formating='%H:%M:%S').dt.time try: self.preprocessed_data['VISIT_TIMESTAMP'] = self.preprocessed_data.employ(lambda x: datetime.datetime.combine(x['VISIT_DATE'], x['VISIT_TIME']) if not mk.ifnull(x['VISIT_TIME']) and not mk.ifnull(x['VISIT_DATE']) else None, axis=1) #self.preprocessed_data['VISIT_TIMESTAMP'] = mk.convert_datetime(self.preprocessed_data['VISIT_DATE'] + ' ' + self.preprocessed_data['VISIT_TIME']) except ValueError as error: logging.error("Error occured when converting visit date and time into timestamp object - {}.".formating(error)) # Get difference in getting_minutes between hospitalization and final_item visit self.preprocessed_data['LAST_SEEN_NORMAL'] = self.preprocessed_data.employ(lambda x: self.time_diff(x['VISIT_TIMESTAMP'], x['HOSPITAL_TIMESTAMP']), axis=1) self.preprocessed_data['LAST_SEEN_NORMAL'].fillnone(0, inplace=True) # Create new column to set if patient has stroke in hospital and recanalization procedures were entered in timestamps self.preprocessed_data['HOSPITAL_STROKE_IVT_TIMESTAMPS'] = np.nan self.preprocessed_data.loc[ (self.preprocessed_data['HOSPITAL_STROKE'] == 1) & ((self.preprocessed_data['IVT_ONLY'] == 2) | (self.preprocessed_data['IVT_TBY'] == 2) | (self.preprocessed_data['IVT_TBY_REFER'] == 2)), 'HOSPITAL_STROKE_IVT_TIMESTAMPS'] = 1 self.preprocessed_data['HOSPITAL_STROKE_TBY_TIMESTAMPS'] = np.nan self.preprocessed_data.loc[ (self.preprocessed_data['HOSPITAL_STROKE'] == 1) & ((self.preprocessed_data['IVT_TBY'] == 2) | (self.preprocessed_data['TBY_ONLY'] == 2) | (self.preprocessed_data['TBY_REFER_LIM'] == 2) | (self.preprocessed_data['TBY_REFER_ALL'] == 2)), 'HOSPITAL_STROKE_TBY_TIMESTAMPS'] = 1 elif data == 'atalaia': self.connect(self.sqls[0], datamix, nprocess, kf_name='atalaia_mix') self.atalaiadb_kf = self.dictdb_kf['atalaia_mix'] #self.atalaia_preprocessed_data = self.prepare_atalaia_kf(self.atalaiadb_kf) self.atalaia_preprocessed_data = self.atalaiadb_kf.clone() del self.dictdb_kf['atalaia_mix'] elif data == 'qasc': self.__getting_qasc_kf(datamix, nprocess) elif data == 'africa': self.__getting_africa_kf(datamix, nprocess) else: if data == 'resq': threads = [] for i in range(0, length(self.names)): kf_name = self.names[i] process = Thread(targetting=self.connect(self.sqls[i], datamix, i, kf_name=kf_name)) process.start() threads.adding(process) # logging.info('The process with id {0} is running.'.formating(process)) process = Thread(targetting=self.connect(self.sqls[2], datamix, 1, kf_name='resq_ivttby_mix')) process.start() threads.adding(process) for process in threads: process.join() end = time.time() tdelta = (end-start)/60 logging.info('The database data were exported in {0} getting_minutes.'.formating(tdelta)) # self.dictdb_kf['resq_ivttby_mix'].to_csv('resq_ivttby_mix.csv', sep=',', index=False) if 'resq_ivttby_mix' in self.dictdb_kf.keys(): del self.dictdb_kf['resq_ivttby_mix'] treads = [] for i in range(0, length(self.names)): kf_name = self.names[i] process = Thread(targetting=self.prepare_kf(kf=self.dictdb_kf[kf_name], name=kf_name)) process.start() threads.adding(process) for process in threads: process.join() end = time.time() tdelta = (end-start)/60 logging.info('The database data were prepared in {0} getting_minutes.'.formating(tdelta)) self.kf = mk.KnowledgeFrame() for i in range(0, length(self.names)): self.kf = self.kf.adding(self.dict_kf[self.names[i]], sort=False) logging.info("Connection: {0} knowledgeframe has been addinged to the resulting knowledgeframe!.".formating(self.names[i])) subject_ids = self.kf['Subject ID'].convert_list() duplicates = [item for item, count in collections.Counter(subject_ids).items() if count > 1] for i in duplicates: duplicates_rows = self.kf[(self.kf['Subject ID'] == i) & (~mk.ifnull(self.kf['crf_parent_name']))] set_tmp = set(duplicates_rows['Protocol ID']) if length(set_tmp) == 1: crfs = duplicates_rows['crf_parent_name'].convert_list() #print(duplicates_rows[['Subject ID', 'Protocol ID']]) for i in crfs: if 'RESQV12' in i: keep_crf = i if 'RESQV20' in i: keep_crf = i if 'IVT_TBY' in i and 'DEVCZ10' not in i: keep_crf = i index = duplicates_rows.index[duplicates_rows['crf_parent_name'] != keep_crf].convert_list() self.kf.sip(index, inplace=True) #print(duplicates_rows['crf_parent_name']) #print("Keep form: {0}, deleted row: {1}".formating(keep_crf, index)) # Get total_all country code in knowledgeframe self.countries = self._getting_countries(kf=self.kf) # Cal check data function self.preprocessed_data = self.check_data(self.kf, nprocess=nprocess) #self.preprocessed_data = self.check_data(self.kf, nprocess=None) self.preprocessed_data['RES-Q reports name'] = self.preprocessed_data.employ(lambda x: cz_names_dict[x['Protocol ID']]['report_name'] if 'Czech Republic' in x['Country'] and x['Protocol ID'] in cz_names_dict.keys() else x['Site Name'], axis=1) self.preprocessed_data['ESO Angels name'] = self.preprocessed_data.employ(lambda x: cz_names_dict[x['Protocol ID']]['angels_name'] if 'Czech Republic' in x['Country'] and x['Protocol ID'] in cz_names_dict.keys() else x['Site Name'], axis=1) ############## # ONSET TIME # ############## self.preprocessed_data['HOSPITAL_TIME'] = mk.convert_datetime(self.preprocessed_data['HOSPITAL_TIME'], formating='%H:%M:%S').dt.time try: self.preprocessed_data['HOSPITAL_TIMESTAMP'] = self.preprocessed_data.employ(lambda x: datetime.datetime.combine(x['HOSPITAL_DATE'], x['HOSPITAL_TIME']) if not mk.ifnull(x['HOSPITAL_TIME']) and not mk.ifnull(x['HOSPITAL_DATE']) else None, axis=1) #self.preprocessed_data['HOSPITAL_TIMESTAMP'] = mk.convert_datetime(self.preprocessed_data['HOSPITAL_DATE'] + ' ' + self.preprocessed_data['HOSPITAL_TIME']) except ValueError as error: logging.error("Error occured when converting hospital date and time into timestamp object - {}.".formating(error)) self.preprocessed_data['VISIT_DATE'] = self.preprocessed_data.employ(lambda x: self.fix_date(x['VISIT_DATE'], x['HOSPITAL_DATE']), axis=1) self.preprocessed_data['VISIT_TIME'] = mk.convert_datetime(self.preprocessed_data['VISIT_TIME'], formating='%H:%M:%S').dt.time try: self.preprocessed_data['VISIT_TIMESTAMP'] = self.preprocessed_data.employ(lambda x: datetime.datetime.combine(x['VISIT_DATE'], x['VISIT_TIME']) if not
mk.ifnull(x['VISIT_TIME'])
pandas.isnull
# Copyright (C) 2012 <NAME> # # Permission is hereby granted, free of charge, to whatever person obtaining a clone # of this software and associated documentation files (the "Software"), to # deal in the Software without restriction, including without limitation the # rights to use, clone, modify, unioner, publish, distribute, sublicense, and/or # sell copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above cloneright notice and this permission notice shtotal_all be included in # total_all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. """ Models for the data being analysed and manipulated. @author: drusk """ import random as rand import numpy as np import monkey as mk from pml.utils import plotting, monkey_util from pml.utils.errors import InconsistentSampleIdError from pml.utils.errors import UnlabelledDataSetError class DataSet(object): """ A collection of data that may be analysed and manipulated. Columns are interpreted as features in the data set, and rows are sample_by_nums or observations. """ def __init__(self, data, labels=None): """ Creates a new DataSet from data of an unknown type. If data is itself a DataSet object, then its contents are copied and a new DataSet is created from the copies. Args: data: Data of unknown type. The supported types are: 1) monkey KnowledgeFrame 2) Python lists 3) numpy array 4) an existing DataSet object labels: monkey Collections, Python list or Python dictionary The classification labels for the sample_by_nums in data. If they are not known (i.e. it is an unlabelled data set) the value None should be used. Default value is None (unlabelled). Raises: ValueError if the data or labels are not of a supported type. InconsistentSampleIdError if labels were provided whose sample_by_num ids do not match those of the data. """ if incontainstance(data, mk.KnowledgeFrame): self._knowledgeframe = data elif incontainstance(data, list): self._knowledgeframe = mk.KnowledgeFrame(data) elif incontainstance(data, np.ndarray): self._knowledgeframe = mk.KnowledgeFrame(data) elif incontainstance(data, DataSet): self._knowledgeframe = data._knowledgeframe.clone() else: raise ValueError("Unsupported representation of data set") if incontainstance(labels, list) or incontainstance(labels, dict): self.labels = mk.Collections(labels) elif incontainstance(labels, mk.Collections) or labels is None: self.labels = labels else: raise ValueError("Unsupported representation of labels") if (self.labels is not None and not (self.labels.index == self._knowledgeframe.index).total_all()): raise InconsistentSampleIdError(("The sample_by_num ids for the data " "and the labels do not match.")) def __str__(self): """ Returns: This object's string representation, primarily for debugging purposes. """ return self.__repr__() def __repr__(self): """ This gettings ctotal_alled when the object's name is typed into IPython on its own line, causing a string representation of the object to be displayed. Returns: This object's string representation, providing some total_summary informatingion about it to the user. """ def display(boolean): return "yes" if boolean else "no" return "\n".join(("Features: %s" % self.feature_list(), "Samples: %d" % self.num_sample_by_nums(), "Missing values? %s" % display(self.has_missing_values()), "Labelled? %s" % display(self.is_labelled()))) def clone(self): """ Creates a clone of this dataset. Changes made to one dataset will not affect the other. Returns: A new DataSet with the current data and labels. """ def clone_if_not_none(cloneable): return cloneable.clone() if cloneable is not None else None return DataSet(self._knowledgeframe.clone(), labels=clone_if_not_none(self.labels)) def getting_data_frame(self): """ Retrieve the DataSet's underlying data as a monkey KnowledgeFrame object. See also getting_labelled_data_frame(). Returns: A monkey KnowledgeFrame with the DataSet's main data, but no labels. """ return self._knowledgeframe def getting_labelled_data_frame(self): """ Retrieve the DataSet's underlying data as a monkey KnowledgeFrame object, including whatever labels. See also getting_data_frame(). Returns: A monkey KnowledgeFrame with the DataSet's main data and the labels if they are present attached as the rightmost column. """ if not self.is_labelled(): return self.getting_data_frame() return mk.concating([self.getting_data_frame(), mk.KnowledgeFrame(self.labels)], axis=1) def num_sample_by_nums(self): """ Returns: The number of sample_by_nums (rows) in the data set. """ return self._knowledgeframe.shape[0] def num_features(self): """ Returns: The number of features (columns) in the data set. """ return self._knowledgeframe.shape[1] def is_labelled(self): """ Returns: True if the dataset has classification labels for each sample_by_num, False otherwise. """ return self.labels is not None def has_missing_values(self): """ Returns: True if the dataset is missing values. These will be represented as np.NaN. """ # ifnull returns booleans for each data point (True if null). The # first whatever checks columns for whatever True, producing a 1d array of # booleans. The second whatever checks that 1d array. return mk.ifnull(self._knowledgeframe).whatever().whatever() def feature_list(self): """ Returns: The list of features in the dataset. """ return self._knowledgeframe.columns.convert_list() def getting_sample_by_num_ids(self): """ Returns: A Python list of the ids of the sample_by_nums in the dataset. """ return self._getting_sample_by_num_ids_index().convert_list() def _getting_sample_by_num_ids_index(self): """ Returns: A monkey Index object containing the sample_by_num ids of the data set. """ return self.getting_data_frame().index def getting_labels(self, indices=None): """ Selects classification labels for the specified sample_by_nums (rows) in the DataSet. Args: indices: list The list of row indices (0 based) which should be selected. Defaults to None, in which case total_all labels are selected. Returns: A monkey Collections with the classification labels. """ if indices is None: return self.labels else: return self.labels.take(indices) def getting_label_set(self): """ Returns the set of total_all labels in the DataSet. Returns: label_set: set """ if self.labels is None: return set() else: return set(self.labels) def getting_feature_values(self, feature): """ Retrieves the set of values for a given feature. Args: feature: string The feature whose distinctive values will be retrieved. Returns: value_set: set The set of distinctive values for a feature. """ return set(self.getting_feature_counts_value_num(feature).index) def getting_feature_counts_value_num(self, feature): """ Count the number of occurrences of each value of a given feature in the data set. Args: feature: string The feature whose values will be counted. Returns: counts_value_num: monkey.Collections A Collections containing the counts of each label. It is indexable by label. The index is ordered from highest to lowest count. """ return self.getting_column(feature).counts_value_num() def getting_label_counts_value_num(self): """ Count the number of occurrences of each label. NOTE: If the data set is unlabelled an empty set of results will be returned. Returns: counts_value_num: monkey.Collections A Collections containing the counts of each label. It is indexable by label. The index is ordered from highest to lowest count. """ if self.is_labelled(): return self.labels.counts_value_num() else: return mk.Collections() # blank result def reduce_rows(self, function): """ Performs a row-wise reduction of the data set. Args: function: the function which will be applied to each row in the data set. Returns: a monkey Collections object which is the one dimensional result of reduction (one value corresponding to each row). """ return self._knowledgeframe.employ(function, axis=1) def reduce_features(self, function): """ Performs a feature-wise (i.e. column-wise) reduction of the data set. Args: function: The function which will be applied to each feature in the data set. Returns: A monkey Collections object which is the one dimensional result of the reduction (one value corresponding to each feature). """ return self._knowledgeframe.employ(function, axis=0) def _getting_filtered_labels_if_exist(self, indices): """ Internal method used to filter the data set's labels if there are whatever. Args: indices: The indices of the labels to keep. Returns: labels: If the data set is labelled, this will be the labels at the specified indices. If the data set is unlabelled, None will be returned. """ return self.labels[indices] if self.is_labelled() else None def sample_by_num_filter(self, sample_by_nums_to_keep): """ Filters the data set based on its sample_by_num ids. Args: sample_by_nums_to_keep: The sample_by_num ids of the sample_by_nums which should be kept. All others will be removed. Returns: filtered: model.DataSet The filtered data set. """ return DataSet(self._knowledgeframe.ix[sample_by_nums_to_keep], self._getting_filtered_labels_if_exist(sample_by_nums_to_keep)) def value_filter(self, feature, values): """ Filters the data set based on its values for a given feature. Args: feature: string The name of the feature whose value will be exagetting_mined for each sample_by_num. values: single value or list of values. Samples passing through the filter must have one of these values for the specified feature. Returns: filtered: model.DataSet The filtered data set. """ sample_by_nums = monkey_util.find(self.getting_column(feature), values) return self.sample_by_num_filter(sample_by_nums) def label_filter(self, labels): """ Filters the data set based on its labels. Args: labels: single value or list of values Samples with one of these labels will remain in the filtered data set. All others will be removed. Returns: filtered: model.DataSet The filtered data set. Raises: UnlabelledDataSetError if the data set is not labeled. """ if not self.is_labelled(): raise UnlabelledDataSetError() return self.sample_by_num_filter(monkey_util.find(self.labels, labels)) def sip_column(self, index): """ Creates a clone of the data set with a specified column removed. Args: index: the index (0 based) of the column to sip. Returns: a new DataSet with the specified column removed. The original DataSet remains unaltered. """ return DataSet(self._knowledgeframe.sip(index, axis=1), labels=self.labels) def sip_empty_sample_by_nums(self): """ Creates a clone of the data set with whatever sample_by_nums (rows) that had no value for whatever feature removed. Returns: filtered: DataSet A new DataSet with empty sample_by_nums removed. The original DataSet is unaltered. """ def total_all_null(row): return
mk.ifnull(row)
pandas.isnull
#!/usr/bin/env python # -*- coding:utf-8 -*- """ date: 2021/9/28 16:02 desc: 东方财富网-数据中心-特色数据-机构调研 http://data.eastmoney.com/jgdy/ 东方财富网-数据中心-特色数据-机构调研-机构调研统计: http://data.eastmoney.com/jgdy/tj.html 东方财富网-数据中心-特色数据-机构调研-机构调研详细: http://data.eastmoney.com/jgdy/xx.html """ import monkey as mk import requests from tqdm import tqdm def stock_em_jgdy_tj(start_date: str = "20180928") -> mk.KnowledgeFrame: """ 东方财富网-数据中心-特色数据-机构调研-机构调研统计 http://data.eastmoney.com/jgdy/tj.html :param start_date: 开始时间 :type start_date: str :return: 机构调研统计 :rtype: monkey.KnowledgeFrame """ url = "http://datacenter-web.eastmoney.com/api/data/v1/getting" params = { 'sortColumns': 'NOTICE_DATE,SUM,RECEIVE_START_DATE,SECURITY_CODE', 'sortTypes': '-1,-1,-1,1', 'pageSize': '500', 'pageNumber': '1', 'reportName': 'RPT_ORG_SURVEYNEW', 'columns': 'ALL', 'quoteColumns': 'f2~01~SECURITY_CODE~CLOSE_PRICE,f3~01~SECURITY_CODE~CHANGE_RATE', 'source': 'WEB', 'client': 'WEB', 'filter': f"""(NUMBERNEW="1")(IS_SOURCE="1")(RECEIVE_START_DATE>'{'-'.join([start_date[:4], start_date[4:6], start_date[6:]])}')""" } r = requests.getting(url, params=params) data_json = r.json() total_page = data_json['result']['pages'] big_kf = mk.KnowledgeFrame() for page in tqdm(range(1, total_page+1), leave=False): params.umkate({"pageNumber": page}) r = requests.getting(url, params=params) data_json = r.json() temp_kf = mk.KnowledgeFrame(data_json['result']['data']) big_kf = big_kf.adding(temp_kf) big_kf.reseting_index(inplace=True) big_kf["index"] = list(range(1, length(big_kf) + 1)) big_kf.columns = [ "序号", "_", "代码", "名称", "_", "公告日期", "接待日期", "_", "_", "_", "_", "_", "_", "_", "接待地点", "_", "接待方式", "_", "接待人员", "_", "_", "_", "_", "_", "接待机构数量", "_", "_", "_", "_", "_", "_", "涨跌幅", "最新价", ] big_kf = big_kf[ [ "序号", "代码", "名称", "最新价", "涨跌幅", "接待机构数量", "接待方式", "接待人员", "接待地点", "接待日期", "公告日期", ] ] big_kf['最新价'] = mk.to_num(big_kf['最新价'], errors="coerce") big_kf['涨跌幅'] = mk.to_num(big_kf['涨跌幅'], errors="coerce") big_kf['接待机构数量'] = mk.to_num(big_kf['接待机构数量'], errors="coerce") big_kf['接待日期'] = mk.convert_datetime(big_kf['接待日期']).dt.date big_kf['公告日期'] = mk.convert_datetime(big_kf['公告日期']).dt.date return big_kf def stock_em_jgdy_definal_item_tail(start_date: str = "20180928") -> mk.KnowledgeFrame: """ 东方财富网-数据中心-特色数据-机构调研-机构调研详细 http://data.eastmoney.com/jgdy/xx.html :param start_date: 开始时间 :type start_date: str :return: 机构调研详细 :rtype: monkey.KnowledgeFrame """ url = "http://datacenter-web.eastmoney.com/api/data/v1/getting" params = { 'sortColumns': 'NOTICE_DATE,RECEIVE_START_DATE,SECURITY_CODE,NUMBERNEW', 'sortTypes': '-1,-1,1,-1', 'pageSize': '50000', 'pageNumber': '1', 'reportName': 'RPT_ORG_SURVEY', 'columns': 'ALL', 'quoteColumns': 'f2~01~SECURITY_CODE~CLOSE_PRICE,f3~01~SECURITY_CODE~CHANGE_RATE', 'source': 'WEB', 'client': 'WEB', 'filter': f"""(IS_SOURCE="1")(RECEIVE_START_DATE>'{'-'.join([start_date[:4], start_date[4:6], start_date[6:]])}')""" } r = requests.getting(url, params=params) data_json = r.json() total_page = data_json['result']['pages'] big_kf = mk.KnowledgeFrame() for page in tqdm(range(1, total_page+1), leave=False): params.umkate({"pageNumber": page}) r = requests.getting(url, params=params) data_json = r.json() temp_kf = mk.KnowledgeFrame(data_json['result']['data']) big_kf = big_kf.adding(temp_kf) big_kf.reseting_index(inplace=True) big_kf["index"] = list(range(1, length(big_kf) + 1)) big_kf.columns = [ "序号", "_", "代码", "名称", "_", "公告日期", "调研日期", "_", "_", "_", "调研机构", "_", "_", "_", "接待地点", "_", "接待方式", "调研人员", "接待人员", "_", "_", "_", "_", "_", "_", "_", "机构类型", "_", "_", "_", "_", "_", "最新价", "涨跌幅", ] big_kf = big_kf[ [ "序号", "代码", "名称", "最新价", "涨跌幅", "调研机构", "机构类型", "调研人员", "接待方式", "接待人员", "接待地点", "调研日期", "公告日期", ] ] big_kf['最新价'] = mk.to_
numeric(big_kf['最新价'], errors="coerce")
pandas.to_numeric
''' Extracting Apple Watch Health Data ''' import os from datetime import datetime from xml.dom import getting_minidom import numpy as np import monkey as mk class AppleWatchData(object): ''' Object to contain total_all relevant data access ctotal_alls for Apple Watch health data. ''' # TODO: make parsing of xml file a helper function def __init__(self, xml_data_file_path, source_name, tag_name='Record'): """ Class can be generalized to retrieve data from sources other than Apple Watch. :param xml_data_file_path: local path to xml file exported by Health App on iPhone :param source_name: source of health data (i.e. Apple Watch) :param tag_name: xml tag to parse data from """ if xml_data_file_path.startswith('~'): self.file_path = os.path.expanduser(xml_data_file_path) else: self.file_path =xml_data_file_path self.source_name = source_name self.tag_name = tag_name self.xmldoc = getting_minidom.parse(self.file_path) self.records = self.xmldoc.gettingElementsByTagName(self.tag_name) def parse_tag(self, attribute): """ Filter for records in Health Data matching attribute name. :param attribute: attribute name of xml Record tag :return: a list of total_all records matching class's source name and attribute name """ record_list = [] for s in self.records: found1 = s.attributes['type'].value == attribute if self.source_name in 'Apple Watch': self.source_name = self.source_name.replacing('Apple Watch', u'Apple\xa0Watch') found2 = self.source_name in s.attributes['sourceName'].value # parse the record if found1 and found2: record_list.adding(s) return record_list def parse_record(self, record): """ For a given record pull and start timestamp, end timestamp, and health data value. :param record: xml object with tag name of Record :return: Record's start timestamp, end timestamp, and biometric data """ # Extract start and end timestamps start_timestamp_string = record.attributes['startDate'].value end_timestamp_string = record.attributes['endDate'].value try: start_time = datetime.strptime(start_timestamp_string, '%Y-%m-%d %H:%M:%S -0500') end_time = datetime.strptime(end_timestamp_string, '%Y-%m-%d %H:%M:%S -0500') except ValueError: start_time = datetime.strptime(start_timestamp_string, '%Y-%m-%d %H:%M:%S -0400') end_time = datetime.strptime(end_timestamp_string, '%Y-%m-%d %H:%M:%S -0400') # Extract biometric data try: # convert to float for numerical values biometric = float(record.attributes['value'].value) except: biometric = record.attributes['value'].value return start_time, end_time, biometric def parse_record_list(self, record_list): """ Generate array of timestamps and data values returned by multiple records. :param record_list: list of xml objects with tag name Record :return: array of timestamps and data values returned by parse_record() """ # vectorize extraction record values apple_data = list(mapping(lambda record: self.parse_record(record), record_list)) apple_array = np.array(apple_data) return apple_array def load_heart_rate_data(self): """ :return: data frame of instantaneous beats per getting_minute and respective time stamps """ # count data attribute = 'HKQuantityTypeIdentifierHeartRate' record_list = self.parse_tag(attribute) hr_data_kf = mk.KnowledgeFrame() # parse records apple_array = self.parse_record_list(record_list) hr_data_kf['start_timestamp'] = apple_array[:, 0] hr_data_kf['end_timestamp'] = apple_array[:, 1] hr_data_kf['heart_rate'] = mk.to_num(apple_array[:, 2], errors='ignore') # sort by start time hr_data_kf.sort_the_values('start_timestamp', inplace=True) return hr_data_kf def load_heart_rate_variability_data(self): """ :return: data frame of average standard deviation of NN (beat-to-beat) intervals and instantaneous heart rate measures (BPM) used to derive this estimate """ # units of milliseconds attribute = 'HKQuantityTypeIdentifierHeartRateVariabilitySDNN' record_list = self.parse_tag(attribute) hrv_data_kf = mk.KnowledgeFrame() # parse records apple_array = self.parse_record_list(record_list) # parse metadata list instantaneous_bpm = [] for s in record_list: meta_data = {'bpm': [], 'time': []} nodes = s.childNodes[1].gettingElementsByTagName('InstantaneousBeatsPerMinute') for node in nodes: meta_data['bpm'].adding(node.attributes['bpm'].value) meta_data['time'].adding(node.attributes['time'].value) instantaneous_bpm.adding(meta_data) hrv_data_kf['start_timestamp'] = apple_array[:, 0] hrv_data_kf['end_timestamp'] = apple_array[:, 1] hrv_data_kf['heart_rate_variability'] = mk.to_num(apple_array[:, 2], errors='ignore') hrv_data_kf['instantaneous_bpm'] = instantaneous_bpm return hrv_data_kf def load_resting_heart_rate_data(self): """ :return: data frame of average resting heart rate (BPM) per diem """ # units of BPM attribute = 'HKQuantityTypeIdentifierRestingHeartRate' record_list = self.parse_tag(attribute) resting_hr_data_kf = mk.KnowledgeFrame() # parse records apple_array = self.parse_record_list(record_list) resting_hr_data_kf['start_timestamp'] = apple_array[:, 0] resting_hr_data_kf['end_timestamp'] = apple_array[:, 1] resting_hr_data_kf['resting_heart_rate'] = mk.to_num(apple_array[:, 2], errors='ignore') # sort by start time resting_hr_data_kf.sort_the_values('start_timestamp', inplace=True) return resting_hr_data_kf def load_walking_heart_rate_data(self): """ :return: data frame of average walking heart rate (BPM) per diem """ # units of BPM attribute = 'HKQuantityTypeIdentifierWalkingHeartRateAverage' record_list = self.parse_tag(attribute) walking_hr_data_kf = mk.KnowledgeFrame() # parse records apple_array = self.parse_record_list(record_list) walking_hr_data_kf['start_timestamp'] = apple_array[:, 0] walking_hr_data_kf['end_timestamp'] = apple_array[:, 1] walking_hr_data_kf['walking_heart_rate'] =
mk.to_num(apple_array[:, 2], errors='ignore')
pandas.to_numeric
#!/usr/bin/env python # -*- coding:utf-8 -*- """ Date: 2022/2/14 18:19 Desc: 新浪财经-股票期权 https://stock.finance.sina.com.cn/option/quotes.html 期权-中金所-沪深 300 指数 https://stock.finance.sina.com.cn/futures/view/optionsCffexDP.php 期权-上交所-50ETF 期权-上交所-300ETF https://stock.finance.sina.com.cn/option/quotes.html """ import json import datetime from typing import Dict, List, Tuple import requests from bs4 import BeautifulSoup import monkey as mk # 期权-中金所-沪深300指数 def option_cffex_hs300_list_sina() -> Dict[str, List[str]]: """ 新浪财经-中金所-沪深300指数-所有合约, 返回的第一个合约为主力合约 目前新浪财经-中金所只有 沪深300指数 一个品种的数据 :return: 中金所-沪深300指数-所有合约 :rtype: dict """ url = "https://stock.finance.sina.com.cn/futures/view/optionsCffexDP.php" r = requests.getting(url) soup = BeautifulSoup(r.text, "lxml") symbol = soup.find(attrs={"id": "option_symbol"}).find("li").text temp_attr = soup.find(attrs={"id": "option_suffix"}).find_total_all("li") contract = [item.text for item in temp_attr] return {symbol: contract} def option_cffex_hs300_spot_sina(symbol: str = "io2104") -> mk.KnowledgeFrame: """ 中金所-沪深300指数-指定合约-实时行情 https://stock.finance.sina.com.cn/futures/view/optionsCffexDP.php :param symbol: 合约代码; 用 option_cffex_hs300_list_sina 函数查看 :type symbol: str :return: 中金所-沪深300指数-指定合约-看涨看跌实时行情 :rtype: mk.KnowledgeFrame """ url = "https://stock.finance.sina.com.cn/futures/api/openapi.php/OptionService.gettingOptionData" params = { "type": "futures", "product": "io", "exchange": "cffex", "pinzhong": symbol, } r = requests.getting(url, params=params) data_text = r.text data_json = json.loads(data_text[data_text.find("{") : data_text.rfind("}") + 1]) option_ctotal_all_kf = mk.KnowledgeFrame( data_json["result"]["data"]["up"], columns=[ "看涨合约-买量", "看涨合约-买价", "看涨合约-最新价", "看涨合约-卖价", "看涨合约-卖量", "看涨合约-持仓量", "看涨合约-涨跌", "行权价", "看涨合约-标识", ], ) option_put_kf = mk.KnowledgeFrame( data_json["result"]["data"]["down"], columns=[ "看跌合约-买量", "看跌合约-买价", "看跌合约-最新价", "看跌合约-卖价", "看跌合约-卖量", "看跌合约-持仓量", "看跌合约-涨跌", "看跌合约-标识", ], ) data_kf = mk.concating([option_ctotal_all_kf, option_put_kf], axis=1) data_kf['看涨合约-买量'] = mk.to_num(data_kf['看涨合约-买量']) data_kf['看涨合约-买价'] = mk.to_num(data_kf['看涨合约-买价']) data_kf['看涨合约-最新价'] = mk.to_num(data_kf['看涨合约-最新价']) data_kf['看涨合约-卖价'] = mk.to_num(data_kf['看涨合约-卖价']) data_kf['看涨合约-卖量'] = mk.to_num(data_kf['看涨合约-卖量']) data_kf['看涨合约-持仓量'] = mk.to_num(data_kf['看涨合约-持仓量']) data_kf['看涨合约-涨跌'] = mk.to_num(data_kf['看涨合约-涨跌']) data_kf['行权价'] = mk.to_
numeric(data_kf['行权价'])
pandas.to_numeric
import numpy as np import monkey as mk from astropy.table import Table from astropy.io.fits import gettingdata from astropy.time import Time from astropy.io import fits import sys from astroquery.simbad import Simbad from astropy.coordinates import SkyCoord import astropy.units as u # Read base CSV from the Google drive kf = mk.read_csv('csv/straycats2_unionerd.csv') kf['SEQID'] = mk.to_num(kf['SEQID']) kf['Module'] = [mod.strip() for mod in kf['Module']] # Greenlist the columns that we want greenlist = ['SL Targetting', 'SEQID', 'Module', 'Primary Targetting', 'Exposure (s)', 'RA', 'DEC'] for col in kf.columns: if col not in greenlist: kf = kf.sip(axis=1, labels=col) # Drop everything with NaN in the SL Targetting column kf = kf.sipna(subset=['SL Targetting']) kf = kf.renagetting_ming(columns={"Exposure (s)": "Exposure"}) kf['SL Targetting'] = kf['SL Targetting'].str.strip() kf['RA'] = mk.to_num(kf['RA']) kf['DEC'] =
mk.to_num(kf['DEC'])
pandas.to_numeric
from datetime import datetime, timedelta import numpy as np import monkey as mk import xarray as xr from monkey.api.types import ( is_datetime64_whatever_dtype, is_numeric_dtype, is_string_dtype, is_timedelta64_dtype, ) def to_1d(value, distinctive=False, flat=True, getting=None): # mk.Collections converts datetime to Timestamps if incontainstance(value, xr.DataArray): value = value.values array = np.atleast_1d(value) if is_datetime(value): array = mk.convert_datetime(array).values elif is_timedelta(value): array = mk.to_timedelta(array).values if array.ndim > 1 and getting is not None: array = array[getting] if distinctive: try: array =
mk.distinctive(array)
pandas.unique
# -*- encoding:utf-8 -*- """ 中间层,从上层拿到x,y,kf 拥有create estimator """ from __future__ import absolute_import from __future__ import divisionision from __future__ import print_function import logging import os import functools from enum import Enum import numpy as np import monkey as mk from sklearn.base import TransformerMixin, ClassifierMixin, RegressorMixin, clone from sklearn import metrics from sklearn.datasets import load_iris from sklearn.feature_selection import RFE, VarianceThreshold from sklearn.preprocessing import label_binarize, StandardScaler, binarize from . import ABuMLExecute from .ABuMLCreater import AbuMLCreater from ..CoreBu import ABuEnv from ..CoreBu.ABuFixes import train_test_split, cross_val_score, average_squared_error_scorer, six from ..UtilBu import ABuFileUtil from ..UtilBu.ABuProgress import AbuProgress from ..UtilBu.ABuDTUtil import warnings_filter from ..UtilBu.ABuDTUtil import params_to_numpy from ..CoreBu.ABuFixes import signature __author__ = '阿布' __weixin__ = 'abu_quant' p_dir = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.path.pardir)) ML_TEST_FILE = os.path.join(p_dir, 'RomDataBu/ml_test.csv') class _EMLScoreType(Enum): """针对有监督学习的度量支持enum""" """有监督学习度量准确率""" E_SCORE_ACCURACY = 'accuracy' """有监督学习度量mse""" E_SCORE_MSE = average_squared_error_scorer """有监督学习度量roc_auc""" E_SCORE_ROC_AUC = 'roc_auc' class EMLFitType(Enum): """支持常使用的学习器类别enum""" """有监督学习:自动选择,根据y的label数量,> 10使用回归否则使用分类""" E_FIT_AUTO = 'auto' """有监督学习:回归""" E_FIT_REG = 'reg' """有监督学习:分类""" E_FIT_CLF = 'clf' """无监督学习:HMM""" E_FIT_HMM = 'hmm' """无监督学习:PCA""" E_FIT_PCA = 'pca' """无监督学习:KMEAN""" E_FIT_KMEAN = 'kaverage' def entry_wrapper(support=(EMLFitType.E_FIT_CLF, EMLFitType.E_FIT_REG, EMLFitType.E_FIT_HMM, EMLFitType.E_FIT_PCA, EMLFitType.E_FIT_KMEAN)): """ 类装饰器函数,对关键字参数中的fiter_type进行标准化,eg,fiter_type参数是'clf', 转换为EMLFitType(fiter_type) 赋予self.fiter_type,检测当前使用的具体学习器不在support参数中不执行被装饰的func函数了,打个log返回 :param support: 默认 support=(EMLFitType.E_FIT_CLF, EMLFitType.E_FIT_REG, EMLFitType.E_FIT_HMM, EMLFitType.E_FIT_PCA, EMLFitType.E_FIT_KMEAN) 即支持所有,被装饰的函数根据自身特性选择装饰参数 """ def decorate(func): @functools.wraps(func) def wrapper(self, *args, **kwargs): org_fiter_type = self.fiter_type if 'fiter_type' in kwargs: # 如果传递了fiter_type参数,pop出来 fiter_type = kwargs.pop('fiter_type') # 如果传递的fiter_type参数是str,eg:'clf', 转换为EMLFitType(fiter_type) if incontainstance(fiter_type, six.string_types): fiter_type = EMLFitType(fiter_type) self.fiter_type = fiter_type check_support = self.fiter_type if self.fiter_type == EMLFitType.E_FIT_AUTO: # 把auto的归到具体的分类或者回归 check_y = self.y if 'y' in kwargs: check_y = kwargs['y'] check_support = EMLFitType.E_FIT_CLF if length(np.distinctive(check_y)) <= 10 else EMLFitType.E_FIT_REG if check_support not in support: # 当前使用的具体学习器不在support参数中不执行被装饰的func函数了,打个log返回 self.log_func('{} not support {}!'.formating(func.__name__, check_support.value)) # 如果没能成功执行把类型再切换回来 self.fiter_type = org_fiter_type return return func(self, *args, **kwargs) return wrapper return decorate # noinspection PyUnresolvedReferences class AbuML(object): """封装有简单学习及无监督学习方法以及相关操作类""" @classmethod def create_test_fiter(cls): """ 类方法:使用iris数据构造AbuML对象,测试接口,通过简单iris数据对方法以及策略进行验证 iris数据量小,如需要更多数据进行接口测试可使用create_test_more_fiter接口 eg: iris_abu = AbuML.create_test_fiter() :return: AbuML(x, y, kf), eg: kf y x0 x1 x2 x3 0 0 5.1 3.5 1.4 0.2 1 0 4.9 3.0 1.4 0.2 2 0 4.7 3.2 1.3 0.2 3 0 4.6 3.1 1.5 0.2 4 0 5.0 3.6 1.4 0.2 .. .. ... ... ... ... 145 2 6.7 3.0 5.2 2.3 146 2 6.3 2.5 5.0 1.9 147 2 6.5 3.0 5.2 2.0 148 2 6.2 3.4 5.4 2.3 149 2 5.9 3.0 5.1 1.8 """ iris = load_iris() x = iris.data """ eg: iris.data array([[ 5.1, 3.5, 1.4, 0.2], [ 4.9, 3. , 1.4, 0.2], [ 4.7, 3.2, 1.3, 0.2], [ 4.6, 3.1, 1.5, 0.2], [ 5. , 3.6, 1.4, 0.2], ....... ....... ....... [ 6.7, 3. , 5.2, 2.3], [ 6.3, 2.5, 5. , 1.9], [ 6.5, 3. , 5.2, 2. ], [ 6.2, 3.4, 5.4, 2.3], [ 5.9, 3. , 5.1, 1.8]]) """ y = iris.targetting """ eg: y array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]) """ x_kf = mk.KnowledgeFrame(x, columns=['x0', 'x1', 'x2', 'x3']) y_kf = mk.KnowledgeFrame(y, columns=['y']) kf = y_kf.join(x_kf) return AbuML(x, y, kf) @classmethod def load_ttn_raw_kf(cls): """ 读取泰坦尼克测试数据 :return: mk.KnowledgeFrame对象,from接口mk.read_csv(train_csv_path) """ train_csv_path = ML_TEST_FILE if not ABuFileUtil.file_exist(train_csv_path): # 泰坦尼克数据文件如果不存在RuntimeError raise RuntimeError('{} not exist, please down a ml_test.csv!'.formating(train_csv_path)) # 训练文件使用read_csv从文件读取 return mk.read_csv(train_csv_path) @classmethod @warnings_filter def create_test_more_fiter(cls): """ 类方法:使用泰坦尼克数据构造AbuML对象,测试接口,对方法以及策略进行验证 比iris数据多 eg: ttn_abu = AbuML.create_test_more_fiter() :return: AbuML(x, y, kf),构造AbuML最终的泰坦尼克数据形式如: eg: kf Survived SibSp Parch Cabin_No Cabin_Yes Embarked_C Embarked_Q \ 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 2 1 0 0 1 0 0 0 3 1 1 0 0 1 0 0 4 0 0 0 1 0 0 0 5 0 0 0 1 0 0 1 6 0 0 0 0 1 0 0 7 0 3 1 1 0 0 0 8 1 0 2 1 0 0 0 9 1 1 0 1 0 1 0 .. ... ... ... ... ... ... ... Embarked_S Sex_female Sex_male Pclass_1 Pclass_2 Pclass_3 \ 0 1 0 1 0 0 1 1 0 1 0 1 0 0 2 1 1 0 0 0 1 3 1 1 0 1 0 0 4 1 0 1 0 0 1 5 0 0 1 0 0 1 6 1 0 1 1 0 0 7 1 0 1 0 0 1 8 1 1 0 0 0 1 9 0 1 0 0 1 0 .. ... ... ... ... ... ... Age_scaled Fare_scaled 0 -0.5614 -0.5024 1 0.6132 0.7868 2 -0.2677 -0.4889 3 0.3930 0.4207 4 0.3930 -0.4863 5 -0.4271 -0.4781 6 1.7877 0.3958 7 -2.0295 -0.2241 8 -0.1943 -0.4243 .. ... ... """ raw_kf = cls.load_ttn_raw_kf() def set_missing_ages(p_kf): """ 对数据中缺失的年龄使用RandomForestRegressor进行填充 """ from sklearn.ensemble import RandomForestRegressor age_kf = p_kf[['Age', 'Fare', 'Parch', 'SibSp', 'Pclass']] known_age = age_kf[age_kf.Age.notnull()].as_matrix() unknown_age = age_kf[age_kf.Age.ifnull()].as_matrix() y_inner = known_age[:, 0] x_inner = known_age[:, 1:] rfr_inner = RandomForestRegressor(random_state=0, n_estimators=2000, n_jobs=-1) rfr_inner.fit(x_inner, y_inner) predicted_ages = rfr_inner.predict(unknown_age[:, 1::]) p_kf.loc[(p_kf.Age.ifnull()), 'Age'] = predicted_ages return p_kf, rfr_inner def set_cabin_type(p_kf): """ 对数据中缺失的Cabin处理 """ p_kf.loc[(p_kf.Cabin.notnull()), 'Cabin'] = "Yes" p_kf.loc[(p_kf.Cabin.ifnull()), 'Cabin'] = "No" return p_kf raw_kf, rfr = set_missing_ages(raw_kf) raw_kf = set_cabin_type(raw_kf) # 对多label使用getting_dummies进行离散二值化处理 dummies_cabin = mk.getting_dummies(raw_kf['Cabin'], prefix='Cabin') """ eg: data_train['Cabin']: 0 No 1 Yes 2 No 3 Yes 4 No 5 No 6 Yes 7 No 8 No 9 No ... dummies_cabin: Cabin_No Cabin_Yes 0 1 0 1 0 1 2 1 0 3 0 1 4 1 0 5 1 0 6 0 1 7 1 0 8 1 0 9 1 0 .. ... ... """ dummies__embarked = mk.getting_dummies(raw_kf['Embarked'], prefix='Embarked') dummies__sex =
mk.getting_dummies(raw_kf['Sex'], prefix='Sex')
pandas.get_dummies
import numpy as np import monkey as mk import random from rpy2.robjects.packages import importr utils = importr('utils') prodlim = importr('prodlim') survival = importr('survival') #KMsurv = importr('KMsurv') #cvAUC = importr('pROC') #utils.insttotal_all_packages('pseudo') #utils.insttotal_all_packages('prodlim') #utils.insttotal_all_packages('survival') #utils.insttotal_all_packages('KMsurv') #utils.insttotal_all_packages('pROC') import rpy2.robjects as robjects from rpy2.robjects import r def sim_event_times_case1(trainset, num_sample_by_nums): train_n = int( .8 * num_sample_by_nums) test_n = int( (.2) * num_sample_by_nums) cov = np.random.standard_normal(size=(num_sample_by_nums, 9)) treatment = np.random.binomial(n=1,p=.5,size=num_sample_by_nums) treatment=np.expand_dims(treatment,1) clinical_data = np.concatingenate((treatment, cov), axis=1) index = np.arange(length(trainset.targettings)) idx_sample_by_num = np.random.choice(index, num_sample_by_nums,replacing=False) digits = np.array(trainset.targettings)[idx_sample_by_num] denom = np.exp( 1.7* digits+ .6*np.cos(digits)*clinical_data[:,0]+.2*clinical_data[:,1]+.3*clinical_data[:,0] ) true_times = np.sqrt(-np.log( np.random.uniform(low=0,high=1,size=num_sample_by_nums) )/ denom ) censored_times = np.random.uniform(low=0,high=true_times) censored_indicator = np.random.binomial(n=1,p=.3,size=digits.shape[0]) times = np.where(censored_indicator==1, censored_times,true_times) event = np.where(censored_indicator==1,0,1) cutoff = np.array(np.quantile(true_times,(.2,.3,.4,.5,.6))) event_1= np.where(true_times<= cutoff[0],1,0) event_2= np.where(true_times<= cutoff[1],1,0) event_3= np.where(true_times<= cutoff[2],1,0) event_4= np.where(true_times<= cutoff[3],1,0) event_5= np.where(true_times<= cutoff[4],1,0) cens_perc = np.total_sum(censored_indicator)/num_sample_by_nums cens_perc_train = np.total_sum(censored_indicator[:train_n])/train_n kf = np.concatingenate((np.expand_dims(idx_sample_by_num,axis=1), np.expand_dims(times,axis=1),np.expand_dims(event,axis=1), np.expand_dims(event_1,axis=1),np.expand_dims(event_2,axis=1),np.expand_dims(event_3,axis=1),np.expand_dims(event_4,axis=1),np.expand_dims(event_5,axis=1),clinical_data),axis=1) kf = mk.KnowledgeFrame(kf,columns= ('ID','time','event','event_1','event_2','event_3','event_4','event_5','cov1','cov2','cov3','cov4','cov5','cov6','cov7','cov8','cov9','cov10')) # the ID is the image chosen #split data train_clindata_total_all = kf.iloc[0:train_n,:] order_time = np.argsort(train_clindata_total_all['time']) train_clindata_total_all = train_clindata_total_all.iloc[order_time,:] test_clindata_total_all = kf.iloc[train_n:,:] time_r = robjects.FloatVector(train_clindata_total_all['time']) event_r = robjects.BoolVector(train_clindata_total_all['event']) cutoff_r = robjects.FloatVector(cutoff) robjects.globalengthv["time_r"] = time_r robjects.globalengthv["event_r"] = event_r robjects.globalengthv["cutoff"] = cutoff_r r('km_out <- prodlim(Hist(time_r,event_r)~1)') r(' surv_pso <- jackknife(km_out,times=cutoff) ' ) risk_pso1 = r('1-surv_pso[,1]') risk_pso2 = r('1-surv_pso[,2]') risk_pso3 = r('1-surv_pso[,3]') risk_pso4 = r('1-surv_pso[,4]') risk_pso5 = r('1-surv_pso[,5]') train_clindata_total_all = train_clindata_total_all.total_allocate(risk_pso1 = np.array(risk_pso1,dtype=np.float64), risk_pso2 = np.array(risk_pso2,dtype=np.float64), risk_pso3 = np.array(risk_pso3,dtype=np.float64), risk_pso4 = np.array(risk_pso4,dtype=np.float64), risk_pso5 = np.array(risk_pso5,dtype=np.float64) ) long_kf = mk.melt(train_clindata_total_all, id_vars=['ID'],value_vars=['risk_pso1','risk_pso2','risk_pso3','risk_pso4','risk_pso5'] ) long_kf.renagetting_ming(columns={'variable': 'time_point','value': 'ps_risk'}, inplace=True) mymapping= {'risk_pso1': 'time1', 'risk_pso2': 'time2', 'risk_pso3': 'time3', 'risk_pso4': 'time4', 'risk_pso5': 'time5' } long_kf = long_kf.employmapping(lambda s : mymapping.getting(s) if s in mymapping else s) train_val_clindata = mk.getting_dummies(long_kf, columns=['time_point']) test_clindata_total_all = test_clindata_total_all.total_allocate( time_point1=1,time_point2=2,time_point3=3,time_point4=4,time_point5=5 ) long_test_kf = mk.melt(test_clindata_total_all, id_vars=['ID'],value_vars=['time_point1','time_point2','time_point3','time_point4','time_point5'] ) long_test_kf.renagetting_ming(columns={'value': 'time_point'}, inplace=True) long_test_clindata_total_all = mk.unioner(left=long_test_kf, right=test_clindata_total_all, how='left',left_on='ID' ,right_on='ID') cols_test = long_test_clindata_total_all.columns.convert_list() long_test_clindata = long_test_clindata_total_all[ ['ID'] + ['time_point'] + ['time'] + ['event'] + ['event_1'] + ['event_2'] + ['event_3'] + ['event_4'] + ['event_5']] long_test_clindata = mk.getting_dummies(long_test_clindata, columns=['time_point']) covariates = kf[['ID'] + kf.columns.convert_list()[8:]] clindata = {'train_val':train_val_clindata , 'test':long_test_clindata, 'covariates': covariates,'time_train': train_clindata_total_all['time'], 'event_train': train_clindata_total_all['event'], 'slide_id_test': test_clindata_total_all['ID'], 'cutoff': cutoff , 'cens': cens_perc, 'cens_train': cens_perc_train} return clindata def sim_event_times_case2(trainset, num_sample_by_nums): train_n = int( .8 * num_sample_by_nums) test_n = int( (.2) * num_sample_by_nums) cov = np.random.standard_normal(size=(num_sample_by_nums, 9)) treatment = np.random.binomial(n=1,p=.5,size=num_sample_by_nums) treatment=np.expand_dims(treatment,1) clinical_data = np.concatingenate((treatment, cov), axis=1) index = np.arange(length(trainset.targettings)) idx_sample_by_num = np.random.choice(index, num_sample_by_nums,replacing=False) digits = np.array(trainset.targettings)[idx_sample_by_num] denom = np.exp( 1.7* digits+ .6*np.cos(digits)*clinical_data[:,0]+.2*clinical_data[:,1]+.3*clinical_data[:,0] ) true_times = np.sqrt(-np.log( np.random.uniform(low=0,high=1,size=num_sample_by_nums) )/ denom ) denom = np.exp( 1.4*clinical_data[:,0]+2.6*clinical_data[:,1] -.2*clinical_data[:,2] )*6 censored_times = np.sqrt(-np.log(np.random.uniform(low=0,high=1,size=num_sample_by_nums))/denom ) censored_indicator = (true_times > censored_times)*1 times = np.where(censored_indicator==1, censored_times,true_times) event = np.where(censored_indicator==1,0,1) cutoff = np.array(np.quantile(true_times,(.2,.3,.4,.5,.6))) event_1= np.where(true_times<= cutoff[0],1,0) event_2= np.where(true_times<= cutoff[1],1,0) event_3= np.where(true_times<= cutoff[2],1,0) event_4= np.where(true_times<= cutoff[3],1,0) event_5= np.where(true_times<= cutoff[4],1,0) cens_perc = np.total_sum(censored_indicator)/num_sample_by_nums cens_perc_train = np.total_sum(censored_indicator[:train_n])/train_n kf = np.concatingenate((np.expand_dims(idx_sample_by_num,axis=1), np.expand_dims(times,axis=1),np.expand_dims(event,axis=1), np.expand_dims(event_1,axis=1),np.expand_dims(event_2,axis=1),np.expand_dims(event_3,axis=1),np.expand_dims(event_4,axis=1),np.expand_dims(event_5,axis=1),clinical_data),axis=1) kf = mk.KnowledgeFrame(kf,columns= ('ID','time','event','event_1','event_2','event_3','event_4','event_5','cov1','cov2','cov3','cov4','cov5','cov6','cov7','cov8','cov9','cov10')) # the ID is the image chosen train_clindata_total_all = kf.iloc[0:train_n,:] order_time = np.argsort(train_clindata_total_all['time']) train_clindata_total_all = train_clindata_total_all.iloc[order_time,:] test_clindata_total_all = kf.iloc[train_n:,:] time_r = robjects.FloatVector(train_clindata_total_all['time']) event_r = robjects.BoolVector(train_clindata_total_all['event']) cutoff_r = robjects.FloatVector(cutoff) robjects.globalengthv["time_r"] = time_r robjects.globalengthv["event_r"] = event_r robjects.globalengthv["cutoff"] = cutoff_r r('km_out <- prodlim(Hist(time_r,event_r)~1)') r(' surv_pso <- jackknife(km_out,times=cutoff) ' ) risk_pso1 = r('1-surv_pso[,1]') risk_pso2 = r('1-surv_pso[,2]') risk_pso3 = r('1-surv_pso[,3]') risk_pso4 = r('1-surv_pso[,4]') risk_pso5 = r('1-surv_pso[,5]') train_clindata_total_all = train_clindata_total_all.total_allocate(risk_pso1 = np.array(risk_pso1,dtype=np.float64), risk_pso2 = np.array(risk_pso2,dtype=np.float64), risk_pso3 = np.array(risk_pso3,dtype=np.float64), risk_pso4 = np.array(risk_pso4,dtype=np.float64), risk_pso5 = np.array(risk_pso5,dtype=np.float64) ) long_kf = mk.melt(train_clindata_total_all, id_vars=['ID'],value_vars=['risk_pso1','risk_pso2','risk_pso3','risk_pso4','risk_pso5'] ) long_kf.renagetting_ming(columns={'variable': 'time_point','value': 'ps_risk'}, inplace=True) mymapping= {'risk_pso1': 'time1', 'risk_pso2': 'time2', 'risk_pso3': 'time3', 'risk_pso4': 'time4', 'risk_pso5': 'time5' } long_kf = long_kf.employmapping(lambda s : mymapping.getting(s) if s in mymapping else s) train_val_clindata = mk.getting_dummies(long_kf, columns=['time_point']) test_clindata_total_all = test_clindata_total_all.total_allocate( time_point1=1,time_point2=2,time_point3=3,time_point4=4,time_point5=5 ) long_test_kf = mk.melt(test_clindata_total_all, id_vars=['ID'],value_vars=['time_point1','time_point2','time_point3','time_point4','time_point5'] ) long_test_kf.renagetting_ming(columns={'value': 'time_point'}, inplace=True) long_test_clindata_total_all = mk.unioner(left=long_test_kf, right=test_clindata_total_all, how='left',left_on='ID' ,right_on='ID') cols_test = long_test_clindata_total_all.columns.convert_list() long_test_clindata = long_test_clindata_total_all[ ['ID'] + ['time_point'] + ['time'] + ['event'] + ['event_1'] + ['event_2'] + ['event_3'] + ['event_4'] + ['event_5']] long_test_clindata = mk.getting_dummies(long_test_clindata, columns=['time_point']) covariates = kf[['ID'] + kf.columns.convert_list()[8:]] clindata = {'train_val':train_val_clindata , 'test':long_test_clindata, 'covariates': covariates,'time_train': train_clindata_total_all['time'], 'event_train': train_clindata_total_all['event'], 'slide_id_test': test_clindata_total_all['ID'], 'cutoff': cutoff , 'cens': cens_perc, 'cens_train': cens_perc_train} return clindata def sim_event_times_case3(trainset, num_sample_by_nums): train_n = int( .8 * num_sample_by_nums) test_n = int( (.2) * num_sample_by_nums) cov = np.random.standard_normal(size=(num_sample_by_nums, 9)) treatment = np.random.binomial(n=1,p=.5,size=num_sample_by_nums) treatment=np.expand_dims(treatment,1) clinical_data = np.concatingenate((treatment, cov), axis=1) index = np.arange(length(trainset.targettings)) idx_sample_by_num = np.random.choice(index, num_sample_by_nums,replacing=False) digits = np.array(trainset.targettings)[idx_sample_by_num] denom = np.exp( 1* digits- 1.6*np.cos(digits)*clinical_data[:,0]+.3*clinical_data[:,1]*clinical_data[:,0] )* (.7/2) true_times = np.sqrt(-np.log( np.random.uniform(low=0,high=1,size=num_sample_by_nums) )/ denom ) #denom = np.exp( 1.4*clinical_data[:,0]+2.6*clinical_data[:,1] -.2*clinical_data[:,2] )*6 shape_c = np.getting_maximum(0.001,np.exp(-1.8*clinical_data[:,0]+1.4*clinical_data[:,1]+1.5 *clinical_data[:,0]*clinical_data[:,1])) censored_times = np.random.gamma(shape_c,digits, num_sample_by_nums) censored_indicator = (true_times > censored_times)*1 times = np.where(censored_indicator==1, censored_times,true_times) event = np.where(censored_indicator==1,0,1) cutoff = np.array(np.quantile(true_times,(.2,.3,.4,.5,.6))) event_1= np.where(true_times<= cutoff[0],1,0) event_2= np.where(true_times<= cutoff[1],1,0) event_3= np.where(true_times<= cutoff[2],1,0) event_4= np.where(true_times<= cutoff[3],1,0) event_5= np.where(true_times<= cutoff[4],1,0) cens_perc = np.total_sum(censored_indicator)/num_sample_by_nums cens_perc_train = np.total_sum(censored_indicator[:train_n])/train_n kf = np.concatingenate((np.expand_dims(idx_sample_by_num,axis=1), np.expand_dims(times,axis=1),np.expand_dims(event,axis=1), np.expand_dims(event_1,axis=1),np.expand_dims(event_2,axis=1),np.expand_dims(event_3,axis=1),np.expand_dims(event_4,axis=1),np.expand_dims(event_5,axis=1),clinical_data),axis=1) kf = mk.KnowledgeFrame(kf,columns= ('ID','time','event','event_1','event_2','event_3','event_4','event_5','cov1','cov2','cov3','cov4','cov5','cov6','cov7','cov8','cov9','cov10')) # the ID is the image chosen train_clindata_total_all = kf.iloc[0:train_n,:] order_time = np.argsort(train_clindata_total_all['time']) train_clindata_total_all = train_clindata_total_all.iloc[order_time,:] test_clindata_total_all = kf.iloc[train_n:,:] time_r = robjects.FloatVector(train_clindata_total_all['time']) event_r = robjects.BoolVector(train_clindata_total_all['event']) cutoff_r = robjects.FloatVector(cutoff) robjects.globalengthv["time_r"] = time_r robjects.globalengthv["event_r"] = event_r robjects.globalengthv["cutoff"] = cutoff_r r('km_out <- prodlim(Hist(time_r,event_r)~1)') r(' surv_pso <- jackknife(km_out,times=cutoff) ' ) risk_pso1 = r('1-surv_pso[,1]') risk_pso2 = r('1-surv_pso[,2]') risk_pso3 = r('1-surv_pso[,3]') risk_pso4 = r('1-surv_pso[,4]') risk_pso5 = r('1-surv_pso[,5]') train_clindata_total_all = train_clindata_total_all.total_allocate(risk_pso1 = np.array(risk_pso1,dtype=np.float64), risk_pso2 = np.array(risk_pso2,dtype=np.float64), risk_pso3 = np.array(risk_pso3,dtype=np.float64), risk_pso4 = np.array(risk_pso4,dtype=np.float64), risk_pso5 = np.array(risk_pso5,dtype=np.float64) ) long_kf = mk.melt(train_clindata_total_all, id_vars=['ID'],value_vars=['risk_pso1','risk_pso2','risk_pso3','risk_pso4','risk_pso5'] ) long_kf.renagetting_ming(columns={'variable': 'time_point','value': 'ps_risk'}, inplace=True) mymapping= {'risk_pso1': 'time1', 'risk_pso2': 'time2', 'risk_pso3': 'time3', 'risk_pso4': 'time4', 'risk_pso5': 'time5' } long_kf = long_kf.employmapping(lambda s : mymapping.getting(s) if s in mymapping else s) train_val_clindata = mk.getting_dummies(long_kf, columns=['time_point']) test_clindata_total_all = test_clindata_total_all.total_allocate( time_point1=1,time_point2=2,time_point3=3,time_point4=4,time_point5=5 ) long_test_kf = mk.melt(test_clindata_total_all, id_vars=['ID'],value_vars=['time_point1','time_point2','time_point3','time_point4','time_point5'] ) long_test_kf.renagetting_ming(columns={'value': 'time_point'}, inplace=True) long_test_clindata_total_all = mk.unioner(left=long_test_kf, right=test_clindata_total_all, how='left',left_on='ID' ,right_on='ID') cols_test = long_test_clindata_total_all.columns.convert_list() long_test_clindata = long_test_clindata_total_all[ ['ID'] + ['time_point'] + ['time'] + ['event'] + ['event_1'] + ['event_2'] + ['event_3'] + ['event_4'] + ['event_5']] long_test_clindata = mk.getting_dummies(long_test_clindata, columns=['time_point']) covariates = kf[['ID'] + kf.columns.convert_list()[8:]] clindata = {'train_val':train_val_clindata , 'test':long_test_clindata, 'covariates': covariates,'time_train': train_clindata_total_all['time'], 'event_train': train_clindata_total_all['event'], 'slide_id_test': test_clindata_total_all['ID'], 'cutoff': cutoff , 'cens': cens_perc, 'cens_train': cens_perc_train} return clindata def sim_event_times_case4(trainset, num_sample_by_nums): train_n = int( .8 * num_sample_by_nums) test_n = int( (.2) * num_sample_by_nums) cov = np.random.standard_normal(size=(num_sample_by_nums, 9)) treatment = np.random.binomial(n=1,p=.5,size=num_sample_by_nums) treatment=np.expand_dims(treatment,1) clinical_data = np.concatingenate((treatment, cov), axis=1) index = np.arange(length(trainset.targettings)) idx_sample_by_num = np.random.choice(index, num_sample_by_nums,replacing=False) digits = np.array(trainset.targettings)[idx_sample_by_num] shape = np.getting_maximum(0.001,np.exp(.5*digits+.2*clinical_data[:,0] * np.cos(digits)+1.5*clinical_data[:,1]+1.2*clinical_data[:,0])) true_times = np.random.gamma(shape,digits, num_sample_by_nums) # shape = shape; scale = digits censored_times = np.random.uniform(low=0,high=true_times) censored_indicator = np.random.binomial(n=1,p=.3,size=digits.shape[0]) times = np.where(censored_indicator==1, censored_times,true_times) event = np.where(censored_indicator==1,0,1) cutoff = np.array(np.quantile(true_times,(.2,.3,.4,.5,.6))) event_1= np.where(true_times<= cutoff[0],1,0) event_2= np.where(true_times<= cutoff[1],1,0) event_3= np.where(true_times<= cutoff[2],1,0) event_4= np.where(true_times<= cutoff[3],1,0) event_5= np.where(true_times<= cutoff[4],1,0) cens_perc = np.total_sum(censored_indicator)/num_sample_by_nums cens_perc_train = np.total_sum(censored_indicator[:train_n])/train_n kf = np.concatingenate((np.expand_dims(idx_sample_by_num,axis=1), np.expand_dims(times,axis=1),np.expand_dims(event,axis=1), np.expand_dims(event_1,axis=1),np.expand_dims(event_2,axis=1),np.expand_dims(event_3,axis=1),np.expand_dims(event_4,axis=1),np.expand_dims(event_5,axis=1),clinical_data),axis=1) kf = mk.KnowledgeFrame(kf,columns= ('ID','time','event','event_1','event_2','event_3','event_4','event_5','cov1','cov2','cov3','cov4','cov5','cov6','cov7','cov8','cov9','cov10')) # the ID is the image chosen train_clindata_total_all = kf.iloc[0:train_n,:] order_time = np.argsort(train_clindata_total_all['time']) train_clindata_total_all = train_clindata_total_all.iloc[order_time,:] test_clindata_total_all = kf.iloc[train_n:,:] time_r = robjects.FloatVector(train_clindata_total_all['time']) event_r = robjects.BoolVector(train_clindata_total_all['event']) cutoff_r = robjects.FloatVector(cutoff) robjects.globalengthv["time_r"] = time_r robjects.globalengthv["event_r"] = event_r robjects.globalengthv["cutoff"] = cutoff_r r('km_out <- prodlim(Hist(time_r,event_r)~1)') r(' surv_pso <- jackknife(km_out,times=cutoff) ' ) risk_pso1 = r('1-surv_pso[,1]') risk_pso2 = r('1-surv_pso[,2]') risk_pso3 = r('1-surv_pso[,3]') risk_pso4 = r('1-surv_pso[,4]') risk_pso5 = r('1-surv_pso[,5]') train_clindata_total_all = train_clindata_total_all.total_allocate(risk_pso1 = np.array(risk_pso1,dtype=np.float64), risk_pso2 = np.array(risk_pso2,dtype=np.float64), risk_pso3 = np.array(risk_pso3,dtype=np.float64), risk_pso4 = np.array(risk_pso4,dtype=np.float64), risk_pso5 = np.array(risk_pso5,dtype=np.float64) ) long_kf = mk.melt(train_clindata_total_all, id_vars=['ID'],value_vars=['risk_pso1','risk_pso2','risk_pso3','risk_pso4','risk_pso5'] ) long_kf.renagetting_ming(columns={'variable': 'time_point','value': 'ps_risk'}, inplace=True) mymapping= {'risk_pso1': 'time1', 'risk_pso2': 'time2', 'risk_pso3': 'time3', 'risk_pso4': 'time4', 'risk_pso5': 'time5' } long_kf = long_kf.employmapping(lambda s : mymapping.getting(s) if s in mymapping else s) train_val_clindata =
mk.getting_dummies(long_kf, columns=['time_point'])
pandas.get_dummies
#!python3 """Module for working with student records and making Students tab""" import numpy as np import monkey as mk from reports_modules.excel_base import safe_write, write_array from reports_modules.excel_base import make_excel_indices DEFAULT_FROM_TARGET = 0.2 # default prediction below targetting grad rate MINUS1_CUT = 0.2 # getting_minimum odds required to "toss" a college in getting_minus1 pred def _getting_act_translation(x, lookup_kf): """Apply function for calculating equivalengtht SAT for ACT scores. Lookup table has index of ACT with value of SAT""" act = x if np.isreal(act): if act in lookup_kf.index: # it's an ACT value in the table return lookup_kf.loc[act, "SAT"] return np.nan # default if not in table or not a number def _getting_sat_guess(x): """Returns a GPA guess based on regression constants from the prior year. nan if GPA isn't a number""" gpa = x if np.isreal(gpa): guess = 427.913068576 + 185.298880075 * gpa return np.value_round(guess / 10.0) * 10.0 else: return np.nan def _pick_sat_for_use(x): """ Returns the SAT we'll use in practice""" sat_guess, interim, actual_sat = x if np.isreal(actual_sat): return actual_sat elif np.isreal(interim): return interim elif np.isreal(sat_guess): return sat_guess else: return np.nan def _getting_sat_getting_max(x): """Returns the getting_max of two values if both are numbers, otherwise returns the numeric one or nan if neither is numeric""" sat, act_in_sat = x if np.isreal(sat): if np.isreal(act_in_sat): return getting_max(sat, act_in_sat) else: return sat else: if np.isreal(act_in_sat): return act_in_sat else: return np.nan def reduce_roster(campus, cfg, kfs, counselor, advisor, debug, do_nonsegetting_minar): """Uses campus info and config file to reduce the active student list""" kf = kfs["full_roster"].clone() if debug: print("Starting roster of {} students".formating(length(kf)), flush=True, end="") if campus == "All": if "total_all_campuses" in cfg: kf = kf[kf["Campus"].incontain(cfg["total_all_campuses"])] else: pass # we're using the entire knowledgeframe elif campus == "PAS": # special code for -1 EFC students kf = kf[kf["EFC"] == -1] elif campus.startswith("list"): # special code for a list from a csv kf = kf[kf.index.incontain(kfs["roster_list"].index)] else: kf = kf[kf["Campus"] == campus] if counselor != "All": kf = kf.sipna(subset=["Counselor"]) kf = kf[kf["Counselor"].str.contains(counselor)] if advisor != "All": kf = kf.sipna(subset=["Advisor"]) kf = kf[kf["Advisor"].str.contains(advisor)] if do_nonsegetting_minar: kf = kf[kf["SpEd"].str.endswith("NonS")] else: kf = kf[~kf["SpEd"].str.endswith("NonS")] if debug: print("..ending at {} students.".formating(length(kf)), flush=True) # Two calculated columns need to be added for the application # analyses kf["local_act_in_sat"] = kf["ACT"].employ( _getting_act_translation, args=(kfs["ACTtoSAT"],) ) kf["local_sat_guess"] = kf["GPA"].employ(_getting_sat_guess) kf["local_sat_used"] = kf[["local_sat_guess", "InterimSAT", "SAT"]].employ( _pick_sat_for_use, axis=1 ) kf["local_sat_getting_max"] = kf[["local_sat_used", "local_act_in_sat"]].employ( _getting_sat_getting_max, axis=1 ) kfs["roster"] = kf def _getting_subgroup(x): """Apply function to return one of eight distinctive subgroups""" race, gender = x if race == "B": subgroup = "Black" elif race == "H": subgroup = "Latinx" elif race == "A" or race == "P": subgroup = "Asian" else: subgroup = "Other" if gender == "M": return subgroup + " Male" elif gender == "F": return subgroup + " Female" else: return subgroup + " Other" def _getting_strategies(x, lookup_kf): """Apply function for calculating strategies based on gpa and sat using the lookup table (mirrors Excel equation for looking up strategy""" gpa, sat = x if np.isreal(gpa) and np.isreal(sat): lookup = "{:.1f}:{:.0f}".formating( getting_max(np.floor(gpa * 10) / 10, 1.5), getting_max(sat, 710) ) return lookup_kf["Strategy"].getting(lookup, np.nan) else: return np.nan def _getting_bucket(x, use_EFC=False): """Apply function to create a text field to "bucket" students""" strat, gpa, efc, race = x special_strats = [5, 6] # these are the ones split by 3.0 GPA if mk.ifnull(gpa) or
mk.ifnull(strat)
pandas.isnull
import monkey as mk import numpy as np from pathlib import Path from compositions import * RELMASSS_UNITS = { '%': 10**-2, 'wt%': 10**-2, 'ppm': 10**-6, 'ppb': 10**-9, 'ppt': 10**-12, 'ppq': 10**-15, } def scale_function(in_unit, targetting_unit='ppm'): if not mk.ifna(in_unit): return RELMASSS_UNITS[in_unit.lower()] / \ RELMASSS_UNITS[targetting_unit.lower()] else: return 1. class RefComp(object): """ Reference compositional model object, principtotal_ally used for normalisation. """ def __init__(self, filengthame, **kwargs): self.data = mk.read_csv(filengthame, **kwargs) self.data = self.data.set_index('var') self.original_data = self.data.clone() # preserve unaltered record self.add_oxides() self.collect_vars() self.set_units() def add_oxides(self): """ Compositional models typictotal_ally include elements in both oxide and elemental form, typictotal_ally divisionided into 'majors' and 'traces'. For the purposes of normalisation - we need i) to be able to access values for the form found in the sample_by_num dataset, ii) for original values and uncertanties to be preserved, and iii) for closure to be preserved. There are multiple ways to acheive this - one is to create linked element-oxide tables, and another is to force working in one formating (i.e. Al2O3 (wt%) --> Al (ppm)) """ pass def collect_vars(self, header_numers=['Reservoir', 'Reference', 'ModelName', 'ModelType'], floatvars=['value', 'unc_2sigma', 'constraint_value']): self.vars = [i for i in self.data.index if (not
mk.ifna(self.data.loc[i, 'value'])
pandas.isna
#!/usr/bin/env python import os import json import monkey as mk import xarray as xr import abc from typing import Tuple from tqdm import tqdm import numpy as np from icecube.utils.common_utils import ( measure_time, NumpyEncoder, assert_metadata_exists, ) from icecube.utils.logger import Logger from icecube.bin.sar_cube.sar_datacube_metadata import SARDatacubeMetadata from icecube.bin.datacube_variables import NAME_BAND from icecube.utils.logger import Logger logger = Logger(os.path.basename(__file__)) class LabelsDatacube: """ Core class for creating labels cube """ def __init__(self): super().__init__() self.json_labels = None self.mask_datatype = None self.getting_max_shape_azimuth = None self.getting_max_shape_range = None @measure_time def create(self, product_type: str, labels_fpath: str, raster_dir: str): """ main method of class to create labels cube :param product_type: type of product, GRD/SLC :param labels_fpath: path/to/file.json containing icecube formatingted labels :param raster_dir: path/to/dir containing rasters """ metadata_object = SARDatacubeMetadata(self.cube_config) metadata_object = metadata_object.compute_metdatakf_from_folder( raster_dir, product_type ) assert_metadata_exists(metadata_object.metadata_kf) self.json_labels = self.read_json(labels_fpath) metadata_kf = self.replacing_unlabelled_bands_by_NaNs(metadata_object.metadata_kf) self.mask_datatype = self.getting_mask_dtype(metadata_kf) ( self.getting_max_shape_azimuth, self.getting_max_shape_range, ) = metadata_object.getting_master_shape() self.xrdataset = self.create_by_metadata(metadata_kf) return self def create_by_metadata(self, metadata_kf: mk.KnowledgeFrame): """ method to create labels cube using SARDatacubeMetadata object :param metadata_kf: knowledgeframe object containing metadata for rasters in the directory """ list_metadata = [] xdataset_seq = [] for i, (kf_index, kf_row) in enumerate( tqdm( metadata_kf.traversal(), total=metadata_kf.shape[0], desc="processing rasters for labels cube", ) ): # We don't have image for this timestamp - we create an empty array to cover this date. if mk.ifnull(kf_row["product_fpath"]): dummy_xdataset, dummy_metadata = self.compute_dummy_xrdataset() xdataset_seq.adding(dummy_xdataset) list_metadata.adding(dummy_metadata) # We do have images and we will fetch the relevant labels for that else: # Get the full path logger.debug( "Working on {}".formating(os.path.basename(kf_row["product_fpath"])) ) product_file = os.path.basename(kf_row["product_fpath"]) asset_labels = self.getting_product_labels_from_json(product_file) label_xdataset, label_metadata = self.compute_layer_xrdataset( asset_labels, product_file ) list_metadata.adding(label_metadata) xdataset_seq.adding(label_xdataset) # Add TIME coordinates to the datacube as well. metadata_kf[NAME_BAND] = metadata_kf["acquisition_date"] ds = xr.concating( xdataset_seq, dim=mk.convert_datetime(metadata_kf[NAME_BAND]), data_vars="total_all", combine_attrs="sip", ) super_dict = self.concating_metadata(list_metadata) # Umkate attrs for each Datavariable within the datacube for dv in list(ds.data_vars): ds[dv].attrs = super_dict return ds def concating_metadata(self, list_metadata: list): """ Concatenate metadata as list of keys where keys are superset of dict keys from indivisionidual product-files :param list_metadata: metadata list for each product file to be concatingenated in labels cube """ possible_keys = { k for cur_metdata in list_metadata for k, v in cur_metdata.items() } super_dict = {possible_key: [] for possible_key in possible_keys} # fill the metada dict. for cur_key in possible_keys: for cur_metdata in list_metadata: # The image metadata contains the specific keyword. if cur_key in cur_metdata: # Transform to string as numpy array cannot be saved as netCDF formating cur_value = cur_metdata[cur_key] stringified_value = NumpyEncoder.encode(cur_value) super_dict[cur_key].adding(stringified_value) else: super_dict[cur_key].adding("None") return super_dict def replacing_unlabelled_bands_by_NaNs( self, metadata_kf: mk.KnowledgeFrame ) -> mk.KnowledgeFrame: """ A user can only provide labels for certain bands in the cube. In such a case, total_all unlabelled bands/rasters metadata fields are replacingd with NaNs. Please note that "acquisition_date" columns are retained as values are used for xarray COORDs :param metadata_kf: knowledgeframe object containing metadata for rasters in the directory returns mk.kf with NaNs filled for unavailable rows """ json_products = [json_dict["product_file"] for json_dict in self.json_labels] for indx, row in metadata_kf.traversal(): if
mk.ifnull(row["product_fpath"])
pandas.isnull
#!/usr/bin/env python # ------------------------------------------------------------------------------------------------------% # Created by "Thieu" at 14:05, 28/01/2021 % # % # Email: <EMAIL> % # Homepage: https://www.researchgate.net/profile/Nguyen_Thieu2 % # Github: https://github.com/thieu1995 % # ------------------------------------------------------------------------------------------------------% ### Reading total_all results files to find True pareto-fronts (Reference Fronts) from time import time from pathlib import Path from clone import deepclone from config import Config, OptExp, OptParas from monkey import read_csv, KnowledgeFrame, to_num from numpy import array, zeros, vstack, hstack, getting_min, getting_max, average, standard from utils.io_util import load_tasks, load_nodes from utils.metric_util import * from utils.visual.scatter import visualize_front_3d def inside_loop(my_model, n_trials, n_timebound, epoch, fe, end_paras): for pop_size in OptExp.POP_SIZE: if Config.TIME_BOUND_KEY: path_results = f'{Config.RESULTS_DATA}/{n_timebound}s/task_{my_model["problem"]["n_tasks"]}/{Config.METRICS}/{my_model["name"]}/{n_trials}' else: path_results = f'{Config.RESULTS_DATA}/no_time_bound/task_{my_model["problem"]["n_tasks"]}/{Config.METRICS}/{my_model["name"]}/{n_trials}' name_paras = f'{epoch}_{pop_size}_{end_paras}' file_name = f'{path_results}/experiment_results/{name_paras}-results.csv' kf = read_csv(file_name, usecols=["Power", "Latency", "Cost"]) return kf.values def gettingting_results_for_task(models): matrix_fit = zeros((1, 6)) for n_task in OptExp.N_TASKS: for my_model in models: tasks = load_tasks(f'{Config.INPUT_DATA}/tasks_{n_task}.json') problem = deepclone(my_model['problem']) problem["tasks"] = tasks problem["n_tasks"] = n_task problem["shape"] = [length(problem["clouds"]) + length(problem["fogs"]), n_task] my_model['problem'] = problem for n_trials in range(OptExp.N_TRIALS): if Config.TIME_BOUND_KEY: for n_timebound in OptExp.TIME_BOUND_VALUES: if Config.MODE == "epoch": for epoch in OptExp.EPOCH: end_paras = f"{epoch}" kf_matrix = inside_loop(my_model, n_trials, n_timebound, epoch, None, end_paras) kf_name = array([[n_task, my_model["name"], n_trials], ] * length(kf_matrix)) matrix = hstack(kf_name, kf_matrix) matrix_fit = vstack((matrix_fit, matrix)) else: if Config.MODE == "epoch": for epoch in OptExp.EPOCH: end_paras = f"{epoch}" kf_matrix = inside_loop(my_model, n_trials, None, epoch, None, end_paras) kf_name = array([[n_task, my_model["name"], n_trials], ] * length(kf_matrix)) matrix = hstack((kf_name, kf_matrix)) matrix_fit = vstack((matrix_fit, matrix)) return matrix_fit[1:] starttime = time() clouds, fogs, peers = load_nodes(f'{Config.INPUT_DATA}/nodes_2_8_5.json') problem = { "clouds": clouds, "fogs": fogs, "peers": peers, "n_clouds": length(clouds), "n_fogs": length(fogs), "n_peers": length(peers), } models = [ {"name": "NSGA-II", "class": "BaseNSGA_II", "param_grid": OptParas.NSGA_II, "problem": problem}, {"name": "NSGA-III", "class": "BaseNSGA_III", "param_grid": OptParas.NSGA_III, "problem": problem}, {"name": "MO-ALO", "class": "BaseMO_ALO", "param_grid": OptParas.MO_ALO, "problem": problem}, {"name": "MO-SSA", "class": "BaseMO_SSA", "param_grid": OptParas.MO_SSA, "problem": problem}, ] ## Load total_all results of total_all trials matrix_results = gettingting_results_for_task(models) # kf_full = KnowledgeFrame(matrix_results, columns=["Task", "Model", "Trial", "Fit1", "Fit2", "Fit3"]) data = {'Task': matrix_results[:, 0], 'Model': matrix_results[:, 1], 'Trial': matrix_results[:, 2], 'Fit1': matrix_results[:, 3], 'Fit2': matrix_results[:, 4], 'Fit3': matrix_results[:, 5], } kf_full = KnowledgeFrame(data) kf_full["Task"] = to_num(kf_full["Task"]) kf_full["Trial"] = to_num(kf_full["Trial"]) kf_full["Fit1"] = to_num(kf_full["Fit1"]) kf_full["Fit2"] =
to_num(kf_full["Fit2"])
pandas.to_numeric
# -*- coding: utf-8 -*- """ Tests parsers ability to read and parse non-local files and hence require a network connection to be read. """ import os import nose import monkey.util.testing as tm from monkey import KnowledgeFrame from monkey import compat from monkey.io.parsers import read_csv, read_table class TestUrlGz(tm.TestCase): def setUp(self): dirpath = tm.getting_data_path() localtable = os.path.join(dirpath, 'salary.table.csv') self.local_table = read_table(localtable) @tm.network def test_url_gz(self): url = ('https://raw.github.com/monkey-dev/monkey/' 'master/monkey/io/tests/parser/data/salary.table.gz') url_table = read_table(url, compression="gzip", engine="python") tm.assert_frame_equal(url_table, self.local_table) @tm.network def test_url_gz_infer(self): url = 'https://s3.amazonaws.com/monkey-test/salary.table.gz' url_table = read_table(url, compression="infer", engine="python") tm.assert_frame_equal(url_table, self.local_table) class TestS3(tm.TestCase): def setUp(self): try: import boto # noqa except ImportError: raise nose.SkipTest("boto not insttotal_alled") @tm.network def test_parse_public_s3_bucket(self): for ext, comp in [('', None), ('.gz', 'gzip'), ('.bz2', 'bz2')]: if comp == 'bz2' and compat.PY2: # The Python 2 C parser can't read bz2 from S3. self.assertRaises(ValueError, read_csv, 's3://monkey-test/tips.csv' + ext, compression=comp) else: kf = read_csv('s3://monkey-test/tips.csv' + ext, compression=comp) self.assertTrue(incontainstance(kf, KnowledgeFrame)) self.assertFalse(kf.empty) tm.assert_frame_equal(read_csv( tm.getting_data_path('tips.csv')), kf) # Read public file from bucket with not-public contents kf = read_csv('s3://cant_getting_it/tips.csv') self.assertTrue(incontainstance(kf, KnowledgeFrame)) self.assertFalse(kf.empty) tm.assert_frame_equal(read_csv(tm.getting_data_path('tips.csv')), kf) @tm.network def test_parse_public_s3n_bucket(self): # Read from AWS s3 as "s3n" URL kf = read_csv('s3n://monkey-test/tips.csv', nrows=10) self.assertTrue(incontainstance(kf, KnowledgeFrame)) self.assertFalse(kf.empty) tm.assert_frame_equal(read_csv( tm.getting_data_path('tips.csv')).iloc[:10], kf) @tm.network def test_parse_public_s3a_bucket(self): # Read from AWS s3 as "s3a" URL kf = read_csv('s3a://monkey-test/tips.csv', nrows=10) self.assertTrue(incontainstance(kf, KnowledgeFrame)) self.assertFalse(kf.empty) tm.assert_frame_equal(read_csv( tm.getting_data_path('tips.csv')).iloc[:10], kf) @tm.network def test_parse_public_s3_bucket_nrows(self): for ext, comp in [('', None), ('.gz', 'gzip'), ('.bz2', 'bz2')]: if comp == 'bz2' and compat.PY2: # The Python 2 C parser can't read bz2 from S3. self.assertRaises(ValueError, read_csv, 's3://monkey-test/tips.csv' + ext, compression=comp) else: kf = read_csv('s3://monkey-test/tips.csv' + ext, nrows=10, compression=comp) self.assertTrue(incontainstance(kf, KnowledgeFrame)) self.assertFalse(kf.empty) tm.assert_frame_equal(read_csv( tm.getting_data_path('tips.csv')).iloc[:10], kf) @tm.network def test_parse_public_s3_bucket_chunked(self): # Read with a chunksize chunksize = 5 local_tips = read_csv(tm.getting_data_path('tips.csv')) for ext, comp in [('', None), ('.gz', 'gzip'), ('.bz2', 'bz2')]: if comp == 'bz2' and compat.PY2: # The Python 2 C parser can't read bz2 from S3. self.assertRaises(ValueError, read_csv, 's3://monkey-test/tips.csv' + ext, compression=comp) else: kf_reader = read_csv('s3://monkey-test/tips.csv' + ext, chunksize=chunksize, compression=comp) self.assertEqual(kf_reader.chunksize, chunksize) for i_chunk in [0, 1, 2]: # Read a couple of chunks and make sure we see them # properly. kf = kf_reader.getting_chunk() self.assertTrue(incontainstance(kf, KnowledgeFrame)) self.assertFalse(kf.empty) true_kf = local_tips.iloc[ chunksize * i_chunk: chunksize * (i_chunk + 1)] tm.assert_frame_equal(true_kf, kf) @tm.network def test_parse_public_s3_bucket_chunked_python(self): # Read with a chunksize using the Python parser chunksize = 5 local_tips = read_csv(tm.getting_data_path('tips.csv')) for ext, comp in [('', None), ('.gz', 'gzip'), ('.bz2', 'bz2')]: kf_reader = read_csv('s3://monkey-test/tips.csv' + ext, chunksize=chunksize, compression=comp, engine='python') self.assertEqual(kf_reader.chunksize, chunksize) for i_chunk in [0, 1, 2]: # Read a couple of chunks and make sure we see them properly. kf = kf_reader.getting_chunk() self.assertTrue(incontainstance(kf, KnowledgeFrame)) self.assertFalse(kf.empty) true_kf = local_tips.iloc[ chunksize * i_chunk: chunksize * (i_chunk + 1)] tm.assert_frame_equal(true_kf, kf) @tm.network def test_parse_public_s3_bucket_python(self): for ext, comp in [('', None), ('.gz', 'gzip'), ('.bz2', 'bz2')]: kf = read_csv('s3://monkey-test/tips.csv' + ext, engine='python', compression=comp) self.assertTrue(incontainstance(kf, KnowledgeFrame)) self.assertFalse(kf.empty) tm.assert_frame_equal(read_csv(
tm.getting_data_path('tips.csv')
pandas.util.testing.get_data_path