def plot(spreading, _type, shift_array):
        def get_line(spread, _type):
            if _type == 'left' or _type == 'right':
                return spread.spread[_type]['val']
            elif _type == 'radius':
                return calc_radius(spread)

        data = {}
        n = 0
        for i, spread_list in enumerate(spreading):
            for j, _file in enumerate(spread_list):
                spread = Spread().read(_file)
                line = get_line(spread, _type)
                times = np.array(spread.times) - shift_array[i][j]
                data[n] = Series(data=line, index=times)
                n += 1

        # Create DataFrame for all values and drop nan
        df = DataFrame(data).dropna()
        domain = df.index

        n = 0
        data = [[], []]
        for i, spread_list in enumerate(spreading):
            for j, _ in enumerate(spread_list):
                data[i].append(df[n].tolist())
                n += 1
        t, p = stats.ttest_ind(data[0], data[1], equal_var=False)
        plot_line(line=p, domain=domain, label=label[0], color=colour[0],
                linestyle=linestyle['line'][0], hold=True)

        return None
Exemple #2
0
    def plot_data(spread, times):
        """Plot either edges or radius of specified line."""

        for _type in args.plot_type:
            velocity = spread[_type]['val']
            plot_line(
                    line=velocity,
                    domain=times,
                    color=colours[i], label=label,
                    linestyle=linestyles['line'][i]
                )

        return None
    def plot_data(spread, times):
        """Plot either edges or radius of specified line."""

        for _type in plot_type:
            plot_line(
                    line=spread[_type]['val'][0:],
                    domain=np.array(times[0:]),
                    color=colours[i], label=label,
                    linestyle=linestyles['line'][i],
                    linewidth=2
                )

        return None
Exemple #4
0
    def plot(spreading, _type, shift, style):
        def get_line(spread, _type):
            if _type == 'left' or _type == 'right':
                return spread.spread[_type]['std_error']
            elif _type == 'radius':
                _, radius = calc_radius(spread, error=True)
                return radius

        for i, spread_list in enumerate(spreading):
            spread, _ = combine_spread(spread_list, shift=shift[0])

            domain = spread.times
            line = get_line(spread, _type)
            plot_line(
                    line=line, domain=domain,
                    color=colours[i], label=labels[i],
                    linestyle=style[i],
                    hold=True
                    )

        return None
        def draw_error_line(spread):
            mean = np.array(get_line(spread, _type, error=False))
            std = np.array(get_line(spread, _type, error=True))

            # If not standard deviation desired, calculate std error
            if args.std:
                error = std
            else:
                error = (std / np.sqrt(spread.spread['num']))*args.sigma

            plot_line(
                    line=(mean + error),
                    domain=spread.times,
                    color=colours[i],
                    linestyle=linestyles['error'][i]
                )
            plot_line(
                    line=(mean - error),
                    domain=spread.times,
                    color=colours[i],
                    linestyle=linestyles['error'][i]
                )

            return None
