I have NoneType function return, how do I print/convert it in django views?

226 views Asked by At

I used lifelines library to do survival analysis, a function called CoxPHFitter().print_summary() return a NoneType object, here is my code in survat.py:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import lifelines
from lifelines import CoxPHFitter,AalenAdditiveFitter
import os

def printcox(*args):
    module_dir = os.path.dirname(__file__)  # get current directory
    file_path = os.path.join(module_dir, 'prostate1.csv')
    prostate_dataset=pd.read_csv(file_path)
    # prostate_dataset=pd.read_csv(args[0])
    prostate_dataset=prostate_dataset[list(args[1:])]

    for x in prostate_dataset:
        if (prostate_dataset[x].dtype=="object"):
            cat=pd.Series(list(prostate_dataset[x]))
            for y in pd.unique(cat):
                prostate_dataset[x+'.'+y]=pd.get_dummies(cat)[y]
            prostate_dataset.drop(x, axis=1, inplace=True)
    #        prostate_dataset=prostate
    #        print prostate_dataset
        else:
            pass
#    for z in prostate_dataset:
#                    print z
    cf=CoxPHFitter()
    cf.fit(prostate_dataset,args[1],event_col=args[2])
    return cf.print_summary()

when I called the function in spyder with , the result's be like: enter image description here

But when I called it in my views.py and try to print it in my .html, it prints nothing because it's NoneType data, what should I do to deal with this?

Here is my views.py:

def listindex(request):
    # Handle file upload
    if request.method == 'POST':

        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.save()
            # ert= form.cleaned_data['aalenMember']
            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('survat.survatapp.views.listindex'))
    else:
        form = DocumentForm() # A empty, unbound form

    # Load documents for the list page
    documents = Document.objects.all()

    def delete_matrix():
        documents = Document.objects.all()
        for document in documents:
            document.delete()

    if request.method == 'POST':
        tempCox = []
        tempAalen = []
        timeColumn = request.POST.get('timeColumn', '')
        tempCox.append(timeColumn)
        tempAalen.append(timeColumn)
        eventColumn = request.POST.get('eventColumn', '')
        tempCox.append(eventColumn)
        tempAalen.append(eventColumn)
        coxMember = request.POST.get('coxMember', '')
        aalenMember = request.POST.get('aalenMember', '')
        coxMemberI = int(float(coxMember))
        aalenMemberI = int(float(aalenMember))
        coxCovariate = []
        aalenCovariate = []
        for j in range(1,coxMemberI):
            coxCovariate.append(request.POST.get('coxCovariate'+str(j), ''))
            tempCox.append(request.POST.get('coxCovariate'+str(j), ''))
        for k in range(1,aalenMemberI):
            aalenCovariate.append(request.POST.get('aalenCovariate'+str(k), ''))
            tempAalen.append(request.POST.get('aalenCovariate'+str(k), ''))
        ert = request.POST.get('coxMember', '')
        ert = str(type(int(float(ert))))
    else:
        tempCox = []
        tempAalen = []
        ert = "niji"
        timeColumn = "niji"
        eventColumn = "niji"
        coxMember = "niji"
        aalenMember = "niji"
        coxMemberI = "niji"
        aalenMemberI = "niji"
        coxCovariate = []
        aalenCovariate = []
        for j in range(1,20):
            coxCovariate.append ("niji")
        for k in range(1,20):
            aalenCovariate.append ("niji")

    asdf=printcox('prostate1.csv','dtime','status1','age','hg','sz','sg','pf','rx')
    # Render list page with the documents and the form
    return render_to_response(
        'survatapp/list.html',
        {'documents': documents, 'form': form, 'ert':ert, 'wer':tempAalen,'asdf':asdf},
        context_instance=RequestContext(request)
    )

and I called it with {{ asdf }} in my .html

thanks in advance :)

0

There are 0 answers