コード例 #1
0
def horserace_go(**kwargs):
    data = request.get_json()
    #logMessage(f"data: {data}")
    playerDF, boutDF, checkbox_dict = _horserace_fetch_dataframes(data, **kwargs)
    output = io.StringIO()

    try:
        fit_info = stat_utils.estimate(
            playerDF, boutDF,
            draws_rule=get_settings()['hr_draws_rule']
        )
        plt.figure(figsize=[3,3])
        fig, axes = plt.subplots(ncols=1, nrows=1)
        graph_yscale_dct = {'hr_graph_yscale_linear': 'linear',
                            'hr_graph_yscale_log': 'log'}
        axes.set_yscale(graph_yscale_dct[get_settings()['hr_graph_yscale']])
        graph_type_dct = {'hr_graph_style_box': 'boxplot',
                          'hr_graph_style_violin': 'violin'}
        graph_type = graph_type_dct[get_settings()['hr_graph_style']]
        fit_info.gen_graph(fig, axes, graph_type)
        FigureCanvas(fig).print_svg(output)
        
    except RuntimeError as e:
        logMessage('horseRace_go exception: %s' % str(e))
    result = {'image': output.getvalue(),
              'announce_html': fit_info.estimate_win_probabilities().as_html()
              }

    return result
コード例 #2
0
ファイル: main.py プロジェクト: oxfordmmm/arboreta
def get_graph_svg(guid):
    """Endpoint returning matplotlib svg graph
    ---
    parameters:
      - name: guid
        in: path
        required: true
        type: string
      - name: reference
        in: query
        type: string
        required: false
      - name: distance
        in: query
        type: string
        required: false
      - name: quality
        in: query
        type: string
        required: false
    responses:
      200:
        description:
    """
    reference = request.args.get('reference')
    if not reference: reference = cfg['default_reference']
    quality = request.args.get('quality')
    if not quality: quality = cfg['default_quality']
    cutoff = request.args.get('cutoff')
    if cutoff and int(cutoff) > 10:
        cutoff = 10
    if cutoff:
        (xs, ys) = graph3(guid, reference, quality, cfg['elephantwalkurl'],
                          int(cutoff))
    else:
        (xs, ys) = graph2(guid, reference, quality, cfg['elephantwalkurl'])
    if len(ys) == 0:
        slopes = []
    else:
        slopes = [0]
    print(xs)
    for n in range(len(xs)):
        if n == 0: continue
        slopes.append((ys[n] - ys[n - 1]) / (xs[n] - xs[n - 1]))
    fig = Figure(figsize=(12, 7), dpi=100)
    fig.suptitle("Sample: {0}, reference: {1}, quality: {2}, ew: {3}".format(
        guid, reference, quality, cfg['elephantwalkurl']))
    ax = fig.add_subplot(111)
    ax.xaxis.set_major_locator(MaxNLocator(integer=True))
    ax.plot(xs, ys, 'gx-', linewidth=1)
    ax.plot(xs, slopes, 'r-', linewidth=1)
    ax.set_xlabel("Distance")
    ax.set_ylabel("Neighbours")
    canvas = FigureCanvas(fig)
    svg_output = StringIO()
    canvas.print_svg(svg_output)
    response = make_response(svg_output.getvalue())
    response.headers['Content-Type'] = 'image/svg+xml'
    return response
