text:
include("model.jl")
include("sampler.jl")
include("simulations.jl")
include("ABC_Gillespie.jl")
include("distances.jl")

using DataFrames
using Gadfly
using Compose
using Colors
using JLD
using MultivariateStats
using RCall
using EntropyEstimators

""" save results of ABC rejection or ABC SMC (each population is saved in a separated directory)

"""
function save_results(results::Array{ABC_population,1},model::ABC_Model,path::ASCIIString)

	#save the priors into a txt and jld file
    writedlm(string(path,"/priors.txt"),model.priors)
    jldopen(string(path,"/priors.jld"),"w") do file
        write(file,"priors",model.priors)
    end

    #save the particles from each population into a file
    for i in 1:length(results)

        #save_pop(i,results[i],model,path)
        compare_prior_posterior(path,pop_num=i)

    end

end

"""
save the results of one population
"""
function save_pop(pop_num::Int64,pop::ABC_population,model::ABC_Model,path::ASCIIString)

    #create population directory (only if non existing)
    pop_dir = string(path,"population",pop_num)
    run(`mkdir -p $pop_dir`)

    #save the variables of the current population into a .jld file that can be later loaded
    jldopen(string(pop_dir,"/pop$pop_num.jld"),"w") do file
       write(file,"distances",pop.distances)
       write(file,"weights",pop.weights)
       write(file,"particles",pop.particles)
       write(file,"epsilon",pop.epsilon)
       write(file,"num_steps",pop.num_steps)
    end

    #open new file for writing
    pop_file = open(string(pop_dir,"/particles.txt"), "w")

    particles = pop.particles

    for j in 1:length(particles)
        write(pop_file,string(particles[j]), "\n")
    end

    close(pop_file)

    #save weights and particles
    weights_file = string(pop_dir,"/weights.txt")
    dis_file = string(pop_dir,"/distances.txt")
    writedlm(weights_file,pop.weights)
    writedlm(dis_file,pop.distances)

    df = DataFrame()
    all_plots = Array{Gadfly.Plot,1}(model.num_params*model.num_params)
    #for models with many parameters, create a plot with the histograms only
    hist_plots = Array{Gadfly.Plot,1}(model.num_params)
    df_cols = collect(1:model.num_params)

    hist_plot_num = 1
    scatter_plot_num = 1

    cols = round(Int,model.num_params/2)
    if (model.num_params%2)==1
        cols+=1
    end

    rows = 2

    if model.num_params==3
    	rows=1
    end

    for j in 1:model.num_params

        #save all the plots of the current parameter into one plot
        par_plots = Array{Gadfly.Plot,1}(model.num_params)

        #get all the current parameter values
        p_vals = [particles[k][j] for k in 1:length(particles)]

        #add them to the dataframe
        insert!(df,j,p_vals,symbol("parameter$j"))

        #generate a histogram plot
        plot_j = plot(x=p_vals,Geom.histogram(bincount=10),
                        Guide.xlabel(string("Parameter ",j)),
                        Guide.ylabel("Frequency"),
                        Guide.title("Parameters Histogram")
                        )
        all_plots[hist_plot_num] = plot_j
        hist_plots[j] = plot_j
        par_plots[j] = plot_j
        hist_plot_num += (model.num_params+1)

        #generate scatter plots with all other parameters
        for n in 1:model.num_params
            if n!=j

                #only create scatter plots with other parameters
                n_vals = [particles[k][n] for k in 1:length(particles)]
                scatter_j_n = plot(x=p_vals,y=n_vals,Geom.point,
                                   Guide.xlabel(string("Parameter ",j)),
                                   Guide.ylabel(string("Parameter ",n)))
                all_plots[scatter_plot_num] = scatter_j_n
                par_plots[n] = scatter_j_n
                scatter_plot_num +=1
            else
                scatter_plot_num +=1
            end
        end

        #save the plots of the current parameter into one plot
        cs = transpose(reshape([Context[render(par_plots[i]) for i in 1:length(par_plots)]], cols,rows))
        p = gridstack(cs)
        draw(PDF(string(pop_dir,"/parameter_",j,"_plots.pdf"),18inch,10inch),p)

    end

    #save the plots into one plot
    cs = transpose(reshape([Context[render(all_plots[i]) for i in 1:length(all_plots)]], model.num_params,model.num_params))
    p = gridstack(cs)
    draw(PDF(string(pop_dir,"/parameters_histograms.pdf"),12inch,12inch),p)

    #save only histogram plots
    cs = transpose(reshape([Context[render(hist_plots[i]) for i in 1:length(hist_plots)]], cols,rows))
    h = gridstack(cs)
    draw(PDF(string(pop_dir,"/parameters_histograms_only.pdf"),18inch,10inch),h)

    #save the dataframe into a file
    writetable(string(pop_dir,"/particles_df.csv"),df)

    #plot the weighted posteriror distributions
    plot_weighted_posterior(pop_dir)
    #TODO: change so that this is done during the loop, create a function for making the plots and call it twice

    #PCA analysis
    #robustness_analysis(pop_dir)
end

"""
plot the data such that each variable is shown over time
"""
function plot_data(data::Array{Array{Float64,1},1},time_points::FloatRange{Float64},num_params::Int64,path::ASCIIString)

    df = DataFrame()
    df_cols = collect(1:num_params)
    p_layers = Layer[]
    p_colors = distinguishable_colors(num_params)
    times = collect(time_points)

    """p = plot(Guide.XLabel("Time"),
                 Guide.YLabel("Value"),
                 Guide.Title("Data"))
    """
    for j in 1:num_params
        #get all the current parameter values
        vals = [data[k][j] for k in 1:length(data)]

        #add them to the dataframe
        insert!(df,j,vals,symbol("var$j"))

        #create the layer
        l = layer(x=times,y=vals,Geom.point,Theme(default_point_size=8px,default_color=color(p_colors[j])))
        push!(p_layers,l[1])
        #Gadfly.add_plot_element!(p,l)
    end

    p = plot(p_layers,Guide.XLabel("Time"),
                     Guide.YLabel("Value"),
                     Guide.Title("Data"),
                     Guide.manual_color_key("Variable", [string("var",i) for i in 1:num_params], p_colors))

    draw(PDF(string(path,"data.pdf"),12inch,12inch),p)

end

"""
plot random walks
"""
function plot_random_walks(walks::Array{Random_Walk,1},path::ASCIIString;walks_title="Random Walks")

    p_layers = Array{Layer,1}(length(walks))
    walk_color = distinguishable_colors(length(walks))

    for i in 1:length(walks)

        w = walks[i]
        points= w.steps_points

        point_x = [points[i][1] for i in 1:length(points)]
        point_y = [points[i][2] for i in 1:length(points)]

        #l = layer(x=point_x,y=point_y,Geom.line)
        l = layer(x=point_x,y=point_y,Geom.path(),Theme(default_color=walk_color[i]))
        p_layers[i] = l[1]
    end

    p = plot(p_layers,Guide.XLabel("X"),
                     Guide.YLabel("Y"),
                     Guide.Title(walks_title),
                     Coord.Cartesian(xmin=-5, xmax=5, ymin=-10, ymax=5)
                     )

                    #Coord.Cartesian(xmin=-5, xmax=5, ymin=-20, ymax=0)

    draw(PDF(string(path,"random_walks.pdf"),12inch,12inch),p)

    return p

end

"""plotting function for transition matrix"""
function my_spy(M::AbstractMatrix, elements::Gadfly.ElementOrFunction...; mapping...)
    is, js, values = Gadfly._findnz(x->!isnan(x), M)
    n,m = size(M)
    df = DataFrames.DataFrame(i=is, j=js, value=values)
    plot(df, x=:j, y=:i, color=:value,
        Coord.cartesian(fixed=true, xmin=0.5, xmax=m+.5, ymin=0.5, ymax=n+.5),
        #Scale.color_continuous(minvalue=0.0, maxvalue=0.015),
        Scale.color_continuous,
        Geom.rectbin,
        Scale.x_continuous,
        Scale.y_continuous,
        elements...; mapping...)
end

"""
plot transition matrixes on the same figure
"""
function plot_TMs(TM_arr::Array{Array{Float64,2},1},path::ASCIIString,particle_num::Int64,pop_num::Int64;num_groups::Int64=4)

    #create population directory (only if non existing)
    pop_dir = string(path,"population",pop_num)
    run(`mkdir -p $pop_dir`)

    TM_plots = Array{Gadfly.Plot,1}(length(TM_arr))

    for i in 1:length(TM_arr)

        #plot the transition matrix
        T_plot = my_spy(TM_arr[i],Guide.xlabel(""),Guide.ylabel(""))
        TM_plots[i] = T_plot

    end

    #plot all the TMs in one plot
    cs = transpose(reshape([Context[render(TM_plots[i]) for i in 1:length(TM_plots)]], num_groups,num_groups))
    p = gridstack(cs)
    title = compose(context(0, 0, 1w, 0.25inch),text(0.5, 1.0, "Time after Injury / Distance from Wound", hcenter, vbottom))
    draw(PDF(string(pop_dir,"/TMs_particle",particle_num,".pdf"),12inch,12inch),vstack(title, p))

end

"""
robustness analysis - PCA on correlation matrix
"""
function robustness_analysis(path::ASCIIString)

    # particles = string(path,"particles_df.csv")

    # weights = string(path,"weights.txt")

    # #open the DF of the particles
    # post_dist_df = readtable(particles)

    # #open the DF of the weights
    # weights_df = readtable(weights,header=false)
    # weights_vec = Array(weights_df[:,1])

    # #get the wieghted posterior
    # post_dist_weighted = weighted_posterior(post_dist_df,weights_vec)

    # """
    # #get the correlation matrix from R - using the weights
    # cor_mat = rcopy(R"cov.wt($post_dist_df,wt=$weights_vec,cor=TRUE)$cor")

    # #run PCA using R
    # #cor_mat_pca_sdev = rcopy(R"princomp($cor_mat,cor=T)$sdev")
    # cor_mat_pca_sdev = rcopy(R"prcomp($cor_mat,cor=T)$sdev")

    # #cor_mat_pca_loadings = rcopy(R"princomp($cor_mat,cor=T)$loadings")
    # cor_mat_pca_loadings = rcopy(R"prcomp($cor_mat,cor=T)$rotation")

    # PCA_vars = cor_mat_pca_sdev

    # PCA_comps = cor_mat_pca_loadings
    # """

    #get the weighted posterior
    post_dist_weighted = readtable(string(path,"/weighted_particles_df.csv"))

    #get the correlation matrix
    post_dist_cor = cor(DataMatrix(post_dist_weighted))

    #run PCA on the correlation matrix
    post_dist_PCA = fit(PCA,Array(post_dist_cor))

    #get the variances of the components
    PCA_vars = principalvars(post_dist_PCA)

    #get the components
    PCA_comps = projection(post_dist_PCA)

    #plot the variances for the first 5 components
    comps = ["PC1","PC2","PC3","PC4","PC5"]
    p = plot(x=comps,y=PCA_vars[1:5],Geom.bar,
                     Guide.XLabel("Principal Components"),
                     Guide.YLabel("Variance"),
                     Guide.Title("Correlation Matrix PCA")
                     )

    draw(PDF(string(path,"/PCA_variance.pdf"),12inch,12inch),p)

    #parameters
    pars = ["w","mb","b0","mp","p0","A","D","tau0","Kd","R0"]

    #plot the first component for all parameters
    pc1=PCA_comps[:,1]
    p = plot(x=pars,y=pc1,Geom.bar,
                     Guide.XLabel(""),
                     Guide.YLabel("PC1"),
                     Guide.Title("Sloppy")
                     )

    draw(PDF(string(path,"/PCA_PC1.pdf"),12inch,12inch),p)

    #plot the fifth component for all parameters
    pc5=PCA_comps[:,5]
    p = plot(x=pars,y=pc5,Geom.bar,
                     Guide.XLabel(""),
                     Guide.YLabel("PC5"),
                     Guide.Title("Stiff")
                     )

    draw(PDF(string(path,"/PCA_PC5.pdf"),12inch,12inch),p)

    """
    #get the projection
    amount = zeros(size(PCA_comps))
    for i in 1:size(PCA_comps)[1]
        #get the sum of the ith row
        #PC_sum = sum(PCA_comps[i,:].^2)
        for j in 1:size(PCA_comps)[2]
            #get the sum of the jth col
            PC_sum = sum(PCA_comps[:,j].^2)
            amount[i,j] = (PCA_comps[i,j]^2) / PC_sum
        end
    end

    #plot the first and fifth projections

    #plot the first component for all parameters
    pc1=amount[:,1]
    p = plot(x=pars,y=pc1,Geom.bar,
                     Guide.XLabel(""),
                     Guide.YLabel("PC1"),
                     Guide.Title("Sloppy")
                     )

    draw(PDF(string(path,"PCA_PC1_projection.pdf"),12inch,12inch),p)

    #plot the fifth component for all parameters
    pc5=amount[:,5]
    p = plot(x=pars,y=pc5,Geom.bar,
                     Guide.XLabel(""),
                     Guide.YLabel("PC5"),
                     Guide.Title("Stiff")
                     )

    draw(PDF(string(path,"PCA_PC5_projection.pdf"),12inch,12inch),p)
    """

