Python 3-How to composite two pictures by using PIL? - python-3.x

I'm trying using genetic algorithm to generate a picture which consists of triangle pictures.I use Image.blend() function,but the result is too dark and looks so bad.How could I deal with it?
image_config = {
'mode': 'RGBA',
'size': sourceimg.size,
'color': 'black'
}
im = Image.new(**image_config)
for res in self.decode(chrom):
im_tmp = Image.new(**image_config)
draw = ImageDraw.Draw(im_tmp, 'RGBA')
draw.polygon(xy=[res[0], res[1], res[2]], fill=res[3])
im = Image.blend(im, im_tmp, 0.5)

You need composite() or alpha_composite().

Related

how to switch widgets in a QSplitter

How to modify a QSplitter panels from right to left, or center, without deleting the splitter and recreate it.
I uses replaceWidget, but it is giving: QSplitter::replaceWidget: Trying to replace a widget with one of its siblings, and QSplitter::replaceWidget: Trying to replace a widget with itself
widget_1, widget_2, widget_3, widget_4 = QWidget(), QWidget(), QWidget(), QWidget()
splitter1 = QSplitter(Qt.Vertical)
splitter1.addWidget(widget_1)
splitter1.addWidget(widget_2)
splitter1.setSizes([450, 350])
splitter2 = QSplitter(Qt.Vertical)
splitter2.addWidget(widget_3)
splitter2.addWidget(widget_4)
splitter2.setSizes([550, 250])
splitter3 = QSplitter(Qt.Horizontal)
splitter3.setObjectName('align_left')
splitter3.addWidget(splitter1)
splitter3.addWidget(splitter2)
splitter3.setSizes([100, 1000])
right_panel, left_panel = splitter3.widget(0), splitter3.widget(1)
splitter3.replaceWidget(0, left_panel)
splitter3.replaceWidget(1, right_panel)
I further tried to create two QSplitter using the same instances, but seems i need to create different instances for each QSplitter
I tried also, but not working
for i in reversed(range(splitter3.count())):
panel = splitter3.widget(i)
panel.setParent(None)
if align_panel == 'left':
splitter3.addWidget(splitter1)
splitter3.addWidget(splitter2)
splitter3.setSizes([100, 1000])
elif align_panel == 'right':
splitter3.addWidget(splitter2)
splitter3.addWidget(splitter1)
splitter3.setSizes([1000, 100])

slider.value values not getting updated using ColumnDataSource(Dataframe).data

I have been working on COVID19 analysis for a dashboard and am using a JSON data source. I have converted the json to dataframe. I am working on plotting bar chart for "Days to reach deaths" over a "States" x-axis (categorical values). I am trying to use a function to update the slider.value. Upon running the bokeh serve with --log-level=DEBUG, I am getting a following error:
Can someone provide me with any direction or help with what might be causing the issue as I am new to Python and any help is appreciated? Or if there's any other alternative.
Please find the code below:
cases_summary = requests.get('https://api.rootnet.in/covid19-in/stats/history')
json_data = cases_summary.json()
#Data Cleaning
cases_summary=pd.json_normalize(json_data['data'], record_path='regional', meta='day')
cases_summary['loc']=np.where(cases_summary['loc']=='Nagaland#', 'Nagaland', cases_summary['loc'])
cases_summary['loc']=np.where(cases_summary['loc']=='Madhya Pradesh#', 'Madhya Pradesh', cases_summary['loc'])
cases_summary['loc']=np.where(cases_summary['loc']=='Jharkhand#', 'Jharkhand', cases_summary['loc'])
#Calculate cumulative days since 1st case for each state
cases_summary['day_count']=(cases_summary['day'].groupby(cases_summary['loc']).cumcount())+1
#Initial plot for default slider value=35
days_reach_death_count=cases_summary.loc[(cases_summary['deaths']>=35)].groupby(cases_summary['loc']).head(1).reset_index()
slider = Slider(start=10, end=max(cases_summary['deaths']), value=35, step=10, title="Total Deaths")
source = ColumnDataSource(data=dict(days_reach_death_count[['loc','day_count', 'deaths']]))
q = figure(x_range=days_reach_death_count['loc'], plot_width=1200, plot_height=600, sizing_mode="scale_both")
q.title.align = 'center'
q.title.text_font_size = '17px'
q.xaxis.axis_label = 'State'
q.yaxis.axis_label = 'Days since 1st Case'
q.xaxis.major_label_orientation = math.pi/2
q.vbar('loc', top='day_count', width=0.9, source=source)
deaths = slider.value
q.title.text = 'Days to reach %d Deaths' % deaths
hover = HoverTool(line_policy='next')
hover.tooltips = [('State', '#loc'),
('Days since 1st Case', '#day_count'), # #$name gives the value corresponding to the legend
('Deaths', '#deaths')
]
q.add_tools(hover)
def update(attr, old, new):
days_death_count = cases_summary.loc[(cases_summary['deaths'] >= slider.value)].groupby(cases_summary['loc']).head(1).reindex()
source.data = [ColumnDataSource().from_df(days_death_count)]
slider.on_change('value', update)
layout = row(q, slider)
tab = Panel(child=layout, title="New Confirmed Cases since Day 1")
tabs= Tabs(tabs=[tab])
curdoc().add_root(tabs)
Your code has 2 issues
(critical) source.data must be a dictionary, but you're assigning it an array
(minor) from_df is a class method, you don't have to construct an object of it
Try using source.data = ColumnDataSource.from_df(days_death_count) instead.

