speice.io/assets/js/92079dc1.37202129.js

1 line
16 KiB
JavaScript

"use strict";(self.webpackChunkspeice_io=self.webpackChunkspeice_io||[]).push([["9366"],{23244:function(e,t,n){n.r(t),n.d(t,{assets:function(){return l},contentTitle:function(){return i},default:function(){return d},frontMatter:function(){return s},metadata:function(){return a},toc:function(){return c}});var a=n(37133),r=n(85893),o=n(50065);let s={slug:"2016/01/complaining-about-the-weather",title:"Complaining about the weather",date:new Date("2016-01-01T12:00:00.000Z"),authors:["bspeice"],tags:[]},i="Tracking Precipitation Chances",l={authorsImageUrls:[void 0]},c=[];function h(e){let t={a:"a",code:"code",h1:"h1",img:"img",p:"p",pre:"pre",...(0,o.a)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(t.p,{children:"Figuring out whether people should be complaining about the recent weather in North Carolina."}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-python",children:"from bokeh.plotting import figure, output_notebook, show\nfrom bokeh.palettes import PuBuGn9 as Palette\nimport pandas as pd\nimport numpy as np\nfrom datetime import datetime\nimport pickle\n\noutput_notebook()\n"})}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{children:"BokehJS successfully loaded.\n"})}),"\n",(0,r.jsx)(t.p,{children:"I'm originally from North Carolina, and I've been hearing a lot of people talking about how often it's been raining recently. They're excited for any day that has sun."}),"\n",(0,r.jsx)(t.p,{children:"So I got a bit curious: Has North Carolina over the past few months actually had more cloudy and rainy days recently than in previous years? This shouldn't be a particularly challenging task, but I'm interested to know if people's perceptions actually reflect reality."}),"\n",(0,r.jsxs)(t.p,{children:["The data we'll use comes from ",(0,r.jsx)(t.a,{href:"https://forecast.io",children:"forecast.io"}),", since they can give us a cloud cover percentage. I've gone ahead and retrieved the data to a pickle file, and included the ",(0,r.jsx)(t.a,{href:"#Generating-the-Forecast-file",children:"code that was used to generate it"}),'. First up: What was the average cloud cover in North Carolina during August - November, and how many days were cloudy? We\'re going to assume that a "cloudy" day is defined as any day in which the cloud cover is above 50%.']}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-python",children:"city_forecasts = pickle.load(open('city_forecasts.p', 'rb'))\nforecast_df = pd.DataFrame.from_dict(city_forecasts)\n"})}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-python",children:"cary_forecast = forecast_df['cary']\nyears = range(1990, 2016)\nmonths = range(7, 12)\nmonths_str = ['July', 'August', 'September', 'October', 'November']\n\ndef safe_cover(frame):\n if frame and 'cloudCover' in frame:\n return frame['cloudCover']\n else:\n return np.NaN\n\ndef monthly_avg_cloudcover(year, month):\n dates = pd.DatetimeIndex(start=datetime(year, month, 1, 12),\n end=datetime(year, month + 1, 1, 12),\n freq='D', closed='left')\n cloud_cover_vals = list(map(lambda x: safe_cover(cary_forecast[x]['currently']), dates))\n cloud_cover_samples = len(list(filter(lambda x: x is not np.NaN, cloud_cover_vals)))\n return np.nanmean(cloud_cover_vals), cloud_cover_samples\n\n\nmonthly_cover_vals = [[monthly_avg_cloudcover(y, m)[0] for y in years] for m in months]\n\nf = figure(title='Monthly Average Cloud Cover',\n x_range=(1990, 2015),\n x_axis_label='Year')\nfor x in range(0, len(months)):\n f.line(years, monthly_cover_vals[x], legend=months_str[x], color=Palette[x])\nshow(f)\n"})}),"\n",(0,r.jsx)(t.p,{children:(0,r.jsx)(t.img,{alt:"Monthly average cloud cover chart",src:n(33754).Z+"",width:"600",height:"600"})}),"\n",(0,r.jsx)(t.p,{children:"As we can see from the chart above, on the whole the monthly average cloud cover has been generally trending down over time. The average cloud cover is also lower than it was last year - it seems people are mostly just complaining. There are some data issues that start in 2012 that we need to be aware of - the cloud cover percentage doesn't exist for all days. Even so, the data that we have seems to reflect the wider trend, so we'll assume for now that the missing data doesn't skew our results."}),"\n",(0,r.jsx)(t.p,{children:"There's one more metric we want to check though - how many cloudy days were there? This is probably a better gauge of sentiment than the average monthly cover."}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-python",children:"def monthly_cloudy_days(year, month):\n dates = pd.DatetimeIndex(start=datetime(year, month, 1, 12),\n end=datetime(year, month + 1, 1, 12),\n freq='D', closed='left')\n cloud_cover_vals = list(map(lambda x: safe_cover(cary_forecast[x]['currently']), dates))\n cloud_cover_samples = len(list(filter(lambda x: x is not np.NaN, cloud_cover_vals)))\n cloudy_days = [cover > .5 for cover in cloud_cover_vals]\n return np.count_nonzero(cloudy_days), cloud_cover_samples\n\nmonthly_days_vals = [[monthly_cloudy_days(y, m)[0] for y in years] for m in months]\nmonthly_cover_samples = [[monthly_cloudy_days(y, m)[1] for y in years] for m in months]\n\nf = figure(title='Monthly Cloudy Days',\n x_range=(1990, 2015),\n x_axis_label='Year')\nfor x in range(0, len(months)):\n f.line(years, monthly_days_vals[x], legend=months_str[x], color=Palette[x])\nshow(f)\n\nf = figure(title='Monthly Cloud Cover Samples',\n x_range=(1990, 2015),\n x_axis_label='Year',\n height=300)\nfor x in range(0, len(months)):\n f.line(years, monthly_cover_samples[x], legend=months_str[x], color=Palette[x])\nshow(f)\n"})}),"\n",(0,r.jsx)(t.p,{children:(0,r.jsx)(t.img,{alt:"Monthly cloudy days chart",src:n(78469).Z+"",width:"600",height:"600"})}),"\n",(0,r.jsx)(t.p,{children:(0,r.jsx)(t.img,{alt:"Monthly cloud cover samples chart",src:n(13918).Z+"",width:"600",height:"300"})}),"\n",(0,r.jsx)(t.p,{children:"On the whole, the number of cloudy days seems to reflect the trend with average cloud cover - it's actually becoming more sunny as time progresses. That said, we need to be careful in how we view this number - because there weren't as many samples in 2015 as previous years, the number of days can get thrown off. In context though, even if most days not recorded were in fact cloudy, the overall count for 2015 would still be lower than previous years."}),"\n",(0,r.jsx)(t.p,{children:"In addition to checking cloud cover, I wanted to check precipitation data as well - what is the average precipitation chance over a month, and how many days during a month is rain likely? The thinking is that days with a high-precipitation chance will also be days in which it is cloudy or depressing."}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-python",children:"def safe_precip(frame):\n if frame and 'precipProbability' in frame:\n return frame['precipProbability']\n else:\n return np.NaN\n\ndef monthly_avg_precip(year, month):\n dates = pd.DatetimeIndex(start=datetime(year, month, 1, 12),\n end=datetime(year, month + 1, 1, 12),\n freq='D', closed='left')\n precip_vals = list(map(lambda x: safe_precip(cary_forecast[x]['currently']), dates))\n precip_samples = len(list(filter(lambda x: x is not np.NaN, precip_vals)))\n return np.nanmean(precip_vals), precip_samples\n\nmonthly_avg_precip_vals = [[monthly_avg_precip(y, m)[0] for y in years] for m in months]\n\nf = figure(title='Monthly Average Precipitation Chance',\n x_range=(1990, 2015),\n x_axis_label='Year')\nfor x in range(0, len(months)):\n f.line(years, monthly_avg_precip_vals[x], legend=months_str[x], color=Palette[x])\nshow(f)\n"})}),"\n",(0,r.jsx)(t.p,{children:(0,r.jsx)(t.img,{alt:"Monthly average precipitation chance chart",src:n(36814).Z+"",width:"600",height:"600"})}),"\n",(0,r.jsx)(t.p,{children:'As we can see from the chart, the average chance of precipitation over a month more or less stays within a band of 0 - .1 for all months over all years. This is further evidence that the past few months are no more cloudy or rainy than previous years. Like the cloud cover though, we still want to get a count of all the rainy days, in addition to the average chance. We\'ll define a "rainy day" as any day in which the chance of rain is greater than 25%.'}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-python",children:"def monthly_rainy_days(year, month):\n dates = pd.DatetimeIndex(start=datetime(year, month, 1, 12),\n end=datetime(year, month + 1, 1, 12),\n freq='D', closed='left')\n precip_prob_vals = list(map(lambda x: safe_precip(cary_forecast[x]['currently']), dates))\n precip_prob_samples = len(list(filter(lambda x: x is not np.NaN, precip_prob_vals)))\n precip_days = [prob > .25 for prob in precip_prob_vals]\n return np.count_nonzero(precip_days), precip_prob_samples\n\nmonthly_precip_days_vals = [[monthly_rainy_days(y, m)[0] for y in years] for m in months]\nmonthly_precip_samples = [[monthly_rainy_days(y, m)[1] for y in years] for m in months]\n\nf = figure(title='Monthly Rainy Days',\n x_range=(1990, 2015),\n x_axis_label='Year')\nfor x in range(0, len(months)):\n f.line(years, monthly_precip_days_vals[x], legend=months_str[x], color=Palette[x])\nshow(f)\n\nf = figure(title='Monthly Rainy Days Samples',\n x_range=(1990, 2015),\n x_axis_label='Year',\n height=300)\nfor x in range(0, len(months)):\n f.line(years, monthly_precip_samples[x], legend=months_str[x], color=Palette[x])\nshow(f)\n"})}),"\n",(0,r.jsx)(t.p,{children:(0,r.jsx)(t.img,{alt:"Monthly rainy days chart",src:n(3118).Z+"",width:"600",height:"600"})}),"\n",(0,r.jsx)(t.p,{children:(0,r.jsx)(t.img,{alt:"Monthly rainy days samples chart",src:n(70966).Z+"",width:"600",height:"300"})}),"\n",(0,r.jsx)(t.p,{children:"After trying to find the number of days that are rainy, we can see that November hit its max value for rainy days in 2015. However, that value is 6, as compared to a previous maximum of 5. While it is a new record, the value isn't actually all that different. And for other months, the values are mostly in-line with the averages."}),"\n",(0,r.jsx)(t.h1,{id:"summary-and-conclusions",children:"Summary and Conclusions"}),"\n",(0,r.jsx)(t.p,{children:"After having looked at forecast data for Cary, it appears that the months of July - November this year in terms of weather were at worst on par with prior years, if not slightly more sunny. This seems to be a case of confirmation bias: someone complains about a string of cloudy or rainy days, and suddenly you start noticing them more."}),"\n",(0,r.jsx)(t.p,{children:"While this analysis doesn't take into account other areas of North Carolina, my initial guess would be to assume that other areas also will show similar results: nothing interesting is happening. Maybe that will be for another blog post later!"}),"\n",(0,r.jsx)(t.p,{children:"Coming soon: I'll compare rain/cloud conditions in North Carolina to some other places in the U.S.!"}),"\n",(0,r.jsx)(t.h1,{id:"generating-the-forecast-file",children:"Generating the Forecast file"}),"\n",(0,r.jsx)(t.p,{children:"The following code was generates the file that was used throughout the blog post. Please note that I'm retrieving data for other cities to use in a future blog post, only Cary data was used for this post."}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-python",children:"import pandas as pd\nfrom functools import reduce\nimport requests\nfrom datetime import datetime\n\n# Coordinate data from http://itouchmap.com/latlong.html\ncary_loc = (35.79154,-78.781117)\nnyc_loc = (40.78306,-73.971249)\nseattle_loc = (47.60621,-122.332071)\nbinghamton_loc = (42.098687,-75.917974)\ncities = {\n 'cary': cary_loc,\n 'nyc': nyc_loc,\n 'seattle': seattle_loc,\n 'binghamton': binghamton_loc\n}\n\napikey = '' # My super-secret API Key\n\ndef get_forecast(lat, long, date=None):\n forecast_base = \"https://api.forecast.io/forecast/\"\n if date is None:\n url = forecast_base + apikey + '/{},{}'.format(lat, long)\n else:\n epoch = int(date.timestamp())\n url = forecast_base + apikey + '/{},{},{}'.format(lat, long, epoch)\n \n return requests.get(url).json()\n \nyears = range(1990,2016)\n# For datetimes, the 12 is for getting the weather at noon.\n# We're doing this over midnight because we're more concerned\n# with what people see, and people don't typically see the weather\n# at midnight.\ndt_indices = [pd.date_range(start=datetime(year, 7, 1, 12),\n end=datetime(year, 11, 30, 12))\n for year in years]\ndt_merge = reduce(lambda x, y: x.union(y), dt_indices)\n\n# Because we have to pay a little bit to use the API, we use for loops here\n# instead of a comprehension - if something breaks, we want to preserve the\n# data already retrieved\ncity_forecasts = {}\nfor city, loc in cities.items():\n print(\"Retrieving data for {} starting at {}\".format(city,\n datetime.now().strftime(\"%I:%M:%S %p\")))\n for dt in dt_merge:\n try:\n city_forecasts[(city, dt)] = get_forecast(*loc, dt)\n except Exception as e:\n print(e)\n city_forecasts[(city, dt)] = None\nprint(\"End forecast retrieval: {}\".format(datetime.now().strftime(\"%I:%M:%S %p\")))\n\nimport pickle\npickle.dump(city_forecasts, open('city_forecasts.p', 'wb'))\n\n### Output:\n# Retrieving data for binghamton starting at 05:13:42 PM\n# Retrieving data for seattle starting at 05:30:51 PM\n# Retrieving data for nyc starting at 05:48:30 PM\n# Retrieving data for cary starting at 06:08:32 PM\n# End forecast retrieval: 06:25:21 PM\n"})})]})}function d(e={}){let{wrapper:t}={...(0,o.a)(),...e.components};return t?(0,r.jsx)(t,{...e,children:(0,r.jsx)(h,{...e})}):h(e)}},33754:function(e,t,n){n.d(t,{Z:function(){return a}});let a=n.p+"assets/images/1-0d5e8450555296218deb0517b80440f3.png"},78469:function(e,t,n){n.d(t,{Z:function(){return a}});let a=n.p+"assets/images/2-062e1e47a07f200ff3b1531a02812bc7.png"},13918:function(e,t,n){n.d(t,{Z:function(){return a}});let a=n.p+"assets/images/3-eea635f8cfe4a12ae649ceb6c984e0cd.png"},36814:function(e,t,n){n.d(t,{Z:function(){return a}});let a=n.p+"assets/images/4-b4c3dbfa10b1997706bc271ca71e2ff5.png"},3118:function(e,t,n){n.d(t,{Z:function(){return a}});let a=n.p+"assets/images/5-8f10acd82b2f025abe57cb93d435a25f.png"},70966:function(e,t,n){n.d(t,{Z:function(){return a}});let a=n.p+"assets/images/6-456ca1125f48947cf3c1c13722af95a0.png"},50065:function(e,t,n){n.d(t,{Z:function(){return i},a:function(){return s}});var a=n(67294);let r={},o=a.createContext(r);function s(e){let t=a.useContext(o);return a.useMemo(function(){return"function"==typeof e?e(t):{...t,...e}},[t,e])}function i(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:s(e.components),a.createElement(o.Provider,{value:t},e.children)}},37133:function(e){e.exports=JSON.parse('{"permalink":"/2016/01/complaining-about-the-weather","source":"@site/blog/2016-01-01-complaining-about-the-weather/index.mdx","title":"Complaining about the weather","description":"Figuring out whether people should be complaining about the recent weather in North Carolina.","date":"2016-01-01T12:00:00.000Z","tags":[],"readingTime":7.475,"hasTruncateMarker":true,"authors":[{"name":"Bradlee Speice","socials":{"github":"https://github.com/bspeice"},"key":"bspeice","page":null}],"frontMatter":{"slug":"2016/01/complaining-about-the-weather","title":"Complaining about the weather","date":"2016-01-01T12:00:00.000Z","authors":["bspeice"],"tags":[]},"unlisted":false,"lastUpdatedAt":1730678252000,"prevItem":{"title":"Cloudy in Seattle","permalink":"/2016/01/cloudy-in-seattle"},"nextItem":{"title":"Testing Cramer","permalink":"/2015/12/testing-cramer"}}')}}]);