annotate() adding more than needed - python

I'm having a small issue with my annotate(sum()) function. Now what I want it to do is show a total for all maturities in the given plan, inside the list. Which for the first one it does. Code below:
#get the invesments maturing this year
for p in plans:
cur_inv = Investment.objects.all().filter(plan = p).order_by('financial_institution').filter(maturity_date__year = current_year)
nxt_inv = Investment.objects.all().filter(plan = p).order_by('financial_institution').filter(maturity_date__year = next_yr)
thr_inv = Investment.objects.all().filter(plan = p).order_by('financial_institution').filter(maturity_date__year = thr_yr)
fr_inv = Investment.objects.all().filter(plan = p).order_by('financial_institution').filter(maturity_date__year = fr_yr)
fv_inv = Investment.objects.all().filter(plan = p).order_by('financial_institution').filter(maturity_date__year = fv_yr)
for inv in cur_inv:
total += inv.amount or 0
for inv in cur_inv:
total_m += inv.maturity_amount or 0
for inv in nxt_inv:
total2 += inv.amount or 0
for inv in nxt_inv:
total_m2 += inv.maturity_amount or 0
for inv in thr_inv:
total3 += inv.amount or 0
for inv in thr_inv:
total_m3 += inv.maturity_amount or 0
for inv in fr_inv:
total4 += inv.amount or 0
for inv in fr_inv:
total_m4 += inv.maturity_amount or 0
for inv in fv_inv:
total5 += inv.amount or 0
for inv in fv_inv:
total_m5 += inv.maturity_amount or 0
#Calculate the holding totals with each company
total_list = p.investment_set.filter(maturity_date__gte= '%s-1-1' % current_year).values('financial_institution__abbr').annotate(Sum('maturity_amount')).order_by('financial_institution__abbr')
gtotal = total_m + total_m2 + total_m3 + total_m4 + total_m5
plan_list.append({
'plan':p,
'investment': cur_inv,
'nxt_inv': nxt_inv,
'thr_inv': thr_inv,
'fr_inv': fr_inv,
'fv_inv': fv_inv,
'total_list': total_list,
'grand': gtotal,
})
My only issue now, is that when it goes to the next plan, it continues to add to the grand total, instead of going back to 0.
Am I missing something?
Any help would be appreciated.
Thanks

You're using += with the total_m* vars but never resetting them to 0 in your loop. They don't automatically reset, just because a new iteration has started.
FWIW, you should try to optimize your code here. You're generating 6*len(plans) queries, which could be rather costly.

Related

Pivot function results differ from TradingView

