Skip to content

Commit

Permalink
indentation correction
Browse files Browse the repository at this point in the history
  • Loading branch information
hfelippe committed Mar 17, 2022
1 parent 040c9ac commit a4d997a
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 148 deletions.
102 changes: 51 additions & 51 deletions scripts/export_density_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,59 @@
path_to_data = str("./data/time_series/")

def export_density_matrix(path_to_data):
"""
Export the density matrices as TXT files (the same ones found in
./data/density_matrices/).
"""
Export the density matrices as TXT files (the same ones found in
./data/density_matrices/).
Parameters
----------
path_to_data : str
The path to the directory containing the time series.
"""
folder = [
file for file in sorted(
glob(path_to_data + '\*',
recursive=True),
key=numericalSort)
]

before = [
file for file in sorted(
glob(folder[1] + '\*',
recursive=True),
key=numericalSort)
]

after = [file for file in sorted(
glob(folder[0] + '\*',
recursive=True),
key=numericalSort)
]


n, m = len(before), len(after)


def export(path, subject, before=False, after=False):

X = array_of_time_series(path)

Parameters
----------
path_to_data : str
The path to the directory containing the time series.
"""
folder = [
file for file in sorted(
glob(path_to_data + '\*',
recursive=True),
key=numericalSort)
]

before = [
file for file in sorted(
glob(folder[1] + '\*',
recursive=True),
key=numericalSort)
]

after = [file for file in sorted(
glob(folder[0] + '\*',
recursive=True),
key=numericalSort)
]


n, m = len(before), len(after)


def export(path, subject, before=False, after=False):

X = array_of_time_series(path)

A = density_matrix(X)
A = density_matrix(X)

if before == True:
return np.savetxt('density_matrix_subject_' + str(subject) + '_before.txt', A)
if before == True:
return np.savetxt('density_matrix_subject_' + str(subject) + '_before.txt', A)

if after == True:
return np.savetxt('density_matrix_subject_' + str(subject) + '_after.txt', A)
if after == True:
return np.savetxt('density_matrix_subject_' + str(subject) + '_after.txt', A)

for i in np.arange(7, 8):
export(
before[i]+'**/timeCourse*.mat', i + 1,
before=True, after=False
)
for i in np.arange(7, 8):
export(
before[i]+'**/timeCourse*.mat', i + 1,
before=True, after=False
)

for i in np.arange(7, 8):
export(
after[i]+'**/timeCourse*.mat', i + 1,
before=False, after=True
)
for i in np.arange(7, 8):
export(
after[i]+'**/timeCourse*.mat', i + 1,
before=False, after=True
)
34 changes: 17 additions & 17 deletions scripts/export_entropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@
path_to_data = str("./data/density_matrices/")

mat_before = [
np.loadtxt(file) for file in sorted(
glob(path_to_data + "density*before.txt",
recursive=True),
key=numericalSort
)
]
np.loadtxt(file) for file in sorted(
glob(path_to_data + "density*before.txt",
recursive=True),
key=numericalSort
)
]

mat_after = [
np.loadtxt(file) for file in sorted(
glob(path_to_data + "density*after.txt",
recursive=True),
key=numericalSort
)
]
np.loadtxt(file) for file in sorted(
glob(path_to_data + "density*after.txt",
recursive=True),
key=numericalSort
)
]


S_before = np.array(
[von_neumann_entropy(X) for X in mat_before]
)
[von_neumann_entropy(X) for X in mat_before]
)

S_after = np.array(
[von_neumann_entropy(X) for X in mat_after]
)
[von_neumann_entropy(X) for X in mat_after]
)

dS = S_before - S_after

print("",
"S_before = [ subject_1 , subject_2, ... , subject_9 ]",
" = "+str(S_before), "",sep="\n\n")
" = "+str(S_before), "",sep="\n\n")