コード例 #3
0
def malignancy_plot(age, tumour_size):

    # Scale age and tumour_size (note: output is numpy array)
    age_tumour_size = [[age] + [tumour_size]]
    age_tumour_size_scaled = malign_scaler.transform(age_tumour_size)

    # Create malignancy feature array
    malign_features = [age_tumour_size_scaled[0].tolist()]

    # Predict malignancy: 0 benign, 1 non-benign
    yscore_cal = malign_clf.predict_proba(malign_features)
    yscore_nonbenign = np.round(yscore_cal[0][1]*100, 1)
    ypred_cal = np.array(yscore_cal[:, 1] > 0.05, dtype=int)[0]  # threshold = 5%
    
    # Select title and title colour
    red = '#CC333F'
    blue = '#1693A7'
    if ypred_cal == 0:
        title = 'Benign'
        title_colour = blue
    elif ypred_cal == 1:
        title = 'Borderline malignancy / Malignant'
        title_colour = red

    ##### Plot Figure #####
    sns.set(font_scale=1.2)
    sns.set_style('white',{'axes.linewidth': 2.5, 'axes.edgecolor': '#D3D3D3'})

    # Create plot
    fig = Figure()
    ax = fig.add_subplot(1, 1, 1)

    # Draw plot
    sns.kdeplot(malign_valid_dist, color='orange', shade=True,
                gridsize=500, bw='silverman', ax=ax)
    ax.vlines(yscore_nonbenign, 0, 0.10, color='#D70E08', zorder=10)  # prediction
    txt = ax.text(yscore_nonbenign, 0.11, '   ' + str(yscore_nonbenign) + '%', color='#D70E08',
             zorder=10, horizontalalignment='center')  # prediction label
    txt.set_path_effects([path_effects.withStroke(linewidth=3, foreground='w')])  # label outline
    ax.vlines(5, 0, 0.25, color='#cccccc', zorder=10, linestyles='dashed')  # threshold (5%)
    ax.set_title('Prediction: ' + title, color=title_colour, fontsize=14)

    ax.set_ylabel('Probability density', labelpad=10)
    ax.set_xlabel('Probability (%)', labelpad=10)
    ax.set_xlim(0, 30)
    ax.set_ylim(0, 0.4)
    for axis in ['top','bottom','left','right']:
        ax.spines[axis].set_linewidth(2)
    sns.despine(ax=ax)
    fig.set_tight_layout(True)

    # Write plot to SVG
    canvas = FigureCanvas(fig)
    output = BytesIO()
    # canvas.print_svg('test.svg')  # For testing fig output locally
    canvas.print_svg(output)
    response = make_response(output.getvalue())
    response.mimetype = 'image/svg+xml'
    return response
コード例 #4
0
    def __init_canvas__(self):
        # Adapted from https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/backends/backend_svg.py : print_svg
        self.canvas = FigureCanvas(self.fig)
        self.export_str = StringIO()

        self.fig.set_dpi(72.0)
        width, height = self.fig.get_size_inches()
        w, h = width * 72, height * 72

        self.renderer = RendererSVG(w, h, self.export_str, None, 72)
コード例 #5
0
def draw_custom_graph(user_agents):
    plot_x_y = []

    # requests = log_parser.get_log_dicts(user_agent = r'SiteSucker.*')
    for user_agent in user_agents:
        requests = logger.get_log_dicts(user_agent=user_agent)
        first_date = None
        x = []
        y = []
        for index, request in enumerate(requests):
            if index is not 0:
                x.append(time_delta(request['datetime'], first_date))
            else:
                first_date = request['datetime']
                x.append(0)
            y.append(index)
        plot_x_y.append({
            'x': x,
            'y': y
        })

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)

    current_index = 0
    for xy in plot_x_y:
        # todo: Use different color (not only random) and add the ability to choose multiple user_agent.
        ax.plot(
            xy['x'], xy['y'],
            color=graph_colors[current_index % len(graph_colors)],
            label=user_agents[current_index]
        )
        ax.legend(framealpha=0.5, loc=4, prop={'size': 8})
        current_index += 1

    plt.xlabel('Delta Time (seconds)')
    plt.ylabel('Number of requests')

    ax.xaxis.set_major_formatter(formatter)

    output = StringIO.StringIO()
    canvas = FigureCanvas(fig)
    canvas.print_svg(output)
    return output
コード例 #6
0
ファイル: sample.py プロジェクト: sfaleron/LeastSqs
fig = Figure(figsize=(9,3), subplotpars=SubplotParams(
    left=.02, right=.98, wspace=.05, bottom=.05, top=.95))