end

"""
sample from posterior population accodring to weights
"""
function weighted_posterior(particles::DataFrames.DataFrame,weights::Array{Float64,1})

        sampled_particles = DataFrame()

        for j in 1:size(particles)[2]
            #sample each of the parameters vector
            par_vec = particles[:,j]
            sampled = StatsBase.sample(par_vec,WeightVec(weights),size(particles)[1])
            insert!(sampled_particles,j,sampled,symbol("parameter$j"))
        end

        return sampled_particles
end

"""
plot weighted posterior distributions
"""
function plot_weighted_posterior(path::ASCIIString)

    #get the particles and weights
    particles = readtable(string(path,"/particles_df.csv"))
    weights_df = readtable(string(path,"/weights.txt"),header=false)
    weights= Array(weights_df[:,1])

    #get the weighted posterior
    weighted_particles = weighted_posterior(particles,weights)

    #save the weighted particles
    writetable(string(path,"/weighted_particles_df.csv"),weighted_particles)

    num_params = size(particles)[2]

    all_plots = Array{Gadfly.Plot,1}(num_params*num_params)
    #for models with many parameters, create a plot with the histograms only
    hist_plots = Array{Gadfly.Plot,1}(num_params)

    hist_plot_num = 1
    scatter_plot_num = 1

    cols = round(Int,num_params/2)
    if (num_params%2)==1
        cols+=1
    end

    rows=2
    if num_params==3
    	rows=1
    end

    for j in 1:num_params

        #save all the plots of the current parameter into one plot
        par_plots = Array{Gadfly.Plot,1}(num_params)

        #get all the current parameter values
        p_vals = weighted_particles[:,j]

        #generate a histogram plot
        plot_j = plot(x=p_vals,Geom.histogram(bincount=10),
                        Guide.xlabel(string("Parameter ",j)),
                        Guide.ylabel("Frequency"),
                        Guide.title("Weighted Parameters Histogram")
                        )
        all_plots[hist_plot_num] = plot_j
        hist_plots[j] = plot_j
        par_plots[j] = plot_j
        hist_plot_num += (num_params+1)

        #generate scatter plots with all other parameters
        for n in 1:num_params
            if n!=j

                #only create scatter plots with other parameters
                n_vals = weighted_particles[:,n]
                scatter_j_n = plot(x=p_vals,y=n_vals,Geom.point,
                                   Guide.xlabel(string("Parameter ",j)),
                                   Guide.ylabel(string("Parameter ",n)))
                all_plots[scatter_plot_num] = scatter_j_n
                par_plots[n] = scatter_j_n
                scatter_plot_num +=1
            else
                scatter_plot_num +=1
            end
        end

        #save the plots of the current parameter into one plot
        cs = transpose(reshape([Context[render(par_plots[i]) for i in 1:length(par_plots)]], cols,rows))
        p = gridstack(cs)
        draw(PDF(string(path,"/weighted_parameter_",j,"_plots.pdf"),18inch,10inch),p)

    end

    #save the plots into one plot
    cs = transpose(reshape([Context[render(all_plots[i]) for i in 1:length(all_plots)]], num_params,num_params))
    p = gridstack(cs)
    draw(PDF(string(path,"/weighted_parameters_histograms.pdf"),12inch,12inch),p)

    #save only histogram plots
    cs = transpose(reshape([Context[render(hist_plots[i]) for i in 1:length(hist_plots)]], cols,rows))
    h = gridstack(cs)
    draw(PDF(string(path,"/weighted_parameters_histograms_only.pdf"),18inch,10inch),h)

end

"""
compare prior and posterior distribution using Entropy
"""
function compare_prior_posterior(path::ASCIIString;pop_num=5,estimator="shrinkage")

    #get the prior distributions
    priors = jldopen(string(path,"/priors.jld"),"r") do file
        read(file, "priors")
    end

    pop_dir = string(path,"/population",pop_num,"/")

    #get the particles
    particles = readtable(string(pop_dir,"/particles_df.csv"))

    #get the weighted particles
    weighted_particles = readtable(string(pop_dir,"/weighted_particles_df.csv"))

    #TODO: check what happends with weighted particles

    entropy_arr = zeros(size(particles)[2])

    #go over each parameter
    for i in 1:size(particles)[2]
        #create the prior distribution
        prior_dist = rand(priors[i],size(particles)[1])

        #get the posterior distribution
        post_dist = particles[:,i]

        #get the entropy using the requsted estimate
        #prior_post_entropy = get_entropy(prior_dist, post_dist)
        prior_ent = get_entropy(prior_dist)
        post_ent = get_entropy(post_dist)
        entropy_arr[i] = prior_ent - post_ent

    end

    #plot the entropy values
    pars = ["w","mb","b0","mp","p0","A","D","tau0","Kd","R0"]
    p = plot(x=pars,y=entropy_arr,Geom.bar,
                     Guide.XLabel(""),
                     Guide.YLabel("Entropy"),
                     Guide.Title("Prior Posterior Entropy")
                     )

    draw(PDF(string(pop_dir,"/entropy.pdf"),12inch,12inch),p)

end


text:
			Posted on

			by

redpajama_set_name: RedPajamaGithub


text:
package permissions.dispatcher.test;

import android.Manifest;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.RuntimePermissions;

@RuntimePermissions
public class ActivityWithOnNeverAskAgain extends AppCompatActivity {
    @NeedsPermission(Manifest.permission.CAMERA)
    void showCamera() {
    }

    @OnNeverAskAgain(Manifest.permission.CAMERA)
    void showNeverAskForCamera() {
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        ActivityWithOnNeverAskAgainPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }
}


text:
import itertools
#generate binary states and sort by number of flipped tiles
n=9
N=2**n
states_init = list(itertools.product([0, 1], repeat=n))
total =[]
for y in states_init:
    total.append(sum(y))
states= [x for _,x in sorted(zip(total,states_init))]

# function to generate all subsets of a given set
def powerset(items):
    combo = []
    for a in range(len(items) + 1):
        combo.append(list(itertools.combinations(items,a)))
    return combo
# function to determine move that results in highest probability subsequent state
def find_best_next_state(state_num, roll):
    unflipped_combos = powerset([d+1 for d,val in enumerate(states[state_num]) if val==0])
    s = -1
    p_max = 0
    for c in unflipped_combos:
        for t in c:
            if sum(t) != roll:
                continue
            state_test = list(states[state_num])
            for e in list(t):
                state_test[e-1] = 1
            state_num_test = states.index(tuple(state_test))
            if prob[state_num_test] > p_max:
                p_max = prob[state_num_test]
                s = state_num_test
    return s
# start at final state and work backward, determining if one or two die rolls is optimal
prob = [0]*N
die = [1]*N
prob[N-1] = 1
for i in range(N-2,-1,-1):
    p1 = p2 = 0
    for j in range(6):
        for k in range(6):
            s = find_best_next_state(i,j+k+2)
            if s != -1:
                p2 += prob[s] / 36
    for j in range(6):
            s = find_best_next_state(i,j+1)
            if s != -1:
                p1 += prob[s] / 6
    prob[i] = max(p1,p2) #forcing 2 rolls for 7/8/9 doesnt change answer
    die[i] += 1 * (p2 > p1)#keep track of which number of die rolls is optimal
print("Probablity of finishing: ", prob[0])


text:
use std::fmt::Formatter;
use std::marker::PhantomData;
use std::mem::size_of;
use std::ops::Deref;
use std::slice;

use coremidi_sys::{
    MIDIEventList, MIDIEventListAdd, MIDIEventListInit, MIDIEventPacket, MIDIEventPacketNext,
};

use crate::protocol::Protocol;

pub type Timestamp = u64;

/// A variable-length list of MIDI event packets
/// See [MIDIEventList](https://developer.apple.com/documentation/coremidi/midieventlist)
///
pub struct EventList(MIDIEventList);

impl EventList {
    pub fn protocol(&self) -> Protocol {
        Protocol::from(self.0.protocol)
    }

    /// Check if the packet list is empty.
    ///
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get the number of packets in the list.
    ///
    pub fn len(&self) -> usize {
        self.0.numPackets as usize
    }

    /// Get an iterator for the packets in the list.
    ///
    pub fn iter(&self) -> EventListIter {
        EventListIter {
            count: self.len(),
            packet_ptr: std::ptr::addr_of!(self.0.packet) as *const MIDIEventPacket,
            _phantom: PhantomData,
        }
    }

    /// For internal usage only.
    /// Requires this instance to actually point to a valid MIDIEventList
    pub(crate) unsafe fn as_ptr(&self) -> *const MIDIEventList {
        self as *const EventList as *const MIDIEventList
    }
}

impl std::fmt::Debug for EventList {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        writeln!(
            f,
            "EventList(protocol={:?}, packets={})",
            self.protocol(),
            self.len()
        )?;
        for packet in self.iter() {
            writeln!(f, "{:?}", packet)?;
        }
        Ok(())
    }
}

pub struct EventListIter<'a> {
    count: usize,
    packet_ptr: *const MIDIEventPacket,
    _phantom: PhantomData<&'a MIDIEventPacket>,
}

impl<'a> Iterator for EventListIter<'a> {
    type Item = &'a EventPacket;

    fn next(&mut self) -> Option<&'a EventPacket> {
        if self.count > 0 {
            let packet = unsafe { &*(self.packet_ptr as *const EventPacket) };
            self.count -= 1;
            self.packet_ptr = unsafe { MIDIEventPacketNext(self.packet_ptr) };
            Some(packet)
        } else {
            None
        }
    }
}

pub struct EventPacket(MIDIEventPacket);

impl EventPacket {
    pub fn timestamp(&self) -> Timestamp {
        self.0.timeStamp as Timestamp
    }

    /// Get the packet data. This method just gives raw MIDI words. You would need another
    /// library to decode them and work with higher level events.
    ///
    pub fn data(&self) -> &[u32] {
        let data_ptr = self.0.words.as_ptr();
        let data_len = self.0.wordCount as usize;
        unsafe { slice::from_raw_parts(data_ptr, data_len) }
    }
}

impl std::fmt::Debug for EventPacket {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "  {:024}:", self.timestamp())?;
        for word in self.data().iter() {
            write!(f, " {:08x}", word)?;
        }
        Ok(())
    }
}

#[derive(Clone)]
pub struct EventBuffer {
    storage: Storage,
    current_packet_offset: usize,
}

impl EventBuffer {
    const PACKET_HEADER_SIZE: usize = 8 +     // MIDIEventPacket::timestamp: MIDITimeStamp/UInt64
                                      4; // MIDIEventPacket::wordCount: UInt32

