Threading and Matplotlib - python

I have created an application which basically generate graphs and displays them. However due to some processes taking a longer time i had created a loading bar which requires the use of threading.
Everytime i run the application the first time, the subsequent time displaying a heatgraph causes "RuntimeError: main thread is not in main loop"
Without the use of threading, it would work perfectly fine. I would like to thread and still continuously use graphs.
Sorry for the messy codes.
class App(customtkinter.CTk):
def generate_heatmap(self):
print('Generate HeatMap')
brightness = []
motion = []
for k in figs_b.keys():
l = figs_b[k][1]
brightness.append(sum(l)/len(l))
for s in figs_m.keys():
print(s)
l = figs_m[s][1]
motion.append(max(l))
data = pd.read_csv(filedialog.askopenfilename())
print(data)
data.insert(2,'Color',avg_c)
data.insert(3,'Motion',motion)
data.insert(4,'Brightness',brightness)
sns.heatmap(data.corr(), annot=True, cmap='viridis', vmin=-1, vmax=1)
plt.show()
def loading(self):
if(self.t.is_alive()):
print('EXECUTE LOAD')
self.main_frame.grid_forget()
self.loading_frame.grid(row=0,column=1,rowspan=4,columnspan=4,sticky="nsew")
self.loading_frame.grid_propagate(False)
self.loading_lbl.pack_propagate(False)
extract_image_from_gif()
self.play_gif()
self.update()
print('RESET LOAD')
self.loading_frame.grid_forget()
self.main_frame.grid(row=0,column=1,rowspan=4,columnspan=4,sticky="nsew")
self.update
def play_gif(self):
global countx, cur_img
try:
print('in')
countx += 2
cur_img = customtkinter.CTkImage(lst_image[countx],size=(800,200))
self.loading_lbl.configure(image = cur_img)
self.update()
self.after(gif_duration,self.play_gif)
except Exception as e:
#print(e)
if(self.t.is_alive()):
countx=0
self.after(gif_duration, self.play_gif)
else:
return
def openfile(self,force=''):
name = force
decision = ''
if force == '':
decision = filedialog.askopenfilename().split('/')
name = decision[len(decision)-1].split('.')[0]
if name == '':
return
self.btn_list.append(customtkinter.CTkButton(self.btn_frame,text=name,command=lambda: self.change_analysis(name)))
for x,btn in enumerate(self.btn_list):
btn.grid(row=x+1,column=0,padx=10,pady=10)
print('*'*20)
self.t = th.Thread(target=self.getGraph, args=(decision,name,))
self.t.start()
self.loading()
self.t.join()
self.change_analysis(name)
print('*'*20)
def getGraph(self,decision,name):
if os.path.exists(f'Frames/{name}') and decision != '':
return
elif(decision !=''):
frame_extraction('\\'.join(decision),name)
red = []
green = []
blue = []
files = os.listdir(f'Frames/{name}')
sorted_files = [file.replace('f_','') for file in files]
sorted_files = sorted(sorted_files, key=lambda x: int(os.path.splitext(x)[0]))
for frame in sorted_files:
rgb = color_extraction(f'Frames/{name}/f_{frame}')
r,g,b = rgb[0]
red.append(r)
green.append(g)
blue.append(b)
#print(f'Frame: {frame} RGB:{rgb[0]}')
frames = [(int(os.path.splitext(x)[0]))*20 for x in sorted_files]
figs_c[name] = generate_graph(red,green,blue,frames)
if(decision != ''):
figs_m[name] = motion_analysis(name)
figs_b[name] = brightness_graph(name)
print(figs_b[name])
self.save_data()
avg_r = round(sum(red) / len(red))
avg_g = round(sum(green) / len(green))
avg_b = round(sum(blue) / len(blue))
avg_c.append(rgbtoint32([avg_r,avg_g,avg_b])

Related

How to change the color of variables/funktions in Pycharm

I'm currently working on a project to download a bunch of hentai using api requests. The code works, but whenever i found a bug, I always had to use the search function to see where variables or funktions i created are located in the code. Then I had the idea, to change the color of the funktions, so that if i wanted to look for the funktion Sample_funktion i would just have to look for the color red.
from time import sleep
from keyboard import is_pressed
import random
import win32clipboard
# SETTINGS
pyautogui.PAUSE = 0.5
reloadtime = 1
s2tv = 1
nothing = 1786, 327
filedownload = "720p.png"
# VARIABLES
captchas_destroyed = 0
fucked_by_captchas = 0
data = None
def get_URL():
global data
pyautogui.hotkey('ctrl', 'l')
pyautogui.hotkey('ctrl', 'c')
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
def p720_checker():
p720 = pyautogui.locateCenterOnScreen(filedownload, grayscale=True, confidence=0.95)
if p720 is not None:
pyautogui.click(p720)
def check4ads():
ads = pyautogui.locateCenterOnScreen('close_ad.png', grayscale=True, confidence=0.95)
if ads is not None:
pyautogui.click(ads)
def yellow_download():
yellow_download_kink = pyautogui.locateCenterOnScreen('getdownloadlinks.png', grayscale=True, confidence=0.9)
if yellow_download_kink is not None:
pyautogui.click(yellow_download_kink)
def cloud_download_checker():
download_cloud = pyautogui.locateCenterOnScreen("gray_cloud.png", grayscale=True, confidence=0.9)
if download_cloud is not None:
pyautogui.click(download_cloud)
def randomclick(box):
x_click = int(random.uniform(box.left, box.left + box.width))
y_click = int(random.uniform(box.top, box.top + box.height))
pyautogui.moveTo(x_click, y_click, duration=random.uniform(0.1, 0.2))
sleep(random.uniform(0.2, 0.3))
pyautogui.click()
sleep(random.uniform(1, 2))
def moverandomasf():
for hot_single_moms in range(random.randint(2, 6)):
x_click = random.randint(1, 1080)
y_click = random.randint(1, 1080)
pyautogui.moveTo(x=x_click, y=y_click, duration=random.uniform(0.2, 0.5))
def reload():
pyautogui.hotkey('ctrl', 'r')
sleep(reloadtime)
def s2t():
sleep(s2tv)
def andere_tabs_schließen():
pyautogui.hotkey('alt', 'o')
sleep(s2tv)
def captcha_checka():
global captchas_destroyed, fucked_by_captchas
captchabox = pyautogui.locateOnScreen("Roboterfeld.png", grayscale=False, confidence=0.9)
if captchabox is not None:
moverandomasf()
randomclick(captchabox)
if pyautogui.locateCenterOnScreen("imnotarobotiswear.png", grayscale=True, confidence=0.9):
print("I DESTROYED DA CAPTCHA BADABABOOM")
captchas_destroyed += 1
elif pyautogui.locateCenterOnScreen("fucked_by_captcha.png", grayscale=True, confidence=0.9):
print("I just got fucked by the captcha... rest in pieces")
reload()
fucked_by_captchas += 1
captcha_checka()
else:
print("No captcha that I can destroy!")
while True:
if is_pressed("ü"):
s2t()
while not is_pressed("ü"):
check4ads()
pyautogui.scroll(-120)
gay_heart = pyautogui.locateCenterOnScreen("gray_heart.png", grayscale=False, confidence=0.9)
if gay_heart is not None:
pyautogui.click(gay_heart)
cloud_download_checker()
s2t()
check4ads()
captcha_checka()
yellow_download()
s2t()
p720_checker()
Sample of my code
Is this possible in pycharm 2020.3.3?
Kind regards, Alupy
As far as I understand, Pycharm does not currently support separate colors for specific functions. It does support using a single color for ALL function definitions and another single color for ALL function calls.
I would start becoming familiar with the actions 'Find Usage', 'Find Usages', 'Find Usages in File', and 'Show Usages'. These will all help you locate where a function is being used.

MyHDL Signals inside functions not showing up in VCD

Should I be able to see these in the generated VCD file?
#always(clk.posedge)
def MentorCluster():
j = Signal(0)
mentorq0, mentorq1, mentorq2, mentorq3 = [[Signal(0) for j in range(10)] for i in range(4)]
I can see all the signals I created at the top level, but not the ones local to the function
Here is the code I used to generate the VCD:
def simulate(timesteps):
traceSignals.timescale = "1ps"
tb = traceSignals(avaca)
sim = Simulation(tb)
sim.run(timesteps)
sim.quit()
#simulate for 2000 ticks (picoseconds) -- very ambitious to do all this in 2ns!
simulate(2000)
Signals created inside an `always’ will not only not show up in the .vcd, but also won’t work.
Here is a small test program to try this.
'''
delay, StopSimulation)
#block
def f1(clk, sigin, sigout):
# this is the place to declare Signals and ListOfSignals
sigind1 = Signal(intbv(0)[4:])
mentorq0, mentorq1, mentorq2, mentorq3 = [[Signal(bool(0)) for j in range(10)] for i in range(4)]
#always_seq(clk.posedge, reset=None)
def f1s():
# declaring Signals and ListOfSignals won't work
# sigind1 = Signal(intbv(0)[4:])
# mentorq0, mentorq1, mentorq2, mentorq3 = [[Signal(bool(0)) for j in range(10)] for i in range(4)]
sigind1.next = sigin
mentorq0[0].next = sigind1[0]
mentorq1[0].next = sigind1[1]
mentorq2[0].next = sigind1[2]
mentorq3[0].next = sigind1[3]
for i in range(1, 10):
mentorq0[i].next = mentorq0[i - 1]
mentorq1[i].next = mentorq1[i - 1]
mentorq2[i].next = mentorq2[i - 1]
mentorq3[i].next = mentorq3[i - 1]
sigout.next[0] = mentorq0[9]
sigout.next[1] = mentorq1[9]
sigout.next[2] = mentorq2[9]
sigout.next[3] = mentorq3[9]
return f1s
if __name__ == '__main__':
import random
random.seed = 'We want repeatable randomness'
#block
def tb_f1():
clk = Signal(bool(0))
sigin = Signal(intbv(0)[4:])
sigout = Signal(intbv(0)[4:])
tCK = 10
dut = f1(clk, sigin, sigout)
#instance
def genclk():
while True:
clk.next = not clk
yield delay(int(tCK // 2))
#instance
def stimulus():
yield delay(int(tCK * 3.5))
for __ in range(10):
sigin.next = random.randint(1, 15)
yield delay(tCK)
yield delay(tCK * 20)
raise StopSimulation
return instances()
# finally
dft = tb_f1()
dft.config_sim(trace=True)
dft.run_sim()
When we activate the Signals inside the always block sigout will remain 0

Python multiprocessing don't wait all elements done

I have the following code
global total_pds
total_pds = []
ksplit = wr.s3.list_objects(pred_path)
ksplit = list(ksplit)
def process(x):
dk = wr.s3.read_parquet(path = pred_path+x,dataset=False)
return dk
def log_result(result):
print(len(total_pds), end = ' ')
total_pds.append(result)
def error_back(error):
print('error', error)
pool = mp.Pool(processes=4,maxtasksperchild=10)
dcms_info = [pool.apply_async(process, args=(spl,), callback = log_result, error_callback = error_back) for spl in ksplit]
for x in dcms_info:
x.wait()
pool.close()
pool.join()
dataset = pd.concat(total_pds, ignore_index=True)
the last element throw me this error:
error("'i' format requires -2147483648 <= number <= 2147483647"
Thank you

Python multiprocessing not running

I tried to use multiprocessing on this for loop:
def segment_features(segment_pixels):
features = []
npixels, nbands = segment_pixels.shape
for b in range(nbands):
stats = scipy.stats.describe(segment_pixels[:, b])
band_stats = list(stats.minmax) + list(stats)[2:]
if npixels == 1:
# in this case the variance = nan, change it 0.0
band_stats[3] = 0.0
features += band_stats
return features
segment_ids = np.unique(segments)
objects = []
object_ids = []
for id in segment_ids:
segment_pixels = img[segments == id]
object_features = segment_features(segment_pixels)
objects.append(object_features)
object_ids.append(id)
By replacing the for loop section with this:
def segment_features_calc(segment_pixels):
segment_pixels = img[segments == id]
object_features = segment_features(segment_pixels)
objects.append(object_features)
object_ids.append(id)
print("segment "+str(id)+" features calculated")
n=mp.cpu_count()
if__name__ == '__main__':
p = mp.Pool(processes = n)
start = time.time()
async_result = p.map_async(segment_features_calc,list(segment_ids))
p.close()
p.join()
print("Complete")
end = time.time()
print('total time (s)= ' + str(end-start))
However, the multiprocessing does not get executed properly (multiprocessing stops after 0.17 seconds whereas the items to loop are close to 270,000+ segment IDs). Any insights on how to solve the issue?

Matplotlib saving plot preferrences without the data

Currently I am looking at saving a matplotlib plot of unknown state as it is user facing. The way that I am intending to do this is by taking the figure and putting the preferences into a dictionary. However this can be done by saving any base type into the dictionary and then utilizing the json library to save and load this dictionary to file.
The overall aim is to have OS independence and cross matplotlib version compatibility.
I've prototyped saving the preferences/settings of the plot to dictionary:
import numpy as np
from matplotlib import ticker
import matplotlib.pyplot as plt
import matplotlib.colors
import json
def get_dict_from_fig(fig):
fig_dict = {}
axes_list = []
for ax in fig.axes:
axes_list.append(get_dict_for_axes(ax))
fig_dict["Axes"] = axes_list
fig_dict["Properties"] = get_dict_from_fig_properties(fig)
return fig_dict
def get_dict_for_axes(ax):
ax_dict = {}
# Get the axis properties
ax_dict["Properties"] = get_dict_from_axes_properties(ax)
# Get lines from the axes and store it's data
lines_list = []
for index, line in enumerate(ax.lines):
lines_list.append(get_dict_from_line(line, index))
ax_dict["Lines"] = lines_list
texts_list = []
for text in ax.texts:
texts_list.append(get_dict_from_text(text))
ax_dict["Texts"] = texts_list
ax_dict["Title"] = get_dict_from_text(ax.title)
ax_dict["XAxis Title"] = get_dict_from_text(ax.xaxis.label)
ax_dict["YAxis Title"] = get_dict_from_text(ax.yaxis.label)
# Potentially need to handle artists that are Text
artist_text_dict = {}
for artist in ax.artists:
if isinstance(artist, matplotlib.text.Text):
artist_text_dict = get_dict_from_text(artist)
ax_dict["Text from artists"] = artist_text_dict
legend_dict = {}
legend = ax.get_legend()
if legend is not None and legend.get_visible():
legend_dict = get_dict_from_legend(legend)
legend_dict["Visible"] = True
ax_dict["Legend"] = legend_dict
return ax_dict
def get_dict_from_axes_properties(ax):
prop_dict = {}
prop_dict["Bounds"] = ax.get_position().bounds
prop_dict["Dynamic"] = ax.get_navigate()
prop_dict["Axison"] = ax.axison
prop_dict["Frame On"] = ax.get_frame_on()
prop_dict["XAxis Properties"] = get_dict_from_axis_properties(ax.xaxis)
prop_dict["YAxis Properties"] = get_dict_from_axis_properties(ax.yaxis)
# XAxis scale and Xlim
prop_dict["XAxis Scale"] = ax.xaxis.get_scale()
prop_dict["XLim"] = ax.get_xlim()
# YAxis scale and Ylim
prop_dict["YAxis Scale"] = ax.xaxis.get_scale()
prop_dict["YLim"] = ax.get_ylim()
return prop_dict
def get_dict_from_axis_properties(ax):
prop_dict = {}
label1On = ax._major_tick_kw.get('label1On', True)
if isinstance(ax, matplotlib.axis.XAxis):
if label1On:
prop_dict["Position"] = "Bottom"
else:
prop_dict["Position"] = "Top"
elif isinstance(ax, matplotlib.axis.YAxis):
if label1On:
prop_dict["Position"] = "Left"
else:
prop_dict["Position"] = "Right"
else:
raise ValueError("Value passed is not a valid axis")
prop_dict["nTicks"] = len(ax.get_major_locator()())
if isinstance(ax.get_major_locator(), ticker.FixedLocator):
prop_dict["Tick Values"] = list(ax.get_major_locator())
else:
prop_dict["Tick Values"] = None
formatter = ax.get_major_formatter()
if isinstance(formatter, ticker.FixedFormatter):
prop_dict["Tick Format"] = list(formatter.seq)
else:
prop_dict["Tick Format"] = ""
labels = ax.get_ticklabels()
if labels:
prop_dict["Font size"] = labels[0].get_fontsize()
else:
prop_dict["Font size"] = ""
prop_dict["Scale"] = ax.get_scale()
prop_dict["Grid Style"] = get_dict_for_grid_style(ax)
prop_dict["Visible"] = ax.get_visible()
return prop_dict
def get_dict_for_grid_style(ax):
grid_style = {}
gridlines = ax.get_gridlines()
if ax._gridOnMajor and len(gridlines) > 0:
grid_style["Color"] = matplotlib.colors.to_hex(gridlines[0].get_color())
grid_style["Alpha"] = gridlines[0].get_alpha()
grid_style["Grid On"] = True
else:
grid_style["Grid On"] = False
return grid_style
def get_dict_from_line(line, index=0):
line_dict = {}
line_dict["Line Index"] = index
line_dict["Label"] = line.get_label()
line_dict["Alpha"] = line.get_alpha()
if line_dict["Alpha"] is None:
line_dict["Alpha"] = 1
line_dict["Color"] = matplotlib.colors.to_hex(line.get_color())
line_dict["Linewidth"] = line.get_linewidth()
line_dict["Line Style"] = line.get_linestyle()
line_dict["Marker Style"] = get_dict_from_marker_style(line)
return line_dict
def get_dict_from_marker_style(line):
style_dict = {}
style_dict["Face Color"] = matplotlib.colors.to_hex(line.get_markerfacecolor())
style_dict["Edge Color"] = matplotlib.colors.to_hex(line.get_markeredgecolor())
style_dict["Edge Width"] = line.get_markeredgewidth()
style_dict["Marker Type"] = line.get_marker()
style_dict["Marker Size"] = line.get_markersize()
style_dict["ZOrder"] = line.get_zorder()
return style_dict
def get_dict_from_text(text):
text_dict = {}
text_dict["Text"] = text.get_text()
if text_dict["Text"]:
text_dict["Transform"] = text.get_transform()
text_dict["Position"] = text.get_position()
text_dict["Style"] = get_dict_from_text_style(text)
return text_dict
def get_dict_from_text_style(text):
style_dict = {}
style_dict["Alpha"] = text.get_alpha()
if style_dict["Alpha"] is None:
style_dict["Alpha"] = 1
style_dict["Text Size"] = text.get_size()
style_dict["Color"] = matplotlib.colors.to_hex(text.get_color())
style_dict["hAlign"] = text.get_horizontalalignment()
style_dict["vAlign"] = text.get_verticalalignment()
style_dict["mAlign"] = text._multialignment
style_dict["Rotation"] = text.get_rotation()
style_dict["ZOrder"] = text.get_zorder()
return style_dict
def get_dict_from_legend(legend):
legend_dict = {}
legend_elements_list = get_list_of_legend_children(legend)
legend_elements_list.append(legend.legendPatch)
text_list = []
line_list = []
for child in legend_elements_list:
try:
if isinstance(child, matplotlib.text.Text):
if child.get_text() != None:
text_list.append(get_dict_from_text(child))
if isinstance(child, matplotlib.lines.Line2D):
line_list.append(get_dict_from_line(child))
except NotImplementedError:
# Basically do nothing
pass
legend_dict["Text"] = text_list
legend_dict["Line List"] = line_list
return legend_dict
def get_list_of_legend_children(legend):
legend_list = []
if hasattr(legend, 'get_children') and len(legend.get_children()) > 0:
for child in legend.get_children():
legend_list.append(get_list_of_legend_children(child))
else:
legend_list.append(legend)
return legend_list
def get_dict_from_fig_properties(fig):
fig_dict = {}
fig_dict["Fig width"] = fig.get_figwidth()
fig_dict["Fig height"] = fig.get_figheight()
fig_dict["dpi"] = fig.dpi
return fig_dict
XVals = np.array([1, 2, 3])
YVals = np.array([1, 2, 3])
plt.plot(XVals, YVals)
dictionary = get_dict_from_fig(plt.gcf())
f = open("./savefile.json", "w+")
f.write(json.dumps(dictionary, indent=4))
I was wondering if there was any way to do this already, perhaps with a maintained library? I've tried to find something to do it and nothing that I could find was overly useful, besides inspiration. I have already used mpld3 for inspiration.
I should probably have mentioned this earlier but when saving it is key to load back the data that has been saved, else there would be little point in saving it.

Categories