Exemple #6
0
def spread_plot(args):
    """Draw the spreading as a function of time."""

    # Get colours and line styles from default
    colours = get_colours(args.colour, len(args.spreading))
    labels, draw_legend = get_labels(args.label, len(args.spreading))

    linestyles = {}
    linestyles['line'] = get_linestyles(args.linestyle, len(args.spreading))
    linestyles['fit'] = get_linestyles(args.fitstyle, len(args.spreading),
            'dashed')

    # Create linear fitting function
    fitfunc = lambda p, t, r: r - p[0] - p[1] * t
    pinit = [1.0, 1/7]

    # Find shift array for synchronisation
    shift_array = get_shift(args.spreading, sync=args.sync)
    impact_shift = get_shift(args.spreading, sync='impact')

    # Create dicts for lists of fit constants (r = amp * t**index)
    amp = {}
    index = {}
    ampError = {}
    indexError = {}

    ampMean = []
    ampMeanError = []
    indexMean = []
    indexMeanError = []

    for i, spread_list in enumerate(args.spreading):
        amp[i] = []
        index[i] = []
        ampError[i] = []
        indexError[i] = []

        spread, full_data = combine_spread(spread_list, shift=shift_array[i])
        spread.times = np.array(spread.times) - spread.times[0]

        for k, _file in enumerate(spread_list):
            data = Spread().read(_file)
            data.times = np.array(data.times) - impact_shift[i][k]

            # Get radius and domain
            radius = {'real': np.array(calc_radius(data))}
            domain = {'real': np.array(data.times)}

            # Cut times outside of range
            for j, time in enumerate(domain['real']):
                if time > args.tend:
                    radius['real'] = radius['real'][:j]
                    domain['real'] = domain['real'][:j]

            # Add logged values
            radius['log'] = np.log10(radius['real'][1:])
            domain['log'] = np.log10(domain['real'][1:])

            # Cut in log range
            for j, logt in enumerate(domain['log']):
                if logt > args.tendlog:
                    radius['log'] = radius['log'][:j]
                    domain['log'] = domain['log'][:j]

            # Fit constants to data
            out = optimize.leastsq(fitfunc, pinit,
                    args=(domain['log'], radius['log']), full_output=1)

            pfinal = out[0]
            covar = out[1]

            # Add unlogged constants to lists
            amp[i].append(10**pfinal[0])
            index[i].append(pfinal[1])
            ampError[i].append(np.sqrt(covar[1][1]) * amp[i][-1])
            indexError[i].append(np.sqrt(covar[0][0]))

            if args.draw == 'log' and args.nomean:
                plot_line(
                        line=radius['log'],
                        domain=domain['log'],
                        color=colours[i],
                        linestyle=linestyles['line'][i]
                    )
                if not args.nofit:
                    plot_line(
                            line=out[0][0] + out[0][1] * domain['log'],
                            domain=domain['log'],
                            color=colours[i], linestyle=linestyles['fit'][i]
                            )
            if args.draw == 'real' and args.nomean:
                plot_line(
                        line=radius['real'],
                        domain=domain['real'],
                        color=colours[i], linestyle=linestyles['line'][i]
                        )
                if not args.nofit:
                    plot_line(
                            line=amp[i][-1] * (domain['real']**index[i][-1]),
                            domain=domain['real'],
                            color=colours[i], linestyle=linestyles['fit'][i]
                            )

        ampMean.append(np.mean(amp[i]))
        ampMeanError.append(np.std(amp[i]) / np.sqrt(len(amp[i]) - 1))
        indexMean.append(np.mean(index[i]))
        indexMeanError.append(np.std(index[i]) / np.sqrt(len(index[i]) - 1))

        if not args.nomean:
            if args.draw == 'log':
                plot_line(
                        line=np.log10(calc_radius(spread)),
                        domain=np.log10(spread.times),
                        label=labels[i],
                        color=colours[i], linestyle=linestyles['line'][i]
                        )
                if not args.nofit:
                    plot_line(
                            line=(np.log10(ampMean[i])
                                    + indexMean[i] * np.log10(spread.times)),
                            domain=np.log10(spread.times),
                            label='C=%.2f, n=%.2f'%(ampMean[i], indexMean[i]),
                            color=colours[i], linestyle=linestyles['fit'][i]
                            )
            if args.draw == 'real':
                plot_line(
                        line=calc_radius(spread),
                        domain=spread.times,
                        label=labels[i],
                        color=colours[i], linestyle=linestyles['line'][i]
                        )
                if not args.nofit:
                    plot_line(
                            line=ampMean[i] * (domain['real']**indexMean[i]),
                            domain=domain['real'],
                            label='C=%.2f, n=%.2f'%(ampMean[i], indexMean[i]),
                            color=colours[i], linestyle=linestyles['fit'][i]
                            )

    plt.title(args.title, fontsize='medium')
    plt.axis('normal')

    plt.legend()

    # Default xlabel and xlims based on draw method
    if args.draw == 'real':
        if args.ylabel == None:
            args.ylabel = "Spread radius (nm)"
        if args.xlabel == None:
            args.xlabel = "Time (ps)"
        if (args.tend and args.tendlog) < np.inf:
            plt.xlim([None, min(args.tend, 10**args.tendlog)])
    elif args.draw == 'log':
        if args.ylabel == None:
            args.ylabel = "log10 of radius (in nm)"
        if args.xlabel == None:
            args.xlabel = "log10 of time (in ps)"
        if (args.tend and args.tendlog) < np.inf:
            plt.xlim([None, min(args.tend, args.tendlog)])

    plt.xlabel(args.xlabel, fontsize='medium')
    plt.ylabel(args.ylabel, fontsize='medium')

    if args.xlim:
        plt.xlim(args.xlim)

    # Print collected output
    print("Fitting spread radius 'R' of input file sets to power law functions "
            "of time 't' as 'R = C * (t ** n)' and taking means:")
    for i, _ in enumerate(amp):
        print()
        # If nomean, print individual line values
        if args.nomean:
            for values in zip(amp[i], ampError[i], index[i], indexError[i]):
                print("%f +/- %f" % (values[0], values[1]), end=', ')
                print("%f +/- %f" % (values[2], values[3]))

        # Print mean values
        if args.nomean:
            print("  -> ", end='')
        print("C = %f +/- %f" % (ampMean[i], ampMeanError[i]))
        if args.nomean:
            print("  -> ", end='')
        print("n = %f +/- %f" % (indexMean[i], indexMeanError[i]))

    # Finish by saving and / or showing
    if args.save:
        plt.savefig(args.save)

    if args.draw != 'off':
        plt.show()

    return None
