Modules — ASTROMER 0.0.6 documentation

# Modules

## astromer

ASTROMER.core.astromer.get_ASTROMER(num_layers=2, d_model=200, num_heads=2, dff=256, base=10000, dropout=0.1, use_leak=False, no_train=True, maxlen=100, batch_size=None)

ASTROMER.core.astromer.predict(model, dataset, conf, predic_proba=False)

ASTROMER.core.astromer.train(model, train_dataset, valid_dataset, patience=20, exp_path='./experiments/test', epochs=1, finetuning=False, use_random=True, num_cls=2, lr=0.001, verbose=1)

ASTROMER.core.astromer.train_step(model, batch, opt)

ASTROMER.core.astromer.valid_step(model, batch, return_pred=False, normed=False)

## attention

class ASTROMER.core.attention.MultiHeadAttention(*args, **kwargs)

  Bases: `Layer`

  call(x, mask)

    This is where the layer’s logic lives.

    The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,

    > in __init__(), or in the build() method that is

    called automatically before call() executes for the first time.

    Args:
      inputs: Input tensor, or dict/list/tuple of input tensors.

        The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero

        > arguments, and inputs cannot be provided via the default value of a keyword argument.

        - NumPy array or Python scalar values in inputs get cast as tensors.

        - Keras mask metadata is only collected from inputs.

        - Layers are built (build(input_shape) method) using shape info from inputs only.

        - input_spec compatibility is only checked against inputs.

        - Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.

        - The SavedModel input specification is generated using inputs only.

        - Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.

      *args: Additional positional arguments. May contain tensors, although

        this is not recommended, for the reasons above.

      **kwargs: Additional keyword arguments. May contain tensors, although

        this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating

        > whether the call is meant for training or inference.

        - mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).

    Returns:

      A tensor or list/tuple of tensors.

  split_heads(x, batch_size, name='qkv')

    Split the last dimension into (num_heads, depth). Transpose the result such that the shape is (batch_size, num_heads, seq_len, depth)

ASTROMER.core.attention.scaled_dot_product_attention(q, k, v, mask)

  Calculate the attention weights. q, k, v must have matching leading dimensions. k, v must have matching penultimate dimension, i.e.: seq_len_k = seq_len_v. The mask has different shapes depending on its type(padding or look ahead) but it must be broadcastable for addition.

  Parameters:

    - q – query shape == (…, seq_len_q, depth)

    - k – key shape == (…, seq_len_k, depth)

    - v – value shape == (…, seq_len_v, depth_v)

    - mask – Float tensor with shape broadcastable to (…, seq_len_q, seq_len_k). Defaults to None.

  Returns:

    output, attention_weights

## classifier

ASTROMER.core.classifier.get_conf(path)

ASTROMER.core.classifier.get_fc_attention(units, num_classes, weigths)

  FC + ATT

ASTROMER.core.classifier.get_lstm_attention(units, num_classes, weigths, dropout=0.5)

  ATT + LSTM + LSTM + FC

ASTROMER.core.classifier.get_lstm_no_attention(units, num_classes, maxlen, dropout=0.5)

  LSTM + LSTM + FC

ASTROMER.core.classifier.load_weights(model, weigths)

ASTROMER.core.classifier.predict(model, test_batches)

ASTROMER.core.classifier.predict_from_path(path, test_batches, mode=0, save=False)

ASTROMER.core.classifier.train(model, train_batches, valid_batches, patience=20, exp_path='./experiments/test', epochs=1, lr=0.001, verbose=1)

ASTROMER.core.classifier.train_step(model, batch, opt)

ASTROMER.core.classifier.valid_step(model, batch, return_pred=False)

## data

ASTROMER.core.data.adjust_fn(func, *arguments)

ASTROMER.core.data.adjust_fn_clf(func, max_obs)

ASTROMER.core.data.create_dataset(meta_df, source='data/raw_data/macho/MACHO/LCs', target='data/records/macho/', n_jobs=None, subsets_frac=(0.5, 0.25), test_subset=None, max_lcs_per_record=100, **kwargs)

ASTROMER.core.data.create_generator(list_of_arrays, labels=None, ids=None)

ASTROMER.core.data.deserialize(sample)

  Read a serialized sample and convert it to tensor Context and sequence features should match with the name used when writing. Args:

  > sample (binary): serialized sample

  Returns:

    type: decoded sample

ASTROMER.core.data.divide_training_subset(frame, train, val, test_meta)

  Divide the dataset into train, validation and test subsets. Notice that:

  > test = 1 - (train + val)

  Args:

    frame (Dataframe): Dataframe following the astro-standard format dest (string): Record destination. train (float): train fraction val (float): validation fraction

  Returns:

    tuple x3 : (name of subset, subframe with metadata)

