问:
NotImplementedError: Cannot convert a symbolic tf.Tensor (Log_2:0) to a numpy array
I have the following code which is based on https://github.com/leonard-seydoux/scatnet. The original code was based on TF v1 and I am in the process of migrating it to TF v2. However, I am facing some issues while trying to perform a numpy operation on a tensor. I am running the code on Google Colab.

The reproducible code is as below, sorry it is quite long:

```<code>import tensorflow as tf
import numpy as np
import scipy as sp

from datetime import datetime
from sklearn.decomposition import PCA
from sklearn.mixture import GaussianMixture

HERMITE = [[1, 0, -3, 2], [0, 0, 3, -2], [0, 1, -2, 1], [0, 0, -1, 1]]
FORMAT = 'float32'

def real_hermite_interp(xi, x, m, p):
    # Hermite polynomial coefficients
    h = tf.Variable(np.array(HERMITE).astype(FORMAT), trainable=False)
    xx = tf.stack([x[:, :-1], x[:, 1:]], axis=2)

    # The concatenated coefficients are of shape (n_knots - 1, 2)
    mm = tf.stack([m[:-1], m[1:]], axis=1)
    pp = tf.stack([p[:-1], p[1:]], axis=1)

    y = tf.concat([mm, pp], axis=1)

    # Extract Hermite polynomial coefficients from y (n_knots - 1, 4)
    yh = tf.matmul(y, h)

    xi_ = tf.expand_dims(tf.expand_dims(xi, 0), 0)
    x0_ = tf.expand_dims(xx[:, :, 0], 2)
    x1_ = tf.expand_dims(xx[:, :, 1], 2)
    xn = (xi_ - x0_) / (x1_ - x0_)

    # Calculate powers of normalized interpolation vector
    mask = tf.logical_and(tf.greater_equal(xn, 0.), tf.less(xn, 1.))
    mask = tf.cast(mask, tf.float32)
    xp = tf.pow(tf.expand_dims(xn, -1), [0, 1, 2, 3])

    # Interpolate
    return tf.einsum('rf,srtf->st', yh, xp * tf.expand_dims(mask, -1))

class Scattering:
    """Learnable scattering network layer."""

    def __init__(self, x, j=None, q=None, k=None, pooling_type='average',
                 decimation=2, pooling=2, index=0, **filters_kw):
        """Scattering network layer.

        Computes the convolution modulus and scattering coefficients of the
        input signal.

        Arguments
        ---------
            x: :class:`~tensorflow.Tensor()`
                Input data of shape ``(batch_size, channels, patch_shape).
        """
        # Filter bank properties
        self.shape_input = x.get_shape().as_list()
        self.j = j = j[index] if type(j) is list else j
        self.q = q = q[index] if type(q) is list else q
        self.k = k = k[index] if type(k) is list else k

        filters = self.init_filters(j, q, k, **filters_kw)
        n_filters, kernel_size = filters.get_shape().as_list()
        filters_concat = tf.concat([tf.math.real(filters), tf.math.imag(filters)], 0)
        filters_kernel = tf.expand_dims(tf.transpose(filters_concat), 1)

        # Pad input in the time dimension before convolution with half the size
        # of filters temporal dimension (kernel_size).
        shape_fast = [np.prod(self.shape_input[:-1]), 1, self.shape_input[-1]]
        paddings = [0, 0], [0, 0], [kernel_size // 2 - 1, kernel_size // 2 + 1]
        x_reshape = tf.reshape(x, shape_fast)
        x_pad = tf.pad(x_reshape, paddings=paddings, mode='SYMMETRIC')

        # Differentiate the case of one input channel or multiple
        # which needs reshaping in order to treat them independently
        # The "NCW" format stores data as batch_shape + [in_channels, in_width]
        x_conv = tf.nn.conv1d(x_pad, filters_kernel, stride=decimation,
                              padding='VALID', data_format='NCW')
        u = tf.sqrt(tf.square(x_conv[:, :n_filters]) +
                    tf.square(x_conv[:, n_filters:]))
        self.u = tf.reshape(u, (*self.shape_input[:-1], n_filters, -1))

        pool = tf.keras.layers.AveragePooling1D

        # Pooling for the scattering coefficients
        if pooling > 1:
            pooled = pool(
                pooling // (decimation ** (index + 1)),
                pooling // (decimation ** (index + 1)),
                padding='valid', data_format='channels_first')
            pooled = pooled(u)

        self.s = tf.reshape(pooled, self.shape_input[:-1] + [j * q] + [-1])
        self.output = self.s

        tf.compat.v1.disable_eager_execution()
        inverse = tf.gradients(x_conv, x, x_conv)[0]

        self.reconstruction_loss = tf.nn.l2_loss(
            inverse - tf.stop_gradient(x)) / np.prod(self.shape_input)

    def init_filters(self, j, q, k, learn_scales=False, learn_knots=False,
                     learn_filters=True, hilbert=False):
        extra_octave = 1 if learn_scales else 0
        self.filter_samples = k * 2 ** (j + extra_octave)

        time_max = np.float32(k * 2**(j - 1 + extra_octave))
        time_grid = tf.linspace(-time_max, time_max, self.filter_samples)

        scales_base = 2**(tf.range(j * q, dtype=tf.float32) / np.float32(q))
        scales_delta = tf.Variable(
            tf.zeros(j * q), trainable=learn_scales, name='scales')
        scales = scales_base + scales_delta

        nyquist_offset = scales + \
            tf.stop_gradient(tf.one_hot(0, j * q) * tf.nn.relu(1 - scales[0]))
        scales_correction = tf.concat(
            [tf.zeros(1),
             tf.nn.relu(nyquist_offset[:-1] - nyquist_offset[1:])], 0)
        self.scales = nyquist_offset + \
            tf.stop_gradient(tf.cumsum(scales_correction))

        knots_base = tf.Variable(
            tf.ones(k), trainable=learn_knots, name='knots')

        knots_sum = tf.cumsum(
            tf.clip_by_value(
                tf.expand_dims(knots_base, 0) * tf.expand_dims(self.scales, 1),
                1, self.filter_samples - k), exclusive=True, axis=1)
        self.knots = knots_sum - (k // 2) * tf.expand_dims(self.scales, 1)

        if hilbert is True:
            m = (np.cos(np.arange(k) * np.pi) * np.hamming(k)).astype(FORMAT)
            p = (np.zeros(k)).astype(FORMAT)
            self.m = tf.Variable(m, name='m', trainable=learn_filters)
            self.p = tf.Variable(p, name='p', trainable=learn_filters)

            # Boundary Conditions and centering
            mask = np.ones(k, dtype=np.float32)
            mask[0], mask[-1] = 0, 0
            m_null = self.m - tf.reduce_mean(self.m[1:-1])
            filters = real_hermite_interp(
                time_grid, self.knots, m_null * mask, self.p * mask)

            # Renorm and set filter-bank
            filters_renorm = filters / tf.reduce_max(filters, 1, keepdims=True)
            filters_fft = tf.signal.rfft(filters_renorm) # was spectral.rfft
            filters = tf.signal.ifft(
                tf.concat([filters_fft, tf.zeros_like(filters_fft)], 1))

        # Define the parameters for saving
        self.parameters = self.m, self.p, self.scales, self.knots
        return filters

    def renorm(self, parent, epsilon=1e-3):
        # Extract all shapes.
        if epsilon > 0:
            s = self.s / (tf.expand_dims(parent.s, -2) + epsilon)
            batch_size, *_, samples = s.get_shape().as_list()
            return tf.reshape(s, [batch_size, -1, samples])
        else:
            return tf.reshape(self.s, [batch_size, -1, samples])

# testing
data = tf.random.uniform((4,3,16800), dtype=tf.float32)
batch_size = 4

args = {'layers': {'j': [4, 6, 8], 'q': [8, 2, 1], 'k': 7, 'pooling_type': 'average', 'decimation': 4, 'pooling': 1024, 'learn_scales': False, 'learn_knots': False, 'learn_filters': True, 'hilbert': True}, 'eps_norm': 0.001, 'eps_log': 0.0001, 'learning': {'epochs': 3, 'rate': 0.001}, 'pca': {'n_components': 5}, 'gmm': {'gmm_type': 'natural', 'trainable': False}, 'gmm_init': {'n_components': 10, 'max_iter': 1000, 'covariance_type': 'full', 'warm_start': True}}

# Run over batches
epochs = args['learning']['epochs']
learning_rate = args['learning']['rate']
for epoch in range(epochs):

  # Gradually decrease learning rate over epochs
  if epoch == epochs // 2:
      learning_rate /= 5
  if epoch == 3 * epochs // 4:
      learning_rate /= 5

  # Calculate scattering coefficients for all batches
  scat_all = list()
  n_batches = data.shape[0] // batch_size
  for b in range(n_batches):
    layers = [Scattering(data, index=0, **args['layers'])]
    for i in range(1, 3):
      layer = Scattering(layers[-1].u, index=i, **args['layers'])
      layers.append(layer)

    # Extract parameters.
    net = [layer.parameters for layer in layers]

    # Get reconstruction losses.
    rl = tf.add_n([a.reconstruction_loss for a in layers])

    # Renormalize coefficients.
    r = list()
    for i in range(1, 3):
        r.append(layers[i].renorm(layers[i - 1], args['eps_norm']))

    # Concatenate.
    sx = tf.transpose(tf.concat(r, axis=1), [1, 0, 2])
    sx = tf.reshape(sx, [sx.get_shape().as_list()[0], -1])
    sx = tf.transpose(sx)
    sx = tf.math.log(sx + args['eps_log'])

    sx[np.isnan(sx)] = np.log(args['eps_log'])
    sx[np.isinf(sx)] = np.log(args['eps_log'])
    scat_all.append(sx)
</code>```

The issue is from the line 'sx[np.isnan(sx)] = np.log(args['eps_log'])'. The full error is shown below:

```<code>---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
Cell In [6], line 34
     31 print("sx:", sx)
     32 print("sx shape: ", sx.shape)
---> 34 sx[np.isnan(sx)] = np.log(args['eps_log'])
     35 sx[np.isinf(sx)] = np.log(args['eps_log'])
     36 scat_all.append(sx)

File c:\Python310\lib\site-packages\tensorflow\python\framework\ops.py:922, in Tensor.__array__(***failed resolving arguments***)
    920 def __array__(self, dtype=None):
    921   del dtype
--> 922   raise NotImplementedError(
    923       f"Cannot convert a symbolic tf.Tensor ({self.name}) to a numpy array."
    924       f" This error may indicate that you're trying to pass a Tensor to"
    925       f" a NumPy call, which is not supported.")

NotImplementedError: Cannot convert a symbolic tf.Tensor (Log_2:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported.
</code>```

Based on solutions on previous stackoverflow posts, I have tried to upgrade my tensorflow (2.11.0) and numpy versions (1.23.5) but that did not solve the problem. I saw some suggestions on downgrading numpy but because of other dependencies that did not work. My Python version is 3.8.16. Any suggestions on how to proceed? Thanks in advance.

Body:
I have the following code which is based on https://github.com/leonard-seydoux/scatnet. The original code was based on TF v1 and I am in the process of migrating it to TF v2. However, I am facing some issues while trying to perform a numpy operation on a tensor. I am running the code on Google Colab.