I'm using this code to calculate pivot points.
def pivots_low(osc, LBR, LBL):
pivots = []
for i in range(len(osc) - LBR):
pivots.append(0)
pivot = True
if i > LBL:
for j in range(1, LBR + 1):
if osc[i] >= osc[i + j]:
pivot = False
for j in range(1, LBL + 1):
if osc[i] > osc[i - j]:
pivot = False
if pivot is True:
pivots[len(pivots) - 1] = osc[i]
for i in range(LBR):
pivots.append(0)
return pivots
This returns an array with 0's where there's no pivots and the value of the pivot if there is one.
When Comparing the results to TradingView (downloaded csv with pivot points), the only time it matches exactly is when lookback left and right are both 5. Otherwise it deviates in the number of total pivots and the location of some.
But using this code to calculate pivot highs:
def pivots_high(osc, LBR, LBL):
pivots = []
for i in range(len(osc)-LBR):
pivots.append(0)
pivot = True
if i > LBL:
for j in range(1,LBL + 1):
if osc[i] < osc[i-j]:
pivot = False
for j in range(1,LBR + 1):
if osc[i] <= osc[i+j]:
pivot = False
if pivot is True:
pivots[len(pivots)-1] = osc[i]
for i in range(LBR):
pivots.append(0)
return pivots
the results are perfect regardless of lookback values. But the code is almost exactly the same besides comparison.
What is going wrong here? This is day 3 of having this problem and I just cant fix it
To Reproduce:
Load Data:
Full_Data = pd.read_csv(file)
use this simple function to check matches between calculated pivots and TradingView pivots.
def match_pivs(data, pivs_h, pivs_l): //Data is a DataFrame loaded from tradingview csv
global lblh
global lbrh
global lbll
global lbrl
start = lbrh
if lbrl > lbrh:
start = lbrl
match_h = 0
tot_hd = 0
tot_hp = 0
match_l = 0
tot_ld = 0
tot_lp = 0
for i in range(start, len(data)):
if data['PivHigh'][i] != 0 and pivs_h[i-lbrh] != 0:
match_h += 1
if data['PivLow'][i] != 0 and pivs_l[i-lbrl] != 0:
match_l += 1
if data['PivHigh'][i] != 0:
tot_hd += 1
if data['PivLow'][i] != 0:
tot_ld += 1
if pivs_h[i] != 0:
tot_hp += 1
if pivs_l[i] != 0:
tot_lp += 1
print('PivsLow ' + str(tot_lp))
print('DataLows ' + str(tot_ld))
print('MatchesL ' + str(match_l))
print('PivsHigh ' + str(tot_hp))
print('DataHighs ' + str(tot_hd))
print('MatchesH ' + str(match_h))
and to get csv from TradingView:
//#version=5
indicator("Data Script", overlay=true, max_labels_count=500)
leftLenL = input.int(title="Pivot Low", defval=10, minval=1, inline="Pivot Low", group=lengthGroupTitle)
rightLenL = input.int(title="/", defval=10, minval=1, inline="Pivot Low", group=lengthGroupTitle)
leftLenH = input.int(title="Pivot High", defval=10, minval=1, inline="Pivot High", group=lengthGroupTitle)
rightLenH = input.int(title="/", defval=10, minval=1, inline="Pivot High", group=lengthGroupTitle)
ph = ta.pivothigh(leftLenH, rightLenH)
pl = ta.pivotlow(leftLenL, rightLenL)
if not na(ph)
plth := ph
else
plth := 0.0
if not na(pl)
pltl := pl
else
pltl := 0.0
plot(plth, 'PivHigh')
plot(pltl, 'PivLow')
then just download csv with this script loaded.
Run program with these three lines:
pl = pivots_low(Full_Data['low'], lbll, lbrl)
ph = pivots_high(Full_Data['high'], lbrh, lblh)
match_pivs(Full_Data, ph, pl)
Finally found a way.
I still have no idea why that code does not work but I've made a different way that seems to be doing the job 100% to tradingview data.
def checkhl(data_back, data_forward, hl):
if hl == 'high' or hl == 'High':
ref = data_back[len(data_back)-1]
for i in range(len(data_back)-1):
if ref < data_back[i]:
return 0
for i in range(len(data_forward)):
if ref <= data_forward[i]:
return 0
return 1
if hl == 'low' or hl == 'Low':
ref = data_back[len(data_back)-1]
for i in range(len(data_back)-1):
if ref > data_back[i]:
return 0
for i in range(len(data_forward)):
if ref >= data_forward[i]:
return 0
return 1
def pivot(osc, LBL, LBR, highlow)
left = []
right = []
for i in range(len(osc)):
pivots.append(0.0)
if i < LBL + 1:
left.append(osc[i])
if i > LBL:
right.append(osc[i])
if i > LBL + LBR:
left.append(right[0])
left.pop(0)
right.pop(0)
if checkhl(left, right, highlow):
pivots[i - LBR] = osc[i - LBR]
return pivots
then just do:
pivots_low = pivot(data, lbl, lbr, 'low')
pivots_high = pivot(data, lbl, lbr, 'high')
All the pivots will be in the actual position that they occur, not lbr bars after, otherwise the value will be 0.0
I'm not sure if this is efficient or not but it seems to work.

multiprocessing in python error using concurrent.futures AttributeError : Can't get attribute 'simulate_end_season' on <module '__main__' (built-in)>

