How to use viewStats method in fMBT

Best Python code snippet using fMBT_python

Appendix.py

Source:Appendix.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2"""3Created on Tue Aug 25 13:29:33 20204@author: andre5"""6import pandas as pd7import matplotlib.pyplot as plt8import numpy as np9import random as rd10import re11import seaborn as sns12import time13from scipy import spatial14import scipy.stats15from numpy import dot16from numpy.linalg import norm17from collections import Counter18from multiprocessing import Pool19import multiprocessing20# 4. Appendix: Additional functions for analysis.21 22# The following functions worked for data wrangling and preparations. No simulation use.23#Get a gini-coefficient value from a list input.24def G(v):25 bins = np.linspace(0., 100., 11)26 total = float(np.sum(v))27 yvals = []28 for b in bins:29 bin_vals = v[v <= np.percentile(v, b)]30 bin_fraction = (np.sum(bin_vals) / total) * 100.031 yvals.append(bin_fraction)32 # perfect equality area\n",33 pe_area = np.trapz(bins, x=bins)34 # lorenz area\n",35 lorenz_area = np.trapz(yvals, x=bins)36 gini_val = (pe_area - lorenz_area) / float(pe_area)37 return bins, yvals, gini_val38#Plot a single run output.39def Plot_Run(M):40 M = pd.DataFrame(M)41 df2 = M#[M.columns[:-2]]42 43 views,rating_mean=[],[]44 for i in df2.values:45 raw_views = []46 raw_rating_mean = []47 for j in i:48 raw_views.append(float(len(j)))49 raw_rating_mean.append(np.mean(j))50 views = views + raw_views51 rating_mean = rating_mean + raw_rating_mean52 df = pd.DataFrame()53 df['views'] = [len(i) for i in df2.values[1]]#np.array(views)54 df['rating'] = [np.mean(i) for i in df2.values[1]]#np.aray(rating_mean)55 gg = sns.jointplot(x='views',y='rating',data=df)56 plt.show()57 58 df = df.sort_values(by='rating',ascending=False)59 plt.plot(list(df.rating),linewidth=2,alpha=0.5,c='r',label='Rating')60 plt.legend()61 plt.show() 62 df = df.sort_values(by='views',ascending=False)63 plt.plot(list(df.views),linewidth=2,alpha=0.5,c='b',label='Views')64 plt.legend()65 plt.show()66 67def plot_many(size,times,classes):68 for k in range(classes):69 temp = []70 for i in range(times):71 temp.append(evaluation(0,[float(rd.randint(-100,100))/100 for i in range(size)],[float(rd.randint(-100,100))/100 for i in range(size)]))72 plt.hist(temp,bins=50)73 plt.show()74 75#Parse data and save to pickle for posterior analysis. 76def manual_clean(cogni='C3',prcnt=50):77 sizes = [[2000,400],[5000,1000],[10000,2000]]78 dff = pd.DataFrame()79 for s in sizes:80 df = pd.read_pickle("["+str(s[0])+", "+str(s[1])+", 'Sociological', 100]")81 dft = load_basic2(df[df.index == 0],s[0],s[1],cogni)82 for j in range(1,len(df)):83 dft = dft.append(load_basic2(df[df.index == j],s[0],s[1],cogni))84 dff = dff.append(dft)85 dff.to_pickle('Results_FSociological_S100_C'+str(cogni)+'_P'+str(prcnt))86 87#Used to load basic features into a pandas dataframe.88def load_basic2(df00,u,p,cogni):89 df0 = get_basics(df00)90 df0['u_size'] = [u]*len(df0)91 df0['p_size'] = [p]*len(df0)92 df0['filter'] = ['Sociological']*len(df0)93 df0['u_cogni'] = [cogni]*len(df0)94 df0['steps'] = ['100']*len(df0)95 return df096#Supports load_basics by giving aggregating all simulation results97def get_basics(df):98 df2 = df#[M.columns[:-2]]\n"99 viewstats = {'mean':[],'median':[],'std':[],'total':[],'minmax':[]}100 ratingstats = {'mean':[],'median':[],'std':[],'total':[],'minmax':[]}101 for i in df2.values:102 raw_views = []103 raw_rating_mean = []104 for j in i:105 raw_views.append(float(len(j)))106 raw_rating_mean.append(np.mean(j)) #Mind we take totals for views and mean for ratings\n",107 #Get mean, median, stdev, q1, q3, \n",108 viewstats = update_stats(viewstats,raw_views)109 ratingstats = update_stats(ratingstats,raw_rating_mean)110 c,bins,gini = G(np.array(raw_views))111 viewstats['gini'] = gini112 c,bins,gini = G(np.array(raw_rating_mean))113 ratingstats['gini'] = gini114 dfv = pd.DataFrame()115 for k in ['mean','median','std','total','minmax','gini']:116 dfv['v_'+str(k)] = viewstats[str(k)]117 for k in ['mean','median','std','total','minmax','gini']:118 dfv['r_'+str(k)] = ratingstats[str(k)]119 return dfv120 121def update_stats(dct,raw_list):122 dct['mean'].append(np.mean(raw_list))123 dct['median'].append(np.median(raw_list))124 dct['std'].append(np.std(raw_list))125 dct['total'].append(len(raw_list))126 dct['minmax'].append([min(raw_list),max(raw_list)])...

