text:
<?php

namespace Google\Service\Contentwarehouse;

class PeoplestackFlexorgsProtoInternalExternal extends \Google\Collection
{
  protected $collection_key = 'stateStatus';
  /**
   * @var string
   */
  public $application;
  protected $stateStatusType = PeoplestackFlexorgsProtoInternalExternalStateStatus::class;
  protected $stateStatusDataType = 'array';

  /**
   * @param string
   */
  public function setApplication($application)
  {
    $this->application = $application;
  }
  /**
   * @return string
   */
  public function getApplication()
  {
    return $this->application;
  }
  /**
   * @param PeoplestackFlexorgsProtoInternalExternalStateStatus[]
   */
  public function setStateStatus($stateStatus)
  {
    $this->stateStatus = $stateStatus;
  }
  /**
   * @return PeoplestackFlexorgsProtoInternalExternalStateStatus[]
   */
  public function getStateStatus()
  {
    return $this->stateStatus;
  }
}

// Adding a class alias for backwards compatibility with the previous class name.
class_alias(PeoplestackFlexorgsProtoInternalExternal::class, 'Google_Service_Contentwarehouse_PeoplestackFlexorgsProtoInternalExternal');


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Domain;
using Interfaces.Repositories;
using Microsoft.Owin.Security;

namespace WebApi.DAL.Repositories
{
    public class ArticleRepository : WebApiRepository<Article>, IArticleRepository
    {
        public ArticleRepository(HttpClient httpClient, string endPoint, IAuthenticationManager authenticationManager) : base(httpClient, endPoint, authenticationManager)
        {
        }

        public Article FindArticleByName(string articleName)
        {
            //var res =
            //    DbSet.Include(a => a.ArticleHeadline.Translations)
            //        .Include(a => a.ArticleBody.Translations)
            //        .FirstOrDefault(a => a.ArticleName == articleName) ?? new Article()
            //        {
            //            ArticleName = "NotFound",
            //            ArticleHeadline = new MultiLangString("Article not found!"),
            //            ArticleBody = new MultiLangString("Article not found!")
            //        };

            return new Article()
            {
                ArticleName = "NotFound",
                ArticleHeadline = new MultiLangString("Article not found!"),
                ArticleBody = new MultiLangString("Article not found!")
            };
        }

        public List<Article> AllWithTranslations()
        {
            throw new NotImplementedException();
        }

        public void DeleteArticleWithTranslations(params object[] id)
        {
            throw new NotImplementedException();
        }
    }
}

redpajama_set_name: RedPajamaGithub


text:
import Database.HaskellDB.HSQL.MySQL

import RunTests

opts = []

main = dbTestMain $ Conn {
                           dbLabel = "hsql-mysql",
                           dbConn = connect driver opts
                         }


text:
namespace ospray {

struct OSPRAY_SDK_INTERFACE Instance : public ManagedObject
{
  Instance(Group *group);
  ~Instance() override = default;

  std::string toString() const override;

  void commit() override;

  box3f getBounds() const override;

  affine3f xfm();

  // Data //

  affine3f instanceXfm;
  affine3f rcpXfm;

  Ref<Group> group;
};

OSPTYPEFOR_SPECIALIZATION(Instance *, OSP_INSTANCE);

// Inlined definitions //////////////////////////////////////////////////////

inline affine3f Instance::xfm()
{
  return instanceXfm;
}

} // namespace ospray


text:
    {{ noValueMessage }}

        {{ item }}

            <input #newvalue type="text" [(ngModel)]="evalue" class="form-control form-control-pf-editor" placeholder="{{ placeholderText() }}"
                   autocomplete="off" (keypress)="onInputKeypress($event)" (keydown)="onInputKeypress($event)">
            <button type="button" class="form-control-pf-empty" aria-label="Clear Value" (click)="clearValue()">

            </button>

        <button title="Save changes." type="button" class="btn btn-primary form-control-pf-save"
                aria-label="Save" (click)="onSave()"></button>
        <button title="Cancel changes." type="button" class="btn btn-default form-control-pf-cancel"
                aria-label="Cancel" (click)="onCancel()"></button>

            {{ item }}


text:
@interface RHCTestEntityPropertyMapping : NSObject <ZLEntityMappingRuleProtocol>

@property (nonatomic, assign) ZLMappingTypeEnum mappingType;

@end


text:
ACCEPTED

#### According to
Index Fungorum

#### Published in
Biblthca Mycol. 170: 44 (1997)

#### Original name
Amanita pseudopantherina Zhu L. Yang

### Remarks
null


text:
package com.joopie.arcturus.plugin.friendfeed.service.pusher.trigger;

import com.google.gson.annotations.Expose;
import com.joopie.arcturus.plugin.friendfeed.service.IService;
import com.joopie.arcturus.plugin.friendfeed.service.ITrigger;

import java.util.List;

/**
 * Base class for a trigger.
 */
public class Trigger implements ITrigger{
    public static final int MAX_CHANNELS = 10;

    @Expose(serialize = false, deserialize = false)
    public final List<String> channels;
    @Expose(serialize = false, deserialize = false)
    public final String trigger;

    /**
     * Create the base trigger class and set the channels and the trigger.
     * @param channels
     * @param trigger
     */
    public Trigger(List<String> channels, String trigger) {
        this.channels = channels;
        this.trigger = trigger;
    }

    public void trigger(IService service) {
        int channelFromIndex = 0;
        int channelToIndex = 0;

        while (channelToIndex < this.channels.size()) {
            channelFromIndex = channelToIndex;
            channelToIndex = Math.min(channelFromIndex + MAX_CHANNELS, this.channels.size());

            List<String> subChannels = this.channels.subList(channelFromIndex, channelToIndex);

            service.trigger(subChannels, this.trigger, this);
        }
    }
}


text:
namespace pythonic
{

  namespace math
  {

    template <class T>
    double degrees(T x);

    PROXY_DECL(pythonic::math, degrees);
  }
}

#endif


text:
namespace Oryol {
namespace _priv {

bool curlURLLoader::curlInitCalled = false;
std::mutex curlURLLoader::curlInitMutex;

//------------------------------------------------------------------------------
curlURLLoader::curlURLLoader() :
curlSession(0),
curlError(0) {

    // we need to do some one-time curl initialization here,
    // thread-protected because curl_global_init() is not thread-safe
    curlInitMutex.lock();
    if (!curlInitCalled) {
        CURLcode curlInitRes = curl_global_init(CURL_GLOBAL_ALL);
        o_assert(0 == curlInitRes);
        curlInitCalled = true;
    }
    curlInitMutex.unlock();

    // setup a new curl session
    this->setupCurlSession();
}

//------------------------------------------------------------------------------
curlURLLoader::~curlURLLoader() {
    this->discardCurlSession();
}

//------------------------------------------------------------------------------
void
curlURLLoader::setupCurlSession() {
    o_assert(0 == this->curlError);
    o_assert(0 == this->curlSession);

    // setup the error buffer
    const int curlErrorBufferSize = CURL_ERROR_SIZE * 4;
    this->curlError = (char*) Memory::Alloc(curlErrorBufferSize);
    Memory::Clear(this->curlError, curlErrorBufferSize);

    // setup the curl session
    this->curlSession = curl_easy_init();
    o_assert(0 != this->curlSession);

    // set session options
    curl_easy_setopt(this->curlSession, CURLOPT_NOSIGNAL, 1L);
    curl_easy_setopt(this->curlSession, CURLOPT_NOPROGRESS, 1L);
    curl_easy_setopt(this->curlSession, CURLOPT_ERRORBUFFER, this->curlError);
    curl_easy_setopt(this->curlSession, CURLOPT_WRITEFUNCTION, curlWriteDataCallback);
    curl_easy_setopt(this->curlSession, CURLOPT_TCP_KEEPALIVE, 1L);
    curl_easy_setopt(this->curlSession, CURLOPT_TCP_KEEPIDLE, 10L);
    curl_easy_setopt(this->curlSession, CURLOPT_TCP_KEEPINTVL, 10L);
    curl_easy_setopt(this->curlSession, CURLOPT_TIMEOUT, 30);
    curl_easy_setopt(this->curlSession, CURLOPT_CONNECTTIMEOUT, 30);
    curl_easy_setopt(this->curlSession, CURLOPT_ACCEPT_ENCODING, "");   // all encodings supported by curl
    curl_easy_setopt(this->curlSession, CURLOPT_FOLLOWLOCATION, 1);
}

//------------------------------------------------------------------------------
void
curlURLLoader::discardCurlSession() {
    o_assert(0 != this->curlError);
    o_assert(0 != this->curlSession);

    curl_easy_cleanup(this->curlSession);
    this->curlSession = 0;
    Memory::Free(this->curlError);
    this->curlError = 0;
}

//------------------------------------------------------------------------------
size_t
curlURLLoader::curlWriteDataCallback(char* ptr, size_t size, size_t nmemb, void* userData) {
    // userData is expected to point to a Buffer object
    int bytesToWrite = (int) (size * nmemb);
    if (bytesToWrite > 0) {
        Buffer* buf = (Buffer*) userData;
        buf->Add((const uint8_t*)ptr, bytesToWrite);
        return bytesToWrite;
    }
    else {
        return 0;
    }
}

//------------------------------------------------------------------------------
bool
curlURLLoader::doRequest(const Ptr<IORead>& req) {
    if (baseURLLoader::doRequest(req)) {
        this->doRequestInternal(req);
        req->Handled = true;
        return true;
    }
    else {
        // request was cancelled
        return false;
    }
}

//------------------------------------------------------------------------------
void
curlURLLoader::doRequestInternal(const Ptr<IORead>& req) {
    o_assert(0 != this->curlSession);
    o_assert(0 != this->curlError);

    // set URL in curl
    const URL& url = req->Url;
    o_assert(url.Scheme() == "http");
    curl_easy_setopt(this->curlSession, CURLOPT_URL, url.AsCStr());
    if (url.HasPort()) {
        uint16_t port = StringConverter::FromString<uint16_t>(url.Port());
        curl_easy_setopt(this->curlSession, CURLOPT_PORT, port);
    }
    curl_easy_setopt(this->curlSession, CURLOPT_HTTPGET, 1);

    // add standard request headers:
    //  User-Agent: need a 'standard' user-agent, otherwise some HTTP servers
    //              won't accept Connection: keep-alive
    //  Connection: keep-alive, don't open/close the connection all the time
    //  Accept-Encoding:    gzip, deflate
    //
    struct curl_slist* requestHeaders = 0;
    requestHeaders = curl_slist_append(requestHeaders, "User-Agent: Mozilla/5.0");
    requestHeaders = curl_slist_append(requestHeaders, "Connection: keep-alive");
    requestHeaders = curl_slist_append(requestHeaders, "Accept-Encoding: gzip, deflate");
    curl_easy_setopt(this->curlSession, CURLOPT_HTTPHEADER, requestHeaders);

    // prepare the HTTPResponse and the response-body stream
    curl_easy_setopt(this->curlSession, CURLOPT_WRITEDATA, &(req->Data));

    // perform the request
    CURLcode performResult = curl_easy_perform(this->curlSession);

    // query the http code
    long curlHttpCode = 0;
    curl_easy_getinfo(this->curlSession, CURLINFO_RESPONSE_CODE, &curlHttpCode);
    req->Status = (IOStatus::Code) curlHttpCode;

    // check for error codes
    if (CURLE_PARTIAL_FILE == performResult) {
        // this seems to happen quite often even though all data has been received,
        // not sure what to do about this, but don't treat it as an error
        Log::Warn("curlURLLoader: CURLE_PARTIAL_FILE received for '%s', httpStatus='%ld'\n", req->Url.AsCStr(), curlHttpCode);
        req->ErrorDesc = this->curlError;
    }
    else if (0 != performResult) {
        // some other curl error
        Log::Warn("curlURLLoader: curl_easy_peform failed with '%s' for '%s', httpStatus='%ld'\n",
            req->Url.AsCStr(), this->curlError, curlHttpCode);
        req->ErrorDesc = this->curlError;
    }

    // free the previously allocated request headers
    if (0 != requestHeaders) {
        curl_slist_free_all(requestHeaders);
        requestHeaders = 0;
    }
}

} // namespace _priv
} // namespace Oryol


text:
package Class::ISA;
require 5;
use strict;
use vars qw($Debug $VERSION);
$VERSION = '0.33';
$Debug = 0 unless defined $Debug;

=head1 NAME

Class::ISA -- report the search path for a class's ISA tree

=head1 SYNOPSIS

  # Suppose you go: use Food::Fishstick, and that uses and
  # inherits from other things, which in turn use and inherit
  # from other things.  And suppose, for sake of brevity of
  # example, that their ISA tree is the same as:

  @Food::Fishstick::ISA = qw(Food::Fish  Life::Fungus  Chemicals);
  @Food::Fish::ISA = qw(Food);
  @Food::ISA = qw(Matter);
  @Life::Fungus::ISA = qw(Life);
  @Chemicals::ISA = qw(Matter);
  @Life::ISA = qw(Matter);
  @Matter::ISA = qw();

  use Class::ISA;
  print "Food::Fishstick path is:\n ",
        join(", ", Class::ISA::super_path('Food::Fishstick')),
        "\n";

That prints:

  Food::Fishstick path is:
   Food::Fish, Food, Matter, Life::Fungus, Life, Chemicals

=head1 DESCRIPTION

Suppose you have a class (like Food::Fish::Fishstick) that is derived,
via its @ISA, from one or more superclasses (as Food::Fish::Fishstick
is from Food::Fish, Life::Fungus, and Chemicals), and some of those
superclasses may themselves each be derived, via its @ISA, from one or
more superclasses (as above).

When, then, you call a method in that class ($fishstick->calories),
Perl first searches there for that method, but if it's not there, it
goes searching in its superclasses, and so on, in a depth-first (or
maybe "height-first" is the word) search.  In the above example, it'd
first look in Food::Fish, then Food, then Matter, then Life::Fungus,
then Life, then Chemicals.

This library, Class::ISA, provides functions that return that list --
the list (in order) of names of classes Perl would search to find a
method, with no duplicates.

=head1 FUNCTIONS

=over

=item the function Class::ISA::super_path($CLASS)

This returns the ordered list of names of classes that Perl would
search thru in order to find a method, with no duplicates in the list.
$CLASS is not included in the list.  UNIVERSAL is not included -- if
you need to consider it, add it to the end.

=item the function Class::ISA::self_and_super_path($CLASS)

Just like C<super_path>, except that $CLASS is included as the first
element.

=item the function Class::ISA::self_and_super_versions($CLASS)

This returns a hash whose keys are $CLASS and its
(super-)superclasses, and whose values are the contents of each
class's $VERSION (or undef, for classes with no $VERSION).

The code for self_and_super_versions is meant to serve as an example
for precisely the kind of tasks I anticipate that self_and_super_path
and super_path will be used for.  You are strongly advised to read the
source for self_and_super_versions, and the comments there.

=back

=head1 CAUTIONARY NOTES

* Class::ISA doesn't export anything.  You have to address the
functions with a "Class::ISA::" on the front.

* Contrary to its name, Class::ISA isn't a class; it's just a package.
Strange, isn't it?

* Say you have a loop in the ISA tree of the class you're calling one
of the Class::ISA functions on: say that Food inherits from Matter,
but Matter inherits from Food (for sake of argument).  If Perl, while
searching for a method, actually discovers this cyclicity, it will
throw a fatal error.  The functions in Class::ISA effectively ignore
this cyclicity; the Class::ISA algorithm is "never go down the same
path twice", and cyclicities are just a special case of that.

* The Class::ISA functions just look at @ISAs.  But theoretically, I
suppose, AUTOLOADs could bypass Perl's ISA-based search mechanism and
do whatever they please.  That would be bad behavior, tho; and I try
not to think about that.

* If Perl can't find a method anywhere in the ISA tree, it then looks
in the magical class UNIVERSAL.  This is rarely relevant to the tasks
that I expect Class::ISA functions to be put to, but if it matters to
you, then instead of this:

  @supers = Class::Tree::super_path($class);

do this:

  @supers = (Class::Tree::super_path($class), 'UNIVERSAL');

And don't say no-one ever told ya!

* When you call them, the Class::ISA functions look at @ISAs anew --
that is, there is no memoization, and so if ISAs change during
runtime, you get the current ISA tree's path, not anything memoized.
However, changing ISAs at runtime is probably a sign that you're out
of your mind!

=head1 COPYRIGHT

Copyright (c) 1999, 2000 Sean M. Burke. All rights reserved.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=head1 AUTHOR

Sean M. Burke C<sburke@cpan.org>

=cut

###########################################################################

sub self_and_super_versions {
  no strict 'refs';
  map {
        $_ => (defined(${"$_\::VERSION"}) ? ${"$_\::VERSION"} : undef)
      } self_and_super_path($_[0])
}

# Also consider magic like:
#   no strict 'refs';
#   my %class2SomeHashr =
#     map { defined(%{"$_\::SomeHash"}) ? ($_ => \%{"$_\::SomeHash"}) : () }
#         Class::ISA::self_and_super_path($class);
# to get a hash of refs to all the defined (and non-empty) hashes in
# $class and its superclasses.
#
# Or even consider this incantation for doing something like hash-data
# inheritance:
#   no strict 'refs';
#   %union_hash =
#     map { defined(%{"$_\::SomeHash"}) ? %{"$_\::SomeHash"}) : () }
#         reverse(Class::ISA::self_and_super_path($class));
# Consider that reverse() is necessary because with
#   %foo = ('a', 'wun', 'b', 'tiw', 'a', 'foist');
# $foo{'a'} is 'foist', not 'wun'.

###########################################################################
sub super_path {
  my @ret = &self_and_super_path(@_);
  shift @ret if @ret;
  return @ret;
}

#--------------------------------------------------------------------------
sub self_and_super_path {
  # Assumption: searching is depth-first.
  # Assumption: '' (empty string) can't be a class package name.
  # Note: 'UNIVERSAL' is not given any special treatment.
  return () unless @_;

  my @out = ();

  my @in_stack = ($_[0]);
  my %seen = ($_[0] => 1);

  my $current;
  while(@in_stack) {
    next unless defined($current = shift @in_stack) && length($current);
    print "At $current\n" if $Debug;
    push @out, $current;
    no strict 'refs';
    unshift @in_stack,
      map
        { my $c = $_; # copy, to avoid being destructive
          substr($c,0,2) = "main::" if substr($c,0,2) eq '::';
           # Canonize the :: -> main::, ::foo -> main::foo thing.
           # Should I ever canonize the Foo'Bar = Foo::Bar thing?
          $seen{$c}++ ? () : $c;
        }
        @{"$current\::ISA"}
    ;
    # I.e., if this class has any parents (at least, ones I've never seen
    # before), push them, in order, onto the stack of classes I need to
    # explore.
  }

  return @out;
}
#--------------------------------------------------------------------------
1;

__END__


text:
package dom.pets;

import java.util.List;

import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.VersionStrategy;

import org.joda.time.DateTime;

import org.apache.isis.applib.DomainObjectContainer;
import org.apache.isis.applib.annotation.BookmarkPolicy;
import org.apache.isis.applib.annotation.DomainObject;
import org.apache.isis.applib.annotation.DomainObjectLayout;
import org.apache.isis.applib.annotation.MemberOrder;
import org.apache.isis.applib.annotation.Title;
import org.apache.isis.applib.util.ObjectContracts;

import dom.owners.Owner;
import dom.owners.Owners;

@javax.jdo.annotations.PersistenceCapable(identityType = IdentityType.DATASTORE)
@javax.jdo.annotations.DatastoreIdentity(strategy = javax.jdo.annotations.IdGeneratorStrategy.IDENTITY, column = "id")
@javax.jdo.annotations.Version(strategy = VersionStrategy.VERSION_NUMBER, column = "version")
@javax.jdo.annotations.Queries({
		@javax.jdo.annotations.Query(
				name = "find",
				language = "JDOQL",
				value = "SELECT "
						+ "FROM dom.pets.Pet "),
		@javax.jdo.annotations.Query(
				name = "findByName",
				language = "JDOQL",
				value = "SELECT "
						+ "FROM dom.pets.Pet "
						+ "WHERE name.matches(:name)"),
		@javax.jdo.annotations.Query(
				name = "findByNameIndexOf",
				language = "JDOQL",
				value = "SELECT "
						+ "FROM dom.pets.Pet "
						+ "WHERE name.indexOf(:name) >= 0 ")
})
@javax.jdo.annotations.Unique(name = "Pet_name_UNQ", members = { "name" })
@DomainObject(objectType = "PET")
@DomainObjectLayout(bookmarking = BookmarkPolicy.AS_ROOT)
public class Pet implements Comparable<Pet> {

	// region > iconName
	public String iconName() {
		return getSpecies().name();
	}

	// endregion

	// region > name (property)

	private String name;

	@javax.jdo.annotations.Column(allowsNull = "false")
	@Title(sequence = "1")
	@MemberOrder(sequence = "1")
	public String getName() {
		return name;
	}

	public void setName(final String name) {
		this.name = name;
	}

	// endregion

	// region > species (property)
	private PetSpecies species;

	@javax.jdo.annotations.Column(allowsNull = "false")
	@MemberOrder(sequence = "2")
	public PetSpecies getSpecies() {
		return species;
	}

	public void setSpecies(final PetSpecies species) {
		this.species = species;
	}

	// endregion

	// region > owner (property)
	// this is an OPTIONAL property
	private Owner owner;

	@javax.jdo.annotations.Column(allowsNull = "true")
	@MemberOrder(sequence = "3")
	public Owner getOwner() {
		return owner;
	}

	public void setOwner(final Owner owner) {
		this.owner = owner;
	}

	public List<Owner> autoCompleteOwner(final String name) {
		return owners.findByName(name);
	}

	// endregion

	//region > health
	private Health health;
	@javax.jdo.annotations.Column(allowsNull = "false")
	@MemberOrder(sequence = "4")
	public Health getHealth() {
		return health;
	}

	public void setHealth(final Health h) {
		this.health = h;
	}
	/*public void changeHealth()
	{
		if(this.health == Health.Healthy)
			setHealth(Health.Sick);
		else
			setHealth(Health.Healthy);
	}*/

	// endregion

	// region > compareTo

	@Override
	public int compareTo(Pet other) {
		return ObjectContracts.compare(this, other, "name");
	}

	// endregion

	private DateTime CheckIn;
	@javax.jdo.annotations.Column(allowsNull = "true")
	@MemberOrder(sequence = "5")
	public DateTime getCheckIn() {
		return CheckIn;
	}

	public void setCheckIn(final DateTime d) {
		this.CheckIn = d;
	}

	private DateTime CheckOut;
	@javax.jdo.annotations.Column(allowsNull = "true")
	@MemberOrder(sequence = "6")
	public DateTime getCheckOut() {
		return CheckOut;
	}

	public void setCheckOut(final DateTime d) {
		this.CheckOut = d;
	}

	private String notes;
	@javax.jdo.annotations.Column(allowsNull = "true")
	@MemberOrder(sequence = "7")
	public String getNotes(){return notes;}

	public void setNotes(final String note)
	{
		this.notes = note;
	}

	// region > injected services

	@javax.inject.Inject
	@SuppressWarnings("unused")
	private DomainObjectContainer container;

	@javax.inject.Inject
	private Owners owners;
	// endregion

}


text:
"""Semantic Training library."""

import functools
from typing import Optional, Tuple

from absl import logging
import chex
import flax
import jax
from jax import numpy as jnp

import jax3d.projects.nesf as jax3d
from jax3d.projects.nesf.nerfstatic import datasets
from jax3d.projects.nesf.nerfstatic.losses import losses
from jax3d.projects.nesf.nerfstatic.models import models
from jax3d.projects.nesf.nerfstatic.models import volumetric_semantic_model
from jax3d.projects.nesf.nerfstatic.nerf import utils
from jax3d.projects.nesf.nerfstatic.utils import config as nerf_config
from jax3d.projects.nesf.nerfstatic.utils import jax_process_zero
from jax3d.projects.nesf.nerfstatic.utils import semantic_utils
from jax3d.projects.nesf.nerfstatic.utils import train_utils
from jax3d.projects.nesf.nerfstatic.utils import types
from jax3d.projects.nesf.utils.typing import PRNGKey, Tree, f32, i32  # pylint: disable=g-multiple-import

def train(
    params: nerf_config.ConfigParams,
    xm_wid: Optional[int] = None,
):
  """Semantic train loop.

  Args:
    params: All training parameters (e.g. train, model, dataset, loss params).
    xm_wid: XManager worker id. Only needed when running under XManager.
  """

  del xm_wid

  rng = jax3d.RandomState(params.train.random_seed)

  # Initialize dataset. Ensure that the model and the dataaset agree on the
  # number of semantic classes. It is assumed that all scenes share the same
  # set of semantic classes.
  logging.info("Loading dataset.")
  dataset, placeholder_batch = _construct_dataset(params)
  train_utils.assert_num_semantic_classes(
      params.models.num_semantic_classes, dataset.all_metadata[0].labels)

  # Initialize & load per-scene NeRF models.
  logging.info("Initializing pretrained NeRF models.")
  recovered_nerf_state = semantic_utils.load_all_nerf_variables(
      save_dir=params.train.nerf_model_ckpt,
      train_dataset=dataset,
      novel_dataset=None,
      recompute_sigma_grid_opts=(
          semantic_utils.RecomputeSigmaGridOptions.from_params(params.train)))

  logging.info("nerf scene_ids: %s", recovered_nerf_state.train_scene_ids)

  # Initialize semantic model.
  logging.info("Initializing semantic model.")
  initialized_vol_sem_model = models.construct_volumetric_semantic_model(
      rng=rng,
      num_scenes=-1,
      placeholder_batch=placeholder_batch,
      args=params.models,
      nerf_model=recovered_nerf_state.model,
      nerf_sigma_grid=recovered_nerf_state.train_sigma_grids[0],
      nerf_variables=recovered_nerf_state.train_variables[0])
  vol_sem_model = initialized_vol_sem_model.model
  semantic_variables = initialized_vol_sem_model.variables
  del initialized_vol_sem_model

  # Write HParams to disk.
  summary_writer = jax_process_zero.SummaryWriter(params.train.train_dir /
                                                  "train")
  summary_writer.text(
      "model_config",
      # Avoid markdown interpretation.
      f"<pre>{vol_sem_model}</pre>",
      step=0,
  )
  summary_writer.text(
      "params",
      # Use ``` for markdown.
      f"```python\n{params.to_gin_operative_repr()}\n```",  # pytype: disable=attribute-error
      step=0,
  )

  # Initialize optimizer.
  optimizer = flax.optim.Adam().create(semantic_variables)
  state = utils.TrainState(optimizer=optimizer)
  del optimizer

  # Function for generating learning rate.
  learning_rate_fn = functools.partial(
      utils.learning_rate_decay,
      lr_init=params.train.lr_init,
      lr_final=params.train.lr_final,
      max_steps=train_utils.get_max_steps(params),
      lr_delay_steps=params.train.lr_delay_steps,
      lr_delay_mult=params.train.lr_delay_mult)

  # Restore model checkpoint from disk.
  logging.info("Restoring optimizer state.")
  checkpoint_dir = train_utils.checkpoint_dir(params)
  state = train_utils.restore_opt_checkpoint(
      save_dir=checkpoint_dir, state=state)
  start_step = state.optimizer.state.step

  # Replicate state across local devices.
  state = flax.jax_utils.replicate(state)

  # Build distributed computation.
  logging.info("Compiling train_pstep.")
  train_pstep = jax.pmap(
      functools.partial(_apply_train_step, vol_sem_model, params),
      axis_name="batch",
      in_axes=(0, 0, 0, 0, 0, None, 0),
  )

  logging.info("Starting training now.")
  for step, (_, batch) in zip(
      range(start_step, params.train.train_steps+1), iter(dataset)):
    # Given a device (axis=0) and scene (axis=1), all values for
    # property "scene_id" are identical. Choose the first one.
    batch_scene_ids = jax.tree_map(lambda x: x[:, :, 0, 0],
                                   batch.target_view.rays.scene_id)
    batch_scene_ids = batch_scene_ids.flatten()

    # Select NeRF variables corresponding to chosen scenes.
    select_nerf_variables = semantic_utils.select_and_stack(
        batch_scene_ids, recovered_nerf_state.train_variables)
    select_nerf_sigma_grids = semantic_utils.select_and_stack(
        batch_scene_ids, recovered_nerf_state.train_sigma_grids)
    select_nerf_scene_ids = semantic_utils.select_and_stack(
        batch_scene_ids, recovered_nerf_state.train_scene_ids)

    # Update random state.
    keys = rng.next(jax.local_device_count())

    train_pstep_outputs = train_pstep(keys, state, select_nerf_variables,
                                      select_nerf_sigma_grids,
                                      batch.target_view, learning_rate_fn(step),
                                      select_nerf_scene_ids)
    state = train_pstep_outputs.train_state
    stats = train_pstep_outputs.stats

    # Write model checkpoint to disk. Model state is "de-replicated" before
    # writing to ensure that only one copy of each variable is stored on disk.
    if train_utils.should_execute_now(step, params.train.save_every):
      logging.info("Writing checkpoint to disk.")
      state_to_save = train_utils.de_replicate(state)
      train_utils.save_checkpoints_for_process(
          model_state=state_to_save,
          ds_state=None,
          step=step,
          save_dir=checkpoint_dir,
          overwrite=params.train.overwrite_checkpoints)

    # All quantities have shape [num_local_devices, num_scenes_per_batch].
    # Logic below averages them together.
    if train_utils.should_execute_now(step, params.train.print_every):
      logging.info("Step: %d", step)
      logging.info("Processing unique scene_ids: %s",
                   jnp.unique(batch_scene_ids))
      logging.info("Semantic Loss: %s",
                   stats.semantic_model.semantic_loss.loss)
      logging.info("Smoothness Regularization Loss: %s",
                   stats.semantic_model.smoothness_regularization_loss.loss)
      logging.info("mIOU: %s", stats.semantic_model.mean_iou)
      logging.info("batch scene ids: %s", batch_scene_ids)
      logging.info("select nerf scene ids: %s", select_nerf_scene_ids)
      logging.info(
          ("Percentage of rays where batch's scene_id matches NeRF's scene_id: "
           "%s"),
          stats.semantic_model.percent_matching_scene_ids)
      train_utils.log_rays_per_scene_counts(batch.target_view.rays.scene_id)
      train_utils.log_rays_per_semantic_class_counts(
          batch.target_view.semantics, dataset.semantic_labels)
      if params.train.log_per_scene:
        train_utils.log_loss_miou_per_scene(
            select_nerf_scene_ids, stats, summary_writer, step)

      summary_writer.scalar(
          "loss/total",
          stats.semantic_model.total,
          step)
      summary_writer.scalar(
          "loss/semantic",
          jnp.mean(stats.semantic_model.semantic_loss.loss),
          step)
      summary_writer.scalar(
          "loss/smoothness_regularization",
          jnp.mean(stats.semantic_model.smoothness_regularization_loss.loss),
          step)
      summary_writer.scalar(
          "metrics/mean_iou_2d",
          jnp.mean(stats.semantic_model.mean_iou),
          step)
      summary_writer.scalar(
          "debug/percent_matching_scene_ids",
          jnp.mean(stats.semantic_model.percent_matching_scene_ids),
          step)

################################################################################
# Internal helper functions.
################################################################################

def _construct_dataset(
    params: nerf_config.ConfigParams,
) -> Tuple[datasets.DatasetIterable, types.Batch]:
  """Constructs a dataset and placeholder batch.

  Args:
    params: Hyperparameters.

  Returns:
    dataset: Dataset itself.
    placeholder_batch: Placeholder batch used for initializing a
      VolumetricSemanticModel.
  """

  dataset = datasets.get_dataset(
      split="train",
      args=params.datasets,
      model_args=params.models,
      example_type=datasets.ExampleType.RAY,
      ds_state=None,
      is_novel_scenes=False,
  )

  # Select device 0, scene 0 from the placeholder batch. Later code will
  # use pmap and vmap, respectively, across these two axes.
  _, placeholder_batch = dataset.peek()
  placeholder_batch = jax.tree_map(lambda t: t[0, 0, ...], placeholder_batch)

  return dataset, placeholder_batch

def _apply_forward_pass_single_scene(
    rng: PRNGKey,
    params: nerf_config.ConfigParams,
    nerf_variables: Tree[jnp.ndarray],
    nerf_sigma_grid: f32["x y z c"],
    semantic_model: volumetric_semantic_model.VolumetricSemanticModel,
    semantic_variables: Tree[jnp.ndarray],
    batch: types.Views,
    nerf_scene_id: i32[""]) -> types.SemanticModelStats:
  """Calculate the loss for all rays corresponding to a single scene.

  Args:
    rng: Random key.
    params: All Train, Loss, Dataset and Model parameters.
    nerf_variables: Variables for NerfMLP describing a single scene.
    nerf_sigma_grid: Density grid corresponding to the same scene.
    semantic_model: Architecture of the SemanticModel we want to learn.
    semantic_variables: Variables corresponding to semantic_model.
    batch: Batch of rays from the same scene.
    nerf_scene_id: Scene id of current scene's nerf variables.

  Returns:
    types.SemanticModelStats containing the metrics and losses.
  """

  rng_names = ["params", "sampling", "data_augmentation"]
  rng, rng_smoothness, *rng_keys = jax.random.split(rng, len(rng_names) + 2)

  is_smoothness_regularization_enabled = (
      params.train.semantic_smoothness_regularization_num_points_per_device > 0)

  # Construct points for smoothness regularization.
  smoothness_regularization_sample_points = None
  if is_smoothness_regularization_enabled:
    smoothness_regularization_sample_points = (
        train_utils.create_sample_points_for_smoothness_regularization(
            scene_id=nerf_scene_id,
            num_points=params.train.semantic_smoothness_regularization_num_points_per_device,  # pylint: disable=line-too-long
            stddev=params.train.semantic_smoothness_regularization_stddev,
            rng=rng_smoothness))

  # Apply model.
  result = semantic_model.apply(
      semantic_variables,
      rngs=dict(zip(rng_names, rng_keys)),
      rays=batch.rays,
      sigma_grid=nerf_sigma_grid,
      randomized_sampling=True,
      is_train=True,
      nerf_model_weights=nerf_variables,
      points=smoothness_regularization_sample_points)

  if is_smoothness_regularization_enabled:
    rendered_rays, semantic_3d_predictions = result
  else:
    rendered_rays, semantic_3d_predictions = result, None
  assert isinstance(rendered_rays, types.RenderedRays)

  # Compute cross entropy loss
  softmax_cross_entropy_loss = losses.softmax_cross_entropy_loss(
      logits=rendered_rays.semantic, labels=batch["semantics"])

  semantic_loss_term = types.LossTerm(
      loss=softmax_cross_entropy_loss, weight=params.train.semantic_weight)

  # Compute the mean_iou
  mean_iou = utils.compute_iou_from_preds(
      logits=rendered_rays.semantic,
      labels=batch["semantics"],
      num_classes=params.models.num_semantic_classes).mean_iou

  # Compute smoothness regularization
  if is_smoothness_regularization_enabled:
    chex.assert_shape(
        semantic_3d_predictions,
        (2,
         params.train.semantic_smoothness_regularization_num_points_per_device,
         params.models.num_semantic_classes))
    smoothness_regularization_loss = losses.l1_smoothness_regularization(
        semantic_3d_predictions[0],  # activations at original points
        semantic_3d_predictions[1])  # activations at perturbed points
  else:
    smoothness_regularization_loss = jnp.array(0.0)

  smoothness_regularization_term = types.LossTerm(
      loss=smoothness_regularization_loss,
      weight=params.train.semantic_smoothness_regularization_weight)

  # Ensure that NeRF and batch agree on scene_id. This quantity should always
  # be 1.0.
  percent_matching_scene_ids = jnp.mean(
      jnp.equal(batch.rays.scene_id, nerf_scene_id))

  return types.SemanticModelStats(
      semantic_loss=semantic_loss_term,
      smoothness_regularization_loss=smoothness_regularization_term,
      mean_iou=mean_iou,
      percent_matching_scene_ids=percent_matching_scene_ids)

# Use vmap to map over scenes.
_apply_forward_pass_per_device = jax.vmap(
    _apply_forward_pass_single_scene, in_axes=(None, None, 0, 0,
                                               None, None, 0, 0))

def _apply_train_step(
    semantic_model: volumetric_semantic_model.VolumetricSemanticModel,
    params: nerf_config.ConfigParams,
    rng: jax3d.RandomState,
    state: utils.TrainState,
    select_nerf_variables: Tree[jnp.ndarray],
    select_nerf_sigma_grids: f32["n x y z c"],
    select_batch: types.Views,
    learning_rate: float,
    select_nerf_scene_ids: i32["n"],
) -> train_utils.TrainStepOutput:
  """Apply a single training step.

  Update semantic_variables.

  Args:
    semantic_model: Semantic model architecture.
    params: All Train, Loss, Dataset and Model parameters.
    rng: Random seed.
    state: State of the model/optimizer.
    select_nerf_variables: Variables for NerfMLPs.
    select_nerf_sigma_grids: Density grids derived from NerfMLPs.
    select_batch: Camera rays.
    learning_rate: Learning rate for decoders for this step.
    select_nerf_scene_ids: Scene ids for this device.

  Returns:
    Updated train state and statistics.
  """

  def loss_fn(semantic_variables):
    # semantic_model_stats will have arrays of shape
    # [num_scenes_per_device, ...] thanks to vmap().
    semantic_model_stats = _apply_forward_pass_per_device(
        rng, params, select_nerf_variables, select_nerf_sigma_grids,
        semantic_model, semantic_variables, select_batch, select_nerf_scene_ids)

    # L2 regularization. This quantity is the same across all devices.
    regularization_weight_l2 = losses.l2_regularization(semantic_variables)
    assert not regularization_weight_l2.shape, regularization_weight_l2.shape
    regularization_term = types.LossTerm(
        loss=regularization_weight_l2, weight=params.train.weight_decay_mult)

    stats = utils.SemanticStats(semantic_model=semantic_model_stats,
                                regularization=regularization_term)

    # Average loss across all scenes on this device.
    loss = stats.total
    assert not loss.shape, loss.shape

    return loss, stats

  (_, stats), grads = jax.value_and_grad(
      loss_fn, has_aux=True)(state.optimizer.target)
  grads = jax.lax.pmean(grads, axis_name="batch")
  new_optimizer = state.optimizer.apply_gradient(
      grads, learning_rate=learning_rate)
  new_state = state.replace(optimizer=new_optimizer)
  return train_utils.TrainStepOutput(train_state=new_state, stats=stats)


text:
import updatePerpendicularLine from './updatePerpendicularLine';

function createPoint(x, y) {
  return {
    x,
    y,
  };
}

describe('updatePerpendicularLine.js', () => {
  it('line has points in expected position', () => {
    const columnPixelSpacing = 1;
    const rowPixelSpacing = 0.5;
    const start = createPoint(2, 3);
    const perpendicularStart = createPoint(1, 0);
    const perpendicularEnd = createPoint(2, 8);
    const fixedPoint = perpendicularStart;
    const intersection = createPoint(4, 4);
    const mid = createPoint(5, 5);

    const baseData = {
      columnPixelSpacing,
      rowPixelSpacing,
      start,
      perpendicularStart,
      perpendicularEnd,
      intersection,
      fixedPoint,
    };

    const newLine = updatePerpendicularLine(baseData, mid);

    expect(newLine.start.x).toBeCloseTo(6.910938354123028);
    expect(newLine.start.y).toBeCloseTo(-1.1150027331936894);
    expect(newLine.end.x).toBeCloseTo(3.500936622008277);
    expect(newLine.end.y).toBeCloseTo(9.797002809573513);
  });
});