    /// Create an empty `EventBuffer` for a given [Protocol] without allocating.
    ///
    pub fn new(protocol: Protocol) -> Self {
        Self::with_capacity(Storage::INLINE_SIZE, protocol)
    }

    /// Create an empty `EventBuffer` of a given capacity for a given [Protocol].
    ///
    pub fn with_capacity(capacity: usize, protocol: Protocol) -> Self {
        let mut storage = Storage::with_capacity(capacity);
        let event_list_ptr = unsafe { storage.as_mut_ptr::<MIDIEventList>() };
        let current_packet_ptr = unsafe { MIDIEventListInit(event_list_ptr, protocol.into()) };
        let current_packet_offset = unsafe {
            (current_packet_ptr as *const u8).offset_from(event_list_ptr as *const u8) as usize
        };
        Self {
            storage,
            current_packet_offset,
        }
    }

    /// Get underlying buffer capacity in bytes
    ///
    pub fn capacity(&self) -> usize {
        self.storage.capacity()
    }

    /// Add a new packet containing the provided timestamp and data.
    /// It consumes the instance and returns it modified with the new packet.
    ///
    /// See [EventBuffer::push] for further details.
    ///
    /// Example:
    ///
    /// ```
    /// use coremidi::{Protocol, Timestamp, EventBuffer};
    ///
    /// let buffer = EventBuffer::new(Protocol::Midi20)
    ///     .with_packet(0, &[0x40903c00, 0xffff0000]); // Note On for Middle C
    ///
    /// assert_eq!(buffer.len(), 1);
    /// assert_eq!(
    ///     buffer.iter()
    ///         .map(|packet| (packet.timestamp(), packet.data().to_vec()))
    ///         .collect::<Vec<(Timestamp, Vec<u32>)>>(),
    ///     vec![(0, vec![0x40903c00, 0xffff0000])],
    /// )
    /// ```
    pub fn with_packet(mut self, timestamp: Timestamp, data: &[u32]) -> Self {
        self.push(timestamp, data);
        self
    }

    /// Add a new event containing the provided timestamp and data.
    ///
    /// According to the official documentation for CoreMIDI, the timestamp represents
    /// the time at which the events are to be played, where zero means "now".
    /// The timestamp applies to the first MIDI word in the packet.
    ///
    /// An event must not have a timestamp that is smaller than that of a previous event
    /// in the same `EventBuffer`
    ///
    /// Example:
    ///
    /// ```
    /// use coremidi::{EventBuffer, Protocol, Timestamp};
    ///
    /// let mut buffer = EventBuffer::new(Protocol::Midi20);
    /// buffer.push(0, &[0x40903c00, 0xffff0000]); // Note On for Middle C
    ///
    /// assert_eq!(buffer.len(), 1);
    /// assert_eq!(
    ///     buffer.iter()
    ///         .map(|packet| (packet.timestamp(), packet.data().to_vec()))
    ///         .collect::<Vec<(Timestamp, Vec<u32>)>>(),
    ///     vec![(0, vec![0x40903c00, 0xffff0000])],
    /// )
    /// ```
    pub fn push(&mut self, timestamp: Timestamp, data: &[u32]) -> &mut Self {
        self.ensure_capacity(data.len());

        let packet_list_ptr = unsafe { self.storage.as_mut_ptr::<MIDIEventList>() };
        let current_packet_ptr = unsafe {
            self.storage.as_ptr::<u8>().add(self.current_packet_offset) as *mut MIDIEventPacket
        };
        let current_packet_ptr = unsafe {
            MIDIEventListAdd(
                packet_list_ptr,
                self.storage.capacity() as u64,
                current_packet_ptr,
                timestamp,
                data.len() as u64,
                data.as_ptr(),
            )
        };

        self.current_packet_offset = unsafe {
            (current_packet_ptr as *const u8).offset_from(packet_list_ptr as *const u8) as usize
        };

        self
    }

    /// Clears the buffer, removing all packets.
    /// Note that this method has no effect on the allocated capacity of the buffer.
    pub fn clear(&mut self) {
        let event_list_ptr = unsafe { self.storage.as_mut_ptr::<MIDIEventList>() };
        let protocol = unsafe { (*event_list_ptr).protocol };
        let current_packet_ptr = unsafe { MIDIEventListInit(event_list_ptr, protocol) };
        self.current_packet_offset = unsafe {
            (current_packet_ptr as *const u8).offset_from(event_list_ptr as *const u8) as usize
        };
    }

    fn ensure_capacity(&mut self, data_len: usize) {
        let next_capacity =
            self.aligned_bytes_len() + Self::PACKET_HEADER_SIZE + data_len * size_of::<u32>();

        unsafe {
            // We ensure capacity for the worst case as if there was no merge with the current packet
            self.storage.ensure_capacity(next_capacity);
        }
    }

    #[inline]
    fn aligned_bytes_len(&self) -> usize {
        let storage_start_ptr = unsafe { self.storage.as_ptr::<u8>() };
        if self.as_ref().is_empty() {
            self.current_packet_offset
        } else {
            let current_packet = unsafe {
                &*(self.storage.as_ptr::<u8>().add(self.current_packet_offset)
                    as *const MIDIEventPacket)
            };
            let current_packet_data_ptr = current_packet.words.as_ptr() as *const u8;
            let data_offset = current_packet_data_ptr as *const u8 as usize
                - storage_start_ptr as *const u8 as usize;
            let data_len = current_packet.wordCount as usize * size_of::<u32>();
            (data_offset + data_len + 3) & !3
        }
    }
}

impl AsRef<EventList> for EventBuffer {
    #[inline]
    fn as_ref(&self) -> &EventList {
        unsafe { &*self.storage.as_ptr::<EventList>() }
    }
}

impl Deref for EventBuffer {
    type Target = EventList;

    #[inline]
    fn deref(&self) -> &EventList {
        self.as_ref()
    }
}

#[derive(Clone)]
pub(crate) enum Storage {
    /// Inline stores the data directly on the stack, if it is small enough.
    /// NOTE: using u32 ensures correct alignment (required on ARM)
    Inline([u32; Storage::INLINE_SIZE / 4]),
    /// External is used whenever the size of the data exceeds INLINE_PACKET_BUFFER_SIZE.
    /// This means that the size of the contained vector is always greater than INLINE_PACKET_BUFFER_SIZE.
    External(Vec<u32>),
}

impl Storage {
    pub(crate) const INLINE_SIZE: usize = 8 // MIDIEventList header
        + 12 // MIDIEventPacket header
        + 4 * 4; // 4 words

    #[inline]
    #[allow(clippy::uninit_vec)]
    pub(crate) fn with_capacity(capacity: usize) -> Self {
        if capacity <= Self::INLINE_SIZE {
            Self::Inline([0; Self::INLINE_SIZE / 4])
        } else {
            let u32_len = ((capacity - 1) / 4) + 1;
            let mut buffer = Vec::with_capacity(u32_len);
            unsafe {
                buffer.set_len(u32_len);
            }
            Storage::External(buffer)
        }
    }

    #[inline]
    pub(crate) fn capacity(&self) -> usize {
        match *self {
            Storage::Inline(ref inline) => inline.len() * 4,
            Storage::External(ref vec) => vec.len() * 4,
        }
    }

    #[inline]
    pub(crate) fn get_slice<T>(&self) -> &[T] {
        unsafe {
            match *self {
                Storage::Inline(ref inline) => slice::from_raw_parts(
                    inline.as_ptr() as *const T,
                    inline.len() * size_of::<u32>() / size_of::<T>(),
                ),
                Storage::External(ref vec) => {
                    slice::from_raw_parts(vec.as_ptr() as *const T, vec.len() * 4 / size_of::<T>())
                }
            }
        }
    }

    /// Call this only with larger length values (won't make the buffer smaller)
    #[allow(clippy::uninit_vec)]
    pub(crate) unsafe fn ensure_capacity(&mut self, capacity: usize) {
        if capacity < Self::INLINE_SIZE || capacity < self.get_slice::<u8>().len() {
            return;
        }

        let vec_capacity = ((capacity - 1) / 4) + 1;
        let vec: Option<Vec<u32>> = match *self {
            Storage::Inline(ref inline) => {
                let mut v = Vec::with_capacity(vec_capacity);
                v.extend_from_slice(inline);
                v.set_len(vec_capacity);
                Some(v)
            }
            Storage::External(ref mut vec) => {
                let current_len = vec.len();
                vec.reserve(vec_capacity - current_len);
                vec.set_len(vec_capacity);
                None
            }
        };

        // to prevent borrow-check errors, this must come after the `match`
        if let Some(v) = vec {
            *self = Storage::External(v);
        }
    }

    #[inline]
    pub(crate) unsafe fn as_ptr<T>(&self) -> *const T {
        match *self {
            Storage::Inline(ref inline) => inline.as_ptr() as *const T,
            Storage::External(ref vec) => vec.as_ptr() as *const T,
        }
    }

    #[inline]
    pub(crate) unsafe fn as_mut_ptr<T>(&mut self) -> *mut T {
        self.as_ptr::<T>() as *mut T
    }
}

impl std::fmt::Debug for Storage {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        for b in self.get_slice::<u8>() {
            write!(f, " {:02x}", *b)?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use crate::events::{Storage, Timestamp};
    use crate::protocol::Protocol;
    use crate::{EventBuffer, EventList};
    use coremidi_sys::{
        kMIDIProtocol_2_0, ByteCount, MIDIEventList, MIDIEventListAdd, MIDIEventListInit,
        MIDIProtocolID,
    };

    #[test]
    fn event_list_accessors() {
        const BUFFER_SIZE: usize = 256;
        let buffer = [0u8; BUFFER_SIZE];
        let event_list_ptr = buffer.as_ptr() as *const MIDIEventList as *mut MIDIEventList;
        let event_packet_ptr =
            unsafe { MIDIEventListInit(event_list_ptr, kMIDIProtocol_2_0 as MIDIProtocolID) };
        let event_packet_ptr = unsafe {
            MIDIEventListAdd(
                event_list_ptr,
                BUFFER_SIZE as ByteCount,
                event_packet_ptr,
                10,
                2,
                [1, 2].as_ptr(),
            )
        };
        let _ = unsafe {
            MIDIEventListAdd(
                event_list_ptr,
                BUFFER_SIZE as ByteCount,
                event_packet_ptr,
                20,
                3,
                [3, 4, 5].as_ptr(),
            )
        };
        let event_list = unsafe { &*(event_list_ptr as *const EventList) };

        assert_eq!(event_list.protocol(), Protocol::Midi20);
        assert!(!event_list.is_empty());
        assert_eq!(event_list.len(), 2);

        assert_eq!(
            event_list
                .iter()
                .map(|packet| (packet.timestamp(), packet.data().to_vec()))
                .collect::<Vec<(Timestamp, Vec<u32>)>>(),
            vec![(10, vec![1, 2]), (20, vec![3, 4, 5]),]
        );
    }

    #[test]
    fn event_buffer_new() {
        let event_buffer = EventBuffer::new(Protocol::Midi20);

        assert_eq!(event_buffer.capacity(), Storage::INLINE_SIZE);
        assert_eq!(event_buffer.protocol(), Protocol::Midi20);
        assert_eq!(event_buffer.len(), 0);
    }

    #[test]
    fn event_buffer_with_capacity_inline() {
        let event_buffer = EventBuffer::with_capacity(2, Protocol::Midi20);

        assert_eq!(event_buffer.capacity(), Storage::INLINE_SIZE);
        assert_eq!(event_buffer.protocol(), Protocol::Midi20);
        assert_eq!(event_buffer.len(), 0);
    }

    #[test]
    fn event_buffer_with_capacity_external() {
        let event_buffer = EventBuffer::with_capacity(Storage::INLINE_SIZE * 2, Protocol::Midi20);

        assert_eq!(event_buffer.capacity(), Storage::INLINE_SIZE * 2);
    }