Full Screen

Full Screen

viewstats.py

Source:viewstats.py Github

copy

Full Screen

1# LIBTBX_SET_DISPATCHER_NAME prime.viewstats2'''3Author : Uervirojnangkoorn, M.4Created : 9/19/20145Description : View convergence and other stats for post-refinement.6'''7from __future__ import absolute_import, division, print_function8import matplotlib.pyplot as plt9import sys10import numpy as np11from six.moves import range12if len(sys.argv)==1:13 print('Use prime.viewstats to view convergence of post-refined parameters.')14 print('Usage: prime.viewstats your_run_no')15 exit()16run_no = sys.argv[1]17#read .paramhist files and display refinement results18import os19cn_file = 020for file_in in os.listdir(run_no):21 if file_in.endswith('.paramhist'):22 cn_file += 123if cn_file == 0:24 print('Cannot find .paramhist file in your ', run_no, '.')25 print('To enable viewstats, rerun prime with flag_output_verbose=True in .phil file.')26 print('The .paramhist parameters will be recorded during the run.')27 exit()28param_file_list = []29for i in range(cn_file):30 param_file_list.append(run_no+'/'+str(i)+'.paramhist')31data_dict_list = []32for param_file in param_file_list:33 pf = open(param_file,'r')34 data = pf.read().split('\n')35 data_dict = {}36 n_data = 037 for data_row in data:38 dc = data_row.split()39 #use row 1 to set n_col40 if n_data == 0:41 n_col = len(dc)42 if len(dc)==n_col:43 data_dict[dc[n_col-1]] = np.array([float(dc[i]) for i in range(n_col-1)])44 n_data += 145 data_dict_list.append(data_dict)46#prepare test key47data_dict_0 = data_dict_list[0]48data_dict_1 = data_dict_list[1]49for i in range(n_data):50 test_key = list(data_dict_list[0].keys())[i]51 if (test_key in data_dict_0) and (test_key in data_dict_1):52 test_id = i53 break54#Fix Tpr and Txy for first data_dict55test_param_0_raw = np.array(data_dict_0[test_key])56test_param_1 = data_dict_1[test_key]57for key in data_dict_0.keys():58 if key in data_dict_1:59 data_param_0 = data_dict_0[key]60 data_param_1 = data_dict_1[key]61 data_param_0[1] = data_param_1[0]62 data_param_0[3] = data_param_1[2]63 data_dict_0[key] = data_param_064test_param_0_update = data_dict_0[test_key]65test_delta_1_calc = np.absolute(test_param_1-test_param_0_update)66print('test id', test_id)67print('test key', test_key)68print('0th cycle (raw):', test_param_0_raw)69print('0th cycle (updated):', test_param_0_update)70print('1st cycle:', test_param_1)71print('delta (calc.):', test_delta_1_calc)72delta_dict_list = []73for i in range(len(data_dict_list)-1):74 data_dict = data_dict_list[i]75 data_dict_next = data_dict_list[i+1]76 delta_dict = {}77 for key in data_dict.keys():78 if (key in data_dict_next):79 delta_param = np.absolute(data_dict_next[key] - data_dict[key])80 else:81 delta_param = np.zeros(n_col-1)82 delta_dict[key] = delta_param83 delta_dict_list.append(delta_dict)84delta_dict_0 = delta_dict_list[0]85test_delta_1 = delta_dict_0[test_key]86print('delta (prog.):', test_delta_1)87print('delta diff.:', test_delta_1 - test_delta_1_calc)88print('sum of delta diff.', np.sum(np.absolute(test_delta_1 - test_delta_1_calc)))89x_range = list(range(1, len(delta_dict_list)+1))90x_label = []91for i in range(1, len(delta_dict_list)+1):92 x_label.append(str(i))93data_title = ['Tpr_i','Tpr','Txy_i','Txy','G','B','RotX','RotY','ry','rz','r0','re','voigt_nu','a','b','c','alpha','beta','gamma','CC1/2']94cn_plot = 195for i in range(n_col-1):96 if i not in (0,2):97 data_series = []98 for delta_dict in delta_dict_list:99 narr = np.array([delta_dict[key][i] for key in delta_dict.keys()])100 data_series.append(narr)101 ax = plt.subplot(3, 6, cn_plot, title=data_title[i])102 plt.boxplot(data_series)103 plt.xticks(x_range, x_label)104 if data_title[i] in ('ry','rz','r0','re'):105 plt.ylim([0, 0.01])106 plt.grid(True)107 cn_plot += 1...