axes = [fig.add_subplot(1,3,i, xticks=[], yticks=[]) for i in (1,2,3)]

for ax, spec in zip(axes, specs):
    ax.plot(spec.x, spec.y, spec.x, spec.yfit, 'g', linewidth=1.0)

import matplotlib

if SVG:
    matplotlib.use('svg')

    from matplotlib.backends.backend_svg import FigureCanvas

    cvs = FigureCanvas(fig)

    with open('sample_ses.svg', 'w') as f:
        cvs.print_svg(f)

if PNG:
    matplotlib.use('agg')

    from matplotlib.backends.backend_agg import FigureCanvas

    cvs = FigureCanvas(fig)

    with open('sample_ses.png', 'wb') as f:
        cvs.print_png(f)
コード例 #7
0
def render_anode09_result(filename):
    """ Read in a file with the anode09 result format, return html to render an 
        FROC graph.
        To be able to read this without changing the evaluation
        executable. anode09 results have the following format:

    <?php
        $x=array(1e-39,1e-39,1e-39,1e-39,1e-39,1e-39,1e-39,1e-39,1e-39,0.02,0.02,0.04,0.06,0.06,0.08,0.08,0.0 etc..
        $frocy=array(0,0.00483092,0.00966184,0.0144928,0.0144928,0.0144928,0.0193237,0.0241546,0.0289855,0.02 etc..
        $frocscore=array(0.135266,0.149758,0.193237,0.236715,0.246377,0.26087,0.26087,0.21187);
        $pleuraly=array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.0169492,0.0169492,0.0169492,0.016 etc..
        $pleuralscore=array(0.0508475,0.0508475,0.0677966,0.118644,0.135593,0.152542,0.152542,0.104116);
        $fissurey=array(0,0,0,0.0285714,0.0285714,0.0285714,0.0571429,0.0571429,0.0571429,0.0571429,0.0571429 etc..
        $fissurescore=array(0.171429,0.171429,0.285714,0.314286,0.314286,0.314286,0.314286,0.269388);
        $vasculary=array(0,0.0116279,0.0116279,0.0116279,0.0116279,0.0116279,0.0116279,0.0116279,0.0116279,0. etc..
        $vascularscore=array(0.116279,0.139535,0.186047,0.209302,0.22093,0.244186,0.244186,0.194352);
        $isolatedy=array(0,0,0.0238095,0.0238095,0.0238095,0.0238095,0.0238095,0.047619,0.0714286,0.0714286,0 etc..
        $isolatedscore=array(0.238095,0.261905,0.309524,0.380952,0.380952,0.380952,0.380952,0.333333);
        $largey=array(0,0.0111111,0.0111111,0.0111111,0.0111111,0.0111111,0.0111111,0.0222222,0.0222222,0.022 etc..
        $largescore=array(0.111111,0.122222,0.144444,0.177778,0.177778,0.188889,0.188889,0.15873);
        $smally=array(0,0,0.00854701,0.017094,0.017094,0.017094,0.025641,0.025641,0.034188,0.034188,0.034188, etc..
        $smallscore=array(0.153846,0.17094,0.230769,0.282051,0.299145,0.316239,0.316239,0.252747);
    ?>


        First row are x values, followed by alternating rows of FROC scores for each x value and
        xxxscore variables which contain FROC scores at
        [1/8     1/4    1/2    1     2    4    8    average] respectively and are meant to be
        plotted in a table

        Returns: string containing html/svg instruction to render an anode09 FROC curve
        of all the variables found in file

    """
    # small nodules,large nodules, isolated nodules,vascular nodules,pleural nodules,peri-fissural nodules,all nodules
    variables = parse_php_arrays(filename)
    assert variables != {}, (
        "parsed result of '%s' was emtpy. I cannot plot anything" % filename)
    fig = Figure(facecolor="white")
    canvas = FigureCanvas(fig)
    classes = {
        "small": "nodules < 5mm",
        "large": "nodules > 5mm",
        "isolated": "isolated nodules",
        "vascular": "vascular nodules",
        "pleural": "pleural nodules",
        "fissure": "peri-fissural nodules",
        "froc": "all nodules",
    }
    for key, label in classes.items():
        fig.gca().plot(variables["x"],
                       variables[key + "y"],
                       label=label,
                       gid=key)
    fig.gca().set_xlim([10**-2, 10**2])
    fig.gca().set_ylim([0, 1])
    fig.gca().legend(loc="best", prop={"size": 10})
    fig.gca().grid()
    fig.gca().grid(which="minor")
    fig.gca().set_xlabel("Average FPs per scan")
    fig.gca().set_ylabel("Sensitivity")
    fig.gca().set_xscale("log")
    fig.set_size_inches(8, 6)
    return canvas_to_svg(canvas)
