Detect Mean Spike rate per trial/episode

(* Pull in Axon text file records of detected spikes exported from ClampFit, trim the Header *)
base=Import["FILENAME.atf","Table"];
baseTrim=base[[4;;-1]];
eps=Range[First[baseTrim[[-1]]]];

(*Count detected events per episode/trial, divide event # by EPISODE_LENGTH*)
(*Ignores Structure inside trial, but avoides weird elements in imported lists*)
traceEvents=Table[baseTrim[[i,1]],{i,Length[baseTrim]}];
traceCounts=Table[Count[traceEvents,i],{i,eps}];
traceWindow=EPISODE_LENGTH;
rates=traceCounts/traceWindow;
Max[rates];

(*Visualize spike rate over time to identify stable baselines from START to END for measurement*)
epsOK=eps;
ListPlot[Transpose[{epsOK,rates[[epsOK]]}],PlotRange->{0,Ceiling[Max[rates],10]},PlotStyle->PointSize[0.02],GridLines->Automatic]

(*Measure the Median spike rate of the stable baseline*)
epsOK=Range[START,END];
Median[rates[[epsOK]]]

Collect Spiketimes across episodes into Histogram

(*Execute First to run code below*)
findPSTHMini[spikeTimes_,triggerTime_,tRelMin_,tRelMax_,binWidth_]:=Module[
{
nEpisodes=Length[spikeTimes],
ep,
spikeTimesRel,
spikeCounts,
spikeCountsTotal,
pSTH,
i,
tRels
},
For[ ep=1,ep≤nEpisodes,ep++,
spikeTimesRel=spikeTimes[[ep]]-triggerTime;
spikeCounts=BinCounts[spikeTimesRel,{tRelMin,tRelMax,binWidth}];
If[ (ep==1),
spikeCountsTotal=spikeCounts;
,
spikeCountsTotal+=spikeCounts;
];
];
pSTH=spikeCountsTotal/(binWidth*nEpisodes);tRels=Range[tRelMin,tRelMax-binWidth,binWidth];

Transpose[{tRels,pSTH}]
]

(* Pull in Axon text file records of detected spikes, trim the Header *)
base=Import["FILENAME.atf","Table"];
baseTrim=base[[4;;-1]];
eps=Range[First[baseTrim[[-1]]]];

(* Pull out spike times, convert to list of lists for each 'trial' *)
baseTimes=Table[baseTrim[[i,10]],{i,Length[baseTrim]}];
trace=Table[baseTrim[[i,1]],{i,Length[baseTrim]}];
baseTimesTrace=Transpose[{trace,baseTimes}];
baseEps=Table[Select[baseTimesTrace,#[[1]]==i&],{i,1,Max[eps]}];
baseSpikes=Table[Map[Last,baseEps[[i]]]/1000,{i,Length[baseEps]}];

hz=Table[baseTrim[[i,-4]],{i,Length[baseTrim]}];
trace=Table[baseTrim[[i,1]],{i,Length[baseTrim]}];
hzTrace=Transpose[{trace,hz}];
hzEps=Table[Select[hzTrace,#[[1]]==i&],{i,eps}];
hzEpsBase=Table[Rest[Map[Last,hzEps[[i]]]],{i,Length[hzEps]}];

(*Remove Glitched Episodes from analysis*)
epsOK=Drop[eps,{BADEP1,BADEP2}];
ListPlot[Table[Mean[hzEpsBase[[i]]],{i,epsOK}],PlotRange->100,PlotStyle->PointSize[0.02]]

(*Make and Plot the Histogram. Time Zero is the episode start, Start and End time can be the whole or a part of the episode duration. *)
baseH=findPSTHMini[baseSpikes[[epsOK]],TIMEZERO,STARTTIME,ENDTIME,BINWIDTH];
ListStepPlot[baseH,PlotRange->{0,100},PlotStyle->Black]

(*Export PSTH to Save*)
filename="FILENAME_PSTH.txt"
Export[filename,baseH,"Table"];

(*Import PSTH for further analysis*)
baseH=Import["FILENAME_PSTH.txt","Table"];

Down Sample and Segment XY Data to make Figures

(* Reduce the points of XY Trace data exported from ClampFit while preserving peak heights. Makes current and voltage traces smaller and easier to manipulate into figures. *)
downSample[xyData_,target_]:=Module[
{
totalPoints=Length[xyData],
factor=target*2,
sampleNum,
starts,ends,
dwnSmple,
k,sample,
orderY,minMax,orderX

},

sampleNum=Round[totalPoints/factor];
starts=Table[1+i*factor,{i,0,sampleNum-1}];
ends=Table[factor+i*factor,{i,0,sampleNum-1}];

dwnSmple={};
For[k=1,k≤sampleNum,k++,

sample=xyData[[starts[[k]];;ends[[k]]]];
orderY=SortBy[sample,Last];
minMax={First[orderY],Last[orderY]};
orderX=SortBy[minMax,First];
AppendTo[dwnSmple,orderX];

];
Flatten[dwnSmple,1]
]

(* Cut out a segment of a trace using the start time, end time and the time step (1/sampling rate) *)
segment[data_,dx_,xMin_,xMax_]:=data[[ Round[1+xMin/dx];;Round[xMax/dx] ]]