Exemple #7
0
def com_plot(args):
    """Draw the center of mass height as a function of time."""

    # Get colours, labels and line styles from default
    colours = get_colours(args.colour, len(args.spreading))
    labels, draw_legend = get_labels(args.label, len(args.spreading))

    linestyles = {}
    linestyles['line'] = get_linestyles(args.linestyle, len(args.spreading))
    linestyles['error'] = get_linestyles(args.errorstyle, len(args.spreading),
            'dashed')

    # Find shift array for synchronisation
    shift_array = get_shift(args.spreading, sync=args.sync)

    for i, spread_list in enumerate(args.spreading):
        spread, data = combine_spread(spread_list, shift=shift_array[i])

        # Check if error bars need to be included
        error = args.error and len(spread_list) > 1

        # Create graph
        if args.nomean:
            label = labels[i]
            for spread_data in data:
                domain = spread_data.times
                line = spread_data.dist
                plot_line(line=line, domain=domain, color=colours[i],
                        label=label, linestyle=linestyles['line'][i])
                label = '_nolegend_'
        else:
            domain = spread.times
            line = spread.dist
            plot_line(line=line, domain=domain, color=colours[i],
                    label=labels[i], linestyle=linestyles['line'][i])

        if error:
            domain = spread.times
            line = list(np.array(spread.dist)
                    + np.array(spread.spread['dist']['std_error'])
                    * args.sigma)
            plot_line(line=line, domain=domain, color=colours[i],
                    linestyle=linestyles['error'][i])
            line = list(np.array(spread.dist)
                    - np.array(spread.spread['dist']['std_error'])
                    * args.sigma)
            plot_line(line=line, domain=domain, color=colours[i],
                    linestyle=linestyles['error'][i])

    plt.title(args.title)
    plt.xlabel(args.xlabel)
    plt.ylabel(args.ylabel)

    plt.axis('normal')

    plt.xlim([args.t0, args.tend])

    if draw_legend:
        plt.legend()

    # Finish by saving and / or showing
    if args.save:
        plt.savefig(args.save)

    if args.show:
        plt.show()

    return None