Skip to content

Commit

Permalink
Added PDF_report function
Browse files Browse the repository at this point in the history
Still needs some more coding to make the PDF look fancy, and also it would be interesting to add a new function in order to generate the PDF report for a set of files.
  • Loading branch information
jpdominguez committed Dec 17, 2020
1 parent ea5c137 commit 01eda59
Showing 1 changed file with 82 additions and 1 deletion.
83 changes: 82 additions & 1 deletion src/pyNAVIS/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
import time
import copy
import random
import datetime

import matplotlib.backends.backend_pdf
import matplotlib.pyplot as plt
import numpy as np

from .loaders import SpikesFile
from .savers import Savers
from .utils import Utils
from .plots import Plots

class Functions:

Expand Down Expand Up @@ -260,4 +263,82 @@ def extract_channels_activities(spikes_file, addresses, reset_addresses = True,
if reset_addresses == True:
new_spikes_file.addresses = [addr - addresses[0] for addr in new_spikes_file.addresses]
new_spikes_file.timestamps = spikes_per_channels_ts
return new_spikes_file
return new_spikes_file




@staticmethod
def PDF_report(spikes_file, settings, output_path, plots = ["Spikegram", "Sonogram", "Histogram", "Average activity", "Difference between L/R"], vector = False, verbose = False):
"""
Generates a PDF report with the spikegram, sonogram, histogram, average activity and difference between L/R plots obtained from the input SpikesFile or path containing SpikeFiles.
Parameters:
spikes_file (SpikesFile or string): File or path to use.
settings (MainSettings): Configuration parameters for the input file.
vector (boolean, optional): Set to True if you want the Spikegram plot vectorized. Note: this may make your PDF heavy.
verbose (boolean, optional): Set to True if you want the execution time of the function to be printed.
Returns:
None.
Notes:
If the path used as input is a folder instead of a spikes file, the PDF report is generated for every spikes file contained in the folder.
"""

if isinstance(spikes_file, str):

spikes_file_extension = os.path.splitext(spikes_file)

if spikes_file_extension == ".aedat":
spikes_file = Loaders.loadAEDAT(spikes_file, settings)
elif spikes_file_extension == ".csv":
spikes_file = Loaders.loadCSV(spikes_file, settings)
elif spikes_file_extension == ".txt":
spikes_file = Loaders.loadZynqGrabberData(spikes_file, settings)

spikes_file = Functions.adapt_SpikesFile(spikes_file, settings)

if isinstance(spikes_file, SpikesFile):

pdf = matplotlib.backends.backend_pdf.PdfPages(output_path)

# Spikegram
if any("Spikegram" in s for s in plots):
spikegram = Plots.spikegram(spikes_file, settings, )
pdf.savefig(spikegram)
plt.draw()
# Sonogram
if any("Sonogram" in s for s in plots):
sonogram = Plots.sonogram(spikes_file, settings)
pdf.savefig(sonogram)
plt.draw()
# Histogram
if any("Histogram" in s for s in plots):
Plots.histogram(spikes_file, settings,)
pdf.savefig()
plt.draw()
# Average activity
if any("Average activity" in s for s in plots):
Plots.average_activity(spikes_file, settings,)
pdf.savefig()
plt.draw()
# Difference between L/R
if settings.mono_stereo == 1 and any("Difference between L/R" in s for s in plots):
Plots.difference_between_LR(spikes_file, settings,)
pdf.savefig()
plt.draw()


d = pdf.infodict()
d['Title'] = 'pyNAVIS report'
d['Author'] = 'Juan P. Dominguez-Morales'
d['Subject'] = 'pyNAVIS report'
d['Keywords'] = 'pyNAVIS'
d['CreationDate'] = datetime.datetime.today()
d['ModDate'] = datetime.datetime.today()

pdf.close()

else:
print("[Functions.PDF_report] > InputFileError: the input SpikesFile is not valid.")

0 comments on commit 01eda59

Please sign in to comment.