The reproducible code is as below, sorry it is quite long:

import tensorflow as tf
import numpy as np
import scipy as sp

from datetime import datetime
from sklearn.decomposition import PCA
from sklearn.mixture import GaussianMixture

HERMITE = [[1, 0, -3, 2], [0, 0, 3, -2], [0, 1, -2, 1], [0, 0, -1, 1]]
FORMAT = 'float32'

def real_hermite_interp(xi, x, m, p):
    # Hermite polynomial coefficients
    h = tf.Variable(np.array(HERMITE).astype(FORMAT), trainable=False)
    xx = tf.stack([x[:, :-1], x[:, 1:]], axis=2)

    # The concatenated coefficients are of shape (n_knots - 1, 2)
    mm = tf.stack([m[:-1], m[1:]], axis=1)
    pp = tf.stack([p[:-1], p[1:]], axis=1)

    y = tf.concat([mm, pp], axis=1)

    # Extract Hermite polynomial coefficients from y (n_knots - 1, 4)
    yh = tf.matmul(y, h)

    xi_ = tf.expand_dims(tf.expand_dims(xi, 0), 0)
    x0_ = tf.expand_dims(xx[:, :, 0], 2)
    x1_ = tf.expand_dims(xx[:, :, 1], 2)
    xn = (xi_ - x0_) / (x1_ - x0_)

    # Calculate powers of normalized interpolation vector
    mask = tf.logical_and(tf.greater_equal(xn, 0.), tf.less(xn, 1.))
    mask = tf.cast(mask, tf.float32)
    xp = tf.pow(tf.expand_dims(xn, -1), [0, 1, 2, 3])

    # Interpolate
    return tf.einsum('rf,srtf->st', yh, xp * tf.expand_dims(mask, -1))

class Scattering:
    """Learnable scattering network layer."""

    def __init__(self, x, j=None, q=None, k=None, pooling_type='average',
                 decimation=2, pooling=2, index=0, **filters_kw):
        """Scattering network layer.

        Computes the convolution modulus and scattering coefficients of the
        input signal.

        Arguments
        ---------
            x: :class:`~tensorflow.Tensor()`
                Input data of shape ``(batch_size, channels, patch_shape).
        """
        # Filter bank properties
        self.shape_input = x.get_shape().as_list()
        self.j = j = j[index] if type(j) is list else j
        self.q = q = q[index] if type(q) is list else q
        self.k = k = k[index] if type(k) is list else k

        filters = self.init_filters(j, q, k, **filters_kw)
        n_filters, kernel_size = filters.get_shape().as_list()
        filters_concat = tf.concat([tf.math.real(filters), tf.math.imag(filters)], 0)
        filters_kernel = tf.expand_dims(tf.transpose(filters_concat), 1)

        # Pad input in the time dimension before convolution with half the size
        # of filters temporal dimension (kernel_size).
        shape_fast = [np.prod(self.shape_input[:-1]), 1, self.shape_input[-1]]
        paddings = [0, 0], [0, 0], [kernel_size // 2 - 1, kernel_size // 2 + 1]
        x_reshape = tf.reshape(x, shape_fast)
        x_pad = tf.pad(x_reshape, paddings=paddings, mode='SYMMETRIC')

        # Differentiate the case of one input channel or multiple
        # which needs reshaping in order to treat them independently
        # The "NCW" format stores data as batch_shape + [in_channels, in_width]
        x_conv = tf.nn.conv1d(x_pad, filters_kernel, stride=decimation,
                              padding='VALID', data_format='NCW')
        u = tf.sqrt(tf.square(x_conv[:, :n_filters]) +
                    tf.square(x_conv[:, n_filters:]))
        self.u = tf.reshape(u, (*self.shape_input[:-1], n_filters, -1))

        pool = tf.keras.layers.AveragePooling1D

        # Pooling for the scattering coefficients
        if pooling > 1:
            pooled = pool(
                pooling // (decimation ** (index + 1)),
                pooling // (decimation ** (index + 1)),
                padding='valid', data_format='channels_first')
            pooled = pooled(u)

        self.s = tf.reshape(pooled, self.shape_input[:-1] + [j * q] + [-1])
        self.output = self.s

        tf.compat.v1.disable_eager_execution()
        inverse = tf.gradients(x_conv, x, x_conv)[0]

        self.reconstruction_loss = tf.nn.l2_loss(
            inverse - tf.stop_gradient(x)) / np.prod(self.shape_input)

    def init_filters(self, j, q, k, learn_scales=False, learn_knots=False,
                     learn_filters=True, hilbert=False):
        extra_octave = 1 if learn_scales else 0
        self.filter_samples = k * 2 ** (j + extra_octave)

        time_max = np.float32(k * 2**(j - 1 + extra_octave))
        time_grid = tf.linspace(-time_max, time_max, self.filter_samples)

        scales_base = 2**(tf.range(j * q, dtype=tf.float32) / np.float32(q))
        scales_delta = tf.Variable(
            tf.zeros(j * q), trainable=learn_scales, name='scales')
        scales = scales_base + scales_delta

        nyquist_offset = scales + \
            tf.stop_gradient(tf.one_hot(0, j * q) * tf.nn.relu(1 - scales[0]))
        scales_correction = tf.concat(
            [tf.zeros(1),
             tf.nn.relu(nyquist_offset[:-1] - nyquist_offset[1:])], 0)
        self.scales = nyquist_offset + \
            tf.stop_gradient(tf.cumsum(scales_correction))

        knots_base = tf.Variable(
            tf.ones(k), trainable=learn_knots, name='knots')

        knots_sum = tf.cumsum(
            tf.clip_by_value(
                tf.expand_dims(knots_base, 0) * tf.expand_dims(self.scales, 1),
                1, self.filter_samples - k), exclusive=True, axis=1)
        self.knots = knots_sum - (k // 2) * tf.expand_dims(self.scales, 1)

        if hilbert is True:
            m = (np.cos(np.arange(k) * np.pi) * np.hamming(k)).astype(FORMAT)
            p = (np.zeros(k)).astype(FORMAT)
            self.m = tf.Variable(m, name='m', trainable=learn_filters)
            self.p = tf.Variable(p, name='p', trainable=learn_filters)

            # Boundary Conditions and centering
            mask = np.ones(k, dtype=np.float32)
            mask[0], mask[-1] = 0, 0
            m_null = self.m - tf.reduce_mean(self.m[1:-1])
            filters = real_hermite_interp(
                time_grid, self.knots, m_null * mask, self.p * mask)

            # Renorm and set filter-bank
            filters_renorm = filters / tf.reduce_max(filters, 1, keepdims=True)
            filters_fft = tf.signal.rfft(filters_renorm) # was spectral.rfft
            filters = tf.signal.ifft(
                tf.concat([filters_fft, tf.zeros_like(filters_fft)], 1))

        # Define the parameters for saving
        self.parameters = self.m, self.p, self.scales, self.knots
        return filters

    def renorm(self, parent, epsilon=1e-3):
        # Extract all shapes.
        if epsilon > 0:
            s = self.s / (tf.expand_dims(parent.s, -2) + epsilon)
            batch_size, *_, samples = s.get_shape().as_list()
            return tf.reshape(s, [batch_size, -1, samples])
        else:
            return tf.reshape(self.s, [batch_size, -1, samples])

# testing
data = tf.random.uniform((4,3,16800), dtype=tf.float32)
batch_size = 4

args = {'layers': {'j': [4, 6, 8], 'q': [8, 2, 1], 'k': 7, 'pooling_type': 'average', 'decimation': 4, 'pooling': 1024, 'learn_scales': False, 'learn_knots': False, 'learn_filters': True, 'hilbert': True}, 'eps_norm': 0.001, 'eps_log': 0.0001, 'learning': {'epochs': 3, 'rate': 0.001}, 'pca': {'n_components': 5}, 'gmm': {'gmm_type': 'natural', 'trainable': False}, 'gmm_init': {'n_components': 10, 'max_iter': 1000, 'covariance_type': 'full', 'warm_start': True}}

# Run over batches
epochs = args['learning']['epochs']
learning_rate = args['learning']['rate']
for epoch in range(epochs):

  # Gradually decrease learning rate over epochs
  if epoch == epochs // 2:
      learning_rate /= 5
  if epoch == 3 * epochs // 4:
      learning_rate /= 5

  # Calculate scattering coefficients for all batches
  scat_all = list()
  n_batches = data.shape[0] // batch_size
  for b in range(n_batches):
    layers = [Scattering(data, index=0, **args['layers'])]
    for i in range(1, 3):
      layer = Scattering(layers[-1].u, index=i, **args['layers'])
      layers.append(layer)

    # Extract parameters.
    net = [layer.parameters for layer in layers]

    # Get reconstruction losses.
    rl = tf.add_n([a.reconstruction_loss for a in layers])

    # Renormalize coefficients.
    r = list()
    for i in range(1, 3):
        r.append(layers[i].renorm(layers[i - 1], args['eps_norm']))

    # Concatenate.
    sx = tf.transpose(tf.concat(r, axis=1), [1, 0, 2])
    sx = tf.reshape(sx, [sx.get_shape().as_list()[0], -1])
    sx = tf.transpose(sx)
    sx = tf.math.log(sx + args['eps_log'])

    sx[np.isnan(sx)] = np.log(args['eps_log'])
    sx[np.isinf(sx)] = np.log(args['eps_log'])
    scat_all.append(sx)

The issue is from the line 'sx[np.isnan(sx)] = np.log(args['eps_log'])'. The full error is shown below:

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
Cell In [6], line 34
     31 print("sx:", sx)
     32 print("sx shape: ", sx.shape)
---> 34 sx[np.isnan(sx)] = np.log(args['eps_log'])
     35 sx[np.isinf(sx)] = np.log(args['eps_log'])
     36 scat_all.append(sx)

File c:\Python310\lib\site-packages\tensorflow\python\framework\ops.py:922, in Tensor.__array__(***failed resolving arguments***)
    920 def __array__(self, dtype=None):
    921   del dtype
--> 922   raise NotImplementedError(
    923       f"Cannot convert a symbolic tf.Tensor ({self.name}) to a numpy array."
    924       f" This error may indicate that you're trying to pass a Tensor to"
    925       f" a NumPy call, which is not supported.")

NotImplementedError: Cannot convert a symbolic tf.Tensor (Log_2:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported.

Based on solutions on previous stackoverflow posts, I have tried to upgrade my tensorflow (2.11.0) and numpy versions (1.23.5) but that did not solve the problem. I saw some suggestions on downgrading numpy but because of other dependencies that did not work. My Python version is 3.8.16. Any suggestions on how to proceed? Thanks in advance.


来源: stackexchange

问:
Show secondary foreign key values in the inline django admin
models.py

```<code>class FarmerAdvisory(BaseModel):
id = models.AutoField(db_column='id', primary_key=True)
    title = models.CharField(db_column='title', max_length=200, null=True, blank=True)
    description = models.CharField(db_column='description', max_length=750, null=True, blank=True)
    farmer_id = models.ForeignKey(Farmer, on_delete=models.CASCADE, null=True, db_column='farmer_id')

class Meta:
    verbose_name = 'Farmer Advisory'
    verbose_name_plural = 'Farmer Advisories'
    managed = True
    db_table = 'farmer_advisory'

class ReplyFarmerAdvisory(BaseModel):
    reply = models.CharField(db_column='reply', max_length=750, null=True, blank=True)
    label = models.CharField(db_column='label', max_length=100, null=True, blank=True)
    farmer_advisory_id = models.ForeignKey(FarmerAdvisory, on_delete=models.CASCADE, db_column='farmer_advisory_id')
    objects = models.Manager()

class Meta:
    verbose_name = 'Reply Farmer Advisory'
    verbose_name_plural = 'Reply Farmer Advisories'
    managed = True
    db_table = 'reply_farmer_advisory'

class AdvisoryMedia(models.Model):
    id = models.AutoField(db_column='id', primary_key=True)
    farmer_advisory_id = models.ForeignKey(FarmerAdvisory, on_delete=models.CASCADE,
                                           db_column='farmer_advisory_id',
                                           null=True, blank=True)
    reply_farmer_advisory_id = models.ForeignKey(ReplyFarmerAdvisory, on_delete=models.CASCADE,
                                                 db_column='reply_farmer_advisory_id', null=True, blank=True)
    farmer_media_file = models.CharField(db_column='farmer_media_file', max_length=50, null=True, blank=True)
    is_active = models.BooleanField(db_column='is_active', null=False, default=True)
    reply_media_file = models.FileField(upload_to=content_file_name, null=True, blank=True,
                                        db_column='reply_media_file')

    class Meta:
        verbose_name = 'Advisory Media'
        verbose_name_plural = 'Advisories Media'
        managed = True
        db_table = 'advisory_media'
</code>```

admin.py

```<code>class AdvisoryMediaInline(NestedStackedInline):
    model = AdvisoryMedia
    extra = 0

class ReplyFarmerAdvisoryInline(NestedStackedInline):
    model = ReplyFarmerAdvisory
    extra = 1
    inlines = [AdvisoryMediaInline]

class FarmerAdvisoryAdmin(NestedModelAdmin):
    inlines = [ReplyFarmerAdvisoryInline]
</code>```

I want to show the fields with respect to the farmer advisory. But when i am adding fk_name = 'farmer_advisory_id' inside AdvisoryMediaInline, i am getting error stating

ValueError: fk_name 'farmer_advisory_id' is not a ForeignKey to
'farmer.ReplyFarmerAdvisory'.

But i want to show the farmer advisory related fields which is not coming by itself.

I think what is happening is the model is looking for values with respect to the to foreign key reply advisory media instead of farmer advisory media.

Please let me know if my question is understandable, if yes please guide me through it.

ContentLicense: CC BY-SA 4.0

Body:
models.py

class FarmerAdvisory(BaseModel):
id = models.AutoField(db_column='id', primary_key=True)
    title = models.CharField(db_column='title', max_length=200, null=True, blank=True)
    description = models.CharField(db_column='description', max_length=750, null=True, blank=True)
    farmer_id = models.ForeignKey(Farmer, on_delete=models.CASCADE, null=True, db_column='farmer_id')

class Meta:
    verbose_name = 'Farmer Advisory'
    verbose_name_plural = 'Farmer Advisories'
    managed = True
    db_table = 'farmer_advisory'

class ReplyFarmerAdvisory(BaseModel):
    reply = models.CharField(db_column='reply', max_length=750, null=True, blank=True)
    label = models.CharField(db_column='label', max_length=100, null=True, blank=True)
    farmer_advisory_id = models.ForeignKey(FarmerAdvisory, on_delete=models.CASCADE, db_column='farmer_advisory_id')
    objects = models.Manager()

class Meta:
    verbose_name = 'Reply Farmer Advisory'
    verbose_name_plural = 'Reply Farmer Advisories'
    managed = True
    db_table = 'reply_farmer_advisory'

class AdvisoryMedia(models.Model):
    id = models.AutoField(db_column='id', primary_key=True)
    farmer_advisory_id = models.ForeignKey(FarmerAdvisory, on_delete=models.CASCADE,
                                           db_column='farmer_advisory_id',
                                           null=True, blank=True)
    reply_farmer_advisory_id = models.ForeignKey(ReplyFarmerAdvisory, on_delete=models.CASCADE,
                                                 db_column='reply_farmer_advisory_id', null=True, blank=True)
    farmer_media_file = models.CharField(db_column='farmer_media_file', max_length=50, null=True, blank=True)
    is_active = models.BooleanField(db_column='is_active', null=False, default=True)
    reply_media_file = models.FileField(upload_to=content_file_name, null=True, blank=True,
                                        db_column='reply_media_file')

    class Meta:
        verbose_name = 'Advisory Media'
        verbose_name_plural = 'Advisories Media'
        managed = True
        db_table = 'advisory_media'

admin.py

class AdvisoryMediaInline(NestedStackedInline):
    model = AdvisoryMedia
    extra = 0

class ReplyFarmerAdvisoryInline(NestedStackedInline):
    model = ReplyFarmerAdvisory
    extra = 1
    inlines = [AdvisoryMediaInline]

class FarmerAdvisoryAdmin(NestedModelAdmin):
    inlines = [ReplyFarmerAdvisoryInline]

I want to show the fields with respect to the farmer advisory. But when i am adding fk_name = 'farmer_advisory_id' inside AdvisoryMediaInline, i am getting error stating

ValueError: fk_name 'farmer_advisory_id' is not a ForeignKey to
'farmer.ReplyFarmerAdvisory'.

But i want to show the farmer advisory related fields which is not coming by itself.

I think what is happening is the model is looking for values with respect to the to foreign key reply advisory media instead of farmer advisory media.

Please let me know if my question is understandable, if yes please guide me through it.


问:
Unable to read a font file in an install folder
I would like to modify a font file with a .fnt extension in an installation file, but it is impossible to open it with a font editor, the file seems compressed (or encrypted), how can I do to find software to read it?

I tried to open it with a text editor, and it says: file compression


问:
.Net Core OData query filter not parsing with special characters like ? &
In our code we are passing theWebAPi call using OData query filter to the API COntroller like

https://localhost:44372/v1/odata/xxx/xxxx/xxxxxx?$select=type_of_secur&$filter=type_of_secur eq 'Ability & ABS'

or

https://localhost:44372/v1/odata/xxx/xxxx/xxxxxx?$select=type_of_secur&$filter=type_of_secur in  ('Ability & ABS')

The field is a Enum and we are getting integer value from the Table column , so based on the Text given in the Filter clause it should match with the Table column value with the ENum String and it should return the result.

But if we add special caracters in the filter values we are getting the below error (ie) its not parsing.

"errors": {
"Invalid request parameters": [
"Invalid OData query '? There is an unterminated string literal at position 37 in 'type_of_secur eq 'Corporate CMOs ?'."
]
},

For that purpose i tried to implement below code using FilterBinder and replace the special characters by some how but the request is not even parsing, so what can we do. If we are using encoding characters in the filter like 'type_of_secur eq 'Corporate CMOs %26 ABS', then it is going inside the BindConstantNode method inside the CustomFilterBinder class.

public class CustomFilterBinder : FilterBinder
{
public override Expression BindConstantNode(Microsoft.OData.UriParser.ConstantNode constantNode)
{
XXXXXXXXX
YYYYYYYYY
}
}


问:
Using Callback and OnnActivityResult with ReactContextBaseJavaModule in ReactNative
I am working on ReactNative-Native bridging.

I have Module as below

```<code>public class DemoModules  extends ReactContextBaseJavaModule {
    private Callback mCallback;
    private static ReactApplicationContext reactContext = null;

    private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() {
        @Override
        public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data)
        {
                        String transactionId = data.getStringExtra("TEST_ID");
                        mCallback.invoke(transactionId);
        }
    };

    DemoModules(ReactApplicationContext context) {
        super(context);
        reactContext = context;
        reactContext.addActivityEventListener(mActivityEventListener);
    }

    @Override
    public String getName() {
        return "DemoModules";
    }
    @ReactMethod
    public void navigate(String mData,Callback mCallback) {
        Activity currentActivity = getCurrentActivity();
        Intent mIntent= new Intent(reactContext, TestActivity.class);
        mIntent.putExtra("TOKEN",mData);
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.mCallback = mCallback;
        currentActivity.startActivityForResult(mIntent,2, null);
    }

}
</code>```

I am calling navigate method as below from my ReactNative code

```<code>DemoModules.navigate("TestToken",mCallback => {
                console.log(`Created a new event with id ${mCallback}`);
              },)}
</code>```

This navigates to another activity which is TestActivity from which I need to revert with some result so In next Activity I am doing as below

```<code>val resultIntent = Intent()
                    resultIntent.putExtra("TEST_ID", "TEST")
                    setResult(Activity.RESULT_OK, resultIntent)
                    finish()
</code>```

So Summarising it
From React Native>Demo Modules>TestActivity>OnActivityResultback to DemoModules>The final result should be delivered to ReactNative code.

But I am not getting any callback in onActivityResult
What I am missing here?

Update

The main issue currently is onActivityResult is not called in DemoModules.

Body:
I am working on ReactNative-Native bridging.

I have Module as below

public class DemoModules  extends ReactContextBaseJavaModule {
    private Callback mCallback;
    private static ReactApplicationContext reactContext = null;

    private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() {
        @Override
        public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data)
        {
                        String transactionId = data.getStringExtra("TEST_ID");
                        mCallback.invoke(transactionId);
        }
    };

    DemoModules(ReactApplicationContext context) {
        super(context);
        reactContext = context;
        reactContext.addActivityEventListener(mActivityEventListener);
    }

    @Override
    public String getName() {
        return "DemoModules";
    }
    @ReactMethod
    public void navigate(String mData,Callback mCallback) {
        Activity currentActivity = getCurrentActivity();
        Intent mIntent= new Intent(reactContext, TestActivity.class);
        mIntent.putExtra("TOKEN",mData);
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.mCallback = mCallback;
        currentActivity.startActivityForResult(mIntent,2, null);
    }

}

I am calling navigate method as below from my ReactNative code

DemoModules.navigate("TestToken",mCallback => {
                console.log(`Created a new event with id ${mCallback}`);
              },)}

This navigates to another activity which is TestActivity from which I need to revert with some result so In next Activity I am doing as below

val resultIntent = Intent()
                    resultIntent.putExtra("TEST_ID", "TEST")
                    setResult(Activity.RESULT_OK, resultIntent)
                    finish()

So Summarising it
From React Native>Demo Modules>TestActivity>OnActivityResultback to DemoModules>The final result should be delivered to ReactNative code.

But I am not getting any callback in onActivityResult
What I am missing here?

Update

The main issue currently is onActivityResult is not called in DemoModules.


问:
Why is intentionally passing any exceptions bad?
I am rewriting an old application from several years ago with the goal of making the code readable and manageable by colleagues (I was away for some time and chaos ensued when others tried to maintain it). I try my hardest to follow the Zen and one of them, as we all know, is "Errors should never pass silently", but alas I do that all the time .. I know, I know... shame... ding ding... shame... ding ding...

Here is a case where I would much rather pass the exception silently with a blanket pass:

```<code>try:
    with open(self.file_path, "r") as f:
        raw = "".join([next(f) for x in range(100)])
except (IOError, UnicodeDecodeError) as e:
    # No need for logging in this case
    raw = ""
</code>```

I know the example fails if the file is less than 100 lines, but it is more to illustrate a simple point.

My reasoning:

In this case I just want a blank output (could also have added a user message, but again, just to illustrate a point)

I initially had IOError as the only exception, but trying to send in a non text-file gave the UnicodeDecodeError, which seems fair enough for some random binary file I tried to break the code with. Now I'm worried some other explicit error might pop up that I haven't tested and that will break the code.

I really just want the code to try and read a text file and if anything fails, just leave it blank...
In the olden days I would have just written this:

```<code>raw = ""
try:
    with open(self.file_path, "r") as f:
        raw = "".join([next(f) for x in range(100)])
except:
    pass
</code>```

Another example:

```<code>df = pandas.DataFrame()
try:
    if file_type == self.FileTypes.DELIMITED:
        df = pandas.read_csv(self.file_path)
    elif file_type == self.FileTypes.FIXEDWIDTH:
        df = pandas.read_fwf(self.file_path)
    elif file_type == self.FileTypes.MIKE:
        df = mikeio.read(self.file_path).to_dataframe()
except:
    pass
</code>```

A multitude of problems could occur from this. Assume most are acceptable user related "errors", probably something wrong with the file, I'd just handle an empty Pandas dataframe later.

Please don't burn me for offending the Zen...

Body:
I am rewriting an old application from several years ago with the goal of making the code readable and manageable by colleagues (I was away for some time and chaos ensued when others tried to maintain it). I try my hardest to follow the Zen and one of them, as we all know, is "Errors should never pass silently", but alas I do that all the time .. I know, I know... shame... ding ding... shame... ding ding...

Here is a case where I would much rather pass the exception silently with a blanket pass:

try:
    with open(self.file_path, "r") as f:
        raw = "".join([next(f) for x in range(100)])
except (IOError, UnicodeDecodeError) as e:
    # No need for logging in this case
    raw = ""

I know the example fails if the file is less than 100 lines, but it is more to illustrate a simple point.

My reasoning:

In this case I just want a blank output (could also have added a user message, but again, just to illustrate a point)

I initially had IOError as the only exception, but trying to send in a non text-file gave the UnicodeDecodeError, which seems fair enough for some random binary file I tried to break the code with. Now I'm worried some other explicit error might pop up that I haven't tested and that will break the code.

I really just want the code to try and read a text file and if anything fails, just leave it blank...
In the olden days I would have just written this:

raw = ""
try:
    with open(self.file_path, "r") as f:
        raw = "".join([next(f) for x in range(100)])
except:
    pass

Another example:

df = pandas.DataFrame()
try:
    if file_type == self.FileTypes.DELIMITED:
        df = pandas.read_csv(self.file_path)
    elif file_type == self.FileTypes.FIXEDWIDTH:
        df = pandas.read_fwf(self.file_path)
    elif file_type == self.FileTypes.MIKE:
        df = mikeio.read(self.file_path).to_dataframe()
except:
    pass

A multitude of problems could occur from this. Assume most are acceptable user related "errors", probably something wrong with the file, I'd just handle an empty Pandas dataframe later.

Please don't burn me for offending the Zen...


问:
Way to upload files to ESP32's spiffs file system using Python
I'm working on a self-project that I need to upload some values to esp32 from Python program.
I want to upload those values esp32 in .txt format and read those with SPIFFS library. I know a way to upload files to esp32 and esp8266 with Arduino ide and Platformio but I want these to automatically upload files when its ready. Is there any way to do that with python scripts?

In a nutshell I want a python script to upload .txt files to esp32's flash to read again with SPIFFS library inside the esp32's code. Any suggestions?


问:
Injection SQL Dynamic Stored Procedure
Recently, I started learning and I have a question.
Please advice , is the below SQL stored procedure protected from ( injection , hack ,  ... )  and it is safe to use it  ?</p>
```<code>Alter Proc spGetEmployeesByDepartment
@DepartmentName nvarchar(50)
As
Begin

Declare @sql nvarchar(max)
set @sql = ' Select E.ID , E.Name , E.Salary ,D.DepartmentName ,E2.Name as ManagerName
                From tblEmployee E
                left join tblDepartment D
                On E.DepartmaintID = D.ID
                Left join tblEmployee E2
                On E.managerID = E2.ID
                Where 1=1'

    if(@DepartmentName is not null )
        Set @sql = @sql + ' and D.DepartmentName = @DepName'

     Execute sp_executesql @sql,
     N'@DepName nvarchar(50)',
     @DepName = @DepartmentName

End

Execute spGetEmployeesByDepartment @DepartmentName ='IT'
</code>```

Title: Injection SQL Dynamic Stored Procedure

Body:
Recently, I started learning and I have a question.
Please advice , is the below SQL stored procedure protected from ( injection , hack ,  ... )  and it is safe to use it  ?

Alter Proc spGetEmployeesByDepartment
@DepartmentName nvarchar(50)
As
Begin

Declare @sql nvarchar(max)
set @sql = ' Select E.ID , E.Name , E.Salary ,D.DepartmentName ,E2.Name as ManagerName
                From tblEmployee E
                left join tblDepartment D
                On E.DepartmaintID = D.ID
                Left join tblEmployee E2
                On E.managerID = E2.ID
                Where 1=1'

    if(@DepartmentName is not null )
        Set @sql = @sql + ' and D.DepartmentName = @DepName'

     Execute sp_executesql @sql,
     N'@DepName nvarchar(50)',
     @DepName = @DepartmentName

End

Execute spGetEmployeesByDepartment @DepartmentName ='IT'


问:
Can heap memory be only accessed with a pointer? C++
So I am learning C++ and I came across this problem whether we can access heap memory without a pointer or not?

Or is there any other way to access heap memory.


问:
in python-docx, I try to read the font size, but I get None
My task is to check several docx documents for font properties, like font name, size, boldness, italic etc. I do not want to change anything, just validate if there are correct values.
I created a small test document, with text "Foo-bar" and I have updated the font name to Garamond from the default, but have not changed the font size.
Now I am able to read the font name from the document, but when I try to read the font size, I get None.

```<code>from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH

def check_para_props(doc, paralist, ffamily, fsize, paligin):
    pargs = doc.paragraphs
    for i in paralist:
        para = pargs[i]
        print(i, para.text)
        for run in para.runs:
            print(run.font.name, run.font.size)

worddoc = Document('Foo-bar.docx')
# pass the document, the paragraphs list which should be tested
# and the expected values
check_para_props(worddoc, (0, ), 'Garamond', 11, WD_ALIGN_PARAGRAPH.JUSTIFY)
</code>```

Output:

```<code>0 Foo-bar
Garamond **None**
</code>```

I assume, there is some default values in docx for the unchanged values, like font-size, but have not found, how could I read them from the document. In python-docx documentation I have not found an API call for it: https://python-docx.readthedocs.io/en/latest/index.html

I have checked the style section, which is readable, but I don't think, this is set there.
Checked API documentation: https://python-docx.readthedocs.io/en/latest/index.html
Word is able to export document in XML format, but still I have doubts how to read the font size.

In that article, I read the about the inheritance of the properties: https://www.toptal.com/xml/an-informal-introduction-to-docx
It tells, to get the end result of a character’s properties you should:

```<code>Use default run/paragraph properties
Append run/paragraph style properties
Append local run/paragraph properties
Append result run properties over paragraph properties
</code>```

Ok, still how may I read the default properties using python-docx?

Please advise!
Thank you in advance!

Body:
My task is to check several docx documents for font properties, like font name, size, boldness, italic etc. I do not want to change anything, just validate if there are correct values.
I created a small test document, with text "Foo-bar" and I have updated the font name to Garamond from the default, but have not changed the font size.
Now I am able to read the font name from the document, but when I try to read the font size, I get None.

from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH

def check_para_props(doc, paralist, ffamily, fsize, paligin):
    pargs = doc.paragraphs
    for i in paralist:
        para = pargs[i]
        print(i, para.text)
        for run in para.runs:
            print(run.font.name, run.font.size)

worddoc = Document('Foo-bar.docx')
# pass the document, the paragraphs list which should be tested
# and the expected values
check_para_props(worddoc, (0, ), 'Garamond', 11, WD_ALIGN_PARAGRAPH.JUSTIFY)

Output:

0 Foo-bar
Garamond **None**

I assume, there is some default values in docx for the unchanged values, like font-size, but have not found, how could I read them from the document. In python-docx documentation I have not found an API call for it: https://python-docx.readthedocs.io/en/latest/index.html

I have checked the style section, which is readable, but I don't think, this is set there.
Checked API documentation: https://python-docx.readthedocs.io/en/latest/index.html
Word is able to export document in XML format, but still I have doubts how to read the font size.

In that article, I read the about the inheritance of the properties: https://www.toptal.com/xml/an-informal-introduction-to-docx
It tells, to get the end result of a character’s properties you should:

Use default run/paragraph properties
Append run/paragraph style properties
Append local run/paragraph properties
Append result run properties over paragraph properties

Ok, still how may I read the default properties using python-docx?

Please advise!
Thank you in advance!


问:
Unable to open JenkinsCI connector in MS Teams
In our project, we were trying to setup 'JenkinsCI Connector' in MS Teams but in vain. Though, we are able to add it in one of channel in Teams but unfortunately NOT able to open it. Any suggestion??

Thanks in advance..!!


问:
Do we need IAP for marketplace iOS app to list a business in the app?
We are developing a marketplace iOS app. For users its a free to use app. For businesses we have a subscription to list their profile in the users app. Any trasaction that happens betweens users and businesses will happen outside of the app. In such a scenario do we need to have the subscription for businesses as IAP? Or is it ok to use a third party payment processor.</p>


问:
How to style this tree table? [reactJS]
I'm relying on the documentation below to create a comparative report for the company I work for, however, I'm not managing to style it, such as putting the main lines in bold. Can you help me?

Here is the documentation used:https://codesandbox.io/s/agitated-goodall-lx1ib?file=/src/index.js

I tried to use "styled-components" but without success.

Title: How to style this tree table? [reactJS]


问:
Powershell script to execute sql files in a directory
I've got a directory with a bunch of SQL files ( queries ) that I'd like to execute.
What's the best way to go about it?

I can do it static but that seems a little inefficient if other files are added.

```<code>invoke-sqlcmd -inputfile "c:\temp\adduserl.sql" -serverinstance "10.129.221.91" -database "GoodOne" -username "user" -password "pw1"

invoke-sqlcmd -inputfile "c:\temp\addaddressesl.sql" -serverinstance "10.129.221.91" -database "GoodOne" -username "user" -password "pw1"
</code>```

Perhaps a for loop of some sorts?

Body:
I've got a directory with a bunch of SQL files ( queries ) that I'd like to execute.
What's the best way to go about it?

I can do it static but that seems a little inefficient if other files are added.

invoke-sqlcmd -inputfile "c:\temp\adduserl.sql" -serverinstance "10.129.221.91" -database "GoodOne" -username "user" -password "pw1"

invoke-sqlcmd -inputfile "c:\temp\addaddressesl.sql" -serverinstance "10.129.221.91" -database "GoodOne" -username "user" -password "pw1"

Perhaps a for loop of some sorts?


问:
Flushing file's with webkit's filesystem API with Safari
I am trying to use filesystem api to create permanent files, write and read data from them.
Although I succeeded creating the file and writing data to it, after calling flush() the file becomes empty (and it's size is 0).
The files that I created exist and I can still see them in a different running of safari, but the data is lost and the file's are all 0 sized.
Even if I try to read the file just after writing to it and flushing, the data is lost and it's size returns to 0.
Does anybody know what I am doing wrong?

I tried running this:

```<code>console.log("Starting");
async function files() {

// Set a Message
const message = "Thank you for reading this.";
const fileName = "Draft.txt";

// Get handle to draft file
const root = await navigator.storage.getDirectory();
const draftHandle = await root.getFileHandle(fileName, { create: true });
console.log("File Name: ", fileName);
// Get sync access handle
const accessHandle = await draftHandle.createSyncAccessHandle();

// Get size of the file.
const fileSize = await accessHandle.getSize();
console.log("File Size: ", fileSize); // getting 0 here
// Read file content to a buffer.
const buffer = new DataView(new ArrayBuffer(fileSize));
const readBuffer = accessHandle.read(buffer, { at: 0 });
console.log("Read: ", readBuffer); // getting 0 here because the file was just created

// Write the message to the file.
const encoder = new TextEncoder();
const encodedMessage = encoder.encode(message);
const writeBuffer = accessHandle.write(encodedMessage, { at: readBuffer });
console.log("Write: ", writeBuffer); // writing 27 bytes here, succeeding

// Persist changes to disk.
accessHandle.flush();

// Always close FileSystemSyncAccessHandle if done.
accessHandle.close();
console.log("Closed file ");

// Find files under root/ and print their details.
for await (const handle of root.values()) {
    console.log('Item in root: ', handle.name);
    if (handle.kind == "file") {
        let f = await handle.getFile();
        console.log("File Details: ", f); // I can see here the file I created now and earlier created files, but all of them are 0 sized
        }
    }
}

files();
</code>```

Body:
I am trying to use filesystem api to create permanent files, write and read data from them.
Although I succeeded creating the file and writing data to it, after calling flush() the file becomes empty (and it's size is 0).
The files that I created exist and I can still see them in a different running of safari, but the data is lost and the file's are all 0 sized.
Even if I try to read the file just after writing to it and flushing, the data is lost and it's size returns to 0.
Does anybody know what I am doing wrong?

I tried running this:

console.log("Starting");
async function files() {

// Set a Message
const message = "Thank you for reading this.";
const fileName = "Draft.txt";

// Get handle to draft file
const root = await navigator.storage.getDirectory();
const draftHandle = await root.getFileHandle(fileName, { create: true });
console.log("File Name: ", fileName);
// Get sync access handle
const accessHandle = await draftHandle.createSyncAccessHandle();

// Get size of the file.
const fileSize = await accessHandle.getSize();
console.log("File Size: ", fileSize); // getting 0 here
// Read file content to a buffer.
const buffer = new DataView(new ArrayBuffer(fileSize));
const readBuffer = accessHandle.read(buffer, { at: 0 });
console.log("Read: ", readBuffer); // getting 0 here because the file was just created

// Write the message to the file.
const encoder = new TextEncoder();
const encodedMessage = encoder.encode(message);
const writeBuffer = accessHandle.write(encodedMessage, { at: readBuffer });
console.log("Write: ", writeBuffer); // writing 27 bytes here, succeeding

// Persist changes to disk.
accessHandle.flush();

// Always close FileSystemSyncAccessHandle if done.
accessHandle.close();
console.log("Closed file ");

// Find files under root/ and print their details.
for await (const handle of root.values()) {
    console.log('Item in root: ', handle.name);
    if (handle.kind == "file") {
        let f = await handle.getFile();
        console.log("File Details: ", f); // I can see here the file I created now and earlier created files, but all of them are 0 sized
        }
    }
}

files();


问:
Separate queries into a different file
At the moment I have a react app that has multiple pages making different queries from Firebase and more specifically Firestore. These queries are at the top of the file with the code for the component in the return statement.

How do I separate the queries into a separate file so that the query can be called across multiple components without repeating the code for the query? I have a provided a sample page from my website, I would like to separate the query to get the createdBy property and put it into a function called getTicketCreatedBy for example that I can call.

I'm new to React so I'm just trying to understand how best to structure files in React.

Thanks

import React, { useState, useEffect, Fragment } from "react";

import { db } from "../../firebase";
import { updateDoc, doc, getDoc, onSnapshot } from "firebase/firestore";

import {
  ArrowPathIcon,
  CalendarIcon,
  ExclamationCircleIcon,
  Bars3Icon,
} from "@heroicons/react/24/outline";
import SelectFieldStandard from "../inputs/SelectFieldStandard";

export default function Details({ currentTicketData, currentTicketRef }) {
  const updateTicketState = async () => {
    await updateDoc(getUser, {
      state: 5,
    });
  };

  //Get CreatedBy property from the Users collection

  const [createdByData, setCreatedByData] = useState("");

  const createdByDocRef = doc(db, "Users", currentTicketData.createdBy);

  useEffect(() => {
    onSnapshot(createdByDocRef, (doc) => {
      const newData = doc.data();
      setCreatedByData(newData);
    });
  }, []);

  return (

            Details

              Created By

              <Bars3Icon
                className="flex-shrink-0 mr-1.5 h-5 w-5"
                aria-hidden="true"
              />
              {createdByData.firstName} {createdByData.lastName}

  );
}
```

I can't seem to find any information online for how to separate Firebase queries.

Title: Separate queries into a different file

Body:
At the moment I have a react app that has multiple pages making different queries from Firebase and more specifically Firestore. These queries are at the top of the file with the code for the component in the return statement.

How do I separate the queries into a separate file so that the query can be called across multiple components without repeating the code for the query? I have a provided a sample page from my website, I would like to separate the query to get the createdBy property and put it into a function called getTicketCreatedBy for example that I can call.

I'm new to React so I'm just trying to understand how best to structure files in React.

Thanks

import React, { useState, useEffect, Fragment } from "react";

import { db } from "../../firebase";
import { updateDoc, doc, getDoc, onSnapshot } from "firebase/firestore";

import {
  ArrowPathIcon,
  CalendarIcon,
  ExclamationCircleIcon,
  Bars3Icon,
} from "@heroicons/react/24/outline";
import SelectFieldStandard from "../inputs/SelectFieldStandard";

export default function Details({ currentTicketData, currentTicketRef }) {
  const updateTicketState = async () => {
    await updateDoc(getUser, {
      state: 5,
    });
  };

  //Get CreatedBy property from the Users collection

  const [createdByData, setCreatedByData] = useState("");

  const createdByDocRef = doc(db, "Users", currentTicketData.createdBy);

  useEffect(() => {
    onSnapshot(createdByDocRef, (doc) => {
      const newData = doc.data();
      setCreatedByData(newData);
    });
  }, []);

  return (

            Details

              Created By

              <Bars3Icon
                className="flex-shrink-0 mr-1.5 h-5 w-5"
                aria-hidden="true"
              />
              {createdByData.firstName} {createdByData.lastName}

  );
}

I can't seem to find any information online for how to separate Firebase queries.


问:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[text()='Error: Password doesn''t match.']' is not a valid XPath expression
Code:
String string = "Error: Password doesn't match."; String errXpath = "//div[text()='" + string + "']"; String err = driver.findElement(By.xpath(errXpath)).getText();

Error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[text()='Error: Password doesn't match.']' is not a valid XPath expression.

Tried to change string to

"Error: Password doesn\'t match."
but it is not working.


问:
403 Access denied, forbidden, when accessing google storage from google kubernetes engine with spring-cloud-gcp
I am using the spring-cloud-gcp library running in the google kubernetes engine for the access of google cloud storage buckets. It should be using the default credentials of the kubernetes engine service account.
The access of creation of buckets or files inside existing buckets fails with 403 Access denied. forbidden.

The storage access works fine when run locally with a different user account, by specific access credentials pointed to by spring.cloud.gcp.credentials.location. Both compute engine as well as the user account have editor permissions.

As the documentation explains, the spring cloud GCP starter should auto-configure the credentials - by default the compute engine service account should be used.

Workload-identity of the cluster does not change anything.

So how could I debug this?
In the process I would also like to verify, which account is used for the storage access.


问:
How to handle a timestamp-column with spring boot?
I have the following Spring Boot Apllication to fetch data from a postgresql database. In this database there is a column with the datatype "Timestamp Without Timezone".

Entity:

```<code>private Timestamp timestamp;
</code>```

Controller:

```<code>@GetMapping("url")
    public ResponseEntity<Messwerte> getMaxId(){
      return new ResponseEntity<>(this.messwertRepository.getMaxID(), HttpStatus.OK);
    }
</code>```

Repository:

```<code>@Repository
public interface MesswertRepository extends JpaRepository<Messwerte, Long> {}
</code>```

When I start the apllication it retturns the following timestamp: "timestamp":"1970-01-01T12:03:29.689+00:00"

Body:
I have the following Spring Boot Apllication to fetch data from a postgresql database. In this database there is a column with the datatype "Timestamp Without Timezone".

Entity:

private Timestamp timestamp;

Controller:

@GetMapping("url")
    public ResponseEntity<Messwerte> getMaxId(){
      return new ResponseEntity<>(this.messwertRepository.getMaxID(), HttpStatus.OK);
    }

Repository:

@Repository
public interface MesswertRepository extends JpaRepository<Messwerte, Long> {}

When I start the apllication it retturns the following timestamp: "timestamp":"1970-01-01T12:03:29.689+00:00"


问:
Apache Batik - How to get all svg Elements in a given radius arround a point?
Actually i am using this not very performant code to get all svg elements arround a mouse click. Is there a better and more correct solution ?</p>
```<code>private List<GraphicsNode> graphicsNodesAtPosition(Point2D point) {
            List<GraphicsNode> graphicsNodes = new ArrayList<>();
            Point2D newPoint = new Point2D.Double();
            canvas.getRenderingTransform().inverseTransform(point, newPoint);

            for (int x = 0; x < 10; x++) {
                for (int y = 0; y < 10; y++) {
                        graphicsNodes.add(graphicsNode.nodeHitAt(
new Point2D.Double(adjustedPoint.getX() + x, adjustedPoint.getY() + y)));

                        graphicsNodes.add(graphicsNode.nodeHitAt(
new Point2D.Double(adjustedPoint.getX() + x, adjustedPoint.getY() - y)));

                        graphicsNodes.add(graphicsNode.nodeHitAt(
new Point2D.Double(adjustedPoint.getX() - x, adjustedPoint.getY() + y)));

                        graphicsNodes.add(graphicsNode.nodeHitAt(
new Point2D.Double(adjustedPoint.getX() - x, adjustedPoint.getY() - y)));
                }
            }

            return graphicsNodes;
        }
</code>```

Body:
Actually i am using this not very performant code to get all svg elements arround a mouse click. Is there a better and more correct solution ?

private List<GraphicsNode> graphicsNodesAtPosition(Point2D point) {
            List<GraphicsNode> graphicsNodes = new ArrayList<>();
            Point2D newPoint = new Point2D.Double();
            canvas.getRenderingTransform().inverseTransform(point, newPoint);

            for (int x = 0; x < 10; x++) {
                for (int y = 0; y < 10; y++) {
                        graphicsNodes.add(graphicsNode.nodeHitAt(
new Point2D.Double(adjustedPoint.getX() + x, adjustedPoint.getY() + y)));

                        graphicsNodes.add(graphicsNode.nodeHitAt(
new Point2D.Double(adjustedPoint.getX() + x, adjustedPoint.getY() - y)));

                        graphicsNodes.add(graphicsNode.nodeHitAt(
new Point2D.Double(adjustedPoint.getX() - x, adjustedPoint.getY() + y)));

                        graphicsNodes.add(graphicsNode.nodeHitAt(
new Point2D.Double(adjustedPoint.getX() - x, adjustedPoint.getY() - y)));
                }
            }

            return graphicsNodes;
        }


问:
Not getting updates from an array of SwiftUI Views In UIKit
I have an array of views that I am adding to UIKit with UIHostingController. Tapping on a view it increases its count and I want to register this back in my UIKit environment using publisher. However the change does not come through to the UIKit environment.

Any help would be appreciated.

UIKit ViewController to import the SwiftUI

```<code>import UIKit
import SwiftUI
import Combine

class ViewController: UIViewController {
    var cancelablesSet:[AnyCancellable] = []
    var tiles:[ Tile] = []

    override func viewDidLoad() {
        super.viewDidLoad()
                        for i in 0...3{
                    let aTile = Tile(counter: 0, index: i )
                    tiles.append( aTile )
                }
                // Do any additional setup after loading the view.

                let testVC = UIHostingController(rootView: TilesView(tiles: tiles ) )

// function to add view controller as Child
                addChildViewContorller( testVC )

                testVC.view.translatesAutoresizingMaskIntoConstraints = false

                    NSLayoutConstraint.activate([
                        testVC.view.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor ),
                        testVC.view.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor ),
                        testVC.view.topAnchor.constraint(equalTo: view.topAnchor ),
                        testVC.view.bottomAnchor.constraint(equalTo: view.bottomAnchor ),
                    ])

        tiles.publisher.sink(receiveValue: { tile in
            print( "updated tile at index:\(tile.index) to value:\( tile.counter )")
        }).store(in: &cancelablesSet )
    }
}
</code>```

The SwiftUI View that takes an array of SwiftUI Views

```<code>class Tile:Identifiable, ObservableObject{
    enum dicValues{
        case index, counter
    }
    var id:UUID = UUID()
    var index:Int
    @Published var counter:Int
    var isSelected:Bool = false

    init(counter: Int = 0, index:Int = -1) {
        self.counter = counter
        self.index = index

    }
}
struct TilesView: View {
    var tiles:[Tile]
    var body: some View {
        ForEach( tiles.indices, id:\.self ){ index in
            TileView( tile: tiles[ index ] )
        }
    }
}

struct TileView: View {
    @ObservedObject var tile:Tile
    var body: some View {
        ZStack{
            RoundedRectangle(cornerRadius: 10)
                .fill( tile.isSelected ? Color.red : Color.yellow )
                .frame(height: 50)
                .padding( .horizontal )
                .padding( .vertical, 5 )
            Text( "\(tile.counter)")
                .font(.system(size: 20, weight: .bold ))
        }
        .onTapGesture {
            tile.counter += 1
            tile.isSelected.toggle()
        }
     }
}
</code>```

Here is the full project code sample if anyone is interested:
Code sample

Body:
I have an array of views that I am adding to UIKit with UIHostingController. Tapping on a view it increases its count and I want to register this back in my UIKit environment using publisher. However the change does not come through to the UIKit environment.

Any help would be appreciated.

UIKit ViewController to import the SwiftUI

import UIKit
import SwiftUI
import Combine

class ViewController: UIViewController {
    var cancelablesSet:[AnyCancellable] = []
    var tiles:[ Tile] = []

    override func viewDidLoad() {
        super.viewDidLoad()
                        for i in 0...3{
                    let aTile = Tile(counter: 0, index: i )
                    tiles.append( aTile )
                }
                // Do any additional setup after loading the view.

                let testVC = UIHostingController(rootView: TilesView(tiles: tiles ) )

// function to add view controller as Child
                addChildViewContorller( testVC )

                testVC.view.translatesAutoresizingMaskIntoConstraints = false

                    NSLayoutConstraint.activate([
                        testVC.view.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor ),
                        testVC.view.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor ),
                        testVC.view.topAnchor.constraint(equalTo: view.topAnchor ),
                        testVC.view.bottomAnchor.constraint(equalTo: view.bottomAnchor ),
                    ])

        tiles.publisher.sink(receiveValue: { tile in
            print( "updated tile at index:\(tile.index) to value:\( tile.counter )")
        }).store(in: &cancelablesSet )
    }
}

The SwiftUI View that takes an array of SwiftUI Views

class Tile:Identifiable, ObservableObject{
    enum dicValues{
        case index, counter
    }
    var id:UUID = UUID()
    var index:Int
    @Published var counter:Int
    var isSelected:Bool = false

    init(counter: Int = 0, index:Int = -1) {
        self.counter = counter
        self.index = index

    }
}
struct TilesView: View {
    var tiles:[Tile]
    var body: some View {
        ForEach( tiles.indices, id:\.self ){ index in
            TileView( tile: tiles[ index ] )
        }
    }
}

struct TileView: View {
    @ObservedObject var tile:Tile
    var body: some View {
        ZStack{
            RoundedRectangle(cornerRadius: 10)
                .fill( tile.isSelected ? Color.red : Color.yellow )
                .frame(height: 50)
                .padding( .horizontal )
                .padding( .vertical, 5 )
            Text( "\(tile.counter)")
                .font(.system(size: 20, weight: .bold ))
        }
        .onTapGesture {
            tile.counter += 1
            tile.isSelected.toggle()
        }
     }
}

Here is the full project code sample if anyone is interested:
Code sample


问:
Why is my Web api Application_Error not catching Exceptions?
I am using a .net framework web-api project and in my global.asax.cs file i have this:

```<code>    protected void Application_Error(object sender, EventArgs e)
    {
        var ctx = HttpContext.Current;

        var exception = ctx.Server.GetLastError(); // get exeption
        // log exeption

        Context.Response.ContentType = "application/json";
        Context.Response.StatusCode = 500;

        // if debug return detailed exeption else generic like below

        Context.Response.Write("Internal server error");
    }
</code>```

I enabeled custom erros in webconfig:

```<code><customErrors mode="On"  />
</code>```

And in a controller endpoint I throw an exception:

```<code> throw new Exception("test");
</code>```

Result I see in the browser is :

```<code>{
"Message": "An error has occurred."
}
</code>```

Result I would have expected is:
what is written in the Application_Error method:

```<code>Context.Response.Write("Internal server error");
</code>```

Something is not registering here, what have I missed?

Body:
I am using a .net framework web-api project and in my global.asax.cs file i have this:

    protected void Application_Error(object sender, EventArgs e)
    {
        var ctx = HttpContext.Current;

        var exception = ctx.Server.GetLastError(); // get exeption
        // log exeption

        Context.Response.ContentType = "application/json";
        Context.Response.StatusCode = 500;

        // if debug return detailed exeption else generic like below

        Context.Response.Write("Internal server error");
    }

I enabeled custom erros in webconfig:

<customErrors mode="On"  />

And in a controller endpoint I throw an exception:

 throw new Exception("test");

Result I see in the browser is :

{
"Message": "An error has occurred."
}

Result I would have expected is:
what is written in the Application_Error method:

Context.Response.Write("Internal server error");

Something is not registering here, what have I missed?


问:
Why .proto file only showing line one as " syntax = "proto3" in vs code version 1.70.2
I was following an online class on grpc! And the tutorer drop a github link, consisting of a .proto files, of what he taught us but when I pull the github everything looks ok but when I try to open with vs code version 1.70.2 it'll open but only showing the first line of the .proto file.
Syntax = "proto3"

I try re-install the grpc plugins from inside vs code because I use gitbash to set up my grpc and as well to pull the repo.
But the re-intall didn't work I don't know what to do again


问:
An unhandled exception occurred: Cannot read properties of undefined (reading 'split')
Facing some trouble in solving this problem - 'An unhandled exception occurred: Cannot read properties of undefined (reading 'split')'

I have tried to check the typeof string using the if condition. And if the condition satisfies, then I'm applying the split method. But that didn't work.

setInterval function is where I have tried to solve the problem by applying if condition and logging the result in the console.

The split method appears in several files. Unable to find out which file the error is pointing out to. Below are some of the files where the method occurs.

Will be grateful for any help!

Thanks.

```<code>    setInterval(function () {
      const random = Math.round(Math.random() * 10);

      const checkvalues = updatingChart.text();

      if(typeof checkvalues === 'string') {
      let values = checkvalues.split(',');
      console.log(values);

      values.shift();
      // values.push(random);

      updatingChart
        .text(values.join(','))
        .change();
      }
    }, 1000);
...
}
</code>```
```<code> TopMarket() {
    this.TopMarketGraph = {
      title: {
        left: 'center',
        top: 'top'
      },
      tooltip: {
        trigger: 'item',
        formatter: function (params) {
          let value = (params.value + '').split('.');
          return params.seriesName + '<br/>' + params.name + ' : ' + value;
        }
      },
....
}
</code>```
```<code> convertMonthly(data) {
    if (data.length > 0) {
      if (data[0].length == 7) {
        let monthInRow = [];
        data.forEach((element) => {
          var dates = element.split('-')[1];
          monthInRow.push(this.mapMonth[dates]);
        });
        return monthInRow;
      }
      else {
        return data;
      }
    }
  }

sendLanguages() {
    let params = {};
    let userinfo = this.localStorageService.fetchStoredUserClient();
    this.params['client_id'] = userinfo['client_id'];
    if (this.optionsModel) {
      if (this.optionsModel.length > 0) {
        this.emptyDropdownError = false;
        if (this.MonthlySelected) {
          if (this.dateSelected['to_date'].split("-")[2] = this.dateSelected['from_date'].split("-")[2] == 6) {
            params = this.dateSelected;
          } else {
            params['from_date'] = this.dateSelected['to_date'].substring(0, this.params['from_date'].lastIndexOf("-"));
            params['to_date'] = this.dateSelected['to_date'].substring(0, this.params['to_date'].lastIndexOf("-"));
          }
        } else {
          params = this.dateSelected;
        }
</code>```

Body:
Facing some trouble in solving this problem - 'An unhandled exception occurred: Cannot read properties of undefined (reading 'split')'

I have tried to check the typeof string using the if condition. And if the condition satisfies, then I'm applying the split method. But that didn't work.

setInterval function is where I have tried to solve the problem by applying if condition and logging the result in the console.

The split method appears in several files. Unable to find out which file the error is pointing out to. Below are some of the files where the method occurs.

Will be grateful for any help!

Thanks.

    setInterval(function () {
      const random = Math.round(Math.random() * 10);

      const checkvalues = updatingChart.text();

      if(typeof checkvalues === 'string') {
      let values = checkvalues.split(',');
      console.log(values);

      values.shift();
      // values.push(random);

      updatingChart
        .text(values.join(','))
        .change();
      }
    }, 1000);
...
}

 TopMarket() {
    this.TopMarketGraph = {
      title: {
        left: 'center',
        top: 'top'
      },
      tooltip: {
        trigger: 'item',
        formatter: function (params) {
          let value = (params.value + '').split('.');
          return params.seriesName + '
' + params.name + ' : ' + value;
        }
      },
....
}

 convertMonthly(data) {
    if (data.length > 0) {
      if (data[0].length == 7) {
        let monthInRow = [];
        data.forEach((element) => {
          var dates = element.split('-')[1];
          monthInRow.push(this.mapMonth[dates]);
        });
        return monthInRow;
      }
      else {
        return data;
      }
    }
  }

sendLanguages() {
    let params = {};
    let userinfo = this.localStorageService.fetchStoredUserClient();
    this.params['client_id'] = userinfo['client_id'];
    if (this.optionsModel) {
      if (this.optionsModel.length > 0) {
        this.emptyDropdownError = false;
        if (this.MonthlySelected) {
          if (this.dateSelected['to_date'].split("-")[2] = this.dateSelected['from_date'].split("-")[2] == 6) {
            params = this.dateSelected;
          } else {
            params['from_date'] = this.dateSelected['to_date'].substring(0, this.params['from_date'].lastIndexOf("-"));
            params['to_date'] = this.dateSelected['to_date'].substring(0, this.params['to_date'].lastIndexOf("-"));
          }
        } else {
          params = this.dateSelected;
        }


问:
Does Java support array size more than 2^31 -1?
I was trying to create a int[] type array in Java with a user input based length. Below is the code for the same :

```<code>int[] change = new int[max+1];
</code>```

When the value of max = 2147483647, the compiler shows the below error code :
type here

```<code>java.lang.NegativeArraySizeException: -2147483648
</code>```

I know that the upper limit for int in Java is -2147483648 to 2147483647 (-231 to 231-1), but why is the size of array going negative with a addition (is it looping back to the lower limit?).
The problem remains the same with long[] type array. So, does Java not support array size above 231-1 ?

Body:
I was trying to create a int[] type array in Java with a user input based length. Below is the code for the same :

int[] change = new int[max+1];

When the value of max = 2147483647, the compiler shows the below error code :
type here

java.lang.NegativeArraySizeException: -2147483648

I know that the upper limit for int in Java is -2147483648 to 2147483647 (-231 to 231-1), but why is the size of array going negative with a addition (is it looping back to the lower limit?).
The problem remains the same with long[] type array. So, does Java not support array size above 231-1 ?


问:
Next.js - Number of page-count reduced in Google Analytics after moving to Next.js
I recently moved my website from PHP to Next.js, keeping all URLs as same.
I noticed that number of page counts in google analytics reduced by a big margin.

Next.js, since it is built over SPA thing.
When I click on a new page from an existing page (Example: /collection/), it looks like the whole page does not refresh, only the main content changes. I can see the same ads (without refresh) from AdSense.
Earlier, in PHP, the whole page refreshes.

My suspicion is that the new page does not count as a new page count by google analytics.

My questions:

How can I make the entire page refreshes on the new page open?

If this is the optimization done by Next.js, how can I make google analytics aware that it's a new page?

Am I using google-analytics in the wrong way (Code attached below)?

```<code>return (
    <SSRProvider>
      <SessionProvider session={pageProps.session}>

        <Script
          src="https://www.googletagmanager.com/gtag/js?id=<my-id>"
          strategy="lazyOnload"
        />
        <Script id="google-analytics" strategy="lazyOnload">
          {`
          window.dataLayer = window.dataLayer || [];
          function gtag(){window.dataLayer.push(arguments);}
          gtag('js', new Date());

          gtag('config', '<my-id>');
        `}
        </Script>

        <Script
          id="Adsense-id"
          data-ad-client={process.env.ADSENSE_ID}
          async
          strategy="lazyOnload"
          onError={(e) => { console.error('adsense failed to load', e) }}
          src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"
          crossorigin="anonymous"
        />

        <Component {...pageProps} />
      </SessionProvider>
    </SSRProvider >
  );
</code>```

Please suggest.

Body:
I recently moved my website from PHP to Next.js, keeping all URLs as same.
I noticed that number of page counts in google analytics reduced by a big margin.

Next.js, since it is built over SPA thing.
When I click on a new page from an existing page (Example: /collection/), it looks like the whole page does not refresh, only the main content changes. I can see the same ads (without refresh) from AdSense.
Earlier, in PHP, the whole page refreshes.

My suspicion is that the new page does not count as a new page count by google analytics.

My questions:

How can I make the entire page refreshes on the new page open?

If this is the optimization done by Next.js, how can I make google analytics aware that it's a new page?

Am I using google-analytics in the wrong way (Code attached below)?

return (
    <SSRProvider>
      <SessionProvider session={pageProps.session}>


问:
Shouldn't table.undo() clear any cell.isEdited() flag when the undo "undoes" a cell edit?
In Tabulator - when using the history module and - the function undo(): It seems that the cell flag isEdited() doesn't get cleared when the undo undoes a cell edit.

I want to mark all edited cells in a table through CSS, but after I did an undo() on the table I noticed that the edit markings were not removed - even though the cells got "undone" as they should.

To mark edited cells, I use a function that look like this:

```<code>const setEditedCellsVisibility = () => {

    document.querySelectorAll(".tabulator-cell").forEach( element => {
        element.classList.remove('edited');
    })

    //(The "Table" is set up elsewhere, but is a normal tabulator table)
    Table.getEditedCells().forEach( cell => {
        cell.getElement().classList.add('edited')
    })
}
</code>```

Do I need to "manually" clear the flag for cell.isEdited() after each Table.undo() call?

Body:
In Tabulator - when using the history module and - the function undo(): It seems that the cell flag isEdited() doesn't get cleared when the undo undoes a cell edit.

I want to mark all edited cells in a table through CSS, but after I did an undo() on the table I noticed that the edit markings were not removed - even though the cells got "undone" as they should.

To mark edited cells, I use a function that look like this:

const setEditedCellsVisibility = () => {

    document.querySelectorAll(".tabulator-cell").forEach( element => {
        element.classList.remove('edited');
    })

    //(The "Table" is set up elsewhere, but is a normal tabulator table)
    Table.getEditedCells().forEach( cell => {
        cell.getElement().classList.add('edited')
    })
}

Do I need to "manually" clear the flag for cell.isEdited() after each Table.undo() call?


问:
How can I improve the accuracy of my pytorch neural network for classification of tabular data?
As a newbie in 'pytorch', I am building a neural network for classification of faulty water pumps in tazania for this competition I am also using ax-platform for hyperparameter tuning.

Yes methods such as gradient boosting classifiers and random forest probably works well or even better than neural network classifier for this tabular data problem but I want to practice using pytorch.

The problem is that when I am doing optimise from ax-platform I am  getting accuracy score not more than 54% and I wish to improve that. It jumps around certain number only as shown here:

I tried to do some debugging in my evaluate function by printing out predicted and labels

```<code>def evaluate(net, testloader):
        correct = 0
        total = 0
        with torch.no_grad():
            for data in testloader:
                inputs, labels = map(Variable,data)
                outputs = net(inputs)
                _,predicted = torch.max(outputs,1)

                print("row in testloader")
                print("predicted: ")
                print(predicted)
                print("labels: ")
                print(labels)
                total += labels.size(0)
                correct += (predicted == labels).sum().item()
                # print(str(correct)+"/"+str(total))
        print('Accuracy of the network: %d %%' % (
            100 * correct / total))
        return 100 * correct / total
</code>```

and net(inputs) is giving a tensor of all ones. It should have been a tensors with varied classifications/numbers of [0,1,2] .

I have tried:

using SGD and ADAM

1,2 and 3 hidden layers

different epoches

'Dropout' with different dropout probabilites

disabling biases

having different learning rates

different number of neurons

different total trails

different betas and eps for ADAM algorithm

'log_scale = True' in parameters optimsation

sci-kit learn StandardScaler on train_values and test_values

How can I break the 54% accuracy barrier here? What are the gaps in my knowledges? What bugs in the code that causes this?

Here is the code:

```<code>import numpy as np
import pandas as pd
import torch
import torchvision
import torchvision.transforms as transforms
from ax import optimize
from ax.plot.contour import plot_contour
from ax.plot.trace import optimization_trace_single_method
from ax.utils.notebook.plotting import render
from sklearn import preprocessing
from sklearn.impute import SimpleImputer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, OrdinalEncoder, StandardScaler
from torch import functional, nn, optim
from torch.autograd import Variable
import math
rng =0

ordinalEncorder  = OrdinalEncoder()
labelEncoder = LabelEncoder()
standardScaler = StandardScaler()

d = {
    "funder": "string","installer": "string",
    "wpt_name": "string",    "basin": "string",
    "subvillage": "string",    "region": "string",    "lga": "string",    "ward": "string",
    "public_meeting": "string",    "recorded_by": "string",    "scheme_management": "string",
    "scheme_name": "string",    "permit": "string",
    "extraction_type": "string",    "extraction_type_group": "string",    "extraction_type_class": "string",
    "management": "string",
    "management_group": "string",    "payment": "string",
    "payment_type": "string",
    "water_quality": "string",    "quality_group": "string",
    "quantity": "string",    "quantity_group": "string",    "source": "string",
    "source_type": "string",
    "source_class": "string",    "waterpoint_type": "string",
    "waterpoint_type_group": "string",
    "date_recorded": "string",
}

str_cat= [
    "funder","installer",
    "wpt_name" ,    "basin",
    "subvillage",    "region",
    "lga",    "ward",
    "public_meeting",    "recorded_by",
    "scheme_management",    "scheme_name",
    "permit",    "extraction_type",
    "extraction_type_group",
    "extraction_type_class",    "management",
    "management_group",
    "payment",    "payment_type",    "water_quality",    "quality_group",
    "quantity",
    "quantity_group",    "source",
    "source_type",    "source_class",
    "waterpoint_type",
    "waterpoint_type_group",
    "date_recorded",
]

test_values =pd.read_csv("./test-set-values.csv")
train_values = pd.read_csv("./training-set-values.csv")
train_labels = pd.read_csv("./training-set-labels.csv")
length = train_values.shape[0]
train_batch_size = 100
test_batch_size= 100

train_labels['status_group'] = labelEncoder.fit_transform(train_labels.status_group.astype('string'))
train_values[str_cat] = ordinalEncorder.fit_transform(train_values[str_cat].astype(d).fillna(""))
test_values[str_cat] = ordinalEncorder.fit_transform(test_values[str_cat].astype(d).fillna(""))

train_values[str_cat] = standardScaler.fit_transform(train_values[str_cat])
test_values[str_cat]=standardScaler.fit_transform(test_values[str_cat])

train_values = train_values.astype('float32').values
test_values = test_values.astype('float32').values
train_labels = train_labels.status_group.astype('long').values

x_train,x_test,y_train,y_test = map(torch.from_numpy,
                                    train_test_split(train_values,train_labels,test_size=0.2,
                                                 random_state=rng,shuffle=True))

train = torch.utils.data.TensorDataset(x_train,y_train)
test = torch.utils.data.TensorDataset(x_test,y_test)
train_loader = torch.utils.data.DataLoader(train,batch_size=train_batch_size,shuffle = False)
test_loader = torch.utils.data.DataLoader(test,batch_size=test_batch_size,shuffle = False)

class Net(nn.Module):
    def __init__(self,hidden1,hidden2,dropoutProbabilities1):
        super(Net,self).__init__()

        self.layer1 = nn.Linear(40,hidden1,)
        self.layer2 = nn.Linear(hidden1,hidden2)
        self.layer3 = nn.Linear(hidden2,3)
        # self.layer2 = nn.Linear(hidden1,3) # layer 2 configuration if layer 3 is not used.
        self.layer4 = nn.Linear(3,3,)
        self.activation =nn.ReLU()
        self.dropout1 = nn.Dropout(p=dropoutProbabilities1)
        # self.dropout2 = nn.Dropout(p=dropoutProbabilities2)
        # self.batchNormalisation1 = nn.BatchNorm1d(hidden1)
        # self.batchNormalisation2 = nn.BatchNorm1d(hidden2)
    def forward(self, x):
        x = self.layer1(x)
        # x = self.batchNormalisation1(x)

        x = self.layer2(x)
        x = self.activation(x)
        x = self.dropout1(x)
#         x = self.batchNormalisation2(x)

        x = self.layer3(x)
        x = self.activation(x)
        x = self.dropout1(x)
        x = self.layer4(x)
        return x

def train(net, parameterization, trainloader):
    optimizer = optim.Adam(net.parameters(),
                           lr=parameterization['lr'],
                           weight_decay=parameterization['weight_decay'],

                           maximize = True
                           )
    criterion = nn.CrossEntropyLoss()

    for epoch in range(5):
        for data in trainloader:
            inputs, labels = map(Variable,data)
            optimizer.zero_grad()
            loss = criterion(net(inputs), labels)
            loss.backward()
            optimizer.step()
    return net

def evaluate(net, testloader):
    correct = 0
    total = 0
    with torch.no_grad():
        for data in testloader:
            inputs, labels = map(Variable,data)
            outputs = net(inputs)
            _,predicted = torch.max(outputs,1)

            # print("row in testloader")
            # print("predicted: ")
            # print(predicted)
            # print("labels: ")
            # print(labels)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()
            # print(str(correct)+"/"+str(total))
    print('Accuracy of the network: %d %%' % (
        100 * correct / total))
    return 100 * correct / total

def train_evaluate(parameters):
    net = Net(parameters["hidden1"],parameters['hidden2'],parameters['dropoutProbabilities1'])
    # net = Net(parameters["hidden1"],parameters['hidden2'])
    net = train(net, parameters, train_loader)
    return evaluate(net, test_loader)
parameters=[
        {"name": "lr", "type": "range",
         "value_type":'float', "bounds": [0.001, 0.5],'log_scale':True},
        {"name": "weight_decay", "type": "range",
         "value_type":'float',"bounds": [0.1, 0.9999],'log_scale':True},
        {"name": "hidden1", "type":"range",
           "value_type":'int',"bounds":[5,1100],'log_scale':True},
         {"name": "hidden2", "type":"range",
            "value_type":'int',"bounds":[5,1100],'log_scale':True},
    {"name":"dropoutProbabilities1","type":"range","value_type"
     :"float","bounds":[0.1,0.4]},

    ]
best_parameters, values, experiment, model = optimize(
    parameters= parameters,
    evaluation_function=train_evaluate,
    objective_name='accuracy',
    minimize=False,
    total_trials=10
)
</code>```

Body:
As a newbie in 'pytorch', I am building a neural network for classification of faulty water pumps in tazania for this competition I am also using ax-platform for hyperparameter tuning.

Yes methods such as gradient boosting classifiers and random forest probably works well or even better than neural network classifier for this tabular data problem but I want to practice using pytorch.

The problem is that when I am doing optimise from ax-platform I am  getting accuracy score not more than 54% and I wish to improve that. It jumps around certain number only as shown here:

I tried to do some debugging in my evaluate function by printing out predicted and labels

def evaluate(net, testloader):
        correct = 0
        total = 0
        with torch.no_grad():
            for data in testloader:
                inputs, labels = map(Variable,data)
                outputs = net(inputs)
                _,predicted = torch.max(outputs,1)

                print("row in testloader")
                print("predicted: ")
                print(predicted)
                print("labels: ")
                print(labels)
                total += labels.size(0)
                correct += (predicted == labels).sum().item()
                # print(str(correct)+"/"+str(total))
        print('Accuracy of the network: %d %%' % (
            100 * correct / total))
        return 100 * correct / total

and net(inputs) is giving a tensor of all ones. It should have been a tensors with varied classifications/numbers of [0,1,2] .

I have tried:

using SGD and ADAM

1,2 and 3 hidden layers

different epoches

'Dropout' with different dropout probabilites

disabling biases

having different learning rates

different number of neurons

different total trails

different betas and eps for ADAM algorithm

'log_scale = True' in parameters optimsation

sci-kit learn StandardScaler on train_values and test_values

How can I break the 54% accuracy barrier here? What are the gaps in my knowledges? What bugs in the code that causes this?

Here is the code:

import numpy as np
import pandas as pd
import torch
import torchvision
import torchvision.transforms as transforms
from ax import optimize
from ax.plot.contour import plot_contour
from ax.plot.trace import optimization_trace_single_method
from ax.utils.notebook.plotting import render
from sklearn import preprocessing
from sklearn.impute import SimpleImputer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, OrdinalEncoder, StandardScaler
from torch import functional, nn, optim
from torch.autograd import Variable
import math
rng =0

ordinalEncorder  = OrdinalEncoder()
labelEncoder = LabelEncoder()
standardScaler = StandardScaler()

d = {
    "funder": "string","installer": "string",
    "wpt_name": "string",    "basin": "string",
    "subvillage": "string",    "region": "string",    "lga": "string",    "ward": "string",
    "public_meeting": "string",    "recorded_by": "string",    "scheme_management": "string",
    "scheme_name": "string",    "permit": "string",
    "extraction_type": "string",    "extraction_type_group": "string",    "extraction_type_class": "string",
    "management": "string",
    "management_group": "string",    "payment": "string",
    "payment_type": "string",
    "water_quality": "string",    "quality_group": "string",
    "quantity": "string",    "quantity_group": "string",    "source": "string",
    "source_type": "string",
    "source_class": "string",    "waterpoint_type": "string",
    "waterpoint_type_group": "string",
    "date_recorded": "string",
}

str_cat= [
    "funder","installer",
    "wpt_name" ,    "basin",
    "subvillage",    "region",
    "lga",    "ward",
    "public_meeting",    "recorded_by",
    "scheme_management",    "scheme_name",
    "permit",    "extraction_type",
    "extraction_type_group",
    "extraction_type_class",    "management",
    "management_group",
    "payment",    "payment_type",    "water_quality",    "quality_group",
    "quantity",
    "quantity_group",    "source",
    "source_type",    "source_class",
    "waterpoint_type",
    "waterpoint_type_group",
    "date_recorded",
]

test_values =pd.read_csv("./test-set-values.csv")
train_values = pd.read_csv("./training-set-values.csv")
train_labels = pd.read_csv("./training-set-labels.csv")
length = train_values.shape[0]
train_batch_size = 100
test_batch_size= 100

train_labels['status_group'] = labelEncoder.fit_transform(train_labels.status_group.astype('string'))
train_values[str_cat] = ordinalEncorder.fit_transform(train_values[str_cat].astype(d).fillna(""))
test_values[str_cat] = ordinalEncorder.fit_transform(test_values[str_cat].astype(d).fillna(""))

train_values[str_cat] = standardScaler.fit_transform(train_values[str_cat])
test_values[str_cat]=standardScaler.fit_transform(test_values[str_cat])

train_values = train_values.astype('float32').values
test_values = test_values.astype('float32').values
train_labels = train_labels.status_group.astype('long').values

x_train,x_test,y_train,y_test = map(torch.from_numpy,
                                    train_test_split(train_values,train_labels,test_size=0.2,
                                                 random_state=rng,shuffle=True))

train = torch.utils.data.TensorDataset(x_train,y_train)
test = torch.utils.data.TensorDataset(x_test,y_test)
train_loader = torch.utils.data.DataLoader(train,batch_size=train_batch_size,shuffle = False)
test_loader = torch.utils.data.DataLoader(test,batch_size=test_batch_size,shuffle = False)

class Net(nn.Module):
    def __init__(self,hidden1,hidden2,dropoutProbabilities1):
        super(Net,self).__init__()

        self.layer1 = nn.Linear(40,hidden1,)
        self.layer2 = nn.Linear(hidden1,hidden2)
        self.layer3 = nn.Linear(hidden2,3)
        # self.layer2 = nn.Linear(hidden1,3) # layer 2 configuration if layer 3 is not used.
        self.layer4 = nn.Linear(3,3,)
        self.activation =nn.ReLU()
        self.dropout1 = nn.Dropout(p=dropoutProbabilities1)
        # self.dropout2 = nn.Dropout(p=dropoutProbabilities2)
        # self.batchNormalisation1 = nn.BatchNorm1d(hidden1)
        # self.batchNormalisation2 = nn.BatchNorm1d(hidden2)
    def forward(self, x):
        x = self.layer1(x)
        # x = self.batchNormalisation1(x)

        x = self.layer2(x)
        x = self.activation(x)
        x = self.dropout1(x)
#         x = self.batchNormalisation2(x)

        x = self.layer3(x)
        x = self.activation(x)
        x = self.dropout1(x)
        x = self.layer4(x)
        return x

def train(net, parameterization, trainloader):
    optimizer = optim.Adam(net.parameters(),
                           lr=parameterization['lr'],
                           weight_decay=parameterization['weight_decay'],

                           maximize = True
                           )
    criterion = nn.CrossEntropyLoss()

    for epoch in range(5):
        for data in trainloader:
            inputs, labels = map(Variable,data)
            optimizer.zero_grad()
            loss = criterion(net(inputs), labels)
            loss.backward()
            optimizer.step()
    return net

def evaluate(net, testloader):
    correct = 0
    total = 0
    with torch.no_grad():
        for data in testloader:
            inputs, labels = map(Variable,data)
            outputs = net(inputs)
            _,predicted = torch.max(outputs,1)

            # print("row in testloader")
            # print("predicted: ")
            # print(predicted)
            # print("labels: ")
            # print(labels)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()
            # print(str(correct)+"/"+str(total))
    print('Accuracy of the network: %d %%' % (
        100 * correct / total))
    return 100 * correct / total

def train_evaluate(parameters):
    net = Net(parameters["hidden1"],parameters['hidden2'],parameters['dropoutProbabilities1'])
    # net = Net(parameters["hidden1"],parameters['hidden2'])
    net = train(net, parameters, train_loader)
    return evaluate(net, test_loader)
parameters=[
        {"name": "lr", "type": "range",
         "value_type":'float', "bounds": [0.001, 0.5],'log_scale':True},
        {"name": "weight_decay", "type": "range",
         "value_type":'float',"bounds": [0.1, 0.9999],'log_scale':True},
        {"name": "hidden1", "type":"range",
           "value_type":'int',"bounds":[5,1100],'log_scale':True},
         {"name": "hidden2", "type":"range",
            "value_type":'int',"bounds":[5,1100],'log_scale':True},
    {"name":"dropoutProbabilities1","type":"range","value_type"
     :"float","bounds":[0.1,0.4]},

    ]
best_parameters, values, experiment, model = optimize(
    parameters= parameters,
    evaluation_function=train_evaluate,
    objective_name='accuracy',
    minimize=False,
    total_trials=10
)