trait pickling with configure_traits - enthought

I use the first example code from traitsui documentation:
from traits.api import HasTraits, Str, Int
import traitsui
class SimpleEmployee(HasTraits):
first_name = Str
last_name = Str
department = Str
employee_number = Str
salary = Int
sam = SimpleEmployee()
sam.configure_traits(filename='/Volumes/FAT/Python/Tests/test.dat')
The only change is a filename specification in configure_traits().
1 - When the file is nonexistent, a new file is not created. It would be nice to have such a behavior.
2 - How to create this file? Using
import pickle
pickle.dump( sam, open( '/Volumes/FAT/Python/Tests/test.dat', "wb" ) )
creates the file
ctraits.traits
__newobj__
p0
(c__main__
SimpleEmployee
p1
tp2
Rp3
(dp4
S'salary'
p5
I0
sS'__traits_version__'
p6
S'4.5.0'
p7
sS'first_name'
p8
S''
p9
sS'last_name'
p10
g9
sS'employee_number'
p11
g9
sS'department'
p12
VManagement
p13
sb.
with 'Management' as the department (I entered this value in the dialog, of course).
But running again
sam.configure_traits(filename='/Volumes/FAT/Python/Tests/test.dat')
changing the department field and clicking OK to close the dialog does not change the file content (as it is supposed to do from the doc).
(MacOS X)

I found what was going wrong: configure_traits() should be made to run in modal mode:
from traits.api import HasTraits, Str, Int
from traitsui.api import View, OKCancelButtons
class SimpleEmployee(HasTraits):
first_name = Str
last_name = Str
department = Str
employee_number = Str
salary = Int
sam = SimpleEmployee()
sam.configure_traits(filename='/Volumes/FAT/Python/Tests/test.dat',kind='modal')
now works OK.
As for my question #1, yes, a new file is created if necessary

Related

Updating a Dash Callback using RadioItems

I am fairly new to python coding so I apologize in advance for my ignorance. I am trying to create a Dash App that drops outliers using standard deviation. The user selects a standard deviation using RadioItem inputs.
My question is what amendments do I need to make to my code so that the RadioItem value updates max_deviations using a callback?
Import packages, clean the data and define a query
import dash
import plotly.express as px
from dash import Dash, dcc, html, Input, Output, State
import pandas as pd
import numpy as np
app = dash.Dash(__name__)
server = app.server
df=pd.read_csv(r'C:\SVS_GIS\POWER BI\CSV_DATA\QSAS2021.csv', encoding='unicode_escape')
#SET DATE OF VALUATION
df['TIME'] = ((pd.to_datetime(df['Sale Date'], dayfirst=True)
.rsub(pd.to_datetime('01/10/2021', dayfirst=True))
.dt.days
)*-1)
df=df[df['TIME'] >= -365]
df = df.query("(SMA >=1 and SMA <= 3) and (LGA==60)")
prepare dataframe for dropping outliers
data = pd.DataFrame(data=df)
x = df.TIME
y = df.CHANGE
mean = np.mean(y)
standard_deviation = np.std(y)
distance_from_mean = abs(y - mean)
app layout
app.layout = html.Div([
html.Label("Standard Deviation Picker:", style={'fontSize':25, 'textAlign':'center'}),
html.Br(),
html.Label("1.0 = 68%, 2.0 = 95%, 3.0 = 99.7%", style={'fontSize':15,
'textAlign':'center'}),
html.Div(id="radio_items"),
dcc.RadioItems(
options=[{'label': i, 'value': i} for i in [1.0, 2.0, 3.0]],
value=2.0
),
html.Div([
dcc.Graph(id="the_graph")]
)])
callback
#app.callback(
Output("the_graph", "figure"),
Input("radio_items", 'value')
)
def update_graph(max_deviations):
not_outlier = distance_from_mean < max_deviations * standard_deviation
no_outliers = y[not_outlier]
trim_outliers = pd.DataFrame(data=no_outliers)
dff = pd.merge(trim_outliers, df, left_index=True, right_index=True)
return (dff)
fig = px.scatter(dff, x='TIME', y='CHANGE_y',
color ='SMA',
trendline='ols',
size='PV',
height=500,
width=800,
hover_name='SMA',
)
return dcc.Graph(id='the_graph', figure=fig)
if __name__ == '__main__':
app.run_server(debug=False)
Your dcc.RadioItems doesn't have an id prop. Add that, and make sure it matches the ID given in the callback, and you should be good.

Pyspark automatically rename repeated columns

I want to automatically rename repeated columns of a df. For example:
df
Out[4]: DataFrame[norep1: string, num1: string, num1: bigint, norep2: bigint, num1: bigint, norep3: bigint]
Apply some function to end with a df like:
f_rename_repcol(df)
Out[4]: DataFrame[norep1: string, num1_1: string, num1_2: bigint, norep2: bigint, num1_3: bigint, norep3: bigint]
I've already create my own function, and works, but I'm sure there is a shorter and better way of doing it:
def f_df_col_renombra_rep(df):
from collections import Counter
from itertools import chain
import pandas as pd
columnas_original = np.array(df.columns)
d1 = Counter(df.columns)
i_corrige = [a>1 for a in dict(d1.items()).values()]
var_corrige = np.array(dict(d1.items()).keys())[i_corrige]
var_corrige_2 = [a for a in columnas_original if a in var_corrige]
columnas_nuevas = []
for var in var_corrige:
aux_corr = [a for a in var_corrige_2 if a in var]
i=0
columnas_nuevas_aux=[]
for valor in aux_corr:
i+=1
nombre_nuevo = valor +"_"+ str(i)
columnas_nuevas_aux.append(nombre_nuevo)
columnas_nuevas.append(columnas_nuevas_aux)
columnas_nuevas=list(chain.from_iterable(columnas_nuevas))
indice_cambio = pd.Series(columnas_original).isin(var_corrige)
i = 0
j = 0
colsalida = [None]*len(df.columns)
for col in df.columns:
if indice_cambio[i] == True:
colsalida[i] = columnas_nuevas[j]
j += 1
else:
colsalida[i] = col
# no cambio el nombre
i += 1
df_out = df.toDF(*(colsalida))
return df_out
You can modify the renaming function here to suit your need, but broadly I find this as the best way to rename all the duplicates columns
old_col=df.schema.names
running_list=[]
new_col=[]
i=0
for column in old_col:
if(column in running_list):
new_col.append(column+"_"+str(i))
i=i+1
else:
new_col.append(column)
running_list.append(column)
print(new_col)
This the conversion I do, the suffix assigned to the duplicate columns is not that of difference until the name(prefix) remains the same & I can save the file.
To update the columns you can simply run:
df=df.toDF(*new_col)
This should update the column names and remove all the duplicates
If you want to keep the numbering as _1,_2,_3:
You can use a dictionary and try and except block,
dict={}
for column in old_col:
try:
i=dict[column]+1
new_col.append(column+"_"+str(i))
dict[column]=i
except:
dict[column]=1
new_col.append(column+"_"+str(1)
print(new_col)
the easy way I am doing it is:
def col_duplicates(self):
'''rename dataframe with dups'''
columnas = self.columns.copy()
for i in range(len(columnas)-1):
for j in range(i+1, len(columnas), 1):
if columnas[i] == columnas[j]:
columnas[j] = columnas[i] + '_dup_' + str(j) # this line controls how to rename
return self.toDF(*columnas)
use as:
new_df_without_duplicates = col_duplicates(df_with_duplicates)

How to get the same initial results if seed is provided , without restarting the Ipython kernel in Tensorflow

I am not sure , whether this question follow any logic as per the design of Tensorflow . Here is the Code
import numpy as np
import tensorflow as tf
np.random.seed(0)
tf.set_random_seed(0)
class Sample():
def __init__(self, hidden_dim = 50 , input_dim = 784):
self.hidden_dim = hidden_dim
self.input_dim = input_dim
self.x = tf.placeholder(tf.float32, [None, self.input_dim])
self._create_network()
self.__minimize()
self.sess = tf.InteractiveSession()
init = tf.initialize_all_variables()
self.sess.run(init)
def _create_network(self):
self.W1 = tf.Variable(tf.random_normal([self.input_dim, self.hidden_dim]))
self.W2 = tf.Variable(tf.random_normal([self.hidden_dim, self.input_dim]))
def __minimize(self):
h1 = tf.matmul(self.x , self.W1)
h2 = tf.matmul(h1, self.W2)
reconstruction = tf.nn.sigmoid(h2)
self.loss = tf.reduce_mean(tf.squared_difference(self.x , reconstruction))
self.optimizer = \
tf.train.AdamOptimizer(learning_rate=0.01).minimize(self.loss)
def partial_fit(self, X):
cost , _ = self.sess.run([self.loss, self.optimizer] , feed_dict = {self.x: X})
return cost
import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
n_samples = mnist.train.num_examples
ex_1 = mnist.train.next_batch(1)[0]
model = Sample()
for i in xrange(11):
c = model.partial_fit(ex_1)
print c
The result is as follows :
0.498799
0.469001
0.449659
0.436665
0.424995
0.414473
0.404129
0.394458
0.39165
0.38483
0.380042
This result is achieved with seed 0 and it is same when I restart the kernel . But suppose , if I ran 10 iteration and then , if I have to start it from the scratch , how will i do it in Ipython . Because , if run after 10 or so iterations , the model continues to start from the remaining values .
I used tf.reset_default_graph() , but that has not make any change to the behavior .
Don't use an InterativeSession but use a normal Session.
Create a new Session each time with the same seed and you will get the same results.
graph = tf.Graph()
with graph.as_default():
model = Sample()
with Session(graph=graph) as sess:
np.random.seed(0)
tf.set_random_seed(0)
for i in xrange(11):
c = model.partial_fit(ex_1)
print c

Suppress output from an interact() widget

I am using a widget to parametrise the generation of some data. I would like to capture the data, without outputting it. Is this possible? Adding ; after interact() doesn't work. Example:
import ipywidgets as widgets
from ipywidgets import interact, interactive, fixed
def generate_data(n, p, s):
return np.random.negative_binomial(n, p, s)
w_n = widgets.IntSlider(min=1, max=10000, step=1)
w_p = widgets.FloatSlider(min=0.01, max=1, step = 0.01)
w_s = widgets.IntSlider(min=500,max=10000,step=50)
data = interact(generate_data, n = w_n, p = w_p, s = w_s);
Instead of returning a value in generate_data(), you could modify a global variable. It avoids printing the output, and you can use val later in your code.
import ipywidgets as widgets
from ipywidgets import interact, interactive, fixed
import numpy as np
val = None
def generate_data(n, p, s):
global val
val = np.random.negative_binomial(n, p, s)
w_n = widgets.IntSlider(min=1, max=10000, step=1)
w_p = widgets.FloatSlider(min=0.01, max=1, step = 0.01)
w_s = widgets.IntSlider(min=500,max=10000,step=50)
interact(generate_data, n = w_n, p = w_p, s = w_s)
I have submitted a pull request to ipywidgets that addresses this problem:
https://github.com/ipython/ipywidgets/pull/712
The changes are actually very simple. With this version, you would simply pass an extra kwarg to interact:
data = interact(generate_data, n = w_n, p = w_p, s = w_s,
__output_result=False)

Matlab name assignment

I am trying to decrease some big chucks of matlab code i had from a while ago, and was hoping to get them a bit more "clean".
The VarName2,VarName3,VarName4 ...etc are provide by measured data and i will know what they are always going to be thus i gave me the name A,B ,C , the think i want changed though is the first part of the name, so every time i run the .m file I will use the input('') option
where as fname = 'SWAN' and A, B , C are the second part of the name and they are constant.
fname = input ('enter name')
fname_A = VarName2
fname_B = VarName3
fname_C = VarName4
and want to be getting
SWAN_A = VarName2
SWAN_B = VarName3
SWAN_C = VarName4
thank you
Following your advices I been trying the structure construction
S.name = input ('enter name of the data ".." ==')
S.A = A;
S.A(1,:)=[];
S.B = B;
S.B(1,:)=[];
S.C = C;
S.C(1,:)=[];
S.D = D;
S.D(1,:)=[];
S.E = E;
S.E(1,:)=[];
may i ask if i can also have an input thing command so i can change the name of the structure?
Precede the script with S='west' and then do
'S'.name = input ('enter name of the data ".." ==')
S.A = A;
Here is how I would probably store the information that you are handling:
S.name = input ('enter name')
S.A = VarName2
S.B = VarName3
S.C = VarName4
And if you want to do it a few times:
for t=3:-1:1
S(t).name = input ('enter name')
S(t).A = VarName2
S(t).B = VarName3
S(t).C = VarName4
end
In this way you could now find the struct named 'swan':
idx = strcmpi({S.name},'SWAN')
you can use eval
eval( sprintf('%s_A = VarName2;', fname ) );
eval( sprintf('%s_B = VarName3;', fname ) );
eval( sprintf('%s_C = VarName4;', fname ) );
Note that the use of eval is not recommended.
One alternative option may be to use struct with dynamic field names:
A.( fname ) = VarName2;
B.( fname ) = VarName3;
C.( fname ) = VarName4;
Now you have three structs (A, B and C) with A.SWAN equal to VarName2, B.SWAN equal to VarName3 etc.