    #[test]
    fn event_buffer_with_packet() {
        let event_buffer = EventBuffer::new(Protocol::Midi20)
            .with_packet(10, &[1, 2])
            .with_packet(20, &[3, 4, 5]);

        assert_eq!(event_buffer.len(), 2);
        assert_eq!(
            event_buffer
                .iter()
                .map(|packet| (packet.timestamp(), packet.data().to_vec()))
                .collect::<Vec<(Timestamp, Vec<u32>)>>(),
            vec![(10, vec![1, 2]), (20, vec![3, 4, 5]),]
        );
    }

    #[test]
    fn event_buffer_push_within_capacity() {
        let mut event_buffer = EventBuffer::new(Protocol::Midi20);
        event_buffer.push(10, &[1, 2]).push(20, &[3, 4, 5]);

        assert_eq!(event_buffer.len(), 2);
        assert_eq!(
            event_buffer
                .iter()
                .map(|packet| (packet.timestamp(), packet.data().to_vec()))
                .collect::<Vec<(Timestamp, Vec<u32>)>>(),
            vec![(10, vec![1, 2]), (20, vec![3, 4, 5]),]
        );
    }

    #[test]
    fn event_buffer_push_over_capacity() {
        let mut event_buffer = EventBuffer::new(Protocol::Midi20);
        event_buffer
            .push(10, &[1, 2])
            .push(20, &[3, 4, 5, 6, 7, 8, 9, 10]);

        assert_eq!(event_buffer.len(), 2);
        assert_eq!(
            event_buffer
                .iter()
                .map(|packet| (packet.timestamp(), packet.data().to_vec()))
                .collect::<Vec<(Timestamp, Vec<u32>)>>(),
            vec![(10, vec![1, 2]), (20, vec![3, 4, 5, 6, 7, 8, 9, 10])]
        );
    }

    #[test]
    fn event_buffer_clear() {
        let mut event_buffer = EventBuffer::new(Protocol::Midi20).with_packet(10, &[1, 2]);

        assert_eq!(event_buffer.len(), 1);
        assert_eq!(
            event_buffer
                .iter()
                .map(|packet| (packet.timestamp(), packet.data().to_vec()))
                .collect::<Vec<(Timestamp, Vec<u32>)>>(),
            vec![(10, vec![1, 2])]
        );

        event_buffer.clear();

        assert_eq!(event_buffer.len(), 0);
        assert_eq!(event_buffer.capacity(), Storage::INLINE_SIZE);
        assert_eq!(
            event_buffer
                .iter()
                .map(|packet| (packet.timestamp(), packet.data().to_vec()))
                .collect::<Vec<(Timestamp, Vec<u32>)>>(),
            vec![]
        );
    }
}


text:
family_renames=/lscr2/andersenlab/kml436/git_repos2/Transposons2/files/round2_WB_familes_set2.txt
TE_lengths=/lscr2/andersenlab/kml436/git_repos2/Transposons2/files/SET2/LENGTHS/lengths.txt
consensus_renamed=/lscr2/andersenlab/kml436/git_repos2/Transposons2/files/SET2/AB-PR/consensus_wTC8.fasta
#consensus_renamed=/lscr2/andersenlab/kml436/git_repos2/Transposons2/files/SET2/LENGTHS/consensus.fasta
ref_tes=/lscr2/andersenlab/kml436/git_repos2/Transposons2/files/WB_pos_family_names.gff

minimal_Distance_to_count=1000
minimal_supporting_reads=3
minimal_supporting_individuals=1

bam_name=${1}
results_file=${2}

cd $bam_name
mkdir final_results
cd raw_results_temp_insertion

##############################################################################################
# 						PROCESSING TEMP INSERTION OUTPUT
##############################################################################################

echo "Processing TEMP output..."
rm ${bam_name}_temp_nonredundant.bed
rm ${bam_name}_temp_raw.bed
rm ${bam_name}_temp_redundant.bed

cp ${bam_name}.insertion.refined.bp.summary current_${bam_name}.insertion.refined.bp.summary
sed '1d' "current_${bam_name}.insertion.refined.bp.summary" | awk '{if ($4 == "sense" || $4 == "antisense"); else print $0}' | awk '{ printf $1"\t"$9"\t"$11"\t"$7"\t"$4"_non-reference_"sample"_temp_\t0\t"; if ( $5 == "sense" ) printf "+"; else printf "-"; print "\t"$6"\t"$10"\t"$12"\t"$8}' > "${bam_name}_temp_presort_raw.txt"
bedtools sort -i "${bam_name}_temp_presort_raw.txt" > "${bam_name}_temp_sorted_raw.txt"
awk '{ printf $1"\t"$2"\t"$3"\t"$4"\t"$5; if ($9 > 0 && $10 > 0) printf "sr_"; else printf "rp_"; print NR"\t"$6"\t"$7"\t"$8"\t"$9"\t"$10"\t"$11}' "${bam_name}_temp_sorted_raw.txt" > "${bam_name}_temp_sorted_precut_raw.txt"
awk '{ print $1"\t"$2"\t"$3"\t"$5"\t"$4"\t"$7"\t"$11}' "${bam_name}_temp_sorted_precut_raw.txt" >> "${bam_name}_temp_raw.bed"

echo "Filtering TEMP output......"
# Filter for insertions with support for both ends
awk '{if ($8 == "1p1") print $0}' "${bam_name}_temp_sorted_precut_raw.txt" > "${bam_name}_temp_sorted_redundant.txt"
cut -f1-3,5-7 "${bam_name}_temp_sorted_redundant.txt" >> "${bam_name}_temp_redundant.bed"
# Filter out redundant insertions
sort -k1,3 -k4rn "${bam_name}_temp_sorted_redundant.txt" | sort -k1,3 | awk '{ print $1"\t"$2"\t"$3"\t"$5"\t"$4"\t"$7"\t"$11}' > "${bam_name}_tmp"
bedtools sort -i "${bam_name}_tmp">> "${bam_name}_temp_nonredundant.bed"

# RENAME TRANSPOSON CALLS
#rename the TEs in the output tp match final consensus names
python /lscr2/andersenlab/kml436/git_repos2/Transposons2/scripts/correct_names_2_set2.py $family_renames ${bam_name}_temp_nonredundant.bed TMP
mv ${bam_name}_temp_nonredundant.bed ${bam_name}_nonredundant_org_copy.bed
mv TMP ${bam_name}_temp_nonredundant.bed
#rename chr X, ensure all lines contain "non-reference" (should already be the case for TEMP)
cat ${bam_name}_temp_nonredundant.bed| sed s'/chrX/X/g'| sed '/^track/d' | awk '$4 ~/non-reference/ && $4 !~ /TC8/ && $1 !~ /MtDNA/ {print $0}'> ${bam_name}_temp && mv ${bam_name}_temp ${bam_name}_temp_nonredundant.bed
cat ${bam_name}_temp_nonredundant.bed | awk '$7>.25 && $5>=8 {print $0}' > tmp && mv tmp ${bam_name}_temp_nonredundant.bed #rec_edit

# COLLAPSE TRANSPOSONS
#run collapse transposons script
python /lscr2/andersenlab/kml436/git_repos2/Transposons2/scripts/collapse_transposons.py $TE_lengths ${bam_name}_temp_nonredundant.bed
cp new_CT_${bam_name}_temp_nonredundant.bed ../final_results/${bam_name}_temp_insertion_nonredundant.bed

# COUNT TRANSPOSONS
#run te_totals.py script
end_tes=`grep -c "specifies an unknown reference name. Continue anyway." ../${bam_name}_TEMP_insertion_log.txt`
python /lscr2/andersenlab/kml436/git_repos2/Transposons2/scripts/te_totals.py new_CT_${bam_name}_temp_nonredundant.bed $consensus_renamed ${bam_name} $results_file new $end_tes
cd ..

##############################################################################################
# 						PROCESSING TEMP ABSENCE OUTPUT
##############################################################################################
cd raw_results_temp_absence
rm ${bam_name}_temp_nonredundant.bed
cat ${bam_name}.absence.refined.bp.summary | sed '1d' | awk -F $'\t' '{print $1"\t"$2"\t"$3"\t"$4"_reference_temp_rp_"NR"\t"$7"\t+"}' > ${bam_name}_temp_nonredundant.bed

# RENAME TRANSPOSON CALLS
#rename the TEs in the output tp match final consensus names
python /lscr2/andersenlab/kml436/git_repos2/Transposons2/scripts/correct_names_2_set2.py $family_renames ${bam_name}_temp_nonredundant.bed TMP
mv ${bam_name}_temp_nonredundant.bed ${bam_name}_nonredundant_org_copy.bed
mv TMP ${bam_name}_temp_nonredundant.bed
#rename chr X, ensure all lines contain "reference"
python /lscr2/andersenlab/kml436/git_repos2/Transposons2/scripts/process_double_deletion.py ${bam_name}_temp_nonredundant.bed $ref_tes
mv tmp_double_deletion.txt ${bam_name}_temp_nonredundant.bed
cat ${bam_name}_temp_nonredundant.bed| sed s'/chrX/X/g'| sed '/^track/d' | awk '$4 ~/_reference/ && $4 !~ /TC8/ && $1 !~ /MtDNA/ {print $0}'> ${bam_name}_temp && mv ${bam_name}_temp ${bam_name}_temp_nonredundant.bed
cat ${bam_name}_temp_nonredundant.bed | sort -k1,1 -k2,2n > ${bam_name}_temp && mv ${bam_name}_temp ${bam_name}_temp_nonredundant.bed
cat ${bam_name}_temp_nonredundant.bed | awk '$5>=3 {print $0}' > tmp && mv tmp ${bam_name}_temp_nonredundant.bed #rec_edit
cp ${bam_name}_temp_nonredundant.bed ../final_results/${bam_name}_temp_absence_nonredundant.bed

# COUNT TRANSPOSONS
#run te_totals.py script
end_tes=`grep -c "specifies an unknown reference name. Continue anyway." ../${bam_name}_TEMP_absence_log.txt`
python /lscr2/andersenlab/kml436/git_repos2/Transposons2/scripts/te_totals.py ${bam_name}_temp_nonredundant.bed $consensus_renamed ${bam_name} $results_file absent $end_tes
cd ..

##############################################################################################
# 						PROCESSING TELOCATE OUTPUT
##############################################################################################

cd raw_results_telocate
rm ${bam_name}_telocate_nonredundant.bed
echo "Filtering TELOCATE output..."
sed -e '1,2d' "./TELOCATE/TEL_${minimal_Distance_to_count}_reads${minimal_supporting_reads}_acc${minimal_supporting_individuals}.info" > "${bam_name}_tmpfile"
awk -F'[\t/]' '{printf $1"\t"; if($17=="old") printf $2-1"\t"$2"\t"$8"\t"$5"_reference_telocate_rp_\t0"; else if($17=="new") printf $2-1"\t"$2"\t"$8"\t"$5"_non-reference_telocate_rp_\t0"; if($14=="parallel") print "\t+"; else if($14 =="uncertain") print "\t."; else print "\t-";}' "${bam_name}_tmpfile" > "${bam_name}_tmpfile_telocate_presort.txt"
bedtools sort -i "${bam_name}_tmpfile_telocate_presort.txt" > "${bam_name}_tmpfile_telocate_sorted_redundant.txt"
awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5NR"\t"$6"\t"$7}' "${bam_name}_tmpfile_telocate_sorted_redundant.txt" > "${bam_name}_tmpfile_telocate_counted_redundant.txt"
awk '{ print $1"\t"$2"\t"$3"\t"$5"\t"$4"\t"$7}' "${bam_name}_tmpfile_telocate_counted_redundant.txt" >> "${bam_name}_tmpfile_telocate_redundant.bed"
cat ${bam_name}_tmpfile_telocate_redundant.bed | awk '$4~/_reference/ {print $0}' > tmp && mv tmp ${bam_name}_tmpfile_telocate_redundant.bed
sort -k1,3 -k4rn "${bam_name}_tmpfile_telocate_counted_redundant.txt"| sort -u -k1,3 | awk '{ print $1"\t"$2"\t"$3"\t"$5"\t"$4"\t"$7}'> "${bam_name}_tmpfileNR"
bedtools sort -i "${bam_name}_tmpfileNR" >> "${bam_name}_telocate_nonredundant.bed"
cat ${bam_name}_telocate_nonredundant.bed | awk '$4~/_reference/ {print $0}' > tmp && mv tmp ${bam_name}_telocate_nonredundant.bed

