how to subset part of data using for-loop

Question: I am a newbie to R programming recently.
I want to transform the existing matrix form to new one.
The matrix that I have as follows,
data()
    1       2       3       4       5       6
1   0.60!   0.29!   1.42!   0.64    1.06    1.02
2   1.33!   0.68!   1.44!   1.44    1.05    1.03
3   0.60    0.67@   0.64    1.37    2.40    1.00
4   1.37    0.29    1.43    0.63    1.05    2.32
5   1.39    0.68    1.40    1.46    1.06    1.02
6   0.63    0.68    0.61    1.46    1.74    1.05
7   1.39    0.68    1.01    0.65    2.43    2.27
....

I've listed the first line with '!' And '@' for easy understanding.
What I want to is,
vari()
    1       2       3       4       5       6       7
1   0.60!   0.29!   1.42!   1.33!   0.68!   1.44!   0.67@
2   0.29    1.42    0.64    0.68    1.44    1.44    0.64
3   1.42    0.64    1.06    1.44    1.44    1.05    1.37
4   0.64    1.06    1.02    1.44    1.05    1.03    2.40
5   1.33    0.68    1.44    0.60    0.67    0.64    0.29
6
….

I would like to list the data in the 1:2 rows and 1:3 columns in one line and locate the (3,2)value to (1,7) in vari matrix.
I think it would be work with the for-loop. I made the code, however, it is my first time to make it. So, it doesn't work well.
This is my trial....
x=matrix(nrow = nrow(data)-2, ncol = 8);

for ( i in 1:(nrow(data)))
{ for ( j in 1:(ncol(data)))
{
  x[i,j]<-((c(as.vector(t(as.matrix(data[i:i+1,j:j+2]))),data[i+2,j+1])))
}
}
x

however, it represent the following sentence
"Error in data[i:i + 1, j:j + 2] : subscript out of bounds"
Please help me!!!
Comment: When you use `j+2` or `j+1` and similarily `I+1` , `i+2`,  for the last column or last row, it gets more 1 or 2 added more than that in the dataset dimensions.   You could either change the code to `1:(nrow(data)-2)` similarly for `j` (not tested)
Answer: You needed to improve your indexing into the second data frame, I have added the n counter for this.
x=matrix(nrow = nrow(data)-2, ncol = 7);

n=1
for ( i in 1:(nrow(data)-3)){
  for ( j in 1:(ncol(data)-2)){
    x[n,]<-c(t(data[i:(i+1),j:(j+2)]),data[i+2,j+1])
    n=n+1
  }
}

 x
     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 0.60 0.29 1.42 1.33 0.68 1.44 0.67
[2,] 0.29 1.42 0.64 0.68 1.44 1.44 0.64
[3,] 1.42 0.64 1.06 1.44 1.44 1.05 1.37
[4,] 0.64 1.06 1.02 1.44 1.05 1.03 2.40
[5,] 1.33 0.68 1.44 0.60 0.67 0.64 0.29

Also be careful i:i+1is different to i:(i+1) which is what you were after.
Comment: geeee............ I really had a great help sir.... Thank you So much!!! thank you and thank you again!!
Comment: That's great! Feel free to accept the answer if it worked for you


I can't set a default value on hook_form_alter()

Question: I have form alter for the user_register_form which is called.
In this hook, I try to set a default value for a textfield but it doesn't work.
I've tried all the syntax possible #default_value, #value, value, default_value, event in the attributes array.
This is my code.
function pre_registration_form_user_register_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $form['field_code_postal']['#cache']['max-age'] = 0;
  $form['field_code_postal']['#default_value'] = 'COUCOU';
  $form['field_code_postal']['#value'] = 'COUCOU';
  $form['field_code_postal']['value'] = 'COUCOU';
  $form['field_code_postal']['value']['#value'] = 'COUCOU';
  $form['field_code_postal']['value']['#default_value'] = 'COUCOU';
  $form['field_code_postal']['value']['value'] = 'COUCOU';
  $form['field_code_postal']['value']['default_value'] = 'COUCOU';
  $form['field_code_postal']['default_value'] = 'COUCOU';

  $form['field_code_postal']['#attributes']['value'] = 'COUCOU1';
  $form['field_code_postal']['#attributes']['#value'] = 'COUCOU2';
  $form['field_code_postal']['#attributes']['default_value'] = 'COUCOU3';
  $form['field_code_postal']['#attributes']['#default_value'] = 'COUCOU4';
}

How can I set a default value?
Comment: $form['field_code_postal'][LANGUAGE_NONE][0]['value']['#default_value']
Comment: Thanks for your answer. It doesn't work, FYI I'm using D8 not D7.
Answer: According to the documentation for hook_form_alter() you can use code similar to the following one.
 $form['field_code_postal']['widget'][0]['value']['#default_value'] ='COUCOU1';

If you want to, for example, fill an entity referenced field, you can use the following code.
$form['field_image']['widget'][0]['target_id']['#default_value'] = $file;

