Related
The question is very straight forward. Content here is information collected during the run of an interconnected system. I simplified functions to emphasise the bottlenecks from line_profiler into three dependend functions objectively similar to real code.
As of now it seems all lines are mandatory, but I refuse to believe this. I did try simplifying logical parts with .all() and .any() but either I am not proficient in Python, or I keep running into a bizarre limitation; just keep giving me a different result than expected.
If you are unable to gather full use case of the 3 functions, try to comprehend on a purely syntactic manner (while at least preserving same number of variables) and provide a usable solution to achieve the same functionality.
Total time: 419.632 s
File: switch_array_logic.py
Function: func_A at line 336
Line # Hits Time Per Hit % Time Line Contents
==============================================================
336 #profile
337 def func_A(_switch,_loc,_prev_state=None,_cur_state=None,bulb_on=True):
338 43048749 27529633.0 0.6 6.6 if bulb_on:
339 43048651 43186202.0 1.0 10.3 _p_cur_state = _switch[_loc][2]
340 43048651 42282929.0 1.0 10.1 _p_prev_state = _switch[_loc-1][2]
341 else:
342 98 80.0 0.8 0.0 _p_prev_state = _prev_state
343 98 54.0 0.6 0.0 _p_cur_state = _cur_state
348 43048749 38735332.0 0.9 9.2 cond_11 = (_p_cur_state >= 0)
349 43048749 35887392.0 0.8 8.6 cond_10 = (_p_cur_state < 0)
350 43048749 34747819.0 0.8 8.3 cond_00 = (_p_prev_state < 0)
351 43048749 34685101.0 0.8 8.3 cond_01 = (_p_prev_state > 0)
352
353 43048749 24860540.0 0.6 5.9 cond_x = cond_00 and cond_11
354 43048749 24083554.0 0.6 5.7 cond_y = cond_10 and cond_01
355 43048749 24902602.0 0.6 5.9 condition = cond_x or cond_y
356
357 43048749 23279349.0 0.5 5.5 _brighness = 0
358 43048749 23704315.0 0.6 5.6 if condition:
359 21573867 18852346.0 0.9 4.5 _brighness = _p_prev_state/2
360 43048749 22894531.0 0.5 5.5 return _brighness
Total time: 66.9593 s
File: switch_array_logic.py
Function: func_B at line 362
Line # Hits Time Per Hit % Time Line Contents
==============================================================
362 #profile
363 def func_B(_switch,_pos,_loc,_brighness):
364 43638012 66959275.0 1.5 100.0 return _switch[_pos][3] - _switch[_loc][3] - _brighness
Total time: 9.59384 s
File: switch_array_logic.py
Function: func_C at line 378
Line # Hits Time Per Hit % Time Line Contents
==============================================================
378 #profile
379 def func_C(_switchs_slice,_valid,_PH_LEVEL):
380 114012 82093.0 0.7 0.9 _idx = 0
381 114012 2403035.0 21.1 25.0 _brighness = func_A(_switchs_slice,_idx+1)
382 809223 583712.0 0.7 6.1 for _pos in range(1, _switchs_slice.shape[0]):
383 808216 831149.0 1.0 8.7 if _valid and _switchs_slice[_idx][0] < _switchs_slice[_pos][0]:
385 57307 44704.0 0.8 0.5 return False,_pos
386 750909 1686519.0 2.2 17.6 elif ~_valid and _switchs_slice[_idx][1] > _switchs_slice[_pos][1]:
388 47536 41899.0 0.9 0.4 return False,_pos
389 703373 3320805.0 4.7 34.6 _ph_out = func_B(_switchs_slice,_pos,_idx,_brighness)
391 703373 592597.0 0.8 6.2 if abs(_ph_out) > _PH_LEVEL[1] :
393 8162 6334.0 0.8 0.1 return True, _pos #Successful Case
395 1007 990.0 1.0 0.0 return False,_switchs_slice.shape[0]
Additionally, as I am not familiar with Cython, but can someone try to provide an optimised version of this in Cython in answers really appreciate it. Not sure we will use it in the code. But I would very much like to see performance disparity.
Thanks in advance!
I have a list of more than 100 points. I'd like to plot a figure like this picture. The lines connect any two points whose distance is less than 3.
1.53 2.40
5.39 3.02
4.35 1.29
9.58 8.34
6.59 1.45
3.44 3.45
7.22 0.43
0.23 8.09
4.38 3.49
https://www.codeproject.com/Articles/1237026/Simple-MLP-Backpropagation-Artificial-Neural-Netwo
You probably have to check every point against every other point whether the distance is less than your threshold. So, create a table with all these points, the vector between them and plot them with vectors. The following example creates some random points with random sizes and random colors.
Code:
### Plot connections between points which are closer than a threshold
reset session
set size square
# create some random test data
set print $Data
myColors = "0xe71840 0x4d76c3 0xf04092 0x47c0ad 0xf58b1e 0xe6eb18 0x59268e 0x36b64c"
myColor(n) = int(word(myColors,n))
do for [i=1:100] {
print sprintf("%g %g %g %d", rand(0), rand(0), rand(0)*2+1, myColor(int(rand(0)*8)+1))
}
set print
d(x1,y1,x2,y2) = sqrt((x2-x1)**2 + (y2-y1)**2)
myDist = 0.2
set print $Connect
do for [i=1:|$Data|-1] {
x1=real(word($Data[i],1))
y1=real(word($Data[i],2))
do for [j=i+1:|$Data|] {
x2=real(word($Data[j],1))
y2=real(word($Data[j],2))
if (d(x1,y1,x2,y2)<myDist) { print sprintf("%g %g %g %g", x1, y1, x2-x1, y2-y1) }
}
}
set print
set key noautotitle
plot $Connect u 1:2:3:4 w vec lc "grey" nohead, \
$Data u 1:2:3:4 w p pt 7 ps var lc rgb var
### end of code
Result:
You do not specify how to choose the node size or color. I show an example using a constant pointsize and taking the color from sequential linetypes
[![enter image description here][1]][1]$DATA << EOD
1.53 2.40
5.39 3.02
4.35 1.29
9.58 8.34
6.59 1.45
3.44 3.45
7.22 0.43
0.23 8.09
4.38 3.49
EOD
N = |$DATA|
do for [i=1:N] {
do for [j=i+1:N] {
x0 = real(word($DATA[i],1))
y0 = real(word($DATA[i],2))
x1 = real(word($DATA[j],1))
y1 = real(word($DATA[j],2))
if ((x1-x0)**2 + (y1-y0)**2 <= 9) {
set arrow from x0,y0 to x1,y1 nohead
}
}
}
unset border
unset tics
unset key
set pointsize 3
plot $DATA using 1:2:0 with points pt 7 lc variable
I am having trouble doing something that seems to me straightforward.
My data is:
ROE_SP500_Q2_2018_quantile.to_json()
'{"index":{"0":0.0,"1":0.05,"2":0.1,"3":0.15,"4":0.2,"5":0.25,"6":0.3,"7":0.35,"8":0.4,"9":0.45,"10":0.5,"11":0.55,"12":0.6,"13":0.65,"14":0.7,"15":0.75,"16":0.8,"17":0.85,"18":0.9,"19":0.95},"ROE_Quantiles":{"0":-0.8931,"1":-0.0393,"2":0.00569,"3":0.03956,"4":0.05826,"5":0.075825,"6":0.09077,"7":0.10551,"8":0.12044,"9":0.14033,"10":0.15355,"11":0.17335,"12":0.1878,"13":0.209175,"14":0.2357,"15":0.27005,"16":0.3045,"17":0.3745,"18":0.46776,"19":0.73119}}'
My code for the plot is:
plt.close()
plt.figure(figsize=(14,8))
sns.barplot(x = 'Quantile', y = 'ROE', data = ROE_SP500_Q2_2018_quantile)
plt.vlines(x = 0.73, ymin = 0, ymax = 0.6, color = 'blue', size = 2)
plt.show()
which returns the following image:
I would like to correct the following problems:
a) The ticklabels which are overly crowded in a strange way I do not understand
b) The vline which appears in the wrong place. I am using the wrong argument to set the thickness of the line and I get an error.
Pass to parameter data DataFrame, check seaborn.barplot:
data : DataFrame, array, or list of arrays, optional
Dataset for plotting. If x and y are absent, this is interpreted as wide-form. Otherwise it is expected to be long-form.
sns.barplot(x = 'index', y = 'ROE_Quantiles', data = ROE_SP500_Q2_2018_quantile)
#TypeError: vlines() missing 2 required positional arguments: 'ymin' and 'ymax'
plt.vlines(x = 0.73, ymin = 0, ymax = 0.6, color = 'blue', linewidth=5)
j = '{"index":{"0":0.0,"1":0.05,"2":0.1,"3":0.15,"4":0.2,"5":0.25,"6":0.3,"7":0.35,"8":0.4,"9":0.45,"10":0.5,"11":0.55,"12":0.6,"13":0.65,"14":0.7,"15":0.75,"16":0.8,"17":0.85,"18":0.9,"19":0.95},"ROE_Quantiles":{"0":-0.8931,"1":-0.0393,"2":0.00569,"3":0.03956,"4":0.05826,"5":0.075825,"6":0.09077,"7":0.10551,"8":0.12044,"9":0.14033,"10":0.15355,"11":0.17335,"12":0.1878,"13":0.209175,"14":0.2357,"15":0.27005,"16":0.3045,"17":0.3745,"18":0.46776,"19":0.73119}}'
import ast
df = pd.DataFrame(ast.literal_eval(j))
print (df)
index ROE_Quantiles
0 0.00 -0.893100
1 0.05 -0.039300
10 0.50 0.153550
11 0.55 0.173350
12 0.60 0.187800
13 0.65 0.209175
14 0.70 0.235700
15 0.75 0.270050
16 0.80 0.304500
17 0.85 0.374500
18 0.90 0.467760
19 0.95 0.731190
2 0.10 0.005690
3 0.15 0.039560
4 0.20 0.058260
5 0.25 0.075825
6 0.30 0.090770
7 0.35 0.105510
8 0.40 0.120440
9 0.45 0.140330
plt.close()
plt.figure(figsize=(14,8))
sns.barplot(x = 'index', y = 'ROE_Quantiles', data = df)
plt.vlines(x = 0.73, ymin = 0, ymax = 0.6, color = 'blue', linewidth=5)
plt.show()
I am applying the quantile function on the duration column of my data frame:
a=df.groupby('version')[['duration']].quantile([.25, .5, .75])
a
duration
version
4229 0.25 1451.00
0.50 1451.00
0.75 1451.00
6065 0.25 213.75
0.50 426.50
0.75 639.25
9209 0.25 386.50
0.50 861.00
0.75 866.00
2304 0.25 664.50
0.50 669.00
0.75 736.50
6389 0.25 1.00
0.50 797.00
0.75 832.00
I am wondering how do I re-shape/re-pivot the above data frame, so the new data frame (yes, it has to be a data frame format) could look like:
version duration_Q1 duration_Q2 duration_Q3
4429 1451.00 1451.00 1451.00
6065 213.75 426.50 639.25
9209 386.50 861.00 866.00
2304 664.50 669.00 736.50
6389 1.00 797.00 832.00
Thanks!
You could use unstack, followed by some renaming operations
a = pd.DataFrame('duration': {(2304L, 0.25): 1565.6861959516361,
(2304L, 0.5): 446.4769649280514,
(2304L, 0.75): 701.8254115357969,
(4229L, 0.25): 1868.982390749203,
(4229L, 0.5): 242.36201172579996,
(4229L, 0.75): 789.482292226787,
(6065L, 0.25): 1421.9585894685038,
(6065L, 0.5): 357.04491735326343,
(6065L, 0.75): 169.78973203074895,
(6389L, 0.25): 1789.1550141153925,
(6389L, 0.5): 516.9365429825862,
(6389L, 0.75): 1830.6493228794639,
(9209L, 0.25): 1129.853279993191,
(9209L, 0.5): 1759.1258334115485,
(9209L, 0.75): 1499.0498929925702}}
)
pvt = a.unstack()
pvt.columns = pvt.columns.droplevel(0)
pvt.rename(columns={0.25:'duration_Q1',0.5:'duration_Q2',0.75:'duration_Q3'},inplace=True)
duration_Q1 duration_Q2 duration_Q3
version
2304 1565.686196 446.476965 701.825412
4229 1868.982391 242.362012 789.482292
6065 1421.958589 357.044917 169.789732
6389 1789.155014 516.936543 1830.649323
9209 1129.853280 1759.125833 1499.049893
I have a data file "data.txt" which contains the coordinates of the borders of several boxes in three dimensions. Each line represents a single box. The file contains over 100 boxes.
x_Min x_Max y_Min y_Max z_Min z_Max
-0.2 0.2 -0.2 0.2 -0.2 0.2
0.2 0.4 -0.2 0.2 -0.2 0.2
....
...
..
Now I want to plot that. In two dimensions it is very easy by using
plot "boxes.txt" u 1:2:3:4 w boxxyerrorbars
With (x-Value):(y-Value):(Half Width):(Half Height).
Than I get this:
But how can I achieve this in three dimensions? I didn't find any solution for this problem.
In case you are still interested in a gnuplot solution...
If it is sufficient to just draw the edges of the boxes you can use the plotting style with vectors. You simply need to select the necessary columns and plot all edges in 3 loops. Here gnuplot's integer division (e.g. 1/2=0) is helpful.
However, if you want to plot surfaces and hide surfaces if they are covered by another box you'd better use with pm3d (check help pm3d). Then, however, you have to re-shape your input data.
Script:
### plot edges of boxes in 3D
reset session
$Data <<EOD
x_Min x_Max y_Min y_Max z_Min z_Max
-0.2 0.2 -0.2 0.2 -0.2 0.2
0.3 0.4 -0.1 0.2 -0.1 0.2
-1.5 -0.5 -1.2 -0.4 -0.9 0.0
0.5 1.0 -1.0 -0.5 -0.5 -0.1
0.0 0.3 -1.4 -1.1 -1.0 -0.7
EOD
set xyplane relative 0
set view equal xyz
set view 60,30,1.7
set xtics 0.5
set ytics 0.5
set ztics 0.5
set key noautotitle
splot for [i=0:3] $Data u 1:i/2+3:i%2+5:($2-$1):(0):(0):0 w vec lc var nohead, \
for [i=0:3] '' u i/2+1:3:i%2+5:(0):($4-$3):(0):0 w vec lc var nohead, \
for [i=0:3] '' u i/2+1:i%2+3:5:(0):(0):($6-$5):0 w vec lc var nohead
### end of script
Result:
I actually found a solution using Python and Matplotlib.
import numpy as np
import matplotlib.pyplot as plt
import random
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
DIM = 3;
# Unit cube
cube = [[[0.0,1.0],[0.0,0.0],[0.0,0.0]],\
[[0.0,0.0],[0.0,1.0],[0.0,0.0]],\
[[0.0,0.0],[0.0,0.0],[0.0,1.0]],\
[[1.0,1.0],[0.0,1.0],[0.0,0.0]],\
[[1.0,0.0],[1.0,1.0],[0.0,0.0]],\
[[1.0,1.0],[0.0,0.0],[0.0,1.0]],\
[[1.0,1.0],[1.0,1.0],[0.0,1.0]],\
[[0.0,0.0],[1.0,1.0],[0.0,1.0]],\
[[0.0,0.0],[0.0,1.0],[1.0,1.0]],\
[[0.0,1.0],[0.0,0.0],[1.0,1.0]],\
[[1.0,1.0],[0.0,1.0],[1.0,1.0]],\
[[0.0,1.0],[1.0,1.0],[1.0,1.0]]]
# Number of Cubes
numb_Cubes = 5
# Array with positions [x, y, z]
pos = [[0 for x in range(DIM)] for y in range(numb_Cubes)]
for k in range(numb_Cubes):
for d in range(DIM):
pos[k][d] = random.uniform(-1,1)
# Size of cubes
size_of_cubes = [0 for y in range(numb_Cubes)]
for k in range(numb_Cubes):
size_of_cubes[k] = random.random()
# Limits
xmin, xmax = -1, 1
ymin, ymax = -1, 1
zmin, zmax = -1, 1
for n in range(numb_Cubes):
for k in range(len(cube)):
x = np.linspace(cube[k][0][0]*size_of_cubes[n]+pos[n][0], cube[k][0][1]*size_of_cubes[n]+pos[n][0], 2)
y = np.linspace(cube[k][1][0]*size_of_cubes[n]+pos[n][1], cube[k][1][1]*size_of_cubes[n]+pos[n][1], 2)
z = np.linspace(cube[k][2][0]*size_of_cubes[n]+pos[n][2], cube[k][2][1]*size_of_cubes[n]+pos[n][2], 2)
ax.plot(x, y, z, 'black', lw=1)
ax.set_xlim([xmin,xmax])
ax.set_ylim([ymin,ymax])
ax.set_zlim([zmin,ymax])
The result I get:
I am still interested in a solution for gnuplot or a faster solution for Python.