Skip to content

Commit

Permalink
initial kdtree script profiled for optimization.
Browse files Browse the repository at this point in the history
  • Loading branch information
leobrowning92 committed Mar 25, 2018
1 parent f936a4c commit 03e17c5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,6 @@ ENV/
.mypy_cache/

# manages cProfiler and gprof2dot outputs
output.*
output.

!speed_milestones/*
33 changes: 16 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,19 @@ the network is labeled:
|...|...|...|...|...|...|
|(r-1)*c|(r-1)*c+1|...|...|...|(r-1)*c+c-1=r*c-1|

## Todo
- consider a way to visualize a current/resistance grid
- Impliment Updateable objects such as FETs/Memristors
- have update matrix U that changes/replaces G to allow for change ie switching
- have U made/updated by querying the edge objects that make up the network
- investigate the networkx package as a way to handle the network to matrix conversions and visualization

## flow
- make network with connections
- add components to edges
- make adjacency matrix from conductance of components
- components must have a way to calculate conductance
- make mna matrix with additional data about sources and ground_nodes
- solve mna
- move mna solutions back to physical network, so that nodes have voltages,
and edges have currents
- update components and resolve

## Optimization
use cProfile and the [gprof2dot](https://github.com/jrfonseca/gprof2dot).py script to generate profiles of time spent for running the script.
explicit number to run optimization on is:

kd_percolation.py 300 --pm 0.135 --length 0 --scaling 5

and corresponds to 300 sticks on a 5um square area with a length distribution of 0.66pm0.44um corresponding to my experimental results. conduction is calculated in each case.

which is run in sequence as:
python -m cProfile -o output.pstats kd_percolation.py 300
python gprof2dot.py -f pstats output.pstats | dot -Tpng -o output.png

The total run time over 5 or 6 tries is between 14 and 15 seconds (user time) when running `time python kd_percolation.py 300` for the original brute force cluster search.

Using a kdtree with a sorted length list and length dependant search radius the same run times are 3.6 to 3.8 seconds.
11 changes: 7 additions & 4 deletions kd_percolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class StickCollection(object):
def __init__(self,n,l,sticks=None,pm=0,scaling=1):
if sticks:
self.sticks, self.intersects = self.make_clusters(sticks)
self.sticks, self.intersects = self.make_clusters(self.make_sticks(n,l=l,pm=pm,scaling=scaling))
self.sticks, self.intersects = self.make_clusters_kdtree(self.make_sticks(n,l=l,pm=pm,scaling=scaling))
self.make_cnet()

def check_intersect(self, s1,s2):
#assert that x intervals overlap
Expand Down Expand Up @@ -106,7 +107,7 @@ def make_clusters_kdtree(self,sticks):
if intersection and 0<=intersection[0]<=1 and 0<=intersection[1]<=1:
sticks.loc[sticks.cluster==sticks.loc[j,'cluster'],'cluster'] = sticks.loc[i,'cluster']
intersects.append([i,j,*intersection, sticks.iloc[i].kind+sticks.iloc[j].kind],)
self.percolating=sticks.loc[0,"cluster"]==sticks.loc[len(sticks)-1,"cluster"]
self.percolating=sticks.loc[0,"cluster"]==sticks.loc[1,"cluster"]
intersects=pd.DataFrame(intersects, columns=["stick1",'stick2','x','y','kind'])
intersects['cluster']=intersects['stick1'].apply(lambda x: sticks.iloc[x].cluster)
return sticks, intersects
Expand Down Expand Up @@ -197,7 +198,7 @@ def show_sticks(self,sticks,intersects,ax=False):
stick_colors=[stick_cmap[i] for i in sticks.kind]
collection=LineCollection(sticks.endarray.values,linewidth=0.5,colors=stick_colors)
ax.add_collection(collection)
isect_cmap={'ms':'g','sm':'g', 'mm':'k','ss':'k', 'vs':'k','sv':'k','vm':'k', 'sg':'k','gs':'k','mg':'k'}
isect_cmap={'ms':'g','sm':'g', 'mm':'k','ss':'k', 'vs':'k','sv':'k','vm':'k', 'sg':'k','gs':'k','mg':'k','gm':'k'}
isect_colors=[isect_cmap[i] for i in self.intersects.kind]
ax.scatter(intersects.x, intersects.y, c=isect_colors, s=20, linewidth=1, marker="x")
ax.set_xlim((-0.02,1.02))
Expand All @@ -216,12 +217,14 @@ def show_sticks(self,sticks,intersects,ax=False):
parser.add_argument("--scaling",type=float,default=5)
parser.add_argument("-t", "--test", action="store_true")
parser.add_argument("-v", "--verbose", action="store_true")
parser.add_argument("--show", action="store_true",default=False)
args = parser.parse_args()
if args.test:
collection=StickCollection(args.number,l=0,pm=args.pm,scaling=5)


else:
collection=StickCollection(args.number,l=args.length,pm=args.pm,scaling=args.scaling)
collection.show_system()
if args.show:
collection.show_system()
# print(len(collection.sticks.cluster.drop_duplicates()))
Binary file added speed_milestones/initialkdtree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added speed_milestones/initialkdtree.pstats
Binary file not shown.

0 comments on commit 03e17c5

Please sign in to comment.