def decomposition(request, file_id): context = {} obj = models.DecompositionFile.objects.filter(file_id=file_id) if not obj.exists(): raise Http404 file_name = obj.first().file_name context['file'] = { 'decomp_file_id': file_id, 'decomp_file_name': file_name } cookie = get_cookie(request) if cookie != None: context["file"]['hash_id'] = cookie["hash_id"] context["file"]['file_name'] = None file = File(obj.first().file) context['lines'] = [] # return top 10 lines with file as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as m: count = 0 while True: line = m.readline().strip() line = line.split(b'/') element = line[0].decode('utf-8').split() value = float(line[1]) context['lines'].append({'cycle': element, 'value': value}) count += 1 if m.tell() == m.size() or count == 10: break return render(request, 'decomposition.html', context)
def GetImg_top_10(file_id): # process scripts img_obj = models.CycleImg.objects.filter(file_id=file_id) if img_obj.exists() and len(img_obj) == 10: # won't plot if 10 imgs were ploted before return False file_obj = models.DecompositionFile.objects.get(file_id=file_id) file = File(file_obj.file) with file as file, mmap.mmap(file.fileno(),0,access=mmap.ACCESS_READ) as m: elements = [] values = [] ele_append = elements.append val_append = values.append count = 0 while True: line = m.readline().strip() line = line.split(b'/') element = line[0].decode('utf-8').split() value = float(line[1]) ele_append(element) val_append(value) count += 1 if m.tell()==m.size() or count >=10: break # # sort file # a = list(zip(range(0,len(values)), values)) # del values # dtype = [('eles_inx', np.int_),('vals', np.float_)] # arr = np.array(a, dtype=dtype) # arr = np.sort(arr, order='vals') # arr = arr[-10:] # top ten # arr = arr[::-1] # reverse array # for i in arr: # element = elements[i['eles_inx']] # value = i['vals'] # lines.append({'cycle':element, 'value':value}) # del elements, arr # # end sort file pending_del = [] for line_inx in range(0, count): img_id=file_id+'_'+str(line_inx) if models.CycleImg.objects.filter(img_id=img_id).exists(): # if this data is not in the database pending_del.append(line_inx) for i in sorted(pending_del, reverse=True): del elements[i] del values[i] inputs = zip(elements, values) pool = Pool() res = pool.map(plot,inputs) for i in range(0,len(res)): instance = res[i]['buffer'] img = ImageFile(instance) obj = models.CycleImg(img_id=file_id+'_'+str(i), value=res[i]['val'], file_id=file_id) obj.img.save(file_id+'_'+str(i)+'.png', img) obj.save() instance.close() pool.close() return True