ASTROMER.core.data.format_label(input_dict, num_cls)

ASTROMER.core.data.get_example(lcid, label, lightcurve)

  Create a record example from numpy values.

  Args:

    lcid (string): object id label (int): class code lightcurve (numpy array): time, magnitudes and observational error

  Returns:

    tensorflow record

ASTROMER.core.data.get_window(sequence, length, pivot, max_obs)

ASTROMER.core.data.get_windows(sample, max_obs, deserialize=True)

ASTROMER.core.data.load_numpy(samples, ids=None, labels=None, batch_size=1, shuffle=False, sampling=False, max_obs=100, msk_frac=0.0, rnd_frac=0.0, same_frac=0.0, repeat=1)

ASTROMER.core.data.mask_sample(x, y, i, msk_prob, rnd_prob, same_prob, max_obs)

  Pretraining formater

ASTROMER.core.data.pretraining_records(source, batch_size, max_obs=100, msk_frac=0.2, rnd_frac=0.1, same_frac=0.1, sampling=False, shuffle=False, n_classes=-1, repeat=1)

  Pretraining data loader. This method build the ASTROMER input format. ASTROMER format is based on the BERT masking strategy.

  Args:

    source (string): Record folder batch_size (int): Batch size no_shuffle (bool): Do not shuffle training and validation dataset max_obs (int): Max. number of observation per serie msk_frac (float): fraction of values to be predicted ([MASK]) rnd_frac (float): fraction of [MASKED] values to replace with random values same_frac (float): fraction of [MASKED] values to replace with true values

  Returns:

    Tensorflow Dataset: Iterator withg preprocessed batches

ASTROMER.core.data.process_lc3(lc_index, label, numpy_lc, writer)

ASTROMER.core.data.sample_lc(sample, max_obs, deserialize=True)

  Sample a random window of “max_obs” observations from the input sequence

ASTROMER.core.data.write_records(frame, dest, max_lcs_per_record, source, unique, n_jobs=None, max_obs=200, **kwargs)

## encoder

class ASTROMER.core.encoder.Encoder(*args, **kwargs)

  Bases: `Layer`

  call(data, training=False)

    This is where the layer’s logic lives.

    The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,

    > in __init__(), or in the build() method that is

    called automatically before call() executes for the first time.

    Args:
      inputs: Input tensor, or dict/list/tuple of input tensors.

        The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero

        > arguments, and inputs cannot be provided via the default value of a keyword argument.

        - NumPy array or Python scalar values in inputs get cast as tensors.

        - Keras mask metadata is only collected from inputs.

        - Layers are built (build(input_shape) method) using shape info from inputs only.

        - input_spec compatibility is only checked against inputs.

        - Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.

        - The SavedModel input specification is generated using inputs only.

        - Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.

      *args: Additional positional arguments. May contain tensors, although

        this is not recommended, for the reasons above.

      **kwargs: Additional keyword arguments. May contain tensors, although

        this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating

        > whether the call is meant for training or inference.

        - mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).

    Returns:

      A tensor or list/tuple of tensors.

class ASTROMER.core.encoder.EncoderLayer(*args, **kwargs)

  Bases: `Layer`

  call(x, training, mask)

    This is where the layer’s logic lives.

    The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,

    > in __init__(), or in the build() method that is

    called automatically before call() executes for the first time.

    Args:
      inputs: Input tensor, or dict/list/tuple of input tensors.

        The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero

        > arguments, and inputs cannot be provided via the default value of a keyword argument.

        - NumPy array or Python scalar values in inputs get cast as tensors.

        - Keras mask metadata is only collected from inputs.

        - Layers are built (build(input_shape) method) using shape info from inputs only.

        - input_spec compatibility is only checked against inputs.

        - Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.

        - The SavedModel input specification is generated using inputs only.

        - Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.

      *args: Additional positional arguments. May contain tensors, although

        this is not recommended, for the reasons above.

      **kwargs: Additional keyword arguments. May contain tensors, although

        this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating

        > whether the call is meant for training or inference.

        - mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).

    Returns:

      A tensor or list/tuple of tensors.

ASTROMER.core.encoder.point_wise_feed_forward_network(d_model, dff)

## losses

ASTROMER.core.losses.custom_bce(y_true, y_pred, sample_weight=None)

ASTROMER.core.losses.custom_rmse(y_true, y_pred, sample_weight=None, mask=None)

## masking

ASTROMER.core.masking.create_look_ahead_mask(size)

ASTROMER.core.masking.get_masked(tensor, frac=0.15)

  Add [MASK] values to be predicted Args:

  > tensor : tensor values frac (float, optional): percentage for masking [MASK]

  Returns:

    binary tensor: a time-distributed mask