# RENAME TRANSPOSON CALLS
#rename the TEs in the output tp match final consensus names
python /lscr2/andersenlab/kml436/git_repos2/Transposons2/scripts/correct_names_2_set2.py $family_renames ${bam_name}_telocate_nonredundant.bed TMP
mv ${bam_name}_telocate_nonredundant.bed ${bam_name}_nonredundant_org_copy.bed
mv TMP ${bam_name}_telocate_nonredundant.bed
#rename chr X, ensure all lines contain "reference"
closestBed -a $ref_tes -b ${bam_name}_telocate_nonredundant.bed  -d -t all | awk '$13<=1000 {print $0}' > ${bam_name}_closest.txt
python /lscr2/andersenlab/kml436/git_repos2/Transposons2/scripts/process_telocate_output.py ${bam_name}_closest.txt
cat processed_calls.txt| sort -k1,1 -k2,2n > ${bam_name}_telocate_nonredundant.bed

cat ${bam_name}_telocate_nonredundant.bed | sed s'/chrX/X/g'| sed '/^track/d' | awk '$4 ~/_reference/ && $4 !~ /TC8/ && $1 !~ /MtDNA/ {print $0}'> ${bam_name}_temp && mv ${bam_name}_temp ${bam_name}_telocate_nonredundant.bed
cp ${bam_name}_telocate_nonredundant.bed ../final_results/${bam_name}_telocate_nonredundant.bed ###HERE ERROR

# COUNT TRANSPOSONS
#run te_totals.py script
end_tes=`grep -c "specifies an unknown reference name. Continue anyway." ../${bam_name}_TELOCATE_log.txt`
python /lscr2/andersenlab/kml436/git_repos2/Transposons2/scripts/te_totals.py ${bam_name}_telocate_nonredundant.bed  $consensus_renamed ${bam_name} $results_file reference $end_tes
cd ..

echo "Done"


text:
import {assert} from 'chai';

import Requester, {_JoinTask} from '../../../esm/data/requester';
import {DATA_OPS} from '../../../esm/registry';

describe('Requester object defines and parses requests', function () {
    describe('Layout parsing', function () {
        before(function () {
            DATA_OPS.add('sumtwo', (
                {plot_state: { state_field = 0 }},
                [left, right],
                some_param,
            ) => left + right + some_param + state_field);
        });

        after(function () {
            DATA_OPS.remove('sumtwo');
        });

        beforeEach(function () {
            this._all_datasources = new Map([
                ['assoc1', { name: 'assoc1', getData: () => Promise.resolve(1) }],
                ['someld', { name: 'someld', getData: () => Promise.resolve(2) }],
                ['intervals', { name: 'intervals', getData: () => Promise.resolve(['a', 'b', 'c', 'd']) }],
                ['assoc2', { name: 'assoc2' }],
                ['catalog', { name: 'catalog' }],
            ]);
            this._requester = new Requester(this._all_datasources);
        });

        it('builds request data from namespaces and join tasks', function () {
            // Test name logic
            const namespace_options = {'assoc': 'assoc1', 'ld': 'someld'};
            const data_operations = [
                { type: 'fetch', from: ['assoc', 'ld(assoc)'] },
                {
                    type: 'left_match',
                    name: 'combined',
                    requires: ['assoc', 'ld'],
                    params: ['assoc:variant', 'ld:variant'],
                },
            ];

            const [entities, dependencies] = this._requester.config_to_sources(namespace_options, data_operations);

            // Validate names of dependencies are correct
            assert.deepEqual(dependencies, ['assoc', 'ld(assoc)', 'combined(assoc, ld)'], 'Dependencies are resolved in expected order');

            // Validate that correct dependencies were wired up
            assert.deepEqual([...entities.keys()], ['assoc', 'ld', 'combined']);
            assert.deepEqual(entities.get('assoc').name, 'assoc1');
            assert.deepEqual(entities.get('ld').name, 'someld');

            assert.instanceOf(entities.get('combined'), _JoinTask, 'A join task was created');
        });

        it('provides developer friendly error messages', function () {
            // Test parse errors: namespaces malformed
            assert.throws(() => {
                this._requester.config_to_sources({ 'not:allowed': 'whatever' }, []);
            }, /Invalid namespace name: 'not:allowed'/);

            assert.throws(
                () => {
                    this._requester.config_to_sources({ 'somenamespace': 'nowhere' }, []);
                },
                /not found in DataSources/,
                'Namespace references something not registered in datasource',
            );

            // Test duplicate namespace errors: joins
            assert.throws(() => {
                this._requester.config_to_sources(
                    {},
                    [
                        {name: 'combined', type: 'left_match', requires: []},
                        {name: 'combined', type: 'left_match', requires: []},
                    ],
                );
            }, /join name 'combined' must be unique/);

            assert.throws(
                () => {
                    this._requester.config_to_sources(
                        {},
                        [
                            {name: 'combined', type: 'left_match', requires: ['unregistered', 'whatisthis']},
                        ],
                    );
                },
                /cannot operate on unknown provider/,
            );

        });

        it('performs joins based on layout spec', function () {
            const namespace_options = {'assoc': 'assoc1', 'ld': 'someld'};
            const data_operations = [
                { type: 'fetch', from: ['assoc', 'ld(assoc)'] },
                {
                    type: 'sumtwo',
                    name: 'combined',
                    requires: ['assoc', 'ld'],
                    params: [3], // tests that params get passed, and can be whatever a join function needs
                },
            ];

            const [entities, dependencies] = this._requester.config_to_sources(namespace_options, data_operations);

            return this._requester.getData({}, entities, dependencies)
                .then((res) => {
                    assert.equal(res, 6);  // 1 + 2 + 3
                });
        });

        it('passes a copy of plot.state to each data op, and can act on the parameters', function () {
            const namespace_options = {'assoc': 'assoc1', 'ld': 'someld'};
            const data_operations = [
                { type: 'fetch', from: ['assoc', 'ld(assoc)'] },
                {
                    type: 'sumtwo',
                    name: 'combined',
                    requires: ['assoc', 'ld'],
                    params: [3], // tests that params get passed, and can be whatever a join function needs
                },
            ];

            const [entities, dependencies] = this._requester.config_to_sources(namespace_options, data_operations);

            return this._requester.getData({ state_field: 20 }, entities, dependencies)
                .then((res) => {
                    assert.equal(res, 26);  // 1 + 2 + 3 + 20
                });
        });

        it('passes a reference to the initiator, allowing it to do things like mutate the data layer layout when data is received', function () {
            const namespace_options = {'intervals': 'intervals'};
            DATA_OPS.add('layer_mutator', ({plot_state, data_layer}, [data]) => {
                // This sort of operation is very special-purpose and tied to the mock layout and changes we want to see!
                data_layer.layout.mock_x_categories = data;
                // An operation must always return data in the end. This usage is pretty esoteric, in that it exits solely to create side effects.
                return data;
            });

            const data_operations = [
                { type: 'fetch', from: ['intervals'] },
                {
                    type: 'layer_mutator',
                    name: 'combined',
                    requires: ['intervals'],
                    params: [3],
                },
            ];

            const mock_layer = {
                layout: {
                    mock_x_categories: [],
                },
            };
            const [entities, dependencies] = this._requester.config_to_sources(namespace_options, data_operations, mock_layer);

            return this._requester.getData({}, entities, dependencies)
                .then((res) => {
                    assert.deepEqual(mock_layer.layout.mock_x_categories, res, 'In this example, mock categories exactly match returned data');
                });
        });

        it('tries to auto-generate data_operations[@type=fetch] if not provided', function () {
            const namespace_options = { assoc: 'assoc1', catalog: 'catalog', ld: 'someld' };
            let data_operations = []; // no operations, fetch or otherwise

            let [_, dependencies] = this._requester.config_to_sources(namespace_options, data_operations);
            assert.deepEqual(
                data_operations,
                [{type: 'fetch', from: ['assoc', 'catalog', 'ld']}], // autogen doesn't specify dependencies, like ld(assoc)
                'Layout data_ops is mutated in place to reference namespaces (no dependencies assumed when auto-specifying)',
            );

            assert.deepEqual(dependencies, ['assoc', 'catalog', 'ld'], 'Dependencies are auto-guessed from namespaces');

            // Related scenario: no fetch rule defined, but other rules are!
            data_operations = [{ type: 'sumtwo', name: 'somejoin', requires: ['assoc', 'ld'], params: [5] }];
            ([_, dependencies] = this._requester.config_to_sources(namespace_options, data_operations));
            assert.deepEqual(
                data_operations,
                [
                    { type: 'fetch', from: ['assoc', 'catalog', 'ld'] },
                    { type: 'sumtwo', name: 'somejoin', requires: ['assoc', 'ld'], params: [5] },
                ],
                'Auto-generates fetch rules specifically; leaves other data ops untouched',
            );
            assert.deepEqual(dependencies, ['assoc', 'catalog', 'ld', 'somejoin(assoc, ld)'], 'Dependencies are (still) auto-guessed from namespaces');
        });

        it('attempts to reference all namespaces in data_operations[@type=fetch] at least once', function () {
            const namespace_options = {'assoc': 'assoc1', 'catalog': 'catalog'};
            const data_operations = [{ type: 'fetch', from: ['assoc'] }]; // Fetch rules exist, but catalog not referenced! Eg, this could be someone creating a child layout; modifying a nested list is annoying

            const [_, dependencies] = this._requester.config_to_sources(namespace_options, data_operations);
            assert.deepEqual(
                data_operations,
                [{type: 'fetch', from: ['assoc', 'catalog']}],
                'Layout data_ops is mutated in place to reference namespaces (no dependencies assumed when auto-specifying)',
            );
            assert.deepEqual(dependencies, ['assoc', 'catalog'], 'Dependencies take all namespaces into account');
        });

        it('autogenerates names for join operations, if none are provided', function () {
            const namespace_options = { assoc: 'assoc1', catalog: 'catalog' };
            const data_operations = [
                { type: 'fetch', from: ['assoc', 'catalog'] },
                { type: 'sumtwo', name: 'has_name', requires: ['assoc', 'catalog'], params: [] },
                { type: 'sumtwo', requires: ['assoc', 'has_name'], params: [] },
            ];

            const [_, dependencies] = this._requester.config_to_sources(namespace_options, data_operations);
            assert.deepEqual(
                data_operations,
                [
                    { type: 'fetch', from: ['assoc', 'catalog'] },
                    { type: 'sumtwo', name: 'has_name', requires: ['assoc', 'catalog'], params: [] },
                    { type: 'sumtwo', name: 'join0', requires: ['assoc', 'has_name'], params: [] },
                ],
                'Layout data_ops is mutated in place to give a name to any join without one',
            );

            assert.deepEqual(
                dependencies,
                ['assoc', 'catalog', 'has_name(assoc, catalog)', 'join0(assoc, has_name)'],
                'Dependencies reference the auto-generated join names correctly');
        });
    });
});


text:
package com.netflix.spinnaker.clouddriver.artifacts.jenkins;

import com.google.common.collect.ImmutableList;
import com.netflix.spinnaker.clouddriver.artifacts.config.ArtifactCredentials;
import com.netflix.spinnaker.clouddriver.artifacts.config.SimpleHttpArtifactCredentials;
import com.netflix.spinnaker.kork.annotations.NonnullByDefault;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.OkHttpClient;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