コード例 #8
0
def main():
    args = parse_args()

    print('Starting Parameters')
    print('-------------------')
    print('Backend:', args.backend)
    print('Coefficients:', args.coefficients)
    print('Target r-squared:', args.rsq)
    print('Number of points:', args.steps)
    print('RNG seed:', args.seed)

    if args.outfile:
        print('Output file:', args.outfile)
        print('Noise density:', args.density)

    pargs, kwargs = decode_args(args)
    params = noisy_fit(*pargs, **kwargs)

    info = params['input']
    print('\nInput Line')
    print('----------')
    print('Coefficients:', info.a, info.b)
    print('Mean:', info.data.mean)

    print('\nMixed Data')
    print('----------')
    print('Mean:', params['ydata'].mean)
    print('Noise Width:', params['noise_width'])

    info = params['output']
    print('\nOutput Line')
    print('-----------')
    print('Coefficients:', info.a, info.b)
    print('Mean:', info.data.mean)
    print('R-squared:', info.rsq)
    print('Relative Error:', info.rsq / args.rsq - 1)

    if args.outfile:
        from matplotlib.figure import Figure, SubplotParams
        import matplotlib

        fig = Figure(figsize=(6, 4.5),
                     subplotpars=SubplotParams(left=0.04,
                                               bottom=0.05,
                                               right=0.96,
                                               top=0.95))

        ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[])

        xdata = params['xdata']
        ydata = params['ydata']
        yIn = params['input'].data
        yOut = params['output'].data
        rng = kwargs['rng']
        N = args.steps

        ax.plot(xdata, yIn, 'b', xdata, yOut, 'g', linewidth=1.0, alpha=0.5)

        drops = [
            int(round(N * rng.random(), 0))
            for i in range(int(round((1.0 - args.density) * N, 0)))
        ]
        drops.sort(reverse=True)

        xdata, ydata = map(list, [xdata, ydata])

        for i in drops:
            del xdata[i]
            del ydata[i]

        ax.plot(xdata, ydata, 'k.', markersize=1.0)

        if args.outfile.lower().endswith('.svg'):
            from matplotlib.backends.backend_svg import FigureCanvas
            mpl_backend = 'svg'
            file_mode = 'w'

        else:
            from matplotlib.backends.backend_agg import FigureCanvas
            mpl_backend = 'agg'
            file_mode = 'wb'

        matplotlib.use(mpl_backend)
        cvs = FigureCanvas(fig)

        with open(args.outfile, file_mode) as f:
            getattr(cvs, 'print_' + f.name[-3:].lower())(f)
