Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impose_deadtime and subconductance_as functions added. #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export(bursts.defined_by_tcrit)
export(bursts.get_gaps)
export(bursts.impose_deadtime)
export(bursts.pcloseds)
export(bursts.popens)
export(bursts.recombine)
Expand All @@ -10,6 +11,7 @@ export(bursts.select)
export(bursts.sort)
export(bursts.space_out)
export(bursts.start_times_update)
export(bursts.subconductance_as)
export(clampfit.read)
export(cplot.log_root_axes)
export(cplot.pclosed_ts)
Expand All @@ -27,17 +29,20 @@ export(hst.write)
export(risetime.correct_gaussian)
export(scan.read)
export(segment.closed_dwells)
export(segment.consecutives_to_dwells)
export(segment.count_closed)
export(segment.count_dwells)
export(segment.count_open)
export(segment.create)
export(segment.duration)
export(segment.impose_deadtime)
export(segment.name)
export(segment.open_dwells)
export(segment.pclosed)
export(segment.popen)
export(segment.seg)
export(segment.start_time)
export(segment.subconductance_as)
export(segment.verify)
export(util.basename)
importFrom(graphics,axis)
Expand Down
39 changes: 39 additions & 0 deletions R/bursts.R
Original file line number Diff line number Diff line change
Expand Up @@ -527,3 +527,42 @@ bursts.popens <- function (bursts) {sapply(bursts, segment.popen)}
#' @export
bursts.pcloseds <- function (bursts) {sapply(bursts, segment.pclosed)}


#' Imposes a deadtime to a burst by applying segment.impose_deadtime to each segment in a burst
#'
#' The user specifies a deadtime in microseconds. The function applies
#' segment.impose_deadtime to each segment in the burst.
#' (See segment.impose_deadtime for details.)
#'
#' @param burst, a burst containing segments of dwells and states.
#' @param dead_time, the briefest possible event in microseconds.
#' @return A modified copy of the original burst
#' @export
bursts.impose_deadtime <- function(burst, dead_time){

b2 = copy(burst)

for (i in 1:length(b2)){b2[[i]] = segment.impose_deatime(b2[[i]],dead_time)}

return(b2)
}


#' Imposes a fixed conductance level (0 or 1) to all dwells with subconductance levels to each segment in a burst by applying segment.subconductance_as.
#'
#' The user specifies the desired level ('open' or 'closed'). The function applies
#' segment.subconductance_as to each segment in the burst.
#' (See segment.subconductance_as for details.)
#'
#' @param burst, a burst containing segments of dwells and states.
#' @param level, either 'open' or 'closed'
#' @return A modified copy of the original burst
#' @export
bursts.subconductance_as <- function(burst, level){

b2 = copy(burst)

for (i in 1:length(b2)){b2[[i]] = segment.subconductance_as(b2[[i]],level)}

return(b2)
}
110 changes: 110 additions & 0 deletions R/segment.R
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,113 @@ segment.verify <- function (segment) {
## Otherwise is OK
return(TRUE)
}


#' Collapses a segment into dwells with alternating conductance level.
#'
#' Segments may contain consecutive dwells with the same conductance level.
#' Consecutives_to_dwells sums together all consecutive dwells with the same
#' conductance level. The result is a segment containing dwells that alternate
#' in conductance level (i.e. 1,0,1,0,1,...)
#'
#' @param segment The dwells and states table
#' @return A modified copy of the original segment
#' @export
segment.consecutives_to_dwells <- function(segment){

s2 = copy(segment)
d = s2$dwells
s = s2$states
sd = list(vector(),vector())
l = length
c = 1
i = 1


while(i < l(s)){
if(s[i] == s[i+1]){
d[i+1] = d[i+1]+d[i]
if((i+1) == l(s)){
sd[[1]] = append(sd[[1]],s[i+1])
sd[[2]] = append(sd[[2]],d[i+1])}
i = i+1}
else if(s[i]!= s[i+1]){
sd[[1]] = append(sd[[1]],s[i])
sd[[2]] = append(sd[[2]],d[i])
if((i+1) == l(s)){
sd[[1]] = append(sd[[1]],s[i+1])
sd[[2]] = append(sd[[2]],d[i+1])}
i = i+1}
}

s2$dwells = sd[[2]]
s2$states = sd[[1]]

return(s2)
}


#' Imposes a deadtime to a segment by removing any dwell that is shorter than the deadtime.
#'
#' The user specifies a deadtime in microseconds. The function effectively undoes
#' the work of the event detection algorithm by reverting the conductance level
#' (of the brief dwell) back to the previous conductance level in the time sequence.
#' The function then returns a collapsed segment containing alternating dwells.
#' (See segment.consecutives_to_dwells for details about the collapsed segment.)
#'
#'
#' @param segment, the segment containing dwells and states.
#' @param dead_time, the briefest possible event in microseconds.
#' @return A modified copy of the original segment
#' @export
segment.impose_deadtime <- function(segment,dead_time){

s2 = copy(segment)
d = s2$dwells
s = s2$states
dt = dead_time / 1e6

#If first dwell is < dead_time, then set the conductance level to 0.
if (d[[1]] < dt){s[[1]] = 0}

#For all other dwells in the segment, if the dwell is < dead_time, change the
#conductance level to the previous (in the time sequence) dwell's
#conductance level.
for (i in 2:length(d)){if (d[[i]] < dt){s[[i]] = s[[i-1]]}}

#returns a collapsed segment of dwells with alternating conductance levels.
return(segment.consecutives_to_dwells(s2))
}

#' Imposes a fixed conductance level (0 or 1) to all dwells with subconductance levels.
#'
#' The user specifies the desired level ('open' or 'closed'). The function will modify
#' any subconductance level (that is not 0 or 1) to be the desired level 1 for 'open'
#' or 0 for 'closed'. The function then reutrns a collapsed segment containing
#' alternating dwells.
#' (See segment.consecutives_to_dwells for details about the collapsed segment.)
#'
#' @param segment, the segment containing dwells and states.
#' @param level, either 'open' or 'closed'
#' @return A modified copy of the original segment
#' @export
segment.subconductance_as <- function(segment,level){

s2 = copy(segment)
d = s2$dwells
s = s2$states


#Sets desired conductance level to 1 or zero depending on users' choice.
#Returns a warning message if level is not 'open' or 'closed'
if (level == 'open') {l = 1}
else if (level == 'closed') {l = 0}
else {return('Conductance level must be either \'open\' or \'closed\'.')}

#For all the dwells in the segment, if the conductance level is not a 1 or a 0,
#then set the conductance level as the desired level l.
for (i in 1:length(s)){if ((s[[i]] != 0) & (s[[i]] != 1)){s[[i]] = l}}

#Returns a collapsed segment of dwells with alternating conductance levels.
return(segment.consecutives_to_dwells(s2))
}
21 changes: 21 additions & 0 deletions man/bursts.impose_deadtime.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions man/bursts.subconductance_as.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions man/segment.consecutives_to_dwells.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/segment.impose_deadtime.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/segment.subconductance_as.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.