@NonnullByDefault
@Slf4j
public class JenkinsArtifactCredentials
    extends SimpleHttpArtifactCredentials<JenkinsArtifactAccount> implements ArtifactCredentials {
  public static final String CREDENTIALS_TYPE = "artifacts-jenkins";
  private static final String TYPE = "jenkins/file";

  @Getter private final String name;

  @Getter private final ImmutableList<String> types = ImmutableList.of(TYPE);

  private final JenkinsArtifactAccount jenkinsArtifactAccount;

  JenkinsArtifactCredentials(JenkinsArtifactAccount account, OkHttpClient okHttpClient) {
    super(okHttpClient, account);
    this.jenkinsArtifactAccount = account;
    this.name = account.getName();
  }

  @Override
  protected HttpUrl getDownloadUrl(Artifact artifact) {
    String formattedJenkinsAddress =
        jenkinsArtifactAccount.getAddress().endsWith("/")
            ? jenkinsArtifactAccount.getAddress()
            : jenkinsArtifactAccount.getAddress() + "/";
    String formattedReference =
        artifact.getReference().startsWith("/")
            ? artifact.getReference()
            : "/" + artifact.getReference();
    String buildUrl =
        formattedJenkinsAddress
            + "job/"
            + artifact.getName()
            + "/"
            + artifact.getVersion()
            + "/artifact"
            + formattedReference;
    HttpUrl url = HttpUrl.parse(buildUrl);
    if (url == null) {
      throw new IllegalArgumentException(
          "Malformed content URL in reference: "
              + buildUrl
              + ". Read more here https://www.spinnaker.io/reference/artifacts/types/");
    }
    return url;
  }

  @Override
  public String getType() {
    return CREDENTIALS_TYPE;
  }
}


text:
import enum
import importlib
import inspect
import pathlib
import types
from typing import List, MutableMapping, Optional, Sequence, Tuple, Type, cast

class MemberDocumenter:
    """
    Abstract base class for module member documenters.

    Member documenters generate reStructuredText output for classes, functions
    and other objects defined in modules. They also implement the logic for
    identifying which of those objects should be selected for documenting via
    their
    :py:meth:`~uqbar.apis.MemberDocumenter.MemberDocumenter.validate_client`
    method. Module documenters loop over their client module's dict and try
    each of their member documenter class' validation method against each dict
    member in turn in order to identify documentable content and associate it
    with a member documenter class.

    .. tip::

       Subclass :py:class:`~uqbar.apis.MemberDocumenter` to implement your own
       custom module member documentation output. You'll need to override
       :py:meth:`~uqbar.apis.MemberDocumenter.MemberDocumenter.validate_client`
       with custom identification logic, and provide your desired
       reStructuredText output via an overridden
       :py:meth:`~uqbar.apis.MemberDocumenter.MemberDocumenter.__str__`
       implementation.

    :param package_path: the module path and name of the member to document
    """

    ### INITIALIZER ###

    def __init__(self, package_path: str) -> None:
        module_path, _, client_name = package_path.rpartition(".")
        module = importlib.import_module(module_path)
        client = getattr(module, client_name)
        if not self.validate_client(client, module_path):
            message = f"Unexpected object: {client!r} from {module_path}"
            raise ValueError(message)
        self._client = client
        self._package_path = package_path

    ### SPECIAL METHODS ###

    def __str__(self) -> str:
        raise NotImplementedError

    ### PUBLIC METHODS ###

    @classmethod
    def validate_client(cls, client: object, module_path: str) -> bool:
        """
        Check if the member documenter can document the provided client.

        :param client: the potential client to validate
        :param module_path: the package path of the module where the client was encountered
        """
        raise NotImplementedError

    ### PUBLIC PROPERTIES ###

    @property
    def client(self) -> object:
        return self._client

    @property
    def documentation_section(self) -> str:
        raise NotImplementedError

    @property
    def package_path(self) -> str:
        return self._package_path

class FunctionDocumenter(MemberDocumenter):
    """
    A basic function documenter.

    ::

        >>> import uqbar.apis
        >>> path = 'uqbar.io.walk'
        >>> documenter = uqbar.apis.FunctionDocumenter(path)
        >>> documentation = str(documenter)
        >>> print(documentation)
        .. autofunction:: walk

    .. tip::

       Subclass :py:class:`~uqbar.apis.FunctionDocumenter` to
       implement your own custom module documentation output.
       You'll need to provide your desired reStructuredText output
       via an overridden
       :py:meth:`~uqbar.apis.FunctionDocumenter.FunctionDocumenter.__str__`
       implementation.

    :param package_path: the module path and name of the member to document
    """

    ### CLASS VARIABLES ###

    __documentation_section__ = "Documenters"

    ### SPECIAL METHODS ###

    def __str__(self) -> str:
        return ".. autofunction:: {}".format(getattr(self.client, "__name__"))

    ### PUBLIC METHODS ###

    @classmethod
    def validate_client(cls, client: object, module_path: str) -> bool:
        return (
            isinstance(client, types.FunctionType) and client.__module__ == module_path
        )

    ### PUBLIC PROPERTIES ###

    @property
    def documentation_section(self) -> str:
        return "Functions"

class ClassDocumenter(MemberDocumenter):
    """
    A basic class documenter.

    ::

        >>> import uqbar.apis
        >>> path = 'uqbar.apis.documenters.ClassDocumenter'
        >>> documenter = uqbar.apis.ClassDocumenter(path)
        >>> documentation = str(documenter)
        >>> print(documentation)
        .. autoclass:: ClassDocumenter
           :members:
           :undoc-members:

    .. tip::

       Subclass :py:class:`~uqbar.apis.ClassDocumenter` to implement your own
       custom class documentation output. You'll need to provide your desired
       reStructuredText output via an overridden
       :py:meth:`~uqbar.apis.ClassDocumenter.ClassDocumenter.__str__`
       implementation.

       See :py:class:`~uqbar.apis.documenters.SummarizingClassDocumenter` for an example.

    :param package_path: the module path and name of the member to document
    """

    ### CLASS VARIABLES ###

    __documentation_section__ = "Documenters"

    ### SPECIAL METHODS ###

    def __str__(self) -> str:
        name = getattr(self.client, "__name__")
        if issubclass(self.client, Exception):  # type: ignore
            return ".. autoexception:: {}".format(name)
        return "\n".join(
            [".. autoclass:: {}".format(name), "   :members:", "   :undoc-members:"]
        )

    ### PUBLIC METHODS ###

    @classmethod
    def validate_client(cls, client: object, module_path: str) -> bool:
        return isinstance(client, type) and getattr(client, "__module__") == module_path

    ### PUBLIC PROPERTIES ###

    @property
    def documentation_section(self) -> str:
        client = cast(type, self.client)
        if hasattr(client, "__documentation_section__"):
            section = getattr(client, "__documentation_section__")
            if section is not None:
                return section
        if inspect.isabstract(client):
            return "Abstract Classes"
        elif issubclass(client, enum.Enum):
            return "Enumerations"
        elif issubclass(client, Exception):
            return "Exceptions"
        return "Classes"

class ModuleDocumenter:
    """
    A basic module documenter.

    ::

        >>> import uqbar.apis
        >>> documenter = uqbar.apis.ModuleDocumenter("uqbar.io")
        >>> print(str(documenter))
        .. _uqbar--io:
        <BLANKLINE>
        io
        ==
        <BLANKLINE>
        .. automodule:: uqbar.io
        <BLANKLINE>
        .. currentmodule:: uqbar.io
        <BLANKLINE>
        .. autoclass:: DirectoryChange
           :members:
           :undoc-members:
        <BLANKLINE>
        .. autoclass:: Profiler
           :members:
           :undoc-members:
        <BLANKLINE>
        .. autoclass:: RedirectedStreams
           :members:
           :undoc-members:
        <BLANKLINE>
        .. autoclass:: Timer
           :members:
           :undoc-members:
        <BLANKLINE>
        .. autofunction:: find_common_prefix
        <BLANKLINE>
        .. autofunction:: find_executable
        <BLANKLINE>
        .. autofunction:: open_path
        <BLANKLINE>
        .. autofunction:: relative_to
        <BLANKLINE>
        .. autofunction:: walk
        <BLANKLINE>
        .. autofunction:: write

    .. tip::

       Subclass :py:class:`~uqbar.apis.ModuleDocumenter` to implement your own
       custom module documentation output.
       You'll need to provide your desired reStructuredText output
       via an overridden
       :py:meth:`~uqbar.apis.ModuleDocumenter.ModuleDocumenter.__str__`
       implementation.

       See :py:class:`~uqbar.apis.SummarizingModuleDocumenter` for an example.

    :param package_path: the module path of the module to document
    :param document_private_members: whether to documenter private module members
    :param member_documenter_classes: a list of
        :py:class:`~uqbar.apis.MemberDocumenter` subclasses, defining what classes
        to use to identify and document module members
    :param module_documenters: a list of of documenters for submodules and
        subpackages of the documented module; these are generated by an
        :py:class:`~uqbar.apis.APIBuilder` instance rather than the module
        documenter directly
    """

    ### CLASS VARIABLES ###

    __documentation_section__ = "Documenters"

    ### INITIALIZER ###

    def __init__(
        self,
        package_path: str,
        document_private_members: bool = False,
        member_documenter_classes: Optional[Sequence[Type[MemberDocumenter]]] = None,
        module_documenters: Optional[Sequence["ModuleDocumenter"]] = None,
    ) -> None:
        self._package_path = package_path
        client = importlib.import_module(package_path)
        assert isinstance(client, types.ModuleType)
        self._client = client
        self._document_private_members = bool(document_private_members)
        if member_documenter_classes is None:
            member_documenter_classes = [ClassDocumenter, FunctionDocumenter]
        for _ in member_documenter_classes:
            assert issubclass(_, MemberDocumenter), _
        self._member_documenter_classes = tuple(member_documenter_classes)
        if module_documenters is not None:
            for submodule_documenter in module_documenters:
                assert isinstance(submodule_documenter, ModuleDocumenter)
            module_documenters = tuple(module_documenters)
        self._module_documenters = module_documenters or ()
        self._member_documenters = self._populate()

    ### SPECIAL METHODS ###

    def __str__(self) -> str:
        result = self._build_preamble()
        result.extend(self._build_toc(self.module_documenters or []))
        for documenter in self._member_documenters:
            result.extend(["", str(documenter)])
        return "\n".join(result)

    ### PRIVATE METHODS ###

    def _populate(self) -> Sequence[MemberDocumenter]:
        documenters = []
        for name in sorted(dir(self.client)):
            if name.startswith("_") and not self.document_private_members:
                continue
            client = getattr(self.client, name)
            for class_ in self.member_documenter_classes:
                if class_.validate_client(client, self.package_path):
                    path = "{}.{}".format(client.__module__, client.__name__)
                    documenter = class_(path)
                    documenters.append(documenter)
                    break
        return tuple(documenters)

    ### PRIVATE METHODS ###

    def _build_toc(self, documenters, **kwargs) -> List[str]:
        result: List[str] = []
        if not documenters:
            return result
        result.extend(["", ".. toctree::"])
        result.append("")
        module_documenters = [_ for _ in documenters if isinstance(_, type(self))]
        for module_documenter in module_documenters:
            path = self._build_toc_path(module_documenter)
            if path:
                result.append("   {}".format(path))
        return result

    def _build_toc_path(self, documenter):
        path = documenter.package_path.partition(self.package_path + ".")[-1]
        base, _, name = path.rpartition(".")
        if name.lower() == "index":
            path = base + "._" + name
        if not isinstance(documenter, ModuleDocumenter):
            path = path.rpartition(".")[0]
        elif documenter.is_package:
            path += "/index"
        if path.lower() == "index":
            path = "_" + path
        return path

    def _build_preamble(self) -> List[str]:
        result: List[str] = [
            ".. _{}:".format(self.reference_name),
            "",
            self.package_name,
            "=" * len(self.package_name),
            "",
            ".. automodule:: {}".format(self.package_path),
            "",
            ".. currentmodule:: {}".format(self.package_path),
        ]
        return result

    ### PUBLIC PROPERTIES ###

    @property
    def client(self) -> object:
        return self._client

    @property
    def is_package(self) -> bool:
        return hasattr(self.client, "__path__")

    @property
    def document_private_members(self) -> bool:
        return self._document_private_members

    @property
    def documentation_path(self) -> pathlib.Path:
        path = pathlib.Path(".").joinpath(*self.package_path.split("."))
        if self.is_package:
            path = path.joinpath("index")
        elif path.name.lower() == "index":
            name = path.name
            path = path.parent.joinpath("_" + name)
        return path.with_suffix(".rst")

    @property
    def is_nominative(self) -> bool:
        if self.is_package or len(self.member_documenters) != 1:
            return False
        parts = self.member_documenters[0].package_path.split(".")
        return parts[-1] == parts[-2]

    @property
    def member_documenter_classes(self) -> Sequence[Type[MemberDocumenter]]:
        return self._member_documenter_classes

    @property
    def member_documenters(self) -> Sequence[MemberDocumenter]:
        return self._member_documenters

    @property
    def member_documenters_by_section(
        self,
    ) -> Sequence[Tuple[str, Sequence[MemberDocumenter]]]:
        result: MutableMapping[str, List[MemberDocumenter]] = {}
        for documenter in self.member_documenters:
            result.setdefault(documenter.documentation_section, []).append(documenter)
        return sorted(result.items())

    @property
    def module_documenters(self) -> Sequence["ModuleDocumenter"]:
        return self._module_documenters

    @property
    def package_name(self) -> str:
        if "." in self.package_path:
            return self._package_path.rpartition(".")[-1]
        return self._package_path

    @property
    def package_path(self) -> str:
        return self._package_path

    @property
    def reference_name(self) -> str:
        return self.package_path.replace("_", "-").replace(".", "--")