コード例 #9
0
def survival_plot(age, tumour_size, sex, race, laterality, site, insurance, tumour_behaviour,
                    sx_nosx, sx_gtr, sx_str, sx_resection,
                    sx_partial, sx_local, sx_radical, sx_other):

    # Rescale age and tumour size by a factor of 10
    age_dict = {'age_at_diagnosis': float(age)/10.}
    tumour_size_dict = {'cs_tumor_size': float(tumour_size)/10.}

    # Onehot encode input features as a Pandas Series
    # Sex: 0=Male, 1=Female
    if sex == 0:
        sex_Female = 0
    elif sex == 1:
        sex_Female = 1
    sex_dict = {'sex_Female': sex_Female}

    # Race: 0=White, 1=Black, 2=Other
    if race == 0:
        race_jrecode_White = 1
        race_jrecode_Other = 0
    elif race == 1:
        race_jrecode_White = 0
        race_jrecode_Other = 0
    elif race == 2:
        race_jrecode_White = 0
        race_jrecode_Other = 1
    race_dict = {'race_jrecode_White': race_jrecode_White, 'race_jrecode_Other': race_jrecode_Other}

    # Site: 0=Cerebral m, 1=Spinal m, 2=Other, 3=Meninges NOS
    if site == 0:
        primary_site_jrecode_Meninges_NOS = 0
        primary_site_jrecode_Spinal_meninges = 0
        primary_site_jrecode_Other = 0
    elif site == 1:
        primary_site_jrecode_Meninges_NOS = 0
        primary_site_jrecode_Spinal_meninges = 1
        primary_site_jrecode_Other = 0
    elif site == 2:
        primary_site_jrecode_Meninges_NOS = 0
        primary_site_jrecode_Spinal_meninges = 0
        primary_site_jrecode_Other = 1
    elif site == 3:
        primary_site_jrecode_Meninges_NOS = 0
        primary_site_jrecode_Spinal_meninges = 0
        primary_site_jrecode_Other = 1
    site_dict = {'primary_site_jrecode_Meninges NOS': primary_site_jrecode_Meninges_NOS,
                 'primary_site_jrecode_Spinal meninges': primary_site_jrecode_Spinal_meninges,
                 'primary_site_jrecode_Other': primary_site_jrecode_Other}

    # Laterality: 0=Not bilateral, 1=Midline, 2=Bilateral
    if laterality == 0:
        laterality_jrecode_Bilateral = 0
        laterality_jrecode_Midline = 0
    elif laterality == 1:
        laterality_jrecode_Bilateral = 0
        laterality_jrecode_Midline = 1
    elif laterality == 2:
        laterality_jrecode_Bilateral = 1
        laterality_jrecode_Midline = 0
    laterality_dict = {'laterality_jrecode_Bilateral': laterality_jrecode_Bilateral,
                       'laterality_jrecode_Midline': laterality_jrecode_Midline}

    # Insurance: 0=Insured, 1=Uninsured, 2=Unknown
    if insurance == 0:
        insurance_jrecode_Uninsured = 0
        insurance_jrecode_Unknown = 0
    elif insurance == 1:
        insurance_jrecode_Uninsured = 1
        insurance_jrecode_Unknown = 0
    elif insurance == 2:
        insurance_jrecode_Uninsured = 0
        insurance_jrecode_Unknown = 1
    insurance_dict = {'insurance_jrecode_Uninsured': insurance_jrecode_Uninsured,
                      'insurance_jrecode_Unknown': insurance_jrecode_Unknown}

    # Tumour behaviour: 0=Unknown, 1=Benign m, 2=Borderline malignancy, 3=Malignant
    if tumour_behaviour == 0:
        behavior_code_Malignant = 0
        behavior_code_Borderline_malignancy = 0
    elif tumour_behaviour == 1:
        behavior_code_Malignant = 0
        behavior_code_Borderline_malignancy = 0
    elif tumour_behaviour == 2:
        behavior_code_Malignant = 0
        behavior_code_Borderline_malignancy = 1
    elif tumour_behaviour == 3:
        behavior_code_Malignant = 1
        behavior_code_Borderline_malignancy = 0
    tumour_behaviour_dict = {'behavior_code_Malignant': behavior_code_Malignant,
                             'behavior_code_Borderline malignancy': behavior_code_Borderline_malignancy}

    # Surgery
    no_sx_dict = {'surgery_jrecode_Other surgery': 0, 'surgery_jrecode_20: Local excision': 0,
    'surgery_jrecode_21: Subtotal resection (brain)': 0, 'surgery_jrecode_22: Resection (spinal cord or nerve)': 0,
    'surgery_jrecode_30: Radical': 0, 'surgery_jrecode_40: Partial resection of lobe': 0,
    'surgery_jrecode_55: Gross total resection (lobectomy)': 0}
    gtr_dict = {'surgery_jrecode_Other surgery': 0, 'surgery_jrecode_20: Local excision': 0,
    'surgery_jrecode_21: Subtotal resection (brain)': 0, 'surgery_jrecode_22: Resection (spinal cord or nerve)': 0,
    'surgery_jrecode_30: Radical': 0, 'surgery_jrecode_40: Partial resection of lobe': 0,
    'surgery_jrecode_55: Gross total resection (lobectomy)': 1}
    str_dict = {'surgery_jrecode_Other surgery': 0, 'surgery_jrecode_20: Local excision': 0,
    'surgery_jrecode_21: Subtotal resection (brain)': 1, 'surgery_jrecode_22: Resection (spinal cord or nerve)': 0,
    'surgery_jrecode_30: Radical': 0, 'surgery_jrecode_40: Partial resection of lobe': 0,
    'surgery_jrecode_55: Gross total resection (lobectomy)': 0}
    resection_dict = {'surgery_jrecode_Other surgery': 0, 'surgery_jrecode_20: Local excision': 0,
    'surgery_jrecode_21: Subtotal resection (brain)': 0, 'surgery_jrecode_22: Resection (spinal cord or nerve)': 1,
    'surgery_jrecode_30: Radical': 0, 'surgery_jrecode_40: Partial resection of lobe': 0,
    'surgery_jrecode_55: Gross total resection (lobectomy)': 0}
    partial_dict = {'surgery_jrecode_Other surgery': 0, 'surgery_jrecode_20: Local excision': 0,
    'surgery_jrecode_21: Subtotal resection (brain)': 0, 'surgery_jrecode_22: Resection (spinal cord or nerve)': 0,
    'surgery_jrecode_30: Radical': 0, 'surgery_jrecode_40: Partial resection of lobe': 1,
    'surgery_jrecode_55: Gross total resection (lobectomy)': 0}
    local_dict = {'surgery_jrecode_Other surgery': 0, 'surgery_jrecode_20: Local excision': 1,
    'surgery_jrecode_21: Subtotal resection (brain)': 0, 'surgery_jrecode_22: Resection (spinal cord or nerve)': 0,
    'surgery_jrecode_30: Radical': 0, 'surgery_jrecode_40: Partial resection of lobe': 0,
    'surgery_jrecode_55: Gross total resection (lobectomy)': 0}
    radical_dict = {'surgery_jrecode_Other surgery': 0, 'surgery_jrecode_20: Local excision': 0,
    'surgery_jrecode_21: Subtotal resection (brain)': 0, 'surgery_jrecode_22: Resection (spinal cord or nerve)': 0,
    'surgery_jrecode_30: Radical': 1, 'surgery_jrecode_40: Partial resection of lobe': 0,
    'surgery_jrecode_55: Gross total resection (lobectomy)': 0}
    other_dict = {'surgery_jrecode_Other surgery': 1, 'surgery_jrecode_20: Local excision': 0,
    'surgery_jrecode_21: Subtotal resection (brain)': 0, 'surgery_jrecode_22: Resection (spinal cord or nerve)': 0,
    'surgery_jrecode_30: Radical': 0, 'surgery_jrecode_40: Partial resection of lobe': 0,
    'surgery_jrecode_55: Gross total resection (lobectomy)': 0}
    surgery_coding = {
        'nosx': ('No surgery', no_sx_dict),  # 0
        'gtr': ('Gross total resection (lobectomy)', gtr_dict),  # 6
        'str': ('Subtotal resection (brain)', str_dict),  # 2
        'resection': ('Resection (spinal cord or nerve)', resection_dict),  # 3
        'partial': ('Partial resection of lobe', partial_dict),  # 5
        'local': ('Local excision', local_dict),  # 1
        'radical': ('Radical', radical_dict),  # 4
        'other': ('Other surgery', other_dict),  # 7
    }
    # dict of sx key and 0 or 1 value indicating state of switch
    sx_args = {'nosx': sx_nosx,
               'gtr': sx_gtr,
               'str': sx_str,
               'resection': sx_resection,
               'partial': sx_partial,
               'local': sx_local,
               'radical': sx_radical,
               'other': sx_other}
    # list of tuples of (Title, sx feature array) for each surgery to plot
    sx_to_plot = []
    for sx_name, sx_bool in sx_args.items():
        if sx_bool == 1:
            sx_to_plot.append(surgery_coding[sx_name])

    # Reason no surgery dicts
    nosx_contraindicated_dict = {'surgery_status_jrecode_Not recommended, contraindicated': 1,
                                 'surgery_status_jrecode_Recommended but not performed, patient refused': 0,
                                 'surgery_status_jrecode_Recommended but not performed, unknown reason': 0}
    nosx_ptrefused_dict = {'surgery_status_jrecode_Not recommended, contraindicated': 0,
                           'surgery_status_jrecode_Recommended but not performed, patient refused': 1,
                           'surgery_status_jrecode_Recommended but not performed, unknown reason': 0}
    sxother_dict = {'surgery_status_jrecode_Not recommended, contraindicated': 0,
                           'surgery_status_jrecode_Recommended but not performed, patient refused': 0,
                           'surgery_status_jrecode_Recommended but not performed, unknown reason': 0}


    # list of tuples of (Title, complete feature array) for each surgery to plot
    feature_arrays_to_plot = []
    for sx in sx_to_plot:
        title = sx[0]
        sx_features = sx[1]

        if title == 'No surgery':
            title1 = 'No surgery (contraindicated)'
            # features1 = [[age] + [tumour_size] + sexvar + racevar +
            #               sitevar + lateralityvar + sx_features + [0, 1, 0]]
            features1 = {**age_dict, **tumour_size_dict, **tumour_behaviour_dict, **sex_dict, **race_dict,
                         **site_dict, **laterality_dict, **insurance_dict, **sx_features, **nosx_contraindicated_dict}
            title2 = 'No surgery (patient refused)'
            features2 = {**age_dict, **tumour_size_dict, **tumour_behaviour_dict, **sex_dict, **race_dict,
                         **site_dict, **laterality_dict, **insurance_dict, **sx_features, **nosx_ptrefused_dict}
            title3 = 'No surgery (not recommended)'
            features3 = {**age_dict, **tumour_size_dict, **tumour_behaviour_dict, **sex_dict, **race_dict,
                         **site_dict, **laterality_dict, **insurance_dict, **sx_features, **sxother_dict}
            feature_arrays_to_plot.append((title1, features1))
            feature_arrays_to_plot.append((title2, features2))
            feature_arrays_to_plot.append((title3, features3))
        else:
            features = {**age_dict, **tumour_size_dict, **tumour_behaviour_dict, **sex_dict, **race_dict,
                         **site_dict, **laterality_dict, **insurance_dict, **sx_features, **sxother_dict}
            feature_arrays_to_plot.append((title, features))

    # list of tuples of (Title, results)
    surv_preds_to_plot = []
    for sx in feature_arrays_to_plot:
        title = sx[0]
        features = sx[1]
        surv_pred = surv_clf.predict_survival_function(pd.Series(features, dtype=float)).T.values[0]
        surv_preds_to_plot.append((title, surv_pred))

    ###### Plot Figure #####
    sns.set(font_scale=1.2)
    sns.set_style('white',{'axes.linewidth': 2.5, 'axes.edgecolor': '#D3D3D3'})
    sns.set_palette('colorblind')
    fig = Figure()
    ax = fig.add_subplot(1, 1, 1)

    for title, surv_pred in surv_preds_to_plot:
        ax.step(np.arange(len(surv_pred)), surv_pred*100, where="post", label=title, lw=2)
    ax.legend(loc='lower left', borderaxespad=0.,  prop={'size': 12})

    ax.set_ylabel('Probability of survival (%)', size=15)
    ax.set_xlabel('Time (months)', size=15)
    ax.title.set_position([.5, 1.05])

    for axis in ['top','bottom','left','right']:
        ax.spines[axis].set_linewidth(2)
    sns.despine(ax=ax)
    ax.yaxis.grid(color='#D3D3D3', linestyle='--')
    fig.set_tight_layout(True)


    ## Write plot to SVG
    canvas = FigureCanvas(fig)
    output = BytesIO()
    canvas.print_svg(output)
    response = make_response(output.getvalue())
    response.mimetype = 'image/svg+xml'
    return response