print("",
"S_after = [ subject_1 , subject_2, ... , subject_9 ]",
Expand Down
161 changes: 81 additions & 80 deletions scripts/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
numbers = re.compile(r'(\d+)')

def numericalSort(value):

parts = numbers.split(value)
parts[1::2] = map(int, parts[1::2])
return parts
Expand All @@ -24,106 +24,107 @@ def numericalSort(value):

def matlab_to_python(file):
"""
Convert MATLAB arrays into Python arrays.
Convert MATLAB arrays into Python arrays.
The original dataset is composed of BOLD fMRI time series as MATLAB
arrays, that is, files with the .m extension. This function is thus
necessary for the Pythonic analysis of the time series.
The original dataset is composed of BOLD fMRI time series as MATLAB
arrays, that is, files with the .m extension. This function is thus
necessary for the Pythonic analysis of the time series.
Parameters
----------
file : MATLAB array
Files with .m extension.
Parameters
----------
file : MATLAB array
Files with .m extension.
Returns
-------
py_arr : ndarray
The converted MATLAB array.
Returns
-------
py_arr : ndarray
The converted MATLAB array.
"""
m_arr = io.loadmat(file)['X_3']
n = len(m_arr)
py_arr = m_arr.reshape(1, n)[0]
m_arr = io.loadmat(file)['X_3']
n = len(m_arr)
py_arr = m_arr.reshape(1, n)[0]

return py_arr
return py_arr


def array_of_time_series(PATH):
"""
Amalgamate the time series in a given folder (of a subject in a
particular condition, "before" or "after"). Returns an array
containing a set of vectors of observations (time series).
Again, since this analysis requires Python arrays, we invoke the
function matlab_to_python to convert MATLAB arrays into Python
ones.
Parameters
----------
PATH: str
The path to the dataset.
Returns
-------
X : ndarray
A 1-D containing multiple variables.
"""
X = np.array(
[
matlab_to_python(file) for file in sorted(
glob(PATH, recursive=True),
key=numericalSort
)
]
)
return X
Amalgamate the time series in a given folder (of a subject in a
particular condition, "before" or "after"). Returns an array
containing a set of vectors of observations (time series).
Again, since this analysis requires Python arrays, we invoke the
function matlab_to_python to convert MATLAB arrays into Python
ones.
Parameters
----------
PATH: str
The path to the dataset.
Returns
-------
X : ndarray
A 1-D containing multiple variables.
"""
X = np.array(
[
matlab_to_python(file) for file in sorted(
glob(PATH, recursive=True),
key=numericalSort
)
]
)

return X


def density_matrix(X):
"""
Calculate the density matrix from a set of vectors.
"""
Calculate the density matrix from a set of vectors.
Parameters
----------
X : array_like
A 1-D or 2-D array containing multiple variables.
Parameters
----------
X : array_like
A 1-D or 2-D array containing multiple variables.
Returns
-------
A : ndarray
The scaled correlation matrix (density matrix) of X.
"""
N = len(X)
R = np.corrcoef(X)
A = R / N
Returns
-------
A : ndarray
The scaled correlation matrix (density matrix) of X.
"""
N = len(X)
R = np.corrcoef(X)
A = R / N

return A
return A


def von_neumann_entropy(A, norm=False):
"""
Calculate the von Neumann entropy of a density matrix.
"""
Calculate the von Neumann entropy of a density matrix.
Parameters
----------
A : array_like
Density matrix whose von Neumann entropy to evaluate.
Parameters
----------
A : array_like
Density matrix whose von Neumann entropy to evaluate.
norm : bool, optional
Evaluates the normalized von Neumann entropy of A. (Default: False)
norm : bool, optional
Evaluates the normalized von Neumann entropy of A. (Default: False)
Returns
-------
S : ndarray
The von Neumann entropy of A.
"""
logA = linalg.logm(A)
AlogA = np.matmul(A, logA)
Returns
-------
S : ndarray
The von Neumann entropy of A.
"""
logA = linalg.logm(A)
AlogA = np.matmul(A, logA)

N = len(A)
S = -np.trace(AlogA)
N = len(A)
S = -np.trace(AlogA)

if norm == True:
return S / np.log(N)
if norm == True:
return S / np.log(N)

else:
return S
else:
return S

0 comments on commit a4d997a

Please sign in to comment.