def simulate_end_season(Parameters, Team,response):
# Update result data in dicts - faster than updating a DataFrame.
points=defaultdict(int)
wins=defaultdict(int)
losses=defaultdict(int)
draws=defaultdict(int)
goals_for=defaultdict(int)
goals_against=defaultdict(int)
Max=10
games_played = 0
# Simulate all the games in a season
for match in response["matches"]:
home_team=match['homeTeam']['name']
away_team=match['awayTeam']['name']
if match['status']=='FINISHED':
home_goals=match['score']['fullTime']['homeTeam']
away_goals=match['score']['fullTime']['awayTeam']
else :
GameResult = SimulateMatch(home_team, away_team, Parameters, Team)
home_goals = GameResult[0]
away_goals =GameResult[1]
# Update the points and win/draw/loss statistics.
if home_goals > away_goals:
points[home_team] += 3
wins[home_team] += 1
losses[away_team] += 1
elif home_goals == away_goals:
points[home_team] += 1
points[away_team] += 1
draws[home_team] += 1
draws[away_team] += 1
else:
points[away_team] += 3
wins[away_team] += 1
losses[home_team] += 1
# Update the goals.
goals_for[home_team] += home_goals
goals_against[home_team] += away_goals
goals_for[away_team] += away_goals
goals_against[away_team] += home_goals
# Return the table as a DataFrame (needs to be sorted on points and goal diff).
# Build the empty table
empty_rows = np.zeros((len(Team),7), dtype=int)
season_table = pd.DataFrame(empty_rows, index=Team,columns=['points', 'wins', 'draws', 'losses', 'goals for', 'goals against', 'goal diff'])
# Fill in the table
for team in Team:
values_list = [points[team], wins[team], draws[team], losses[team], goals_for[team], goals_against[team]]
season_table.loc[team, ['points', 'wins', 'draws', 'losses', 'goals for', 'goals against']] = values_list
# Calculate the goal diff.
season_table.loc[:, 'goal diff']= season_table.loc[:, 'goals for'] - season_table.loc[:, 'goals against']
season_table=season_table.sort_values(['points', 'goal diff'], ascending=[False, False])
rank=[k for k in range(1,len(Team)+1)]
season_table.insert(1,"rank",rank,True)
season_table=season_table.sort_index()
return season_table
def simulate_n_seasons(Team, Parameters, response,n,Max = 10):
start = time.time()
team_position=np.zeros((len(Team),n),dtype=int)
GoalsFor =np.zeros((len(Team),n))
GoalsAgainst= np.zeros((len(Team),n))
Losses = np.zeros((len(Team),n))
Wins = np.zeros((len(Team),n))
Draws = np.zeros((len(Team),n))
Points = np.zeros((len(Team),n))
Name= Team.sort()
with concurrent.futures.ProcessPoolExecutor() as executor:
results= [executor.submit(simulate.simulate_end_season,Params, Team, response) for k in range(n)]
i=0
for f in concurrent.futures.as_completed(results):
season_table = f.result()
team_position[:,i]=list(season_table['rank'])
GoalsFor[:,i]=list(season_table['goals for'])
GoalsAgainst[:,i]=list(season_table['losses'])
Losses[:,i]= list(season_table['losses'])
Draws[:,i]= list(season_table['draws'])
Wins[:,i]= list(season_table['wins'])
Points[:,i]=list(season_table['points'])
i+=1
end = time.time()
Name= sorted(Team)
print(end - start)
return Name,team_position,GoalsFor,GoalsAgainst,Points,Wins,Draws,Losses
Hello everyone, I am using concurrent.futures in order to do multiproscessing for my project in python. I have read that i need to import the function used for multiprocessing from another file, i can run the function simulate_n_seasons on Google colab but on Spyder or Pycharm, i got the following error message which is in the title.
What should i do ?
Thanks for your help.

Python Pulp - Multiple ADP contraints for snake draft

