-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp_is_gpu.py
73 lines (62 loc) · 2.08 KB
/
mp_is_gpu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import cupy as cp
import os
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
def lempel_ziv_complexity(binary_sequence):
"""Lempel-Ziv complexity for a binary sequence, in simple Python code."""
u, v, w = 0, 1, 1
v_max = 1
length = len(binary_sequence)
complexity = 1
while True:
if binary_sequence[u + v - 1] == binary_sequence[w + v - 1]:
v += 1
if w + v >= length:
complexity += 1
break
else:
if v > v_max:
v_max = v
u += 1
if u == w:
complexity += 1
w += v_max
if w > length:
break
else:
u = 0
v = 1
v_max = 1
else:
v = 1
return complexity
# function to calculate Lempel-Ziv complexity for a given file
def calculate_LZ_complexity(file_name):
with open("D:\Education\Dissertation\code\data55" + "/"+file_name, "r") as text_file:
# read whole file to a string
data = text_file.read()
# convert string to binary sequence
binary_sequence = cp.asarray([int(d) for d in data if d.isdigit()])
# calculate Lempel-Ziv complexity
n = lempel_ziv_complexity(binary_sequence)
return n
# define the list of file names to process
file_names = []
for x in os.listdir("D:\Education\Dissertation\code\data55"):
if x.endswith(".txt"):
# Prints only text file present in My Folder
file_names.append(x)
print(x)
# calculate Lempel-Ziv complexity for each file using CuPy in parallel
with cp.cuda.Device(0):
n_list = cp.array(list(map(calculate_LZ_complexity, file_names)))
# plot Lempel-Ziv complexity against temperature
temp = np.arange(0.1, 5.1, 0.1)
T_list = np.round(temp,3)
plt.axvline(x = 2.26918, color = 'b', label = 'Critical temperature')
plt.plot(T_list, n_list.get(), 'o-')
plt.xlabel('Temperature')
plt.ylabel('Lempel-Ziv Complexity')
plt.legend()
plt.show()