How to use list_groups method in tempest

Best Python code snippet using tempest_python

zabbixotrs.py

Source:zabbixotrs.py Github

copy

Full Screen

1#!/usr/bin/python2# -*- coding: utf-8 -*-3"""4Created on Mon Out 22 10:57:32 20185@author: Heberson Aguiar <hebersonaguiar@gmail.com>6"""7from zabbix_api import ZabbixAPI8from pyotrs import Client, Article, Ticket, DynamicField9from mysql.connector import Error10import mysql.connector11import time12import sys13reload(sys)14sys.setdefaultencoding('utf8')15ZBX_SERVER = "http://x.x.x.x/zabbix"16ZBX_USER = sys.argv[1]17ZBX_PASS = sys.argv[2] 18OTRS_SERVER = "http://x.x.x.x"19OTRS_USER = sys.argv[3]20OTRS_PASS = sys.argv[4]21OTRS_DB_NAME = "otrs"22OTRS_DB_USER = sys.argv[5]23OTRS_DB_PASS = sys.argv[6]24OTRS_DB_SERVER = "x.x.x.x"25QUEUE = "9 - Gestão de Liberação::Infraestrutura"26STATE = "Registrada"27PRIORITY = "3 normal"28CUSTOMERUSER = "heberson.aguiar"29SERVICE = "Problema::2 - Sustentação::2.2 - Itens de Hardware::2.2.2 - Servidores virtuais::2.2.2.1 - Investigar"30SLA = "2 - Sustentação::0::NA"31TYPE = "Problema"32def getTriggers(ZBX_SERVER, ZBX_USER, ZBX_PASS):33 zapi = ZabbixAPI(server=ZBX_SERVER)34 zapi.login(ZBX_USER, ZBX_PASS)35 triggers = zapi.trigger.get ({36 "output": ["description", "lastchange", "triggerid", "priority"],37 "selectHosts": ["hostid", "host"],38 "selectLastEvent": ["eventid", "acknowledged", "objectid", "clock", "ns"],39 "sortfield" : "lastchange",40 "monitored": "true",41 "only_true": "true",42 "maintenance": "false",43 "expandDescription": True,44 "filter":{"value":1}45 })46 triggerid = []47 for y in triggers:48 nome_host = y["hosts"][0]["host"]49 severity = int(y["priority"])50 triggerid.append(y["triggerid"])51 52 idade = time.time() - float(y["lastchange"])53 pegadia = "{0.tm_yday}".format(time.gmtime(idade))54 dia = int(pegadia) - 155 duracao = "dias {0.tm_hour} horas {0.tm_min} minutos".format(time.gmtime(idade))56 ultima_alteracao = time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(float(y["lastchange"])))57 58 return triggerid59def getHostGroup(ZBX_SERVER, ZBX_USER, ZBX_PASS, HOST):60 zapi = ZabbixAPI(server=ZBX_SERVER)61 zapi.login(ZBX_USER, ZBX_PASS)62 hostinfo = zapi.host.get({"filter":{"host":HOST},"output":"hostid", "selectGroups": "extend", "selectParentTemplates": ["templateid","name"]})[0]63 host_group_list = []64 for x in hostinfo["groups"]:65 host_group_list.append(x["name"])66 hostgroup = str(host_group_list)67 68 return hostgroup69def getTriggersDB(OTRS_DB_NAME, OTRS_DB_USER, OTRS_DB_PASS, OTRS_DB_SERVER):70 connection = mysql.connector.connect(host=OTRS_DB_SERVER,71 database=OTRS_DB_NAME,72 user=OTRS_DB_USER,73 password=OTRS_DB_PASS)74 query = "SELECT \75 TB2.value_text AS triggerid \76 FROM dynamic_field TB1 \77 INNER JOIN dynamic_field_value TB2 ON TB2.field_id = TB1.id \78 INNER JOIN ticket TB3 ON TB3.id = TB2.object_id \79 WHERE TB1.name = 'TriggerID' AND TB1.valid_id = 1 AND TB3.ticket_state_id = 12 AND TB2.value_text;"80 rows = []81 cursor = connection.cursor()82 cursor.execute(query)83 result = cursor.fetchall()84 for row in result:85 rows.append(row[0])86 return rows87def getTriggersAndTicketDB(OTRS_DB_NAME, OTRS_DB_USER, OTRS_DB_PASS, OTRS_DB_SERVER):88 connection = mysql.connector.connect(host=OTRS_DB_SERVER,89 database=OTRS_DB_NAME,90 user=OTRS_DB_USER,91 password=OTRS_DB_PASS)92 query = "SELECT \93 TB2.value_text AS triggerid, \94 TB3.id AS ticketid \95 FROM dynamic_field TB1 \96 INNER JOIN dynamic_field_value TB2 ON TB2.field_id = TB1.id \97 INNER JOIN ticket TB3 ON TB3.id = TB2.object_id \98 WHERE TB1.name = 'TriggerID' AND TB1.valid_id = 1 AND TB3.ticket_state_id = 12 AND TB2.value_text;"99 rows = []100 cursor = connection.cursor()101 cursor.execute(query)102 result = cursor.fetchall()103 for row in result:104 rows.append(row[0])105 return rows106def buildTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, HOST, TRIGGERID, SEVERITY, DESCRIPTION, QUEUE, STATE, PRIORITY, CUSTOMERUSER, SERVICE ,SLA, TYPE, HOSTGROUP):107 client = Client(OTRS_SERVER, OTRS_USER, OTRS_PASS)108 client.session_create()109 new_ticket = Ticket.create_basic(Title=""+ DESCRIPTION +"",110 Queue="" + QUEUE + "",111 State=u"" + STATE + "",112 Priority=u"" + PRIORITY + "",113 SLA="" + SLA + "",114 Type="" + TYPE + "",115 Service="" + SERVICE + "",116 CustomerUser="" + CUSTOMERUSER + "")117 first_article = Article({"Subject": "Server " + HOST + " Down" , "Body": "Host: " + HOST + " Trigger ID: " + TRIGGERID +" Severity: " + SEVERITY + " HostGroup: " + HOSTGROUP + " Descricao: " + DESCRIPTION })118 dynamic_field = DynamicField("TriggerID", value="" + TRIGGERID + "")119 client.ticket_create(new_ticket, first_article, None ,dynamic_fields=[dynamic_field])120 return "Ticket Build"121def getHostGroupTrigger(HOST, TRIGGERID, SEVERITY, HOSTGROUP, DESCRIPTION):122 list_groups = getHostGroup(ZBX_SERVER, ZBX_USER, ZBX_PASS, HOST)123 if 'ICS/CORREIO' in str(list_groups) or 'SS/MAIL' in str(list_groups) or 'SS/MAIL/SMTP' in str(list_groups):124 SERVICE = "Problema::2 - Sustentação::2.1- Itens organizacionais::2.1.2 - Caixas postais de correio eletrônico::2.1.2.1 - Investigar"125 buildTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, HOST, TRIGGERID, SEVERITY, DESCRIPTION, QUEUE, STATE, PRIORITY, CUSTOMERUSER, SERVICE, SLA, TYPE, HOSTGROUP)126 elif 'SS/AUTHENTICATION/AD' in str(list_groups):127 SERVICE = "Problema::2 - Sustentação::2.1- Itens organizacionais::2.1.3 - Domínios (LDAP)::2.1.3.1 - Investigar"128 buildTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, HOST, TRIGGERID, SEVERITY, DESCRIPTION, QUEUE, STATE, PRIORITY, CUSTOMERUSER, SERVICE, SLA, TYPE, HOSTGROUP)129 elif 'Virtualização' in str(list_groups) or 'ICS/SRV-FISICOS' in str(list_groups) or 'Virtualizacao::Vmware' in str(list_groups) or 'CLOUD/HYPERVISOR/VMWARE' in str(list_groups) or 'ICS/VIRTUALIZACAO' in str(list_groups) or 'CLOUD/BAREMETAL' in str(list_groups):130 SERVICE = "Problema::2 - Sustentação::2.2 - Itens de Hardware::2.2.1 - Servidores físicos::2.2.1.1 - Investigar"131 buildTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, HOST, TRIGGERID, SEVERITY, DESCRIPTION, QUEUE, STATE, PRIORITY, CUSTOMERUSER, SERVICE, SLA, TYPE, HOSTGROUP)132 elif 'NETWORKS/ROUTER' in str(list_groups) or 'NETWORKS/ROUTER/EXTERNAL' in str(list_groups) or 'ICS/ROTEADORES' in str(list_groups):133 SERVICE = "Problema::2 - Sustentação::2.2 - Itens de Hardware::2.2.10 - Roteadores::2.2.10.1 - Investigar"134 buildTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, HOST, TRIGGERID, SEVERITY, DESCRIPTION, QUEUE, STATE, PRIORITY, CUSTOMERUSER, SERVICE, SLA, TYPE, HOSTGROUP)135 elif 'ICS/LINKS-INTERNET' in str(list_groups) or 'URL/SSL' in str(list_groups) or 'URL/INTERNAL' in str(list_groups) or 'URL/EXTERNAL' in str(list_groups):136 SERVICE = "Problema::2 - Sustentação::2.2 - Itens de Hardware::2.2.11 - Links com a Internet::2.2.11.1 - Investigar"137 buildTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, HOST, TRIGGERID, SEVERITY, DESCRIPTION, QUEUE, STATE, PRIORITY, CUSTOMERUSER, SERVICE, SLA, TYPE, HOSTGROUP)138 elif 'SECURITY' in str(list_groups) or 'ICS/FIREWALL' in str(list_groups):139 SERVICE = "Problema::2 - Sustentação::2.2 - Itens de Hardware::2.2.13 - Sistemas ou Hardwares de Segurança da Informação::2.2.13.1 - Investigar"140 buildTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, HOST, TRIGGERID, SEVERITY, DESCRIPTION, QUEUE, STATE, PRIORITY, CUSTOMERUSER, SERVICE, SLA, TYPE, HOSTGROUP)141 elif 'ICS/ATIVOS-WIFI' in str(list_groups) or 'NETWORKS/ACCESS-POINT' in str(list_groups):142 SERVICE = "Problema::2 - Sustentação::2.2 - Itens de Hardware::2.2.14 - Ativos ou Passivos de rede e WiFi::2.2.14.1 - Investigar"143 buildTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, HOST, TRIGGERID, SEVERITY, DESCRIPTION, QUEUE, STATE, PRIORITY, CUSTOMERUSER, SERVICE, SLA, TYPE, HOSTGROUP)144 elif 'Voip' in str(list_groups):145 SERVICE = "Problema::2 - Sustentação::2.2 - Itens de Hardware::2.2.15 - Telefonia IP (apenas hardware)::2.2.15.1 - Investigar"146 buildTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, HOST, TRIGGERID, SEVERITY, DESCRIPTION, QUEUE, STATE, PRIORITY, CUSTOMERUSER, SERVICE, SLA, TYPE, HOSTGROUP)147 elif 'ICS/SRV-VIRTUAIS' in str(list_groups) or 'CLOUD/VMS' in str(list_groups) or 'Maquinas Virtuais Srv085' in str(list_groups) or 'SO/LINUX' in str(list_groups) or 'SO/WINDOWS' in str(list_groups) or 'Windows servers' in str(list_groups):148 SERVICE = "Problema::2 - Sustentação::2.2 - Itens de Hardware::2.2.2 - Servidores virtuais::2.2.2.1 - Investigar"149 buildTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, HOST, TRIGGERID, SEVERITY, DESCRIPTION, QUEUE, STATE, PRIORITY, CUSTOMERUSER, SERVICE, SLA, TYPE, HOSTGROUP)150 elif 'Storage' in str(list_groups) or 'ICS/STORAGES' in str(list_groups) or 'CLOUD/STORAGE' in str(list_groups):151 SERVICE = "Problema::2 - Sustentação::2.2 - Itens de Hardware::2.2.6 - Storages corporativos::2.2.6.1 - Investigar"152 buildTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, HOST, TRIGGERID, SEVERITY, DESCRIPTION, QUEUE, STATE, PRIORITY, CUSTOMERUSER, SERVICE, SLA, TYPE, HOSTGROUP)153 elif 'CLOUD/BACKUP' in str(list_groups):154 SERVICE = "Problema::2 - Sustentação::2.2 - Itens de Hardware::2.2.7 - Sistema de backup::2.2.7.1 - Investigar"155 buildTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, HOST, TRIGGERID, SEVERITY, DESCRIPTION, QUEUE, STATE, PRIORITY, CUSTOMERUSER, SERVICE, SLA, TYPE, HOSTGROUP)156 elif 'Switches' in str(list_groups) or 'NETWORKS/SWITCH/ACCESS' in str(list_groups) or 'NETWORKS/SWITCH/CORE' in str(list_groups) or 'NETWORKS/SWITCH/DISTRIBUTION' in str(list_groups) or 'ICS/GERENCIA-INFRA' in str(list_groups) or 'ICS/GERENCIA-SERVICOS' in str(list_groups) or 'ICS/SWITCHES' in str(list_groups) or 'Newave' in str(list_groups):157 SERVICE = "Problema::2 - Sustentação::2.2 - Itens de Hardware::2.2.8 - Switches::2.2.8.1 - Investigar"158 buildTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, HOST, TRIGGERID, SEVERITY, DESCRIPTION, QUEUE, STATE, PRIORITY, CUSTOMERUSER, SERVICE, SLA, TYPE, HOSTGROUP)159 elif 'DATABASES/MSSQL' in str(list_groups) or 'DATABASES/MYSQL' in str(list_groups) or 'ICS/SGBD' in str(list_groups) or 'DATABASES' in str(list_groups) or 'DATABASES/SQLSERVER' in str(list_groups):160 SERVICE = "Problema::2 - Sustentação::2.3 - Itens de Software::2.3.1 - Gerenciador de banco de dados::2.3.1.1 - Investigar"161 buildTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, HOST, TRIGGERID, SEVERITY, DESCRIPTION, QUEUE, STATE, PRIORITY, CUSTOMERUSER, SERVICE, SLA, TYPE, HOSTGROUP)162 elif 'ICS/CONTROLE-COD-MALICIOSOS' in str(list_groups):163 SERVICE = "Problema::2 - Sustentação::2.3 - Itens de Software::2.3.13 - Sistema centralizado de controle de código malicioso::2.3.13.1 - Investigar"164 buildTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, HOST, TRIGGERID, SEVERITY, DESCRIPTION, QUEUE, STATE, PRIORITY, CUSTOMERUSER, SERVICE, SLA, TYPE, HOSTGROUP)165 elif 'SS/APPLICATION/APACHE' in str(list_groups) or 'SS/APPLICATION/HTTP' in str(list_groups) or 'SS/FTP' in str(list_groups) or 'Servidor de Arquivos' in str(list_groups) or 'ICS/SRV-SITES' in str(list_groups):166 SERVICE = "Problema::2 - Sustentação::2.3 - Itens de Software::2.3.2 - Servidor WEB (IIS, Apache, outros)::2.3.2.1 - Investigar"167 buildTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, HOST, TRIGGERID, SEVERITY, DESCRIPTION, QUEUE, STATE, PRIORITY, CUSTOMERUSER, SERVICE, SLA, TYPE, HOSTGROUP)168 elif 'ZBX/MONITOR' in str(list_groups) or 'ZBX/SERVER' in str(list_groups) or 'ICS/MONITORAMENTO' in str(list_groups):169 SERVICE = "Problema::2 - Sustentação::2.3 - Itens de Software::2.3.6 - Sistema centralizado de monitoramento de infraestrutura de TI::2.3.6.1 - Investigar"170 buildTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, HOST, TRIGGERID, SEVERITY, DESCRIPTION, QUEUE, STATE, PRIORITY, CUSTOMERUSER, SERVICE, SLA, TYPE, HOSTGROUP)171 elif 'ICS/SRV-SISTEMAS' in str(list_groups) or 'ICS/SISTEMAS' in str(list_groups) or 'Sistema' in str(list_groups) or 'SRV204::Websites' in str(list_groups) or 'SRV209::Websites' in str(list_groups) or 'SEI - PROD' in str(list_groups) or 'QlikView' in str(list_groups):172 SERVICE = "Problema::2 - Sustentação::2.4 - Serviços de TI Disponibilizados::2.4.1 - Sistemas aplicativos::2.4.1.1 - Investigar"173 buildTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, HOST, TRIGGERID, SEVERITY, DESCRIPTION, QUEUE, STATE, PRIORITY, CUSTOMERUSER, SERVICE, SLA, TYPE, HOSTGROUP)174 elif 'ICS/SITES' in str(list_groups) or 'Websites' in str(list_groups) or 'Portal Homologacao' in str(list_groups) or 'Websites::Produção' in str(list_groups) or 'Portal Producao' in str(list_groups):175 SERVICE = "Problema::2 - Sustentação::2.4 - Serviços de TI Disponibilizados::2.4.2 - Sites WEB (Internet, intranet e extranets)::2.4.2.1 - Investigar"176 buildTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, HOST, TRIGGERID, SEVERITY, DESCRIPTION, QUEUE, STATE, PRIORITY, CUSTOMERUSER, SERVICE, SLA, TYPE, HOSTGROUP)177 else:178 print "NENHUM GRUPO ENCONTRADO"179def closeTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, ZBX_SERVER, ZBX_USER, ZBX_PASS):180 181 zapi = ZabbixAPI(server=ZBX_SERVER)182 zapi.login(ZBX_USER, ZBX_PASS)183 client = Client(OTRS_SERVER, OTRS_USER, OTRS_PASS)184 client.session_create()185 connection = mysql.connector.connect(host=OTRS_DB_SERVER,186 database=OTRS_DB_NAME,187 user=OTRS_DB_USER,188 password=OTRS_DB_PASS)189 query = "SELECT \190 TB2.value_text AS triggerid, \191 TB3.id AS ticketid \192 FROM dynamic_field TB1 \193 INNER JOIN dynamic_field_value TB2 ON TB2.field_id = TB1.id \194 INNER JOIN ticket TB3 ON TB3.id = TB2.object_id \195 WHERE TB1.name = 'TriggerID' AND TB1.valid_id = 1 AND TB3.ticket_state_id = 12 AND TB2.value_text;"196 cursor = connection.cursor()197 cursor.execute(query)198 result = cursor.fetchall()199 for row in result:200 TRIGGERID = row[0]201 TICKETID = str(row[1])202 TICKETINT = int(row[1])203 triggers = zapi.trigger.get ({204 "output": ["description", "lastchange", "triggerid", "priority", "status", "state"],205 "selectHosts": ["hostid", "host"],206 "selectLastEvent": ["eventid", "acknowledged", "objectid", "clock", "ns", "state"],207 "sortfield" : "priority",208 "sortorder": "DESC",209 "selectDependencies": True,210 "monitored": "true",211 "only_true": "true",212 "maintenance": "false",213 "expandDescription": True,214 "skipDependent": "1",215 "filter":{216 "value":0,217 "triggerid": ""+ TRIGGERID +""}218 })219 for x in triggers:220 HOST = x["hosts"][0]["host"]221 severity = int(x["priority"])222 status = x["status"]223 triggerid = x["triggerid"]224 state = x["state"]225 if TRIGGERID == 0 and TICKETINT == 0:226 print "Nenhum chamado para ser fechado"227 else:228 # print "Chamado com Trigger ID: " + TRIGGERID + " Ticket ID: " + TICKETID + " Sera Fechado"229 client = Client(OTRS_SERVER, OTRS_USER, OTRS_PASS)230 client.session_create()231 article = Article({"Subject": "Chamado Fechado" , "Body": "Problema com Servidor "+ HOST +" e Trigger ID: "+ TRIGGERID +" resolvido!"})232 client.ticket_update(TICKETINT, article, attachments=None, dynamic_fields=None, State=u"Concluido")233def createTicket(ZBX_SERVER, ZBX_USER, ZBX_PASS):234 triggerszabbix = getTriggers(ZBX_SERVER, ZBX_USER, ZBX_PASS)235 triggersotrs = getTriggersDB(OTRS_DB_NAME, OTRS_DB_USER, OTRS_DB_PASS, OTRS_DB_SERVER)236 difftriggers = list(set(triggerszabbix) - set(triggersotrs))237 for TRIGGERID in difftriggers:238 if not TRIGGERID:239 print "Lista vazia"240 else:241 zapi = ZabbixAPI(server=ZBX_SERVER)242 zapi.login(ZBX_USER, ZBX_PASS)243 triggers = zapi.trigger.get ({244 "output": ["description", "lastchange", "triggerid", "priority"],245 "selectHosts": ["hostid", "host"],246 "selectLastEvent": ["eventid", "acknowledged", "objectid", "clock", "ns"],247 "sortfield" : "lastchange",248 "selectGroups": ["groupid", "name"],249 "monitored": "true",250 "only_true": "true",251 "maintenance": "false",252 "expandDescription": True,253 "filter":{254 "value":1,255 "triggerid": ""+ TRIGGERID +""}256 })257 for y in triggers:258 HOST = y["hosts"][0]["host"]259 severity = int(y["priority"])260 TRIGGERID = y["triggerid"]261 DESCRIPTION = y["description"]262 HOSTGROUP = y["groups"][0]["name"]263 if severity == 0:264 SEVERITY = "Not Classified"265 elif severity == 1:266 SEVERITY = "Information"267 elif severity == 2:268 SEVERITY = "Warning"269 getHostGroupTrigger(HOST, TRIGGERID, SEVERITY, HOSTGROUP, DESCRIPTION)270 elif severity == 3:271 SEVERITY = "Average"272 getHostGroupTrigger(HOST, TRIGGERID, SEVERITY, HOSTGROUP, DESCRIPTION)273 elif severity == 4:274 SEVERITY = "High"275 getHostGroupTrigger(HOST, TRIGGERID, SEVERITY, HOSTGROUP, DESCRIPTION)276 elif severity == 5: 277 SEVERITY = "Disaster"278 getHostGroupTrigger(HOST, TRIGGERID, SEVERITY, HOSTGROUP, DESCRIPTION)279 else:280 print "Unknown Severity"281 return "Chamado criado com sucesso!"282closeTicket(OTRS_SERVER, OTRS_USER, OTRS_PASS, ZBX_SERVER, ZBX_USER, ZBX_PASS)...