I am new to python/pulp and I am trying to replicate an excel solver snake draft problem in python. I have found a couple of python optimizer solutions for dfs. My model is similar with the exception of instead of a single constraint for salary, I have multiple constraints based on ADP. For instance, if I am in a 10 team league with the 7th pick and I want to optimize my first 8 picks. My 1st ADP constraint would be 8 of my picks would have an ADP higher than 6. 2nd constraint would be at least 7 of my picks would have an ADP higher than 13. 3rd - at least 6 picks have an ADP higher than 26 and so forth for 8 rounds. The suggestion that was made was to make a binary helper column which I have done for each round. This approach seems cumbersome and is not useful if I want to create a loop for each draft position's optimal lineup. Is there a better way to write the code for the Rnd1 thru the Rnd8 sections of the code. Thanks
Fantasy Football Data
availables = players.groupby(["Position", "Rk", "PLAYER", "TEAM", "EPPG", "Rnd1", "Rnd2",
"Rnd3", "Rnd4", "Rnd5", "Rnd6", "Rnd7", "Rnd8"]).agg('count')
availables = availables.reset_index()
ADPs = {}
points = {}
R1s = {}
R2s = {}
R3s = {}
R4s = {}
R5s = {}
R6s = {}
R7s = {}
R8s = {}
lineups_dict = {}
for pos in availables.Position.unique():
available_pos = availables[availables.Position == pos]
point = list(available_pos[['PLAYER', 'EPPG']].set_index("PLAYER").to_dict().values())[0]
R1 = list(available_pos[['PLAYER', 'Rnd1']].set_index("PLAYER").to_dict().values())[0]
R2 = list(available_pos[['PLAYER', 'Rnd2']].set_index("PLAYER").to_dict().values())[0]
R3 = list(available_pos[['PLAYER', 'Rnd3']].set_index("PLAYER").to_dict().values())[0]
R4 = list(available_pos[['PLAYER', 'Rnd4']].set_index("PLAYER").to_dict().values())[0]
R5 = list(available_pos[['PLAYER', 'Rnd5']].set_index("PLAYER").to_dict().values())[0]
R6 = list(available_pos[['PLAYER', 'Rnd6']].set_index("PLAYER").to_dict().values())[0]
R7 = list(available_pos[['PLAYER', 'Rnd7']].set_index("PLAYER").to_dict().values())[0]
R8 = list(available_pos[['PLAYER', 'Rnd8']].set_index("PLAYER").to_dict().values())[0]
points[pos] = point
R1s[pos] = R1
R2s[pos] = R2
R3s[pos] = R3
R4s[pos] = R4
R5s[pos] = R5
R6s[pos] = R6
R7s[pos] = R7
R8s[pos] = R8
pos_num_available = {
"QB": 1,
"RB": 3,
"TE": 1,
"WR": 3,
"DEF": 0,
"K": 0
}
for lineup in range(1,9):
_vars = {k: LpVariable.dict(k, v, cat='Binary') for k, v in points.items()}
prob = LpProblem("Fantasy", LpMaximize)
rewards = []
Rnd_1 = []
Rnd_2 = []
Rnd_3 = []
Rnd_4 = []
Rnd_5 = []
Rnd_6 = []
Rnd_7 = []
Rnd_8 = []
for k, v in _vars.items():
rewards += lpSum([points[k][i] * _vars[k][i] for i in v])
Rnd_1 += lpSum([R1s[k][i] * _vars[k][i] for i in v])
Rnd_2 += lpSum([R2s[k][i] * _vars[k][i] for i in v])
Rnd_3 += lpSum([R3s[k][i] * _vars[k][i] for i in v])
Rnd_4 += lpSum([R4s[k][i] * _vars[k][i] for i in v])
Rnd_5 += lpSum([R5s[k][i] * _vars[k][i] for i in v])
Rnd_6 += lpSum([R6s[k][i] * _vars[k][i] for i in v])
Rnd_7 += lpSum([R7s[k][i] * _vars[k][i] for i in v])
Rnd_8 += lpSum([R8s[k][i] * _vars[k][i] for i in v])
prob += lpSum([_vars[k][i] for i in v]) == pos_num_available[k]
prob += lpSum(rewards)
prob += Rnd_1 >= 8
prob += Rnd_2 >= 7
prob += Rnd_3 >= 6
prob += Rnd_4 >= 5
prob += Rnd_5 >= 4
prob += Rnd_6 >= 3
prob += Rnd_7 >= 2
prob += Rnd_8 >= 1
if not lineup == 1:
prob += (lpSum(rewards) <= total_score-0.01)
prob.solve()
score= str(prob.objective)
constraints = [str(const) for const in prob.constraints.values()]
lineupList = []
for v in prob.variables():
score = score.replace(v.name, str(v.varValue))
if v.varValue !=0:
lineupList.append(v.name)
ADP constraint examples