How can I use stamps to create a shape in turtle?

I tried to do this
def ship_merge(*ship_parts, translate_parts = {'part', (x, y)}):
merge.pu()
merge.home()
merge.begin_poly()
for part in ship_parts:
merge.home()
merge.goto(translate_parts[part])
merge.shape(part)
merge.stamp()
merge.end_poly()
merged_shape = merge.get_poly()
name = 'Ship 1'
win.register_shape(name, merged_shape)
new_ship = turtle.Turtle()
new_ship.shape(name)
return new_ship
But the 'new_ship' turtle has no shape. I think that it might be the result of the stamps not registering between 'begin_poly' and 'end_poly'. How do I fix this?

Updating multiple line plots dynamically in callback in bokeh

I have a use case where I have multiple line plots (with legends), and I need to update the line plots based on a column condition. Below is an example of two data set, based on the country, the column data source changes. But the issue I am facing is, the number of columns is not fixed for the data source, and even the types can vary. So, when I update the data source based on a callback when there is a new country selected, I get this error:
Error: attempted to retrieve property array for nonexistent field 'pay_conv_7d.content'.
I am guessing because in the new data source, the pay_conv_7d.content column doesn't exist, but in my plot those lines were already there. I have been trying to fix this issue by various means (making common columns for all country selection - adding the missing column in the data source in callback, but still get issues.
Is there any clean way to have multiple line plots updating using callback, and not do a lot of hackish way? Any insights or help would be really appreciated. Thanks much in advance! :)
def setup_multiline_plots(x_axis, y_axis, title_text, data_source, plot):
num_categories = len(data_source.data['categories'])
legends_list = list(data_source.data['categories'])
colors_list = Spectral11[0:num_categories]
# xs = [data_source.data['%s.'%x_axis].values] * num_categories
# ys = [data_source.data[('%s.%s')%(y_axis,column)] for column in data_source.data['categories']]
# data_source.data['x_series'] = xs
# data_source.data['y_series'] = ys
# plot.multi_line('x_series', 'y_series', line_color=colors_list,legend='categories', line_width=3, source=data_source)
plot_list = []
for (colr, leg, column) in zip(colors_list, legends_list, data_source.data['categories']):
xs, ys = '%s.'%x_axis, ('%s.%s')%(y_axis,column)
plot.line(xs,ys, source=data_source, color=colr, legend=leg, line_width=3, name=ys)
plot_list.append(ys)
data_source.data['plot_names'] = data_source.data.get('plot_names',[]) + plot_list
plot.title.text = title_text
def update_plot(country, timeseries_df, timeseries_source,
aggregate_df, aggregate_source, category,
plot_pay_7d, plot_r_pay_90d):
aggregate_metrics = aggregate_df.loc[aggregate_df.country == country]
aggregate_metrics = aggregate_metrics.nlargest(10, 'cost')
category_types = list(aggregate_metrics[category].unique())
timeseries_df = timeseries_df[timeseries_df[category].isin(category_types)]
timeseries_multi_line_metrics = get_multiline_column_datasource(timeseries_df, category, country)
# len_series = len(timeseries_multi_line_metrics.data['time.'])
# previous_legends = timeseries_source.data['plot_names']
# current_legends = timeseries_multi_line_metrics.data.keys()
# common_legends = list(set(previous_legends) & set(current_legends))
# additional_legends_list = list(set(previous_legends) - set(current_legends))
# for legend in additional_legends_list:
# zeros = pd.Series(np.array([0] * len_series), name=legend)
# timeseries_multi_line_metrics.add(zeros, legend)
# timeseries_multi_line_metrics.data['plot_names'] = previous_legends
timeseries_source.data = timeseries_multi_line_metrics.data
aggregate_source.data = aggregate_source.from_df(aggregate_metrics)
def get_multiline_column_datasource(df, category, country):
df_country = df[df.country == country]
df_pivoted = pd.DataFrame(df_country.pivot_table(index='time', columns=category, aggfunc=np.sum).reset_index())
df_pivoted.columns = df_pivoted.columns.to_series().str.join('.')
categories = list(set([column.split('.')[1] for column in list(df_pivoted.columns)]))[1:]
data_source = ColumnDataSource(df_pivoted)
data_source.data['categories'] = categories
Recently I had to update data on a Multiline glyph. Check my question if you want to take a look at my algorithm.
I think you can update a ColumnDataSource in three ways at least:
You can create a dataframe to instantiate a new CDS
cds = ColumnDataSource(df_pivoted)
data_source.data = cds.data
You can create a dictionary and assign it to the data attribute directly
d = {
'xs0': [[7.0, 986.0], [17.0, 6.0], [7.0, 67.0]],
'ys0': [[79.0, 69.0], [179.0, 169.0], [729.0, 69.0]],
'xs1': [[17.0, 166.0], [17.0, 116.0], [17.0, 126.0]],
'ys1': [[179.0, 169.0], [179.0, 1169.0], [1729.0, 169.0]],
'xs2': [[27.0, 276.0], [27.0, 216.0], [27.0, 226.0]],
'ys2': [[279.0, 269.0], [279.0, 2619.0], [2579.0, 2569.0]]
}
data_source.data = d
Here if you need different sizes of columns or empty columns you can fill the gaps with NaN values in order to keep column sizes. And I think this is the solution to your question:
import numpy as np
d = {
'xs0': [[7.0, 986.0], [17.0, 6.0], [7.0, 67.0]],
'ys0': [[79.0, 69.0], [179.0, 169.0], [729.0, 69.0]],
'xs1': [[17.0, 166.0], [np.nan], [np.nan]],
'ys1': [[179.0, 169.0], [np.nan], [np.nan]],
'xs2': [[np.nan], [np.nan], [np.nan]],
'ys2': [[np.nan], [np.nan], [np.nan]]
}
data_source.data = d
Or if you only need to modify a few values then you can use the method patch. Check the documentation here.
The following example shows how to patch entire column elements. In this case,
source = ColumnDataSource(data=dict(foo=[10, 20, 30], bar=[100, 200, 300]))
patches = {
'foo' : [ (slice(2), [11, 12]) ],
'bar' : [ (0, 101), (2, 301) ],
}
source.patch(patches)
After this operation, the value of the source.data will be:
dict(foo=[11, 22, 30], bar=[101, 200, 301])
NOTE: It is important to make the update in one go to avoid performance issues

