def _upload_new_grid(self): L.l.info("Uploading new {} grid metadata to plot.ly".format(self.grid_unique_name)) # grid was not retrieved yet from plotly, create it upload_columns = [] # create column list for grid upload, put first column = x axis upload_columns.append(Column(self.columns_cache[self.axis_x_name], self.axis_x_name)) # then add the remaining columns, except the above one that is already added for column_name in self.columns_cache.keys(): if column_name != self.axis_x_name: upload_columns.append(Column(self.columns_cache[column_name], column_name)) grid = Grid(upload_columns) self.grid_url = py.grid_ops.upload(grid, self.grid_unique_name, # name of the grid in your plotly account world_readable=True, # public or private auto_open=False) # open the grid in the browser # save new uploaded column names to maintain upload order for grid_column in upload_columns: if grid_column.name not in self.column_name_list_uploaded: self.column_name_list_uploaded.append(grid_column.name) # save to db cache. expect record to be empty plotly_cache_record = models.PlotlyCache().query_filter_first(models.PlotlyCache.grid_name.in_( [self.grid_unique_name])) if plotly_cache_record: L.l.critical("While uploading a new grid found a cached one in DB, unexpected failure!") else: plotly_cache_record = models.PlotlyCache(grid_name=self.grid_unique_name) plotly_cache_record.grid_url = self.grid_url my_column_list = ','.join(map(str, self.column_name_list_uploaded)) plotly_cache_record.column_name_list = my_column_list plotly_cache_record.created_by_node_name = Constant.HOST_NAME plotly_cache_record.save_changed_fields(new_record=plotly_cache_record, notify_transport_enabled=True, save_to_graph=False) L.l.info("Uploading {} grid completed".format(self.grid_unique_name))
def test3(): column_1 = Column([1, 2, 3], 'column 1') column_2 = Column(['a', 'b', utils.get_base_location_now_date()], 'column 2') grid = Grid([column_1, column_2]) unique_url = py.grid_ops.upload(grid, 'grid1', world_readable=True) print unique_url
def test5(): column_1 = Column([1, 2, 3], 'column 1') column_2 = Column( ['a', 'b', datetime.datetime.now()], 'column 2') # Tabular data can be numbers, strings, or dates grid = Grid([column_1, column_2]) url = py.grid_ops.upload( grid, 'grid example', # name of the grid in your plotly account world_readable=True, # public or private auto_open=True) # open the grid in the browser
def test_column_json_encoding(): columns = [ Column(numeric_list, 'col 1'), Column(mixed_list, 'col 2'), Column(np_list, 'col 3') ] json_columns = json.dumps( columns, cls=utils._plotlyJSONEncoder, sort_keys=True ) assert('[{"data": [1, 2, 3], "name": "col 1"}, ' '{"data": [1, "A", "2014-01-05"], "name": "col 2"}, ' '{"data": [1, 2, 3], "name": "col 3"}]' == json_columns)
def make_the_grid(self,): df = self.get_csv_data() close = list(df['close']) columns = [] self.n = len(close) for i in range(1, self.n): x = Column(list(df.index.values)[:i], 'x{}'.format(i - 1)) columns.append(x) y = Column(close[:i], 'y{}'.format(i - 1)) columns.append(y) grid = Grid(columns) return grid
def test_upload_meta_with_grid(): c1 = Column([1, 2, 3, 4], 'first column') Grid([c1]) unique_filename = _random_filename() py.grid_ops.upload(_grid, unique_filename, meta=_meta, auto_open=False)
def test_duplicate_filenames(): c1 = Column([1, 2, 3, 4], 'first column') g = Grid([c1]) random_chars = [random.choice(string.ascii_uppercase) for _ in range(5)] unique_filename = 'Valid Grid ' + ''.join(random_chars) py.grid_ops.upload(g, unique_filename, auto_open=False) try: py.grid_ops.upload(g, unique_filename, auto_open=False) except PlotlyRequestError as e: assert (e.status_code == 409)
def test_duplicate_filenames(self): c1 = Column([1, 2, 3, 4], 'first column') g = Grid([c1]) random_chars = [random.choice(string.ascii_uppercase) for _ in range(5)] unique_filename = 'Valid Grid ' + ''.join(random_chars) py.grid_ops.upload(g, unique_filename, auto_open=False) try: py.grid_ops.upload(g, unique_filename, auto_open=False) except PlotlyRequestError as e: pass else: self.fail('Expected this to fail!')
class MetaTest(PlotlyTestCase): _grid = grid = Grid([Column([1, 2, 3, 4], 'first column')]) _meta = {"settings": {"scope1": {"model": "Unicorn Finder", "voltage": 4}}} def setUp(self): super(MetaTest, self).setUp() py.sign_in('PythonTest', 'xnyU0DEwvAQQCwHVseIL') def random_filename(self): random_chars = [ random.choice(string.ascii_uppercase) for _ in range(5) ] unique_filename = 'Valid Grid with Meta ' + ''.join(random_chars) return unique_filename @attr('slow') def test_upload_meta(self): unique_filename = self.random_filename() grid_url = py.grid_ops.upload(self._grid, unique_filename, auto_open=False) # Add some Metadata to that grid py.meta_ops.upload(self._meta, grid_url=grid_url) @attr('slow') def test_upload_meta_with_grid(self): c1 = Column([1, 2, 3, 4], 'first column') Grid([c1]) unique_filename = self.random_filename() py.grid_ops.upload(self._grid, unique_filename, meta=self._meta, auto_open=False) @skip('adding this for now so test_file_tools pass, more info' + 'https://github.com/plotly/python-api/issues/263') def test_metadata_to_nonexistent_grid(self): non_exist_meta_url = 'https://local.plot.ly/~GridTest/999999999' with self.assertRaises(PlotlyRequestError): py.meta_ops.upload(self._meta, grid_url=non_exist_meta_url)
def _update_grid(self): # append empty new columns that were not yet uploaded to cloud grid upload_columns = [] for column_name in self.columns_cache.keys(): if column_name not in self.column_name_list_uploaded: upload_columns.append(Column([], column_name)) if len(upload_columns) > 0: L.l.info("Appending new columns grid={} count={}".format( self.grid_unique_name, len(upload_columns))) py.grid_ops.append_columns(columns=upload_columns, grid_url=self.grid_url) # save new uploaded column names to maintain upload order for grid_column in upload_columns: if grid_column.name not in self.column_name_list_uploaded: self.column_name_list_uploaded.append(grid_column.name) # append rows to grid, already in memory # convert data from columns to a list of rows upload_rows = [] rows_left = True index = 0 while index < self.max_row_count: row = [self.columns_cache[self.axis_x_name][index]] # adding primary key value # adding row value from each column at current index position for column_name in self.columns_cache.keys(): if column_name != self.axis_x_name: value = self.columns_cache[column_name][index] if value is None: value = '' row.append(value) upload_rows.append(row) index += 1 if len(upload_rows) > 0: L.l.info("Appending grid {} rows={}".format( self.grid_unique_name, len(upload_rows))) # upload all rows. column order and number of columns must match the grid in the cloud py.grid_ops.append_rows(grid_url=self.grid_url, rows=upload_rows) L.l.info("Uploading {} grid completed".format(self.grid_unique_name))
def test_column_append_of_non_uploaded_grid(self): c1 = Column([1, 2, 3, 4], 'first column') c2 = Column(['a', 'b', 'c', 'd'], 'second column') g = Grid([c1]) with self.assertRaises(PlotlyError): py.grid_ops.append_columns([c2], grid=g)
from nose import with_setup from nose.plugins.attrib import attr from nose.tools import raises from unittest import skip import plotly.plotly as py from plotly.exceptions import PlotlyRequestError from plotly.grid_objs import Column, Grid def init(): py.sign_in('PythonTest', '9v9f20pext') _grid = grid = Grid([Column([1, 2, 3, 4], 'first column')]) _meta = {"settings": {"scope1": {"model": "Unicorn Finder", "voltage": 4}}} def _random_filename(): random_chars = [random.choice(string.ascii_uppercase) for _ in range(5)] unique_filename = 'Valid Grid with Meta ' + ''.join(random_chars) return unique_filename @attr('slow') @with_setup(init) def test_upload_meta(): unique_filename = _random_filename() grid_url = py.grid_ops.upload(_grid, unique_filename, auto_open=False)
# Upload Grid grid_filename = chart_filename + " Grid" columns = [] for i, year in enumerate(years): # Filter df for current year only current_year = df[df.index == int(year)] # Get countries and their years ([0] since .valures returns a list of numpy arrays) countries = list(current_year.columns.astype(str)) zvalues = list(current_year.values.astype(float)[0]) #print(countries, zvalues) columns.append(Column(countries, "location{}".format(i + 1))) columns.append(Column(zvalues, "z{}".format(i + 1))) #print(countries,zvalues) # Will throw error if file exists or path is not root grid = Grid(columns) py.grid_ops.upload(grid, grid_filename, auto_open=False) # In[]: # Create data yellowblue = [[0, "rgb(255,255,204)"], [0.35, "rgb(161,218,180)"], [0.5, "rgb(65,182,196)"], [0.6, "rgb(44,127,184)"], [0.7, "rgb(8,104,172)"], [1, "rgb(37,52,148)"]]
sizes = list(current_year["blade_l"].astype(float)) # Iteratively grow list to create running counts xvalues = years[:i + 1] yvalues = ylist[:i + 1] yvalues_cum = ylist_cum[:i + 1] # Idem for state count statevalues = statecount[:i + 1] # Get data for Choropleth inlay statelist = current_year.groupby("state").count()["unique_id"] states = list(statelist.index.astype(str)) zvalues = list(statelist.astype(int)) columns.append(Column(lons, "x{}".format(i + 1))) columns.append(Column(lats, "y{}".format(i + 1))) columns.append(Column(texts, "text{}".format(i + 1))) columns.append(Column(sizes, "size{}".format(i + 1))) columns2.append(Column(xvalues, "x{}".format(i + 1))) columns2.append(Column(yvalues, "y{}".format(i + 1))) columns2.append(Column(yvalues_cum, "y_cum{}".format(i + 1))) columns2.append(Column(blade_avgs, "blade_avg{}".format(i + 1))) columns2.append(Column(statevalues, "state{}".format(i + 1))) columns2.append(Column(states, "location{}".format(i + 1))) columns2.append(Column(zvalues, "z{}".format(i + 1))) # Will throw error if file exists or path is not root grid = Grid(columns) py.grid_ops.upload(grid, grid_filename, auto_open=False)
def test_column_append(self): g = self.upload_and_return_grid() new_col = Column([1, 5, 3], 'new col') py.grid_ops.append_columns([new_col], grid=g)
def test_scatter_from_non_uploaded_grid(): c1 = Column([1, 2, 3, 4], 'first column') c2 = Column(['a', 'b', 'c', 'd'], 'second column') g = Grid([c1, c2]) Scatter(xsrc=g[0], ysrc=g[1])
for continent in dataset['continent']: if continent not in continents: continents.append(continent) columns = [] # make grid for year in years: for continent in continents: dataset_by_year = dataset[dataset['year'] == int(year)] dataset_by_year_and_cont = dataset_by_year[dataset_by_year['continent'] == continent] for col_name in dataset_by_year_and_cont: # each column name is unique column_name = '{year}_{continent}_{header}_gapminder_grid'.format( year=year, continent=continent, header=col_name ) a_column = Column(list(dataset_by_year_and_cont[col_name]), column_name) columns.append(a_column) # upload grid grid = Grid(columns) url = py.grid_ops.upload(grid, 'gapminder_grid'+str(time.time()), auto_open=False) figure = { 'data': [], 'layout': {}, 'frames': [], 'config': {'scrollzoom': True} } # fill in most of layout figure['layout']['xaxis'] = {'range': [30, 85], 'title': 'Life Expectancy', 'gridcolor': '#FFFFFF'}
# In[]: # Upload Grid chart_filename = "Pendulum " + str(datetime.now()) grid_filename = chart_filename + " Grid" columns = [] # Actual animation function for i in range(1000): pendulum.step(dt) x, y = pendulum.position() x = list(x) y = list(y) columns.append(Column(x, "x{}".format(i + 1))) columns.append(Column(y, "y{}".format(i + 1))) grid = Grid(columns) py.grid_ops.upload(grid, grid_filename, auto_open=False) # In[]: # Create data trace1 = Scatter( xsrc = grid.get_column_reference("x1"), ysrc = grid.get_column_reference("y1"), mode = "lines+markers", )
valid = valid[0:145] table = FF.create_table(valid) py.plot(table, filename='valid_run_data_table', auto_open=False) # %% def to_unix_time(dt): epoch = datetime.datetime.utcfromtimestamp(0) return (dt - epoch).total_seconds() * 1000 valid_value = list(valid['Value']) my_columns = [] for k in range(len(valid.index) - 1): my_columns.append(Column(valid.index[:k + 1], 'x{}'.format(k + 1))) my_columns.append(Column(valid_value[:k + 1], 'y{}'.format(k + 1))) grid = Grid(my_columns) py.grid_ops.upload(grid, 'valid_run_data_table' + str(time.time()), auto_open=False) # %% data = [dict( type='scatter', xsrc=grid.get_column_reference('x1'), ysrc= grid.get_column_reference('y1'), name='Model Accuracy', mode='lines', line=dict(color= '#FFA14E'), fill='tozeroy', fillcolor='#BB773A')]
def test_scatter_from_non_uploaded_grid(self): c1 = Column([1, 2, 3, 4], 'first column') c2 = Column(['a', 'b', 'c', 'd'], 'second column') g = Grid([c1, c2]) with self.assertRaises(InputError): Scatter(xsrc=g[0], ysrc=g[1])
[0.55, 'rgb(122, 137, 154)'], [0.6, 'rgb(133, 153, 165)'], [0.65, 'rgb(145, 169, 177)'], [0.7, 'rgb(156, 184, 188)'], [0.75, 'rgb(168, 199, 199)'], [0.8, 'rgb(185, 210, 210)'], [0.85, 'rgb(203, 221, 221)'], [0.9, 'rgb(220, 233, 233)'], [0.95, 'rgb(238, 244, 244)'], [1.0, 'rgb(255, 255, 255)']] my_columns = [] nr_frames = 68 for k in range(nr_frames): my_columns.extend( [Column((6.7 - k * 0.1) * np.ones((r, c)), 'z{}'.format(k + 1)), Column(np.flipud(volume[67 - k]), 'surfc{}'.format(k + 1))] ) grid = Grid(my_columns) py.grid_ops.upload(grid, 'anim_sliceshead'+str(time.time()), auto_open=False) data=[ dict( type='surface', zsrc=grid.get_column_reference('z1'), surfacecolorsrc=grid.get_column_reference('surfc1'), colorscale=pl_bone, colorbar=dict(thickness=20, ticklen=4) ) ]
for t in b: a.append("{}/{}:{}".format(t[0], t[1], t[2])) cursor.execute( "SELECT day FROM input WHERE frequency={} and transmission_power={}". format(f, T)) outcome = cursor.fetchall() days = [] for d in outcome: days.append(d[0]) a = list(set(days)) list_sort(a) x = np.arange(0, 8, 1) y = np.arange(0, 4, 1) my_columns = [Column(x, 'x'), Column(y, 'y')] for i in range(1, 38): raw = [] lines = [] cursor.execute( "SELECT Rss_mrc FROM input WHERE day='{}' and node_sender={} and frequency={} and transmission_power={}" .format(a[-1], i, f, T)) raw.append(cursor.fetchall()) for j in raw: for k in j: lines.append(k[0]) z = [[ "{}".format(lines[4]), "{}".format(lines[9]), "{}".format(lines[14]), "", "", "", "{}".format(lines[29]), "{}".format(lines[34]), "{}".format(lines[36]) ],
def test_row_append_of_non_uploaded_grid(self): c1 = Column([1, 2, 3, 4], 'first column') rows = [[1], [2]] g = Grid([c1]) with self.assertRaises(PlotlyError): py.grid_ops.append_rows(rows, grid=g)
def test_column_append_of_non_uploaded_grid(): c1 = Column([1, 2, 3, 4], 'first column') c2 = Column(['a', 'b', 'c', 'd'], 'second column') g = Grid([c1]) py.grid_ops.append_columns([c2], grid=g)
def test_duplicate_columns(self): c1 = Column([1, 2, 3, 4], 'first column') c2 = Column(['a', 'b', 'c', 'd'], 'first column') with self.assertRaises(InputError): Grid([c1, c2])
def test_row_append_of_non_uploaded_grid(): c1 = Column([1, 2, 3, 4], 'first column') rows = [[1], [2]] g = Grid([c1]) py.grid_ops.append_rows(rows, grid=g)
def get_grid(self): c1 = Column([1, 2, 3, 4], 'first column') c2 = Column(['a', 'b', 'c', 'd'], 'second column') g = Grid([c1, c2]) return g
def test_duplicate_columns(): c1 = Column([1, 2, 3, 4], 'first column') c2 = Column(['a', 'b', 'c', 'd'], 'first column') Grid([c1, c2])
def animated_chart(merged_df): ''' Gera um plot animado. O eixo x merged_df.columns[3], sendo o eixo y merged_df.columns[2], a legenda sendo os continentes, o tamanho dos circulos sendo merged_df.columns[4] e o controle deslizante é a variação dos anos de 1975 a 2014. Parameters: ----------- merged_df -> dataframe de 5 colunas, a configuração do gráfico exige formatação correta da coluna: eixo x merged_df.columns[3], sendo o eixo y merged_df.columns[2], A legenda sendo os continentes, O tamanho dos circulos sendo merged_df.columns[4] O controle deslizante é a variação dos anos de 1975 a 2014. Return: ------- Um gráfico animado. Example: -------- animated_chart(merged_df) ''' #Normalizando os dados para impedir problemas de escala: norm = lambda df: preprocessing.MinMaxScaler().fit_transform( np.array(df, dtype="float64").reshape(-1, 1)) for i in merged_df.columns[2:-1]: merged_df[i] = norm(merged_df[i]) #Criando lista de anos e continentes para usar na legenda e controle deslizante: List_of_years = list(range(1975, 2015)) List_Of_Continents = list(set(Continents.values())) merged_df['Year'] = merged_df['Year'].apply(int) columns = [] # Fazendo o grid: for year in List_of_years: for continent in List_Of_Continents: dataset_by_year = merged_df[merged_df['Year'] == int(year)] dataset_by_year_and_cont = dataset_by_year[ dataset_by_year['Continents'] == continent] for col_name in dataset_by_year_and_cont: column_name = '{year}_{continent}_{header}_gapminder_grid'.format( year=year, continent=continent, header=col_name) a_column = Column(list(dataset_by_year_and_cont[col_name]), column_name) columns.append(a_column) grid = Grid(columns) url = py.grid_ops.upload(grid, 'GDP.rateplot' + str(time.time()), auto_open=False) url #Criando a figura: figure1 = { 'data': [], 'layout': {}, 'frames': [], 'config': { 'scrollzoom': True } } figure1['layout']['xaxis'] = { 'title': list(merged_df.columns)[3], 'gridcolor': '#FFFFFF' } figure1['layout']['yaxis'] = { 'title': list(merged_df.columns)[2], 'type': 'log', 'gridcolor': '#FFFFFF' } figure1['layout']['hovermode'] = 'closest' figure1['layout']['plot_bgcolor'] = 'rgb(223, 232, 243)' #Criando Controle Deslizante: sliders_dict = { 'active': 0, 'yanchor': 'top', 'xanchor': 'left', 'currentvalue': { 'font': { 'size': 20 }, 'prefix': 'Year:', 'visible': True, 'xanchor': 'right' }, 'transition': { 'duration': 300, 'easing': 'cubic-in-out' }, 'pad': { 'b': 10, 't': 50 }, 'len': 0.9, 'x': 0.1, 'y': 0, 'steps': [] } #Criando legendas: figure1['layout']['updatemenus'] = [{ 'buttons': [{ 'args': [ None, { 'frame': { 'duration': 500, 'redraw': False }, 'fromcurrent': True, 'transition': { 'duration': 300, 'easing': 'quadratic-in-out' } } ], 'label': 'Play', 'method': 'animate' }, { 'args': [[None], { 'frame': { 'duration': 0, 'redraw': False }, 'mode': 'immediate', 'transition': { 'duration': 0 } }], 'label': 'Pause', 'method': 'animate' }], 'direction': 'left', 'pad': { 'r': 10, 't': 87 }, 'showactive': False, 'type': 'buttons', 'x': 0.1, 'xanchor': 'right', 'y': 0, 'yanchor': 'top' }] custom_colors = { 'East Asia & Pacific': 'rgb(171, 99, 250)', 'Europe & Central Asia': 'rgb(230, 99, 250)', 'Africa': 'rgb(99, 110, 250)', 'Latin America & Caribbean': 'rgb(25, 211, 243)', 'Middle East & North Africa': 'rgb(50, 170, 255)', 'North America': 'rgb(215,20,20)', 'South Asia': 'rgb(20,215,20)', 'Sub-Saharan Africa': 'rgb(0,0,0)', } #Criando layout da figura: col_name_template = '{year}_{continent}_{header}_gapminder_grid' year = 1975 for continent in List_Of_Continents: data_dict = { 'xsrc': grid.get_column_reference( col_name_template.format(year=year, continent=continent, header=list(merged_df.columns)[3])), 'ysrc': grid.get_column_reference( col_name_template.format(year=year, continent=continent, header=list(merged_df.columns)[2])), 'mode': 'markers', 'textsrc': grid.get_column_reference( col_name_template.format(year=year, continent=continent, header='Country')), 'marker': { 'sizemode': 'area', 'sizeref': 0.005, 'sizesrc': grid.get_column_reference( col_name_template.format(year=year, continent=continent, header=list( merged_df.columns)[4])), 'color': custom_colors[continent] }, 'name': continent } figure1['data'].append(data_dict) for year in List_of_years: frame = {'data': [], 'name': str(year)} for continent in List_Of_Continents: data_dict = { 'xsrc': grid.get_column_reference( col_name_template.format(year=year, continent=continent, header=list( merged_df.columns)[3])), 'ysrc': grid.get_column_reference( col_name_template.format(year=year, continent=continent, header=list( merged_df.columns)[2])), 'mode': 'markers', 'textsrc': grid.get_column_reference( col_name_template.format(year=year, continent=continent, header='Country')), 'marker': { 'sizemode': 'area', 'sizeref': 0.005, 'sizesrc': grid.get_column_reference( col_name_template.format(year=year, continent=continent, header=list( merged_df.columns)[4])), 'color': custom_colors[continent] }, 'name': continent } frame['data'].append(data_dict) figure1['frames'].append(frame) slider_step = { 'args': [[year], { 'frame': { 'duration': 300, 'redraw': False }, 'mode': 'immediate', 'transition': { 'duration': 300 } }], 'label': year, 'method': 'animate' } sliders_dict['steps'].append(slider_step) figure1['layout']['sliders'] = [sliders_dict] #Plotando: return py.icreate_animations(figure1, 'Plot1CreditGDP' + str(time.time()))
# Bug with Grid parsing if dataset isn't sanitized, need to return NaN instead of empty [] #years = [str(i) for i in range(1962,2007)] # In[]: # Upload Grid grid_filename = chart_filename + " Grid" columns = [] for i, year in enumerate(years): lons = df[df["YEAR"] == int(year)]["LON"].astype(float) lats = df[df["YEAR"] == int(year)]["LAT"].astype(float) texts = df[df["YEAR"] == int(year)]["STRCITY"].astype(str) columns.append(Column(lons, "x{}".format(i + 1))) columns.append(Column(lats, "y{}".format(i + 1))) columns.append(Column(texts, "text{}".format(i + 1))) # Will throw error if file exists or path is not root grid = Grid(columns) py.grid_ops.upload(grid, grid_filename, auto_open=False) # In[]: # Create data trace1 = Scattermapbox( # GENERAL lonsrc=grid.get_column_reference("x1"), latsrc=grid.get_column_reference("y1"),