how to update value in csr_matrix

I have created a csr_matrix of size 16^4 and 16^8. But I need to update the value in csr_matrix, So how can I update the value in sparse matrix.
I have also tried twoByte.toarray()[i] += 1, twoByte.toarray()[0][i] += 1 and twoByte[0].toarray()[i] += 1 but it does not work. below is the code snippet.
feature_matrix_two = csr_matrix((len(files),16**4),dtype=int)
feature_matrix_four = csr_matrix((len(files),16**6),dtype=int)
k=0
byte_feature_file=open('bigramresult.csv','w+')
for file in files:
byte_feature_file.write(file+",")
if(file.endswith("txt")):
with open('byteFiles/'+file,"r") as byte_code:
twoByte = csr_matrix((1,16**4),dtype = int)
fourByte = csr_matrix((1,16**8),dtype = int)
for row in byte_code:
codes = row.rstrip().split(" ")
codes_2g = codes[:-1]
codes_4g = codes[:-2]
for i in range(len(codes_2g)):
codes_2g[i] += codes[i+1]
for i in range(len(codes_4g)):
codes_4g[i] += codes[i+1]+codes[i+2]
twoByteCode = []
for i in codes_2g:
if '??' not in i:
twoByteCode += [int(i,16)]
fourByteCode = []
for i in codes_4g:
if '??' not in i:
fourByteCode += [int(i,16)]
for i in twoByteCode:
twoByte[i] += 1
for i in fourByteCode:
fourByte[i] += 1
byte_code.close()
feature_matrix_two[k] = twoByte
feature_matrix_four[k] = fourByte
for i in feature_matrix_two[k]:
byte_feature_file.write(str(i)+",")
for i in feature_matrix_four[k]:
byte_feature_file.write(str(i)+",")
byte_feature_file.write("\n")
k+=1
From the code, I think you don't need sparse matrix, you can use a dict object, for example:
from collections import defaultdict
twoByte = defaultdict(int)
fourByte = defaultdict(int)

While loop incrementer not functioning properly

Right now, my code is correctly spitting out the first game (identified by start_id) in games. I am trying to increment in the bottom two lines, but the while loop doesn't seem to read the fact that I'm incrementing. So the input of this with start_id 800 and end_id 802 is just the information from 800, for some reason.
Am I using the incrementers correctly? Should I be initializing one of i or start_id elsewhere?
games = console(start_id, end_id)
final_output = []
while start_id < (end_id + 1):
single_game = []
i = 0
game_id = games[i][0]
time_entries = games[i][1][2][0]
play_entries = games[i][1][2][1]
score_entries = games[i][1][2][2]
team_entries = games[i][1][2][3]
bovada = games[i][1][0][0][0]
at_capacity = games[i][1][0][1]
idisagree_yetrespect_thatcall = games[i][1][0][2][0]
imsailingaway = games[i][1][1][0][0]
homeiswheretheheartis = games[i][1][1][1][0]
zipper = zip(time_entries, play_entries, score_entries, team_entries)
for play_by_play in zipper:
single_game.append(game_id)
single_game.append(play_by_play)
single_game.append(bovada)
single_game.append(at_capacity)
single_game.append(idisagree_yetrespect_thatcall)
single_game.append(imsailingaway)
single_game.append(homeiswheretheheartis)
start_id += 1
i += 1
final_output.append(single_game)
return final_output
Your problem is that you initialize the increment-er i inside the while loop so every time your loop iterates i is reset to zero.
Try changing it to:
i = 0
while start_id < (end_id + 1):
...

Categories