'''
this is a visualization class
'''
import statsmodels.tsa.api as tsa
import pandas as pd
import zerorpc
class App():
'''Access to the services provided by the app
Args:
appParams (dict) : parameters needed to connect to app
'''
def __init__(self,port=None,modelHub=None):
self.port = port
self.portStub = 'tcp://127.0.0.1:'
self.portGuess = 4242 #check index.js in app for this number
self.portSearchIncrement = 123 #check index.js in app for this number
self.client = zerorpc.Client()
self.modelHub = modelHub
self.modelResult = {} #result of a fitted model
#connect:
self.connect()
def connect(self):
'''
connects to the pypungi
'''
if self.port is not None:
self.client.connect(self.port)
else:
keepTrying = 1
while keepTrying == 1:
try:
self.client.connect(self.portStub + str(self.portGuess))
self.ping()
self.port = self.portGuess
keepTrying = 0
except:
self.portGuess = self.portGuess + self.portSearchIncrement
def ping(self,text='ping'):
return(self.client.ping(text))
def plot(self,data,select=None):
'''
given data and plottype process and send it to app
'''
if select is None:
#if isinstance(data,pd.DataFrame) or isinstance(data,pd.Series):
# select = 'tseries'
#else:
select = 'line'
rawdata = self._transform(data,select)
#self.client.plot({'data':transmitData, 'plottype':plottype})
self.client.plot({'rawdata':rawdata,'select':select})
def model(self,modelName,*args,**kwargs):
'''send model id (eg ols) and the 'app' part of the fitted model'''
#self.model = statsModel(modelName,*args,**kwargs)
#self.client.model( {'id':modelName,'table':fitmodel.model['app'] } )
self.modelResult = self.modelHub(modelName,*args,**kwargs)
self.client.model(self.modelResult['app'])
def _transform(self,data,select):
if select in ['line','scatter']:
return(self._transformDataTypeToDictionary(data)) #if series, transform to dataframe
elif select in ['tseries']:
sendData = {}
sendData['rawdata'] = self._transformDataTypeToDictionary(data)
sendData['tables'] = self._tsaStats(sendData['rawdata'])
return(sendData)
elif select in ['table']:
return(self._transformDataTypeToDictionary(data,format='split'))
else:
return(self._transformDataTypeToDictionary(data))
def _transformDataTypeToDictionary(self,data,format='list'):
'''
given data of certain type, transform it to the most common used dictionary.
if a type of plot, as indicated by select, needs a specific data transformation, do it there.
Else, introduce a new input var (select) here and use it.
for data:
format should be list: eg {'a':[1,2,3,4], 'b':[1,2,3,4]}
for tables:
default format should be split: eg {'index': [0, 1], 'columns': [0, 1, 2], 'data': [[1, 2, 3], [1, 2, 3]]}
'''
if isinstance(data,pd.DataFrame) or isinstance(data,pd.Series):
if 'date' in data.columns or 'index' in data.columns:
return(pd.DataFrame(data).rename({'data':'index'},axis='columns').astype({'index':str}).to_dict(format))
else:
return(pd.DataFrame(data).assign(index = data.index).astype({'index':str}).to_dict(format))
elif isinstance(data,list):
return({'index':list(range(len(data))), '':data })
else:
return(data)
def _tsaStats(self,rawdata):
'''for data in 'records' format: {'a':[1,2,3]}, calculate time series stats'''
#TODO: it would be better to round on the app side and give user the option to select precission
tableStats = {}
#STATIONARITY TESTS
kpssTable = {'columns': ['variable','stat','p-value','lags','10%','5%', '2.5%', '1%'] }
data = []
for var in rawdata.keys():
if var != 'index':
kpssTest = list(tsa.kpss(rawdata[var],nlags='auto'))
kpssTest = [var] + [float(round(x,4)) for x in kpssTest[0:3] + list(kpssTest[3].values())] #float bc can't serialize numpy.int64
data += [kpssTest]
kpssTable['data'] = data
tableStats['kpss'] = kpssTable
adfTable = {'columns': ['variable','stat','p-value','lags','N obs', '1%','5%', '10%', 'IC'] }
data = []
for var in rawdata.keys():
if var != 'index':
adfTest = list(tsa.adfuller(rawdata[var]))
adfTest = [var] + [float(round(x,2)) for x in adfTest[0:4] + list(adfTest[4].values())] + [round(adfTest[5],1)] #float bc can't serialize numpy.int64
data += [adfTest]
adfTable['data'] = data
tableStats['adf'] = adfTable
tableStats['describe'] = pd.DataFrame(rawdata).describe().T.reset_index().rename({'index':'variable'}, axis=1).to_dict('split')
return(tableStats)
def stash(self,data):
if isinstance(data,pd.DataFrame):
rawdata = data.to_dict('list')
elif isinstance(data,pd.Series):
rawdata = data.to_dict()
else:
rawdata = data
content = {'data':rawdata, 'type': str(type(data))}
self.client.stash(content)
def getStash(self):
stash = self.client.getStash('')
if stash['type'] == "<class 'pandas.core.frame.DataFrame'>":
return(pd.DataFrame(stash['data']))
elif stash['type'] == "<class 'pandas.core.series.Series'>":
return(pd.Series(stash['data']))
else:
return(stash)
def download(self):
download = self.client.load('')
return(download)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共9个文件
py:6个
toml:1个
gitignore:1个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
资源分类:Python库 所属语言:Python 资源全名:pypungi-0.10.12.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-dosexec](https://img-home.csdnimg.cn/images/20210720083343.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 9 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/277f6345dca0446498fbbc03843436aa_qq_38161040.jpg!1)
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
- sping-cloud-advance
- 基于python的求职招聘网站 python+django+vue搭建的求职招聘管理系统-毕业设计 (源码+数据库+演示地址)
- qwfRDwarWwrerA
- 跑带调速(实验室用),可调0m/s-10m/s
- 毕业设计springboot基于vue.js的客户关系管理系统(crm)的设计与实现源码含文档含教程
- 使用OpenCV-Python批量为照片应用写生素描滤镜(高分课后作业)
- LCD1602显示屏使用.pdf
- 毕业设计springboot基于JAVA语言的在线考试与学习交流网页平台源码含文档含教程
- STM32F系列通过hx711芯片实现压力传感器的测量(CUBEMAX)
- 火狐浏览器,支持win7的版本,106
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)