TabLayout do app não aparece quando opera em Mobile

Question: Estou criando um app que possui uma TabLayout com três Tabs que devem retornar Fragmentos específicos cada. No entanto, quando testo o app no mobile nenhuma das tabs é apresentada, o que impossibilita fazer qualquer coisa. Segue o layout da activity_main:
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="8dp"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_weight="1"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:title="@string/app_name">

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/title_timeline" />

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/title_graph" />

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/title_ll_settings" />
    </android.support.design.widget.TabLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="24dip"
    tools:context=".MainActivity"
    app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">

    <com.google.android.material.tabs.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top" />

</android.support.v4.view.ViewPager>

Segue a Main Activity:
package com.example.mastiffcollar;

import java.util.Timer;
import java.util.TimerTask;

import com.example.mastiffcollar.contents.ActivityReport;
import com.example.mastiffcollar.contents.ContentObject;
import com.example.mastiffcollar.fragments.GraphFragment;
import com.example.mastiffcollar.fragments.IFragmentListener;
import com.example.mastiffcollar.fragments.LLFragmentAdapter;
import com.example.mastiffcollar.fragments.TimelineFragment;
import com.example.mastiffcollar.service.RetroBandService;
import com.example.mastiffcollar.utils.AppSettings;
import com.example.mastiffcollar.utils.Constants;
import com.example.mastiffcollar.utils.Logs;
import com.example.mastiffcollar.utils.RecycleUtils;
import com.example.mastiffcollar.utils.Utils;

import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.bluetooth.BluetoothAdapter;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity implements ActionBar.TabListener, IFragmentListener {

// Debugging
private static final String TAG = "RetroWatchActivity";

// Context, System
private Context mContext;
private RetroBandService mService;
private Utils mUtils;
private ActivityHandler mActivityHandler;

// Global

// UI stuff
private FragmentManager mFragmentManager;
private LLFragmentAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private TabLayout mTabLayout;

private ImageView mImageBT = null;
private TextView mTextStatus = null;

// Refresh timer
private Timer mRefreshTimer = null;

/*****************************************************
 *   Overrided methods
 ******************************************************/

@Override
public void onCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //----- System, Context
    mContext = this;//.getApplicationContext();
    mActivityHandler = new ActivityHandler();
    AppSettings.initializeAppSettings(mContext);

    setContentView(R.layout.activity_main);

    // Load static utilities
    mUtils = new Utils((MainActivity) mContext);

    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the primary sections of the app.
    mFragmentManager = getSupportFragmentManager();
    mSectionsPagerAdapter = new LLFragmentAdapter(mFragmentManager, mContext, this, mActivityHandler);

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.view_pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mTabLayout = (TabLayout) findViewById(R.id.tabs);
    mTabLayout.setupWithViewPager(mViewPager);

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by the adapter.
        actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }

    // Setup views
    //mImageBT = (ImageView) findViewById(R.id.status_title);
    mImageBT.setImageDrawable(getResources().getDrawable(android.R.drawable.presence_invisible));
    //mTextStatus = (TextView) findViewById(R.id.status_text);
    mTextStatus.setText(getResources().getString(R.string.bt_state_init));

    // Do data initialization after service started and binded
    doStartService();
}

@Override
public synchronized void onStart() {
    super.onStart();
}

@Override
public synchronized void onPause() {
    super.onPause();
}

@Override
public void onStop() {
    // Stop the timer
    if(mRefreshTimer != null) {
        mRefreshTimer.cancel();
        mRefreshTimer = null;
    }
    super.onStop();
}

@Override
public void onDestroy() {
    super.onDestroy();
    finalizeActivity();
}

@Override
public void onLowMemory (){
    super.onLowMemory();
    // onDestroy is not always called when applications are finished by Android system.
    finalizeActivity();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_scan:
            // Launch the DeviceListActivity to see devices and do scan
            doScan();
            return true;
        case R.id.action_discoverable:
            // Ensure this device is discoverable by others
            ensureDiscoverable();
            return true;
    }
    return false;
}

@Override
public void onBackPressed() {
    super.onBackPressed();      // TODO: Disable this line to run below code
}

@Override
public void onConfigurationChanged(Configuration newConfig){
    // This prevents reload after configuration changes
    super.onConfigurationChanged(newConfig);
}

/**
 * Implements TabListener
 */
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void OnFragmentCallback(int msgType, int arg0, int arg1, String arg2, String arg3, Object arg4) {
    switch(msgType) {
        case IFragmentListener.CALLBACK_RUN_IN_BACKGROUND:
            if(mService != null)
                mService.startServiceMonitoring();
            break;

        default:
            break;
    }
}

/*****************************************************
 *  Private methods
 ******************************************************/

/**
 * Service connection
 */
private ServiceConnection mServiceConn = new ServiceConnection() {

    public void onServiceConnected(ComponentName className, IBinder binder) {
        Log.d(TAG, "Activity - Service connected");

        mService = ((RetroBandService.LLServiceBinder) binder).getService();

        // Activity couldn't work with mService until connections are made
        // So initialize parameters and settings here, not while running onCreate()
        initialize();
    }

    public void onServiceDisconnected(ComponentName className) {
        mService = null;
    }
};

/**
 * Start service if it's not running
 */
private void doStartService() {
    Log.d(TAG, "# Activity - doStartService()");
    startService(new Intent(this, RetroBandService.class));
    bindService(new Intent(this, RetroBandService.class), mServiceConn, Context.BIND_AUTO_CREATE);
}

/**
 * Stop the service
 */
private void doStopService() {
    Log.d(TAG, "# Activity - doStopService()");
    mService.finalizeService();
    stopService(new Intent(this, RetroBandService.class));
}

/**
 * Initialization / Finalization
 */
private void initialize() {
    Logs.d(TAG, "# Activity - initialize()");
    mService.setupService(mActivityHandler);

    // If BT is not on, request that it be enabled.
    // RetroWatchService.setupBT() will then be called during onActivityResult
    if(!mService.isBluetoothEnabled()) {
        Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableIntent, Constants.REQUEST_ENABLE_BT);
    }

    // Load activity reports and display
    if(mRefreshTimer != null) {
        mRefreshTimer.cancel();
    }

    // Use below timer if you want scheduled job
    //mRefreshTimer = new Timer();
    //mRefreshTimer.schedule(new RefreshTimerTask(), 5*1000);
}

private void finalizeActivity() {
    Logs.d(TAG, "# Activity - finalizeActivity()");

    if(!AppSettings.getBgService()) {
        doStopService();
    } else {
    }

    // Clean used resources
    RecycleUtils.recursiveRecycle(getWindow().getDecorView());
    System.gc();
}

/**
 * Launch the DeviceListActivity to see devices and do scan
 */
private void doScan() {
    Intent intent = new Intent(this, DeviceListActivity.class);
    startActivityForResult(intent, Constants.REQUEST_CONNECT_DEVICE);
}

/**
 * Ensure this device is discoverable by others
 */
private void ensureDiscoverable() {
    if (mService.getBluetoothScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        startActivity(intent);
    }
}

/*****************************************************
 *  Public classes
 ******************************************************/

/**
 * Receives result from external activity
 */
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Logs.d(TAG, "onActivityResult " + resultCode);

    switch(requestCode) {
        case Constants.REQUEST_CONNECT_DEVICE:
            // When DeviceListActivity returns with a device to connect
            if (resultCode == Activity.RESULT_OK) {
                // Get the device MAC address
                String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
                // Attempt to connect to the device
                if(address != null && mService != null)
                    mService.connectDevice(address);
            }
            break;

        case Constants.REQUEST_ENABLE_BT:
            // When the request to enable Bluetooth returns
            if (resultCode == Activity.RESULT_OK) {
                // Bluetooth is now enabled, so set up a BT session
                mService.setupBT();
            } else {
                // User did not enable Bluetooth or an error occured
                Logs.e(TAG, "BT is not enabled");
                Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
            }
            break;
    }   // End of switch(requestCode)
}

/*****************************************************
 *  Handler, Callback, Sub-classes
 ******************************************************/

public class ActivityHandler extends Handler {
    @Override
    public void handleMessage(Message msg)
    {
        switch(msg.what) {
            // BT state messages
            case Constants.MESSAGE_BT_STATE_INITIALIZED:
                mTextStatus.setText(getResources().getString(R.string.bt_title) + ": " +
                        getResources().getString(R.string.bt_state_init));
                mImageBT.setImageDrawable(getResources().getDrawable(android.R.drawable.presence_invisible));
                break;
            case Constants.MESSAGE_BT_STATE_LISTENING:
                mTextStatus.setText(getResources().getString(R.string.bt_title) + ": " +
                        getResources().getString(R.string.bt_state_wait));
                mImageBT.setImageDrawable(getResources().getDrawable(android.R.drawable.presence_invisible));
                break;
            case Constants.MESSAGE_BT_STATE_CONNECTING:
                mTextStatus.setText(getResources().getString(R.string.bt_title) + ": " +
                        getResources().getString(R.string.bt_state_connect));
                mImageBT.setImageDrawable(getResources().getDrawable(android.R.drawable.presence_away));
                break;
            case Constants.MESSAGE_BT_STATE_CONNECTED:
                if(mService != null) {
                    String deviceName = mService.getDeviceName();
                    if(deviceName != null) {
                        mTextStatus.setText(getResources().getString(R.string.bt_title) + ": " +
                                getResources().getString(R.string.bt_state_connected) + " " + deviceName);
                        mImageBT.setImageDrawable(getResources().getDrawable(android.R.drawable.presence_online));
                    }
                }
                break;
            case Constants.MESSAGE_BT_STATE_ERROR:
                mTextStatus.setText(getResources().getString(R.string.bt_state_error));
                mImageBT.setImageDrawable(getResources().getDrawable(android.R.drawable.presence_busy));
                break;

            // BT Command status
            case Constants.MESSAGE_CMD_ERROR_NOT_CONNECTED:
                mTextStatus.setText(getResources().getString(R.string.bt_cmd_sending_error));
                mImageBT.setImageDrawable(getResources().getDrawable(android.R.drawable.presence_busy));
                break;

            ////////////////////////////////////////////
            // Contents changed
            ////////////////////////////////////////////
            case Constants.MESSAGE_READ_ACCEL_REPORT:
                ActivityReport ar = (ActivityReport)msg.obj;
                if(ar != null) {
                    TimelineFragment frg = (TimelineFragment) mSectionsPagerAdapter.getItem(LLFragmentAdapter.FRAGMENT_POS_TIMELINE);
                    frg.showActivityReport(ar);
                }
                break;

            case Constants.MESSAGE_READ_ACCEL_DATA:
                ContentObject co = (ContentObject)msg.obj;
                if(co != null) {
                    GraphFragment frg = (GraphFragment) mSectionsPagerAdapter.getItem(LLFragmentAdapter.FRAGMENT_POS_GRAPH);
                    frg.drawAccelData(co.mAccelData);
                }
                break;

            default:
                break;
        }

        super.handleMessage(msg);
    }
}   // End of class ActivityHandler

/**
 * Auto-refresh Timer
 */
private class RefreshTimerTask extends TimerTask {
    public RefreshTimerTask() {}

    public void run() {
        mActivityHandler.post(new Runnable() {
            public void run() {
                // TODO:
                mRefreshTimer = null;
            }
        });
    }
}

}
E isso é tudo o que aparece quando o aplicativo está operando no aparelho mobile:
Answer: O que está acontecendo é que o ViewPager está sobrepondo o TabLayout. Para corrigir isso, crie um novo layout chamado content_main.xml e inclua-o no activity_main.xml.
<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="8dp"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_weight="1"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:title="@string/app_name">

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/title_timeline" />

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/title_graph" />

        <android.support.design.widget.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/title_ll_settings" />
    </android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>

<include layout="content_main.xml"/>

No content_main.xml inclua o ViewPager.
<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:context=".MainActivity">
    </android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>

Isso é o suficiente pra corrigir o problema da sobreposição. Ou simplesmente adicione um marginTop de aproximadamente 110dp do ViewPager, mas não é uma prática recomendada. Também notei que você incluiu outro elemento dentro no ViewPager, e isso não faz sentido. Então deixe-o vazio.
O layout activity_main.xml precisa ficar dentro de um android.support.design.widget.CoordinatorLayout.
Comment: Eu testei isso mas ainda não funcionou, existe algo muito específico impedindo que o ViewPager seja apresentado pela Main Activity.


Unable to figure out right transformation

Question: I have obtained some data about how complexity of Java open source projects varies with time. I want to fit a curve to the data, however I am unable to figure out the right kind of transformation. I tried some that came immediately to my head: $\ 1/y \sim x,\ \log(y) \sim x,\ y \sim 1 + x + x^2$, etc. but I never got a good $R^2$ value.
Can you please suggest how do I proceed?
Below is the plot:

I have also shared the data here.
Comment: You may be aiming for too high an $R^2$, the data has noise in it.

Also, you say "time and complexity" but your graph has "lines of code" and "complexity"
Comment: How is project complexity measured?
Comment: I think sharing the data has led to better comments and responses than would have been possible without it.
Answer: y ~ log(x) results in a pretty good graph.  I can think of a bunch of good reasons to use that.  However, it's still hard to interpret without some more info on what complexity is here.
The data are noisy.  You're not seeing that as well in your raw plots because the very steep initial slope hides a lot of noise in graphs like that.  And it is pretty clear you have some heterogenous sources.  If you can make them predictors as well you'll explain more of the data.  As it is, the R2 with a log predictor is 0.54 and the QQ plot of residuals is great.  Also, even a loess model with reasonable smoothing will only give you an R2 equivalent of about 0.58.  That's a very small gain for a much more complex model (equivalent number of parameters 5).
Comment: Some very good discussion here.
Answer: First up: why is the value of $R^2$ important? If there's a certain level of noise in your data, you don't fix it (indeed, you can't), you measure it.
A loess smooth suggests that there's not an obvious simple functional relationship between mean scattering and log(LOC), though it does look as if monotonicity could be plausible:

Are there other predictors? Is there likely to be any serial dependence issues or other forms of substantive dependence?
Constant variance doesn't look tenable as an assumption; you might consider modelling both mean and variance (and perhaps something like AVAS - e.g. via the avas function in the ace package in R - might be relevant to you).
Edit: whuber makes some good points in the comment - you should certainly take that into account before jumping into something like AVAS.
Comment: +1 Nice analysis. These data actually *do* look homoscedastic: you have to bear in mind that where log(LOC) is large, there is little data. The deceptive parts are the linear sequences of points: they probably reflect groups of closely related projects, indicating the points in this plot are not independent.
Comment: Thanks for this valuable comment. I could buy the argument that it's homoskedastic in the low end (where the spread looks small, but it could be easily put down to less data). Looking at log(LOC) above about 13.5 (LOC above 730K), the spread looks substantially smaller to me over a fairly large fraction of x-space. It *could* be due to unmodelled positive dependence, but without further information I wouldn't want to just assume the variance was constant. In the meantime, substantial doubt about the behavior away from the middle third would be prudent.


Help using preg_match for phone numbers

Question: how would i write an if statement that would find phone numbers and store them to a variable. Here is what i have so far but its not working.
if (preg_match('/^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$
/', $buffer, $matches))
{
    $phonenumber = html_entity_decode($matches[1]);
}

Comment: Can you provide some examples of phone numbers you want to match, and numbers you don't want to match? Or at least some more detailed requirements?
Comment: Can you explain in what way it's not working? Can you give an example where it fails, what output it gives, and what you expected?
Comment: This. Is a perfect "two problems" example.
Comment: It needs to match 3334445555, 333.444.5555, 333-444-5555, 333 444 5555, (333) 444 5555 and all combinations thereof.
Comment: And what about 1-333-444-5555? How about 333-444-5555x1234?
Answer: Since you're using preg_match(), I'll assume you're using PHP.  For phone numbers, because of their variability even in N.Am. (11,10 or 7 digits, varying or no separating characters, etc.) you may find a function like this easier to deal with than a regex:
function validphone(&$value) { //test for N.Am. phone number and reformat in standard format
    $valid=false;
    $area=NULL;
    $working=preg_replace('/\D/', '', $value); //only numbers left, no preceding zeros
    switch (strlen($working)) { //cases fall through from 11 to 7
        case 11: //e.g. 19024355764
            $working=stristr($working,'1'); //trims off 1st 1
        case 10: //e.g. 9024355764
            $area=substr($working,0,-7);
            $working=substr($working,3); //trims off 1st 3
        case 7: //e.g. 4355764
            $value=implode('-',array($area,substr($working,0,-4),substr($working,-4)));
            $valid=true;
            break;
        default:
            $valid=false;
            break;
    }
    return $valid;

}

ETA your questions in the comments:
You have a string that should be a phone number
$phonish='blahblah#._foo(123)4567890 ixlybob';
if(validphone($phonish)){ //function checks if $phonish is valid & reformats it in a standard way
  //do something with $phonish, which now equals '123-456-7890'
} else {
  echo 'not a valid phone number';
}

The validphone() function is most appropriate for shortish strings that are expected to be phone numbers. If you dump an entire page into a string and then feed it to validphone($mywholepage), it will extract all the numbers in the string at once. So text with multiple phone numbers will return false and text that happens to have 11,10 or 7 digits distributed throughout will return true.
Comment: Still no idea. If i add that to code, how do I return what it gets to a variable
Comment: @Kirk: the function is pass by reference, so the original `$value` is substituted with the reformatted one, if it exists and a boolean is returned so you know if it worked.  Call it like this `if(validphone($phonish)) //do something`.  In terms of how you determine what should be in `$phonish`, it sounds like you are committing the cardinal sin of parsing HTML with regexes. Don't.  There are better and easier options.  Just search SO for HTML and regex- it's one of the most popular questions out there.
Comment: I don't know how to call this function properly. Could you help me?
Answer: In the beginning, your searching for optional +1 and some greedy string. Changing
(?:[.-]\s*)?

to
(?:[.-]\d*?)?

should do the trick, but maybe there are even more problems in your regex.

Help using preg_match for phone numbers


Google Datastore new pricing effect operations

Question: Google Datastore will have new pricing effective July 1st (https://cloud.google.com/datastore/docs/pricing) and I having trouble understanding how the changes will effect me.
My KIND does have a structure to it. My kind is called MESSAGES and it looks like this for the every entity:
ID
FROM
TO
MESSAGE
DATE_CREATED
MISC1
MISC2

I have an index on ID, FROM, TO, DATE_CREATED, MISC1, and MISC2. With the new pricing:

What will be the cost of inserting a new entity into this kind?
If I run a query to get all the attributes and it returns 10    entities what is the cost of the query?
If I run a projection query to get all the attributes except MISC1    and MISC2 and it returns 10 entities what is the cost of the query?
If I update a entity with all these indexes what will be the cost?

The old pricing is based primarily on how many index you have, but it seems the new prices are not based on indexes at all. All the documentation on understanding the costs of read and writes are shown with indexes, so it is confusing how it applies without indexes in the pricing model. I would like know how much these 4 types of operations would cost in terms of read/write/small ops.
Comment: The short answer - it will be cheaper than you pay today as there are no more charges per indexed property. You still have to pay for the space the indexes occupy, but you are paying for it now too.
Answer: Writing a new Entity
In the current pricing model, inserting a new entity costs 2 write operations for the entity + 2 write operations per index.
So in your example, with 6 indexes properties it would be:

2 + 2 * 6 = 8 write operations
The effective price would be (8 * $0.06) per 100K entities written
Summary current: $0.48/100K

The new pricing just counts the entities written:

Summary new: $0.18/100K

Regular Queries
In the current model you are charged the number of entities returned + 1

11 read operations @ $0.06/100K

In the new pricing model, you only get charged the number entities

10 entity reads @ $0.06/100K

Projection Queries
Reading projections count as 'Small Ops' and are free. The query itself costs 1 read though - this stays the same in both current and new pricing models.
Updating Entities
In the current pricing model, updating an new entity costs 1 write operation for the entity + 4 write operations per index.
So in your example, with 6 indexes properties it would be:

1 + 4 * 6 = 25 write operations
The effective price would be (25 * $0.06) per 100K entities written
Summary current: $1.50/100K

The new pricing just counts the entities written:

Summary new: $0.18/100K
Comment: Thanks for the awesome explanation! Especially with specifying that projection queries use small ops as this was the most confusing to me!
Answer: Isn't the new one simpler? it's only based on the number of entities, ignore the indexes. You can see the number and explanation here https://cloudplatform.googleblog.com/2016/03/Google-Cloud-Datastore-simplifies-pricing-cuts-cost-dramatically-for-most-use-cases.html.
Comment: Thanks for the link, the link definitely made me understand the insert and update costs, but I still don't understand the query costs. So will my projection queries now cost more? If before a projection query was 1 write operation, but with the new pricing model if the costs is per entity retrieved then are projection queries no loner existent in the pricing model?
Comment: Projection query never has write ops, why would you count write for projection query?


Remove stack field from Nuxt createError response

Question: I made server API for validate some data on Nuxt like this :
export default defineEventHandler(async (event) => {
  const body = await readBody(event);

  const result = schema.safeParse(body);

  if (result.success === false) {
    const error = result.error;

    const customError = createError({
      statusCode: 412,
      statusMessage: `${error.errors[0].path} ${error.errors[0].message}`,
    });
    throw customError;
  }

  return { ok: true };
});

so when i have bad inputs the createError function get called and i get some return like this :
{
    "url": "/api/car/listings",
    "statusCode": 412,
    "statusMessage": "make Required",
    "message": "make Required",
    "stack": "
at createError (/H:/nuxt/cartrader/node_modules/h3/dist/index.mjs:48:15)\nat /H:/nuxt/cartrader/.nuxt/dev/index.mjs:794:25\nat process.processTicksAndRejections (node:internal/process/task_queues:95:5)\nat async Object.handler (/H:/nuxt/cartrader/node_modules/h3/dist/index.mjs:723:19)\nat async Server.toNodeHandle (/H:/nuxt/cartrader/node_modules/h3/dist/index.mjs:798:7)
"
}

the question is how can I remove that stack field in the error response because it is a bit annoying to have that in JSON response especially if you want share that api with others? I tried to remove it like this :
const customError = createError({
      statusCode: 412,
      statusMessage: `${error.errors[0].path} ${error.errors[0].message}`,
    });
    delete customError.stack;
    throw customError;

but it only remove content inside stack field and the field it self still exist
{
    "url": "/api/car/listings",
    "statusCode": 412,
    "statusMessage": "make Required",
    "message": "make Required",
    "stack": "

"
}

Answer: did some research on this. apparently it only includes the stack field if the server is in dev mode. so this won't be appended in production.
if it does bother you in development, the cleanest 'fix' (which is essentially the same as delete), you can do this:
throw createError({
  statusCode: 412,
  statusMessage: `${error.errors[0].path} ${error.errors[0].message}`,
  stack: undefined,
});


Prove that $A\subseteq B\iff \complement B\subseteq \complement A$

Question: I am working with the proof of $A\subseteq B\iff \complement B\subseteq \complement A$, from the book Introduction to Abstract Algebra of Thomas Whitelaw. This is the formal proof of the author (following a Venn Diagram justification).

Without error on my part, the author proves the direct statement and not the converse. What am I missing here? Thanks a lot!
Comment: every statement in this proof is an "if and only if" statement, and you can simply trace the proof backwards to show the converse
Comment: We have the words "it is equivalent to", which is the same thing as "if and only if". The author assumes you are already familiar with contraposition though.
Answer: We can break down the argument and show that every step can go both ways, i.e., "if and only if".

"$A\subseteq B$" if and only if "$x\in A\Longrightarrow x\in B$": this is the definition of subset relation, so it goes both ways.
"$x\in A\Longrightarrow x\in B$" if and only if "$x\notin B\Longrightarrow x\notin A$": If you can understand that the second statement is the contrapositive of the first statement, you would also realize that the first statement is the contrapositive of the second statement. Therefore, the statements are equivalent.
"$x\notin B\Longrightarrow x\notin A$" if and only if "$x\in \complement B\Longrightarrow x\in \complement A$": this uses the definition of set complements, so it also goes both ways.
"$x\in \complement B\Longrightarrow x\in \complement A$" if and only if "$\complement B\subseteq \complement A$": similar to (1), this is the definition of subset relation.


Node.js in production with forever and connect's vhosts / nginx alternative?

Question: I'm using mainly node.js for my projects and I use nodemon for development and forever for production.
I usually split my projects in vhosts so my structure could be something like this:
bootstrap.js
apps/
  admin/
  front/
  api/

and my bootstrap.js look like this:
// Get Express
var express = require('express');

// Create express server
var app = express.createServer();

// Configure Development Environment
app.configure('development', function() {
  //app.use(express.vhost('localhost', require('./apps/front')));
  app.use(express.vhost('localhost', require('./apps/admin')));
  app.use(express.vhost('localhost', require('./apps/api')));
  app.listen(3000);
});

// Configure Production Environment
app.configure('production', function() {
  app.use(express.vhost('example.com', require('./apps/front')));
  app.use(express.vhost('admin.example.com', require('./apps/admin')));
  app.use(express.vhost('api.example.com', require('./apps/api')));
  app.listen(80);
});

When I want to start my apps I use forever start bootstrap.js and bootstrap.js starts my defined vhost apps.
In that way I have each vhost listening in it's own port and vhost listening on 3000.
My problem now is that forever monitor's bootstrap.js and this have as a result that if any of vhost fails for any reason forever will reboot bootstra.js which will result in unnecessary restart of other vhosts too.
My question is what would be the best way to go in my case?
I though of instead having one bootstrap.js file to have a structure like this:
admin.js
front.js
api.js
apps/
  admin/
  front/
  api/

and use forever start for each one of those root js files, in this case I won't be using vhost but I believe vhosts are something that in one way or another I need them.
I though that in case I drop connect's vhosts to use nginx and have the following configuration for each app:
server
{
    listen 80;
    server_name admin.example.com;

    access_log /var/log/nginx/admin.example.com.access.log;

    error_log /var/log/nginx/admin.example.com.error.log;

    root /home/panosru/domains/example.com/apps/admin/public;

    location ~* ^.+\.(svg|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
        root /home/panosru/domains/example.com/apps/admin/public;
    }

    location / {
        proxy_pass http://admin.example.com:3001;
    }
}

I can see that with nginx I have some more benefits like I use nginx to serve my static content's instead of node, which would be an unnecessary overhead for node.
What do you think? Am I correct or you could propose me something better instead?
Thanks!
Answer: I think you basically answered your own question with a reasonable answer, but you're looking for some alternatives...
I'm not familiar with the vhost functionality in express.  However, I think you're asking for trouble by running what is effectively 3 different apps with 3 different scalability needs in a single Node.js process.
You could still use the express vhosts solution by using child_process.fork to launch multiple instances of express (all of which could still share the same http server).
Comment: yes in a way I answered my own question, I just need some confirmation, I didn't really knew about the child process fork and thanks for sharing but as you say, sharing the same http server between multiple node apps isn't the best way to do right? Also some type of alternatives I expected to hear would be things like nginx vs varnish or something like that, I'm just say that in order to make more clear the reason I did that question :) Thanks for taking the effort to answer :)


How to save selected Items to Qsettings from QListWidget, QTableWidget

Question: I am Using PyQt5 and Py3.7, I am trying to loop through all my Qlistwidgets and save their string data, but also save all selected items on that widget. I slightly modified the loop from here, but am having some trouble getting the selected items to save and restore using the listwidget array loop. I checked the docs but can't seem to understand how to add additional options into the array (such as save all selected items), from the Qt docs for SetArrayIndex, here.
My listWidgets have selectionMode set to MultiSelection.
I am currently saving using this:
def save_list_data(self):
    self.settings = QSettings("data.ini", QSettings.IniFormat)
    for name, obj in inspect.getmembers(self):
        if isinstance(obj, QListWidget):
            name = obj.objectName()
            self.settings.beginWriteArray(name)
            for i in range(obj.count()):
                self.settings.setArrayIndex(i)
                self.settings.setValue(name, obj.item(i).text())
            self.settings.endArray()

And then restoring the listWidget data using:
def open_list_data(self):
    self.settings = QSettings("data.ini", QSettings.IniFormat)
    for name, obj in inspect.getmembers(self):
        if isinstance(obj, QListWidget):
            name = obj.objectName()
            size = self.settings.beginReadArray(name)
            for i in range(size):
                self.settings.setArrayIndex(i)
                value = self.settings.value(name)
                if value != None:
                    obj.addItem(value)
            self.settings.endArray()

This works fine for the data, but how can I get the selectedItems from the ListWidgets to save and restore as well?
Comment: I am using PyQt5.
Answer: For my solution take into account the following:

Using the inspect module may be beneficial for other libraries, but for Qt the widget is not necessarily a member of the class so it is best to use the Qt introspection itself using findChildren.
In the example you use, you are only saving the text but a QListWidgetItem can have more information associated with the roles such as background color, foreground color, etc. So instead I will use the QDataStream operator since this take and save takes the item information.
I will use the "objectname/property" format to save the information since the same widget can have several properties that you want to save.
To save the information of the selected items, it is only necessary to save the row.

Considering the above, the solution is:
from PyQt5 import QtCore, QtGui, QtWidgets

class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.listwidget_1 = QtWidgets.QListWidget(
            objectName="listwidget_1",
            selectionMode=QtWidgets.QAbstractItemView.MultiSelection
        )
        listwidget_2 = QtWidgets.QListWidget(
            objectName="listwidget_2",
            selectionMode=QtWidgets.QAbstractItemView.MultiSelection
        )

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self.listwidget_1)
        lay.addWidget(listwidget_2)

        self.read_settings()

    def closeEvent(self, event):
        self.write_settings()
        super().closeEvent(event)

    def read_settings(self):
        settings = QtCore.QSettings("data.ini", QtCore.QSettings.IniFormat)
        childrens = self.findChildren(QtWidgets.QWidget)
        for children in childrens:
            if isinstance(children, QtWidgets.QListWidget) and children.objectName():
                settings.beginGroup(children.objectName())
                items = settings.value("items")
                selecteditems = settings.value("selecteditems")
                selectionMode = settings.value("selectionMode", type=QtWidgets.QAbstractItemView.SelectionMode)
                children.setSelectionMode(selectionMode)
                # In the first reading the initial values must be established
                if items is None:
                    if children.objectName() == "listwidget_1":
                        for i in range(10):
                            children.addItem(QtWidgets.QListWidgetItem(str(i)))
                    elif children.objectName() == "listwidget_2":
                        for i in "abcdefghijklmnopqrstuvwxyz":
                            children.addItem(QtWidgets.QListWidgetItem(i))
                else:
                    stream = QtCore.QDataStream(items, QtCore.QIODevice.ReadOnly)
                    while not stream.atEnd():
                        it = QtWidgets.QListWidgetItem()
                        stream >> it
                        children.addItem(it)
                    stream = QtCore.QDataStream(selecteditems, QtCore.QIODevice.ReadOnly)
                    while not stream.atEnd():
                        row = stream.readInt()
                        it = children.item(row)
                        it.setSelected(True)
                settings.endGroup()

    def write_settings(self):
        settings = QtCore.QSettings("data.ini", QtCore.QSettings.IniFormat)
        childrens = self.findChildren(QtWidgets.QWidget)
        for children in childrens:
            if isinstance(children, QtWidgets.QListWidget) and children.objectName():
                settings.beginGroup(children.objectName())
                items = QtCore.QByteArray()
                stream = QtCore.QDataStream(items, QtCore.QIODevice.WriteOnly)
                for i in range(children.count()):
                    stream << children.item(i)
                selecteditems = QtCore.QByteArray()
                stream = QtCore.QDataStream(selecteditems, QtCore.QIODevice.WriteOnly)
                for it in children.selectedItems():
                    stream.writeInt(children.row(it))
                settings.setValue("items", items)
                settings.setValue("selecteditems", selecteditems)
                settings.setValue("selectionMode", children.selectionMode())
                settings.endGroup()

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

Plus:
import contextlib
from PyQt5 import QtCore, QtGui, QtWidgets

class SettingsManager:
    def __init__(self, filename):
        self.m_settings = QtCore.QSettings(filename, QtCore.QSettings.IniFormat)

    @property
    def settings(self):
        return self.m_settings

    def read(self, widget):
        self.settings.beginGroup(widget.objectName())
        if isinstance(widget, QtWidgets.QAbstractItemView):
            selectionMode = self.settings.value(
                "selectionMode", type=QtWidgets.QAbstractItemView.SelectionMode
            )
            widget.setSelectionMode(selectionMode)
        if isinstance(widget, QtWidgets.QListWidget):
            items = self.settings.value("items")
            selecteditems = self.settings.value("selecteditems")
            # In the first reading the initial values must be established
            if items is None:
                self.read_defaults(widget)
            else:
                stream = QtCore.QDataStream(items, QtCore.QIODevice.ReadOnly)
                while not stream.atEnd():
                    it = QtWidgets.QListWidgetItem()
                    stream >> it
                    widget.addItem(it)
                stream = QtCore.QDataStream(
                    selecteditems, QtCore.QIODevice.ReadOnly
                )
                while not stream.atEnd():
                    row = stream.readInt()
                    it = widget.item(row)
                    if it is not None:
                        it.setSelected(True)
        if isinstance(widget, QtWidgets.QTableWidget):
            rowCount = self.settings.value("rowCount", type=int)
            columnCount = self.settings.value("columnCount", type=int)
            widget.setRowCount(rowCount)
            widget.setColumnCount(columnCount)
            items = self.settings.value("items")
            if items is None:
                self.read_defaults(widget)
            else:
                stream = QtCore.QDataStream(items, QtCore.QIODevice.ReadOnly)
                while not stream.atEnd():
                    it = QtWidgets.QTableWidgetItem()
                    i = stream.readInt()
                    j = stream.readInt()
                    stream >> it
                    widget.setItem(i, j, it)
                selecteditems = self.settings.value("selecteditems")
                stream = QtCore.QDataStream(
                    selecteditems, QtCore.QIODevice.ReadOnly
                )
                while not stream.atEnd():
                    i = stream.readInt()
                    j = stream.readInt()
                    it = widget.item(i, j)
                    if it is not None:
                        it.setSelected(True)
        self.settings.endGroup()

    def write(self, widget):
        self.settings.beginGroup(widget.objectName())
        if isinstance(widget, QtWidgets.QAbstractItemView):
            self.settings.setValue("selectionMode", widget.selectionMode())
        if isinstance(widget, QtWidgets.QListWidget):
            items = QtCore.QByteArray()
            stream = QtCore.QDataStream(items, QtCore.QIODevice.WriteOnly)
            for i in range(widget.count()):
                stream << widget.item(i)
            self.settings.setValue("items", items)
            selecteditems = QtCore.QByteArray()
            stream = QtCore.QDataStream(
                selecteditems, QtCore.QIODevice.WriteOnly
            )
            for it in widget.selectedItems():
                stream.writeInt(widget.row(it))

            self.settings.setValue("selecteditems", selecteditems)
        if isinstance(widget, QtWidgets.QTableWidget):
            self.settings.setValue("rowCount", widget.rowCount())
            self.settings.setValue("columnCount", widget.columnCount())
            items = QtCore.QByteArray()
            stream = QtCore.QDataStream(items, QtCore.QIODevice.WriteOnly)
            for i in range(widget.rowCount()):
                for j in range(widget.columnCount()):
                    it = widget.item(i, j)
                    if it is not None:
                        stream.writeInt(i)
                        stream.writeInt(j)
                        stream << it
            self.settings.setValue("items", items)
            selecteditems = QtCore.QByteArray()
            stream = QtCore.QDataStream(
                selecteditems, QtCore.QIODevice.WriteOnly
            )
            for it in widget.selectedItems():
                # print(it.row(), it.column())
                stream.writeInt(it.row())
                stream.writeInt(it.column())
            self.settings.setValue("selecteditems", selecteditems)
        self.settings.endGroup()

    def release(self):
        self.m_settings.sync()

    def read_defaults(self, widget):
        if widget.objectName() == "listwidget_1":
            widget.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
            for i in range(10):
                widget.addItem(QtWidgets.QListWidgetItem(str(i)))
        elif widget.objectName() == "listwidget_2":
            widget.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
            for i in "abcdefghijklmnopqrstuvwxyz":
                widget.addItem(QtWidgets.QListWidgetItem(i))
        elif widget.objectName() == "tablewidget":
            widget.setSelectionMode(QtWidgets.QAbstractItemView.MultiSelection)
            widget.setRowCount(10)
            widget.setColumnCount(10)
            for i in range(widget.rowCount()):
                for j in range(widget.columnCount()):
                    it = QtWidgets.QTableWidgetItem("{}-{}".format(i, j))
                    widget.setItem(i, j, it)

@contextlib.contextmanager
def settingsContext(filename):
    manager = SettingsManager(filename)
    try:
        yield manager
    finally:
        manager.release()

class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.listwidget_1 = QtWidgets.QListWidget(objectName="listwidget_1")
        listwidget_2 = QtWidgets.QListWidget(objectName="listwidget_2")

        tablewidget = QtWidgets.QTableWidget(objectName="tablewidget")

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(self.listwidget_1)
        lay.addWidget(listwidget_2)
        lay.addWidget(tablewidget)

        self.read_settings()

    def closeEvent(self, event):
        self.write_settings()
        super().closeEvent(event)

    def read_settings(self):
        with settingsContext("data.ini") as m:
            for children in self.findChildren(QtWidgets.QWidget):
                if children.objectName():
                    m.read(children)

    def write_settings(self):
        with settingsContext("data.ini") as m:
            for children in self.findChildren(QtWidgets.QWidget):
                if children.objectName():
                    m.write(children)

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

Comment: @Laylor010 If you do not tell me what your confusion is I can not help you, on the other hand I have created an example of how to implement the logic for QTableWidget
Comment: Does this method only work for static items?  My lists will always have different data in them. So the part to at your comment `initial values must be established`, would not work in my case. Any method around that and only identify selected items? Basically making a save/restore feature for data in listWidgets. Everything is there but the selections.
Comment: @Laylor010 I did not say that the items are static but must have an initial value. My solution works if in the execution of the application items are added dynamically.
Comment: Was able to get that to work, still a little confused with `children.objectName()` and the `children.addItem` section. Would something like this work for tableWidgets as well?
Comment: I'll review it further with the documentation, Thanks for the answer and the additional info for the TableWidgets.


How to assign the TField to TcxGridtableViewColumn

Question: I am working on one project where I want to assign the TField to the TcxGridTableViewColumn.
When I am Trying to do this it is Showing Error as Incompatible type TField with TcxGridTableViewColumn.
To overcome this I tried to do this by Typecasting But I am Unable to do this too.
In this case it is showing Exception as Unable to edit Running Database.
I am not able to trace out the solution.
Can any one tell me the way to do this?
Thanks in Advance.
Comment: You should change the delphi version to be the one you are using and also keep the Delphi tag. for the question, I don't understand what you are trying to do. A TField is one type of object and a TcxGridTableViewColumn is a different type. What would you like to accomplish at the end of the day?
Answer: Assuming you want to bind the column of a TcxGrid to a field in a dataset, you need to connect the gridView.DataController.DataSource property to a TDataSource that is link to your dataset. The grid can then either retrieve it's fields and create columns for those fields, or you can create them manually and set the column.DataBinding.Fieldname property to the field.FieldName property.


Sample size for cluster analysis

Question: We have recently conducted a cluster analysis for an open card sorting task in which 19 experts grouped 112 items each. We achieved decent silhouette values for the cluster analysis but have been asked by reviewers to provide evidence that the number of experts we used was sufficient for this task (I think they are expecting something kind of power analysis).
We have used Kendall's Coefficient of Concordance to examine the concordance in individual sort patterns between participants (this came out as .25), but it seems they want something more. After looking around I cannot find anything which might suggest a suitable sample size or even that sample size is not important and I'm pretty sure power analyses don't apply for cluster analysis. Is there anything I can report to show my sample size is sufficient (or not if the case may be)? I am using R to analyse the data.
BTW by sample size I mean number of participants not number of items included in the cluster analysis.
Comment: I could not see cluster analysis in what you've done. Is that grouping of cards by experts you call cluster analysis? Or, if you did cluster analysis on the results of that grouping please provide more details about that analysis.
Comment: @ttnphns Sorry, I should have added more detail. The participants are asked to group 112 items. We created a matrix for each participant from these groupings assigning 0 pairs to items which were assigned to the same group and 1 to pairs of items in different groups. So we had a binary matrix (a simple distance matrix) for all possible item pairings for each participant. These were then averaged across participants to created one aggregated distance matrix. This aggregated distance matrix was then submitted for cluster analysis (fuzzy CA) to identify the grouping of items across participants.
Comment: I see. Thanks. Cluster analysis is not inferential technique, so question of power cannot be arised. However, if your experts are in a good agreement according to Kendall W that you used (and there were no unusual experts-outliers), than the averaging of the matrices into one and doing clustering of it is warranted whatever the number of experts is. The question of the number of experts (the sample size) is important, as always, but is unrelated to the cluster analysis.
Comment: If I understand correctly, each participant created a partition of the data in groups or clusters. You can look at mutual consistency between participants by considering distances between clusterings (I prefer a metric distance like 'variance of information' over indexes such as Rand). This would give you a participant/participant (dis)agreement matrix. For a unified clustering there is a lot published about 'consensus clustering'. Unfortunately this does not answer your question, but relates to a stage of your research preceding that question.
Comment: Thanks so much for your answers. I think I have enough information to formulate a response to the reviewers now. I will definitely be looking up 'consensus clustering' seems very interesting.
Answer: It sounds like they want you to do a post hoc power analysis. The purpose of a power analysis is to optimize the study that will be conducted (e.g., to increase the likelihood of getting a statistically significant result). A power analysis is always speculative - you have to guess what the reality is. A power analysis is NOT data analysis. It sounds like you've already conducted the study. So the original power analysis cannot tell you ANYTHING about the results. A "new" power analysis could tell you how to optimize the NEXT study, but cannot inform the current study. All your results are represented by your statisitcal analysis (your parameter estimates, confidence intervals, and p-values). There have been many articles in the statistical literature on why post hoc power analysis makes no sense and can only lead to contradictions. Uninformed researchers often resort to this to try to explain why their results were NOT statistically significant - but this is not possible. If your results WERE statistically significant, well  that's your results (you can't argue with statistical significance). Search for the paper "The Abuse of Power: the Pervasive Fallacy of Power Calculations as Data Analysis"- http://www.vims.edu/people/hoenig_jm/pubs/hoenig2.pdf pu blished in The American Statistician. It's sad when reviewers aske researches to do ridiculous things that make no sense statistically.
Comment: I don't think power analysis is applicable to cluster analysis - there are no hypotheses being tested, and no statistical significance.

Sample size for cluster analysis


best way display array order by key

Question: <?php
$array = array(
    "1" => 'Hi',
    "4" => 'are',
    "3" => 'How',
    "7" => 'my',
    "6" => 'you',
    "9" => 'brother',
);

forEach($array as $key => $value) {
    echo $key;
    echo ':-';
    print_r($value);
    echo '
';
}
?>

the out put of this code is
1:-Hi
4:-are
3:-How
7:-my
6:-you
9:-brother

but i need to display this order by key. please tell me which is easiest way
thanks
Answer: Use ksort
ksort($array);

foreach($array as $key => $value) {
    echo $key;
    echo ':-';
    print_r($value);
    echo '
';
}

Answer: The nice thing about PHP is that there's a function for everything. You can use the ksort function to sort the array by its keys: http://php.net/manual/en/function.ksort.php
Your new code would look like this:
<?php
$array = array(
"1" => 'Hi',
"4" => 'are',
"3" => 'How',
"7" => 'my',
"6" => 'you',
"9" => 'brother',
);

ksort($array);

forEach($array as $key => $value) {
echo $key;
echo ':-';
print_r($value);
echo '
';
}
?>

Comment: asort is sort by value, not key. `a` means maintain index association
Answer: use ksort(), this will arrange it by key order.
<?php
$array = array(
"1" => 'Hi',
"4" => 'are',
"3" => 'How',
"7" => 'my',
"6" => 'you',
"9" => 'brother',
);
ksort($array);

forEach($array as $key => $value) {
echo $key;
echo ':-';
print_r($value);
echo '
';
}
?>

best way display array order by key


How to prevent a mobile app from being debugged - React Native?

Question: How to prevent a debugger from attaching to the running application in a react native app?
Comment: There are a lot of known anti-debugging techniques, but I don't think many of them can be applied in React Native, usually such measures are implemented in C. Implementing good anti-debugging measures require a lot of knowledge on processes, threads, debugging and the underlaying platform. Alternatively there are commercial tools for hardening apps, not sure how much of them do work with React Native. On such solutions as a rule of thumb you get what you pay for, which means the more expensive the solution the better the protection.
Answer: with XCode just running in Release mode, in Android Studio I only build apk file to test, before build .aab file to upload to Google Console


When creating a content type programmatically, is there a way to disable promoting to the front page by default?

Question: In Drupal 7, I'm creating a large number (15+) of content types programmatically. There is one in particular that should never be promoted to the front page. It would be really nice if, by default, the box to do this was unchecked. I'm aware that this can be done through the front-end, but my goal is to make the web application as portable as possible. That is, other than content, I want it to be able to be moved completely and properly by only installing the correct modules.
I'm removing authoring information, for instance, by setting the variable node_submitted_{content type name} to FALSE. It would seem like there should be something like this available for promoting by default.
Thanks.
Answer: Whether nodes of a particular content type are promoted to the front page by default is stored in the Drupal array variable node_options_{content type name}:
$node_options = variable_get('node_options_{content type name}', array());
$promote_key = array_search('promote', $node_options);
if ($promote_key !== false) {
  unset($node_options[$promote_key]);
  variable_set('node_options_{content type name}', $node_options);
}

Answer: BEWARE: when the node_options_{content type name} variable is not set a default is used that features the promote option.
so I needed to actually set the options just to 'status' that means published:
variable_set('node_options_drupal_projects', array('status'));


AJAX calls in CTools Content Type Plugin?

Question: I'm building a CTools Panels Content Type (i.e., that thing you insert into panels when adding content, not a type of node) and I am trying to use a form item's #ajax attribute to set some default values. See code below.
This is all inside of the content type's my_module_content_type_edit_form($form, &$form_state) call, by the way.
  $form['link_type'] = array(
    '#type' => 'radios',
    '#title' => t('Link Type'),
    '#ajax' => array(
      'callback' => 'my_module_set_target'
    ),
    '#default_value' => empty($conf['link_type']) ? '_blank' : $conf['link_type'],
    '#options' => array('none'=>t('No Link'), 'internal'=>t('Internal Link'), 'external'=>t('External Link'), 'document'=>t('Document Link')),
  );

My callback is the following.
function my_module_set_target($form, $form_state) {
  watchdog("Test", "Testing callback", array(), WATCHDOG_ALERT);
  $form['link_target']['#default_value'] = '_parent';

  return $form['link_target']['#default_value'];
}

Regardless of whether the return I'm suggesting would actually work, watchdog() doesn't even work.
I know CTools does some weird stuff with AJAX, but it can't be that weird. Do you have any idea on how I'd do what I'm wanting to do?
Thanks!
Alternatively: How do I set a default value based on the value of a previous form option?
I figured out how to do this, but it's a bit hacky -- you create new form fields for each dependency fork. You can then merge the values together in hook_content_type_edit_form_submit(), using whichever one corresponds to the value chosen for the component that initially forked everything.
I'm leaving the question open because I (and, frankly, every programmer I'm working with) is really wanting a good way to use AJAX inside these Panel content type edit forms.
Update: It appears you can't do stuff with #attached, either.
$form['link'][$i] = array(
  '#type' => 'fieldset',
  '#title' => t('Link #@num', array('@num' => $i)),
  '#collapsible' => TRUE,
  '#collapsed' => TRUE,
  '#attached' => array(
    'js' => array(
      'alert("Yay.");', 'inline'
    ),
  )
);

Comment: Being a "Panels for everything" type of Drupal developer, I imagine I could find use for this in the future as well, so I added a bounty, lets see what happens.
Comment: Wow, that bounty came and went without even a comment (Thanks BTW, Letharion). Is what I'm asking impossible or something?
Comment: Should be worth noting that I've been able to successfully add Javascript using `ctools_add_js();` or `drupal_add_js();` at the end of `hook_content_type_edit_form();`. If you're just doing simple UI-related stuff, seems like that might be the best call (At least until somebody properly answers this question).
Answer: Short answer: you should use #ajax['path'].
Long answer:
Having a ajax callback does not help as ctools creates it's forms differently afaik. The callback done by system/ajax cannot find the complete form definition thus cannot find an element to process the ajax request for. Using #ajax[path] just triggers a menu item.
You can check for yourself by dumping the form when using #ajax[callback]
function ajax_form_callback() {
  list($form, $form_state) = ajax_get_form();
  drupal_process_form($form['#form_id'], $form, $form_state);

I've modified the simplecontext_content_type_edit_form by adding the user autocomplete widget and your field which both work :)
function simplecontext_content_type_edit_form($form, &$form_state) {
  $conf = $form_state['conf'];

  $form['owner_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Username'),
    '#default_value' => 'admin',
    '#autocomplete_path' => 'user/autocomplete',
    '#size' => '6',
    '#maxlength' => '60',
    '#description' => '$description',
  );

  $form['link_type'] = array(
    '#type' => 'radios',
    '#title' => t('Link Type'),
    '#ajax' => array(
      'path' => 'my_module_set_target'
    ),
    '#default_value' => empty($conf['link_type']) ? '_blank' : $conf['link_type'],
    '#options' => array('none' => t('No Link'), 'internal' => t('Internal Link'), 'external' => t('External Link'), 'document' => t('Document Link')),
  );
...

As you now use #ajax path you need to add a menu item as I did like
<?php

function my_module_menu() {
  $items = array(
    'my_module_set_target' => array(
      'title' => 'AJAX Example',
      'page callback' => 'my_module_set_target',
      'access callback' => TRUE,
      'expanded' => TRUE,
    )
  );
  return $items;
}

function my_module_set_target() {
  drupal_json_output( array('data' => "ABC"));
}

A nittpick about #attached[js]: the inline js should be key => value like:
'#attached' => array(
  'js' => array(
    'alert("Yay.");' => 'inline',
  ),
),

I've used Firebug to check for the resulting value not the resulted effect. So I hope this helps to fix your problem.
Comment: Wow. I am so stoked to finally have an answer to this question. Will try it out in a bit. Thanks also for the note on the #attached bit, that's actually what I had initially (But changed it because I figured that couldn't be right).
Comment: So, any success? Would be interesting to know before the bounty is over :)
Comment: I hope I do get my bounty :p
Comment: Sorry for the delay with this, having a bit of a time getting it working -- when I try to return the $form array, it tells me my validate function isn't defined. It is, and when I try to re-declare it in my module (as opposed to content type plugin file), it tells me I'm redeclaring it. I'm guessing that's not what I need to do, but I can't figure out how to structure drupal_json_output so it outputs something that my form will interpret as a meaningful response. Any help? A better example callback would help a LOT. Thanks!
Comment: I've missed the tiny little part of your question which is about the return value: __return $form['link_target']['#default_value'];__ which is your trying to return a __drupal__ form to a __html/javascript__ context. That will not work. Have you seen similar thing what you want in the panels UI? (I myself disable JavaScript now and then with views UI just to see the non js workflow) (edited this comment a zillion times ... awkward)
Answer: I've had a similar problem, where I wanted to include a Media element type into a CTools content type plugin, that also uses ajax for selecting an image.
It uses it's own ajax 'path' setting, instead of the 'callback' setting, but when selecting an image, the form was rebuilt without the media element entirely.
I traced this to the fact that drupal_rebuild_form couldn't find neither the CTools form wrapper function, nor the actual settings form function. So I fixed it by adding these lines of code to the ctools settings form:
function custom_module_my_content_plugin_content_type_edit_form($form, &$form_state) {

$background_image = isset($conf['background_image']) ? $conf['background_image'] : array();
  $form['background_image'] = array(
    '#title' => t('Background image'),
    '#default_value' => $background_image,
    '#type' => 'media',
    '#input' => TRUE,
    '#extended' => TRUE,
    '#tree' => TRUE,
    '#media_options' => array(),
  );

  // The two function calls below are necessary if we want to use a media
  // element type, because it causes ajax requests, which in turn call
  // drupal_form_rebuild(), and without the below includes, Drupal will
  // not be able to rebuild the form.

  // Include the CTools content type plugin file, because it provides
  // the ctools_content_configure_form_defaults() function, which is needed
  // when rebuilding the form, because of an ajax action, like selecting
  // a media element.
  ctools_form_include($form_state, 'content');

  // Include this plugin file as well, so that when the form is rebuilt, it
  // can successfully retrieve the settings form.
  ctools_form_include($form_state, 'my_content_plugin', 'custom_module', 'plugins/content_types/my_content_plugin');

}

Maybe I'm missing something obvious why the include files are not loaded, but including them manually fixed the issue for me.
Answer: on my side, I had to write a form wrapper function in .module file and to include the pane manually (where original form content is defined) like that:
function mymodule_form($form, &$form_state) {
  // include mymodule/panes/mypane.inc
  ctools_include('mypane', 'mymodule', 'panes');
  return mymodule_form_content($form, $form_state);
}

Now, during ajax call, Drupal is able to retrieve my form so I can use the standard AJAX form API.


Normalize function for train, test, and validation sets

Question: I am trying to normalize data for a neural network task.  I want to normalize the training set using this function:
normalize <- function(x) {
  return((x-min(x)) / (max(x)-min(x)))
}

so I can then use it in this way:
trainData1 <- as.data.frame(lapply(trainData, normalize))

I am new to writing functions in R and need a function to save the min/max from each column in the pre-normalized training set to then use in normalizing each column in the testing and validation sets.
My idea would be to write a function like:
normalize <- function(x, trainData) {
  return((x-min(trainData[,x])) / (max(trainData[,x]-min(trainData[,x]))
}

where trainData is already defined as a data.frame and has the same column names as the test data set.
When I run the function I get:
testData1 <- as.data.frame(lapply(testData, normalize))
Error in FUN(X[[i]], ...) :
  argument "trainData" is missing, with no default

Anyone have any ideas?
Comment: I'm voting to close this question as off-topic because it is about how to use R without a reproducible example.
Comment: Please add a [reproducible example](http://stackoverflow.com/q/5963269/1217536) for people to work with.
Answer: Questions like this might perhaps be better asked at stackoverflow, and there are R packages which do training and test data rescaling as part of machine learning, such as the preProcess function in the caret package
The following may not be the prettiest or fastest but it seems to produce more or less what you are looking for (lapply is just a loop, so if it is not working for you then consider using an explicit loop)
normalize <- function(newdataf, dataf){
  normalizeddataf <- newdataf
  for (n in names(newdataf)){
     normalizeddataf[,n] <-
         (newdataf[,n] - min(dataf[,n])) /  (max(dataf[,n]) -  min(dataf[,n]))
     }
  return(normalizeddataf)
}

which given some toy numbers
trainData <- data.frame(a=c(4,7,9), b=c(19,11,12))
testData  <- data.frame(a=c(8,4),   b=c(14,15))
trainData1 <- normalize(trainData, trainData)
testData1  <- normalize(testData,  trainData)

results in
> trainData1
    a     b
1 0.0 1.000
2 0.6 0.000
3 1.0 0.125
> testData1
    a     b
1 0.8 0.375
2 0.0 0.500

much as you might expect


Can a CUDA parent kernel launch a child kernel with more threads than the parent?

Question: I'm trying to learn how to use CUDA Dynamic Parallelism.
I have a simple CUDA kernel that creates some work, then launches new kernels to perform that work. Let's say I launch the parent kernel with only 1 block of 1 thread, like so:
<code>int nItems = 100
parentKernel<<<1,1>>>(nItems);
</code>
Now, inside my parent kernel, I create the work, then launch a sub-kernel, like this:
<code>__global__ void parentKernel(int nItems)
{
    // create some work

    // invoke child kernel
    childKernel<<<2, 256>>>();
}
</code>
Notice that the child kernel was launched with more threads and blocks (2x256) than the parent kernel was given (1x1).
Will the child kernel actually run 512 threads in parallel? Or does the parent kernel have to divvy out its threads to its children?
Answer: The child kernel launch is just as if you had launched a new kernel from host code. There is no restriction based on the parent kernel that governs the grid size of a child kernel.
Your child kernel will run with 2 blocks of 256 threads each, as if you had launched it from host code.  Those would all run "in parallel" on any GPU I can think of.


Derivations of the algebra $K[x,y]/(y^2-x^3)$

Question: Let $K$ be a field of characteristic zero, and let $S=K[x,y]/(y^2-x^3)$. It is easy to see that $S\cong R:=K[t^2,t^3]$ via the isomorphism induced by the ring homomorphism $K[x,y]\to R$ given by $x\mapsto t^2, y\mapsto t^2$. It is known that for any $K$-algebra $A$ and any ideal $J$,
$$
\operatorname{Der}_K(A/J) \cong \operatorname{Der}_J(A)/J\operatorname{Der}_K(A),
$$
where $\operatorname{Der}_K(A)$ is the space of $K$-linear derivations of $A$, and
$$
\operatorname{Der}_J(A) = \{D\in \operatorname{Der}_K(A) \; | \; D(J)\subseteq J\}.
$$
In Exercise 3.6 of Chapter 3 in the book A primer on Algebraic $D$-modules by Coutinho, I'm asked to show that the derivations
$$
D_1 = 2y\partial_x + 3x^2\partial_y \quad \text{and} \quad D_2 = 3y\partial_y - 2x\partial_x
$$
generate $\operatorname{Der}_K(S)$ as $S$-module. Then I have to conclude that $\operatorname{Der}_K(R)$ is generated as an $R$-module by $t\partial_t$ and $t^2\partial_t$.
I have two issues here: First, it seems to me that $D_2$ must be $3y\partial_y + 2x\partial_x$, as the application of the chain rule gives
$$
t\partial_t f = t \frac{\partial f}{\partial x} \frac{dx}{dt} + t \frac{\partial f}{\partial y} \frac{dy}{dt} = 2t^2 \frac{\partial f}{\partial x} + 3t^3 \frac{\partial f}{\partial y} = (2x\partial_x + 3y\partial_y)f
$$
and
$$
t^2\partial_t f = t^2 \frac{\partial f}{\partial x} \frac{dx}{dt} + t^2 \frac{\partial f}{\partial y} \frac{dy}{dt} = 2t^3 \frac{\partial f}{\partial x} + 3t^4 \frac{\partial f}{\partial y} = (2y\partial_x + 3x^2\partial_y)f = D_1f.
$$
Is it a typo in the book or am I misunderstanding something?
The second one is about the proof that $D_1$ and $D_2$ generates $\operatorname{Der}_K(S)$ as $S$-module. First of all, an element of $\operatorname{Der_J(K[x,y])}$, where $J=(y^2-x^3)$ has the form $d=f_1 \partial_x + f_2\partial_y$, and the condition $d(J)\subseteq J$ means that
$$
-3x^2f_1 + 2yf_1 \in J,
$$
and the condition that that $D_1$ and $D_2$ generates $\operatorname{Der}_K(S)$ as $S$-module means that there are polynomials $g,h\in K[x,y]$ such that
$$
f_1 \partial_x + f_2\partial_y - gD_1 -hD_2 \in J\operatorname{Der}_K(K[x,y]),
$$
but I can't manage to show that such $g$ and $h$ would exist.
Working a little bit this attempt, I need to find polynomials $u,v\in K[x,y]$ such that
$$
f_1 \partial_x + f_2\partial_y - gD_1 -hD_2 = (y^2-x^3)(u\partial_x + v\partial_y).
$$
Taking $D_2 = 3y\partial_y + 2x\partial_x$ with the "right" sign, we can see that the above equation is equivalent to the following equalities:
$$
f_1 - 2yg - 2xh = (y^2-x^3u) \quad \text{and} \quad f_2 - 3x^2g - 3yh = (y^2-x^3)v,
$$
and, as $-3x^2f_1 + 2yf_2 \in J$ there exists some polynomial $w\in K[x,y]$ such that $-3x^2f_1 + 2yf_2=(y_2-x^3)w$. A little manipulation gives
$$
-6h = -3x^2v + 2yu - w
$$
and I don't know if I can do more, or even if my attempt is correct, so any help will be appreciated.
Comment: Are you at all familiar with Groebner bases?  (Because that's how I'd approach this problem, personally - set up the problem as asking to calculate the kernel of a certain $S$-module morphism, and then use the machinery of Groebner bases to calculate a generating set of that kernel.)
Comment: @DanielSchepler Yes! I'm familiar with Groebner basis.
Answer: In terms of the modules of Kähler differentials, we have an exact sequence of $S$-modules
$$J/J^2 \overset{\delta}{\rightarrow} S \,dx \oplus S \,dy \rightarrow \Omega^1_{S/K} \to 0.$$
(Here, the middle term is $\Omega^1_{K[x,y]/K} \otimes_{K[x,y]} S$, and $J / J^2 \simeq S$; and $\delta : y^2 - x^3 \mapsto 2y\,dy - 3x^2\,dx$.)  Taking the duals, we get an exact sequence
$$0 \to \operatorname{Der}_K(S) \to S \partial_x \oplus S \partial_y \to S.$$
Therefore, $\operatorname{Der}_K(S)$ is the kernel of the map $\varphi : S \partial_x \oplus S \partial_y \to S$ given by $\partial_x \mapsto -3x^2, \partial_y \mapsto 2y$.  (In particular, I think you're right that there's a typo in the book: $3y \partial_y + 2x \partial_x \mapsto 3y (2y) + 2x (-3x^2) = 6(y^2 - x^3) = 0$; so $3y \partial_y + 2x \partial_x$ is indeed in the kernel.)
Note that the above is essentially equivalent to your calculation based on $\operatorname{Der}_K(A/J) \simeq \operatorname{Der}_J(A) / J \operatorname{Der}_K(A)$ where $\operatorname{Der}_J(A) = \{ \partial \in \operatorname{Der}_K(A) \mid \partial J \subseteq J \}$.
Now, it is easy to see that the graph of $\varphi$, as a submodule of $S \partial_x \oplus S \partial_y \oplus S$, is generated by
\begin{align*}
e_1 & = ( 1, 0, -3x^2 ) \\
e_2 & = ( 0, 1, 2y).
\end{align*}
This is equivalent to looking at the $K[x,y]$-submodule of $K[x,y] \partial_x \oplus K[x,y] \partial_y \oplus K[x,y]$ generated by
\begin{align*}
\bar e_1 & = (1, 0, -3x^2 ) \\
\bar e_2 & = (0, 1, 2y) \\
f_1 & = (y^2 - x^3, 0, 0) \\
f_2 & = (0, y^2 - x^3, 0) \\
f_3 & = (0, 0, y^2 - x^3).
\end{align*}
From here, calculate a Gröbner basis for this submodule with respect to an elimination order in which the domain coordinates are smaller than the codomain coordinates.  That will make it easy to get a generating set for the intersection of the graph of $\varphi$ with $S \partial_x \oplus S \partial_y \oplus 0$, from which it will be easy to deduce a generating set for $\operatorname{ker}(\varphi) \simeq \operatorname{Der}_K(S)$.


Change the front page value for logged in users

Question: On my site I have two front pages:

node/31, for anonymous users
calendar-node-field-journal-date/month, for logged in users

When a users clicks on the front page (which is rendered as $front_page in my page.tpl.php file), I want them to be taken to the appropriate front page.
However, if a logged in user goes to node/31 directly (e.g. by not clicking on the front page link) they should still be able to see the node (and not be re-directed).
After researching, I've found Rules is the best way to do this and there are instructions here.
However, this re-directs logged in users when they visit node/31. How can I get the Rule to only run when the user clicks on the $front_page link and not on the direct node/31 link?
I don't want to set it up via my theme, as I am sharing the theme on a multi-site. I also don't really want to install other modules, as I am already using Rules for other things.
Answer: I would recommend the Front Page module, not rules:

The Front Page module allows for customised home pages dependant on
role. The pages can be of the following types:
Alias (only 2.x branch) - Allows for another Drupal path to be shown
as the home page without redirecting the user.
Redirect - Allows you to redirect users to a local or remote URL.
Themed - Allows you to enter some static text that will be placed into the content area > of a standard Drupal page. Full - Allows for static content to be displayed
to the screen as is. This method does not pass through Drupal's
theming system.
Comment: then I would suspect that that is expected behaviour - since Drupal associates the node with the Front page, it assumes your intent was to view the front page when the node is viewed
Comment: this has the same problem as rules: If the logged-in user clicks on node/31 directly they will be taken to the front page. Thanks for your help!