Comment: For future readers, the documentation for `hook_form_alter()` does not describe the structure of a form containing entity fields. The description of the form structure used for an entity field is given in the documentation for that field type.
Answer: For my case, I wanted a default automatic node title for a custom content type.
In hook_form_FORM_ID_alter(), I added a line containing kint($form['title']); which allowed me to see the array structure for the node title field. Then, I changed the default value for the node title with the following code.
$form['title']['widget'][0]['value']['#default_value'] = 'my custom node title';`

Answer: To set the default value in hook_form_alter(), make a dump of $form. You will see the array structure and what you need to target.
In my case with a webform form, actually I can do this.
/**
 * Implements hook_form_alter().
 */
function my_module_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'form_submission_lorem_add_form') {
    if (isset($form['elements']['nom'])) {
      $form['elements']['nom']['#default_value'] = 'hello world!';
    }
  }
}

The form markup then includes the following lines.
Answer: In Drupal 8:
$form['field_name']['widget']['#default_value'][0] = 'value';


Setting a default value on pageload with JQuery Tablesorter

Question: I was wondering how to have a default filter value in tablesorter. I have a parameter being passed from one page to the next, and I want that parameter to be shown in the search box. I have tried to set the value after the page is ready by using
<code>$(document).ready(function() {
        $("#listTable").tablesorter({ headers: {0:{ sorter: 'digit'}}, debug: false, sortList: [[0, 0]], widgets: ['zebra'] })
                    .tablesorterPager({ container: $("#pager"), positionFixed: false })
                    .tablesorterFilter({ filterContainer: $("#filter-box"),
                        filterClearContainer: $("#filter-clear-button"),
                        filterColumns: [1, 2, 3, 4],
                        filterCaseSensitive: false
                    });
        $('#filter-box').val("${showAccounts}");
  });;
</code>
This does display the showAccounts parameter inside of the "filter-box" input, but it doesn't filter by it when the page loads. Is there any way to do this?
Thanks!
Answer: You'll need to trigger filterEnd in your page load as well. Adding the below snippet should work.
<code>$("#listTable").trigger("filterEnd");
</code>


Under the transformation $ w=\sqrt \frac{1-iz}{z-i}$,

Question: Under the transformation $\displaystyle w=\sqrt \frac{1-iz}{z-i}$, the region $D=\{z \in \mathbb{C} : |z|\leq1\}$ is transformed to

$1).\displaystyle \{z \in \mathbb{C} :0< arg (z)< \pi\}$
$2).\displaystyle\{z \in \mathbb{C} :-\pi< arg (z)< 0\}$
$3).\displaystyle \{z \in \mathbb{C} :0< arg (z)< \frac{\pi}{2}
\;or \;  \pi< arg (z)< \frac{3\pi}{2}\
\}$
$4).\displaystyle\{z \in \mathbb{C} :\frac{\pi}{2} < arg (z)< \pi\;\;or\;{\frac{3\pi}{2} :< arg (z)< 2\pi\}
}$

As i try to solve the question by taking random value of $z$  let take $z=0$ ,thus after the calculation i get $w=\sqrt i$ ,whose principal value i calculated and got $\displaystyle\frac{\pi}{4}$
 so the answer can be $1$ or $3$ but further i don't have idea how to solve ,because if i try for another value of $z$ calculations becomes bit tricky
Please help
Thank you
Comment: In 3 there is misprint. Must be $\pi<\text{arg}z<\frac{3\pi}{2}$, and 3 -- is the right answer.
Comment: I checked it in real paper and you are correct ,can you please tell me how to think about this question?
Comment: Express $z$ in terms of $w$ (i.e. as a function $z(w)$ in $w$) and then use the inequality $|z(w)|\leq 1$ to figure out the region.
Comment: @GoranMalic This will be very lengthy and difficult to solve.
Answer: Consider $w_1=\dfrac{1-iz}{z-i}$. Since the pole $i\in\partial G$, then the image of $\partial G$ -- is straight line. We'll find her by two points: since $w_1(1)=1$, $w_1(-1)=-1$, then $w_1(\partial G)=\mathbb{R}$. Since $w_1(0)=i$, then $w_1(G)$ is upper half plane. Here is $|w_1|>0$ and $0<\text {arg}w_1<\pi$
Now consider $w=\sqrt {w_1}$. We need to find the image of upper half plane. But we know that $\sqrt{w_1}=\sqrt{|w_1|}e^{i\frac{{arg}w_1+2\pi k}{2}}$, $k=0,1$. All that's left is view possible values of fraction $\displaystyle\frac{{arg}w_1+2\pi k}{2}$.
Comment: I fixed some misprints in the answer.
Comment: +1 for such a beautiful explanation.


Check value if exist with uppercase and lowercase letters

Question: I want to check if username exists in Database but I have problem Ex : If I have username = jack and another register with username = JaCk
It accepts
I want to check the full username with lowercase and uppercase and refuse such cases (jack - Jack - JaCk ....)
$stmtr = $DB_con->prepare("SELECT username FROM users WHERE username=:username");
$stmtr->bindparam(":username", $val['username']);
$stmtr->execute();
$row=$stmtr->fetch(PDO::FETCH_ASSOC);

if($rowr['username']==$val['username']) {
echo "Exists";
}

Comment: What database server software is in use?
Comment: What did you set for collation? You also don't need a second comparison, if you get a result it matched..
Answer: There is a typo. In line:
if($rowr['username']==$val['username']) {

it should be:
if($row['username']==$val['username']) {

Also, by default, MySQL performs case-insensitive query. Note the _ci part in your DB collation (e.g. utf8_general_ci), it means case insensitive. You have to perform extra checking in PHP to check for different upper- and lowercase.
Comment: Changing the collations is also an option, then just need to check if there is a result.
Comment: @Raptor whats is perform extra checking in PHP to check for different upper- and lowercase. ?? thats whats I want
Comment: @user3783243 Agree, but for compatibility and migration-ability, I suggest to use case-insensitive collations. See [this](https://stackoverflow.com/questions/11578591/what-are-the-advantages-and-disadvantages-of-explicitly-specifying-collation-in) for discussion in details.
Comment: The PHP comparison checking `==` already can check for upper and lowercase. i.e. `'Jack' == 'jack'` will return `false`.
Answer:  if( strtolower($row['username'])==strtolower($val['username']) ) { echo "Exists";}

You can also replace strtolower with strtoupper that will convert a string to upper case.
NOTE: That for this case strtolower and strtoupper will result in the same outcome as noted by @user3783243
Also if your trying to compare both values with Case Sensitivity that is you don't want if('JAck'== 'jAck') to return true then use strict comparison ===
 if('JAck'== 'jAck') //will return true
 if('JAck'=== 'jAck') //will retrun false

Comment: OP asks for reverse. He wants to check for upper and lowercase.
Comment: Okay, guess that was inverted, `strtoupper` should be removed though, it will have the same affect as `tolower`.
Comment: i understand the misconception but do consider this line in the question **If I have username = jack and another register's with username = JaCk It accepts**
Comment: @user3783243 yes that's true just letting the user know he can use that as well.
Comment: @BobbyAxe This answer your other question https://stackoverflow.com/questions/24873771/how-to-prevent-xss-attacks-in-php
Comment: @Havenard thanks i wanted to improve the original question but on second thought i deleted it thanks for the info i have been doing a lot of reading on it if you think its still useful you can vote for it to be opened though
Answer: You can using UPPER_CASE in SQL query:
$stmtr = $DB_con->prepare("SELECT username FROM users WHERE UPPER(username)=:username");
$stmtr->bindparam(":username", strtoupper($val['username']));

But I think you should store username as uppercase or lowercase when user register. It is better than using UPPER in SQL


Chrome browser - unwanted sorting of data in ajax response

Question: I am returning an array of banners using json_encode like this from the AJAX handler code (PHP)-
echo json_encode(array("bannersData" =>$bannersData));

The order of data is proper upto here, when it is echoed.
However, in the AJAX response handling code (javascript part), I get a different sorting of the returned object (sorted by banner ID it seems) in chrome Version 26.0.1410.64 m. I am not sure whether we have any option to enforce keeping the original sort order intact.
Here is the code -
$.ajax({
type: "POST",
async: false,
url: posturl,
data:postdata,
dataType: "json",
success: function(msg){

    if($.isEmptyObject(msg.bannersData))
    {
        bannerOptionsHtml = "No Banner available";
    }
    else
    {
        /*getting unexpectedly sorted result here*/

    }

});

Example data-
Input
When I do echo json_encode(), following is original ordering of data -
'fggd'             [12658]
banner 2           [12653]
Copy 2 of banner 1 [12655]
Copy 3 of banner 1 [12656]
Copy 4 of banner 1 [12657]
sdfds = 'xyz'      [12654]

Outputs
But, following is what I get in AJAX response -
In chrome (sorting happening based on banner ID, it seems - R.H.S. column below is banner ID)
Test                [12652]
banner 2            [12653]
sdfds = 'xyz'       [12654]
Copy 2 of banner 1  [12655]
Copy 3 of banner 1  [12656]
Copy 4 of banner 1  [12657]
'fggd'              [12658]

In firefox (as expected)
'fggd'             [12658]
banner 2           [12653]
Copy 2 of banner 1 [12655]
Copy 3 of banner 1 [12656]
Copy 4 of banner 1 [12657]
sdfds = 'xyz'      [12654]

Comment: @hjpotter92: can you Please provide link for duplicate Question. I need answer for this one.
Answer: Did you try to convert numeric key to String in your PHP array?
It seems that Chrome sorts int keys array (same issue with opera)
Answer: Can you provide the format of the JSON object you actually have? I'd bet £1 that the bannersData is actually being transferred across as an array and not as an object.
There is a standard that browsers follow to re-create objects from JSON strings, and this standard orders browsers to keep the key-value order intact. This, however, is not the case for arrays, and there is absolutely no standard on those. Browsers, therefore, have differed on this.
This isn't an obscure fact: How do you stop Chrome and Opera sorting JSON objects by Index ASC? is a duplicate of your question (and I will probably vote it as such).
Two solutions: transform your array from PHP into an object with key-value pairs (even if the keys are numeric!) to keep the order, or understand that the order will not be kept across all browsers. The first option requires no JS code change unless if you are using for integer-iterated loops (for (var i = 0; i < array.length; i++) {, at which point you will need to iterate on the object's keys rather than the object itself.


How to access parent class params literals?

Question: Using Scala 2.x macros, how to access literals passed to parent classes when defining subclasses, typically with ADTs such as the following:
sealed abstract class Base(val s: String, val i: Int)
object Base {
  case object A extends Base("a", 1)
  case object B extends Base("b", 2)
  case class C(override val s: String) extends Base(s, 3)
}

For object A, I would like to know parameter values are literals "a" and 1.
For object B, I would like to know parameter values are literals "b" and 2.
For case class C, I would like to know the second parameter is literal 3.
How can I accomplish this with Scala 2.x macros?
Comment: This appears to be an X:Y question?
Comment: OK. I removed the last sentence.
Answer: After much investigation and getting inspiration from  bwmcadams /
supreme-macro-adventure, it looks like one solution is based on the combination of Macro Paradise and the creation of an @ADT annotation.
Macro paradise provides a mechanism to expand annotations on ADTs to rewrite them, thanks to the macroTransform function.
import scala.annotation.{compileTimeOnly, StaticAnnotation}
import scala.language.experimental.macros
import scala.reflect.macros.whitebox.Context

@compileTimeOnly("enable macro paradise to expand macro annotations")
class ADT extends StaticAnnotation {
  def macroTransform(annottees: Any*): Any = macro ADT.impl
}

object ADT {
  def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
    import c.universe._

    val trees = annottees.map(_.tree)
    val rewritten = trees match {
      case (cd: ClassDef) :: q"object $name { ..$body }" :: Nil =>
        $body match {
          case q"case object $name extends $_($literal, $_)" :: tail =>
            [...]
        }
      case _ =>
        trees
    }

    c.Expr[Any](q"{..$rewritten}")
  }
}

With the help of quasiquotes, one can navigate $body and pattern match the different cases to access the arguments passed to the parent class (here $literal). Finally, one returns the rewritten ADT.


How to fill select options based on previous select?

Question: I'm trying to get the hang of Forms'N'Ajax in Drupal 8.
I have a classic problem with my selects in D8:
$form['manufactureYear'] = array(
  '#type' => 'select',
  '#title' => $this->t('Manufacture year'),
  '#options' => $manufactureYears,
  '#required' => TRUE,
  '#ajax' => [
    'callback' => array($this, 'populateDurationYears'),
    'wrapper' => 'edit-loanDurationYears'
  ],
);

Then I have a second select:
$form['loanDurationYears'] = array(
  '#type' => 'select',
  '#title' => $this->t('Loan duration years'),
);

The function populateDurationYears is the one that are killing me. I've tried with the ReplaceCommand in a AjaxResponse, but when I submit the form the loanDurationYears select is empty.
Can somebody help me here?
EDIT:
 - If I change ReplaceCommand with AppendCommand it's almost correct. The code now looks like this:
<select id="edit-loandurationyears" class="form-select" name="loanDurationYears" data-drupal-selector="edit-loandurationyears">

    <option value="3">3 years</option>
    <option value="4">4 years</option>
    <option value="5">5 years</option>
    <option value="6">6 years</option>
    <option value="7">7 years</option>
    <option value="8">8 years</option>
    <option value="9">9 years</option>

</select>

What's up with that
? It messes up the select :-(
EDIT 2:
The populateDurationYears method looks like this:
$response = new AjaxResponse();
if($maxDurationYears != 0) {
  for($i = $minLoanDurationYears-1; $i < $maxDurationYears; $i++) {
    $option .= "<option value='" . (string)($i+1) . "'>" . (string)($i+1) . " år</option>";
  }
  $response->addCommand(new AppendCommand('#edit-loandurationyears', $option));
}
return $response;

Answer: I've figured it out on how to do this...
I fill out the #options for the second <select> when I build the form.
I hide it with a #states.
When the user then selects something in the first <select> I use the populateDurationYears method to remove invalid options like this:
$options = [];
if($maxDurationYears != 0) {
  for($i = $minLoanDurationYears-1; $i < $maxDurationYears; $i++) {
    $options[] = ($i+1) . " years";
  }
  $form['loanDurationYears']['#options'] = $options;
}
return $form['loanDurationYears'];

This does the trick :-) No need for AjaxResponse or anything like the ReplaceCommand etc... Keep it simpel Stupid! ;-)
Comment: I think you shoud mark this (your own) answer as accepted ...
Comment: can you share the full code ?
Answer: You should use #prefix and #suffix to add a wrapper around $form['loanDurationYears']. Your code would look like this:
$form['manufactureYear'] = array(
  '#type' => 'select',
  '#title' => $this->t('Manufacture year'),
  '#options' => $manufactureYears,
  '#required' => TRUE,
  '#ajax' => [
    'callback' => array($this, 'populateDurationYears'),
    'wrapper' => 'loanduration-wrapper'
  ],
);

$form['loanDurationYears'] = array(
  '#type' => 'select',
  '#title' => $this->t('Loan duration years'),
  '#prefix' => '
',
  '#suffix' => '
',
);

Comment: it still gives the DIV tag inside the SELECT tag? The OPTION tags are there - but the DIV messes it up :-(
Comment: Do you get the same results if you use `ReplaceCommand` instead of `AppendCommand`?
Comment: The ReplaceCommand completly removes the `` tag with the `option` tags.
Comment: I figured it out by myself - thank you very much for your time and effort.
Answer: The drupal core ajax.js will check the number of elements inside the string you pass to the response. The div container will only be added if you pass more than 1 element. Adding one option at a time will prevent this behaviour. In your case the correct way to go by ajax would be:
$response = new AjaxResponse();
if($maxDurationYears != 0) {
  for($i = $minLoanDurationYears-1; $i < $maxDurationYears; $i++) {
    $option = "<option value='" . (string)($i+1) . "'>" . (string)($i+1) . " år</option>";
    $response->addCommand(new AppendCommand('#edit-loandurationyears', $option));
  }
}
return $response;


loss function values converge differently with each new model launch

Question: I build a neural network model with regression for predict an insurance prime. My loss function decrease until near the zero, but loss function values converge differently with each new model launch.
<code>...
Epoch: 30/30 Train Loss: 0.1665
Epoch: 30/30 Validation Loss: 1.2689

...
Epoch: 30/30 Train Loss: 4.6166
Epoch: 30/30 Validation Loss: 4.4621

...
Epoch: 30/30 Train Loss: 1.3190
Epoch: 30/30 Validation Loss: 1.3700
</code>
Is correct that each time the loss converge with different values? The predict value following the loss output
The code of model:
<code>#################
#TRAIN E TEST SET
#################
from sklearn.model_selection import train_test_split

train_data, test_data = train_test_split(df, test_size=0.25)

train_id=train_data["ID"]
test_id=test_data["ID"]

##################
#TESNORFLOW MODEL
##################

from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split

############################
#Split training in validation
############################

def split_valid_test_data(data, fraction=(1 - 0.8)):
    data_y=train_data.as_matrix(columns=[train_data.columns[8]])
    data_x = data.drop(["Prime"], axis=1)
    train_x, valid_x, train_y, valid_y = train_test_split(data_x, data_y, test_size=fraction)
    return train_x.values, train_y, valid_x, valid_y

train_x, train_y, valid_x, valid_y = split_valid_test_data(train_data)

print("train_x:{}".format(train_x.shape))
print("train_y:{}".format(train_y.shape))
print("train_y content:{}".format(train_y[:3]))

print("valid_x:{}".format(valid_x.shape))
print("valid_y:{}".format(valid_y.shape))

##########
#Parameters
##########
learning_rate = 0.001
training_epochs = 30
batch_size =100
display_step = 1
total_len = train_x.shape[0]
# Network Parameters
n_hidden_1 = 16# 1st layer number of features
n_hidden_2 = 14 # 2nd layer number of features
n_hidden_3 = 12
n_hidden_4 = 10
n_input = train_x.shape[1]
n_classes = 1

###############
#tf Graph input
###############

x = tf.placeholder("float", [None, train_x.shape[1]])
y = tf.placeholder("float", [None,n_classes])

#################
#Model
#################

def multilayer_perceptron(x, weights, biases):
    # Hidden layer with RELU activation
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
    layer_1 = tf.nn.relu(layer_1)
    # Hidden layer with RELU activation
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
    layer_2 = tf.nn.relu(layer_2)
    # Hidden layer with RELU activation
    layer_3 = tf.add(tf.matmul(layer_2, weights['h3']), biases['b3'])
    layer_3 = tf.nn.relu(layer_3)
    # Hidden layer with RELU activation
    layer_4 = tf.add(tf.matmul(layer_3, weights['h4']), biases['b4'])
    layer_4 = tf.nn.relu(layer_4)
    # Output layer with linear activation
    out_layer = tf.matmul(layer_4, weights['out']) + biases['out']
    return out_layer

##################################
#weight & bias
##################################
weights = {
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1], 0,0.1, dtype=tf.float32)),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2], 0,0.1,dtype=tf.float32)),
    'h3': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_3], 0,0.1,dtype=tf.float32)),
    'h4': tf.Variable(tf.random_normal([n_hidden_3, n_hidden_4], 0,0.1,dtype=tf.float32)),
    'out': tf.Variable(tf.random_normal([n_hidden_4, n_classes], 0, 0.1,dtype=tf.float32))
}
biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1], 0,0.1,dtype=tf.float32)),
    'b2': tf.Variable(tf.random_normal([n_hidden_2], 0,0.1,dtype=tf.float32)),
    'b3': tf.Variable(tf.random_normal([n_hidden_3], 0,0.1,dtype=tf.float32)),
    'b4': tf.Variable(tf.random_normal([n_hidden_4], 0,0.1,dtype=tf.float32)),
    'out': tf.Variable(tf.random_normal([n_classes], 0, 0.1,dtype=tf.float32))
}

pred= multilayer_perceptron(x, weights, biases)

##############################################
#  loss e optimizer
###############################################

###########
#MSE
###########

cost = tf.reduce_mean(tf.square(pred-y))

optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

########
#batch
########

def get_batch(data_x,data_y,batch_size):
    batch_n=len(data_x)//batch_size
    for i in range(batch_n):
        batch_x=data_x[i*batch_size:(i+1)*batch_size]
        batch_y=data_y[i*batch_size:(i+1)*batch_size]
        yield batch_x,batch_y

train_collect = 50
train_print=train_collect*2

x_collect = []
train_loss_collect = []
train_acc_collect = []
valid_loss_collect = []
valid_acc_collect = []

saver = tf.train.Saver()

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    iteration=0
    for e in range(training_epochs) :
        for batch_x,batch_y in get_batch(train_x,train_y,batch_size):
            iteration+=1
            feed = {x: train_x,
                    y: train_y
                   }
            sess.run(optimizer, feed_dict={x:train_x, y: train_y})
            train_loss, train_acc = sess.run([cost, accuracy], feed_dict=feed)  #_, optimizer
            prediction_train = sess.run([pred], feed_dict=feed)
            if iteration % train_collect == 0:
                x_collect.append(e)
                train_loss_collect.append(train_loss)
                train_acc_collect.append(train_acc)
                if iteration % train_print==0:
                     print("Epoch: {}/{}".format(e + 1, training_epochs),
                      "Train Loss: {:.4f}".format(train_loss),
                      "Train Acc: {:.4f}".format(train_acc))
                feed = {x: valid_x,
                        y: valid_y
                       }
                val_loss, val_acc = sess.run([cost, accuracy], feed_dict=feed)
                valid_loss_collect.append(val_loss)
                valid_acc_collect.append(val_acc)
                if iteration % train_print==0:
                    print("Epoch: {}/{}".format(e + 1,training_epochs),
                      "Validation Loss: {:.4f}".format(val_loss),
                      "Validation Acc: {:.4f}".format(val_acc))
    saver.save(sess, "./prova_habitas.ckpt")
</code>
Answer: Sometimes, neural networks are very sensitive to initialisation.
Try reducing the initialised values of your variables, for example
<code>'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1], 0,0.1, dtype=tf.float32) * 0.001)
</code>
Comment: I tried to change learning rate (0.5, 0.1, 0.01 and 0.001) --> the best is last one. With 3 hidden layer then 2 the loss function decrease more quick.  I chenged mean and sd in weight and bias but the best is what you see in the code. Other moltiplication a value to weight and bias there are other initialised? I don't understand if is correct or not that the loss function decrease with different value
Comment: The convergence will always be a little bit different. You will never get 2 exactly the same values for the loss function because the initialisation and some other bits are random.
As long as you always converge to a similar value (let's say your loss always converges to 0.01), then your training is finished (whether or not your model can perform better is a different question).
Comment: All right, I know that the model doesn't  return always the same loss value. But my model sometimes return loss values about 4 and other times return value about 0.16. There is a big different between 4 and 0.16, for this motive iask if is correct. Probabily I have to set better the hyperparameters. Do you know other methods to initialise the hyperparameters?


ScrollView com Imagens e LinearLayout

Question: Estou desenvolvendo uma aplicação mobile gostaria de deixar a aplicação com esse visual uma aplicação horizontal com uma uma imagem lado a lado e com um ScrollView

Eu ja tentei tirar o ScrollView, mas a tela do celular corta as duas ultimas imagens abaixo do  filme O Regresso e Matar

Mas quando adiciono o  ScrollView para poder rolar a barra de rolagem para baixo e visualizar os filmes abaixo do O Regresso e Matar ele apresenta o seguinte erro ,ScrollView can only one direct child
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.tulio.exercicio5.MainActivity">

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="match_parent">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"

    >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/aliados" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/chamado3" />

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"

    >
<ImageView
    android:id="@+id/imageView3"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:srcCompat="@drawable/regresso" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/john_wick" />

</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"

    >

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/residentevil6" />

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/xxxreativado" />
    </LinearLayout>
    </ScrollView>
    </LinearLayout>

Answer: O ScrollView só aceita uma View/Layout dentro dele.
Não posso ter a certeza mas parece-me que, nesse seu caso, ele deviria ser o primeiro elemento:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.tulio.exercicio5.MainActivity">
    android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <LinearLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    ...
    ...
        </LinearLayout>
    </LinearLayout>
</ScrollView>

Eventualmemte terá de ajustar os valores de android:layout_width e android:layout_height tanto do ScrollView como do primeiro LinearLayout.
Answer: No seu caso você tem 2 filhos para o scrollview, e o mesmo só aceita uma view filho para sua hierarquia..
Darei um exemplo de forma correta e forma incorrecta.
CORRETA
        <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context="com.example.tulio.exercicio5.MainActivity">
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <LinearLayout
            <LinearLayout>
            </LinearLayout>
            <LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

Observe que temos 3 lineares layout dentro do scrollview, mas apenas um é filho direto do mesmo.
INCORRETA
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.tulio.exercicio5.MainActivity">
    android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <LinearLayout
    </LinearLayout>
    <LinearLayout>
    </LinearLayout>
</ScrollView>

Se parar para analisar, verá que o scrollview dessa vez possui 2 LinearLayout filhos, com isto causará o erro :

a scroll view can only have one child

ScrollView com Imagens e LinearLayout


Making a calendar - months starting on a Sunday gets first week trimmed off

Question: I'm trying to make a calendar, but I'm facing a problem where months starting on a Sunday, gets their first week cut off. Everything else seems to work, like the number of days in a month, and the dates actually being correct:
        August 2021
MON TUE WED THU FRI SAT SUN
 02  03  04  05  06  07  08
 09  10  11  12  13  14  15
 16  17  18  19  20  21  22
 23  24  25  26  27  28  29
 30  31

This is my code so far:
DateTime displayMonth = new DateTime(2021, 8, 1); // This will be dynamic, ofc.
int firstDay = (int)displayMonth.DayOfWeek;
int cellCounter = 0;
int daysInMonth = (int)DateTime.DaysInMonth(displayMonth.Year, displayMonth.Month);
int weeksInMonth = (int)Math.Ceiling((double)(firstDay + daysInMonth) / 7);
for (var w = 0; w <= weeksInMonth; w++)
{

        @for (var d = 0; d < 7; d++)
        {
            cellCounter++;

                @{
                    int day = 1 + cellCounter - firstDay;
                    if (day > 0 && day <= daysInMonth)
                    {

@day

                    }
                }

        }

}

Comment: Note, `displayMonth.DayOfWeek` will be 0 when Sunday (because that's what the US uses for the start of its week), but you _really_ want it to be day number 7 (or more aptly offset 6).
Comment: Move the cellCounter++; below the "coll" div as you increment the day before you actually use it and as @gunr2171 mentioned Sunday starts at a 0 day of the week.
Answer: I already knew about the index values of DayOfWeek and Sunday being 0. What I didn't have, was a "conversion" to shift Monday to 0:
int firstDay = ((int)displayMonth.DayOfWeek + 6) % 7;

This is the working code:
DateTime displayMonth = new DateTime(2021, 9, 1);
int firstDay = ((int)displayMonth.DayOfWeek + 6) % 7;
int cellCounter = 0;
int daysInMonth = (int)DateTime.DaysInMonth(displayMonth.Year, displayMonth.Month);
int weeksInMonth = (int)Math.Ceiling((double)(firstDay + daysInMonth) / 7);
for (var w = 0; w <= weeksInMonth; w++)
{

        @for (var d = 0; d < 7; d++)
        {
            cellCounter++;

                @{
                    int day = cellCounter - firstDay;
                    if (day > 0 && day <= daysInMonth)
                    {

@day

                    }
                }

        }

}


Show url inside a tab

Question: This is probably a very easy question however I have been banging my head for the last week on how to solve this problem
I am using query tabs
In say tab 6 I have got a link to a php file. What I want to do is when tab 6 is opened display the url e.g. my folder/file.php?p=x&p=y&p=z .inside the tab.
I have tried the following:

also
<a href="folder/file.php?p=x&p=y&p=z"<scr="">

However noting will display
Is there anybody who can help?
thanks for looking
Comment: Is the file.php producing an image?
Comment: Thnks for your reply yes it is
Comment: Can you show the code in your file.php? Does it set the header?
Answer:
Should be

notice the typo in scr/src ^
Comment: sorry that didn't work. I think it might not be an image thinking about as when you put the ref on the image and then click on it, it will to you to a new tab in your browser. Sorry about that.
Comment: I suggest you look into https://jqueryui.com/resources/demos/tabs/ajax.html a bit more as these are beginner problems with the component itself..

Show url inside a tab


PHP Fatal error: Uncaught Error: Call to undefined function cache_get()

Question: I'm trying to get Drupal to work inside an Alpine Linux docker container but for some reason drush can't be used to manage the website. The website itselfactually works fine, drush just can't bootstrap.
Here's the pertinent information:
 /var/www/html # drush core-status
 Drupal version         :  7.56
 Site URI               :  http://default
 Database driver        :  mysql
 Database hostname      :  vlp-dcimysql01.dhe.duke.edu
 Database port          :
 Database username      :  tmp_drupal
 Database name          :  tmp_drupal
 PHP configuration      :  /etc/php7/php.ini
 PHP OS                 :  Linux
 Drush script           :  /usr/bin/drush
 Drush version          :  8.1.14
 Drush temp directory   :  /tmp
 Drush configuration    :
 Drush alias files      :
 Drupal root            :  /var/www/html
 Drupal Settings File   :  sites/default/settings.php
 Site path              :  sites/default

/var/www/html # drush wd-show
PHP Fatal error:  Uncaught Error: Call to undefined function cache_get() in /var/www/html/includes/module.inc:754
Stack trace:
#0 /var/www/html/includes/module.inc(954): module_implements('system_theme_in...')
#1 /var/www/html/modules/system/system.module(2511): module_invoke_all('system_theme_in...')
#2 /var/www/html/includes/theme.inc(798): _system_rebuild_theme_data()
#3 /var/www/html/includes/theme.maintenance.inc(57): list_themes()
#4 /var/www/html/includes/bootstrap.inc(2872): _drupal_maintenance_theme()
#5 /var/www/html/includes/errors.inc(179): drupal_maintenance_theme()
#6 /var/www/html/includes/bootstrap.inc(2609): _drupal_log_error(Array, true)
#7 [internal function]: _drupal_exception_handler(Object(Error))
#8 {main}
  thrown in /var/www/html/includes/module.inc on line 754
Drush command terminated abnormally due to an unrecoverable error.                                                                                [error]
Error: Uncaught Error: Call to undefined function cache_get() in /var/www/html/includes/module.inc:754
Stack trace:
#0 /var/www/html/includes/module.inc(954): module_implements('system_theme_in...')
#1 /var/www/html/modules/system/system.module(2511): module_invoke_all('system_theme_in...')
#2 /var/www/html/includes/theme.inc(798): _system_rebuild_theme_data()
#3 /var/www/html/includes/theme.maintenance.inc(57): list_themes()
#4 /var/www/html/includes/bootstrap.inc(2872): _drupal_maintenance_theme()
#5 /var/www/html/includes/errors.inc(179): drupal_maintenance_theme()
#6 /var/www/html/includes/bootstrap.inc(2609): _drupal_log_error(Array, true)
#7 [internal function]: _drupal_exception_handler(Object(Error))
#8 {main}
  thrown in /var/www/html/includes/module.inc, line 754

As stated before, the platform is Alpine Linux, PHP is 7.1.9
I can add more if anything is unclear. I've checked the PHP logs but they just produce the above output. I'm open to there just being a module/extension missing, but I'm not entirely sure how to figure that out if it doesn't ever tell me "undefined function extensionFunctionX" or something like that. The above undefined function appears to be a Drupal core function.
Note that the website works, so it's communicating with the database without issue, I can also do a SHOW TABLES in MySQL and see the Drupal installer's tables were created.
Comment: I'm now thinking the issue is with my understanding of `drush`. The "fix" seems to be installing the `mysql` client program. [See here](https://dpaste.de/SVZ9) where it immediately started working once I added those packages. Is there a less install-heavy way of fixing this problem? Why would installing the client program matter? Shouldn't `drush` use the PDO MySQL extensions?
Comment: @kenorb Tried `drush cc all` and it just cleared the `drush` cache out. We're planning on using `redis` eventually, but haven't progressed that far into the install yet.
Comment: Does `drush sql-connect` returns the right credentials? If so, can you run: `$(drush sql-connect)` in shell?
Comment: Installing db client matter, because drush is using the command when connecting to the database. However I'm not sure why it's needed for `wd-show` command. You can suggest some improvements at [GitHub](https://github.com/drush-ops/drush/issues).
Answer: In my case (moving production website to fresh server) memcached was enabled in settings.php, PHP daemon was installed and running on server, but no PHP extension was installed
Answer: The error happens because your Drupal is not correctly bootstrapped. Normally, you should see the following lines when running drush status:
Database                        :  Connected
Drupal bootstrap                :  Successful

So you should double check your database credentials in your settings file whether they're pointing to the right database and host (it's accessible from your command-line), your MySQL client command is installed and present (for example, with apk add mariadb-client) and your MySQL server is up-and-running.
You can check your database connection via drush sql-connect command. If that's correct, try to connect to the database by running $(drush sql-connect) in your shell.
In case of problems, try to clear Drush caches by using drush cc drush first.
If your website works fine, double check how you load your database credentials. Sometimes, people are using if or switch/case statements for different environments based on the host and forgot about the command-line interface which is not included.
Comment: Yeah like I was saying, the website works, so it's communicating with the database without issue, I can also do a `SHOW TABLES;` in MySQL and see the drupal installer's tables were created.
Comment: Website and command-line can work differently, they can use different PHP configuration. You need to make sure `drush` has access to the right database credentials. Please double check the answer whether you missed anything. Make sure `mysql` command is available in your shell. When you checked something, please re-edit your question to clarify what you've done so far. E.g. what's the output of: `$(drush sql-connect)` shell command.


How to add a ScrollBar to a StackPanel in Silverlight?

Question: I have a Grid, 3 by 3 (3 RowDefinitions and 3 ColumnDefinitions). I want some content (a StackPanel) in one of those grid cells to scroll. I'm fairly sure this is possible but I cannot figure out how. I've tried adding ScrollViewers and ScrollBar controls to the grid cell I want to scroll, but this usually ends up creating scrolling for the entire page.
Edit:
My issue is more specificlly how I can get scrolling over a StackPanel. An example if the issue I am having is here:
<Grid x:Name="LayoutRoot">
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <Grid>
        <TextBlock FontSize="16">1,1</TextBlock>
    </Grid>
    <Grid Grid.Column="1">
        <TextBlock FontSize="16">1,2</TextBlock>
    </Grid>
    <Grid Grid.Row="1">
        <TextBlock FontSize="16">2,1</TextBlock>
    </Grid>
    <Grid Grid.Column="1" Grid.Row="1">
        <StackPanel>
            <TextBlock>Title</TextBlock>
            <Grid>
                <ScrollViewer>
                    <StackPanel>
                        <TextBlock FontSize="32">2,2</TextBlock>
                        <TextBlock FontSize="32">2,2</TextBlock>
                        <TextBlock FontSize="32">2,2</TextBlock>
                        <TextBlock FontSize="32">2,2</TextBlock>
                        <TextBlock FontSize="32">2,2</TextBlock>
                        <TextBlock FontSize="32">2,2</TextBlock>
                        <TextBlock FontSize="32">2,2</TextBlock>
                        <TextBlock FontSize="32">2,2</TextBlock>
                        <TextBlock FontSize="32">2,2</TextBlock>
                        <TextBlock FontSize="32">2,2</TextBlock>
                        <TextBlock FontSize="32">2,2</TextBlock>
                        <TextBlock FontSize="32">2,2</TextBlock>
                        <TextBlock FontSize="32">2,2</TextBlock>
                        <TextBlock FontSize="32">2,2</TextBlock>
                    </StackPanel>
                </ScrollViewer>
            </Grid>
        </StackPanel>
    </Grid>
</Grid>

Comment: ScrollViewer is the tool for the Job, perhaps you can show use the Xaml that doesn't work using ScrollViewer.
Comment: Anthony's right. A ScrollViewer in the Grid Cell is what you're looking for.
Comment: Thanks for offering help. I've updated my post to be more specific.
Answer: StackPanel treats its content has having infinite space.  To scroll the stackpanel, you're going to have to put a height constraint on something - the grid parent of the stackpanel, most likely.
Comment: is there a way to set the height of the stackpanel so that it dynamically changes along with its parent grid?
Comment: If you set the height of the stackpanel, then the controls within it won't be displayed.  You need to let the stackpanel size to fit the controls within it, and then scroll the stackpanel within the display viewport.  Set the height of the grid that is the parent of the innermost stackpanel.  The scrollviewer will then scroll the larger stackpanel within the smaller grid viewport.


Approaching categorical predictors and responses

Question: I have a data set of 4 predictors and 1 response, all of which are categorical.
I'm looking to answer questions like: (for the sample below) Of the weapons in predictor, which one best correlates to not dying? Can someone point me in the right direction or towards a tutorial?
I'm working in Python so a library would be helpful but I most need the model and the explanation because it's been a good few years since my university stats classes.
Sample data set:
<code>     Setting  |  Gender  |  Weapon  |  Time_of_day  || Outcome
     Urban    |  Male    |  Knife   | Dawn          || Dead
     Urban    |  Male    |  Gun     | Dawn          || Dead
     Forest   |  Male    |  Gun     | Noon          || Alive
     Urban    |  Female  |  Knife   | Night         || Alive
     Highway  |  Male    |  Gun     | Dawn          || Alive
     Urban    |  Female  |  Knife   | Dawn          || Dead
     Forest   |  Female  |  Rope    | Noon          || Injured
</code>
Comment: Some measures of association between categorical (nominal) variables are given in [this answer](http://stats.stackexchange.com/a/112674/127790). A python implementation is described [here](http://stackoverflow.com/a/38032115/2426909).
Answer: The linked answer on the comment might lead you to use of chi-square test of independence, but I am not sure this is what you are looking for. I recommend using logistic regression. Here, multinomial logistic regression should be preferred because the response variable has multiple unordered categories. But you can also recode response variable into binary categories (say, 0 "Alive" 1 "Dead"; by recoding "Injured" also as "Alive", etc.) and use binomial logistic regression (or simply logistic regression). This might be easier to understand, because the independent variable(s) can be interpreted as increasing/decreasing the odds of dying.
Another issue is about the independent variables. They are also categorical (and nominal) variables, so you should recode them into dummy variables and then include in the analysis. The important point is having one category as your reference category (by excluding from the model). So, let's say you have three dummies derived from Weapon variable: Knife (1 "Knife", 0 "Other"), Gun (1 "Gun" 0 "Other"), and Rope (1 "Rope" 0 "Other"). Assuming that Gun is your reference category, interpretation will be something like this: "Using a gun compared to using a knife would increase the odds of dying by a factor of X".
Maybe this is not exactly what you want ("which one best correlates to not dying"), but I think it gets close considering that the variables are categorical. I am not a Python user, but here is a tutorial explaining how to do logistic regression in Python, as well as how to create dummy variables.


Mongo UUID python vs java format

Question: I have an application that sends requests to a restAPI, where a java process stores the data in mongo. When I try to read this data back using pymongo, reading the database directly, it gets the UUIDs differently (seems it is due to different encoding in java/python).
Is there a way to convert this UUID back and forth?
EDIT:
A few examples:

in java: 38f51c1d-360e-42c1-8f9a-3f0a9d08173d,
  1597d6ea-8e5f-473b-a034-f51de09447ec
in python:
  c1420e36-1d1c-f538-3d17-089d0a3f9a8f,
  3b475f8e-ead6-9715-ec47-94e01df534a0

thanks,
Comment: It would be helpful if you posted examples of both types (ideally the same value!).
Answer: I spent a day of my life trying to tackle this same issue...
The root problem is likely that your Java code is storing the UUIDs in the Mongo database with the Java drivers using the legacy UUID3 standard.  To verify, you just login with the Mongo shell and look at the raw output of your UUIDs.  If there's a 3, then that's the issue.
db.my_collection_name.find().limit(1)
...BinData(3,"blahblahblahblahblah"),...

With UUID3, Mongo decided to do everything different with all their drivers based on the given language. (thanks Mongo…)  It wasn't until UUID4 that Mongo decided to standardize across all their different drivers for various languages.  Ideally you should probably switch to UUID4, but that's a more impactful solution, so not necessarily practical.  REFERENCE: http://3t.io/blog/best-practices-uuid-mongodb/
Not to worry, there's hope!  The magic technique to make it all work involves simply pulling the collection with the JAVA_LEGACY uuid specification in the CodecOptions.
my_collection = db.get_collection('MyCollectionName', CodecOptions(uuid_representation=JAVA_LEGACY))

After that you can query with the UUIDs from your APIs and your query results will also have the UUIDs in the same format as your APIs.
Here is a complete query example using this technique.
import pprint
import uuid
from bson.binary import JAVA_LEGACY
from bson.codec_options import CodecOptions
from pymongo import MongoClient

PP = pprint.PrettyPrinter(indent=2)

client = MongoClient('localhost', 27017)
db = client.my_database

# REFERENCES: http://3t.io/blog/best-practices-uuid-mongodb/  |  http://api.mongodb.org/python/current/api/bson/binary.html
my_collection = db.get_collection('my_collection', CodecOptions(uuid_representation=JAVA_LEGACY))

my_java_uuid3 = "bee4ecb8-11e8-4267-8885-1bf7657fe6b7"
results = list(my_collection.find({"my_uuid": uuid.UUID(my_java_uuid3)}))

if results and len(results) > 0:
    for result in results:
        PP.pprint(result)

Comment: I remember having this issue a long time (a few years) ago, but I had forgotten what the solution was. Thank you for bringing it up again and posting an answer.
Comment: you're a lifesaver! thanks! it worked for me!

Mongo UUID python vs java format


Strange w3c validation on a certain points

Question: Can someone help me and explain me why this isn't validated?
My doctype:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

The errors:
 1. line 55 column 14 - error: document
    type does not allow element "div"
    here; missing one of "object",
    "applet", "map", "iframe", "button",
    "ins", "del" start-tag

 2. line 62 column 24 - Fout: document
    type does not allow element "div"
    here; missing one of "object",
    "applet", "map", "iframe", "button",
    "ins", "del" start-tag

  3. line 65 column 22 - Fout: document
     type does not allow element "div"
     here; missing one of "object",
     "applet", "map", "iframe", "button",
     "ins", "del" start-tag

  4.

     line 76 column 19 - Fout: document
     type does not allow element "div"
     here; missing one of "object",
     "applet", "map", "iframe", "button",
     "ins", "del" start-tag

  5. line 83 column 7 - Fout: end tag for
     "a" omitted, but OMITTAG NO was
     specified

This is the html, which can be viewed here also.
http://www.coldcharlie.nl/test/
Comment: not without seeing the actual html ...
Comment: If I had to guess, it's because you have `` s where they're not permitted by the doctype.
Comment: I placed the html in my post.
Answer: You shouldn't put a block level element such as
 inside an inline element such as  or
On line 55 you have an  element (hyperlink) that isn't closed. Then you have few
s. The validator thinks these divs are inside the .
Comment: Great, I needed to close the <a href, after closing it, every other error was gone too. Thanks!


How to improve the sound of a bass when playing with Guitar rig (software)?

Question: Sound is very moody and unclear.
When I choose a Bass Amp from the list, even with all high frequency knobs (mid, hi, and presence) turned up in their highest position, it still sounds like underwater.
I connect my guitar straightly to a "line in" input on my motherboard (I use standard motherboard audio, no additional sound card module), without DI boxes or whatever stuff.
Here's the plot of the spectrum:

When I hit only one (lowest) 'E' string (with all amps eqs and compressors turned off, just clean signal as it goes from 'line in'):

Should I get a DI box or some additional equipment?
Added: sound sample Sound sample
Comment: I'd guess the impedances are mismatched, in which case, yes, some kind of USB interface specifically designed for instruments would make a world of difference [assuming your bass sounds fine in a regular amp]
Comment: @Tetsujin, could you please look at spectrum plots I've added?
Comment: It doesn't really give me any new info, just confirms what you already said. I'd still think it's an impedance mismatch, but I'm no electronics expert, just a player.
Comment: @Tetsujin, I'm asking your diagnostic as a sound engineer :)
Comment: …but you're asking my opinion on something i've never tried - I've never just bunged a guitar straight in a lappy's headset socket. I imagine it's not going to sound very good.
Comment: @Tetsujin, wheredo you typically plug it in?
Comment: direct to a computer, something like these - http://www.sweetwater.com/c695--USB_Audio_Interfaces/low2high There are hundreds to choose from
Answer: You should never connect a guitar straight in to a Line-In connection.

Line-in is around 1 Volt (in reality, audio signals are rated in dB and line levels can be in a range but 1V is a useful reference point)
Guitars output somewhere around 150mV so you really need to put a pre-amp in the chain. A pre-amp will also sort out your impedance matching (which in your case is probably cutting a lot of your higher frequencies)

Simple answer - get a cheap pre-amp.


Proof that the even natural numbers are a subset of $\mathbb{N}$

Question: I'm doing some self-studying on discrete mathematics and so far its going well however I came upon a question that whilst I can make sense of and I know that set $A$ is a subset of set $B$, I cannot think of how to express it and the book does not feature an answer for this question.
The question is, prove that $A$ is a subset of $B$, where
$$A = \{ 2n \mid n\in\mathbb{Z}^+\},\quad B = \{ n \mid n\in\mathbb{Z}^+\}.$$
I am somewhat at a loss to the logic of this, both sets are identified as they both have elements that contain positive integers and $n$ is half of $2n$, but then isn't if we were to use numbers: $n = 1$, $2n = 2$ etc... therefore the two are not equal as they will never contain the same numbers, I believe I am thinking about this wrong but am somewhat at a loss with this simple question! Any help is appreciated, thanks!
Comment: The $n$ is a 'dummy' variable. Suppose $2n \in A$. Since $2n \in \mathbb{Z}^+$, clearly $2n \in B$. Hence $A \subset B$.
Answer: Recall that a set $S$ is a subset of a set $T$, which we express as $S\subseteq T$, when
$$x\in S\implies x\in T.$$
Thus, in your problem, to show that $A\subseteq B$, what you must show is that, for any $n\in\mathbb{Z}^+$, we have that $2n$ is among the elements of $B$. Note that, because $B$ has precisely the same elements as $\mathbb{Z}^+$, we simply have $B=\mathbb{Z}^+$.
But it's true that $2n\in\mathbb{Z}^+$; we know that $\mathbb{Z}^+$ is closed under multiplication by $2$! Thus we are done.
The user copper.hat explains it quite well in the comment above; in set-builder notation, the variables are dummy variables. That is, they do not continue to have a fixed meaning outside of the set-builder expression. Thus, for example,
$$\mathbb{Z}^+=\{n\mid n\in\mathbb{Z}^+\}=\{m\mid m\in\mathbb{Z}^+\}=\{\xi\mid \xi\in\mathbb{Z}^+\}=\{\star\mid \star\in\mathbb{Z}^+\}$$
and, when you want to prove that $A$ is a subset of $B$, the correct interpretation of what you must show is that
$$\text{for any }x\in A\text{, we have }x=y\text{ for some }y\in B$$
$$\text{for any }n\in\mathbb{Z}^+\text{, we have }2n=y\text{ for some }y\in B$$
$$\text{for any }n\in\mathbb{Z}^+\text{, we have }2n=m\text{ for some }m\in\mathbb{Z}^+$$
Answer: Ok if $A\subset B$ you have to show that $x\in A \implies x \in B$.
$$x\in A \implies \exists n \in \mathbb{Z}: x=2 n$$
Because of $2 \in \mathbb{Z}^+$ and $n \in \mathbb{Z}^+$ and $(\mathbb{Z}^+, \cdot)$ is a semi-group (you just need to say that when $a\in \mathbb{Z}^+$ and $b\in \mathbb{Z}^+$ than
$$a\cdot b\in \mathbb{Z}^+.$$
Because of this there exists a $m$ so that $ 2 n = m \in \mathbb{Z}$ and thats why $A\subset B$


Groovy remove part of string within < >

Question: Hi In Groovy I need to remove part of string
the string.
 <Results xsi:type="xsd:string"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Updated User

            id:nish.test11</Results>

should look like " Updated User id:nish.test11
how can i do that?
Answer: As the content looks like an XML,
def xml = """
 <Results xsi:type="xsd:string"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Updated User
    id:nish.test11</Results>
"""

it's better to use XmlSlurper than parsing/extracting strings by hand
def result = new XmlSlurper().parseText(xml)

println result.toString()

this gives the desired result (the content of the Result)
Comment: You are welcome. If it works for you, could you please accept the answer so it doesn't appear as unanswered questions. :)
Answer: If I run the code:
""" <Results xsi:type="xsd:string"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Updated User

        id:nish.test11</Results>""".replaceAll( /<\/?[^<>]>/, '' ).replaceAll( /[\n\s]+/, ' ' )

it gives me
Updated User id:nish.test11

as output

Groovy remove part of string within < >


Setting line colors in legend of MATLAB plot?

Question: I am using the plotgauss2d function of BNT to visualize how the response of a 2D Gaussian node changes when evidence is observed elsewhere in the net.
eng = jtree_inf_engine(bnet);
evidence = cell(1, 2)
eng = enter_evidence(eng, evidence);
marginals = marginal_nodes(eng, 1); p_1 = marginals.T
marginals = marginal_nodes(eng, 2); p_2 = marginals.T
marginals
clf; plotgauss2d(marginals.mu, marginals.Sigma);
hold all;
evidence{1} = 1;
marginals = marginal_nodes(enter_evidence(eng, evidence), 2);
p = plotgauss2d(marginals.mu, marginals.Sigma);
set(p, 'Color', 'green');
evidence{1} = 2;
marginals = marginal_nodes(enter_evidence(eng, evidence), 2);
p = plotgauss2d(marginals.mu, marginals.Sigma);
set(p, 'Color', 'red');
legend({'Unknown', 'Class 1', 'Class 2'});
hold off;

As you can see, the legend is not picking up the changed plot colors I had to manually set. (Sadly plotgauss2d does not cycle through colors automatically as you might wish.)
Is there a way to set the line colors used in the legend too?
Answer: The PLOTGAUSS2D function returns a vector of three handles corresponding to minor axis, major axis, and ellipse respectively. So here is an example how to store the handles and call LEGEND at the end:
figure, hold on
h = zeros(3,3);
h(:,1) = plotgauss2d(rand(2,1), [1 0.5; 0.5 2]);
h(:,2) = plotgauss2d(rand(2,1), [2 -0.5; -0.5 1]);
h(:,3) = plotgauss2d(rand(2,1), [1 0; 0 2]);
hold off
set(h(:,1), 'Color','r')
set(h(:,2), 'Color','g')
set(h(:,3), 'Color','c')
legend(h(1,:), {'1','2','3'})

Answer: Legend does pick up line colors, styles, markers, and so on.
You must have at least three plots in each plotgauss2d plot.  As such, your legend command is applying the legend to the first three plots, all from the first call to plotgauss2d.
Skipping some of your code, you can make your legend right by doing this:
p = plotgauss2d(marginals.mu, marginals.Sigma);
h = p(1);
hold all;
evidence{1} = 1;
marginals = marginal_nodes(enter_evidence(eng, evidence), 2);
p = plotgauss2d(marginals.mu, marginals.Sigma);
h(end+1) = p(1);
set(p, 'Color', 'green');
evidence{1} = 2;
marginals = marginal_nodes(enter_evidence(eng, evidence), 2);
p = plotgauss2d(marginals.mu, marginals.Sigma);
h(end+1) = p(1);
set(p, 'Color', 'red');
legend(h,{'Unknown', 'Class 1', 'Class 2'});

Now you are causing legend to apply the legend to one plot from each of your plotgauss2d calls.
Also, I would suggest adding a line at the end:
axis equal;

I think you will like what it does.
Comment: @tmpearce I tihkn it must be because he is successfully setting the colors of those lines with his `set` commands.


Redimensionando imagem ao inserir com php

Question: Estou querendo que ao inserir a imagem no site pelo type="file" ela redimensione para 400x300, já tentei de tudo mas não consigo.
Código de verificação da imagem para enviar pro BD e inserir no site:
require_once("conn.php");
$imgm=$_FILES['imageM'];
$imgf=$_FILES['imageF'];
$destinoM = 'img/mini/';
$tipoM = $imgm["type"];
$sizeM = $imgm["size"];
$tempM = $imgm["tmp_name"];
$extM = end(explode(".",$imgm["name"]));
$nomeM = "mini_" . "_" . rand(0,99999) . date("zdmYHisu") . "." . $extM;

    echo $destinoM . "
" . $tipoM . "
" . $sizeM . "
" . $tempM . "
" . $extM . "
" . $nomeM;

    if (preg_match("/^image\/(gif|jpeg|jpg|png)$/",$tipoM)){
        $caminhoM = $destinoM . $nomeM;
        move_uploaded_file($tempM,$caminhoM);
    }
    $query = "INSERT INTO `TABELAPT` (`nome`, `tipo`, `desc`, `menu`, `imageM`, `imageF`) VALUES ('".$nome."', '".$tipo."', '".$desc."', '".$menu."', '".$nomeM."', '".$nomeF."')";

    // Executa a query
    $inserir = mysql_query($query);

    if ($inserir) {
    echo "Post inserido com sucesso!";
    } else {
    echo "Não foi possível inserir o Post, tente novamente.";
    // Exibe dados sobre o erro:
    echo "Dados sobre o erro:" . mysql_error();
    }

Comment: Só uma dica, nada a ver com a questão: utilize o método `pathinfo()` para verificar a extensão.
Comment: O que você usa para manipulação de imagens, imagick?
Answer: Você precisa usar as funções ImageMagick ou GD do PHP para trabalhar com imagens.
Com GD, Voce pode fazer desta forma
function resize_image($file, $w, $h, $crop=FALSE) {
    list($width, $height) = getimagesize($file);
    $r = $width / $height;
    if ($crop) {
        if ($width > $height) {
            $width = ceil($width-($width*abs($r-$w/$h)));
        } else {
            $height = ceil($height-($height*abs($r-$w/$h)));
        }
        $newwidth = $w;
        $newheight = $h;
    } else {
        if ($w/$h > $r) {
            $newwidth = $h*$r;
            $newheight = $h;
        } else {
            $newheight = $w/$r;
            $newwidth = $w;
        }
    }
    $src = imagecreatefromjpeg($file);
    $dst = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    return $dst;
}

E  chamar esta função assim...
$img = resize_image('$destinoM/$nomeM.$tipoM', 400, 300);

Comment: Essa função não vem instalada por padrão no PHP. O ideal seria informá-lo.
Comment: Verdade respondi aqui do trampo vou melhorar a resposta
Answer: Existe uma extensão para trabalhar com imagens, se chama php-gd, sua instalação será necessária.
private function resizeImage(&$file) {
    $source = imagecreatefromstring($file);

    $width = imagesx($source);
    $height = imagesy($source);

    $newwidth = 400;
    $newheight = 300;

    $thumb = imagecreatetruecolor($newwidth, $newheight);

    imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    ob_start();
    imagejpeg($thumb);
    $imagedata = ob_get_contents();
    ob_end_clean();

    imagedestroy($source);
    imagedestroy($thumb);

    return $imagedata;
}

Envie como parâmetro da função o conteúdo do arquivo enviado
$file = file_get_contents($_FILES['imageM']['tmp_name']);

Depois é só escolher em que local do disco irá salvar o retorno


c++ std::log function doesn't scale

Question: I'm running a piece of code to test if the log() function in  scales. I ran it on a 4-core machine and the result shows that it does no scale. My code is as below:
#include<iostream>
#include<cmath>
#include<omp.h>
#include<chrono>
using namespace std;

typedef std::chrono::milliseconds ms;

int main(){
        #pragma omp parallel for schedule(static)
        for(int i=0;i<4;i++){
                auto start = std::chrono::high_resolution_clock::now();

                double tmp=1.0;
                for(double j=0.0;j<10000000;j++){
                        tmp=log(j);
                }

                auto end = std::chrono::high_resolution_clock::now();
                #pragma omp critical
                {
                        cout<<"tmp="<<tmp<<endl;
                        cout<<"Thread "<<omp_get_thread_num()<<" calculated tmp, time used: "<<std::chrono::duration_cast<ms>(end - start).count() << "ms" << endl;
                }
        }

        return 0;
}

If I use 4 threads, the result is:
Thread 1 calculated tmp, time used: 21ms
Thread 0 calculated tmp, time used: 21ms
Thread 2 calculated tmp, time used: 21ms
Thread 3 calculated tmp, time used: 21ms

If only using 1 thread, the result is:
Thread 0 calculated tmp, time used: 20ms
Thread 0 calculated tmp, time used: 16ms
Thread 0 calculated tmp, time used: 16ms
Thread 0 calculated tmp, time used: 15ms

So when running in parallel, each thread takes longer than running in sequential. Does anybody know why it doesn't scale? How does the std::log actually work (may there's something that the threads have to share)? Is there any way to have a log() function that scales? Thanks!
EDIT1:
I increased the number of iterations to 10e10 times, but the result shows that the parallel version is even slower, so maybe it's not the thread creation time which is dominant here.
4 threads:
Thread 0 calculated tmp, time used: 17890ms
Thread 2 calculated tmp, time used: 17890ms
Thread 1 calculated tmp, time used: 17892ms
Thread 3 calculated tmp, time used: 17892ms

1 thread:
Thread 0 calculated tmp, time used: 15664ms
Thread 0 calculated tmp, time used: 15659ms
Thread 0 calculated tmp, time used: 15660ms
Thread 0 calculated tmp, time used: 15647ms

EDIT2: I let the tmp variable to be printed out in the end, so that the log() can't be optimized out. But the result is still like before. Any other ideas?
EDIT3: So the speed-up of the total execution time is 3.5, and it does not become higher anymore even if the number of iterations increases. I'm not sure whether it's a reasonable speed-up, because I was expecting a speed-up like 3.7 or 3.8 on for a simple program like this.
Comment: Your log calculation probably gets optimized out. It doesn't do anything observable.
Comment: Thread creation is expensive.  You need to do more work in the thread vs it's creation time.
Comment: Is this a 4 core machine, or a 2 core machine with hyperthreading?  Does it have single-core boost, where when running single core it overclocks that core?  In short, what exactly is your hardware?   Finally, multithreaded code rarely scales perfectly.  The case where it does is the surprising one.
Comment: What is the total execution time in each case? Even if each thread takes longer, they can still work faster together.
Comment: What happens if you make loop to iterate over integers, not doubles?
Comment: @RichardCritten The speed-up doesn't change if I change the iterator to integers. The absolute time becomes longer both of the parallel version and the sequential version, which I guess is for the type conversion.
Comment: I don't think that thread creation can take so much time , because for this example program create 4 threads and difference between 10e10 and 10e7 can't be so big
Comment: Just because you print out the tmp variable does not mean it won't be optimized out afaik.
Comment: The compiler may realize that the loop can be removed, and replaced with just `tmp = log(9999999);`
Comment: I think Revolver_Ocelot is on the right track. You are measuring the wrong time. Total execution time is what matters, not what each thread takes to execute.
Comment: As you're on a 4 core machine 4 threads may not be optimal (as there are other things to take care of), you could try with 3 threads to see how the number of threads vs the number of core impact the results.
Comment: Interesting detail: If I use 4 threads on a machine with 4 real cores each thread is about 5 percent slower but if I use 8 threads (on 8 hyperthreading cores) each thread is only about 48% slower than a single running thread.
Answer: In short
You are completely right.  But to fully measure the multicore performance improvement, you shouldn't rely solely on timing individual threads : you should time as well measure the overall execution.
Multicore architectures seem to achieve higher throughput at the expense of a slight decrease of each core when several of them are active.  Here another benchmark (using std::thread instead of OMP) with similar observations.
So each individual log calculation doesn't scale, but the oveall system does very well.
Additional details
If you'd run some overall end-to-end measurements:
int main()
{
    auto common_start = std::chrono::high_resolution_clock::now();
    ...
    auto common_end = std::chrono::high_resolution_clock::now();
    cout << "Overall calculated time : " << std::chrono::duration_cast<ms>(common_end - common_start).count() << "ms" << endl;
    return 0;
}

you'd certainly observe a better overall performance in parallel.
Here my own timings with 4 threads:
Thread 2 calculated tmp, time used: 269ms
Thread 3 calculated tmp, time used: 274ms
Thread 0 calculated tmp, time used: 281ms
Thread 1 calculated tmp, time used: 289ms
Overall calculated time : 296ms

and with one:
Thread 0 calculated tmp, time used: 218ms
Thread 0 calculated tmp, time used: 218ms
Thread 0 calculated tmp, time used: 229ms
Thread 0 calculated tmp, time used: 224ms
Overall calculated time : 903ms

As you already observed, with one thread the calculations are performed 22% faster.  But overall, it takes 3 times the time needed in multithreading to do the same number of calculations.
So it's all about throughput: run at only 44 K-iterations/ms monothread compared to 135 K-iterations/ms with multithreading.

c++ std::log function doesn't scale


Using Timer inside Thread - IsAlive property is false

Question: As you might know Thread class has IsAlive property. It is false if the thread method returned or the thread was aborted. So I have an issue with this:
I have a windows service that starts several tasks in different threads. It permanently checks  IsAlive property of the threads and if it is false - recreates a task:
    foreach (var worker in _workers)
        _threads.Add(new Thread(worker.ProtectedRun));

    foreach (var thread in _threads)
        thread.Start();

    while (!EventWaitHandle.WaitOne(0))
    {
        for (var i = 0; i < _threads.Count; i++)
        {
            if (!_threads[i].IsAlive)
            {
                _threads[i] = new Thread(_workers[i].ProtectedRun);
                _threads[i].Start();
            }
        }

        EventWaitHandle.WaitOne(1000);
    }

But one of the tasks has Timer inside. In ProtectedRun method it starts timer and returns. After method is returned -> Thread's IsAlive property becomes false -> windows service starts thread again -> infinite loop :)
    public override void ProtectedRun()
    {
        _timer = new System.Timers.Timer(24 * 60 * 1000);
        _timer.Elapsed += OnTimedEvent;
        _timer.Enabled = true;
        _timer.Start();
    }

Do someone has an idea how to handle this situation? Maybe check thread status instead of IsAlive property?
Comment: If you don't check thread status/IsAlive, don't let threads terminate and don't recreate them, you cannot have any problems in those areas.  Write thread code as loops - you need never struggle with thread status and create/terminate ever again.  Try hard to concentrate on 'real app code', instead of thread micro-management, and your life will be so much easier :-)  Do you really need a timer?  Sleep(interval) is a pile easier and doesn't involve signals/execution from another pool thread running a timer.
Answer: Well, you ahve a broken design, simply like that.

You run tasks in threads and restart them when they are not alive anymore. GOod.
You have a specific tasks ending immediately but queueing more work on a timer. Good.

Bad news is, though, that the one task and the testing under the first premise are nsimply not compatible.
Choices:

Rework the restart logic or
Keep the thread alive after starting the timer.
Rework it so that there is a specific TASK LEVEL (not thread level) status field to follow.

At the end, your one ProtectedRun method is not following the specifications it should by returning while having active work queued (via the timer).
Comment: Thank you for the explanation and answer. I'll do some redesign work.
Answer: What's happening is that your thread (the one with the timer) actually exits successfully. That's why IsAlive is false.
The code executed in your timer handler is not executed in the thread you created.
See the following example:
internal class Program
{
    private static void Main(string[] args)
    {
        Thread z = new Thread(new ParameterizedThreadStart(Test)) { Name = "My new thread !" };
        z.Start();

        Console.ReadKey();
    }

    private static void Test(object obj)
    {
        Console.WriteLine(string.Format("My name is: {0}, my ID is: {1}", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId));

        var _timer = new System.Timers.Timer(1000);
        _timer.Elapsed += (sender, e) =>
            {
                Console.WriteLine(string.Format("[Timer] My name is: {0}, my ID is: {1}", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId));
            };
        _timer.Enabled = true;
        _timer.Start();
    }
}

Result:
My name is: My new thread !, my ID is: 10
[Timer] My name is: , my ID is: 12
[Timer] My name is: , my ID is: 12
[Timer] My name is: , my ID is: 12

You have to rethink the design of your application. Are you sure you need to start a thread that spawns a timer in the first place?


Derivative of a matrix

Question: I want to know if this derivative is correct. I have derived this but not sure if this is correct. I think it is but just to confirm
<code>F= A-(B/C)*D
where A,B,C and D are square matrices

dF/dx(partial derivative) = d(A-(B/C)*D)/dx

----- deriving the final result will be -----------

dA/dx - [(dB/dx - B*inv(C)*dC/dx)/C]*D - (B/C)*dD/dx
</code>
Comment: Do all 4 matrices have entries which are functions of $x$?  Does $B/C$ mean $BC^{-1}$?
Comment: Yes, the matrices are functions of x and it is B*inv(C)
Answer: In general, if A and B are square matrices that depend on $x$, the multiplication rule applies: $$\frac{d(A B)}{dx} = \frac{dA}{dx} B + A \frac{dB}{dx}  $$
Applying this, you result follows directly (but, please don't write $B/C$ instead of $B C^{-1}$)
$$\frac{d(A -B C^{-1} D)}{dx} = \frac{dA}{dx} - \frac{dB}{dx} C^{-1} D   - B \frac{d \, C^{-1}}{dx}D -   B C^{-1} \frac{dD}{dx}  $$
What remains is to compute $\frac{d \, C^{-1}}{dx}$. But deriving $C C^{-1} = I $ we get
$$ \frac{dC}{dx} C^{-1} + C\frac{d \, C^{-1}}{dx } = 0
\; \Rightarrow \; \frac{d \, C^{-1}}{dx} = - C^{-1}\frac{dC}{dx}C^{^-1}
$$
which (check) is the generalization of $\frac{d (y^{-1})}{dx} = - \frac{1}{x^2}\frac{dy}
{dx}$
Finally
$$\frac{d(A -B C^{-1} D)}{dx} = \frac{dA}{dx} - \frac{dB}{dx} C^{-1} D   + B C^{-1}\frac{d \, C}{dx} C^{-1}D -   B C^{-1} \frac{dD}{dx}  $$
which, fixing notation, coincides with your result.
Comment: The above derivation is similar to mine except the signs. I think you forgot the - sign. Other than that it is similar to mine right?
Comment: @rajansthapit: your result is (except for the bad notation) correct; perhaps your derivation is also correct
Comment: But the signs are different aren't they?
Comment: @rajansthapit: no they aren't (now) :-) (i have missed the initial minus)

Derivative of a matrix


How to find which block is causing PHP errors?

Question: I'm cleaning up a Drupal 7 site and currently its spitting out a bunch of PHP syntax errors pointing to some block that is causing it. It doesn't say which block but only says php_eval() error. So I'm assuming its in a block somewhere.
How would one find the culprit of which block it is that is causing this php error?
https://i.stack.imgur.com/WSJXM.png
Comment: please give some detail about this error.
Comment: according to error in image it seems that you have placed an unexpected php close tag "?>" into some block or page. you may review them.
Comment: This issue could happen when someone used "<?" instead of "<?php" somewhere, but your server's php.ini disables short open tags. Check this line in your php.ini - "short_open_tag = On".
Comment: Related: [How to find all inputs that make use of the php input filter to fully disable the php filter module?](http://drupal.stackexchange.com/q/110552/16495), [What are the downsides of using 'custom' PHP code in blocks, nodes, views-args, etc?](http://drupal.stackexchange.com/q/2509/16495). I'm sure this exact question was already asked, too, but can't find older one.
Answer: The most probable cause of the error is a missing or a mismatched parenthesis in the PHP code you are using in your custom blocks or functions. To solve the missing parenthesis error in PHP blocks or in other areas, the code has to be checked from the beginning to search for it. But it's really hard to check all PHP code. One way to avoid errors is to use proper indentation in the code. Once all the parentheses in the code have been set correctly, parse error: syntax error, unexpected $end will be fixed.
What causes this error?
In fact, this error means that PHP has finished analyzing your code, but you forgot to close a symbol somewhere in your page or in those that were included. There could be a number of causes:

This issue could happen when someone used <? instead of <?php somewhere, but your server's php.ini disables short open tags. Check this line in your php.ini - "short_open_tag = On".
You forgot to close a quote, so PHP is continuing to analyze your code until it finds the closing quotation mark.
You forgot to close a bracket, so from the last opening, PHP considers all the code that follows as part of a block that never ends.
You forgot to close a parenthesis, so from the last open parenthesis, PHP considers all the code that follows as part of a specific block (condition, arguments of functions etc) that does not end.
You forgot a comma, so for PHP there is an instruction in your code that has no end.

NOTE: This means that the problem may not be on the line mentioned in the error message, as the missing symbol could be anywhere after that point.
Answer: It appears to be an error in the php code in some of the blocks. There in no direct way to find out which block is causing the error. But you can find the ids of the blocks inside that page which contains php code by adding the following patch inside modules/block/block.module, just for debugging.
diff --git a/modules/block/block.module b/modules/block/block.module
index 48c80d7..d25a497 100644
--- a/modules/block/block.module
+++ b/modules/block/block.module
@@ -243,6 +243,9 @@ function block_block_view($delta = '') {
   $block = db_query('SELECT body, format FROM {block_custom} WHERE bid = :bid',
   $data['subject'] = NULL;
   $data['content'] = check_markup($block->body, $block->format, '', TRUE);
+  if (strpos($block->format, 'php')) {
+    echo "
"; print_r($delta, TRUE); echo "
";
+  }
   return $data;
 }

EDIT: Please note, the above code is just for debugging purpose and not meant to be commited. As Molot said on comment, Never Hack Core
Comment: [Never hack core](https://www.drupal.org/best-practices/do-not-hack-core)
Comment: @Mołot It is just for debugging purpose.
Comment: There always are excuses for bad practices. This doesn't make them any better. Neither does it help future maintainers...
Comment: @Mołot, you got me wrong. The above code was not meant to be committed. It is just for debugging current situation. I have edited my answer. Can you please check?


Fenchel conjugate of $\| \cdot \|_1$ and dual of logistic regression

Question: I am trying to replicate some results from
Koh, K., Kim, S. J., & Boyd, S. (2007). An interior-point method for large-scale l1-regularized logistic regression. Journal of Machine learning research, 8(Jul), 1519-1555.
The rewrite the problem of optimizing logistic regression with $\ell 1$ penalty as convex optimization problem with equality constraints.
What I am interested in is the dual function of this problem:
\begin{align}
\inf_{v,w,z} L(v,w,z,\theta) &= (1/m)\sum_{i=1}^{m}f(z_i-m\theta_iz_i)+\inf_{w}(\lambda ||w||_1+ \theta^{T} Aw) + \inf_{v} \theta^{T} b v \nonumber \\
& = \begin{cases}
-(1/m)\sum_{i=1}^{m}f^{*}(-m\theta_i) &\text{ } ||A^{T}\theta||_{\infty} \leq \lambda \text{,       } b^{T}\theta = 0\\
-\infty &\text{ otherwise },
\end{cases}
\end{align}
where w is the weight vector, v is the intercept vector, $\theta$ are the lagrange multipliers, A the data matrix and b is the vector of binary outcomes.
Now, $f^{*}$ is the conjugate of the logistic loss function:
\begin{equation}
f^{*}(y)= \sup_u(y^{T}u-f_(u))=
\begin{cases}
(-y)\log(-y)+(1+y)\log(1+y) &\text{ if } -1<y<0\\
0 &\text{ if } y = -1 \text{ or } y = 0\\
\infty &\text{ ontherwise }.
\end{cases}
\end{equation}
Specifically the last two terms of the dual are of interest:
\begin{equation}
\inf_{w}(\lambda ||w||_1+ \theta^{T} Aw)
\end{equation}
I understand that this term can be restated as the conjugate of the $||.||_1$ norm (Fenchel conjugate of a norm), which is the $||.||_{\infty}$ norm, but why is this a
constraint on the dual function?
Also I dont understand how the last term of the dual $\inf_{v} \theta^{T} b v$ results in the solution
$b^{T}\theta = 0$
Comment: I'm not sure what the question is. Can you try to restate again?
Comment: Hi there, I think I got most of the answers by myself. The only thing that I don't understand is the very last part with the infimum over a linear function resulting in kind of the derivative.
Comment: Please include a link to the paper.
Comment: [Direct Link](https://web.stanford.edu/~boyd/papers/pdf/l1_logistic_reg.pdf)
Answer: Regarding your question on $\inf_v \theta^Tbv$: Consider the case where $\theta^Tb= \alpha\neq 0$. Then since $v$ is defined over all $\mathbb{R}$ we can set it arbitrary large, and just keep the sign opposite to $\alpha$. This will mean the Lagrangian is unbounded. For that reason we define the feasible set of $L$ to be $\theta^Tb=b^T\theta=0$, and the the actual value of $\inf_v \theta^Tbv = 0$.


OWASP security and drupal

Question: Recently, To improve our site security we implemented and enabled OWASP security in our server using mod_security.
After we enabled the OWASP security, whenever I try to create or edit contents in my site after logging in as admin, It is showing it as SQL INJECTION and it does not allow to crate or edit new node.
When I edit content it redirects me to "/node/1078/edit"  and then it shows error
"Error
The website encountered an unexpected error. Please try again later."

I also tried to find solution here but so far nothing helps. We have used drupal database API for all our database operations. I am not sure why OWASP is showing "SQL INJECTION".
How can I fix this?
Answer: It would appear that OWASP think your site is vulnerable to SQL injection. You'll need to find the exact reason yourself (it requires debugging with the site in front of you), but here are some pointers:

Make sure your core version is >=7.32. There was a massive SQL hole in Drupal before that update.
Once that's confirmed, start disabling custom and contrib modules one-by-one, checking the edit form each time. If one of those is to blame, that simple process will find it. If you find a contrib module that's to blame, be sure to open a bug report on Drupal.org.
If your Drupal core version is up to date, and none of the custom/contrib modules are responsible, you'll need to contact OWASP to ask them for their criteria, and how they've arrived at the conclusion that your site is vulnerable.
On the off-chance they respond by pointing out another SQL injection vulnerability in Drupal core (very, very unlikely), be sure to report it to the security team. Same thing for contrib modules (more likely).
Comment: Thanks @clive.. for helpful answer. I have drupal up to date and we also applied the security patch, let me try disabling  custom and contrib modules one-by-one, checking the edit form each time. It will definitely help to narrow down the problem . Thanks
Answer: I don't think it has something to do with Drupal. I faced similar issue before in WordPress and it was because of mod_security rules. Mod_security was blocking passing parameters in URLs. The top answer in this post might help you.
Comment: so how did you resolve the problem
Comment: I was using a hosting service so they changed mod_security rules to fix the problem.

OWASP security and drupal


Unable to find and replace text with win32com .client using python

Question: I am trying to find a text in a word document and replace it some other text. I continue to get the error.
AttributeError: Property '<unknown>.Text' can not be set.

i tried giving the text directly and also by assigning to a variable and it still does not work below is my code.
import win32com.client
import win32api
from datetime import date

path_docx=r'C:\Docs\Approved 10_18_17_TAY.docx'
word = win32com.client.Dispatch('Word.Application')
word.Visible = True
doc=word.Documents.Open(path_docx)
word.Selection.Find.Text = "< DATE >"
word.Selection.Find.Replacement.Text=date.today()
word.Selection.Find.Execute(Replace=word.WdReplace.wdReplaceAll)

It always fails at finding the text
i tried the variables like file_date="< DATE >" and rundate=date.today()
still it did not work.
Any suggestions how to avoid this.
Regards,
Ren.
Comment: try wrapping the date.today() in str(). It seems like you are trying to paste a datetime-object (or a date-object, rather) and Office doesn't know how to interpret it as text.
Comment: @tst Thanks i tried that. I get an Attribute error at `word.Selection.Find.Execute(Replace=word.WdReplace.wdReplaceAll)`
Comment: I made this change to the code:`FromTo={"":today} for From in FromTo.keys():
    word.Selection.Find.Text = From
    word.Selection.Find.Replacement.Text = FromTo[From]
    word.Selection.Find.Execute(Replace=1, Forward=True)`. I am able to find the text, however it does not get replaced. I changed `Replace=2` as well still does not work. I am using Word 2016.
Answer: I tried it myself, but there were a lot of small issues.
1) For the wdReplaceAll-constant you need to load the constants separately in Python. This is not well documented and took me a while to find out myself, the firs time I needed it too. You can tell if something is a constant by "wd" for Word or "xl" for Excel prefixes.
2) I used gencache.EnsureDispatch. You can try it with just Dispatch though. I am not sure it will work both ways, but it might.
3) I was not able to get any result when I tried to run it with the 3 .Find commands on separate lines. Once I consolidated it all into the Execute() parameters, it worked.
4) If I pass it the date-object directly, it replaces all occurances of "< DATE >" with "00:00:00". However if I convert the date-object to string within python and pass that, it works. You might want to change the formatting of the date, though. Checkout the documentation of datetime. Its quite simple.
5) You passed the code a "Selection" object. I am not sure if the default selection is just "everything" when nothing is yet selected. I changed it to iterate over the Paragraphs-object instead. This should yield more consistent results
Heres my code:
import win32com.client as win32
from datetime import date

today = str(date.today())

path_docx='C:\\Scripts\\test.docx' #< obviously you need to change that
word = win32.gencache.EnsureDispatch('Word.Application')
const = win32.constants
word.Visible = True
doc=word.Documents.Open(path_docx)
for paragraph in doc.Paragraphs:
    print(paragraph)
    paragraph.Range.Find.Execute(FindText="< DATE >", ReplaceWith=today, Replace=const.wdReplaceAll)

I hope this works for you. If you have any questions about this solution, let me know.


android layout image fill width

Question: I need to strech a background image for a layout in my app,
please note, the yellow background in my layout is not streched

how to accomplish this [yellow bar image filling parent]?
also please note that the blue layout is not the whole width of the screen, how to?
code:
<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:paddingLeft="8dp"
         android:paddingRight="8dp"
         android:id="@+id/myfragment">

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="50dp"
         android:layout_weight="1"
          android:background="#0000FF">

         <ImageView
             android:id="@+id/imageView1"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:src="@drawable/titlebar" />

     </LinearLayout>

     <ListView android:id="@id/android:list"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:layout_weight="1"
               android:drawSelectorOnTop="false"/>

 </LinearLayout>

Thanks a lot!
Answer: This is the reason why your linearlayout (blue) is not fitting the whole screenwidth
android:paddingLeft="8dp"
android:paddingRight="8dp"

You need to change this. Put a margin (left and right) for the inner layout instead.
Instead of the image view you could set the background of the 2nd linearlyout directly
android:background="@+id/imageView1"

Incase you want to use the image view instead
     <ImageView
         android:id="@+id/imageView1"
         android:layout_width="fill_parent"
         android:layout_height="50dp"
         android:scaleType="fitXY"
         android:src="@drawable/titlebar" />

android layout image fill width


Ruby on rails não grava no banco

Question: Boa noite, galera!
Criei um pequeno form com dois campos e futuramente pretendo expandir esse form com mais um ou dois campos. Acontece que a inserção dos dados no sqlite não esta sendo feita, porém não dá nenhum erro.
A aplicação esta sendo desenvolvida para ser um to-do list.
Vocês podem, por gentileza me dizer possíveis motivos?
bem, eu tenho como model:
class ToDoList < ActiveRecord::Base
   attr_accessible :is_favorite, :name, :description
  has_many :tasks,  dependent: :destroy
  belongs_to :member
end
e como controller:
  class ToDoListsController < ApplicationController
    ...
     def new
        @todo_list = ToDoList.new

        respond_to do |format|
          format.html # new.html.erb
          format.json { render json: @todo_list }
        end
      end
    ...
     def create
    @todo_list = ToDoList.new(params[:todo_list])

    respond_to do |format|
      if @todo_list.save
        format.html { redirect_to @todo_list, notice: 'Todo list was successfully created.' }
        format.json { render json: @todo_list, status: :created, location: @todo_list }
      else
        format.html { render action: "new" }
        format.json { render json: @todo_list.errors, status: :unprocessable_entity }
      end
    end
  end
        end

Agradeço desde já!

Edit

Notei que eu estava chamando o params errado, chamando-o de @todo_list e era @to_do_list. Serve como exemplo a falta de atenção!
Veleu pela atenção, galera.
Comment: Qual o conteúdo do método create da sua classe ToDoListsControler? É nesse método onde o objeto é salvo no banco de dados.
Comment: esqueci de por o create, vou editar rs. o create é:

`def create
    @todo_list = ToDoList.new(params[:todo_list])

    respond_to do |format|
      if @todo_list.save
        format.html { redirect_to @todo_list, notice: 'Todo list was successfully created.' }
        format.json { render json: @todo_list, status: :created, location: @todo_list }
      else
        format.html { render action: "new" }
        format.json { render json: @todo_list.errors, status: :unprocessable_entity }
      end
    end
  end`
Answer: Como falado pelo Bernado Botelho normalmente quando se aprende Rails um controller do Rails tem as seguintes ações:
- Ação     - Descrição

#index   - Mostra todos os registros
#show    - Mostra apenas um registro
#new     - Mostra formulário para novo item
#create  - Cria o novo item
#edit    - Mostra formulário para editar item
#update  - Atualiza novo item
#delete  - Mostra alerta sobre deletar um item
#destroy - Deleta o item

Neste caso vimos a sua new que provavelmente foi implementada para criar um novo TodoList, contudo ele não persiste este TodoList.
Para fazer a persistência do TodoList no action Create (provavelmente, você pode ter outros) você vai dar no @todolist (tendo em mente que este é um objeto do Model TodoList e que está com os dados do formulário) o seguinte comando:
@todolist.save

Isso deve salvar a todolist no banco que você está utilizando.
Uma forma de testar isso fácil é:
No terminal digite:
rails console

Isso irá abrir o console do Rails, digite:
todo_list = ToDoList.new
todo_list.save

Feito isso você deverá ver uma query SQL sendo exibida na página (caso esteja usando um banco SQL), isso significa que ele tentou salvar (e se não tiver um ROLLBACK significa que ele salvou);

Ruby on rails não grava no banco


How can I clear the styles on a .draggable element's children, before moving them to the target?

Question: I have a div in which I do some manipulation of list-items using the excellent fcbkcomplete jquery plugin. However, when i plug this in with my draggable and sortable page, I realized that the extra decorations are frozen when I move it over. This makes the end result feel unpolished, to say the least. I know I can intercept the mousedown event before the drag, but I don't know what elements to modify to keep the style simple.
Here is a demo of fcbkcomplete, it resembles facebook's message compose feature. In my jsfiddle, once I am ready to drag the div to the sortable list below, I want all the fancy decoration (close image, separation into blue boxes, the "Start to type..." tooltip, and the final empty textbox to be replaced by a simple string, such that when I click on the h1 header, I still can retrieve the values. Does anyone know how to do this?
<code>                            $("#draggable").draggable({
                            connectToSortable: "#sortable",
                            helper: "clone",
                            revert: "invalid",
                            distance: 20
                        });

                        $('#draggable').each(function () {
                            $(this).mousedown(function () {

                                // Need to clear styles here
                                //$(this).parent().children('.maininput').hide('blind', 500);
                            });
                        });
</code>
Thanks for looking. JSFiddle is here. And here is a screenshot of the problem.
Comment: You could use a custom helper : http://jqueryui.com/draggable/#visual-feedback
Comment: Tried it, but didn't work. Attaching a simpler, trimmed-down JSFiddle: http://jsfiddle.net/parikramatic/7KEGs/
Comment: I ended up modifying the helper of the draggable from clone to custom instead
Answer: Asked a similar question, and the answer solves this problem as well. Had to use the custom helper instead of clone, but everything worked out fine. As for the droppable,
<code>                $("#container").droppable({
                accept: '.product',
                drop: function (event, ui) {
                    var x = ui.helper.clone();
                    x.removeClass().attr('style', '');
                    x.addClass("ui-state-default");
                    x.appendTo('#container');
                    ui.helper.remove();
                }
            });
</code>
the problem is, I wasn't using one!