Skip to content

Commit

Permalink
ran python 2to3 tool on Python test scripts, OK
Browse files Browse the repository at this point in the history
Updated example slurm jobs scripts to work on USI ICS cluster
git ignore example script output files
  • Loading branch information
stivalaa committed Dec 15, 2021
1 parent 185523a commit ee2c5ac
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 40 deletions.
1 change: 1 addition & 0 deletions TestChangeStatsDirected/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ polblogs_test_results_arrays.out
testChangeStatsDirected_array.exe
testChangeStatsDirected_array
polblogs_test_results_baseline_no2pathtables.txt
*.bak
5 changes: 5 additions & 0 deletions pythonDemo/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
*~
dzA_values_*.txt
theta_values_*.txt
theta_values_*.eps
dzA_values_*.txt
dzA_values_*.eps
*.bak
slurm-*.out
22 changes: 11 additions & 11 deletions pythonDemo/Digraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ def __init__(self, pajek_edgelist_filename, binattr_filename=None,
self.binattr = None # binary attributes: list by node (int not boolean)
self.catattr = None # categorical attributes: list by node

print 'Reading graph and building matrices...',
print('Reading graph and building matrices...', end=' ')
start = time.time()
f = open(pajek_edgelist_filename)
l = f.readline() # first line must be e.g. "*vertices 500"
n = int(l.split()[1])

# empty graph n nodes
self.G = dict(zip(range(n), [dict() for i in range(n)]))
self.Grev = dict(zip(range(n), [dict() for i in range(n)]))
self.G = dict(list(zip(list(range(n)), [dict() for i in range(n)])))
self.Grev = dict(list(zip(list(range(n)), [dict() for i in range(n)])))
self.InTwoPathMatrix = np.zeros((n, n))
self.OutTwoPathMatrix = np.zeros((n, n))
self.MixTwoPathMatrix = np.zeros((n, n))
Expand All @@ -84,20 +84,20 @@ def __init__(self, pajek_edgelist_filename, binattr_filename=None,
l = f.readline()
lsplit = f.readline().split()
while len(lsplit) == 2:
(i, j) = map(int, lsplit)
(i, j) = list(map(int, lsplit))
assert(i >= 1 and i <= n and j >= 1 and j <= n)
self.insertArc(i-1, j-1) # input is 1-based but we are 0-based
lsplit = f.readline().split()

if binattr_filename is not None:
self.binattr = map(int, open(binattr_filename).read().split()[1:])
self.binattr = list(map(int, open(binattr_filename).read().split()[1:]))
assert(len(self.binattr) == n)

if catattr_filename is not None:
self.catattr = map(int, open(catattr_filename).read().split()[1:])
self.catattr = list(map(int, open(catattr_filename).read().split()[1:]))
assert(len(self.catattr) == n)

print time.time() - start, 's'
print(time.time() - start, 's')


def numNodes(self):
Expand All @@ -110,7 +110,7 @@ def numArcs(self):
"""
Return number of arcs in digraph
"""
return sum([len(v.keys()) for v in self.G.itervalues()])
return sum([len(list(v.keys())) for v in self.G.values()])

def density(self):
"""
Expand Down Expand Up @@ -143,13 +143,13 @@ def outIterator(self, i):
"""
Return iterator over out-neighbours of i
"""
return self.G[i].iterkeys()
return iter(self.G[i].keys())

def inIterator(self, i):
"""
Return iterator over in-neighbours of i
"""
return self.Grev[i].iterkeys()
return iter(self.Grev[i].keys())

def insertArc(self, i, j):
"""
Expand All @@ -175,7 +175,7 @@ def updateTwoPathMatrices(self, i, j, isAdd):
"""
incval = 1 if isAdd else -1
n = self.numNodes()
for v in xrange(n):
for v in range(n):
if v == i or v == j:
continue
if self.isArc(i, v):
Expand Down
32 changes: 16 additions & 16 deletions pythonDemo/EstimNetDirectedSimpleDemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ def BasicSampler(G, changestats_func_list, theta, performMove):
accepted = 0
addChangeStats = np.zeros(n)
delChangeStats = np.zeros(n)
for k in xrange(sampler_m):
for k in range(sampler_m):
# basic sampler: select two nodes i and j uniformly at random
# and toggle edge between them
(i, j) = random.sample(range(G.numNodes()), 2) # without replacement
(i, j) = random.sample(list(range(G.numNodes())), 2) # without replacement
assert(i != j)
isDelete = (G.isArc(i, j))
if isDelete:
Expand All @@ -74,7 +74,7 @@ def BasicSampler(G, changestats_func_list, theta, performMove):
# compute change statistics for each of the n statistics using the
# list of change statistic functions
changestats = np.zeros(n)
for l in xrange(n):
for l in range(n):
changestats[l] = changestats_func_list[l](G, i, j)
assert(changestats[l] >= 0)
changeSignMul = -1 if isDelete else +1
Expand Down Expand Up @@ -124,7 +124,7 @@ def algorithm_S(G, changestats_func_list, M1, theta_outfile):
n = len(changestats_func_list)
theta = np.zeros(n)
D0 = np.zeros(n)
for t in xrange(M1):
for t in range(M1):
accepted = 0
(acceptance_rate,
addChangeStats,
Expand All @@ -136,7 +136,7 @@ def algorithm_S(G, changestats_func_list, M1, theta_outfile):
assert(np.all(sumChangeStats >= 0)) # zero is handled below
D0 += dzA**2 # 1/D0 is squared derivative
da = np.zeros(n)
for l in xrange(n):
for l in range(n):
if (sumChangeStats[l] != 0):
da[l] = ACA / sumChangeStats[l]**2
theta_step = np.sign(dzAmean) * da * dzA**2
Expand Down Expand Up @@ -173,9 +173,9 @@ def algorithm_EE(G, changestats_func_list, theta, D0,
n = len(changestats_func_list)
dzA = np.zeros(n) # zero outside loop, dzA accumulates in loop
t = 0
for touter in xrange(Mouter):
for touter in range(Mouter):
thetamatrix = np.empty((M, n)) # rows theta vectors, 1 per inner iter
for tinner in xrange(M):
for tinner in range(M):
accepted = 0
(acceptance_rate,
addChangeStats,
Expand Down Expand Up @@ -240,27 +240,27 @@ def run_on_network_attr(edgelist_filename, param_func_list, labels,
# inner steps of EE
M = int(Msteps * G.density()*(1 - G.density())*G.numNodes()**2 / sampler_m)

print 'M1 = ', M1, ' Mouter = ', Mouter, ' M = ', M
print('M1 = ', M1, ' Mouter = ', Mouter, ' M = ', M)

theta_outfile = open(THETA_OUTFILENAME, 'w',1) # 1 means line buffering
theta_outfile.write('t ' + ' '.join(labels) + ' ' + 'AcceptanceRate' + '\n')
print 'Running Algorithm S...',
print('Running Algorithm S...', end=' ')
start = time.time()
(theta, Dmean) = algorithm_S(G, param_func_list, M1, theta_outfile)
print time.time() - start, 's'
print 'after Algorithm S:'
print 'theta = ', theta
print 'Dmean = ', Dmean
print(time.time() - start, 's')
print('after Algorithm S:')
print('theta = ', theta)
print('Dmean = ', Dmean)
dzA_outfile = open(DZA_OUTFILENAME, 'w',1)
dzA_outfile.write('t ' + ' '.join(labels) + '\n')
print 'Running Algorithm EE...',
print('Running Algorithm EE...', end=' ')
start = time.time()
theta = algorithm_EE(G, param_func_list, theta, Dmean,
Mouter, M, theta_outfile, dzA_outfile)
print time.time() - start, 's'
print(time.time() - start, 's')
theta_outfile.close()
dzA_outfile.close()
print 'at end theta = ', theta
print('at end theta = ', theta)



Expand Down
3 changes: 0 additions & 3 deletions pythonDemo/README
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ The only module (other than default pythone
ones) it needs is numpy https://www.scipy.org/scipylib/download.html
just for simple and efficient vector and matrix data types.

Note python scripts all require Python 2 (and NOT Python 3,
which is not backward compatibile with Python 2).

sample_statistics_n500_directed_binattr_sim420000000.txt and
binaryAttributes_50_50_n500.txt is a simulated 500 node network
with binary attribute, simulated from the following ERGM parameters using PNet:
Expand Down
9 changes: 6 additions & 3 deletions pythonDemo/example_n1000_slurm_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

echo -n "started at: "; date

module load numpy/1.8.2-vlsci_intel-2015.08.25-Python-2.7.9
module load R/3.2.1-vlsci_intel-2015.08.25

time python ./runExample_n1000.py
module load python/3.8.5

time python3 ./runExample_n1000.py

module unload python # on cluster module load r will not work if this is not done
module load r

time Rscript plotEstimNetSimpleDemoResults.R theta_values_sample_statistics_n1000_directed_binattr_sim620000000.txt dzA_values_sample_statistics_n1000_directed_binattr_sim620000000.txt

Expand Down
10 changes: 6 additions & 4 deletions pythonDemo/example_polblogs_slurm_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

#SBATCH --job-name="Py_EstimNetSimple"
#SBATCH --ntasks=1
#SBATCH --time=0-24:00:00
#SBATCH --time=0-48:00:00

echo -n "started at: "; date

module load numpy/1.8.2-vlsci_intel-2015.08.25-Python-2.7.9
module load R/3.2.1-vlsci_intel-2015.08.25
module load python/3.8.5

time python ./runExample_polblogs.py
time python3 ./runExample_polblogs.py

module unload python # on cluster module load r will not work if this is not done
module load r

time Rscript plotEstimNetSimpleDemoResults.R theta_values_polblogs_arclist.txt dzA_values_polblogs_arclist.txt

Expand Down
8 changes: 5 additions & 3 deletions pythonDemo/example_slurm_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

echo -n "started at: "; date

module load numpy/1.8.2-vlsci_intel-2015.08.25-Python-2.7.9
module load R/3.2.1-vlsci_intel-2015.08.25
module load python/3.8.5

time python ./runExample.py
time python3 ./runExample.py

module unload python # on cluster module load r will not work if this is not done
module load r

time Rscript plotEstimNetSimpleDemoResults.R theta_values_sample_statistics_n500_directed_binattr_sim420000000.txt dzA_values_sample_statistics_n500_directed_binattr_sim420000000.txt

Expand Down

0 comments on commit ee2c5ac

Please sign in to comment.