Full Screen

Full Screen

database.py

Source:database.py Github

copy

Full Screen

1"""Database interface functions."""2from typing import Dict, Union, Optional, Iterable3from update import comicdata4from datetime import date5from modules.config import MEMBERS, VIEWSTATS, Member, Comic6def db_update(comic_num: int) -> None:7 """8 Updates the database, specifically any entry related to comic_num.9 """10 # Replace the comic11 curr = comicdata.find_one({"nr": comic_num})12 curr["viewed"] = True13 comicdata.replace_one({"nr": comic_num}, curr)14 viewstats = comicdata.find_one(VIEWSTATS)15 viewstats["lviewed"] = comic_num16 viewstats["updated"] = date.today().isoformat()17 # Update the latest viewstats18 comicdata.replace_one(VIEWSTATS, viewstats)19def get_comic(cnum: int, tag: str='c') -> Comic:20 """21 Gets the comic object for Comic #[cnum].22 """23 comic = comicdata.find_one({"nr": cnum, "tag": tag})24 return comic25def get_comics(26 cnum: Optional[int]=None, tag: Optional[str]=None27) -> Iterable[Dict[str, Comic]]:28 """29 Gets a cursor of comics matching the criteria from the database.30 """31 search = {}32 if cnum is not None:33 search["nr"] = cnum34 if tag is not None:35 search["tag"] = tag36 37 return comicdata.find(search)38def get_metadata(field: str) -> int:39 """40 Gets the specific viewstats metadata value indicated by 'field'.41 """42 return comicdata.find_one(VIEWSTATS)[field]43def get_members() -> Dict[Union[str, int], Member]:44 """45 Gets the members list from the database.46 """47 m = comicdata.find_one(MEMBERS)48 return m49# This is a dictionary of people in the server50people = comicdata.find_one(MEMBERS)51people = {52 int(key): value for key, value in people.items()53 if key.isdigit()54}55def set_metadata(field: str, value) -> None:56 """57 Sets the specific metadata value indicated by 'field'.58 """59 viewstats = comicdata.find_one(VIEWSTATS)60 viewstats[field] = value61 comicdata.replace_one(VIEWSTATS, viewstats)62def update_members(members: Dict[str, str]) -> None:63 """64 Updates the members list in the database.65 NOTE: This function exhibits add-only behaviour. You can't66 overwrite old members.67 Parameters68 ----------69 members : dict[str, str]70 A dictionary of members, keyed by Discord ID.71 """72 m = comicdata.find_one(MEMBERS)73 for member in members:74 m[str(member)] = members[member]75 76 comicdata.replace_one(MEMBERS, m)77def get_date(field: str) -> date:78 """79 Gets the date given by the 'field' field in comicdata.viewstats.80 """81 update = comicdata.find_one(VIEWSTATS)[field]82 return date(*map(int, update.split('-')))83def published(cnum: int) -> str:84 """85 Gets the publishing date of Comic #[cnum].86 """87 comic = comicdata.find_one({"nr": cnum})...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run fMBT automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful