DEV Community

htraprocks
htraprocks

Posted on

Help me with Threading in this code. I am unable to figure out how to use threading in this

def data_import_date(start_date,end_date):
end_date1=end_date.strftime('%Y-%m-%dT%H:%M:%S')
start_date=pd.Timestamp(start_date)
end_date=pd.Timestamp(end_date1)

button=widgets.Button(description='Pull Data')
button.on_click(functools.partial(select_data,rs_=[start_date,end_date]))

vbox=widgets.VBox([button])
display(vbox,out)

def select_data(b,rs_):

clear_output()

start_date=rs_[0]
end_date1=rs_[1]
print("Data pulling started")

with out:
    clear_output()
    seeq_login() 
    [item1,item2]=query_seeq_for_data()
    i=0
    while True:
        end_date=end_date1.strftime('%Y-%m-%dT%H:%M:%S')
        print("Start & End date: ",start_date,end_date)
        if i==0:
            [X_data,Y_data]=pull_data(item1,item2,start_date,end_date)
        else:
            [X_data_live,Y_data_live]=pull_data(item1,item2,start_date,end_date)
            X_data=X_data.append(X_data_live)
            Y_data=Y_data.append(Y_data_live)
        print("Data pulling completed.\nNow you're ready for your analysis")

        clear_output()
        [X_train,X_test,Y_train,Y_test]=train_test(X_data,Y_data)

        [Xp_train,components,explained_variance_ratio,_,_]=apply_PCA(X_train,X_test)
        plot_PC_variance(X_data,explained_variance_ratio)
        plot_PC(X_data,components)

        time.sleep(20)
        start_date=end_date
        end_date1=end_date1+datetime.timedelta(days=1)
        i+=1

a=interact(data_import_date,
start_date=widgets.DatePicker(value=pd.to_datetime(start_date)),
end_date=widgets.DatePicker(value=pd.to_datetime(end_date)))

def plot_PC(X_data,components):
Np_comp=(1,len(X_data.columns),1)
@interact
def principal_components(PC1=Np_comp,PC2=Np_comp):
fig,ax=plt.subplots(1,1,figsize=(10,10))
plt.figure(5)
print(PC1, PC2)

    ax.set_xlabel("Principal Component {}".format(PC1), fontsize=14)
    ax.set_ylabel("Principal Component {}".format(PC2), fontsize=14)
    ax.set_title("Principal components {0} & {1}".format(PC1,PC2), fontsize=(20))
    ax.scatter(components[:,PC1],components[:,PC2])

Top comments (1)

Collapse
 
htraprocks profile image
htraprocks

I am sorry, if something went wrong with publishing the code.(This was my first time).

1)This is the code snippet that I am trying to run.The main idea is that, I want the ipywidgets to be interactive, while the data is constantly being fetched from the data source. And the data is being updated at certain interval using the while loop.The objective is to plot the principal components interactively by changing the number of principal components(PC) to be considered, using the @interact function. Also it runs perfectly fine without while loop, that is, when we are not considering the auto-update for the dataset, with the while loop. But when I include the while loop, it doesn't handle the interactive-ness of the widgets(that is, the interaction of the number of PC). My feeling is that "while True" loop does not let the interaction of ipywidget to happen, due to some execution issue.

2) Also I looked into threading but i am unsure of how to use the fucntion which is in functools(select_data), be called using threading.Thread.

Any sort of help would be appreciated. Thanks