[relacs] add __str__ and __repr__ methods to classes

This commit is contained in:
Jan Grewe 2020-08-18 11:32:15 +02:00
parent 8b93d92f4d
commit 6ab71b2fe9

View File

@ -3,7 +3,6 @@ from .util import BoltzmannFit, unzip_if_needed, gaussian_kernel, zero_crossings
import numpy as np import numpy as np
import nixio as nix import nixio as nix
from scipy.stats import circstd from scipy.stats import circstd
# from scipy.optimize import curve_fit
import os import os
import subprocess import subprocess
from tqdm import tqdm from tqdm import tqdm
@ -372,6 +371,15 @@ class BaselineData:
l = f.readline() l = f.readline()
return np.asarray(data) return np.asarray(data)
def __str__(self):
str = "Baseline activity\n"
str += "Cell %s\t avg. firing rate: %.2f Hz\n" % (self.cell.id, self.baseline_rate)
str += "CV: %.2f\t vector strength: %.2f" % (self.coefficient_of_variation, self.vector_strength)
return str
def __repr__(self):
return self.__str__()
class FIData: class FIData:
"""Class representing the data recorded with the relacs FI-Curve repro. The instance will load the data upon """Class representing the data recorded with the relacs FI-Curve repro. The instance will load the data upon
@ -633,6 +641,14 @@ class FIData:
boltzmann_fit = BoltzmannFit(contrasts, rates) boltzmann_fit = BoltzmannFit(contrasts, rates)
return boltzmann_fit return boltzmann_fit
def __str__(self):
str = "FICurve\n"
str += "FI Curve recorded for cell %s" % self.__cell.id
return str
def __repr__(self):
return self.__str__()
class FileStimulusData: class FileStimulusData:
"""The FileStimulus class provides access to the data recorded and the stimulus presented (if accessible) """The FileStimulus class provides access to the data recorded and the stimulus presented (if accessible)
@ -892,12 +908,16 @@ class FileStimulusData:
Returns: Returns:
[type]: [description] [type]: [description]
""" """
if index == -1: if index == -1:
time_axes = [] time_axes = []
rates = [] rates = []
for i in range(self.size): for i in range(self.size):
t = self.time_axis(i) t = self.time_axis(i)
spikes = self.spikes(i) spikes = self.spikes(i)
if len(spikes) == 0 or len(t) <= 1:
print("Warning: no spikes or time axis to short for index:", i)
continue
r = spike_times_to_rate(spikes, t, kernel_width) r = spike_times_to_rate(spikes, t, kernel_width)
time_axes.append(t) time_axes.append(t)
rates.append(r) rates.append(r)
@ -905,11 +925,31 @@ class FileStimulusData:
elif index >= 0 and index < self.size: elif index >= 0 and index < self.size:
t = self.time_axis(index) t = self.time_axis(index)
spikes = self.spikes(index) spikes = self.spikes(index)
if len(spikes) == 0 or len(t) <= 1:
print("Warning: no spikes or time axis to short for index:", i)
return None, None
r = spike_times_to_rate(spikes, t, kernel_width) r = spike_times_to_rate(spikes, t, kernel_width)
return t, r return t, r
else: else:
raise IndexError("FileStimulusData: index %i out of bounds for time_axes of size %i" % (index, self.size)) raise IndexError("FileStimulusData: index %i out of bounds for time_axes of size %i" % (index, self.size))
def __str__(self):
str = "FileStimulusData\n"
str += "Data recorded running the FileStimulus RePro on cell %s\n" % self.cell.id
contrasts = np.array(self.contrast())
stimuli = np.array(self.stimulus_files())
str += "Stimuli:\n"
for s in np.unique(stimuli):
str += "%s:\n" % (s.split("/")[-1])
s_contrasts = np.unique(contrasts[stimuli == s])
for c in s_contrasts:
str += "\tcontrast %.2f (%i trials)\n" % (c, len(contrasts[(stimuli == s) & (contrasts == c)]))
return str
def __repr__(self):
return self.__str__()
if __name__ == "__main__": if __name__ == "__main__":