Summary table using Coffeescript? - coffeescript
I've programmed a summary chart, and I'm trying to add a "Totals" line that will sum up three columns. The exercise is comprised of a number of questions, and the participant can increase, decrease or maintain a certain dollar amount. The column B total shows the initial values (where the slider starts off on). The column C shows the amount the participant either increased or decreased and the last column shows the resulting dollar amount. (B + C)
Here's an example of the summary chart
Column A --- B ------ C------ D
Question 1 | 100$ | +28$ | 128$ |
Question 2 | 150$ | (10$) | 140$ |
Totals ------| 250$ | +18$ | 268$ |
So far I've been able to program the totals for column B and D, but I can't figure out how to show a total for column C.
class window.CustomSimulator extends window.TaxSimulator
constructor: (#options = {}) ->
super
#updateTable()
update: ->
super
#updateTable()
updateTable: ->
self = this
$table = $('<table class="table table-condensed"><thead><tr><th>Category</th><th>Before</th><th>Your Choice</th><th>After</th></tr></table>')
$tbody = $('<tbody></tbody>')
before_total = 0
after_total = 0
#scope.find('.slider').each ->
$this = $(this)
$parents = $this.parents('tr')
category = $parents.find('.header').clone().children().remove().end().text().replace(/How would you adjust service levels and property tax funding for /, '').replace('?', '')
before = self.tipScaler($this, $this.data('initial'))
your_choice = $parents.find('.value').text()
after = $parents.find('.tip-content').text()
if $parents.find('.key').text() == 'Decrease:'
css_class = 'decrease'
your_choice = "(#{your_choice})"
else
css_class = 'increase'
$tr = $("""<tr><td>#{category}</td><td>#{before}</td><td class="table-#{css_class}">#{your_choice}</td><td>#{after}</td></tr>""")
$tbody.append($tr)
before_total += parseFloat(before.replace('$', ''))
after_total += parseFloat(after.replace('$', ''))
before_total = SimulatorHelper.number_to_currency(before_total, precision: 2, strip_insignificant_zeros: true)
after_total = SimulatorHelper.number_to_currency(after_total, precision: 2, strip_insignificant_zeros: true)
$("""<tfoot><tr><th>Totals</th><th>#{before_total}</th></th><th><th>#{after_total}</th></tr></tfoot>""").appendTo($table)
$table.append($tbody)
$('#summary-table').html($table)
I'm pretty new at this so I'm not sure if this is enough information.
Thanks!
Related
Talend - split a string to n rows
I would like to split a string in a column to n rows in Talend. For example : column 2aabbccdd The first number is the "n" which I use to define the row lenght, so the expected result should be : row 1 = aa row 2 = bb row 3 = cc row 4 = dd The idea here is to iterate on the string and cut it every 2 characters. Any idea please ?
I would use a tJavaFlex to split the string, with a trick to have n rows coming out of it. tJavaFlex's main code: int n = Integer.parseInt(row1.str.substring(0, 4)); //get n from the first 4 characters String str2 = row1.str.substring(4); //get the string after n int nbParts = (str2.length() + 1) / n; System.out.println("number of parts = " + nbParts); for (int i = 0; i < nbParts; i++) { String part = str2.substring(i * n); if(part.length() > n) { part = part.substring(0, n); } row2.str = part; And tJavaFlex's end code is just a closing brace: } The trick is to use a for loop in the main code, but only close it in the end code. tFixedFlowInput contains just one column holding the input string.
PowerBI Cumulative Distinctcount by Date, Condition and through Dimension
I have the following Table: It represents cases on which a certain Team is working on over the Time until the case is closed. And there is also a Date Table over column Date. I would like to cumulative count the open cases until the selected date. So I used this measure: CountOpen = VAR CurrentDate = MAX('Date'[Date]) VAR Closed = CALCULATE( DISTINCTCOUNT(Tabelle1[case]), ALL('Date'),'Date'[Date]<=CurrentDate,Tabelle1[Status_Open]="0") VAR OpenAll = CALCULATE( DISTINCTCOUNT(Tabelle1[case]), ALL('Date'),'Date'[Date]<=CurrentDate,Tabelle1[Status_Open]="1") RETURN OpenAll-Closed And it works for the overall view. But for the view within the Dimension CurrentTeam it's not correct: It should be: a = 0 b = 1 c = 0
So... this is actually quite tricky, you have to pick the latest status per case up to the selected date. In my solution I create a table, with a column R which ranks the cases per date, then in the result I filter for those depending on which team you have selected. Measure is below: VAR CurrentDate = MAX('Date'[Date]) VAR CurrentTeam = SELECTEDVALUE(Tabelle1[CurrentTeam]) VAR tbl = SUMMARIZE( FILTER(ALL('Tabelle1'), 'Tabelle1'[Date] <= CurrentDate), Tabelle1[case], Tabelle1[CurrentTeam], Tabelle1[Status_Open], Tabelle1[Date], "R", VAR c = MAX(Tabelle1[case]) VAR d = LASTDATE(Tabelle1[Date]) RETURN CALCULATE(DISTINCTCOUNT(Tabelle1[Date]), ALLSELECTED(Tabelle1), Tabelle1[case] = c, Tabelle1[Date] >= d) ) RETURN SUMX( FILTER(tbl, [R] = 1 && (ISBLANK(CurrentTeam) || [CurrentTeam] = CurrentTeam) && [Status_Open]) , 1) + 0 //+0 is here to show 0 where it would be blank
Apple turicreate always return the same label
I'm test-driving turicreate, to resolve a classification issue, in which data consists of 10-uples (q,w,e,r,t,y,u,i,o,p,label), where 'q..p' is a sequence of characters (for now of 2 types), +,-, like this: q,w,e,r,t,y,u,i,o,p,label -,+,+,e,e,e,e,e,e,e,type2 +,+,e,e,e,e,e,e,e,e,type1 -,+,e,e,e,e,e,e,e,e,type2 'e' is just a padding character, so that vectors have a fixed lenght of 10. note:data is significantly tilted toward one label (90% of it), and the dataset is small, < 100 points. I use Apple's vanilla script to prepare and process the data (derived from here): import turicreate as tc # Load the data data = tc.SFrame('data.csv') # Note, for sake of investigating why predictions do not work on Swift, the model is deliberately over-fitted, with split 1.0 train_data, test_data = data.random_split(1.0) print(train_data) # Automatically picks the right model based on your data. model = tc.classifier.create(train_data, target='label', features = ['q','w','e','r','t','y','u','i','o','p']) # Generate predictions (class/probabilities etc.), contained in an SFrame. predictions = model.classify(train_data) # Evaluate the model, with the results stored in a dictionary results = model.evaluate(train_data) print("***********") print(results['accuracy']) print("***********") model.export_coreml("MyModel.mlmodel") Note:The model is over-fitted on the whole data (for now). Convergence seems ok, PROGRESS: Model selection based on validation accuracy: PROGRESS: --------------------------------------------- PROGRESS: BoostedTreesClassifier : 1.0 PROGRESS: RandomForestClassifier : 0.9032258064516129 PROGRESS: DecisionTreeClassifier : 0.9032258064516129 PROGRESS: SVMClassifier : 1.0 PROGRESS: LogisticClassifier : 1.0 PROGRESS: --------------------------------------------- PROGRESS: Selecting BoostedTreesClassifier based on validation set performance. And the classification works as expected (although over-fitted) However, when i use the mlmodel in my code, no matter what, it returns always the same label, here 'type2'. The rule is here type1 = only "+" and "e", type2 = all others combinations. I tried using the text_classifier, the results are far less accurate... I have no idea what I'm doing wrong.... Just in case someone wants to check, for a small data set, here's the raw data. q,w,e,r,t,y,u,i,o,p,label -,+,+,e,e,e,e,e,e,e,type2 -,+,e,e,e,e,e,e,e,e,type2 +,+,-,+,e,e,e,e,e,e,type2 -,-,+,-,e,e,e,e,e,e,type2 +,e,e,e,e,e,e,e,e,e,type1 -,-,+,+,e,e,e,e,e,e,type2 +,-,+,-,e,e,e,e,e,e,type2 -,+,-,-,e,e,e,e,e,e,type2 +,-,-,+,e,e,e,e,e,e,type2 +,+,e,e,e,e,e,e,e,e,type1 +,+,-,-,e,e,e,e,e,e,type2 -,+,-,e,e,e,e,e,e,e,type2 -,-,-,-,e,e,e,e,e,e,type2 -,-,e,e,e,e,e,e,e,e,type2 -,-,-,e,e,e,e,e,e,e,type2 +,+,+,+,e,e,e,e,e,e,type1 +,-,+,+,e,e,e,e,e,e,type2 +,+,+,e,e,e,e,e,e,e,type1 +,-,-,-,e,e,e,e,e,e,type2 +,-,-,e,e,e,e,e,e,e,type2 +,+,+,-,e,e,e,e,e,e,type2 +,-,e,e,e,e,e,e,e,e,type2 +,-,+,e,e,e,e,e,e,e,type2 -,-,+,e,e,e,e,e,e,e,type2 +,+,-,e,e,e,e,e,e,e,type2 e,e,e,e,e,e,e,e,e,e,type1 -,+,+,-,e,e,e,e,e,e,type2 -,-,-,+,e,e,e,e,e,e,type2 -,e,e,e,e,e,e,e,e,e,type2 -,+,+,+,e,e,e,e,e,e,type2 -,+,-,+,e,e,e,e,e,e,type2 And the swift code: //Helper extension MyModelInput { public convenience init(v:[String]) { self.init(q: v[0], w: v[1], e: v[2], r: v[3], t: v[4], y: v[5], u: v[6], i: v[7], o: v[8], p:v[9]) } } let classifier = MyModel() let data = ["-,+,+,e,e,e,e,e,e,e,e", "-,+,e,e,e,e,e,e,e,e,e", "+,+,-,+,e,e,e,e,e,e,e", "-,-,+,-,e,e,e,e,e,e,e","+,e,e,e,e,e,e,e,e,e,e"] data.forEach { (tt) in let gg = MyModelInput(v: tt.components(separatedBy: ",")) if let prediction = try? classifier.prediction(input: gg) { print(prediction.labelProbability) } } The python code saves a MyModel.mlmodel file, which you can add to any Xcode project and use the code above. note: the python part works fine, for example: +---+---+---+---+---+---+---+---+---+---+-------+ | q | w | e | r | t | y | u | i | o | p | label | +---+---+---+---+---+---+---+---+---+---+-------+ | + | + | + | + | e | e | e | e | e | e | type1 | +---+---+---+---+---+---+---+---+---+---+-------+ is labelled as expected. But when using the swift code, the label comes out as type2. This thing is driving be berserk (and yes, I checked that the mlmodel replaces the old one whenever i create a new version, and also in Xcode).
Join two hashtables to make one
I have two hash tables and I need to compare them. Let me explain my problem : [hashtable]$User = #{ "Jack" = "AdminLA, AdminUSA"; "John" = "AdminAustralia"; "Sarah" = "AdminIceland"; "Arnold" = "AdminUSA"; "Maurice" = "AdminAustralia, AdminCanada"; } [hashtable]$Profil = #{ "AdminLA" = "P1"; "AdminIceland" = "P2"; "AdminUSA" = "P3"; "AdminCanada" = "P4"; "AdminAustralia" = "P5" ; "AdminCroatia" = "P6"; } I want to have this kind of result : Key Value --- ----- Jack P1, P3 John P5 Sarah P2 Arnold P3 Maurice P5, P4 Actually, I have only one value (I haven't succeeded to have multiple values. For example Jack must have P1 and P3 and I have only P1). How can I fix it? I have already tried: $User.GetEnumerator() | select Key, #{n='Value'; e={$Profil[$_.Value]}} and $User.GetEnumerator() | %{[PSCustomObject]#{aKey=$_.Key;bValue=$Profil[$_.Value]}} Any idea?
Use this expression $User.GetEnumerator() | Select-Object Key, #{name='Value'; expression={($_.Value -split ", " | Foreach-Object {$Profil[$_]}) -join ", "}} This basically creates an array of input values, get the values from $Profil for each element and then creates a string from these values.
Matlab IF and ELSEIF loop
function final = fcn(sensor1, sensor2, sensor3) % resolution = res res = 10; % value1 = ((sensor1+sensor2+sensor3)/3); % | is used for 'or' command if sensor1 > res+sensor2 | sensor1> res+sensor3; value1 = ((sensor2+sensor3)/2); elseif sensor2 > res+sensor1 | sensor2> res+sensor3; value1 = ((sensor1+sensor3)/2); elseif sensor3 > res+sensor1 | sensor3> res+sensor2; value1 = ((sensor1+sensor2)/2); else value1 = ((sensor1+sensor2+sensor3)/3); end final = value1; I want it to display the final value based on the average. If any single value is greater than any of the other two by a certain number (resolution in this case) then it should neglect that number and just use the average of the other two. On matlab, my IF and ELSEIF loop has an error saying 'Parse error at , and Parse error at elseif.
You should have you if and your conditions on the same line. And no semi colon after the conditions: . . . if sensor1 > res+sensor2 || sensor1> res+sensor3 value1 = ((sensor2+sensor3)/2); elseif sensor2 > res+sensor1 || sensor2> res+sensor3 value1 = ((sensor1+sensor3)/2); . . . btw you should be using || in this case because you're dealing with scalars.
You can get rid of almost all conditional statements with some vectorized approach. Additionally, it will automatically scale if you have many sensors as inputs with the same conditions. Code function value = fcn(sensor1, sensor2, sensor3) res = 10; sensor = [sensor1;sensor2;sensor3]; ind_first_cond_met = find(any(bsxfun(#gt,sensor,(res+sensor)'),2),1,'first'); if isempty(ind_first_cond_met) value = mean(sensor); else sum_mat = bsxfun(#plus,sensor,sensor'); mean_every_other_two = [sum_mat(1,2) sum_mat(2,3) sum_mat(3,1)]./2; value = mean_every_other_two(ind_first_cond_met); end