Full Screen

Full Screen

advance_d_PostHocTest.py

Source:advance_d_PostHocTest.py Github

copy

Full Screen

1#!/usr/bin/env python32# -*- coding: utf-8 -*-3"""4Created on Tue Oct 23 20:21:58 20185@author: shenwanxiang6"""7import numpy as np8import itertools9import math10from scipy.stats import ttest_ind11import pandas as pd12from utils.pandastool import ParseDFtypes, isCategory, isSeries13from utils.modelbase import ModelBase14import coloredlogs,logging15coloredlogs.install()16# 误差均方17def MSE(list_groups,list_total):18 sse=SSE(list_groups)19 mse=sse/(len(list_total)-1*len(list_groups))*1.0 20 return mse 21#sum of squares within group error,also know as random error 22def SSE(list_groups): 23 sse=024 for group in list_groups:25 se=SE(group)26 sse+=se27 return sse28#one within group error,also know as random error29def SE(group):30 se=031 mean1=np.mean(group)32 for i in group:33 error=i-mean134 se+=error**235 return se36 37def Combination(list_groups):38 combination= []39 for i in range(1,len(list_groups)+1):40 iter = itertools.combinations(list_groups,i)41 combination.append(list(iter))42 #需要排除第一个和最后一个43 return combination[1:-1][0]44#LSD(least significant difference)最小显著差异45def LSD(list_groups,list_total,sample1,sample2,a=0.05):46 mean1=np.mean(sample1)47 mean2=np.mean(sample2)48 distance=mean1-mean249 #print("distance:",distance)50 #t检验的自由度51 #df=len(list_total)-1*len(list_groups)52 mse=MSE(list_groups,list_total)53 #print("MSE:",mse)54 55 56 #t_value=stats.t(df).isf(a/2)57 #p = 1 - stats.t.cdf(t_value, df=df)58 59 t_value,p = ttest_ind(sample1,sample2)60 61 lsd=t_value*math.sqrt(mse*(1.0/len(sample1)+1.0/len(sample2)))62 63 res = [mean1,mean2, distance, lsd, p]64 return pd.DataFrame(res, index=['(I)平均值', '(J)平均值','差值(I-J)','LSD统计量','P值'])65def Multiple_test(list_groups,list_total):66 #print("multiple test----------------------------------------------")67 combination=Combination(list_groups)68 69 a = []70 for pair in combination:71 a.append(LSD(list_groups,list_total,pair[0],pair[1]))72 73 return a74 75class PostHocTest(ModelBase):76 """77 事后检验:用于分析定类数据与定量数据之间的关系情况78 例如研究人员想知道三组学生(本科以下,本科,本科以上,tsy)的智商平均值是否有显著差异.79 比如分析显示三组学生智商(tsx)有着明显的差异,那具体是本科以下与本科这两组之间,80 还是本科以下与本科以上两组之间的差异;即具体两两组别之间的差异对比,则称为事后检验; 81 返回一个dataframe,在‘result‘关键字中82 83 84 方法85 -------86 get_info : 87 获取该模型的信息, 返回一个字典,包含‘id’和‘name’, 'description','limited'关键字88 含义分别为:模型的id, 模型的名称,模型的描述,模型的适用范围89 run: 90 参数91 ----------92 dfx: pandas DataFrame93 需要每列的数据都是数字型数据,不能是字符串或者object94 95 tsy: pandas Series96 需要一列的数据都是分类型数据97 98 99 返回结果100 ---------- 101 返回一个字典,带有‘result’关键字,其值为按照tsy分组的均值、p-值等等系数组成的dataframe102 """103 104 105 106 def __init__(self, 107 model_id = None, 108 model_limiation = None,109 ):110 111 self._id_ = model_id112 self._limitation_ = model_limiation113 114 115 def get_info(self):116 117 return {'id': self._id, 118 'name': self._name, 119 'description': self._description,120 'limited':self._limitation121 }122 123 124 def run(self, 125 dfx, 126 tsy): 127 128 129 msg = {}130 131 tsy = tsy.reset_index(drop=True)132 dfx = dfx.reset_index(drop=True) 133 134 xl = len(dfx)135 yl = len(tsy)136 if xl != yl:137 logging.error('the length of input X:%s is not equal the length of Y: %s ! ' % (xl,yl))138 msg['error'] = 'the length of input X:%s is not equal the length of Y: %s ! ' % (xl,yl)139 return {'result':pd.DataFrame(), 'msg':msg}140 141 142 if not isSeries(tsy) or not isCategory(tsy):143 logging.error('input tsy is not a pandas Series or not a category data!')144 msg['error'] = 'input tsy is not a pandas Series or not a category data!'145 146 return {'result':pd.DataFrame(), 'msg':msg}147 148 149 150 else:151 152 x_numer_cols, x_cate_cols = ParseDFtypes(dfx)153 if x_numer_cols ==[]:154 logging.error('All input dfx are no numeric columns, Please check your input dfx data!')155 msg['error'] = 'All input dfx are no numeric columns, Please check your input dfx data!'156 return {'result':pd.DataFrame(), 'msg':msg}157 158 159 else:160 161 if x_cate_cols != []:162 logging.warning('input dfx has non-numeric columns: %s, will ignore these columns!' % x_cate_cols)163 164 msg['warning'] = 'input dfx has non-numeric columns: %s, will ignore these columns!' % x_cate_cols165 166 167 168 169 res = []170 for i in x_numer_cols:171 tsx = dfx[i]172 lg = list(tsy.unique())173 list_groups = [tsx[tsy==i].tolist() for i in lg]174 list_total = tsx.tolist()175 a = Multiple_test(list_groups,list_total) 176 dfr = pd.concat(a,axis=1).T177 dfr.index = [(i[0]+'(I)', i[1] + '(J)') for i in Combination(lg)]178 179 dfr['分析项'] = i180 181 dfrr = dfr.reset_index().set_index(['分析项','index'])182 183 res.append(dfrr)184 185 dfres = pd.concat(res)186 187 return {'result':dfres, 'msg':msg}188 189 190if __name__ == '__main__':191 192 #读取数据193 from dataset import load_MedExp194 195 196 df =load_MedExp()197 testdata = load_MedExp()198 dfx = df[['med','age','idp']]199 200 tsy = df.health201 #tsy = tsy[tsy.isin(['good', 'excellent'])]202 203 204 #类的初始化205 O = PostHocTest()206 #打印该类描述的信息207 print(O.get_info().get('description'))208 209 #执行运算,传入tsx、tsy参数210 dict_res = O.run(dfx,tsy)211 212 #获取返回的字典...