コード例 #10
0
from matplotlib.backends.backend_svg import FigureCanvasSVG as FigureCanvas
#from matplotlib.backends.backend_cairo import FigureCanvasCairo as FigureCanvas
from matplotlib.figure import Figure

xlim = [0, 10.0]
YMAX = 240.00
ylim = [0, YMAX]
fig = Figure()
canvas = FigureCanvas(fig)
#xticks = [ 1,2,3,4,5,6,7,8  ]
ax = fig.add_subplot(111, xlim=xlim, ylim=ylim, xticks=range(10))
data = [[False, False], [1, 200], [4, 100], [6, 120]]
#ax.plot(data)
D = dict(data)

ax.stem(D.keys(), D.values(), '-.')

ax.set_title('hi mom')
ax.grid(True)
ax.set_xlabel('time')
ax.set_ylabel('glucose')
canvas.print_figure('test')

#####
# EOF
コード例 #11
0
def plot(request):
    """Example plot in django."""

    image_type = 'png'  # svg or png

    # https://scipy-cookbook.readthedocs.io/items/Matplotlib_Django.html
    import random
    import django
    import datetime
    import matplotlib
    import numpy as np
    from matplotlib.pyplot import figure
    from matplotlib.figure import Figure
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    import matplotlib.cbook as cbook

    if image_type == 'svg':
        from matplotlib.backends.backend_svg import FigureCanvasSVG as FigureCanvas
        # todo: the svg output is kind of verbose
        # fix is here: https://stackoverflow.com/questions/34387893/output-matplotlib-figure-to-svg-with-text-as-text-not-curves
    elif image_type == 'png':
        from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas

    years = mdates.YearLocator()  # every year
    months = mdates.MonthLocator()  # every month
    yearsFmt = mdates.DateFormatter('%Y')

    from matplotlib.figure import Figure
    # from matplotlib.dates import DateFormatter

    all_entries = Price.objects.all().order_by('time')

    dates = []
    prices = []

    for entry in all_entries:
        dates.append(datetime.datetime.fromtimestamp(entry.time))
        prices.append(entry.high)

    fig = figure()
    # Firs plot
    ax = fig.add_subplot(211)
    ax.plot(dates, prices)

    # Format the ticks
    ax.xaxis.set_major_locator(years)
    ax.xaxis.set_major_formatter(yearsFmt)
    ax.xaxis.set_minor_locator(months)

    # Second plot
    x = [1, 2, 3, 4, 5, 6]
    y = [50, 12, 88, 43, 23, 89]
    bx = fig.add_subplot(212)
    bx.plot(x, y)

    # Third plot
    x2 = [1, 2, 3, 4, 5, 6]
    y2 = [50, 12, 88, 43, 23, 89]
    bx = fig.add_subplot(21)
    bx.plot(x2, y2)

    canvas = FigureCanvas(fig)

    if image_type == 'svg':
        response = django.http.HttpResponse(content_type='image/svg+xml')
        canvas.print_svg(response)
    elif image_type == 'png':
        response = django.http.HttpResponse(content_type='image/png')
        canvas.print_png(response)

    return response