def create_line_chart(categories, values, title, filename, charttype, orientation, axisreverse, stack): chart = Highchart() options = { 'chart': { 'type': charttype }, 'title': { 'text': title }, 'xAxis': { 'categories': categories, 'labels': { 'rotation': 45 } }, 'yAxis': { 'title': { 'text': '#Cases' }, 'stackLabels': { 'enabled': False, 'verticalAlign': 'top', 'y': 100 } }, 'plotOptions': { 'series': { 'dataLabels': { 'enabled': True }, 'stacking': 'normal' } }, 'legend': { 'enabled': True }, 'tooltip': { 'enabled': True, 'shared': True, 'formatter': "function(){ var s = '<b>'+ this.x +'</b>'; var s1=''; var sum = 0; $.each(this.points, function(i, point) { s1 += '<br/><b>'+ point.series.name +'</b>: '+ point.y ; sum += point.y; }); s += '<br/><b>Total</b>: '+sum ; return s+s1;}" } } chart.set_dict_options(options) print(options) colors = ['#1A5276', '#6E2C00', '#2C3E50', '#45B39D'] keys = list(values.keys()) for series in keys: data = values[series] chart.add_data_set(data, charttype, series, marker={'enabled': True}, color=colors[keys.index(series)]) print(filename) chart.save_file(filename)
def save_map(): chart = Highchart() chart.set_options('chart', {'inverted': True}) options = { 'title': { 'text': 'LoadBoy 接口负载图' }, 'subtitle': { 'text': '响应时间/吞吐量/线程数' }, 'xAxis': { 'reversed': False, 'title': { 'enabled': True, 'text': '响应时间' }, 'labels': { 'formatter': 'function () {\ return this.value + " t/s";\ }' }, 'maxPadding': 0.05, 'showLastLabel': True }, 'yAxis': { 'title': { 'text': '线程数' }, 'labels': { 'formatter': "function () {\ return this.value + '';\ }" }, 'lineWidth': 2 }, 'legend': { 'enabled': False }, 'tooltip': { 'headerFormat': '<b>{series.name}</b><br/>', 'pointFormat': '' } } chart.set_dict_options(options) global rt2 global tps global t_num tps_data = list(zip(tps, t_num)) chart.add_data_set(tps_data, 'spline', 'tps', marker={'enabled': False}) rt_data = list(zip(rt2, t_num)) chart.add_data_set(rt_data, 'line', 'rt', marker={'enabled': False}) chart.save_file()
def compareChart(currentWeek, students, names, averageRank): if not os.path.exists(chart_folder): os.makedirs(chart_folder) raw_options['chart']['height'] = 800 options = copy.deepcopy(raw_options) options['title']['text'] = 'Normalized Rank Comparison Chart' options['yAxis']['title']['text'] = 'Normalized Rank' options['xAxis']['title']['text'] = 'Week' chart = Highchart() chart.set_dict_options(options) series = [] for student in students: if not student.is_active: continue name = names[student.user_name] data = [] for week in range(1, currentWeek + 1): rank = averageRank[week-1].get(student.user_name) if rank is not None: point = [week, rank] data.append(point) series.append({'name': name, 'data': data}) chart.add_data_set(data, 'spline', name, marker={'enabled': True}) #options['series'] = series chart.save_file(chart_folder + 'compare')
def make_chart_daily_max_long_short(stats): chart = Highchart() options = { 'chart': { 'zoomType': 'x' }, 'title': { 'text': 'Max Long/Short intra day' }, 'xAxis': { 'type': 'datetime' }, 'yAxis': { 'title': { 'text': '$' } }, 'legend': { 'enabled': False } } chart.set_dict_options(options) timestamps = stats.index.astype(np.int64) // 1000000 long_data = np.dstack((timestamps, stats["max_long"].values))[0].tolist() chart.add_data_set(long_data, 'column', 'Max Long') short_data = np.dstack( (timestamps, -stats["max_short"].values))[0].tolist() chart.add_data_set(short_data, 'column', 'Max Short') return chart
def make_chart_daily_exposure(stats): chart = Highchart() options = { 'chart': { 'zoomType': 'x' }, 'title': { 'text': 'End of day exposure' }, 'xAxis': { 'type': 'datetime' }, 'yAxis': { 'title': { 'text': 'Exposure' } }, 'legend': { 'enabled': False } } chart.set_dict_options(options) timestamps = stats.index.astype(np.int64) // 1000000 plot_data = np.dstack( (timestamps, stats["day_end_exposure"].values))[0].tolist() chart.add_data_set(plot_data, 'area', 'Exposure') return chart
def make_chart_realized_pnl(stats): chart = Highchart() options = { 'chart': { 'zoomType': 'x' }, 'title': { 'text': 'Realized Cumulative Profit and Loss' }, 'xAxis': { 'type': 'datetime' }, 'yAxis': { 'title': { 'text': 'Realized P/L' } }, 'legend': { 'enabled': False } } chart.set_dict_options(options) timestamps = stats.index.astype(np.int64) // 1000000 plot_data = np.dstack( (timestamps, stats["realized_pnl"].values))[0].tolist() chart.add_data_set(plot_data, 'line', 'PnL') return chart
def make_chart_max_drawdown(stats): chart = Highchart() options = { 'chart': { 'zoomType': 'x' }, 'title': { 'text': 'Max Drawdown' }, 'xAxis': { 'type': 'datetime' }, 'yAxis': { 'title': { 'text': 'Daily P/L' } }, 'legend': { 'enabled': False } } chart.set_dict_options(options) timestamps = stats.index.astype(np.int64) // 1000000 plot_data = np.dstack( (timestamps, stats["max_drawdown"].values))[0].tolist() chart.add_data_set(plot_data, 'column', 'Max Drawdown') return chart
def create_chart(): chart = Highchart() options = { 'title': { 'text': 'Loss over time for various models' }, 'xAxis': { 'title': { 'text': 'Number of images' }, 'maxPadding': 0.05, 'showLastLabel': True }, 'yAxis': { 'title': { 'text': 'Loss' }, 'lineWidth': 2 }, 'legend': { 'enabled': True, 'layout': 'vertical', 'align': 'left', 'verticalAlign': 'middle' }, 'tooltip': { 'headerFormat': '<b>{series.name}</b><br/>', 'pointFormat': '{point.x} images: Loss = {point.y}' } } chart.set_dict_options(options) return chart
def timeline_python_highcharts(): """Return filename of plot of the damped_vibration function.""" H = Highchart() timeline_data = pd.read_csv('timeline.txt') options = { 'chart': { 'type': 'columnrange', 'inverted': True, 'zoomType': 'y' }, 'title': { 'text': 'BepiTimeline Test' }, 'xAxis': { 'categories': list(set(timeline_data['Instrument'].values.tolist())) }, 'yAxis': { 'type': 'datetime' }, 'tooltip': { 'formatter': "function () {return Highcharts.dateFormat('%e %B %H:%M', this.point.low) + ' - ' + Highcharts.dateFormat('%e %B %H:%M', this.point.high);}" }, 'plotOptions': { 'columnrange': { 'grouping': False } } } H.set_dict_options(options) grouped = timeline_data.groupby('Instrument') grouped = [grouped.get_group(x) for x in grouped.groups] for level, frame in enumerate(grouped): df = {} df['name'] = frame['Instrument'].values[0] df['data'] = [] for row in frame.itertuples(): block = {} block['x'] = level st = dt.strptime(row[2], '%Y-%m-%d %H:%M') st = int((st-dt(1970,1,1)).total_seconds()*1000) en = dt.strptime(row[3], '%Y-%m-%d %H:%M') en = int((en-dt(1970,1,1)).total_seconds()*1000) block['low'] = st block['high'] = en df['data'].append(block) H.add_data_set(df['data'], 'columnrange', df['name'] ) print(H.iframe) return 0
def generateCharts(currentWeek, students, names, averageRank, averageToken): if not os.path.exists(chart_folder): os.makedirs(chart_folder) raw_options['chart']['height'] = 500 options = copy.deepcopy(raw_options) options['yAxis'] = [{ 'min' : -1, 'max' : 1, 'reversed': True, 'title': { 'text': 'Normalized Rank' }, 'labels': { 'formatter': "function () {\ return this.value;\ }" }, 'lineWidth': 2 }, { 'reversed': True, 'title': { 'text': 'Normalized Token' }, 'labels': { 'formatter': "function () {\ return this.value;\ }" }, 'lineWidth': 2, 'opposite': True }, ] options['xAxis']['title']['text'] = 'Week' for student in students: if not student.is_active: continue chart = Highchart() options['title']['text'] = names[student.user_name] options['chart']['renderTo'] = 'container_' + student.user_name chart.set_dict_options(options) rank_data = [] token_data = [] for week in range(1, currentWeek + 1): rank = averageRank[week-1].get(student.user_name) token = averageToken[week-1].get(student.user_name) if rank is not None and token is not None: point = [week, rank] rank_data.append(point) point = [week, token] token_data.append(point) chart.add_data_set(rank_data, 'spline', 'Normalized Rank', marker={'enabled': True}) chart.add_data_set(token_data, 'spline', 'Normalized Token', marker={'enabled': True}, yAxis=1) chart.save_file(chart_folder + student.user_name)
def create_highcharts(device_name, results): categories = [] ssid_categories = results[3] for pair in ssid_categories: categories.append(pair[1]) chart = Highchart(width=1000, height=400) styling = '<span style="font-family: \'DejaVu Sans\', Arial, Helvetica, sans-serif; color: ' options = { 'title': { 'text': styling + '#00AEEF; font-size: 20px; line-height: 1.2640625; ">' + 'Wi-Fi Performance ' + device_name + '</span>' }, 'chart': { 'zoomType': 'x' }, 'xAxis': { 'type': 'datetime', 'title': { 'text': styling + '#F7941C; font-size: 12px; line-height: 1.4640625; font-weight: bold;">Time [h:min:s]</span>' } }, 'yAxis': [{ 'title': { 'text': styling + '#00AEEF; font-size: 12px; line-height: 1.2640625; font-weight: bold; ">Throughput [bps]</span>' }, }, { 'title': { 'text': styling + '#EC008C; font-size: 12px; line-height: 1.2640625; font-weight: bold; ">RSSI</span>' }, 'opposite': 'true' }, { 'title': { 'text': styling + '#00A650; font-size: 12px; line-height: 1.2640625; font-weight: bold; ">SSID/BSSID</span>' }, 'categories': categories, 'opposite': 'true' }], } chart.set_dict_options(options) return chart
def create_graph(self, custom_begin_date, custom_end_date, quantity, time_interval): sensor_node = SensorNode(self.node_id) timestamps = self.get_begin_and_end_timestamps(custom_begin_date, custom_end_date, time_interval) line_graph = Highchart() time_label = '' sensor_node.set_measurements( self.select_measurements(timestamps[0], timestamps[1], quantity)) measurement_types = sensor_node.get_distinct_measurement_types() for m_type in measurement_types: data_list = sensor_node.create_data_list(m_type) line_graph.add_data_set( data_list, series_type='line', name=(sensor_node.get_sensor_description(m_type))) if time_interval == TimeInterval.TODAY or time_interval == TimeInterval.YESTERDAY: time_label = "'hour': '%H:%M'" elif time_interval == TimeInterval.LAST_WEEK: time_label = "'day': '%e. %b'" options = { 'title': { 'text': str(quantity) + " meetdata van bijenkast " + str(sensor_node.node_id) }, 'subtitle': { 'text': "iBee 2017-2018" }, 'xAxis': { 'type': "datetime", 'dateTimeLabelFormats': {time_label}, 'title': { 'text': "Tijd" } }, 'yAxis': { 'title': { 'text': quantity } } } line_graph.set_dict_options(options) return line_graph
def plot_2d(self): h = Highchart(width=750, height=600) options = { 'title': { 'text': 'Value Function Iteration' }, 'xAxis': { 'title': { 'text': "K - Capital Level" } }, 'yAxis': { 'title': { 'text': "Value of Capital" } }, 'tooltip': { 'crosshairs': False, 'shared': True, }, 'legend': {} } h.set_dict_options(options) for x in range(0, len(self.z_vector)): df1 = pd.DataFrame({ 'k': self.k_vector[1:], 'value': self.value_matrix[1:, [x]].flatten() }) df1 = df1[['k', 'value']] df1 = df1.values.tolist() h.add_data_set(df1, 'spline', 'z_' + str(x) + ' = ' + str(self.z_vector[x]), zIndex=1, marker={ 'fillColor': 'white', 'lineWidth': 2, 'lineColor': 'Highcharts.getOptions().colors[1]' }) html_str = h.htmlcontent.encode('utf-8') html_file = open("chart.html", "w") html_file.write(html_str) html_file.close()
def renderChart(stats, bottom, left="Workflow Job"): keys = [k for k, _ in stats.items()] values = [v for _, v in stats.items()] chart = Highchart() chart.set_options("chart", {"inverted": True}) options = { "title": { "text": "{0} by {1}".format(bottom, left) }, "xAxis": { "categories": keys, "title": { "text": left }, "maxPadding": 0.05, "showLastLabel": True, }, "yAxis": { "title": { "text": bottom }, "labels": { "formatter": "function () {\ return this.value;\ }" }, "lineWidth": 2, }, "legend": { "enabled": False }, "tooltip": { "headerFormat": "<b>{series.name}</b><br/>", "pointFormat": "{point.x} : {point.y}", }, } chart.set_dict_options(options) chart.add_data_set(values, "bar") return str(chart)
def plot_bar_chart_from_pandas_dataframe(df): assert list(df[df.columns[0]]) == list( range(1, len(chart_config.OFIG_X_AXIS_CATEGORIES) + 1)), "Dataframe categories are in the wrong order" assert len(df.index) == len( chart_config.OFIG_X_AXIS_CATEGORIES ), "Dataframe does not contain values for every category" question_export_tag = df.columns[0] chart = Highchart() chart.set_dict_options( chart_config.OfigChartOptions.CHART_OPTIONS.to_dict()) for series_name in df.columns[1:]: data = list(df[series_name]) chart.add_data_set( data, series_type="bar", name=series_name, color=chart_config.THISCOVERY_RED, ) return output_to_files(chart, question_export_tag)
def make_chart_asset_allocation(product_map, account): allocations = list( map(functools.partial(get_position_value, product_map, account), account.get_ids())) capital = account.get_stats().tail(1)['day_end_capital'][0] allocations.append({'name': 'Cash', 'value': capital}) total_allocations = sum(map(lambda x: x['value'], allocations)) allocations_percent = list( map( lambda x: { 'name': x['name'], 'y': x['value'] / total_allocations * 100 }, allocations)) chart = Highchart() options = { 'chart': { 'zoomType': 'x' }, 'title': { 'text': 'Asset allocation' }, 'xAxis': { 'type': 'datetime' }, 'yAxis': { 'title': { 'text': 'Daily P/L' } }, 'legend': { 'enabled': False } } chart.set_dict_options(options) chart.add_data_set(allocations_percent, 'pie', 'Asset allocation') return chart
def cat_page(): # return render_template('cat_page.html') url = 'http://catsgalore.me/api/breeds/' r = requests.get(url) breeds = r.json()['breeds'] data = [br['weight'] for br in breeds] weights = [map(float, tuple(b[1:b.find(')')].split(' to '))) for b in data] chart = Highchart() chart.add_data_set(weights, 'scatter', 'Cat Weights') chart.set_dict_options(options) w = BeautifulSoup(open(BASE_URL + "templates/cat_page.html")) c = BeautifulSoup(str(chart.htmlcontent)) scripts = c.find_all(lambda tag: (tag.has_attr('src') and tag.name == 'script') or tag.name == 'link') chart_script = c.find_all( lambda tag: not tag.has_attr('type') and tag.name == 'script')[0] loading = c.div head = w.head div = w.find_all('div', class_='table-wrapper')[0] div.insert(0, loading) div.insert(1, chart_script) for s in scripts: head.append(s) html = w.prettify("utf-8") with open(BASE_URL + "templates/cats.html", "wb") as f: f.write(html) return render_template("cats.html")
class QuestionChart: available_series_colours = copy.copy(THISCOVERY_SERIES_COLOURS) study_group_colour_map = dict() def __init__(self, **kwargs): self.q_df = kwargs["question_dataframe"] self.tag = kwargs["question_tag"] self.levels = kwargs["target_levels"] self.study_groups_definition = kwargs["study_groups_definition"] self.chart = Highchart() chart_options = kwargs["chart_options"] if isinstance(chart_options, DeepChainMap): chart_options = chart_options.to_dict() self.chart.set_dict_options(chart_options) self.chart_definition = None # calculated by _chart method def add_index_column(self): self.q_df.insert(loc=0, column=self.tag, value=self.q_df.index) def python_highcharts_hacks(self, chart_definition): """ Hacks to overcome current limitations of python-highcharts """ # interpret JS undefined as such chart_definition = chart_definition.replace('"undefined"', "undefined") # remove quotes from JS functions (so they are not strings) # this is only necessary because python-highcharts has no # support for the responsive option at present function_re = re.compile(r'"function \(\).+;}"') if (m := function_re.search(chart_definition)): chart_definition = chart_definition.replace( m.group(), m.group().strip('"')) return chart_definition
def visualize_users_by_followers(self): df_path = f'{os.path.expanduser(self.save_path)}/' \ f'{self.keyword}/users_by_followers/' \ f'users_by_followers.csv' df = pd.read_csv(df_path) top_users = 20 df = df.head(top_users) options = { 'title': { 'text': f'Top {top_users} of users by number ' f'of followers for {self.keyword}', 'style': { 'fontSize': '20' } }, 'xAxis': { 'categories': df.user.values.tolist(), 'labels': { 'style': { 'fontSize': '13px' } }, 'title': { 'text': 'User', 'style': { 'fontSize': '15' } } }, 'yAxis': { 'title': { 'text': 'Number of followers', 'style': { 'fontSize': '15' } }, 'allowDecimals': False, 'labels': { 'style': { 'fontSize': '15px' }, 'format': '{value}' } }, 'plotOptions': { 'series': { 'showInLegend': False } }, 'chart': { 'backgroundColor': 'white' } } # Create chart h = Highchart(width=1000, height=700) h.set_dict_options(options) h.add_data_set(df.count_followers.values.tolist(), 'bar', 'Mentions count', color='#1998CB') # Save chart save_chart_path = f'{os.path.expanduser(self.save_path)}/' \ f'{self.keyword}/users_by_followers/' \ f'users_by_followers.html' self.func_save_html(save_chart_path, h.htmlcontent)
option is a (python) dict for options settings The way to use this method is very similar to the options object as on highcharts docs: http://www.highcharts.com/docs/getting-started/how-to-set-options 1. construct option (python) dict similar to the option object in javascript 2. input option dict using set_dict_options (for all the option details please ref to highcharts API: http://api.highcharts.com/highcharts#) """ options = { 'xAxis': { 'plotBands': [{ 'color': '#FCFFC5', 'from': 2, 'to': 4 }, { 'color': '#FCFFC5', 'from': 6, 'to': 8 }, { 'color': '#FCFFC5', 'from': 10, 'to': 12 }] } } H.set_dict_options( options) # input option object using set_dict_options method H # showing the chart on ipython H.save_file('highcharts' ) # save result as .html file with input name (and location path)
def create_chart(categories, values, title, filename, charttype, orientation, axisreverse, stack): chart = Highchart() options = { 'chart': { 'type': charttype }, 'title': { 'text': title }, 'xAxis': { 'reversed': axisreverse, 'categories': categories, 'maxPadding': 0.05, 'showLastLabel': True }, 'yAxis': { 'title': { 'text': '#Cases' }, 'stackLabels': { 'enabled': stack, 'style': { 'color': 'black' } }, 'lineWidth': 2 }, 'plotOptions': { 'series': { 'dataLabels': { 'enabled': True, 'style': { 'fontWeight': 'bold', 'color': 'gray' } }, 'stacking': 'normal' } }, 'legend': { 'enabled': True }, 'tooltip': { 'shared': True, 'enabled': True } } chart.set_dict_options(options) print(options) colors = ['#1A5276', '#6E2C00', '#2C3E50'] keys = list(values.keys()) for series in keys: data = values[series] chart.add_data_set(data, charttype, series, marker={'enabled': True}, color=colors[keys.index(series)]) print(filename) chart.save_file(filename)
countries = {} for doc in docs: if LANG in doc: lang = doc[LANG] if lang: lang = lang.upper() if lang in langs: langs[lang] += 1 else: langs[lang] = 1 if COUNTRY in doc: country = doc[COUNTRY] if country: if country in countries: countries[country] += 1 else: countries[country] = 1 chart = Highchart() options = {TITLE : {TEXT : 'Results per Language'}} chart.set_dict_options(options) chart.add_data_set(langs.items(), series_type='pie', name='Results') chart.save_file(splitext(argv[2])[0]) chart = Highchart() options = {TITLE : {TEXT : 'Results per Country'}} chart.set_dict_options(options) chart.add_data_set(countries.items(), series_type='pie', name='Results') chart.save_file(splitext(argv[3])[0])
H.set_options('tooltip', {'formatter': 'default_tooltip'}) H.set_options('xAxis', {'events': {'pointBreak': 'function(e){return}'}}) H.set_options('chart', {'style': {'fontFamily': 'Lucida Grande, sans-serif', "fontfontSize": '12px'}}) H.set_options('chart', {'style': {"fontfontSize": '22px'}}) H.set_options('chart', {'resetZoomButton': {'position': {'x': 10}}}) H.set_options('chart', {'resetZoomButton': {'relativeTo': 'chart'}}) """ Set up highchart options using 2. set_dict_options method: set_dict_options(options) option is a (python) dict for options settings The way to use this method is very similar to the options object as on highcharts docs: http://www.highcharts.com/docs/getting-started/how-to-set-options 1. construct option (python) dict similar to the option object in javascript 2. input option dict using set_dict_options (for all the option details please ref to highcharts API: http://api.highcharts.com/highcharts#) """ options = { 'xAxis':{ 'plotBands': [{'color': '#FCFFC5', 'from': 2, 'to': 4}, {'color': '#FCFFC5', 'from': 6, 'to': 8}, {'color': '#FCFFC5', 'from': 10, 'to': 12}] } } H.set_dict_options(options) # input option object using set_dict_options method H # showing the chart on ipython H.save_file('highcharts') # save result as .html file with input name (and location path)
}, 'yAxis': { 'allowDecimals': False, 'min': 0, 'title': { 'text': 'Number of fruits' } }, 'tooltip': { 'formatter': "function () {\ return '<b>' + this.x + '</b><br/>' +\ this.series.name + ': ' + this.y + '<br/>' +\ 'Total: ' + this.point.stackTotal;\ }" }, 'plotOptions': { 'column': { 'stacking': 'normal' } } } H.set_dict_options(options) H.add_data_set(data1, 'column', 'John', stack='male' ) H.add_data_set(data2, 'column', 'Joe', stack='male') H.add_data_set(data3, 'column', 'Jane', stack='female') H.add_data_set(data4, 'column', 'Janet', stack='female') H.htmlcontent
from highcharts import Highchart chart = Highchart() options = { 'chart': { 'type':'bar'}, 'title':{ 'text':'Top 10 Hashtags'}, 'xAxis':{ 'categories':['1','2','3','4','5','6','7','8','9','10']}, 'yAxis':{ 'title':{ 'text':'Number of times mentioned'} }, } data1 #This is an array. make size 10. first means first graoh chart.set_dict_options(options) chart.add_data_set(data1, 'bar', 'Count') chart.save_file('./bar-highcharts')
def plot(summary, currency=None, width=None, height=None): """ Given a transaction summary of the form output by :func:`summarize`, plot it using Python HighCharts. Include the given currency units (string; e.g. 'NZD') in the y-axis label. Override the default chart width and height, if desired. """ f = summary.copy() chart = Highchart() # Initialize chart options. # HighCharts kludge: use categorical x-axis to display dates properly. dates = f['date'].map(lambda x: x.strftime('%Y-%m-%d')).unique() dates = sorted(dates.tolist()) if currency is not None: y_text = 'Money ({!s})'.format(currency) else: currency = '' y_text = 'Money' chart_opts = { 'lang': { 'thousandsSep': ',' }, 'chart': { 'zoomType': 'xy', }, 'title': { 'text': 'Account Summary' }, 'xAxis': { 'type': 'category', 'categories': dates, }, 'yAxis': { 'title': { 'text': y_text, }, 'reversedStacks': False, }, 'tooltip': { 'headerFormat': '<b>{point.key}</b><table>', 'useHTML': True, }, 'plotOptions': { 'column': { 'pointPadding': 0, 'borderWidth': 1, 'borderColor': '#333333', } }, 'credits': { 'enabled': False, }, } if width is not None: chart_opts['chart']['width'] = width if height is not None: chart_opts['chart']['height'] = height if 'category' in f.columns: # Update chart options chart_opts['plotOptions']['series'] = {'stacking': 'normal'} chart_opts['tooltip']['pointFormat'] = ''' <tr> <td style="padding-right:1em">{series.name} ({point.percentage:.0f}%)</td> <td style="text-align:right">{point.y:,.0f} ''' + currency +\ ''' </td> </tr> ''' chart_opts['tooltip']['footerFormat'] = ''' <tr> <td style="padding-right:1em">Stack total</td> <td style="text-align:right">{point.total:,.0f} ''' + currency +\ ''' </td> </tr></table> ''' chart_opts['tooltip']['shared'] = False # Create data series. # Split income and expense into two stacks, each split by category. for column in ['income', 'expense']: # Sort categories by greatest value to least g = f.groupby('category').sum().reset_index( ).sort_values(column, ascending=False) categories = g.loc[g[column] > 0, 'category'].unique() n = len(categories) colors = get_colors(column, n) cond1 = f[column] > 0 for category, color in zip(categories, colors): cond2 = (cond1 | f[column].isnull()) &\ (f['category'] == category) g = f[cond2].copy() name = '{!s} {!s}'.format(column.capitalize(), category) series_opts = { 'name': name, 'color': color, 'series_type': 'column', 'stack': column, } chart.add_data_set(g[column].values.tolist(), **series_opts) # Aggregate balance def my_agg(group): d = {} d['balance'] = group['balance'].iat[0] return pd.Series(d) g = f.groupby('date')['balance'].first().reset_index() series_opts = { 'name': 'Balance', 'color': get_colors('balance', 1)[0], 'series_type': 'line', } chart.add_data_set(g['balance'].values.tolist(), **series_opts) else: # Update chart options chart_opts['tooltip']['pointFormat'] = ''' <tr> <td style="padding-right:1em">{series.name}</td> <td style="text-align:right">{point.y:,.0f} ''' + currency +\ ''' </td> </tr> ''' chart_opts['tooltip']['footerFormat'] = '</table>' chart_opts['tooltip']['shared'] = True # Create data series for column in ['income', 'expense', 'balance']: series_opts = { 'color': get_colors(column, 1)[0], 'name': column.split('_')[-1].capitalize(), 'series_type': 'line' if column == 'balance' else 'column' } chart.add_data_set(f[column].values.tolist(), **series_opts) chart.set_dict_options(chart_opts) return chart
def visualize_most_mentioned_hashtags(self): df_path = f'{os.path.expanduser(self.save_path)}/{self.keyword}/' \ f'most_mentioned_hashtags/' \ f'most_mentioned_hashtags.csv' df = pd.read_csv(df_path) top_hashtags = 20 df = df.head(top_hashtags) options = { 'title': { 'text': f'Top {top_hashtags} most mentioned hashtags ' f'for {self.keyword}', 'style': { 'fontSize': '20' } }, 'xAxis': { 'categories': df.hashtags.values.tolist(), 'labels': { 'style': { 'fontSize': '13px' } }, 'title': { 'text': 'Hashtags mentioned', 'style': { 'fontSize': '15' } } }, 'yAxis': { 'title': { 'text': 'Count of hashtags', 'style': { 'fontSize': '15' } }, 'allowDecimals': False, 'labels': { 'style': { 'fontSize': '15px' }, 'format': '{value}' } }, 'plotOptions': { 'series': { 'showInLegend': False } }, 'chart': { 'backgroundColor': 'white' } } # Create chart h = Highchart(width=1000, height=700) h.set_dict_options(options) h.add_data_set(df.hashtags_count.values.tolist(), 'bar', 'Mentions count', color='#1998CB') # Save chart save_chart_path = f'{os.path.expanduser(self.save_path)}/' \ f'{self.keyword}/most_mentioned_hashtags/' \ f'most_mentioned_hashtags.html' self.func_save_html(save_chart_path, h.htmlcontent)
def visualize_grouped_date(self): df_path = f'{os.path.expanduser(self.save_path)}/{self.keyword}/' \ f'grouped_date/grouped_date.csv' df = pd.read_csv(df_path) # Order month category df['month'] = df['month'].astype('category') \ .cat.add_categories([month for month in self.months_order if month not in df.month.unique().tolist()]) df.set_index(['year', 'month'], inplace=True) options = { 'title': { 'text': f'Count of tweets, retweets, and favorites by ' f'month for {self.keyword}' f'from {df.index.min()[0]}-{df.index.min()[1]} ' f'to {df.index.max()[0]}-{df.index.max()[1]}', 'style': { 'fontSize': '20' } }, 'xAxis': { 'categories': df.index.tolist(), 'labels': { 'style': { 'fontSize': '13px' } }, 'title': { 'text': 'Date (Year, Month)', 'style': { 'fontSize': '15' } } }, 'yAxis': { 'title': { 'text': 'Count', 'style': { 'fontSize': '15' } }, 'allowDecimals': False, 'labels': { 'style': { 'fontSize': '15px' }, 'format': '{value}' } }, 'plotOptions': { 'column': { 'stacking': 'normal' } }, 'chart': { 'backgroundColor': 'white' } } # Create chart h = Highchart(width=1000, height=700) h.set_dict_options(options) h.add_data_set(df.retweet_count.values.tolist(), 'line', 'Retweets count', color='#1998CB') h.add_data_set(df.favorite_count.values.tolist(), 'line', 'Favorites count', color='#F0CD13') h.add_data_set(df.tweets_published.values.tolist(), 'line', 'Tweets published count', color='#EC3C37') # Save chart save_chart_path = f'{os.path.expanduser(self.save_path)}/' \ f'{self.keyword}/grouped_date/grouped_date.html' self.func_save_html(save_chart_path, h.htmlcontent)
'series': { 'label': { 'minFontSize': 5, 'maxFontSize': 15, 'enabled':True, 'style': { 'color': 'blue' }, 'onArea':True } } } } H_area.set_dict_options(options) H_area.add_data_set(Joy, 'area', 'joy',color = 'rgb(241,199,28)') H_area.add_data_set(Anger, 'area', 'anger',color ='rgb(207,46,17)') H_area.add_data_set(Disgust, 'area', 'disgust',color ='rgb(118,181,92)') H_area.add_data_set(Fear, 'area', 'fear',color ='rgb(151,77,193)') H_area.add_data_set(Sadness, 'area', 'sadness',color ='rgb(46,116,213)') H_area.save_file('output_charts_area') options_column = { 'chart': { 'type': 'column', 'renderTo': 'container_column', 'options3d': { 'enabled': True, 'alpha': 15,
def main(): pwd = str(os.getcwd()) home = os.path.abspath(os.path.join(pwd, os.pardir)) # data location, relative. dir = '/data/train_1/' # define test file, layout is from when i wanted to iterate over all files # to run over all files change line to '*.mat' file_type = '1_920_0.mat' file_list = glob.glob(home + dir + file_type) for f in file_list: name = f[:-4] name = name.split('/') name = name[len(name) - 1] # load .mat file mat = scipy.io.loadmat(f) headers = [ 'channel0', 'channel1', 'channel2', 'channel3', 'channel4', 'channel5', 'channel6', 'channel7', 'channel8', 'channel9', 'channel10', 'channel11', 'channel12', 'channel13', 'channel14', 'channel15' ] # get actual data from the .mat file channels_data = mat['dataStruct'][0][0][0] # resample file rs_data = resample(channels_data, 3000, axis=0) df = pandas.DataFrame(rs_data, columns=headers) charts = Highchart() options = { 'chart': { 'type': 'line', 'zoomType': 'x' }, 'title': { 'text': 'test' }, 'xAxis': { 'type': 'float', 'title': { 'enabled': True, 'text': 'time (ms)' } }, 'yAxis': [{ 'type': 'int', 'title': { 'enabled': True, 'text': 'EEG signal' }, 'opposite': False }, { 'type': 'int', 'title': { 'enabled': True, 'text': 'variance' }, 'opposite': True }] } charts.set_dict_options(options) for i in headers: data = df[i].tolist() mean = df[i].rolling(window=2).mean() d_var = df[i].rolling(window=2).var() d_var = d_var.dropna() mean = mean.dropna() data = [float(j) for j in data] disp = [] d_var = [float(j) for j in d_var] mean = [float(j) for j in mean] for k in range(len(mean)): disp.append(d_var[k] / mean[k]) charts.add_data_set(data, 'line', i) name = str(i) + '_disp' charts.add_data_set(disp, 'line', name, yAxis=1) charts.save_file(name)
} }, 'yAxis': { 'title': { 'text': "Value of Capital" } }, 'tooltip': { 'crosshairs': True, 'shared': True, }, 'legend': { } } H.set_dict_options(options) H.add_data_set(discrete, 'scatter', 'Discrete', color='rgba(223, 83, 83, .5)') H.add_data_set(continuous, 'line', 'Continuous', zIndex=1, marker={ 'fillColor': 'white', 'lineWidth': 2, 'lineColor': 'Highcharts.getOptions().colors[0]' }) html_str = H.htmlcontent.encode('utf-8') html_file = open("chart.html", "w") html_file.write(html_str) html_file.close()
def draw(folderName, videoName, modelName): # videoName = '6'#define your test video name here fileList = getJsonList(folderName) fileList = set(fileList) print(len(fileList)) fullData = [] initFrame = 0 while (folderName + "_" + str(initFrame + 29).zfill(12) + "_keypoints.json" in fileList): matrix = [] for i in range(30): file = folderName + "_" + str(initFrame + i).zfill(12) + "_keypoints.json" matrixLine = getJsonForOneFrame(folderName + '/' + file, folderName) matrix.append(matrixLine) fullData.append(matrix) initFrame += 30 def cleanX(raw): length = len(raw) temp = np.zeros((length, 10, 190)) for i in range(len(raw)): for j in range(10): for k in range(190): temp[i][j][k] = raw[i][j][k] print("progress:{0}%".format(round((i + 1) * 100 / len(raw))), end="\r") return temp temp = cleanX(fullData) # model1 = load_model('10/balancetalking.h5') # r1 = model1.predict(temp) # model2 = load_model('10/balancedrinking.h5') # r2 = model2.predict(temp) def ProcessSilentData(x_train, x_test): def proc(x_train_silent): length = len(x_train_silent) temp = np.zeros((length, 10, 46)) for i in range(len(x_train_silent)): for j in range(10): for k in range(190): if 146 <= k <= 185: temp[i][j][k - 146] = x_train_silent[i][j][k] temp[i][j][40] = x_train_silent[i][j][67] # face temp[i][j][41] = x_train_silent[i][j][68] temp[i][j][42] = x_train_silent[i][j][8] # left hand temp[i][j][43] = x_train_silent[i][j][9] temp[i][j][44] = x_train_silent[i][j][14] # right hand temp[i][j][45] = x_train_silent[i][j][15] return temp x_train_40 = proc(x_train) x_test_40 = proc(x_test) x_train = x_train_40 x_test = x_test_40 # define distance between two points def calDis(material, a, b): # material = x_train[0][0] ax = (a - 1) * 2 ay = ax + 1 bx = (b - 1) * 2 by = bx + 1 d1 = abs(material[ax] - material[bx]) d2 = abs(material[ay] - material[by]) dis = np.sqrt(d1 * d1 + d2 * d2) return dis def getSilent(x): # x_train[0][0] mouthLen = calDis(x, 13, 17) sideLen = calDis(x, 1, 12) + calDis(x, 1, 2) + calDis( x, 8, 7) + calDis(x, 6, 7) mouthWid = calDis(x, 14, 20) + calDis(x, 15, 19) + calDis( x, 16, 18) handLen = calDis(x, 21, 22) + calDis(x, 21, 23) if mouthLen == 0: silentWeight = 0 else: silentWeight = mouthWid / mouthLen if sideLen == 0: sideWeight = 0 handWeight = 0 else: sideWeight = mouthWid / sideLen handWeight = handLen / sideLen if sideLen == 0: return [-1, -1] else: return [sideWeight, silentWeight] def proX(x_train): pro_x_train = np.zeros((len(x_train), 10, 4)) for i in range(len(x_train)): for j in range(len(x_train[i])): var0, var1 = getSilent(x_train[i][j]) pro_x_train[i][j][0] = var0 pro_x_train[i][j][1] = var1 for i in range(len(pro_x_train)): # 10 2 varList1 = [] varList2 = [] for j in range(10): varList1.append(pro_x_train[i][j][0]) varList2.append(pro_x_train[i][j][1]) var2 = np.var(varList1) var3 = np.var(varList2) for j in range(10): pro_x_train[i][j][2] = var2 pro_x_train[i][j][3] = var3 return pro_x_train return proX(x_train), proX(x_test) # print(x_train.shape) x_train_silent, x_test = ProcessSilentData(temp, temp) x_train_eat, x_test = ProcessSilentData(temp, temp) model4 = load_model('talking.h5') # 还是两个点的silenttalking不要动摇 r4 = model4.predict(x_train_silent) model3 = load_model('confusing.h5') # eating不要只搞两个点 r3 = model3.predict(x_train_eat) # print(r4) X_TRAIN = [] for i in range(len(r4)): # X_TRAIN.append([[r1[i][0]], [r2[i][0]], [r3[i][0]], [r4[i][0]]]) X_TRAIN.append([[r4[i][0]], [r3[i][0]]]) X_TRAIN = np.array(X_TRAIN) # print(X_TRAIN.shape) model = load_model(modelName) predict = model.predict(X_TRAIN) # return predict########################### a = predict.tolist() result = [] frames = 30 for i in a: result.append([frames, i[0]]) frames += 30 # print(result) chart = Highchart() chart.set_options('chart', {'inverted': False}) options = { 'title': { 'text': 'Prediction for video ' + folderName + '.mp4' }, 'subtitle': { 'text': '1 means talking while 0 means non-talking' }, 'xAxis': { 'title': { 'text': 'Second' } }, 'yAxis': { 'title': { 'text': 'Flag' }, } } chart.set_dict_options(options) chart.add_data_set(result, series_type='line', name='prediction') newresult = [] for r in result: newresult.append([r[0] / 30, 0.5]) chart.add_data_set(newresult, series_type='line', name='talking/silent') index = [] for i in result: for j in range(30): index.append(i[1]) import cv2 video = videoName result_video = folderName + '_result.mp4' cap = cv2.VideoCapture(video) fps_video = cap.get(cv2.CAP_PROP_FPS) fourcc = cv2.VideoWriter_fourcc(*"mp4v") frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) videoWriter = cv2.VideoWriter(result_video, fourcc, fps_video, (frame_width, frame_height)) frame_id = 0 while (cap.isOpened()): ret, frame = cap.read() if ret == True: frame_id += 1 left_x_up = int(frame_width / frame_id) left_y_up = int(frame_height / frame_id) right_x_down = int(left_x_up + frame_width / 10) right_y_down = int(left_y_up + frame_height / 10) word_x = left_x_up + 5 word_y = left_y_up + 25 if frame_id >= len(index): pass else: cv2.putText(frame, 'frame_%s' % index[frame_id], (word_x, word_y), cv2.FONT_HERSHEY_SIMPLEX, 1, (55, 255, 155), 2) videoWriter.write(frame) else: videoWriter.release() break frameRate = 30 # default video frame rate for i in result: i[0] = i[0] / frameRate d = {} d['talking'] = result file_name = 'timeLabel.json' with open(file_name, 'w') as file_object: json.dump(d, file_object) file_name = '430000313.json' with open(file_name, 'w') as file_object: json.dump(d, file_object) return chart
'text': 'Highchart Bar' }, 'legend': { 'enabled': True }, 'xAxis': { 'categories': ['User 1', 'User 2', 'User 3', 'User 4', 'User 5'], }, 'yAxis': { 'title': { 'text': 'Number of posts (thousands)' } }, } # we can use our own data data1 = [107, 31, 635, 203, 2] data2 = [133, 156, 947, 408, 6] data3 = [973, 914, 4054, 732, 34] data4 = [1052, 954, 4250, 740, 38] chart.set_dict_options(options) # add options to the chart object chart.add_data_set(data1, 'bar', 'Day 1') # data, type_of_chart, legend_text chart.add_data_set(data2, 'bar', 'Day 2') chart.add_data_set(data3, 'bar', 'Day 3') chart.add_data_set(data4, 'bar', 'Day 4') chart.save_file('./highcharts-bar' ) # it will crete an html file. Used to save the html file
def create_histogram(src, dst_html, title=None, subtitle=None, x_axis_header=None, y_axis_header=None, x_axis_name=None, y_axis_name=None): """Creates basic histogram using Highcharts Args: src (string or DataFrame): Path to csv with source data or DataFrame with source data. dst_html (string): Path to destination html output title (string): Title of histogram subtitle (string): Subtitle of histogram x_axis_header (str): Name of x-axis header in csv file. Defaults to None. y_axis_header (str): Name of y-axis header in csv file. Defaults to None. x_axis_name (str): Name of x-axis to use in histogram. Defaults to None. y_axis_name (str): Name of y-axis to use in histogram. Defaults to None. Returns: None. """ # Check destination if dst_html[-5:] == '.html': dst_chart = dst_html[:-5] # Omit '.html' for exporting with Highcharts else: dst_chart = dst_html dst_html += '.html' # Import source csv into pandas DataFrame if type(src) == 'str': src_df = pd.read_csv(src) elif isinstance(src, pd.core.frame.DataFrame): src_df = src # Define headers if not given if not x_axis_header: x_axis_header = src_df.columns[0] if not y_axis_header: y_axis_header = src_df.columns[1] # Define axis names if not given if not x_axis_name: x_axis_name = x_axis_header if not y_axis_name: y_axis_name = y_axis_header # Convert source to list of lists, convert x-axis to category list for labels hist_data = [] hist_categories = [] # Explicitly define x-axis categories to workaround zoom glitch for index, row in src_df.iterrows(): hist_data.append([str(row[x_axis_header]), float(row[y_axis_header])]) hist_categories.append(str(row[x_axis_header])) # Define chart options options = { 'chart': { 'type': 'column', 'zoomType': 'x' # Causes incorrect x-axis labels if categories are not explicitly defined }, 'title': { 'text': title }, 'subtitle': { 'text': subtitle }, 'xAxis': { 'type': 'category', 'categories': hist_categories, 'title': { 'text': x_axis_name } }, 'yAxis': { 'title': { 'text': y_axis_name } }, 'tooltip': { 'shared': True, 'pointFormat': '{point.y:,.0f}' }, 'legend': { 'enabled': False }, 'plotOptions': { 'series': { 'borderWidth': 0, 'dataLabels': { 'enabled': True, 'format': '{point.y:,.0f}' } } }, } # Create chart, export, and open h = Highchart(width=1920, height=1080) h.set_dict_options(options) h.add_data_set(hist_data, 'column') h.save_file(dst_chart) webbrowser.open('file://' + dst_html)
def visualize_key_topics(self): df_path = f'{os.path.expanduser(self.save_path)}/{self.keyword}/' \ f'key_topics/key_topics.csv' df = pd.read_csv(df_path) options = { 'title': { 'text': f'Top {df.shape[0]} key topics ' f'for {self.keyword}', 'style': { 'fontSize': '20' } }, 'xAxis': { 'categories': df.topic.tolist(), 'labels': { 'style': { 'fontSize': '13px' } }, 'title': { 'text': 'Topic', 'style': { 'fontSize': '15' } } }, 'yAxis': { 'title': { 'text': 'Importance in percentage (%)', 'style': { 'fontSize': '15' } }, 'allowDecimals': False, 'labels': { 'style': { 'fontSize': '15px' }, 'format': '{value}%' }, 'min': 0, 'max': df.weight_normalized.max() }, 'plotOptions': { 'series': { 'showInLegend': False } }, 'chart': { 'backgroundColor': 'white' } } # Create chart h = Highchart(width=1000, height=700) h.set_dict_options(options) h.add_data_set(df.weight_normalized.values.tolist(), 'bar', 'Retweets count', color='#1998CB') # Save chart save_chart_path = f'{os.path.expanduser(self.save_path)}/' \ f'{self.keyword}/key_topics/key_topics.html' self.func_save_html(save_chart_path, h.htmlcontent)
def main(): """ main function read all files , make plots and process them """ ## data location, relative dir = config.training_dir ## run over all files with '*.mat' file_type = '*.mat' # plotting setup charts = Highchart() options = { 'chart': { 'type': 'line' }, 'title': { 'text': 'test' }, 'xAxis': { 'type': 'float', 'title': { 'enabled': True, 'text': 'time (ms)' } }, 'yAxis': { 'type': 'int', 'title': { 'enabled': True, 'text': 'EEG signal' } } } charts.set_dict_options(options) list_dict = [] target_list = [] # get a list of all files to be processed file_list = glob.glob(dir + file_type) for f in file_list: name = f[-9:-4] df = convert_mat(f, config.resample_size) # create lables that will eventually use in the clasification algorithm if "1.mat" in f: target = 1 else: target = 0 values_dict = {} # get summary statistics of each channel in the EGG, save them to a list of dictionaries for i in df.columns: values_dict[i + '_mean'] = df[i].mean values_dict[i + '_median'] = df[i].median values_dict[i + '_std'] = df[i].std values_dict[i + '_min'] = df[i].min values_dict[i + '_max'] = df[i].max values_dict[i + '_kurt'] = df[i].kurt values_dict[i + '_kurtosis'] = df[i].kurtosis values_dict[i + '_skew'] = df[i].skew values_dict[i + '_var'] = df[i].var # plot each channel data = df[i].tolist() data = [float(j) for j in data] charts.add_data_set(data, 'line', i) # append summary of each measurement list_dict.append(values_dict) target_list.append(target) charts.save_file(config.out_dir + name) # get final data frame summary_df = pd.DataFrame.from_records(list_dict) summary_df['target'] = pd.Series(target_list) summary_df.to_csv(config.out_dir + 'Summary_Stats_df_Training1.csv')