How to manipulate nodes with using networkx (Python3, twitter, networkx)

I'm a beginner who try to make followers network of twitter with Python3. I made this code and got result however always one node is separated as you can see like left bottom node.
I have no idea what is happening. In addition, I also want to see smaller world for instance, how can I pick up only 5 friends and elucidate their connection ?? This my result is too huge... It would be greatly appreciated if you could explain the details. Here is my code :
api = twitter.Api(consumer_key = my_consumer_key,
consumer_secret = my_consumer_secret,
access_token_key = my_access_token_key,
access_token_secret = my_access_token_secret,
input_encoding = "UTF-8",
sleep_on_rate_limit=True)
friends = api.GetFriends()
G = networkx.Graph()
for friend in friends:
G.add_edge(myname,friend.screen_name)
for friend in friends[-3:]:
for user in api.GetFriends(friend.id):
if user in friends:
G.add_edge(friend.screen_name,user.screen_name)
pos = spring_layout(G)
draw_networkx_nodes(G, pos, node_size = 100, node_color = 'w')
draw_networkx_edges(G, pos, width = 1)
draw_networkx_labels(G, pos, font_size = 12, font_family = 'sans-
serif', font_color = 'r')
xticks([])
yticks([])
savefig("egonetwork.png")
show()

Resources