ASTROMER.core.masking.get_padding_mask(steps, lengths)

  Create mask given a tensor and true length

ASTROMER.core.masking.reshape_mask(mask)

  Reshape Mask to match attention dimensionality

ASTROMER.core.masking.set_random(serie_1, mask_1, serie_2, rnd_frac, name='set_random')

  Add Random values in serie_1 Note that if serie_2 == serie_1 then it replaces the true value Args:

  > serie_1: current serie mask_1 : mask containing the [MASKED]-indices from serie_1 serie_2: random values to be placed within serie_1 rnd_frac (float): fraction of [MASKED] to be replaced by random

  > > elements from serie_2

  Returns:

    serie_1: serie_1 with random values

## metrics

ASTROMER.core.metrics.custom_acc(y_true, y_pred)

## output

class ASTROMER.core.output.RegLayer(*args, **kwargs)

  Bases: `Layer`

  call(inputs)

    This is where the layer’s logic lives.

    The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,

    > in __init__(), or in the build() method that is

    called automatically before call() executes for the first time.

    Args:
      inputs: Input tensor, or dict/list/tuple of input tensors.

        The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero

        > arguments, and inputs cannot be provided via the default value of a keyword argument.

        - NumPy array or Python scalar values in inputs get cast as tensors.

        - Keras mask metadata is only collected from inputs.

        - Layers are built (build(input_shape) method) using shape info from inputs only.

        - input_spec compatibility is only checked against inputs.

        - Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.

        - The SavedModel input specification is generated using inputs only.

        - Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.

      *args: Additional positional arguments. May contain tensors, although

        this is not recommended, for the reasons above.

      **kwargs: Additional keyword arguments. May contain tensors, although

        this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating

        > whether the call is meant for training or inference.

        - mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).

    Returns:

      A tensor or list/tuple of tensors.

class ASTROMER.core.output.SauceLayer(*args, **kwargs)

  Bases: `Layer`

  build(input_shape)

    Creates the variables of the layer (optional, for subclass implementers).

    This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call. It is invoked automatically before the first execution of call().

    This is typically used to create the weights of Layer subclasses (at the discretion of the subclass implementer).

    Args:
      input_shape: Instance of TensorShape, or list of instances of

        TensorShape if the layer expects a list of inputs (one instance per input).

  call(inputs)

    This is where the layer’s logic lives.

    The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,

    > in __init__(), or in the build() method that is

    called automatically before call() executes for the first time.

    Args:
      inputs: Input tensor, or dict/list/tuple of input tensors.

        The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero

        > arguments, and inputs cannot be provided via the default value of a keyword argument.

        - NumPy array or Python scalar values in inputs get cast as tensors.

        - Keras mask metadata is only collected from inputs.

        - Layers are built (build(input_shape) method) using shape info from inputs only.

        - input_spec compatibility is only checked against inputs.

        - Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.

        - The SavedModel input specification is generated using inputs only.

        - Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.

      *args: Additional positional arguments. May contain tensors, although

        this is not recommended, for the reasons above.

      **kwargs: Additional keyword arguments. May contain tensors, although

        this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating

        > whether the call is meant for training or inference.

        - mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).

    Returns:

      A tensor or list/tuple of tensors.

  init(shape, **kwargs)

## plots

ASTROMER.core.plots.plot_cm(cm, ax, title='CM', fontsize=15, cbar=False, yticklabels=True, class_names=None)

  Plot Confusion Matrix

## positional

ASTROMER.core.positional.get_angles(times, d_model)

ASTROMER.core.positional.get_angles_original(times, d_model, base=10000)

ASTROMER.core.positional.positional_encoding(times, d_model, base=10000, mjd=False)

## scheduler

class ASTROMER.core.scheduler.CustomSchedule(d_model, warmup_steps=4000)

  Bases: `LearningRateSchedule`

## tboard

ASTROMER.core.tboard.draw_graph(model, dataset, writer, logdir='')

  Decorator that reports store fn graph.

ASTROMER.core.tboard.save_scalar(writer, value, step, name='')

## utils

ASTROMER.core.utils.get_folder_name(path, prefix='')

  Look at the current path and change the name of the experiment if it is repeated

  Args:

    path (string): folder path prefix (string): prefix to add

  Returns:

    string: unique path to save the experiment

ASTROMER.core.utils.get_metrics(path_logs, metric_name='epoch_loss')

ASTROMER.core.utils.my_summary_iterator(path)

ASTROMER.core.utils.standardize(tensor, axis=0, return_mean=False)

  Standardize a tensor subtracting the mean

  Args:

    tensor (1-dim tensorflow tensor): values axis (int): axis on which we calculate the mean return_mean (bool): output the mean of the tensor

    > turning on the original scale

  Returns:

    tensor (1-dim tensorflow tensor): standardize tensor