From f2015bdf889cbb5a135360634729df66ceacb18f Mon Sep 17 00:00:00 2001 From: x Date: Mon, 24 Jun 2019 14:31:38 -0400 Subject: [PATCH] impose_deadtime and subconductance_as functions added. --- NAMESPACE | 5 ++ R/bursts.R | 39 +++++++++ R/segment.R | 110 ++++++++++++++++++++++++++ man/bursts.impose_deadtime.Rd | 21 +++++ man/bursts.subconductance_as.Rd | 21 +++++ man/segment.consecutives_to_dwells.Rd | 20 +++++ man/segment.impose_deadtime.Rd | 23 ++++++ man/segment.subconductance_as.Rd | 23 ++++++ 8 files changed, 262 insertions(+) create mode 100644 man/bursts.impose_deadtime.Rd create mode 100644 man/bursts.subconductance_as.Rd create mode 100644 man/segment.consecutives_to_dwells.Rd create mode 100644 man/segment.impose_deadtime.Rd create mode 100644 man/segment.subconductance_as.Rd diff --git a/NAMESPACE b/NAMESPACE index 07c2010..1803d9f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -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) @@ -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) diff --git a/R/bursts.R b/R/bursts.R index 075c61b..6d9eaa6 100644 --- a/R/bursts.R +++ b/R/bursts.R @@ -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) +} diff --git a/R/segment.R b/R/segment.R index e17202b..61158e3 100644 --- a/R/segment.R +++ b/R/segment.R @@ -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)) +} diff --git a/man/bursts.impose_deadtime.Rd b/man/bursts.impose_deadtime.Rd new file mode 100644 index 0000000..9728686 --- /dev/null +++ b/man/bursts.impose_deadtime.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/bursts.R +\name{bursts.impose_deadtime} +\alias{bursts.impose_deadtime} +\title{Imposes a deadtime to a burst by applying segment.impose_deadtime to each segment in a burst} +\usage{ +bursts.impose_deadtime(burst, dead_time) +} +\arguments{ +\item{burst, }{a burst containing segments of dwells and states.} + +\item{dead_time, }{the briefest possible event in microseconds.} +} +\value{ +A modified copy of the original burst +} +\description{ +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.) +} diff --git a/man/bursts.subconductance_as.Rd b/man/bursts.subconductance_as.Rd new file mode 100644 index 0000000..a0bd907 --- /dev/null +++ b/man/bursts.subconductance_as.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/bursts.R +\name{bursts.subconductance_as} +\alias{bursts.subconductance_as} +\title{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.} +\usage{ +bursts.subconductance_as(burst, level) +} +\arguments{ +\item{burst, }{a burst containing segments of dwells and states.} + +\item{level, }{either 'open' or 'closed'} +} +\value{ +A modified copy of the original burst +} +\description{ +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.) +} diff --git a/man/segment.consecutives_to_dwells.Rd b/man/segment.consecutives_to_dwells.Rd new file mode 100644 index 0000000..705b38a --- /dev/null +++ b/man/segment.consecutives_to_dwells.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/segment.R +\name{segment.consecutives_to_dwells} +\alias{segment.consecutives_to_dwells} +\title{Collapses a segment into dwells with alternating conductance level.} +\usage{ +segment.consecutives_to_dwells(segment) +} +\arguments{ +\item{segment}{The dwells and states table} +} +\value{ +A modified copy of the original segment +} +\description{ +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,...) +} diff --git a/man/segment.impose_deadtime.Rd b/man/segment.impose_deadtime.Rd new file mode 100644 index 0000000..752227c --- /dev/null +++ b/man/segment.impose_deadtime.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/segment.R +\name{segment.impose_deadtime} +\alias{segment.impose_deadtime} +\title{Imposes a deadtime to a segment by removing any dwell that is shorter than the deadtime.} +\usage{ +segment.impose_deadtime(segment, dead_time) +} +\arguments{ +\item{segment, }{the segment containing dwells and states.} + +\item{dead_time, }{the briefest possible event in microseconds.} +} +\value{ +A modified copy of the original segment +} +\description{ +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.) +} diff --git a/man/segment.subconductance_as.Rd b/man/segment.subconductance_as.Rd new file mode 100644 index 0000000..7f284af --- /dev/null +++ b/man/segment.subconductance_as.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/segment.R +\name{segment.subconductance_as} +\alias{segment.subconductance_as} +\title{Imposes a fixed conductance level (0 or 1) to all dwells with subconductance levels.} +\usage{ +segment.subconductance_as(segment, level) +} +\arguments{ +\item{segment, }{the segment containing dwells and states.} + +\item{level, }{either 'open' or 'closed'} +} +\value{ +A modified copy of the original segment +} +\description{ +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.) +}