class RootDocumenter:
    """
    A basic root documenter.

    This documenter generates only one reStructuredText document: the root API
    index page, which contains information about all of the modules traversed
    by an :py:class:`~uqbar.apis.APIBuilder`.

    Output is a basic `toctree` directive.

    ::

        >>> import uqbar.apis
        >>> documenter = uqbar.apis.RootDocumenter(
        ...     module_documenters=[
        ...         uqbar.apis.ModuleDocumenter('uqbar.io'),
        ...         uqbar.apis.ModuleDocumenter('uqbar.strings'),
        ...         ],
        ...     )
        >>> print(str(documenter))
        API
        ===
        <BLANKLINE>
        .. toctree::
        <BLANKLINE>
           uqbar/io
           uqbar/strings
        <BLANKLINE>

    .. tip::

       Subclass :py:class:`~uqbar.apis.RootDocumenter` to
       implement your own custom module documentation output.
       You'll need to provide your desired reStructuredText output
       via an overridden
       :py:meth:`~uqbar.apis.RootDocumenter.RootDocumenter.__str__`
       implementation.

       See :py:class:`~uqbar.apis.SummarizingRootDocumenter` for an example.

    :param module_documenters: a list of of documenters for modules and
        packages of the root documenter; these are generated by an
        :py:class:`~uqbar.apis.APIBuilder` instance rather than the module
        documenter directly
    """

    ### CLASS VARIABLES ###

    __documentation_section__ = "Documenters"

    ### INITIALIZER ###

    def __init__(self, module_documenters=None, title="API"):
        import uqbar.apis

        if module_documenters is not None:
            assert all(
                isinstance(_, uqbar.apis.ModuleDocumenter) for _ in module_documenters
            ), module_documenters
            module_documenters = tuple(module_documenters)
        self._module_documenters = module_documenters or ()
        self._title = title

    ### SPECIAL METHODS ###

    def __str__(self):
        result = [self.title, "=" * len(self.title), ""]
        if self.module_documenters:
            result.extend([".. toctree::", ""])
            for module_documenter in self.module_documenters:
                path = module_documenter.package_path.replace(".", "/")
                if module_documenter.is_package:
                    path = "{}/index".format(path)
                result.append("   {}".format(path))
            result.append("")
        return "\n".join(result)

    ### PUBLIC PROPERTIES ###

    @property
    def documentation_path(self):
        return pathlib.Path("index.rst")

    @property
    def module_documenters(self):
        return self._module_documenters

    @property
    def title(self):
        return self._title


text:
class SkCanvas;

namespace cc {

class CC_EXPORT TransformDisplayItem : public DisplayItem {
 public:
  TransformDisplayItem();
  ~TransformDisplayItem() override;

  void SetNew(const gfx::Transform& transform);

  void ToProtobuf(proto::DisplayItem* proto) const override;
  void FromProtobuf(const proto::DisplayItem& proto) override;
  void Raster(SkCanvas* canvas,
              const gfx::Rect& canvas_target_playback_rect,
              SkPicture::AbortCallback* callback) const override;
  void AsValueInto(const gfx::Rect& visual_rect,
                   base::trace_event::TracedValue* array) const override;

 private:
  gfx::Transform transform_;
};

class CC_EXPORT EndTransformDisplayItem : public DisplayItem {
 public:
  EndTransformDisplayItem();
  ~EndTransformDisplayItem() override;

  static scoped_ptr<EndTransformDisplayItem> Create() {
    return make_scoped_ptr(new EndTransformDisplayItem());
  }

  void ToProtobuf(proto::DisplayItem* proto) const override;
  void FromProtobuf(const proto::DisplayItem& proto) override;
  void Raster(SkCanvas* canvas,
              const gfx::Rect& canvas_target_playback_rect,
              SkPicture::AbortCallback* callback) const override;
  void AsValueInto(const gfx::Rect& visual_rect,
                   base::trace_event::TracedValue* array) const override;
};

}  // namespace cc

#endif  // CC_PLAYBACK_TRANSFORM_DISPLAY_ITEM_H_


text:
/* jshint node: true */

module.exports = function(deployTarget) {
  var ENV = {
    build: {},
    // include other plugin configuration that applies to all deploy targets here
    git: {
      repo: 'git@github.com:knownasilya/bootstrap-modal.git',
      branch: 'gh-pages',
      worktreePath: '/tmp/deploy'
    }
  };

  if (deployTarget === 'development') {
    ENV.build.environment = 'development';
    // configure other plugins for development deploy target here
  }

  if (deployTarget === 'staging') {
    ENV.build.environment = 'production';
    // configure other plugins for staging deploy target here
  }

  if (deployTarget === 'production') {
    ENV.build.environment = 'production';
    // configure other plugins for production deploy target here
  }

  // Note: if you need to build some configuration asynchronously, you can return
  // a promise that resolves with the ENV object instead of returning the
  // ENV object synchronously.
  return ENV;
};


text:
layout: page
title: Mind Falls Company Show
date: 2016-05-24
author: Grace Sellers
tags: weekly links, java
status: published
summary: Nullam sem purus, venenatis condimentum mattis eget, mollis pharetra.
banner: images/banner/leisure-03.jpg
booking:
  startDate: 01/11/2016
  endDate: 01/13/2016
  ctyhocn: SDFHUHX
  groupCode: MFCS
published: true
---
In sed pulvinar turpis. Mauris eu nulla ultrices purus varius bibendum eu sit amet eros. Fusce nulla mi, mollis nec sem ac, dapibus imperdiet nisl. Suspendisse blandit ante euismod tellus sodales ultricies. Mauris hendrerit justo vitae mi mollis ullamcorper. Aliquam gravida dictum nulla ac mattis. Mauris sed augue et nibh tristique facilisis. In dui elit, condimentum et odio ut, dictum tristique metus. Nulla facilisi. Mauris vitae feugiat ligula, varius venenatis arcu. Nunc ligula odio, tempus eget tortor a, posuere elementum ligula. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec id purus sed tellus eleifend interdum non at turpis. Nulla facilisi.

* Mauris volutpat nunc non euismod faucibus
* Sed luctus justo id varius volutpat
* Curabitur bibendum tortor eget auctor vestibulum
* Duis non libero ac augue molestie pharetra ut a eros.

Curabitur at odio ac purus fermentum blandit et ut purus. Sed sollicitudin, arcu eu porta viverra, lorem ipsum sodales turpis, sit amet iaculis ex risus et sapien. Ut sit amet semper nunc, efficitur accumsan diam. Cras pulvinar laoreet velit, ut dignissim urna gravida sed. Ut dapibus diam lorem, vel efficitur lacus euismod nec. Proin sit amet dui a risus consectetur rhoncus. In hac habitasse platea dictumst. Donec urna orci, varius accumsan dictum a, pellentesque ac augue. Aliquam aliquam nibh blandit aliquam dapibus. Aliquam iaculis ex lacus, at posuere ex sagittis ut. Cras elementum dignissim felis sed tempor. Fusce volutpat blandit vestibulum. Quisque ac fringilla lectus. Nam enim eros, consequat id malesuada at, tempus quis justo.


text:
package org.knowm.xchange.btcchina.dto.trade.request;

import org.knowm.xchange.btcchina.dto.BTCChinaRequest;

public class BTCChinaTransactionsRequest extends BTCChinaRequest {

  public static final String TYPE_ALL = "all";
  public static final String SINCE_TIME = "time";
  public static final String SINCE_ID = "id";

  /**
   * Constructor
   */
  public BTCChinaTransactionsRequest() {

    method = "getTransactions";
    params = "[]";
  }

  /**
   * Constructs a getting transactions log request.
   *
   * @param type Fetch transactions by type. Default is 'all'. Available types 'all | fundbtc | withdrawbtc | fundmoney | withdrawmoney | refundmoney
   * | buybtc | sellbtc | buyltc | sellltc | tradefee | rebate '
   * @param limit Limit the number of transactions, default value is 10.
   * @param offset Start index used for pagination, default value is 0.
   * @param since To fetch the transactions from this point, which can either be an order id or a unix timestamp, default value is 0.
   * @param sincetype Specify the type of 'since' parameter, can either be 'id' or 'time'. default value is 'time'.
   */
  public BTCChinaTransactionsRequest(String type, Integer limit, Integer offset, Integer since, String sincetype) {

    method = "getTransactions";
    params = String.format("[\"%s\",%d,%d,%d,\"%s\"]", type == null ? TYPE_ALL : type, limit == null ? 10 : limit.intValue(),
        offset == null ? 0 : offset.intValue(), since == null ? 0 : since.intValue(), sincetype == null ? "time" : sincetype);
  }

  @Override
  public String toString() {

    return String.format("BTCChinaTransactionsRequest{id=%d, method=%s, params=%s}", id, method, params);
  }
}


text:
description 'Symlink the database.yml file from one app to another'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
maintainer 'Engine Yard'
maintainer_email 'info@engineyard.com'
version '0.0.1'


text:
'use strict';
const path = require('path');
const _ = require('lodash');

const _configs = {
    global: require(__dirname + '/global'),
    production: require(__dirname + '/prod'),
    development: require(__dirname + '/dev')
};

const PATHS = {
    source: path.resolve('src'),
    target: path.resolve('dist')
};

const _load = function (environment) {
    if (!environment) throw 'Can\'t find local environment variable via process.env.NODE_ENV';
    if (!_configs[environment]) throw 'Can\'t find environments see _config object';

    return _configs && _.merge(
        _configs[environment](PATHS),
        _configs['global'](PATHS)
    );
};

module.exports = _load(process.env.NODE_ENV);