Full Screen

Full Screen

advance_d_PostHocTest.md

Source:advance_d_PostHocTest.md Github

copy

Full Screen

1#!/usr/bin/env python32# -*- coding: utf-8 -*-3"""4Created on Tue Oct 23 20:21:58 20185@author: shenwanxiang6"""7import numpy as np8import itertools9import math10from scipy.stats import ttest_ind11import pandas as pd12from utils.pandastool import ParseDFtypes, isCategory, isSeries13from utils.modelbase import ModelBase14import coloredlogs,logging15coloredlogs.install()16# 误差均方17def MSE(list_groups,list_total):18 sse=SSE(list_groups)19 mse=sse/(len(list_total)-1*len(list_groups))*1.0 20 return mse 21#sum of squares within group error,also know as random error 22def SSE(list_groups): 23 sse=024 for group in list_groups:25 se=SE(group)26 sse+=se27 return sse28#one within group error,also know as random error29def SE(group):30 se=031 mean1=np.mean(group)32 for i in group:33 error=i-mean134 se+=error**235 return se36 37def Combination(list_groups):38 combination= []39 for i in range(1,len(list_groups)+1):40 iter = itertools.combinations(list_groups,i)41 combination.append(list(iter))42 #需要排除第一个和最后一个43 return combination[1:-1][0]44#LSD(least significant difference)最小显著差异45def LSD(list_groups,list_total,sample1,sample2,a=0.05):46 mean1=np.mean(sample1)47 mean2=np.mean(sample2)48 distance=mean1-mean249 #print("distance:",distance)50 #t检验的自由度51 #df=len(list_total)-1*len(list_groups)52 mse=MSE(list_groups,list_total)53 #print("MSE:",mse)54 55 56 #t_value=stats.t(df).isf(a/2)57 #p = 1 - stats.t.cdf(t_value, df=df)58 59 t_value,p = ttest_ind(sample1,sample2)60 61 lsd=t_value*math.sqrt(mse*(1.0/len(sample1)+1.0/len(sample2)))62 63 res = [mean1,mean2, distance, lsd, p]64 return pd.DataFrame(res, index=['(I)平均值', '(J)平均值','差值(I-J)','LSD统计量','P值'])65def Multiple_test(list_groups,list_total):66 #print("multiple test----------------------------------------------")67 combination=Combination(list_groups)68 69 a = []70 for pair in combination:71 a.append(LSD(list_groups,list_total,pair[0],pair[1]))72 73 return a74 75class PostHocTest(ModelBase):76 """77 事后检验:用于分析定类数据与定量数据之间的关系情况78 例如研究人员想知道三组学生(本科以下,本科,本科以上,tsy)的智商平均值是否有显著差异.79 比如分析显示三组学生智商(tsx)有着明显的差异,那具体是本科以下与本科这两组之间,80 还是本科以下与本科以上两组之间的差异;即具体两两组别之间的差异对比,则称为事后检验; 81 返回一个dataframe,在‘result‘关键字中82 83 84 方法85 -------86 get_info : 87 获取该模型的信息, 返回一个字典,包含‘id’和‘name’, 'description','limited'关键字88 含义分别为:模型的id, 模型的名称,模型的描述,模型的适用范围89 run: 90 参数91 ----------92 dfx: pandas DataFrame93 需要每列的数据都是数字型数据,不能是字符串或者object94 95 tsy: pandas Series96 需要一列的数据都是分类型数据97 98 99 返回结果100 ---------- 101 返回一个字典,带有‘result’关键字,其值为按照tsy分组的均值、p-值等等系数组成的dataframe102 """103 104 105 106 def __init__(self, 107 model_id = None, 108 model_limiation = None,109 ):110 111 self._id_ = model_id112 self._limitation_ = model_limiation113 114 115 def get_info(self):116 117 return {'id': self._id, 118 'name': self._name, 119 'description': self._description,120 'limited':self._limitation121 }122 123 124 def run(self, 125 dfx, 126 tsy): 127 128 129 msg = {}130 131 tsy = tsy.reset_index(drop=True)132 dfx = dfx.reset_index(drop=True) 133 134 xl = len(dfx)135 yl = len(tsy)136 if xl != yl:137 logging.error('the length of input X:%s is not equal the length of Y: %s ! ' % (xl,yl))138 msg['error'] = 'the length of input X:%s is not equal the length of Y: %s ! ' % (xl,yl)139 return {'result':pd.DataFrame(), 'msg':msg}140 141 142 if not isSeries(tsy) or not isCategory(tsy):143 logging.error('input tsy is not a pandas Series or not a category data!')144 msg['error'] = 'input tsy is not a pandas Series or not a category data!'145 146 return {'result':pd.DataFrame(), 'msg':msg}147 148 149 150 else:151 152 x_numer_cols, x_cate_cols = ParseDFtypes(dfx)153 if x_numer_cols ==[]:154 logging.error('All input dfx are no numeric columns, Please check your input dfx data!')155 msg['error'] = 'All input dfx are no numeric columns, Please check your input dfx data!'156 return {'result':pd.DataFrame(), 'msg':msg}157 158 159 else:160 161 if x_cate_cols != []:162 logging.warning('input dfx has non-numeric columns: %s, will ignore these columns!' % x_cate_cols)163 164 msg['warning'] = 'input dfx has non-numeric columns: %s, will ignore these columns!' % x_cate_cols165 166 167 168 169 res = []170 for i in x_numer_cols:171 tsx = dfx[i]172 lg = list(tsy.unique())173 list_groups = [tsx[tsy==i].tolist() for i in lg]174 list_total = tsx.tolist()175 a = Multiple_test(list_groups,list_total) 176 dfr = pd.concat(a,axis=1).T177 dfr.index = [(i[0]+'(I)', i[1] + '(J)') for i in Combination(lg)]178 179 dfr['分析项'] = i180 181 dfrr = dfr.reset_index().set_index(['分析项','index'])182 183 res.append(dfrr)184 185 dfres = pd.concat(res)186 187 return {'result':dfres, 'msg':msg}188 189 190if __name__ == '__main__':191 192 #读取数据193 from dataset import load_MedExp194 195 196 df =load_MedExp()197 testdata = load_MedExp()198 dfx = df[['med','age','idp']]199 200 tsy = df.health201 #tsy = tsy[tsy.isin(['good', 'excellent'])]202 203 204 #类的初始化205 O = PostHocTest()206 #打印该类描述的信息207 print(O.get_info().get('description'))208 209 #执行运算,传入tsx、tsy参数210 dict_res = O.run(dfx,tsy)211 212 #获取返回的字典...

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 tempest 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