Guest User

Untitled

a guest
Sep 22nd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. # ---
  2. # jupyter:
  3. # jupytext_format_version: '1.2'
  4. # jupytext_formats: ipynb,py
  5. # kernelspec:
  6. # display_name: Python 3
  7. # language: python
  8. # name: python3
  9. # language_info:
  10. # codemirror_mode:
  11. # name: ipython
  12. # version: 3
  13. # file_extension: .py
  14. # mimetype: text/x-python
  15. # name: python
  16. # nbconvert_exporter: python
  17. # pygments_lexer: ipython3
  18. # version: 3.6.5
  19. # toc:
  20. # base_numbering: 1
  21. # nav_menu: {}
  22. # number_sections: true
  23. # sideBar: true
  24. # skip_h1_title: false
  25. # title_cell: Table of Contents
  26. # title_sidebar: Contents
  27. # toc_cell: false
  28. # toc_position: {}
  29. # toc_section_display: true
  30. # toc_window_display: false
  31. # ---
  32.  
  33. # %autosave 0
  34.  
  35. df['doy']
  36.  
  37. # +
  38. import time
  39. from datetime import datetime
  40. import pprint
  41.  
  42. import pandas as pd
  43. import ee; ee.Initialize()
  44.  
  45. # connection
  46. import pymongo
  47. from pymongo import MongoClient
  48. mongourl = 'mongodb://admin:snap1234@ds251362.mlab.com:51362/snapdb-test'
  49. client = MongoClient(mongourl)
  50. db = client.get_database()
  51.  
  52. cur = db.variables.find({'District':'Krishna'})
  53. df = pd.DataFrame(list(cur))
  54. # -
  55.  
  56. df.loc[(df['doy'] >= 152) & (df['doy'] <= 320)]
  57.  
  58. dir('334')
  59.  
  60. # +
  61. df['year'] = df['idate'].apply(lambda x: datetime.fromtimestamp(x).strftime('%Y'))
  62. df['doy'] = df['idate'].apply(lambda x: int(datetime.fromtimestamp(x).strftime('%j')))
  63. df = df.loc[(df['doy'] > 152) & (df['doy'] < 320)]
  64. df = df.sort_values(by=['doy'])
  65. df['mmdd'] = df['idate'].apply(lambda x: datetime.fromtimestamp(x).strftime('%b-%d'))
  66.  
  67. lastyear = df.loc[df['year'] == '2017'].groupby(['doy','mmdd','District'])['value'].mean().reset_index()
  68. thisyear = df.loc[df['year'] == '2018'].groupby(['doy','mmdd','District'])['value'].mean().reset_index()
  69.  
  70. vmin = df.groupby(['doy','mmdd','District'])['value'].quantile(0.30).reset_index()
  71. vmax = df.groupby(['doy','mmdd','District'])['value'].quantile(0.70).reset_index();
  72.  
  73. import plotly
  74. plotly.offline.init_notebook_mode() # run at the start of every notebook
  75. import plotly.graph_objs as go
  76. trace0 = go.Scatter(
  77. x=vmin['mmdd'],
  78. y=vmin['value'] / 10,
  79. fill= None,
  80. # mode='lines',
  81. name = 'quantile>30%',
  82. line=dict(
  83. width = 0.1,
  84. color='rgb(143, 19, 131)',
  85. dash = 'dash'
  86. )
  87. )
  88. trace1 = go.Scatter(
  89. x=vmax['mmdd'],
  90. y=vmax['value'] / 10,
  91. fill='tonexty',
  92. # mode='lines',
  93. name = 'quantile<70%',
  94. line=dict(
  95. width = 0,
  96. color='rgb(143, 19, 131)',
  97. dash = 'dash'
  98. )
  99. )
  100.  
  101. trace2 = go.Bar(
  102. x = lastyear['mmdd'],
  103. y = lastyear['value'] / 10,
  104. # mode = 'markers',
  105. name = 'year 2017',
  106. # line=dict(
  107. # color='rgb(0, 255, 0)',
  108. # )
  109. )
  110.  
  111. trace3 = go.Bar(
  112. x = thisyear['mmdd'],
  113. y = thisyear['value'] / 10,
  114. # mode = 'markers',
  115. name = 'year 2018',
  116. # line=dict(
  117. # color='rgb(255, 0, 0)',
  118. # )
  119. )
  120.  
  121.  
  122. data = [trace0, trace1, trace2, trace3]
  123. legend=dict(
  124. x=0,
  125. y=1,
  126. traceorder='normal',
  127. font=dict(
  128. family='sans-serif',
  129. size=12,
  130. color='#000'
  131. ),
  132. bgcolor='#E2E2E2',
  133. bordercolor='#FFFFFF',
  134. borderwidth=2
  135. )
  136. layout = go.Layout(
  137. title='Krishna District: LAI',
  138. legend = legend,
  139. xaxis=dict(
  140. title='Date',
  141. hoverformat='.3f',
  142. titlefont=dict(
  143. family='Courier New, monospace',
  144. size=18,
  145. color='#7f7f7f'
  146. )
  147. ),
  148. yaxis=dict(
  149. title='Value',
  150. hoverformat='.3f',
  151. titlefont=dict(
  152. family='Courier New, monospace',
  153. size=18,
  154. color='#7f7f7f'
  155. )
  156. )
  157. )
  158. fig = go.Figure(data=data, layout=layout)
  159. plotly.offline.iplot(fig, filename='basic-area-no-bound')
  160. # -
  161.  
  162. pd.DataFrame({'vmin':vmin, 'vmax':vmax, 'lastyear':lastyear, 'thisyear': thisyear})
  163.  
  164. vmin = vmin.rename(index=str, columns={"value": "quantile>30%"})
  165. vmax = vmax.rename(index=str, columns={"value": "quantile<70%"})
  166. lastyear = lastyear.rename(index=str, columns={"value": "2017"})
  167. thisyear = thisyear.rename(index=str, columns={"value": "2018"})
  168. dataframe = lastyear.merge(thisyear,how='left').merge(vmin,how='left').merge(vmax,how='left')
  169. dataframe.head()
  170.  
  171. dataframe
  172.  
  173. vmax.head()
Add Comment
Please, Sign In to add comment