text:
mw::mw(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::mw)
{
    ui->setupUi(this);
    connect(ui->inputDeviceCombo,SIGNAL(currentIndexChanged(int)),
            this,SLOT(activeDeviceChanged(qint32)));
    connect(ui->outputDeviceCombo,SIGNAL(currentIndexChanged(int)),
            this,SLOT(activeDeviceChanged(qint32)));

    connect(ui->playButton,SIGNAL(clicked()),SIGNAL(play()));
    connect(ui->pauseButton,SIGNAL(clicked()),SIGNAL(pause()));
    connect(ui->stopButton,SIGNAL(clicked()),SIGNAL(stop()));

    connect(ui->foldersTreeView,SIGNAL(entered(QModelIndex)),
            SLOT(directory_selected(QModelIndex)));

    dirModel = new QDirModel();
    dirModel->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
    ui->foldersTreeView->setModel(dirModel);
    for (qint32 i = 1; i < dirModel->columnCount();i++){
        ui->foldersTreeView->setColumnHidden(i,true);
    }

    fileModel = new QFileSystemModel();
    fileModel->setFilter(QDir::Files);
    ui->fileListView->setModel(fileModel);

    QModelIndex index = dirModel->index(QStandardPaths::standardLocations(
                              QStandardPaths::HomeLocation).first());
    ui->foldersTreeView->scrollTo(index);
}

mw::~mw()
{
    delete ui;
}

void mw::on_openButton_clicked()
{
    QFileDialog dialog;
    QString filePath = dialog.getOpenFileName(this,
                           "Select file",
                           "",
                           "Midi Files(*.midi *.mid);;All Files(*.*)");
    emit openFile(filePath);

}

void mw::setMidiDeviceList(QStringList inputList,QStringList outputList){
    ui->inputDeviceCombo->clear();
    ui->outputDeviceCombo->clear();
    ui->inputDeviceCombo->addItem(tr("None"));
    ui->outputDeviceCombo->addItem(tr("None"));
    ui->inputDeviceCombo->insertItems(1,inputList);
    ui->outputDeviceCombo->insertItems(1,outputList);
}

void mw::activeDeviceChanged(qint32 index){
    Q_UNUSED(index)
    qint32 inputNumber = ui->inputDeviceCombo->currentIndex() - 1;
    qint32 outputNumber = ui->outputDeviceCombo->currentIndex() - 1;
    emit activeDeviceChanged(inputNumber >= -1 ? inputNumber : -1,
                             outputNumber >= -1 ? outputNumber : -1);
}

void mw::directory_selected(QModelIndex index){
    QString dirPath = dirModel->filePath(index);
    fileModel->setRootPath(dirPath);
}


text:
require 'cases/helper_sqlserver'
require 'models/post'

class OrderTestSQLServer < ActiveRecord::TestCase

  fixtures :posts

  it 'not mangel complex order clauses' do
    xyz_order = "CASE WHEN [title] LIKE N'XYZ%' THEN 0 ELSE 1 END"
    xyz_post = Post.create title: 'XYZ Post', body: 'Test cased orders.'
    assert_equal xyz_post, Post.order(Arel.sql(xyz_order)).first
  end

  it 'support column' do
    order = "title"
    post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
    assert_equal post1, Post.order(order).first
  end

  it 'support column ASC' do
    order = "title ASC"
    post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
    assert_equal post1, Post.order(order).first
  end

  it 'support column DESC' do
    order = "title DESC"
    post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
    assert_equal post1, Post.order(order).first
  end

  it 'support column as symbol' do
    order = :title
    post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
    assert_equal post1, Post.order(order).first
  end

  it 'support table and column' do
    order = "posts.title"
    post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
    assert_equal post1, Post.order(order).first
  end

  it 'support quoted column' do
    order = "[title]"
    post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
    assert_equal post1, Post.order(order).first
  end

  it 'support quoted table and column' do
    order = "[posts].[title]"
    post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
    assert_equal post1, Post.order(order).first
  end

  it 'support primary: column, secondary: column' do
    order = "title DESC, body"
    post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
    post2 = Post.create title: 'ZZZ Post', body: 'ZZZ Test cased orders.'
    assert_equal post1, Post.order(order).first
    assert_equal post2, Post.order(order).second
  end

  it 'support primary: table and column, secondary: column' do
    order = "posts.title DESC, body"
    post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
    post2 = Post.create title: 'ZZZ Post', body: 'ZZZ Test cased orders.'
    assert_equal post1, Post.order(order).first
    assert_equal post2, Post.order(order).second
  end

  it 'support primary: case expression, secondary: column' do
    order = "(CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END) DESC, body"
    post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
    post2 = Post.create title: 'ZZZ Post', body: 'ZZZ Test cased orders.'
    assert_equal post1, Post.order(order).first
    assert_equal post2, Post.order(order).second
  end

  it 'support primary: quoted table and column, secondary: case expresion' do
    order = "[posts].[body] DESC, (CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END) DESC"
    post1 = Post.create title: 'ZZZ Post', body: 'ZZZ Test cased orders.'
    post2 = Post.create title: 'ZZY Post', body: 'ZZZ Test cased orders.'
    assert_equal post1, Post.order(order).first
    assert_equal post2, Post.order(order).second
  end

  it 'support inline function' do
    order = "LEN(title)"
    post1 = Post.create title: 'A', body: 'AAA Test cased orders.'
    assert_equal post1, Post.order(order).first
  end

  it 'support inline function with parameters' do
    order = "SUBSTRING(title, 1, 3)"
    post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
    assert_equal post1, Post.order(order).first
  end

  it 'support inline function with parameters DESC' do
    order = "SUBSTRING(title, 1, 3) DESC"
    post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
    assert_equal post1, Post.order(order).first
  end

  it 'support primary: inline function, secondary: column' do
    order = "LEN(title), body"
    post1 = Post.create title: 'A', body: 'AAA Test cased orders.'
    post2 = Post.create title: 'A', body: 'Test cased orders.'
    assert_equal post1, Post.order(order).first
    assert_equal post2, Post.order(order).second
  end

  it 'support primary: inline function, secondary: column with direction' do
    order = "LEN(title) ASC, body DESC"
    post1 = Post.create title: 'A', body: 'ZZZ Test cased orders.'
    post2 = Post.create title: 'A', body: 'Test cased orders.'
    assert_equal post1, Post.order(order).first
    assert_equal post2, Post.order(order).second
  end

  it 'support primary: column, secondary: inline function' do
    order = "body DESC, LEN(title)"
    post1 = Post.create title: 'Post', body: 'ZZZ Test cased orders.'
    post2 = Post.create title: 'Longer Post', body: 'ZZZ Test cased orders.'
    assert_equal post1, Post.order(order).first
    assert_equal post2, Post.order(order).second
  end

  it 'support primary: case expression, secondary: inline function' do
    order = "CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END DESC, LEN(body) ASC"
    post1 = Post.create title: 'ZZZ Post', body: 'Z'
    post2 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
    assert_equal post1, Post.order(order).first
    assert_equal post2, Post.order(order).second
  end

  it 'support primary: inline function, secondary: case expression' do
    order = "LEN(body), CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END DESC"
    post1 = Post.create title: 'ZZZ Post', body: 'Z'
    post2 = Post.create title: 'Post', body: 'Z'
    assert_equal post1, Post.order(order).first
    assert_equal post2, Post.order(order).second
  end

end


text:
app.factory('getGalleries', ['$http', function($http) {

    var demoCards = {
        cards: []
    };

    $http.get("./scripts/php/galleryQuery.php")
    .then(function (response) {demoCards.cards = response.data.records;});

    return demoCards;
}]);


text:
// Copyright (c) 2020 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <cuckoocache.h>
#include <script/sigcache.h>

#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h>
#include <test/util/setup_common.h>

#include <cstdint>
#include <string>
#include <vector>

namespace {
FuzzedDataProvider *fuzzed_data_provider_ptr = nullptr;

struct RandomHasher {
    template <uint8_t> uint32_t operator()(const bool & /* unused */) const {
        assert(fuzzed_data_provider_ptr != nullptr);
        return fuzzed_data_provider_ptr->ConsumeIntegral<uint32_t>();
    }
};

struct FuzzCuckooCacheKey {
    bool b;

public:
    FuzzCuckooCacheKey(bool _b = false) : b(_b){};
    operator bool() const { return b; }
};
} // namespace

void test_one_input(const std::vector<uint8_t> &buffer) {
    FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
    fuzzed_data_provider_ptr = &fuzzed_data_provider;
    CuckooCache::cache<CuckooCache::KeyOnly<FuzzCuckooCacheKey>, RandomHasher>
        cuckoo_cache{};
    if (fuzzed_data_provider.ConsumeBool()) {
        const size_t megabytes =
            fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 16);
        cuckoo_cache.setup_bytes(megabytes << 20);
    } else {
        cuckoo_cache.setup(
            fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, 4096));
    }
    while (fuzzed_data_provider.ConsumeBool()) {
        if (fuzzed_data_provider.ConsumeBool()) {
            cuckoo_cache.insert(
                FuzzCuckooCacheKey(fuzzed_data_provider.ConsumeBool()));
        } else {
            cuckoo_cache.contains(
                FuzzCuckooCacheKey(fuzzed_data_provider.ConsumeBool()),
                fuzzed_data_provider.ConsumeBool());
        }
    }
    fuzzed_data_provider_ptr = nullptr;
}


text:
#ifndef CAFFE_LOG_LAYER_HPP_
#define CAFFE_LOG_LAYER_HPP_

#include <vector>

#include "caffe/blob.hpp"
#include "caffe/layer.hpp"
#include "caffe/proto/caffe.pb.h"

#include "caffe/layers/neuron_layer.hpp"

namespace caffe {

/**
 * @brief Computes @f$ y = log_{\gamma}(\alpha x + \beta) @f$,
 *        as specified by the scale @f$ \alpha @f$, shift @f$ \beta @f$,
 *        and base @f$ \gamma @f$.
 */
template <typename Dtype>
class LogLayer : public NeuronLayer<Dtype> {
 public:
  /**
   * @param param provides LogParameter log_param,
   *     with LogLayer options:
   *   - scale (\b optional, default 1) the scale @f$ \alpha @f$
   *   - shift (\b optional, default 0) the shift @f$ \beta @f$
   *   - base (\b optional, default -1 for a value of @f$ e \approx 2.718 @f$)
   *         the base @f$ \gamma @f$
   */
  explicit LogLayer(const LayerParameter& param)
      : NeuronLayer<Dtype>(param) {}
  virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  virtual inline const char* type() const { return "Log"; }

 protected:
  /**
   * @param bottom input Blob vector (length 1)
   *   -# @f$ (N \times C \times H \times W) @f$
   *      the inputs @f$ x @f$
   * @param top output Blob vector (length 1)
   *   -# @f$ (N \times C \times H \times W) @f$
   *      the computed outputs @f$
   *        y = log_{\gamma}(\alpha x + \beta)
   *      @f$
   */
  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  /**
   * @brief Computes the error gradient w.r.t. the exp inputs.
   *
   * @param top output Blob vector (length 1), providing the error gradient with
   *      respect to the outputs
   *   -# @f$ (N \times C \times H \times W) @f$
   *      containing error gradients @f$ \frac{\partial E}{\partial y} @f$
   *      with respect to computed outputs @f$ y @f$
   * @param propagate_down see Layer::Backward.
   * @param bottom input Blob vector (length 1)
   *   -# @f$ (N \times C \times H \times W) @f$
   *      the inputs @f$ x @f$; Backward fills their diff with
   *      gradients @f$
   *        \frac{\partial E}{\partial x} =
   *            \frac{\partial E}{\partial y} y \alpha \log_e(gamma)
   *      @f$ if propagate_down[0]
   */
  virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
  virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);

  Dtype base_scale_;
  Dtype input_scale_, input_shift_;
  Dtype backward_num_scale_;
};

}  // namespace caffe

#endif  // CAFFE_LOG_LAYER_HPP_


text:
package org.ovirt.engine.core.common.businessentities.storage;

import java.util.HashMap;
import java.util.Map;

public enum PropagateErrors {
    Off(0),
    On(1);

    private int intValue;
    private static Map<Integer, PropagateErrors> mappings;

    static {
        mappings = new HashMap<>();
        for (PropagateErrors error : values()) {
            mappings.put(error.getValue(), error);
        }
    }

    private PropagateErrors(int value) {
        intValue = value;
    }

    public int getValue() {
        return intValue;
    }

    public static PropagateErrors forValue(int value) {
        return mappings.get(value);
    }
}